EventLoop.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Clipboard.h"
  27. #include <Kernel/KeyCode.h>
  28. #include <Kernel/MousePacket.h>
  29. #include <LibCore/LocalSocket.h>
  30. #include <LibCore/Object.h>
  31. #include <WindowServer/ClientConnection.h>
  32. #include <WindowServer/Cursor.h>
  33. #include <WindowServer/Event.h>
  34. #include <WindowServer/EventLoop.h>
  35. #include <WindowServer/Screen.h>
  36. #include <WindowServer/WindowManager.h>
  37. #include <errno.h>
  38. #include <fcntl.h>
  39. #include <stdio.h>
  40. #include <sys/select.h>
  41. #include <sys/socket.h>
  42. #include <sys/time.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. //#define WSMESSAGELOOP_DEBUG
  46. namespace WindowServer {
  47. EventLoop::EventLoop()
  48. : m_server(Core::LocalServer::construct())
  49. {
  50. m_keyboard_fd = open("/dev/keyboard", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  51. m_mouse_fd = open("/dev/mouse", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  52. bool ok = m_server->take_over_from_system_server();
  53. ASSERT(ok);
  54. m_server->on_ready_to_accept = [this] {
  55. auto client_socket = m_server->accept();
  56. if (!client_socket) {
  57. dbg() << "WindowServer: accept failed.";
  58. return;
  59. }
  60. static int s_next_client_id = 0;
  61. int client_id = ++s_next_client_id;
  62. IPC::new_client_connection<ClientConnection>(*client_socket, client_id);
  63. };
  64. ASSERT(m_keyboard_fd >= 0);
  65. ASSERT(m_mouse_fd >= 0);
  66. m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Read);
  67. m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
  68. m_mouse_notifier = Core::Notifier::construct(m_mouse_fd, Core::Notifier::Read);
  69. m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); };
  70. Clipboard::the().on_content_change = [&] {
  71. ClientConnection::for_each_client([&](auto& client) {
  72. client.notify_about_clipboard_contents_changed();
  73. });
  74. };
  75. }
  76. EventLoop::~EventLoop()
  77. {
  78. }
  79. void EventLoop::drain_mouse()
  80. {
  81. auto& screen = Screen::the();
  82. MousePacket state;
  83. state.buttons = screen.mouse_button_state();
  84. unsigned buttons = state.buttons;
  85. MousePacket packets[32];
  86. ssize_t nread = read(m_mouse_fd, &packets, sizeof(packets));
  87. if (nread < 0) {
  88. perror("EventLoop::drain_mouse read");
  89. return;
  90. }
  91. size_t npackets = nread / sizeof(MousePacket);
  92. if (!npackets)
  93. return;
  94. for (size_t i = 0; i < npackets; ++i) {
  95. auto& packet = packets[i];
  96. #ifdef WSMESSAGELOOP_DEBUG
  97. dbgprintf("EventLoop: Mouse X %d, Y %d, Z %d, relative %d\n", packet.x, packet.y, packet.z, packet.is_relative);
  98. #endif
  99. buttons = packet.buttons;
  100. state.is_relative = packet.is_relative;
  101. if (packet.is_relative) {
  102. state.x += packet.x;
  103. state.y -= packet.y;
  104. state.z += packet.z;
  105. } else {
  106. state.x = packet.x;
  107. state.y = packet.y;
  108. state.z += packet.z;
  109. }
  110. if (buttons != state.buttons) {
  111. state.buttons = buttons;
  112. #ifdef WSMESSAGELOOP_DEBUG
  113. dbgprintf("EventLoop: Mouse Button Event\n");
  114. #endif
  115. screen.on_receive_mouse_data(state);
  116. if (state.is_relative) {
  117. state.x = 0;
  118. state.y = 0;
  119. state.z = 0;
  120. }
  121. }
  122. }
  123. if (state.is_relative && (state.x || state.y || state.z))
  124. screen.on_receive_mouse_data(state);
  125. if (!state.is_relative)
  126. screen.on_receive_mouse_data(state);
  127. }
  128. void EventLoop::drain_keyboard()
  129. {
  130. auto& screen = Screen::the();
  131. for (;;) {
  132. ::KeyEvent event;
  133. ssize_t nread = read(m_keyboard_fd, (u8*)&event, sizeof(::KeyEvent));
  134. if (nread == 0)
  135. break;
  136. ASSERT(nread == sizeof(::KeyEvent));
  137. screen.on_receive_keyboard_data(event);
  138. }
  139. }
  140. }