You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncSelection.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* Copyright 2016-2019 Pierre Ossman for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_DIX_CONFIG_H
  19. #include <dix-config.h>
  20. #endif
  21. #include <X11/Xatom.h>
  22. #include "propertyst.h"
  23. #include "scrnintstr.h"
  24. #include "selection.h"
  25. #include "windowstr.h"
  26. #include "xace.h"
  27. #include "xorg-version.h"
  28. #include "vncExtInit.h"
  29. #include "vncSelection.h"
  30. #include "RFBGlue.h"
  31. #define LOG_NAME "Selection"
  32. #define LOG_ERROR(...) vncLogError(LOG_NAME, __VA_ARGS__)
  33. #define LOG_STATUS(...) vncLogStatus(LOG_NAME, __VA_ARGS__)
  34. #define LOG_INFO(...) vncLogInfo(LOG_NAME, __VA_ARGS__)
  35. #define LOG_DEBUG(...) vncLogDebug(LOG_NAME, __VA_ARGS__)
  36. static Atom xaPRIMARY, xaCLIPBOARD;
  37. static Atom xaTARGETS, xaTIMESTAMP, xaSTRING, xaTEXT, xaUTF8_STRING;
  38. static WindowPtr pWindow;
  39. static Window wid;
  40. static char* clientCutText;
  41. static int vncCreateSelectionWindow(void);
  42. static int vncOwnSelection(Atom selection);
  43. static int vncProcConvertSelection(ClientPtr client);
  44. static int vncProcSendEvent(ClientPtr client);
  45. static void vncSelectionCallback(CallbackListPtr *callbacks,
  46. void * data, void * args);
  47. static int (*origProcConvertSelection)(ClientPtr);
  48. static int (*origProcSendEvent)(ClientPtr);
  49. void vncSelectionInit(void)
  50. {
  51. xaPRIMARY = MakeAtom("PRIMARY", 7, TRUE);
  52. xaCLIPBOARD = MakeAtom("CLIPBOARD", 9, TRUE);
  53. xaTARGETS = MakeAtom("TARGETS", 7, TRUE);
  54. xaTIMESTAMP = MakeAtom("TIMESTAMP", 9, TRUE);
  55. xaSTRING = MakeAtom("STRING", 6, TRUE);
  56. xaTEXT = MakeAtom("TEXT", 4, TRUE);
  57. xaUTF8_STRING = MakeAtom("UTF8_STRING", 11, TRUE);
  58. /* There are no hooks for when these are internal windows, so
  59. * override the relevant handlers. */
  60. origProcConvertSelection = ProcVector[X_ConvertSelection];
  61. ProcVector[X_ConvertSelection] = vncProcConvertSelection;
  62. origProcSendEvent = ProcVector[X_SendEvent];
  63. ProcVector[X_SendEvent] = vncProcSendEvent;
  64. if (!AddCallback(&SelectionCallback, vncSelectionCallback, 0))
  65. FatalError("Add VNC SelectionCallback failed\n");
  66. }
  67. void vncClientCutText(const char* str)
  68. {
  69. int rc;
  70. if (clientCutText != NULL)
  71. free(clientCutText);
  72. clientCutText = strdup(str);
  73. if (clientCutText == NULL) {
  74. LOG_ERROR("Could not allocate clipboard buffer");
  75. DeleteWindowFromAnySelections(pWindow);
  76. return;
  77. }
  78. if (vncGetSetPrimary()) {
  79. rc = vncOwnSelection(xaPRIMARY);
  80. if (rc != Success)
  81. LOG_ERROR("Could not set PRIMARY selection");
  82. }
  83. rc = vncOwnSelection(xaCLIPBOARD);
  84. if (rc != Success)
  85. LOG_ERROR("Could not set CLIPBOARD selection");
  86. }
  87. static int vncCreateSelectionWindow(void)
  88. {
  89. ScreenPtr pScreen;
  90. int result;
  91. if (pWindow != NULL)
  92. return Success;
  93. pScreen = screenInfo.screens[0];
  94. wid = FakeClientID(0);
  95. pWindow = CreateWindow(wid, pScreen->root,
  96. 0, 0, 100, 100, 0, InputOnly,
  97. 0, NULL, 0, serverClient,
  98. CopyFromParent, &result);
  99. if (!pWindow)
  100. return result;
  101. if (!AddResource(pWindow->drawable.id, RT_WINDOW, pWindow))
  102. return BadAlloc;
  103. LOG_DEBUG("Created selection window");
  104. return Success;
  105. }
  106. static int vncOwnSelection(Atom selection)
  107. {
  108. Selection *pSel;
  109. int rc;
  110. SelectionInfoRec info;
  111. rc = vncCreateSelectionWindow();
  112. if (rc != Success)
  113. return rc;
  114. rc = dixLookupSelection(&pSel, selection, serverClient, DixSetAttrAccess);
  115. if (rc == Success) {
  116. if (pSel->client && (pSel->client != serverClient)) {
  117. xEvent event = {
  118. .u.selectionClear.time = currentTime.milliseconds,
  119. .u.selectionClear.window = pSel->window,
  120. .u.selectionClear.atom = pSel->selection
  121. };
  122. event.u.u.type = SelectionClear;
  123. WriteEventsToClient(pSel->client, 1, &event);
  124. }
  125. } else if (rc == BadMatch) {
  126. pSel = dixAllocateObjectWithPrivates(Selection, PRIVATE_SELECTION);
  127. if (!pSel)
  128. return BadAlloc;
  129. pSel->selection = selection;
  130. rc = XaceHookSelectionAccess(serverClient, &pSel,
  131. DixCreateAccess | DixSetAttrAccess);
  132. if (rc != Success) {
  133. free(pSel);
  134. return rc;
  135. }
  136. pSel->next = CurrentSelections;
  137. CurrentSelections = pSel;
  138. }
  139. else
  140. return rc;
  141. pSel->lastTimeChanged = currentTime;
  142. pSel->window = wid;
  143. pSel->pWin = pWindow;
  144. pSel->client = serverClient;
  145. LOG_DEBUG("Grabbed %s selection", NameForAtom(selection));
  146. info.selection = pSel;
  147. info.client = serverClient;
  148. info.kind = SelectionSetOwner;
  149. CallCallbacks(&SelectionCallback, &info);
  150. return Success;
  151. }
  152. static int vncConvertSelection(ClientPtr client, Atom selection,
  153. Atom target, Atom property,
  154. Window requestor, CARD32 time)
  155. {
  156. Selection *pSel;
  157. WindowPtr pWin;
  158. int rc;
  159. Atom realProperty;
  160. xEvent event;
  161. LOG_DEBUG("Selection request for %s (type %s)",
  162. NameForAtom(selection), NameForAtom(target));
  163. rc = dixLookupSelection(&pSel, selection, client, DixGetAttrAccess);
  164. if (rc != Success)
  165. return rc;
  166. /* We do not validate the time argument because neither does
  167. * dix/selection.c and some clients (e.g. Qt) relies on this */
  168. rc = dixLookupWindow(&pWin, requestor, client, DixSetAttrAccess);
  169. if (rc != Success)
  170. return rc;
  171. if (property != None)
  172. realProperty = property;
  173. else
  174. realProperty = target;
  175. /* FIXME: MULTIPLE target */
  176. if (target == xaTARGETS) {
  177. Atom targets[] = { xaTARGETS, xaTIMESTAMP,
  178. xaSTRING, xaTEXT, xaUTF8_STRING };
  179. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  180. XA_ATOM, 32, PropModeReplace,
  181. sizeof(targets)/sizeof(targets[0]),
  182. targets, TRUE);
  183. if (rc != Success)
  184. return rc;
  185. } else if (target == xaTIMESTAMP) {
  186. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  187. XA_INTEGER, 32, PropModeReplace, 1,
  188. &pSel->lastTimeChanged.milliseconds,
  189. TRUE);
  190. if (rc != Success)
  191. return rc;
  192. } else if ((target == xaSTRING) || (target == xaTEXT)) {
  193. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  194. XA_STRING, 8, PropModeReplace,
  195. strlen(clientCutText), clientCutText,
  196. TRUE);
  197. if (rc != Success)
  198. return rc;
  199. } else if (target == xaUTF8_STRING) {
  200. unsigned char* buffer;
  201. unsigned char* out;
  202. size_t len;
  203. const unsigned char* in;
  204. size_t in_len;
  205. buffer = malloc(strlen(clientCutText)*2);
  206. if (buffer == NULL)
  207. return BadAlloc;
  208. out = buffer;
  209. len = 0;
  210. in = clientCutText;
  211. while (*in != '\0') {
  212. if (*in & 0x80) {
  213. *out++ = 0xc0 | (*in >> 6);
  214. *out++ = 0x80 | (*in & 0x3f);
  215. len += 2;
  216. in++;
  217. } else {
  218. *out++ = *in++;
  219. len++;
  220. }
  221. }
  222. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  223. xaUTF8_STRING, 8, PropModeReplace,
  224. len, buffer, TRUE);
  225. free(buffer);
  226. if (rc != Success)
  227. return rc;
  228. } else {
  229. return BadMatch;
  230. }
  231. event.u.u.type = SelectionNotify;
  232. event.u.selectionNotify.time = time;
  233. event.u.selectionNotify.requestor = requestor;
  234. event.u.selectionNotify.selection = selection;
  235. event.u.selectionNotify.target = target;
  236. event.u.selectionNotify.property = property;
  237. WriteEventsToClient(client, 1, &event);
  238. return Success;
  239. }
  240. static int vncProcConvertSelection(ClientPtr client)
  241. {
  242. Bool paramsOkay;
  243. WindowPtr pWin;
  244. Selection *pSel;
  245. int rc;
  246. REQUEST(xConvertSelectionReq);
  247. REQUEST_SIZE_MATCH(xConvertSelectionReq);
  248. rc = dixLookupWindow(&pWin, stuff->requestor, client, DixSetAttrAccess);
  249. if (rc != Success)
  250. return rc;
  251. paramsOkay = ValidAtom(stuff->selection) && ValidAtom(stuff->target);
  252. paramsOkay &= (stuff->property == None) || ValidAtom(stuff->property);
  253. if (!paramsOkay) {
  254. client->errorValue = stuff->property;
  255. return BadAtom;
  256. }
  257. rc = dixLookupSelection(&pSel, stuff->selection, client, DixReadAccess);
  258. if (rc == Success && pSel->client == serverClient &&
  259. pSel->window == wid) {
  260. rc = vncConvertSelection(client, stuff->selection,
  261. stuff->target, stuff->property,
  262. stuff->requestor, stuff->time);
  263. if (rc != Success) {
  264. xEvent event;
  265. memset(&event, 0, sizeof(xEvent));
  266. event.u.u.type = SelectionNotify;
  267. event.u.selectionNotify.time = stuff->time;
  268. event.u.selectionNotify.requestor = stuff->requestor;
  269. event.u.selectionNotify.selection = stuff->selection;
  270. event.u.selectionNotify.target = stuff->target;
  271. event.u.selectionNotify.property = None;
  272. WriteEventsToClient(client, 1, &event);
  273. }
  274. return Success;
  275. }
  276. return origProcConvertSelection(client);
  277. }
  278. static void vncSelectionRequest(Atom selection, Atom target)
  279. {
  280. Selection *pSel;
  281. xEvent event;
  282. int rc;
  283. rc = vncCreateSelectionWindow();
  284. if (rc != Success)
  285. return;
  286. LOG_DEBUG("Requesting %s for %s selection",
  287. NameForAtom(target), NameForAtom(selection));
  288. rc = dixLookupSelection(&pSel, selection, serverClient, DixGetAttrAccess);
  289. if (rc != Success)
  290. return;
  291. event.u.u.type = SelectionRequest;
  292. event.u.selectionRequest.owner = pSel->window;
  293. event.u.selectionRequest.time = currentTime.milliseconds;
  294. event.u.selectionRequest.requestor = wid;
  295. event.u.selectionRequest.selection = selection;
  296. event.u.selectionRequest.target = target;
  297. event.u.selectionRequest.property = target;
  298. WriteEventsToClient(pSel->client, 1, &event);
  299. }
  300. static Bool vncHasAtom(Atom atom, const Atom list[], size_t size)
  301. {
  302. size_t i;
  303. for (i = 0;i < size;i++) {
  304. if (list[i] == atom)
  305. return TRUE;
  306. }
  307. return FALSE;
  308. }
  309. static void vncHandleSelection(Atom selection, Atom target,
  310. Atom property, Atom requestor,
  311. TimeStamp time)
  312. {
  313. PropertyPtr prop;
  314. int rc;
  315. rc = dixLookupProperty(&prop, pWindow, property,
  316. serverClient, DixReadAccess);
  317. if (rc != Success)
  318. return;
  319. LOG_DEBUG("Selection notification for %s (target %s, property %s, type %s)",
  320. NameForAtom(selection), NameForAtom(target),
  321. NameForAtom(property), NameForAtom(prop->type));
  322. if (target != property)
  323. return;
  324. if (target == xaTARGETS) {
  325. if (prop->format != 32)
  326. return;
  327. if (prop->type != XA_ATOM)
  328. return;
  329. if (vncHasAtom(xaSTRING, (const Atom*)prop->data, prop->size))
  330. vncSelectionRequest(selection, xaSTRING);
  331. else if (vncHasAtom(xaUTF8_STRING, (const Atom*)prop->data, prop->size))
  332. vncSelectionRequest(selection, xaUTF8_STRING);
  333. } else if (target == xaSTRING) {
  334. char* filtered;
  335. if (prop->format != 8)
  336. return;
  337. if (prop->type != xaSTRING)
  338. return;
  339. filtered = vncConvertLF(prop->data, prop->size);
  340. if (filtered == NULL)
  341. return;
  342. vncServerCutText(filtered);
  343. vncStrFree(filtered);
  344. } else if (target == xaUTF8_STRING) {
  345. char *filtered;
  346. unsigned char* buffer;
  347. unsigned char* out;
  348. size_t len;
  349. const unsigned char* in;
  350. size_t in_len;
  351. if (prop->format != 8)
  352. return;
  353. if (prop->type != xaUTF8_STRING)
  354. return;
  355. buffer = malloc(prop->size);
  356. if (buffer == NULL)
  357. return;
  358. out = buffer;
  359. len = 0;
  360. in = prop->data;
  361. in_len = prop->size;
  362. while (in_len > 0) {
  363. if ((*in & 0x80) == 0x00) {
  364. *out++ = *in++;
  365. len++;
  366. in_len--;
  367. } else if ((*in & 0xe0) == 0xc0) {
  368. unsigned ucs;
  369. ucs = (*in++ & 0x1f) << 6;
  370. in_len--;
  371. if (in_len > 0) {
  372. ucs |= (*in++ & 0x3f);
  373. in_len--;
  374. }
  375. if (ucs <= 0xff)
  376. *out++ = ucs;
  377. else
  378. *out++ = '?';
  379. len++;
  380. } else {
  381. *out++ = '?';
  382. len++;
  383. do {
  384. in++;
  385. in_len--;
  386. } while ((in_len > 0) && ((*in & 0xc0) == 0x80));
  387. }
  388. }
  389. filtered = vncConvertLF(buffer, len);
  390. free(buffer);
  391. if (filtered == NULL)
  392. return;
  393. vncServerCutText(filtered);
  394. vncStrFree(filtered);
  395. }
  396. }
  397. #define SEND_EVENT_BIT 0x80
  398. static int vncProcSendEvent(ClientPtr client)
  399. {
  400. REQUEST(xSendEventReq);
  401. REQUEST_SIZE_MATCH(xSendEventReq);
  402. stuff->event.u.u.type &= ~(SEND_EVENT_BIT);
  403. if (stuff->event.u.u.type == SelectionNotify &&
  404. stuff->event.u.selectionNotify.requestor == wid) {
  405. TimeStamp time;
  406. time = ClientTimeToServerTime(stuff->event.u.selectionNotify.time);
  407. vncHandleSelection(stuff->event.u.selectionNotify.selection,
  408. stuff->event.u.selectionNotify.target,
  409. stuff->event.u.selectionNotify.property,
  410. stuff->event.u.selectionNotify.requestor,
  411. time);
  412. }
  413. return origProcSendEvent(client);
  414. }
  415. static void vncSelectionCallback(CallbackListPtr *callbacks,
  416. void * data, void * args)
  417. {
  418. SelectionInfoRec *info = (SelectionInfoRec *) args;
  419. if (info->kind != SelectionSetOwner)
  420. return;
  421. if (info->client == serverClient)
  422. return;
  423. LOG_DEBUG("Selection owner change for %s",
  424. NameForAtom(info->selection->selection));
  425. if ((info->selection->selection != xaPRIMARY) &&
  426. (info->selection->selection != xaCLIPBOARD))
  427. return;
  428. if ((info->selection->selection == xaPRIMARY) &&
  429. !vncGetSendPrimary())
  430. return;
  431. vncSelectionRequest(info->selection->selection, xaTARGETS);
  432. }