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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* Copyright 2016 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 clientCutTextLen;
  42. static int vncCreateSelectionWindow(void);
  43. static int vncOwnSelection(Atom selection);
  44. static int vncProcConvertSelection(ClientPtr client);
  45. static int vncProcSendEvent(ClientPtr client);
  46. static void vncSelectionCallback(CallbackListPtr *callbacks,
  47. void * data, void * args);
  48. static int (*origProcConvertSelection)(ClientPtr);
  49. static int (*origProcSendEvent)(ClientPtr);
  50. void vncSelectionInit(void)
  51. {
  52. xaPRIMARY = MakeAtom("PRIMARY", 7, TRUE);
  53. xaCLIPBOARD = MakeAtom("CLIPBOARD", 9, TRUE);
  54. xaTARGETS = MakeAtom("TARGETS", 7, TRUE);
  55. xaTIMESTAMP = MakeAtom("TIMESTAMP", 9, TRUE);
  56. xaSTRING = MakeAtom("STRING", 6, TRUE);
  57. xaTEXT = MakeAtom("TEXT", 4, TRUE);
  58. xaUTF8_STRING = MakeAtom("UTF8_STRING", 11, TRUE);
  59. /* There are no hooks for when these are internal windows, so
  60. * override the relevant handlers. */
  61. origProcConvertSelection = ProcVector[X_ConvertSelection];
  62. ProcVector[X_ConvertSelection] = vncProcConvertSelection;
  63. origProcSendEvent = ProcVector[X_SendEvent];
  64. ProcVector[X_SendEvent] = vncProcSendEvent;
  65. if (!AddCallback(&SelectionCallback, vncSelectionCallback, 0))
  66. FatalError("Add VNC SelectionCallback failed\n");
  67. }
  68. void vncClientCutText(const char* str, int len)
  69. {
  70. int rc;
  71. if (clientCutText != NULL)
  72. free(clientCutText);
  73. clientCutText = malloc(len);
  74. if (clientCutText == NULL) {
  75. LOG_ERROR("Could not allocate clipboard buffer");
  76. DeleteWindowFromAnySelections(pWindow);
  77. return;
  78. }
  79. memcpy(clientCutText, str, len);
  80. clientCutTextLen = len;
  81. if (vncGetSetPrimary()) {
  82. rc = vncOwnSelection(xaPRIMARY);
  83. if (rc != Success)
  84. LOG_ERROR("Could not set PRIMARY selection");
  85. }
  86. vncOwnSelection(xaCLIPBOARD);
  87. if (rc != Success)
  88. LOG_ERROR("Could not set CLIPBOARD selection");
  89. }
  90. static int vncCreateSelectionWindow(void)
  91. {
  92. ScreenPtr pScreen;
  93. int result;
  94. if (pWindow != NULL)
  95. return Success;
  96. pScreen = screenInfo.screens[0];
  97. wid = FakeClientID(0);
  98. pWindow = CreateWindow(wid, pScreen->root,
  99. 0, 0, 100, 100, 0, InputOnly,
  100. 0, NULL, 0, serverClient,
  101. CopyFromParent, &result);
  102. if (!pWindow)
  103. return result;
  104. if (!AddResource(pWindow->drawable.id, RT_WINDOW, pWindow))
  105. return BadAlloc;
  106. LOG_DEBUG("Created selection window");
  107. return Success;
  108. }
  109. static int vncOwnSelection(Atom selection)
  110. {
  111. Selection *pSel;
  112. int rc;
  113. SelectionInfoRec info;
  114. rc = vncCreateSelectionWindow();
  115. if (rc != Success)
  116. return rc;
  117. rc = dixLookupSelection(&pSel, selection, serverClient, DixSetAttrAccess);
  118. if (rc == Success) {
  119. if (pSel->client && (pSel->client != serverClient)) {
  120. xEvent event = {
  121. .u.selectionClear.time = currentTime.milliseconds,
  122. .u.selectionClear.window = pSel->window,
  123. .u.selectionClear.atom = pSel->selection
  124. };
  125. event.u.u.type = SelectionClear;
  126. WriteEventsToClient(pSel->client, 1, &event);
  127. }
  128. } else if (rc == BadMatch) {
  129. pSel = dixAllocateObjectWithPrivates(Selection, PRIVATE_SELECTION);
  130. if (!pSel)
  131. return BadAlloc;
  132. pSel->selection = selection;
  133. rc = XaceHookSelectionAccess(serverClient, &pSel,
  134. DixCreateAccess | DixSetAttrAccess);
  135. if (rc != Success) {
  136. free(pSel);
  137. return rc;
  138. }
  139. pSel->next = CurrentSelections;
  140. CurrentSelections = pSel;
  141. }
  142. else
  143. return rc;
  144. pSel->lastTimeChanged = currentTime;
  145. pSel->window = wid;
  146. pSel->pWin = pWindow;
  147. pSel->client = serverClient;
  148. LOG_DEBUG("Grabbed %s selection", NameForAtom(selection));
  149. info.selection = pSel;
  150. info.client = serverClient;
  151. info.kind = SelectionSetOwner;
  152. CallCallbacks(&SelectionCallback, &info);
  153. return Success;
  154. }
  155. static int vncConvertSelection(ClientPtr client, Atom selection,
  156. Atom target, Atom property,
  157. Window requestor, CARD32 time)
  158. {
  159. Selection *pSel;
  160. WindowPtr pWin;
  161. int rc;
  162. Atom realProperty;
  163. xEvent event;
  164. LOG_DEBUG("Selection request for %s (type %s)",
  165. NameForAtom(selection), NameForAtom(target));
  166. rc = dixLookupSelection(&pSel, selection, client, DixGetAttrAccess);
  167. if (rc != Success)
  168. return rc;
  169. /* We do not validate the time argument because neither does
  170. * dix/selection.c and some clients (e.g. Qt) relies on this */
  171. rc = dixLookupWindow(&pWin, requestor, client, DixSetAttrAccess);
  172. if (rc != Success)
  173. return rc;
  174. if (property != None)
  175. realProperty = property;
  176. else
  177. realProperty = target;
  178. /* FIXME: MULTIPLE target */
  179. if (target == xaTARGETS) {
  180. Atom targets[] = { xaTARGETS, xaTIMESTAMP,
  181. xaSTRING, xaTEXT, xaUTF8_STRING };
  182. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  183. XA_ATOM, 32, PropModeReplace,
  184. sizeof(targets)/sizeof(targets[0]),
  185. targets, TRUE);
  186. if (rc != Success)
  187. return rc;
  188. } else if (target == xaTIMESTAMP) {
  189. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  190. XA_INTEGER, 32, PropModeReplace, 1,
  191. &pSel->lastTimeChanged.milliseconds,
  192. TRUE);
  193. if (rc != Success)
  194. return rc;
  195. } else if ((target == xaSTRING) || (target == xaTEXT)) {
  196. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  197. XA_STRING, 8, PropModeReplace,
  198. clientCutTextLen, clientCutText,
  199. TRUE);
  200. if (rc != Success)
  201. return rc;
  202. } else if (target == xaUTF8_STRING) {
  203. unsigned char* buffer;
  204. unsigned char* out;
  205. size_t len;
  206. const unsigned char* in;
  207. size_t in_len;
  208. buffer = malloc(clientCutTextLen*2);
  209. if (buffer == NULL)
  210. return BadAlloc;
  211. out = buffer;
  212. len = 0;
  213. in = clientCutText;
  214. in_len = clientCutTextLen;
  215. while (in_len > 0) {
  216. if (*in & 0x80) {
  217. *out++ = 0xc0 | (*in >> 6);
  218. *out++ = 0x80 | (*in & 0x3f);
  219. len += 2;
  220. in++;
  221. in_len--;
  222. } else {
  223. *out++ = *in++;
  224. len++;
  225. in_len--;
  226. }
  227. }
  228. rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
  229. xaUTF8_STRING, 8, PropModeReplace,
  230. len, buffer, TRUE);
  231. free(buffer);
  232. if (rc != Success)
  233. return rc;
  234. } else {
  235. return BadMatch;
  236. }
  237. event.u.u.type = SelectionNotify;
  238. event.u.selectionNotify.time = time;
  239. event.u.selectionNotify.requestor = requestor;
  240. event.u.selectionNotify.selection = selection;
  241. event.u.selectionNotify.target = target;
  242. event.u.selectionNotify.property = property;
  243. WriteEventsToClient(client, 1, &event);
  244. return Success;
  245. }
  246. static int vncProcConvertSelection(ClientPtr client)
  247. {
  248. Bool paramsOkay;
  249. WindowPtr pWin;
  250. Selection *pSel;
  251. int rc;
  252. REQUEST(xConvertSelectionReq);
  253. REQUEST_SIZE_MATCH(xConvertSelectionReq);
  254. rc = dixLookupWindow(&pWin, stuff->requestor, client, DixSetAttrAccess);
  255. if (rc != Success)
  256. return rc;
  257. paramsOkay = ValidAtom(stuff->selection) && ValidAtom(stuff->target);
  258. paramsOkay &= (stuff->property == None) || ValidAtom(stuff->property);
  259. if (!paramsOkay) {
  260. client->errorValue = stuff->property;
  261. return BadAtom;
  262. }
  263. rc = dixLookupSelection(&pSel, stuff->selection, client, DixReadAccess);
  264. if (rc == Success && pSel->client == serverClient &&
  265. pSel->window == wid) {
  266. rc = vncConvertSelection(client, stuff->selection,
  267. stuff->target, stuff->property,
  268. stuff->requestor, stuff->time);
  269. if (rc != Success) {
  270. xEvent event;
  271. memset(&event, 0, sizeof(xEvent));
  272. event.u.u.type = SelectionNotify;
  273. event.u.selectionNotify.time = stuff->time;
  274. event.u.selectionNotify.requestor = stuff->requestor;
  275. event.u.selectionNotify.selection = stuff->selection;
  276. event.u.selectionNotify.target = stuff->target;
  277. event.u.selectionNotify.property = None;
  278. WriteEventsToClient(client, 1, &event);
  279. }
  280. return Success;
  281. }
  282. return origProcConvertSelection(client);
  283. }
  284. static void vncSelectionRequest(Atom selection, Atom target)
  285. {
  286. Selection *pSel;
  287. xEvent event;
  288. int rc;
  289. rc = vncCreateSelectionWindow();
  290. if (rc != Success)
  291. return;
  292. LOG_DEBUG("Requesting %s for %s selection",
  293. NameForAtom(target), NameForAtom(selection));
  294. rc = dixLookupSelection(&pSel, selection, serverClient, DixGetAttrAccess);
  295. if (rc != Success)
  296. return;
  297. event.u.u.type = SelectionRequest;
  298. event.u.selectionRequest.owner = pSel->window;
  299. event.u.selectionRequest.time = currentTime.milliseconds;
  300. event.u.selectionRequest.requestor = wid;
  301. event.u.selectionRequest.selection = selection;
  302. event.u.selectionRequest.target = target;
  303. event.u.selectionRequest.property = target;
  304. WriteEventsToClient(pSel->client, 1, &event);
  305. }
  306. static Bool vncHasAtom(Atom atom, const Atom list[], size_t size)
  307. {
  308. size_t i;
  309. for (i = 0;i < size;i++) {
  310. if (list[i] == atom)
  311. return TRUE;
  312. }
  313. return FALSE;
  314. }
  315. static void vncHandleSelection(Atom selection, Atom target,
  316. Atom property, Atom requestor,
  317. TimeStamp time)
  318. {
  319. PropertyPtr prop;
  320. int rc;
  321. rc = dixLookupProperty(&prop, pWindow, property,
  322. serverClient, DixReadAccess);
  323. if (rc != Success)
  324. return;
  325. LOG_DEBUG("Selection notification for %s (target %s, property %s, type %s)",
  326. NameForAtom(selection), NameForAtom(target),
  327. NameForAtom(property), NameForAtom(prop->type));
  328. if (target != property)
  329. return;
  330. if (target == xaTARGETS) {
  331. if (prop->format != 32)
  332. return;
  333. if (prop->type != XA_ATOM)
  334. return;
  335. if (vncHasAtom(xaSTRING, (const Atom*)prop->data, prop->size))
  336. vncSelectionRequest(selection, xaSTRING);
  337. else if (vncHasAtom(xaUTF8_STRING, (const Atom*)prop->data, prop->size))
  338. vncSelectionRequest(selection, xaUTF8_STRING);
  339. } else if (target == xaSTRING) {
  340. if (prop->format != 8)
  341. return;
  342. if (prop->type != xaSTRING)
  343. return;
  344. vncServerCutText(prop->data, prop->size);
  345. } else if (target == xaUTF8_STRING) {
  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. vncServerCutText((const char*)buffer, len);
  390. free(buffer);
  391. }
  392. }
  393. #define SEND_EVENT_BIT 0x80
  394. static int vncProcSendEvent(ClientPtr client)
  395. {
  396. REQUEST(xSendEventReq);
  397. REQUEST_SIZE_MATCH(xSendEventReq);
  398. stuff->event.u.u.type &= ~(SEND_EVENT_BIT);
  399. if (stuff->event.u.u.type == SelectionNotify &&
  400. stuff->event.u.selectionNotify.requestor == wid) {
  401. TimeStamp time;
  402. time = ClientTimeToServerTime(stuff->event.u.selectionNotify.time);
  403. vncHandleSelection(stuff->event.u.selectionNotify.selection,
  404. stuff->event.u.selectionNotify.target,
  405. stuff->event.u.selectionNotify.property,
  406. stuff->event.u.selectionNotify.requestor,
  407. time);
  408. }
  409. return origProcSendEvent(client);
  410. }
  411. static void vncSelectionCallback(CallbackListPtr *callbacks,
  412. void * data, void * args)
  413. {
  414. SelectionInfoRec *info = (SelectionInfoRec *) args;
  415. if (info->kind != SelectionSetOwner)
  416. return;
  417. if (info->client == serverClient)
  418. return;
  419. LOG_DEBUG("Selection owner change for %s",
  420. NameForAtom(info->selection->selection));
  421. if ((info->selection->selection != xaPRIMARY) &&
  422. (info->selection->selection != xaCLIPBOARD))
  423. return;
  424. if ((info->selection->selection == xaPRIMARY) &&
  425. !vncGetSendPrimary())
  426. return;
  427. vncSelectionRequest(info->selection->selection, xaTARGETS);
  428. }