Screen.cpp 6.0 KB

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