2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-01-24 11:28:26 -03:00
|
|
|
#include <AK/Debug.h>
|
2020-07-04 12:22:23 -03:00
|
|
|
#include <Kernel/API/MousePacket.h>
|
2022-02-25 07:18:30 -03:00
|
|
|
#include <WindowServer/ConnectionFromClient.h>
|
2020-02-06 16:03:37 -03:00
|
|
|
#include <WindowServer/Cursor.h>
|
|
|
|
|
#include <WindowServer/EventLoop.h>
|
|
|
|
|
#include <WindowServer/Screen.h>
|
2022-02-25 07:18:30 -03:00
|
|
|
#include <WindowServer/WMConnectionFromClient.h>
|
2020-02-06 16:03:37 -03:00
|
|
|
#include <WindowServer/WindowManager.h>
|
2019-06-07 06:47:19 -03:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
2019-04-14 00:15:22 -03:00
|
|
|
#include <unistd.h>
|
2019-01-16 13:03:50 -02:00
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
namespace WindowServer {
|
|
|
|
|
|
|
|
|
|
EventLoop::EventLoop()
|
2019-01-16 13:03:50 -02:00
|
|
|
{
|
2022-09-10 06:25:20 -03:00
|
|
|
m_keyboard_fd = open("/dev/input/keyboard/0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
|
|
|
|
m_mouse_fd = open("/dev/input/mouse/0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
2019-01-16 14:20:58 -02:00
|
|
|
|
2022-02-25 07:18:30 -03:00
|
|
|
m_window_server = MUST(IPC::MultiServer<ConnectionFromClient>::try_create("/tmp/portal/window"));
|
|
|
|
|
m_wm_server = MUST(IPC::MultiServer<WMConnectionFromClient>::try_create("/tmp/portal/wm"));
|
2021-04-14 18:38:53 -03:00
|
|
|
|
2021-01-24 15:15:13 -03:00
|
|
|
if (m_keyboard_fd >= 0) {
|
|
|
|
|
m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Read);
|
|
|
|
|
m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
|
|
|
|
|
} else {
|
2022-09-10 06:25:20 -03:00
|
|
|
dbgln("Couldn't open /dev/input/keyboard/0");
|
2021-01-24 15:15:13 -03:00
|
|
|
}
|
2019-07-17 06:09:09 -03:00
|
|
|
|
2021-01-24 15:15:13 -03:00
|
|
|
if (m_mouse_fd >= 0) {
|
|
|
|
|
m_mouse_notifier = Core::Notifier::construct(m_mouse_fd, Core::Notifier::Read);
|
|
|
|
|
m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); };
|
|
|
|
|
} else {
|
2022-09-10 06:25:20 -03:00
|
|
|
dbgln("Couldn't open /dev/input/mouse/0");
|
2021-01-24 15:15:13 -03:00
|
|
|
}
|
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
|
|
|
{
|
2021-06-13 09:16:06 -03:00
|
|
|
auto& screen_input = ScreenInput::the();
|
2020-02-03 13:30:35 -03:00
|
|
|
MousePacket state;
|
2021-06-13 09:16:06 -03:00
|
|
|
state.buttons = screen_input.mouse_button_state();
|
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;
|
2021-10-24 12:51:29 -03:00
|
|
|
|
|
|
|
|
bool state_is_sent = false;
|
2020-02-07 07:30:11 -03:00
|
|
|
for (size_t i = 0; i < npackets; ++i) {
|
|
|
|
|
auto& packet = packets[i];
|
2021-12-13 19:12:01 -03:00
|
|
|
dbgln_if(WSMESSAGELOOP_DEBUG, "EventLoop: Mouse X {}, Y {}, Z {}, W {}, relative={}", packet.x, packet.y, packet.z, packet.w, packet.is_relative);
|
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;
|
|
|
|
|
} else {
|
|
|
|
|
state.x = packet.x;
|
|
|
|
|
state.y = packet.y;
|
|
|
|
|
}
|
2021-10-24 12:46:29 -03:00
|
|
|
state.z += packet.z;
|
2021-12-13 19:12:01 -03:00
|
|
|
state.w += packet.w;
|
2021-10-24 12:51:29 -03:00
|
|
|
state_is_sent = false;
|
2020-02-03 13:30:35 -03:00
|
|
|
|
2021-10-24 12:44:30 -03:00
|
|
|
if (packet.buttons != state.buttons) {
|
|
|
|
|
state.buttons = packet.buttons;
|
2021-05-01 16:10:08 -03:00
|
|
|
dbgln_if(WSMESSAGELOOP_DEBUG, "EventLoop: Mouse Button Event");
|
2021-10-24 23:47:33 -03:00
|
|
|
|
|
|
|
|
// Swap primary (1) and secondary (2) buttons if checked in Settings.
|
|
|
|
|
// Doing the swap here avoids all emulator and hardware issues.
|
|
|
|
|
if (WindowManager::the().get_buttons_switched()) {
|
|
|
|
|
bool has_primary = state.buttons & MousePacket::Button::LeftButton;
|
|
|
|
|
bool has_secondary = state.buttons & MousePacket::Button::RightButton;
|
|
|
|
|
state.buttons = state.buttons & ~(MousePacket::Button::LeftButton | MousePacket::Button::RightButton);
|
|
|
|
|
// Invert the buttons:
|
|
|
|
|
if (has_primary)
|
|
|
|
|
state.buttons |= MousePacket::Button::RightButton;
|
|
|
|
|
if (has_secondary)
|
|
|
|
|
state.buttons |= MousePacket::Button::LeftButton;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
screen_input.on_receive_mouse_data(state);
|
2021-10-24 12:51:29 -03:00
|
|
|
state_is_sent = true;
|
2020-02-03 21:38:50 -03:00
|
|
|
if (state.is_relative) {
|
|
|
|
|
state.x = 0;
|
|
|
|
|
state.y = 0;
|
|
|
|
|
state.z = 0;
|
2021-12-13 19:12:01 -03:00
|
|
|
state.w = 0;
|
2020-02-03 21:38:50 -03:00
|
|
|
}
|
2019-01-16 13:03:50 -02:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-24 12:51:29 -03:00
|
|
|
if (state_is_sent)
|
|
|
|
|
return;
|
2021-12-13 19:12:01 -03:00
|
|
|
if (state.is_relative && (state.x || state.y || state.z || state.w))
|
2021-06-13 09:16:06 -03:00
|
|
|
screen_input.on_receive_mouse_data(state);
|
2020-02-03 21:38:50 -03:00
|
|
|
if (!state.is_relative)
|
2021-06-13 09:16:06 -03:00
|
|
|
screen_input.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
|
|
|
{
|
2021-06-13 09:16:06 -03:00
|
|
|
auto& screen_input = ScreenInput::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;
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(nread == sizeof(::KeyEvent));
|
2021-06-13 09:16:06 -03:00
|
|
|
screen_input.on_receive_keyboard_data(event);
|
2019-01-16 14:20:58 -02:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-06 16:03:37 -03:00
|
|
|
|
|
|
|
|
}
|