| 1 | /* |
| 2 | * Copyright (C) 2017 Damir Porobic <https://github.com/damirporobic> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * blendCursorImage() and getNativeCursorPosition() functions have been taken |
| 22 | * from KDE Spectacle X11ImageGrabber and slightly modified to fit this |
| 23 | * implementation. |
| 24 | */ |
| 25 | |
| 26 | #include "X11Wrapper.h" |
| 27 | |
| 28 | #include <X11/Xlib.h> |
| 29 | |
| 30 | bool X11Wrapper::isCompositorActive() const |
| 31 | { |
| 32 | auto display = QX11Info::display(); |
| 33 | auto prop_atom = XInternAtom(display, "_NET_WM_CM_S0", false); |
| 34 | return XGetSelectionOwner(display, prop_atom) != None; |
| 35 | } |
| 36 | |
| 37 | QRect X11Wrapper::getFullScreenRect() const |
| 38 | { |
| 39 | return getWindowRect(QX11Info::appRootWindow()); |
| 40 | } |
| 41 | |
| 42 | QRect X11Wrapper::getActiveWindowRect() const |
| 43 | { |
| 44 | auto windowId = getActiveWindowId(); |
| 45 | return getWindowRect(windowId); |
| 46 | } |
| 47 | |
| 48 | QRect X11Wrapper::getWindowRect(xcb_window_t windowId) const |
| 49 | { |
| 50 | // In case of window id 0 return empty rect |
| 51 | if (windowId == 0) { |
| 52 | return {}; |
| 53 | } |
| 54 | |
| 55 | auto connection = QX11Info::connection(); |
| 56 | auto geomCookie = xcb_get_geometry_unchecked(connection, windowId); |
| 57 | ScopedCPointer<xcb_get_geometry_reply_t> geomReply(xcb_get_geometry_reply(connection, geomCookie, nullptr)); |
| 58 | |
| 59 | return { geomReply->x, geomReply->y, geomReply->width, geomReply->height }; |
| 60 | } |
| 61 | |
| 62 | xcb_window_t X11Wrapper::getActiveWindowId() const |
| 63 | { |
| 64 | xcb_query_tree_cookie_t treeCookie; |
| 65 | auto connection = QX11Info::connection(); |
| 66 | ScopedCPointer<xcb_get_input_focus_reply_t> focusReply(xcb_get_input_focus_reply(connection, xcb_get_input_focus(connection), nullptr)); |
| 67 | |
| 68 | // Get ID of focused window, however, this must not always be the top level |
| 69 | // window so we loop through parents and search for the top level window. |
| 70 | auto windowId = focusReply->focus; |
| 71 | while (true) { |
| 72 | treeCookie = xcb_query_tree_unchecked(connection, windowId); |
| 73 | ScopedCPointer<xcb_query_tree_reply_t> treeReply(xcb_query_tree_reply(connection, treeCookie, nullptr)); |
| 74 | if (!treeReply) { |
| 75 | return 0; |
| 76 | } |
| 77 | // If the root window it is equal to the parent or the window ID itself |
| 78 | // the this must be the top level window. |
| 79 | if (windowId == treeReply->root || treeReply->parent == treeReply->root) { |
| 80 | return windowId; |
| 81 | } else { |
| 82 | windowId = treeReply->parent; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | QPoint X11Wrapper::getNativeCursorPosition() const |
| 88 | { |
| 89 | // QCursor::pos() is not used because it requires additional calculations. |
| 90 | // Its value is the offset to the origin of the current screen is in |
| 91 | // device-independent pixels while the origin itself uses native pixels. |
| 92 | |
| 93 | auto xcbConn = QX11Info::connection(); |
| 94 | auto pointerCookie = xcb_query_pointer_unchecked(xcbConn, QX11Info::appRootWindow()); |
| 95 | ScopedCPointer<xcb_query_pointer_reply_t> pointerReply(xcb_query_pointer_reply(xcbConn, pointerCookie, nullptr)); |
| 96 | |
| 97 | return { pointerReply->root_x, pointerReply->root_y }; |
| 98 | } |
| 99 | |
| 100 | CursorDto X11Wrapper::getCursorWithPosition() const |
| 101 | { |
| 102 | auto cursorPosition = getNativeCursorPosition(); |
| 103 | |
| 104 | // now we can get the image and start processing |
| 105 | auto xcbConn = QX11Info::connection(); |
| 106 | |
| 107 | auto cursorCookie = xcb_xfixes_get_cursor_image_unchecked(xcbConn); |
| 108 | ScopedCPointer<xcb_xfixes_get_cursor_image_reply_t> cursorReply(xcb_xfixes_get_cursor_image_reply(xcbConn, cursorCookie, nullptr)); |
| 109 | if (cursorReply.isNull()) { |
| 110 | return CursorDto(); |
| 111 | } |
| 112 | |
| 113 | auto pixelData = xcb_xfixes_get_cursor_image_cursor_image(cursorReply.data()); |
| 114 | if (!pixelData) { |
| 115 | return CursorDto(); |
| 116 | } |
| 117 | |
| 118 | // process the image into a QImage |
| 119 | auto cursorImage = QImage((quint8 *) pixelData, |
| 120 | cursorReply->width, |
| 121 | cursorReply->height, |
| 122 | QImage::Format_ARGB32_Premultiplied); |
| 123 | |
| 124 | // a small fix for the cursor position for fancier cursor |
| 125 | cursorPosition -= QPoint(cursorReply->xhot, cursorReply->yhot); |
| 126 | |
| 127 | return { QPixmap::fromImage(cursorImage, Qt::AutoColor), cursorPosition }; |
| 128 | } |