Serenity Operating System
at portability 157 lines 5.2 kB view raw
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 27#include "Clipboard.h" 28#include <Kernel/KeyCode.h> 29#include <Kernel/MousePacket.h> 30#include <LibCore/LocalSocket.h> 31#include <LibCore/Object.h> 32#include <WindowServer/ClientConnection.h> 33#include <WindowServer/Cursor.h> 34#include <WindowServer/Event.h> 35#include <WindowServer/EventLoop.h> 36#include <WindowServer/Screen.h> 37#include <WindowServer/WindowManager.h> 38#include <errno.h> 39#include <fcntl.h> 40#include <stdio.h> 41#include <sys/select.h> 42#include <sys/socket.h> 43#include <sys/time.h> 44#include <time.h> 45#include <unistd.h> 46 47//#define WSMESSAGELOOP_DEBUG 48 49namespace WindowServer { 50 51EventLoop::EventLoop() 52 : m_server(Core::LocalServer::construct()) 53{ 54 m_keyboard_fd = open("/dev/keyboard", O_RDONLY | O_NONBLOCK | O_CLOEXEC); 55 m_mouse_fd = open("/dev/mouse", O_RDONLY | O_NONBLOCK | O_CLOEXEC); 56 57 bool ok = m_server->take_over_from_system_server(); 58 ASSERT(ok); 59 60 m_server->on_ready_to_accept = [this] { 61 auto client_socket = m_server->accept(); 62 if (!client_socket) { 63 dbg() << "WindowServer: accept failed."; 64 return; 65 } 66 static int s_next_client_id = 0; 67 int client_id = ++s_next_client_id; 68 IPC::new_client_connection<ClientConnection>(*client_socket, client_id); 69 }; 70 71 ASSERT(m_keyboard_fd >= 0); 72 ASSERT(m_mouse_fd >= 0); 73 74 m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Read); 75 m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); }; 76 77 m_mouse_notifier = Core::Notifier::construct(m_mouse_fd, Core::Notifier::Read); 78 m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); }; 79 80 Clipboard::the().on_content_change = [&] { 81 ClientConnection::for_each_client([&](auto& client) { 82 client.notify_about_clipboard_contents_changed(); 83 }); 84 }; 85} 86 87EventLoop::~EventLoop() 88{ 89} 90 91void EventLoop::drain_mouse() 92{ 93 auto& screen = Screen::the(); 94 MousePacket state; 95 state.buttons = screen.mouse_button_state(); 96 unsigned buttons = state.buttons; 97 MousePacket packets[32]; 98 99 ssize_t nread = read(m_mouse_fd, &packets, sizeof(packets)); 100 if (nread < 0) { 101 perror("EventLoop::drain_mouse read"); 102 return; 103 } 104 size_t npackets = nread / sizeof(MousePacket); 105 if (!npackets) 106 return; 107 for (size_t i = 0; i < npackets; ++i) { 108 auto& packet = packets[i]; 109#ifdef WSMESSAGELOOP_DEBUG 110 dbgprintf("EventLoop: Mouse X %d, Y %d, Z %d, relative %d\n", packet.x, packet.y, packet.z, packet.is_relative); 111#endif 112 buttons = packet.buttons; 113 114 state.is_relative = packet.is_relative; 115 if (packet.is_relative) { 116 state.x += packet.x; 117 state.y -= packet.y; 118 state.z += packet.z; 119 } else { 120 state.x = packet.x; 121 state.y = packet.y; 122 state.z += packet.z; 123 } 124 125 if (buttons != state.buttons) { 126 state.buttons = buttons; 127#ifdef WSMESSAGELOOP_DEBUG 128 dbgprintf("EventLoop: Mouse Button Event\n"); 129#endif 130 screen.on_receive_mouse_data(state); 131 if (state.is_relative) { 132 state.x = 0; 133 state.y = 0; 134 state.z = 0; 135 } 136 } 137 } 138 if (state.is_relative && (state.x || state.y || state.z)) 139 screen.on_receive_mouse_data(state); 140 if (!state.is_relative) 141 screen.on_receive_mouse_data(state); 142} 143 144void EventLoop::drain_keyboard() 145{ 146 auto& screen = Screen::the(); 147 for (;;) { 148 ::KeyEvent event; 149 ssize_t nread = read(m_keyboard_fd, (u8*)&event, sizeof(::KeyEvent)); 150 if (nread == 0) 151 break; 152 ASSERT(nread == sizeof(::KeyEvent)); 153 screen.on_receive_keyboard_data(event); 154 } 155} 156 157}