WSScreen.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "WSScreen.h"
  27. #include "WSCompositor.h"
  28. #include "WSEvent.h"
  29. #include "WSEventLoop.h"
  30. #include "WSWindowManager.h"
  31. #include <Kernel/FB.h>
  32. #include <fcntl.h>
  33. #include <stdio.h>
  34. #include <sys/mman.h>
  35. #include <unistd.h>
  36. static WSScreen* s_the;
  37. WSScreen& WSScreen::the()
  38. {
  39. ASSERT(s_the);
  40. return *s_the;
  41. }
  42. WSScreen::WSScreen(unsigned desired_width, unsigned desired_height)
  43. {
  44. ASSERT(!s_the);
  45. s_the = this;
  46. m_framebuffer_fd = open("/dev/fb0", O_RDWR | O_CLOEXEC);
  47. if (m_framebuffer_fd < 0) {
  48. perror("failed to open /dev/fb0");
  49. ASSERT_NOT_REACHED();
  50. }
  51. if (fb_set_buffer(m_framebuffer_fd, 0) == 0) {
  52. m_can_set_buffer = true;
  53. }
  54. set_resolution(desired_width, desired_height);
  55. m_cursor_location = rect().center();
  56. }
  57. WSScreen::~WSScreen()
  58. {
  59. }
  60. void WSScreen::set_resolution(int width, int height)
  61. {
  62. FBResolution resolution { 0, (int)width, (int)height };
  63. int rc = fb_set_resolution(m_framebuffer_fd, &resolution);
  64. ASSERT(rc == 0);
  65. on_change_resolution(resolution.pitch, resolution.width, resolution.height);
  66. }
  67. void WSScreen::on_change_resolution(int pitch, int width, int height)
  68. {
  69. if (m_framebuffer) {
  70. size_t previous_size_in_bytes = m_size_in_bytes;
  71. int rc = munmap(m_framebuffer, previous_size_in_bytes);
  72. ASSERT(rc == 0);
  73. }
  74. int rc = fb_get_size_in_bytes(m_framebuffer_fd, &m_size_in_bytes);
  75. ASSERT(rc == 0);
  76. m_framebuffer = (RGBA32*)mmap(nullptr, m_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_framebuffer_fd, 0);
  77. ASSERT(m_framebuffer && m_framebuffer != (void*)-1);
  78. m_pitch = pitch;
  79. m_width = width;
  80. m_height = height;
  81. m_cursor_location.constrain(rect());
  82. }
  83. void WSScreen::set_buffer(int index)
  84. {
  85. ASSERT(m_can_set_buffer);
  86. int rc = fb_set_buffer(m_framebuffer_fd, index);
  87. ASSERT(rc == 0);
  88. }
  89. void WSScreen::on_receive_mouse_data(int dx, int dy, int dz, unsigned buttons)
  90. {
  91. auto prev_location = m_cursor_location;
  92. m_cursor_location.move_by(dx, dy);
  93. m_cursor_location.constrain(rect());
  94. unsigned prev_buttons = m_mouse_button_state;
  95. m_mouse_button_state = buttons;
  96. unsigned changed_buttons = prev_buttons ^ buttons;
  97. auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
  98. if (!(changed_buttons & (unsigned)button))
  99. return;
  100. auto message = make<WSMouseEvent>(buttons & (unsigned)button ? WSEvent::MouseDown : WSEvent::MouseUp, m_cursor_location, buttons, button, m_modifiers);
  101. CEventLoop::current().post_event(WSWindowManager::the(), move(message));
  102. };
  103. post_mousedown_or_mouseup_if_needed(MouseButton::Left);
  104. post_mousedown_or_mouseup_if_needed(MouseButton::Right);
  105. post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
  106. if (m_cursor_location != prev_location) {
  107. auto message = make<WSMouseEvent>(WSEvent::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
  108. CEventLoop::current().post_event(WSWindowManager::the(), move(message));
  109. }
  110. if (dz) {
  111. auto message = make<WSMouseEvent>(WSEvent::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, dz);
  112. CEventLoop::current().post_event(WSWindowManager::the(), move(message));
  113. }
  114. if (m_cursor_location != prev_location)
  115. WSCompositor::the().invalidate_cursor();
  116. }
  117. void WSScreen::on_receive_keyboard_data(KeyEvent kernel_event)
  118. {
  119. m_modifiers = kernel_event.modifiers();
  120. auto message = make<WSKeyEvent>(kernel_event.is_press() ? WSEvent::KeyDown : WSEvent::KeyUp, kernel_event.key, kernel_event.character, kernel_event.modifiers());
  121. CEventLoop::current().post_event(WSWindowManager::the(), move(message));
  122. }