2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
#include "Clipboard.h"
|
2019-06-07 06:47:19 -03:00
|
|
|
#include <Kernel/KeyCode.h>
|
|
|
|
|
#include <Kernel/MousePacket.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/LocalSocket.h>
|
|
|
|
|
#include <LibCore/Object.h>
|
2020-02-06 16:03:37 -03:00
|
|
|
#include <WindowServer/ClientConnection.h>
|
|
|
|
|
#include <WindowServer/Cursor.h>
|
|
|
|
|
#include <WindowServer/Event.h>
|
|
|
|
|
#include <WindowServer/EventLoop.h>
|
|
|
|
|
#include <WindowServer/Screen.h>
|
|
|
|
|
#include <WindowServer/WindowManager.h>
|
2019-06-07 06:47:19 -03:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
2019-04-14 00:15:22 -03:00
|
|
|
#include <sys/select.h>
|
2019-06-07 06:47:19 -03:00
|
|
|
#include <sys/socket.h>
|
2019-04-14 00:15:22 -03:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <unistd.h>
|
2019-01-16 13:03:50 -02:00
|
|
|
|
2019-03-01 06:51:58 -03:00
|
|
|
//#define WSMESSAGELOOP_DEBUG
|
2019-01-16 13:03:50 -02:00
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
namespace WindowServer {
|
|
|
|
|
|
|
|
|
|
EventLoop::EventLoop()
|
2020-02-02 08:34:39 -03:00
|
|
|
: m_server(Core::LocalServer::construct())
|
2019-01-16 13:03:50 -02:00
|
|
|
{
|
2019-02-16 21:13:47 -02:00
|
|
|
m_keyboard_fd = open("/dev/keyboard", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
2020-02-18 10:30:39 -03:00
|
|
|
m_mouse_fd = open("/dev/mouse", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
2019-01-16 14:20:58 -02:00
|
|
|
|
2019-11-26 13:30:35 -03:00
|
|
|
bool ok = m_server->take_over_from_system_server();
|
|
|
|
|
ASSERT(ok);
|
2019-01-16 14:20:58 -02:00
|
|
|
|
2019-09-21 05:46:55 -03:00
|
|
|
m_server->on_ready_to_accept = [this] {
|
|
|
|
|
auto client_socket = m_server->accept();
|
2019-07-27 05:53:50 -03:00
|
|
|
if (!client_socket) {
|
|
|
|
|
dbg() << "WindowServer: accept failed.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
static int s_next_client_id = 0;
|
|
|
|
|
int client_id = ++s_next_client_id;
|
2020-02-06 16:03:37 -03:00
|
|
|
IPC::new_client_connection<ClientConnection>(*client_socket, client_id);
|
2019-07-27 05:53:50 -03:00
|
|
|
};
|
2019-02-14 14:18:35 -02:00
|
|
|
|
2019-01-16 14:20:58 -02:00
|
|
|
ASSERT(m_keyboard_fd >= 0);
|
|
|
|
|
ASSERT(m_mouse_fd >= 0);
|
2019-07-13 14:42:03 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Read);
|
2019-07-17 06:09:09 -03:00
|
|
|
m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
m_mouse_notifier = Core::Notifier::construct(m_mouse_fd, Core::Notifier::Read);
|
2019-07-17 06:09:09 -03:00
|
|
|
m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); };
|
2019-09-14 04:19:05 -03:00
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
Clipboard::the().on_content_change = [&] {
|
|
|
|
|
ClientConnection::for_each_client([&](auto& client) {
|
2019-09-14 04:19:05 -03:00
|
|
|
client.notify_about_clipboard_contents_changed();
|
|
|
|
|
});
|
|
|
|
|
};
|
2019-02-13 06:10:32 -02:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
EventLoop::~EventLoop()
|
2019-02-13 06:10:32 -02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
void EventLoop::drain_mouse()
|
2019-01-16 13:03:50 -02:00
|
|
|
{
|
2020-02-06 16:03:37 -03:00
|
|
|
auto& screen = Screen::the();
|
2020-02-03 13:30:35 -03:00
|
|
|
MousePacket state;
|
|
|
|
|
state.buttons = screen.mouse_button_state();
|
|
|
|
|
unsigned buttons = state.buttons;
|
2020-02-07 07:30:11 -03:00
|
|
|
MousePacket packets[32];
|
|
|
|
|
|
|
|
|
|
ssize_t nread = read(m_mouse_fd, &packets, sizeof(packets));
|
|
|
|
|
if (nread < 0) {
|
|
|
|
|
perror("EventLoop::drain_mouse read");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
size_t npackets = nread / sizeof(MousePacket);
|
|
|
|
|
if (!npackets)
|
|
|
|
|
return;
|
|
|
|
|
for (size_t i = 0; i < npackets; ++i) {
|
|
|
|
|
auto& packet = packets[i];
|
2020-02-03 21:38:50 -03:00
|
|
|
#ifdef WSMESSAGELOOP_DEBUG
|
2020-02-06 16:03:37 -03:00
|
|
|
dbgprintf("EventLoop: Mouse X %d, Y %d, Z %d, relative %d\n", packet.x, packet.y, packet.z, packet.is_relative);
|
2020-02-03 21:38:50 -03:00
|
|
|
#endif
|
2019-03-05 10:11:46 -03:00
|
|
|
buttons = packet.buttons;
|
2019-02-07 05:07:37 -02:00
|
|
|
|
2020-02-03 21:38:50 -03:00
|
|
|
state.is_relative = packet.is_relative;
|
2020-02-03 13:30:35 -03:00
|
|
|
if (packet.is_relative) {
|
|
|
|
|
state.x += packet.x;
|
|
|
|
|
state.y -= packet.y;
|
|
|
|
|
state.z += packet.z;
|
|
|
|
|
} else {
|
|
|
|
|
state.x = packet.x;
|
|
|
|
|
state.y = packet.y;
|
|
|
|
|
state.z += packet.z;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buttons != state.buttons) {
|
|
|
|
|
state.buttons = buttons;
|
2020-02-03 21:38:50 -03:00
|
|
|
#ifdef WSMESSAGELOOP_DEBUG
|
2020-02-06 16:03:37 -03:00
|
|
|
dbgprintf("EventLoop: Mouse Button Event\n");
|
2020-02-03 21:38:50 -03:00
|
|
|
#endif
|
2020-02-03 13:30:35 -03:00
|
|
|
screen.on_receive_mouse_data(state);
|
2020-02-03 21:38:50 -03:00
|
|
|
if (state.is_relative) {
|
|
|
|
|
state.x = 0;
|
|
|
|
|
state.y = 0;
|
|
|
|
|
state.z = 0;
|
|
|
|
|
}
|
2019-01-16 13:03:50 -02:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-03 13:30:35 -03:00
|
|
|
if (state.is_relative && (state.x || state.y || state.z))
|
|
|
|
|
screen.on_receive_mouse_data(state);
|
2020-02-03 21:38:50 -03:00
|
|
|
if (!state.is_relative)
|
|
|
|
|
screen.on_receive_mouse_data(state);
|
2019-01-16 13:03:50 -02:00
|
|
|
}
|
2019-01-16 14:20:58 -02:00
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
void EventLoop::drain_keyboard()
|
2019-01-16 14:20:58 -02:00
|
|
|
{
|
2020-02-06 16:03:37 -03:00
|
|
|
auto& screen = Screen::the();
|
2019-02-16 21:13:47 -02:00
|
|
|
for (;;) {
|
2020-02-06 16:03:37 -03:00
|
|
|
::KeyEvent event;
|
2020-02-06 20:01:28 -03:00
|
|
|
ssize_t nread = read(m_keyboard_fd, (u8*)&event, sizeof(::KeyEvent));
|
2019-02-16 21:13:47 -02:00
|
|
|
if (nread == 0)
|
|
|
|
|
break;
|
2020-02-06 16:21:34 -03:00
|
|
|
ASSERT(nread == sizeof(::KeyEvent));
|
2019-01-21 04:05:31 -02:00
|
|
|
screen.on_receive_keyboard_data(event);
|
2019-01-16 14:20:58 -02:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-06 16:03:37 -03:00
|
|
|
|
|
|
|
|
}
|