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