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#pragma once
28
29#include <Kernel/KeyCode.h>
30#include <LibGfx/Color.h>
31#include <LibGfx/Rect.h>
32#include <LibGfx/Size.h>
33
34struct MousePacket;
35
36namespace WindowServer {
37
38class Screen {
39public:
40 Screen(unsigned width, unsigned height);
41 ~Screen();
42
43 bool set_resolution(int width, int height);
44 bool can_set_buffer() { return m_can_set_buffer; }
45 void set_buffer(int index);
46
47 int width() const { return m_width; }
48 int height() const { return m_height; }
49 size_t pitch() const { return m_pitch; }
50 Gfx::RGBA32* scanline(int y);
51
52 static Screen& the();
53
54 Gfx::Size size() const { return { width(), height() }; }
55 Gfx::Rect rect() const { return { 0, 0, width(), height() }; }
56
57 Gfx::Point cursor_location() const { return m_cursor_location; }
58 unsigned mouse_button_state() const { return m_mouse_button_state; }
59
60 void on_receive_mouse_data(const MousePacket&);
61 void on_receive_keyboard_data(::KeyEvent);
62
63private:
64 void on_change_resolution(int pitch, int width, int height);
65
66 size_t m_size_in_bytes;
67
68 Gfx::RGBA32* m_framebuffer[2] { nullptr };
69 bool m_can_set_buffer { false };
70
71 int m_pitch { 0 };
72 int m_width { 0 };
73 int m_height { 0 };
74 int m_framebuffer_fd { -1 };
75
76 Gfx::Point m_cursor_location;
77 unsigned m_mouse_button_state { 0 };
78 unsigned m_modifiers { 0 };
79};
80
81inline Gfx::RGBA32* Screen::scanline(int y)
82{
83 size_t off = y * m_pitch;
84 int index = 0;
85 if (off >= m_size_in_bytes) {
86 off -= m_size_in_bytes;
87 index++;
88 }
89 return reinterpret_cast<Gfx::RGBA32*>(((u8*)m_framebuffer[index]) + off);
90}
91
92}