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