Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * Copyright (C) 2004-2008 Constantin Kaplinsky. All Rights Reserved. |
Peter Åstrand (astrand) | 3a1db16 | 2017-10-16 11:11:45 +0200 | [diff] [blame] | 3 | * Copyright 2017 Peter Astrand <astrand@cendio.se> for Cendio AB |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | */ |
| 20 | |
| 21 | #include <x0vncserver/XDesktop.h> |
| 22 | |
| 23 | #include <X11/XKBlib.h> |
| 24 | #ifdef HAVE_XTEST |
| 25 | #include <X11/extensions/XTest.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_XDAMAGE |
| 28 | #include <X11/extensions/Xdamage.h> |
| 29 | #endif |
| 30 | #ifdef HAVE_XFIXES |
| 31 | #include <X11/extensions/Xfixes.h> |
| 32 | #endif |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 33 | #ifdef HAVE_XRANDR |
| 34 | #include <X11/extensions/Xrandr.h> |
| 35 | #include <RandrGlue.h> |
| 36 | extern "C" { |
| 37 | void vncSetGlueContext(Display *dpy, void *res); |
| 38 | } |
| 39 | #endif |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 40 | #include <x0vncserver/Geometry.h> |
| 41 | #include <x0vncserver/XPixelBuffer.h> |
| 42 | |
| 43 | using namespace rfb; |
| 44 | |
| 45 | extern const unsigned short code_map_qnum_to_xorgevdev[]; |
| 46 | extern const unsigned int code_map_qnum_to_xorgevdev_len; |
| 47 | |
| 48 | extern const unsigned short code_map_qnum_to_xorgkbd[]; |
| 49 | extern const unsigned int code_map_qnum_to_xorgkbd_len; |
| 50 | |
Pierre Ossman | ce4722f | 2017-11-08 16:00:05 +0100 | [diff] [blame] | 51 | BoolParameter useShm("UseSHM", "Use MIT-SHM extension if available", true); |
| 52 | BoolParameter rawKeyboard("RawKeyboard", |
| 53 | "Send keyboard events straight through and " |
| 54 | "avoid mapping them to the current keyboard " |
| 55 | "layout", false); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 56 | |
| 57 | static rfb::LogWriter vlog("XDesktop"); |
| 58 | |
| 59 | // order is important as it must match RFB extension |
| 60 | static const char * ledNames[XDESKTOP_N_LEDS] = { |
| 61 | "Scroll Lock", "Num Lock", "Caps Lock" |
| 62 | }; |
| 63 | |
| 64 | XDesktop::XDesktop(Display* dpy_, Geometry *geometry_) |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 65 | : dpy(dpy_), geometry(geometry_), pb(0), server(0), |
| 66 | oldButtonMask(0), haveXtest(false), haveDamage(false), |
| 67 | maxButtons(0), running(false), ledMasks(), ledState(0), |
| 68 | codeMap(0), codeMapLen(0) |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 69 | { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 70 | int major, minor; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 71 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 72 | int xkbOpcode, xkbErrorBase; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 73 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 74 | major = XkbMajorVersion; |
| 75 | minor = XkbMinorVersion; |
| 76 | if (!XkbQueryExtension(dpy, &xkbOpcode, &xkbEventBase, |
| 77 | &xkbErrorBase, &major, &minor)) { |
| 78 | vlog.error("XKEYBOARD extension not present"); |
| 79 | throw Exception(); |
| 80 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 81 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 82 | XkbSelectEvents(dpy, XkbUseCoreKbd, XkbIndicatorStateNotifyMask, |
| 83 | XkbIndicatorStateNotifyMask); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 84 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 85 | // figure out bit masks for the indicators we are interested in |
| 86 | for (int i = 0; i < XDESKTOP_N_LEDS; i++) { |
| 87 | Atom a; |
| 88 | int shift; |
| 89 | Bool on; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 90 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 91 | a = XInternAtom(dpy, ledNames[i], True); |
| 92 | if (!a || !XkbGetNamedIndicator(dpy, a, &shift, &on, NULL, NULL)) |
| 93 | continue; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 94 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 95 | ledMasks[i] = 1u << shift; |
| 96 | vlog.debug("Mask for '%s' is 0x%x", ledNames[i], ledMasks[i]); |
| 97 | if (on) |
| 98 | ledState |= 1u << i; |
| 99 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 100 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 101 | // X11 unfortunately uses keyboard driver specific keycodes and provides no |
| 102 | // direct way to query this, so guess based on the keyboard mapping |
| 103 | XkbDescPtr desc = XkbGetKeyboard(dpy, XkbAllComponentsMask, XkbUseCoreKbd); |
| 104 | if (desc && desc->names) { |
| 105 | char *keycodes = XGetAtomName(dpy, desc->names->keycodes); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 106 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 107 | if (keycodes) { |
| 108 | if (strncmp("evdev", keycodes, strlen("evdev")) == 0) { |
| 109 | codeMap = code_map_qnum_to_xorgevdev; |
| 110 | codeMapLen = code_map_qnum_to_xorgevdev_len; |
| 111 | vlog.info("Using evdev codemap\n"); |
| 112 | } else if (strncmp("xfree86", keycodes, strlen("xfree86")) == 0) { |
| 113 | codeMap = code_map_qnum_to_xorgkbd; |
| 114 | codeMapLen = code_map_qnum_to_xorgkbd_len; |
| 115 | vlog.info("Using xorgkbd codemap\n"); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 116 | } else { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 117 | vlog.info("Unknown keycode '%s', no codemap\n", keycodes); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 118 | } |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 119 | XFree(keycodes); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 120 | } else { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 121 | vlog.debug("Unable to get keycode map\n"); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 122 | } |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 123 | |
| 124 | XkbFreeKeyboard(desc, XkbAllComponentsMask, True); |
| 125 | } |
| 126 | |
| 127 | #ifdef HAVE_XTEST |
| 128 | int xtestEventBase; |
| 129 | int xtestErrorBase; |
| 130 | |
| 131 | if (XTestQueryExtension(dpy, &xtestEventBase, |
| 132 | &xtestErrorBase, &major, &minor)) { |
| 133 | XTestGrabControl(dpy, True); |
| 134 | vlog.info("XTest extension present - version %d.%d",major,minor); |
| 135 | haveXtest = true; |
| 136 | } else { |
| 137 | #endif |
| 138 | vlog.info("XTest extension not present"); |
| 139 | vlog.info("Unable to inject events or display while server is grabbed"); |
| 140 | #ifdef HAVE_XTEST |
| 141 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 142 | #endif |
| 143 | |
| 144 | #ifdef HAVE_XDAMAGE |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 145 | int xdamageErrorBase; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 146 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 147 | if (XDamageQueryExtension(dpy, &xdamageEventBase, &xdamageErrorBase)) { |
| 148 | haveDamage = true; |
| 149 | } else { |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 150 | #endif |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 151 | vlog.info("DAMAGE extension not present"); |
| 152 | vlog.info("Will have to poll screen for changes"); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 153 | #ifdef HAVE_XDAMAGE |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 154 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 155 | #endif |
| 156 | |
| 157 | #ifdef HAVE_XFIXES |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 158 | int xfixesErrorBase; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 159 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 160 | if (XFixesQueryExtension(dpy, &xfixesEventBase, &xfixesErrorBase)) { |
| 161 | XFixesSelectCursorInput(dpy, DefaultRootWindow(dpy), |
| 162 | XFixesDisplayCursorNotifyMask); |
| 163 | } else { |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 164 | #endif |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 165 | vlog.info("XFIXES extension not present"); |
| 166 | vlog.info("Will not be able to display cursors"); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 167 | #ifdef HAVE_XFIXES |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 168 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 169 | #endif |
| 170 | |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 171 | #ifdef HAVE_XRANDR |
| 172 | int xrandrErrorBase; |
| 173 | |
| 174 | randrSyncSerial = 0; |
| 175 | if (XRRQueryExtension(dpy, &xrandrEventBase, &xrandrErrorBase)) { |
| 176 | XRRSelectInput(dpy, DefaultRootWindow(dpy), |
| 177 | RRScreenChangeNotifyMask | RRCrtcChangeNotifyMask); |
| 178 | /* Override TXWindow::init input mask */ |
| 179 | XSelectInput(dpy, DefaultRootWindow(dpy), |
| 180 | PropertyChangeMask | StructureNotifyMask | ExposureMask); |
| 181 | } else { |
| 182 | #endif |
| 183 | vlog.info("RANDR extension not present"); |
| 184 | vlog.info("Will not be able to handle session resize"); |
| 185 | #ifdef HAVE_XRANDR |
| 186 | } |
| 187 | #endif |
| 188 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 189 | TXWindow::setGlobalEventHandler(this); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | XDesktop::~XDesktop() { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 193 | if (running) |
| 194 | stop(); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
| 198 | void XDesktop::poll() { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 199 | if (pb and not haveDamage) |
| 200 | pb->poll(server); |
| 201 | if (running) { |
| 202 | Window root, child; |
| 203 | int x, y, wx, wy; |
| 204 | unsigned int mask; |
| 205 | XQueryPointer(dpy, DefaultRootWindow(dpy), &root, &child, |
| 206 | &x, &y, &wx, &wy, &mask); |
Pierre Ossman | c86ce3e | 2018-09-10 17:03:17 +0200 | [diff] [blame] | 207 | x -= geometry->offsetLeft(); |
| 208 | y -= geometry->offsetTop(); |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 209 | server->setCursorPos(rfb::Point(x, y)); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 210 | } |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 211 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 212 | |
| 213 | |
| 214 | void XDesktop::start(VNCServer* vs) { |
| 215 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 216 | // Determine actual number of buttons of the X pointer device. |
| 217 | unsigned char btnMap[8]; |
| 218 | int numButtons = XGetPointerMapping(dpy, btnMap, 8); |
| 219 | maxButtons = (numButtons > 8) ? 8 : numButtons; |
| 220 | vlog.info("Enabling %d button%s of X pointer device", |
| 221 | maxButtons, (maxButtons != 1) ? "s" : ""); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 222 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 223 | // Create an ImageFactory instance for producing Image objects. |
| 224 | ImageFactory factory((bool)useShm); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 225 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 226 | // Create pixel buffer and provide it to the server object. |
| 227 | pb = new XPixelBuffer(dpy, factory, geometry->getRect()); |
| 228 | vlog.info("Allocated %s", pb->getImage()->classDesc()); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 229 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 230 | server = (VNCServerST *)vs; |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 231 | server->setPixelBuffer(pb, computeScreenLayout()); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 232 | |
| 233 | #ifdef HAVE_XDAMAGE |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 234 | if (haveDamage) { |
| 235 | damage = XDamageCreate(dpy, DefaultRootWindow(dpy), |
| 236 | XDamageReportRawRectangles); |
| 237 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 238 | #endif |
| 239 | |
Peter Åstrand (astrand) | 3abc7d4 | 2017-10-11 15:12:10 +0200 | [diff] [blame] | 240 | #ifdef HAVE_XFIXES |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 241 | setCursor(); |
Peter Åstrand (astrand) | 3abc7d4 | 2017-10-11 15:12:10 +0200 | [diff] [blame] | 242 | #endif |
| 243 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 244 | server->setLEDState(ledState); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 245 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 246 | running = true; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void XDesktop::stop() { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 250 | running = false; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 251 | |
| 252 | #ifdef HAVE_XDAMAGE |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 253 | if (haveDamage) |
| 254 | XDamageDestroy(dpy, damage); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 255 | #endif |
| 256 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 257 | server->setPixelBuffer(0); |
| 258 | server = 0; |
Michal Srb | 18a7707 | 2017-09-29 14:47:56 +0200 | [diff] [blame] | 259 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 260 | delete pb; |
| 261 | pb = 0; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | bool XDesktop::isRunning() { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 265 | return running; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void XDesktop::pointerEvent(const Point& pos, int buttonMask) { |
| 269 | #ifdef HAVE_XTEST |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 270 | if (!haveXtest) return; |
| 271 | XTestFakeMotionEvent(dpy, DefaultScreen(dpy), |
| 272 | geometry->offsetLeft() + pos.x, |
| 273 | geometry->offsetTop() + pos.y, |
| 274 | CurrentTime); |
| 275 | if (buttonMask != oldButtonMask) { |
| 276 | for (int i = 0; i < maxButtons; i++) { |
| 277 | if ((buttonMask ^ oldButtonMask) & (1<<i)) { |
| 278 | if (buttonMask & (1<<i)) { |
| 279 | XTestFakeButtonEvent(dpy, i+1, True, CurrentTime); |
| 280 | } else { |
| 281 | XTestFakeButtonEvent(dpy, i+1, False, CurrentTime); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 285 | } |
| 286 | oldButtonMask = buttonMask; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 287 | #endif |
| 288 | } |
| 289 | |
| 290 | #ifdef HAVE_XTEST |
| 291 | KeyCode XDesktop::XkbKeysymToKeycode(Display* dpy, KeySym keysym) { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 292 | XkbDescPtr xkb; |
| 293 | XkbStateRec state; |
Pierre Ossman | 9ee59ec | 2018-07-25 20:02:02 +0200 | [diff] [blame] | 294 | unsigned int mods; |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 295 | unsigned keycode; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 296 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 297 | xkb = XkbGetMap(dpy, XkbAllComponentsMask, XkbUseCoreKbd); |
| 298 | if (!xkb) |
| 299 | return 0; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 300 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 301 | XkbGetState(dpy, XkbUseCoreKbd, &state); |
Pierre Ossman | 9ee59ec | 2018-07-25 20:02:02 +0200 | [diff] [blame] | 302 | // XkbStateFieldFromRec() doesn't work properly because |
| 303 | // state.lookup_mods isn't properly updated, so we do this manually |
| 304 | mods = XkbBuildCoreState(XkbStateMods(&state), state.group); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 305 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 306 | for (keycode = xkb->min_key_code; |
| 307 | keycode <= xkb->max_key_code; |
| 308 | keycode++) { |
| 309 | KeySym cursym; |
Pierre Ossman | 9ee59ec | 2018-07-25 20:02:02 +0200 | [diff] [blame] | 310 | unsigned int out_mods; |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 311 | XkbTranslateKeyCode(xkb, keycode, mods, &out_mods, &cursym); |
| 312 | if (cursym == keysym) |
| 313 | break; |
| 314 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 315 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 316 | if (keycode > xkb->max_key_code) |
| 317 | keycode = 0; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 318 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 319 | XkbFreeKeyboard(xkb, XkbAllComponentsMask, True); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 320 | |
Pierre Ossman | 9ee59ec | 2018-07-25 20:02:02 +0200 | [diff] [blame] | 321 | // Shift+Tab is usually ISO_Left_Tab, but RFB hides this fact. Do |
| 322 | // another attempt if we failed the initial lookup |
| 323 | if ((keycode == 0) && (keysym == XK_Tab) && (mods & ShiftMask)) |
| 324 | return XkbKeysymToKeycode(dpy, XK_ISO_Left_Tab); |
| 325 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 326 | return keycode; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 327 | } |
| 328 | #endif |
| 329 | |
| 330 | void XDesktop::keyEvent(rdr::U32 keysym, rdr::U32 xtcode, bool down) { |
| 331 | #ifdef HAVE_XTEST |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 332 | int keycode = 0; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 333 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 334 | if (!haveXtest) |
| 335 | return; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 336 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 337 | // Use scan code if provided and mapping exists |
| 338 | if (codeMap && rawKeyboard && xtcode < codeMapLen) |
| 339 | keycode = codeMap[xtcode]; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 340 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 341 | if (!keycode) { |
| 342 | if (pressedKeys.find(keysym) != pressedKeys.end()) |
| 343 | keycode = pressedKeys[keysym]; |
| 344 | else { |
| 345 | // XKeysymToKeycode() doesn't respect state, so we have to use |
| 346 | // something slightly more complex |
| 347 | keycode = XkbKeysymToKeycode(dpy, keysym); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 348 | } |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 349 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 350 | |
Pierre Ossman | a6072cc | 2018-07-25 20:02:20 +0200 | [diff] [blame] | 351 | if (!keycode) { |
| 352 | vlog.error("Could not map key event to X11 key code"); |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 353 | return; |
Pierre Ossman | a6072cc | 2018-07-25 20:02:20 +0200 | [diff] [blame] | 354 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 355 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 356 | if (down) |
| 357 | pressedKeys[keysym] = keycode; |
| 358 | else |
| 359 | pressedKeys.erase(keysym); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 360 | |
Pierre Ossman | a6072cc | 2018-07-25 20:02:20 +0200 | [diff] [blame] | 361 | vlog.debug("%d %s", keycode, down ? "down" : "up"); |
| 362 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 363 | XTestFakeKeyEvent(dpy, keycode, down, CurrentTime); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 364 | #endif |
| 365 | } |
| 366 | |
| 367 | void XDesktop::clientCutText(const char* str, int len) { |
| 368 | } |
| 369 | |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 370 | ScreenSet XDesktop::computeScreenLayout() |
| 371 | { |
| 372 | ScreenSet layout; |
| 373 | |
| 374 | #ifdef HAVE_XRANDR |
| 375 | XRRScreenResources *res = XRRGetScreenResources(dpy, DefaultRootWindow(dpy)); |
| 376 | if (!res) { |
| 377 | vlog.error("XRRGetScreenResources failed"); |
| 378 | return layout; |
| 379 | } |
| 380 | vncSetGlueContext(dpy, res); |
| 381 | |
| 382 | layout = ::computeScreenLayout(&outputIdMap); |
| 383 | XRRFreeScreenResources(res); |
| 384 | #endif |
| 385 | |
| 386 | return layout; |
| 387 | } |
| 388 | |
| 389 | #ifdef HAVE_XRANDR |
| 390 | /* Get the biggest mode which is equal or smaller to requested |
| 391 | size. If no such mode exists, return the smallest. */ |
| 392 | static void GetSmallerMode(XRRScreenResources *res, |
| 393 | XRROutputInfo *output, |
| 394 | unsigned int *width, unsigned int *height) |
| 395 | { |
| 396 | XRRModeInfo best = {}; |
| 397 | XRRModeInfo smallest = {}; |
| 398 | smallest.width = -1; |
| 399 | smallest.height = -1; |
| 400 | |
| 401 | for (int i = 0; i < res->nmode; i++) { |
| 402 | for (int j = 0; j < output->nmode; j++) { |
| 403 | if (output->modes[j] == res->modes[i].id) { |
| 404 | if ((res->modes[i].width > best.width && res->modes[i].width <= *width) && |
| 405 | (res->modes[i].height > best.height && res->modes[i].height <= *height)) { |
| 406 | best = res->modes[i]; |
| 407 | } |
| 408 | if ((res->modes[i].width < smallest.width) && res->modes[i].height < smallest.height) { |
| 409 | smallest = res->modes[i]; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (best.id == 0 && smallest.id != 0) { |
| 416 | best = smallest; |
| 417 | } |
| 418 | |
| 419 | *width = best.width; |
| 420 | *height = best.height; |
| 421 | } |
| 422 | #endif /* HAVE_XRANDR */ |
| 423 | |
| 424 | unsigned int XDesktop::setScreenLayout(int fb_width, int fb_height, |
| 425 | const rfb::ScreenSet& layout) |
| 426 | { |
| 427 | #ifdef HAVE_XRANDR |
| 428 | char buffer[2048]; |
| 429 | vlog.debug("Got request for framebuffer resize to %dx%d", |
| 430 | fb_width, fb_height); |
| 431 | layout.print(buffer, sizeof(buffer)); |
| 432 | vlog.debug("%s", buffer); |
| 433 | |
| 434 | XRRScreenResources *res = XRRGetScreenResources(dpy, DefaultRootWindow(dpy)); |
| 435 | if (!res) { |
| 436 | vlog.error("XRRGetScreenResources failed"); |
| 437 | return rfb::resultProhibited; |
| 438 | } |
| 439 | vncSetGlueContext(dpy, res); |
| 440 | |
| 441 | /* The client may request a screen layout which is not supported by |
| 442 | the Xserver. This happens, for example, when adjusting the size |
| 443 | of a non-fullscreen vncviewer window. To handle this and other |
| 444 | cases, we first call tryScreenLayout. If this fails, we try to |
| 445 | adjust the request to one screen with a smaller mode. */ |
| 446 | vlog.debug("Testing screen layout"); |
| 447 | unsigned int tryresult = ::tryScreenLayout(fb_width, fb_height, layout, &outputIdMap); |
| 448 | rfb::ScreenSet adjustedLayout; |
| 449 | if (tryresult == rfb::resultSuccess) { |
| 450 | adjustedLayout = layout; |
| 451 | } else { |
| 452 | vlog.debug("Impossible layout - trying to adjust"); |
| 453 | |
| 454 | ScreenSet::const_iterator firstscreen = layout.begin(); |
| 455 | adjustedLayout.add_screen(*firstscreen); |
| 456 | ScreenSet::iterator iter = adjustedLayout.begin(); |
| 457 | RROutput outputId = None; |
| 458 | |
| 459 | for (int i = 0;i < vncRandRGetOutputCount();i++) { |
| 460 | unsigned int oi = vncRandRGetOutputId(i); |
| 461 | |
| 462 | /* Known? */ |
| 463 | if (outputIdMap.count(oi) == 0) |
| 464 | continue; |
| 465 | |
| 466 | /* Find the corresponding screen... */ |
| 467 | if (iter->id == outputIdMap[oi]) { |
| 468 | outputId = oi; |
| 469 | } else { |
| 470 | outputIdMap.erase(oi); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* New screen */ |
| 475 | if (outputId == None) { |
| 476 | int i = getPreferredScreenOutput(&outputIdMap, std::set<unsigned int>()); |
| 477 | if (i != -1) { |
| 478 | outputId = vncRandRGetOutputId(i); |
| 479 | } |
| 480 | } |
| 481 | if (outputId == None) { |
| 482 | vlog.debug("Resize adjust: Could not find corresponding screen"); |
| 483 | XRRFreeScreenResources(res); |
| 484 | return rfb::resultInvalid; |
| 485 | } |
| 486 | XRROutputInfo *output = XRRGetOutputInfo(dpy, res, outputId); |
| 487 | if (!output) { |
| 488 | vlog.debug("Resize adjust: XRRGetOutputInfo failed"); |
| 489 | XRRFreeScreenResources(res); |
| 490 | return rfb::resultInvalid; |
| 491 | } |
| 492 | if (!output->crtc) { |
| 493 | vlog.debug("Resize adjust: Selected output has no CRTC"); |
| 494 | XRRFreeScreenResources(res); |
| 495 | XRRFreeOutputInfo(output); |
| 496 | return rfb::resultInvalid; |
| 497 | } |
| 498 | XRRCrtcInfo *crtc = XRRGetCrtcInfo(dpy, res, output->crtc); |
| 499 | if (!crtc) { |
| 500 | vlog.debug("Resize adjust: XRRGetCrtcInfo failed"); |
| 501 | XRRFreeScreenResources(res); |
| 502 | XRRFreeOutputInfo(output); |
| 503 | return rfb::resultInvalid; |
| 504 | } |
| 505 | |
| 506 | unsigned int swidth = iter->dimensions.width(); |
| 507 | unsigned int sheight = iter->dimensions.height(); |
| 508 | |
| 509 | switch (crtc->rotation) { |
| 510 | case RR_Rotate_90: |
| 511 | case RR_Rotate_270: |
| 512 | unsigned int swap = swidth; |
| 513 | swidth = sheight; |
| 514 | sheight = swap; |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | GetSmallerMode(res, output, &swidth, &sheight); |
| 519 | XRRFreeOutputInfo(output); |
| 520 | |
| 521 | switch (crtc->rotation) { |
| 522 | case RR_Rotate_90: |
| 523 | case RR_Rotate_270: |
| 524 | unsigned int swap = swidth; |
| 525 | swidth = sheight; |
| 526 | sheight = swap; |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | XRRFreeCrtcInfo(crtc); |
| 531 | |
| 532 | if (sheight != 0 && swidth != 0) { |
| 533 | vlog.debug("Adjusted resize request to %dx%d", swidth, sheight); |
| 534 | iter->dimensions.setXYWH(0, 0, swidth, sheight); |
| 535 | fb_width = swidth; |
| 536 | fb_height = sheight; |
| 537 | } else { |
| 538 | vlog.error("Failed to find smaller or equal screen size"); |
| 539 | XRRFreeScreenResources(res); |
| 540 | return rfb::resultInvalid; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | vlog.debug("Changing screen layout"); |
| 545 | unsigned int ret = ::setScreenLayout(fb_width, fb_height, adjustedLayout, &outputIdMap); |
| 546 | XRRFreeScreenResources(res); |
| 547 | |
| 548 | /* Send a dummy event to the root window. When this event is seen, |
| 549 | earlier change events (ConfigureNotify and/or CrtcChange) have |
| 550 | been processed. An Expose event is used for simplicity; does not |
| 551 | require any Atoms, and will not affect other applications. */ |
| 552 | unsigned long serial = XNextRequest(dpy); |
| 553 | XExposeEvent ev = {}; /* zero x, y, width, height, count */ |
| 554 | ev.type = Expose; |
| 555 | ev.display = dpy; |
| 556 | ev.window = DefaultRootWindow(dpy); |
| 557 | if (XSendEvent(dpy, DefaultRootWindow(dpy), False, ExposureMask, (XEvent*)&ev)) { |
| 558 | while (randrSyncSerial < serial) { |
| 559 | TXWindow::handleXEvents(dpy); |
| 560 | } |
| 561 | } else { |
| 562 | vlog.error("XSendEvent failed"); |
| 563 | } |
| 564 | |
| 565 | /* The protocol requires that an error is returned if the requested |
| 566 | layout could not be set. This is checked by |
| 567 | VNCSConnectionST::setDesktopSize. Another ExtendedDesktopSize |
| 568 | with reason=0 will be sent in response to the changes seen by the |
| 569 | event handler. */ |
Pierre Ossman | 44a1d71 | 2018-09-11 14:22:04 +0200 | [diff] [blame] | 570 | if (adjustedLayout != layout) |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 571 | return rfb::resultInvalid; |
Pierre Ossman | 44a1d71 | 2018-09-11 14:22:04 +0200 | [diff] [blame] | 572 | |
| 573 | // Explicitly update the server state with the result as there |
| 574 | // can be corner cases where we don't get feedback from the X server |
| 575 | server->setScreenLayout(computeScreenLayout()); |
| 576 | |
| 577 | return ret; |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 578 | |
| 579 | #else |
| 580 | return rfb::resultProhibited; |
| 581 | #endif /* HAVE_XRANDR */ |
| 582 | } |
| 583 | |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 584 | |
| 585 | bool XDesktop::handleGlobalEvent(XEvent* ev) { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 586 | if (ev->type == xkbEventBase + XkbEventCode) { |
| 587 | XkbEvent *kb = (XkbEvent *)ev; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 588 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 589 | if (kb->any.xkb_type != XkbIndicatorStateNotify) |
| 590 | return false; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 591 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 592 | vlog.debug("Got indicator update, mask is now 0x%x", kb->indicators.state); |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 593 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 594 | ledState = 0; |
| 595 | for (int i = 0; i < XDESKTOP_N_LEDS; i++) { |
| 596 | if (kb->indicators.state & ledMasks[i]) |
| 597 | ledState |= 1u << i; |
Peter Åstrand (astrand) | 3abc7d4 | 2017-10-11 15:12:10 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 600 | if (running) |
| 601 | server->setLEDState(ledState); |
| 602 | |
| 603 | return true; |
| 604 | #ifdef HAVE_XDAMAGE |
| 605 | } else if (ev->type == xdamageEventBase) { |
| 606 | XDamageNotifyEvent* dev; |
| 607 | Rect rect; |
| 608 | |
| 609 | if (!running) |
| 610 | return true; |
| 611 | |
| 612 | dev = (XDamageNotifyEvent*)ev; |
| 613 | rect.setXYWH(dev->area.x, dev->area.y, dev->area.width, dev->area.height); |
| 614 | server->add_changed(rect); |
| 615 | |
| 616 | return true; |
| 617 | #endif |
| 618 | #ifdef HAVE_XFIXES |
| 619 | } else if (ev->type == xfixesEventBase + XFixesCursorNotify) { |
| 620 | XFixesCursorNotifyEvent* cev; |
| 621 | |
| 622 | if (!running) |
| 623 | return true; |
| 624 | |
| 625 | cev = (XFixesCursorNotifyEvent*)ev; |
| 626 | |
| 627 | if (cev->subtype != XFixesDisplayCursorNotify) |
| 628 | return false; |
| 629 | |
| 630 | return setCursor(); |
| 631 | #endif |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 632 | #ifdef HAVE_XRANDR |
| 633 | } else if (ev->type == Expose) { |
| 634 | XExposeEvent* eev = (XExposeEvent*)ev; |
| 635 | randrSyncSerial = eev->serial; |
| 636 | |
| 637 | return false; |
| 638 | |
| 639 | } else if (ev->type == ConfigureNotify) { |
| 640 | XConfigureEvent* cev = (XConfigureEvent*)ev; |
| 641 | |
| 642 | if (cev->window != DefaultRootWindow(dpy)) { |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | XRRUpdateConfiguration(ev); |
| 647 | geometry->recalc(cev->width, cev->height); |
| 648 | |
| 649 | if (!running) { |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | if ((cev->width != pb->width() || (cev->height != pb->height()))) { |
| 654 | // Recreate pixel buffer |
| 655 | ImageFactory factory((bool)useShm); |
| 656 | delete pb; |
| 657 | pb = new XPixelBuffer(dpy, factory, geometry->getRect()); |
| 658 | server->setPixelBuffer(pb, computeScreenLayout()); |
| 659 | |
| 660 | // Mark entire screen as changed |
| 661 | server->add_changed(rfb::Region(Rect(0, 0, cev->width, cev->height))); |
| 662 | } |
| 663 | |
| 664 | return true; |
| 665 | |
| 666 | } else if (ev->type == xrandrEventBase + RRNotify) { |
| 667 | XRRNotifyEvent* rev = (XRRNotifyEvent*)ev; |
| 668 | |
| 669 | if (rev->window != DefaultRootWindow(dpy)) { |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | if (!running) |
| 674 | return false; |
| 675 | |
| 676 | if (rev->subtype == RRNotify_CrtcChange) { |
| 677 | server->setScreenLayout(computeScreenLayout()); |
| 678 | } |
| 679 | |
| 680 | return true; |
| 681 | #endif |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | return false; |
Peter Åstrand (astrand) | 3abc7d4 | 2017-10-11 15:12:10 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | bool XDesktop::setCursor() |
| 688 | { |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 689 | XFixesCursorImage *cim; |
Peter Åstrand (astrand) | 3abc7d4 | 2017-10-11 15:12:10 +0200 | [diff] [blame] | 690 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 691 | cim = XFixesGetCursorImage(dpy); |
| 692 | if (cim == NULL) |
| 693 | return false; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 694 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 695 | // Copied from XserverDesktop::setCursor() in |
| 696 | // unix/xserver/hw/vnc/XserverDesktop.cc and adapted to |
| 697 | // handle long -> U32 conversion for 64-bit Xlib |
| 698 | rdr::U8* cursorData; |
| 699 | rdr::U8 *out; |
| 700 | const unsigned long *pixels; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 701 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 702 | cursorData = new rdr::U8[cim->width * cim->height * 4]; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 703 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 704 | // Un-premultiply alpha |
| 705 | pixels = cim->pixels; |
| 706 | out = cursorData; |
| 707 | for (int y = 0; y < cim->height; y++) { |
| 708 | for (int x = 0; x < cim->width; x++) { |
| 709 | rdr::U8 alpha; |
| 710 | rdr::U32 pixel = *pixels++; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 711 | |
Pierre Ossman | 6d0a5a2 | 2018-09-18 15:41:25 +0200 | [diff] [blame] | 712 | alpha = (pixel >> 24) & 0xff; |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 713 | if (alpha == 0) |
| 714 | alpha = 1; // Avoid division by zero |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 715 | |
Pierre Ossman | 6d0a5a2 | 2018-09-18 15:41:25 +0200 | [diff] [blame] | 716 | *out++ = ((pixel >> 16) & 0xff) * 255/alpha; |
| 717 | *out++ = ((pixel >> 8) & 0xff) * 255/alpha; |
| 718 | *out++ = ((pixel >> 0) & 0xff) * 255/alpha; |
| 719 | *out++ = ((pixel >> 24) & 0xff); |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 720 | } |
| 721 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 722 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 723 | try { |
| 724 | server->setCursor(cim->width, cim->height, Point(cim->xhot, cim->yhot), |
| 725 | cursorData); |
| 726 | } catch (rdr::Exception& e) { |
| 727 | vlog.error("XserverDesktop::setCursor: %s",e.str()); |
| 728 | } |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 729 | |
Pierre Ossman | 07580a8 | 2018-03-07 15:33:42 +0100 | [diff] [blame] | 730 | delete [] cursorData; |
| 731 | XFree(cim); |
| 732 | return true; |
Peter Åstrand (astrand) | 3112f50 | 2017-10-10 12:27:38 +0200 | [diff] [blame] | 733 | } |