Serenity Operating System
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 "Screen.h"
28#include "Compositor.h"
29#include "Event.h"
30#include "EventLoop.h"
31#include "WindowManager.h"
32#ifdef __serenity__
33#include <Kernel/FB.h>
34#elif defined(__OpenBSD__)
35#include <LibSerenity/FB.h>
36#endif
37#include <Kernel/MousePacket.h>
38#include <fcntl.h>
39#include <stdio.h>
40#include <sys/mman.h>
41#include <unistd.h>
42
43namespace WindowServer {
44
45static Screen* s_the;
46
47Screen& Screen::the()
48{
49 ASSERT(s_the);
50 return *s_the;
51}
52
53Screen::Screen(unsigned desired_width, unsigned desired_height)
54{
55 ASSERT(!s_the);
56 s_the = this;
57
58#if defined(__OpenBSD__)
59 m_framebuffer_fd = open("/dev/drm0", O_RDWR | O_CLOEXEC);
60 if (m_framebuffer_fd < 0) {
61 perror("failed to open /dev/drm0");
62 ASSERT_NOT_REACHED();
63 }
64#else
65 m_framebuffer_fd = open("/dev/fb0", O_RDWR | O_CLOEXEC);
66 if (m_framebuffer_fd < 0) {
67 perror("failed to open /dev/fb0");
68 ASSERT_NOT_REACHED();
69 }
70#endif
71
72 if (fb_set_buffer(m_framebuffer_fd, 0) == 0) {
73 m_can_set_buffer = true;
74 }
75
76 set_resolution(desired_width, desired_height);
77 m_cursor_location = rect().center();
78}
79
80Screen::~Screen()
81{
82}
83
84bool Screen::set_resolution(int width, int height)
85{
86 FBResolution resolution { 0, (int)width, (int)height };
87 int rc = fb_set_resolution(m_framebuffer_fd, &resolution);
88#ifdef WSSCREEN_DEBUG
89 dbg() << "fb_set_resolution() - return code " << rc;
90#endif
91 if (rc == 0) {
92 on_change_resolution(resolution.pitch, resolution.width, resolution.height);
93 return true;
94 }
95 if (rc == -1) {
96 dbg() << "Invalid resolution " << width << "x" << height;
97 on_change_resolution(resolution.pitch, resolution.width, resolution.height);
98 return false;
99 }
100 ASSERT_NOT_REACHED();
101}
102
103void Screen::on_change_resolution(int pitch, int width, int height)
104{
105 if (m_can_set_buffer && m_framebuffer[1] && m_framebuffer[1] != m_framebuffer[0]) {
106 int rc = munmap(m_framebuffer[1], m_size_in_bytes);
107 ASSERT(rc == 0);
108 }
109 if (m_framebuffer[0]) {
110 int rc = munmap(m_framebuffer[0], m_size_in_bytes);
111 ASSERT(rc == 0);
112 }
113
114 int rc = fb_get_size_in_bytes(m_framebuffer_fd, &m_size_in_bytes);
115 ASSERT(rc == 0);
116
117#ifdef __OpenBSD__
118 m_framebuffer[0] = (Gfx::RGBA32*)fb_get_addr(m_framebuffer_fd, 0);
119 if (m_can_set_buffer)
120 m_framebuffer[1] = (Gfx::RGBA32*)fb_get_addr(m_framebuffer_fd, 1);
121#else
122 m_framebuffer[0] = (Gfx::RGBA32*)mmap(nullptr, m_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_framebuffer_fd, 0);
123 if (m_can_set_buffer)
124 m_framebuffer[1] = m_framebuffer[0];
125#endif
126 ASSERT(m_framebuffer[0] && m_framebuffer[0] != (void*)-1);
127
128 m_pitch = pitch;
129 m_width = width;
130 m_height = height;
131
132 m_cursor_location.constrain(rect());
133}
134
135void Screen::set_buffer(int index)
136{
137 ASSERT(m_can_set_buffer);
138 int rc = fb_set_buffer(m_framebuffer_fd, index);
139 ASSERT(rc == 0);
140}
141
142void Screen::on_receive_mouse_data(const MousePacket& packet)
143{
144 auto prev_location = m_cursor_location;
145 if (packet.is_relative) {
146 m_cursor_location.move_by(packet.x, packet.y);
147#ifdef WSSCREEN_DEBUG
148 dbgprintf("Screen: New Relative mouse point @ X %d, Y %d\n", m_cursor_location.x(), m_cursor_location.y());
149#endif
150 } else {
151 m_cursor_location = { packet.x * m_width / 0xffff, packet.y * m_height / 0xffff };
152#ifdef WSSCREEN_DEBUG
153 dbgprintf("Screen: New Absolute mouse point @ X %d, Y %d\n", m_cursor_location.x(), m_cursor_location.y());
154#endif
155 }
156
157 m_cursor_location.constrain(rect());
158
159 unsigned buttons = packet.buttons;
160 unsigned prev_buttons = m_mouse_button_state;
161 m_mouse_button_state = buttons;
162 unsigned changed_buttons = prev_buttons ^ buttons;
163 auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
164 if (!(changed_buttons & (unsigned)button))
165 return;
166 auto message = make<MouseEvent>(buttons & (unsigned)button ? Event::MouseDown : Event::MouseUp, m_cursor_location, buttons, button, m_modifiers);
167 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
168 };
169 post_mousedown_or_mouseup_if_needed(MouseButton::Left);
170 post_mousedown_or_mouseup_if_needed(MouseButton::Right);
171 post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
172 if (m_cursor_location != prev_location) {
173 auto message = make<MouseEvent>(Event::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
174 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
175 }
176
177 if (packet.z) {
178 auto message = make<MouseEvent>(Event::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.z);
179 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
180 }
181
182 if (m_cursor_location != prev_location)
183 Compositor::the().invalidate_cursor();
184}
185
186void Screen::on_receive_keyboard_data(::KeyEvent kernel_event)
187{
188 m_modifiers = kernel_event.modifiers();
189 auto message = make<KeyEvent>(kernel_event.is_press() ? Event::KeyDown : Event::KeyUp, kernel_event.key, kernel_event.character, kernel_event.modifiers());
190 Core::EventLoop::current().post_event(WindowManager::the(), move(message));
191}
192
193}