Screen.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include "Screen.h"
  27. #include "Compositor.h"
  28. #include "Event.h"
  29. #include "EventLoop.h"
  30. #include "WindowManager.h"
  31. #include <AK/Debug.h>
  32. #include <Kernel/API/FB.h>
  33. #include <Kernel/API/MousePacket.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <sys/mman.h>
  37. #include <unistd.h>
  38. namespace WindowServer {
  39. static Screen* s_the;
  40. Screen& Screen::the()
  41. {
  42. ASSERT(s_the);
  43. return *s_the;
  44. }
  45. Screen::Screen(unsigned desired_width, unsigned desired_height, int scale_factor)
  46. {
  47. ASSERT(!s_the);
  48. s_the = this;
  49. m_framebuffer_fd = open("/dev/fb0", O_RDWR | O_CLOEXEC);
  50. if (m_framebuffer_fd < 0) {
  51. perror("failed to open /dev/fb0");
  52. ASSERT_NOT_REACHED();
  53. }
  54. if (fb_set_buffer(m_framebuffer_fd, 0) == 0) {
  55. m_can_set_buffer = true;
  56. }
  57. set_resolution(desired_width, desired_height, scale_factor);
  58. m_physical_cursor_location = physical_rect().center();
  59. }
  60. Screen::~Screen()
  61. {
  62. close(m_framebuffer_fd);
  63. }
  64. bool Screen::set_resolution(int width, int height, int new_scale_factor)
  65. {
  66. int new_physical_width = width * new_scale_factor;
  67. int new_physical_height = height * new_scale_factor;
  68. if (physical_width() == new_physical_width && physical_height() == new_physical_height) {
  69. assert(scale_factor() != new_scale_factor);
  70. on_change_resolution(m_pitch, physical_width(), physical_height(), new_scale_factor);
  71. return true;
  72. }
  73. FBResolution physical_resolution { 0, (unsigned)new_physical_width, (unsigned)new_physical_height };
  74. int rc = fb_set_resolution(m_framebuffer_fd, &physical_resolution);
  75. dbgln<debug_wsscreen>("fb_set_resolution() - return code {}", rc);
  76. if (rc == 0) {
  77. on_change_resolution(physical_resolution.pitch, physical_resolution.width, physical_resolution.height, new_scale_factor);
  78. return true;
  79. }
  80. if (rc == -1) {
  81. dbgln("Invalid resolution {}x{}", width, height);
  82. on_change_resolution(physical_resolution.pitch, physical_resolution.width, physical_resolution.height, new_scale_factor);
  83. return false;
  84. }
  85. ASSERT_NOT_REACHED();
  86. }
  87. void Screen::on_change_resolution(int pitch, int new_physical_width, int new_physical_height, int new_scale_factor)
  88. {
  89. if (physical_width() != new_physical_width || physical_height() != new_physical_height) {
  90. if (m_framebuffer) {
  91. size_t previous_size_in_bytes = m_size_in_bytes;
  92. int rc = munmap(m_framebuffer, previous_size_in_bytes);
  93. ASSERT(rc == 0);
  94. }
  95. int rc = fb_get_size_in_bytes(m_framebuffer_fd, &m_size_in_bytes);
  96. ASSERT(rc == 0);
  97. m_framebuffer = (Gfx::RGBA32*)mmap(nullptr, m_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_framebuffer_fd, 0);
  98. ASSERT(m_framebuffer && m_framebuffer != (void*)-1);
  99. }
  100. m_pitch = pitch;
  101. m_width = new_physical_width / new_scale_factor;
  102. m_height = new_physical_height / new_scale_factor;
  103. m_scale_factor = new_scale_factor;
  104. m_physical_cursor_location.constrain(physical_rect());
  105. }
  106. void Screen::set_buffer(int index)
  107. {
  108. ASSERT(m_can_set_buffer);
  109. int rc = fb_set_buffer(m_framebuffer_fd, index);
  110. ASSERT(rc == 0);
  111. }
  112. void Screen::set_acceleration_factor(double factor)
  113. {
  114. ASSERT(factor >= mouse_accel_min && factor <= mouse_accel_max);
  115. m_acceleration_factor = factor;
  116. }
  117. void Screen::set_scroll_step_size(unsigned step_size)
  118. {
  119. ASSERT(step_size >= scroll_step_size_min);
  120. m_scroll_step_size = step_size;
  121. }
  122. void Screen::on_receive_mouse_data(const MousePacket& packet)
  123. {
  124. auto prev_location = m_physical_cursor_location / m_scale_factor;
  125. if (packet.is_relative) {
  126. m_physical_cursor_location.move_by(packet.x * m_acceleration_factor, packet.y * m_acceleration_factor);
  127. #ifdef WSSCREEN_DEBUG
  128. dbgln("Screen: New Relative mouse point @ {}", m_physical_cursor_location);
  129. #endif
  130. } else {
  131. m_physical_cursor_location = { packet.x * physical_width() / 0xffff, packet.y * physical_height() / 0xffff };
  132. #ifdef WSSCREEN_DEBUG
  133. dbgln("Screen: New Absolute mouse point @ {}", m_physical_cursor_location);
  134. #endif
  135. }
  136. m_physical_cursor_location.constrain(physical_rect());
  137. auto new_location = m_physical_cursor_location / m_scale_factor;
  138. unsigned buttons = packet.buttons;
  139. unsigned prev_buttons = m_mouse_button_state;
  140. m_mouse_button_state = buttons;
  141. unsigned changed_buttons = prev_buttons ^ buttons;
  142. auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
  143. if (!(changed_buttons & (unsigned)button))
  144. return;
  145. auto message = make<MouseEvent>(buttons & (unsigned)button ? Event::MouseDown : Event::MouseUp, new_location, buttons, button, m_modifiers);
  146. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  147. };
  148. post_mousedown_or_mouseup_if_needed(MouseButton::Left);
  149. post_mousedown_or_mouseup_if_needed(MouseButton::Right);
  150. post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
  151. post_mousedown_or_mouseup_if_needed(MouseButton::Back);
  152. post_mousedown_or_mouseup_if_needed(MouseButton::Forward);
  153. if (new_location != prev_location) {
  154. auto message = make<MouseEvent>(Event::MouseMove, new_location, buttons, MouseButton::None, m_modifiers);
  155. if (WindowManager::the().dnd_client())
  156. message->set_mime_data(WindowManager::the().dnd_mime_data());
  157. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  158. }
  159. if (packet.z) {
  160. auto message = make<MouseEvent>(Event::MouseWheel, new_location, buttons, MouseButton::None, m_modifiers, packet.z * m_scroll_step_size);
  161. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  162. }
  163. if (new_location != prev_location)
  164. Compositor::the().invalidate_cursor();
  165. }
  166. void Screen::on_receive_keyboard_data(::KeyEvent kernel_event)
  167. {
  168. m_modifiers = kernel_event.modifiers();
  169. auto message = make<KeyEvent>(kernel_event.is_press() ? Event::KeyDown : Event::KeyUp, kernel_event.key, kernel_event.code_point, kernel_event.modifiers(), kernel_event.scancode);
  170. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  171. }
  172. }