| 1 | // LAF OS Library |
| 2 | // Copyright (c) 2020-2021 Igara Studio S.A. |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifndef OS_X11_SCREEN_H |
| 8 | #define OS_X11_SCREEN_H |
| 9 | #pragma once |
| 10 | |
| 11 | #include "os/screen.h" |
| 12 | #include "os/x11/x11.h" |
| 13 | |
| 14 | #include <X11/Xatom.h> |
| 15 | |
| 16 | namespace os { |
| 17 | |
| 18 | class ScreenX11 : public Screen { |
| 19 | public: |
| 20 | ScreenX11(int screen) { |
| 21 | auto x11 = X11::instance(); |
| 22 | auto x11display = x11->display(); |
| 23 | |
| 24 | m_bounds.w = XDisplayWidth(x11display, screen); |
| 25 | m_bounds.h = XDisplayHeight(x11display, screen); |
| 26 | |
| 27 | ::Window root = XDefaultRootWindow(x11display); |
| 28 | Atom _NET_WORKAREA = XInternAtom(x11display, "_NET_WORKAREA" , False); |
| 29 | |
| 30 | Atom actual_type; |
| 31 | int actual_format; |
| 32 | unsigned long nitems; |
| 33 | unsigned long bytes_after; |
| 34 | unsigned long* prop; |
| 35 | |
| 36 | int res = XGetWindowProperty(x11display, root, |
| 37 | _NET_WORKAREA, |
| 38 | 0, 4, |
| 39 | False, XA_CARDINAL, |
| 40 | &actual_type, &actual_format, |
| 41 | &nitems, &bytes_after, |
| 42 | (unsigned char**)&prop); |
| 43 | if (res == Success && nitems == 4) { |
| 44 | m_workarea.x = prop[0]; |
| 45 | m_workarea.y = prop[1]; |
| 46 | m_workarea.w = prop[2]; |
| 47 | m_workarea.h = prop[3]; |
| 48 | XFree(prop); |
| 49 | } |
| 50 | else { |
| 51 | m_workarea = m_bounds; |
| 52 | } |
| 53 | } |
| 54 | bool isMainScreen() const override { |
| 55 | return (m_screen == XDefaultScreen(X11::instance()->display())); |
| 56 | } |
| 57 | gfx::Rect bounds() const override { return m_bounds; } |
| 58 | gfx::Rect workarea() const override { return m_workarea; } |
| 59 | os::ColorSpaceRef colorSpace() const override { |
| 60 | // TODO get screen color space |
| 61 | return os::instance()->makeColorSpace(gfx::ColorSpace::MakeSRGB()); |
| 62 | } |
| 63 | void* nativeHandle() const override { |
| 64 | return reinterpret_cast<void*>(m_screen); |
| 65 | } |
| 66 | private: |
| 67 | int m_screen; |
| 68 | gfx::Rect m_bounds; |
| 69 | gfx::Rect m_workarea; |
| 70 | }; |
| 71 | |
| 72 | } // namespace os |
| 73 | |
| 74 | #endif |
| 75 | |