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.

InputXKB.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* Copyright (C) 2009 TightVNC Team
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. * Copyright 2013-2018 Pierre Ossman for Cendio AB
  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. #ifdef HAVE_DIX_CONFIG_H
  21. #include <dix-config.h>
  22. #endif
  23. #include "xorg-version.h"
  24. #include <stdio.h>
  25. #include <X11/keysym.h>
  26. #include <X11/Xlib.h>
  27. #include <X11/Xutil.h>
  28. #include "xkbsrv.h"
  29. #include "xkbstr.h"
  30. #include "eventstr.h"
  31. #include "scrnintstr.h"
  32. #include "mi.h"
  33. #include "Input.h"
  34. #ifndef KEYBOARD_OR_FLOAT
  35. #define KEYBOARD_OR_FLOAT MASTER_KEYBOARD
  36. #endif
  37. #if XORG < 118
  38. #if XORG < 110
  39. #define GetMaster(dev, type) ((dev)->u.master)
  40. #else
  41. #define GetMaster(dev, type) ((dev)->master)
  42. #endif
  43. #endif
  44. extern DeviceIntPtr vncKeyboardDev;
  45. static const KeyCode fakeKeys[] = {
  46. #ifdef __linux__
  47. 92, 203, 204, 205, 206, 207
  48. #else
  49. 8, 124, 125, 156, 127, 128
  50. #endif
  51. };
  52. static void vncXkbProcessDeviceEvent(int screenNum,
  53. InternalEvent *event,
  54. DeviceIntPtr dev);
  55. /* Stolen from libX11 */
  56. static Bool
  57. XkbTranslateKeyCode(register XkbDescPtr xkb, KeyCode key,
  58. register unsigned int mods, unsigned int *mods_rtrn,
  59. KeySym *keysym_rtrn)
  60. {
  61. XkbKeyTypeRec *type;
  62. int col,nKeyGroups;
  63. unsigned preserve,effectiveGroup;
  64. KeySym *syms;
  65. if (mods_rtrn!=NULL)
  66. *mods_rtrn = 0;
  67. nKeyGroups= XkbKeyNumGroups(xkb,key);
  68. if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
  69. if (keysym_rtrn!=NULL)
  70. *keysym_rtrn = NoSymbol;
  71. return False;
  72. }
  73. syms = XkbKeySymsPtr(xkb,key);
  74. /* find the offset of the effective group */
  75. col = 0;
  76. effectiveGroup= XkbGroupForCoreState(mods);
  77. if ( effectiveGroup>=nKeyGroups ) {
  78. unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
  79. switch (XkbOutOfRangeGroupAction(groupInfo)) {
  80. default:
  81. effectiveGroup %= nKeyGroups;
  82. break;
  83. case XkbClampIntoRange:
  84. effectiveGroup = nKeyGroups-1;
  85. break;
  86. case XkbRedirectIntoRange:
  87. effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
  88. if (effectiveGroup>=nKeyGroups)
  89. effectiveGroup= 0;
  90. break;
  91. }
  92. }
  93. col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
  94. type = XkbKeyKeyType(xkb,key,effectiveGroup);
  95. preserve= 0;
  96. if (type->map) { /* find the column (shift level) within the group */
  97. register int i;
  98. register XkbKTMapEntryPtr entry;
  99. for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
  100. if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
  101. col+= entry->level;
  102. if (type->preserve)
  103. preserve= type->preserve[i].mask;
  104. break;
  105. }
  106. }
  107. }
  108. if (keysym_rtrn!=NULL)
  109. *keysym_rtrn= syms[col];
  110. if (mods_rtrn)
  111. *mods_rtrn= type->mods.mask&(~preserve);
  112. return (syms[col]!=NoSymbol);
  113. }
  114. static XkbAction *XkbKeyActionPtr(XkbDescPtr xkb, KeyCode key, unsigned int mods)
  115. {
  116. XkbKeyTypeRec *type;
  117. int col,nKeyGroups;
  118. unsigned effectiveGroup;
  119. XkbAction *acts;
  120. if (!XkbKeyHasActions(xkb, key))
  121. return NULL;
  122. nKeyGroups= XkbKeyNumGroups(xkb,key);
  123. if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0))
  124. return NULL;
  125. acts = XkbKeyActionsPtr(xkb,key);
  126. /* find the offset of the effective group */
  127. col = 0;
  128. effectiveGroup= XkbGroupForCoreState(mods);
  129. if ( effectiveGroup>=nKeyGroups ) {
  130. unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
  131. switch (XkbOutOfRangeGroupAction(groupInfo)) {
  132. default:
  133. effectiveGroup %= nKeyGroups;
  134. break;
  135. case XkbClampIntoRange:
  136. effectiveGroup = nKeyGroups-1;
  137. break;
  138. case XkbRedirectIntoRange:
  139. effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
  140. if (effectiveGroup>=nKeyGroups)
  141. effectiveGroup= 0;
  142. break;
  143. }
  144. }
  145. col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
  146. type = XkbKeyKeyType(xkb,key,effectiveGroup);
  147. if (type->map) { /* find the column (shift level) within the group */
  148. register int i;
  149. register XkbKTMapEntryPtr entry;
  150. for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
  151. if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
  152. col+= entry->level;
  153. break;
  154. }
  155. }
  156. }
  157. return &acts[col];
  158. }
  159. static unsigned XkbKeyEffectiveGroup(XkbDescPtr xkb, KeyCode key, unsigned int mods)
  160. {
  161. int nKeyGroups;
  162. unsigned effectiveGroup;
  163. nKeyGroups= XkbKeyNumGroups(xkb,key);
  164. if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0))
  165. return 0;
  166. effectiveGroup= XkbGroupForCoreState(mods);
  167. if ( effectiveGroup>=nKeyGroups ) {
  168. unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
  169. switch (XkbOutOfRangeGroupAction(groupInfo)) {
  170. default:
  171. effectiveGroup %= nKeyGroups;
  172. break;
  173. case XkbClampIntoRange:
  174. effectiveGroup = nKeyGroups-1;
  175. break;
  176. case XkbRedirectIntoRange:
  177. effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
  178. if (effectiveGroup>=nKeyGroups)
  179. effectiveGroup= 0;
  180. break;
  181. }
  182. }
  183. return effectiveGroup;
  184. }
  185. void vncPrepareInputDevices(void)
  186. {
  187. /*
  188. * Not ideal since these callbacks do not stack, but it's the only
  189. * decent way we can reliably catch events for both the slave and
  190. * master device.
  191. */
  192. mieqSetHandler(ET_KeyPress, vncXkbProcessDeviceEvent);
  193. mieqSetHandler(ET_KeyRelease, vncXkbProcessDeviceEvent);
  194. }
  195. unsigned vncGetKeyboardState(void)
  196. {
  197. DeviceIntPtr master;
  198. master = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT);
  199. return XkbStateFieldFromRec(&master->key->xkbInfo->state);
  200. }
  201. unsigned vncGetLevelThreeMask(void)
  202. {
  203. unsigned state;
  204. KeyCode keycode;
  205. XkbDescPtr xkb;
  206. XkbAction *act;
  207. /* Group state is still important */
  208. state = vncGetKeyboardState();
  209. state &= ~0xff;
  210. keycode = vncKeysymToKeycode(XK_ISO_Level3_Shift, state, NULL);
  211. if (keycode == 0) {
  212. keycode = vncKeysymToKeycode(XK_Mode_switch, state, NULL);
  213. if (keycode == 0)
  214. return 0;
  215. }
  216. xkb = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT)->key->xkbInfo->desc;
  217. act = XkbKeyActionPtr(xkb, keycode, state);
  218. if (act == NULL)
  219. return 0;
  220. if (act->type != XkbSA_SetMods)
  221. return 0;
  222. if (act->mods.flags & XkbSA_UseModMapMods)
  223. return xkb->map->modmap[keycode];
  224. else
  225. return act->mods.mask;
  226. }
  227. KeyCode vncPressShift(void)
  228. {
  229. unsigned state;
  230. XkbDescPtr xkb;
  231. unsigned int key;
  232. state = vncGetKeyboardState();
  233. if (state & ShiftMask)
  234. return 0;
  235. xkb = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT)->key->xkbInfo->desc;
  236. for (key = xkb->min_key_code; key <= xkb->max_key_code; key++) {
  237. XkbAction *act;
  238. unsigned char mask;
  239. act = XkbKeyActionPtr(xkb, key, state);
  240. if (act == NULL)
  241. continue;
  242. if (act->type != XkbSA_SetMods)
  243. continue;
  244. if (act->mods.flags & XkbSA_UseModMapMods)
  245. mask = xkb->map->modmap[key];
  246. else
  247. mask = act->mods.mask;
  248. if ((mask & ShiftMask) == ShiftMask)
  249. return key;
  250. }
  251. return 0;
  252. }
  253. size_t vncReleaseShift(KeyCode *keys, size_t maxKeys)
  254. {
  255. size_t count;
  256. unsigned state;
  257. DeviceIntPtr master;
  258. XkbDescPtr xkb;
  259. unsigned int key;
  260. state = vncGetKeyboardState();
  261. if (!(state & ShiftMask))
  262. return 0;
  263. count = 0;
  264. master = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT);
  265. xkb = master->key->xkbInfo->desc;
  266. for (key = xkb->min_key_code; key <= xkb->max_key_code; key++) {
  267. XkbAction *act;
  268. unsigned char mask;
  269. if (!key_is_down(master, key, KEY_PROCESSED))
  270. continue;
  271. act = XkbKeyActionPtr(xkb, key, state);
  272. if (act == NULL)
  273. continue;
  274. if (act->type != XkbSA_SetMods)
  275. continue;
  276. if (act->mods.flags & XkbSA_UseModMapMods)
  277. mask = xkb->map->modmap[key];
  278. else
  279. mask = act->mods.mask;
  280. if (!(mask & ShiftMask))
  281. continue;
  282. if (count >= maxKeys)
  283. return 0;
  284. keys[count++] = key;
  285. }
  286. return count;
  287. }
  288. KeyCode vncPressLevelThree(void)
  289. {
  290. unsigned state, mask;
  291. KeyCode keycode;
  292. XkbDescPtr xkb;
  293. XkbAction *act;
  294. mask = vncGetLevelThreeMask();
  295. if (mask == 0)
  296. return 0;
  297. state = vncGetKeyboardState();
  298. if (state & mask)
  299. return 0;
  300. keycode = vncKeysymToKeycode(XK_ISO_Level3_Shift, state, NULL);
  301. if (keycode == 0) {
  302. keycode = vncKeysymToKeycode(XK_Mode_switch, state, NULL);
  303. if (keycode == 0)
  304. return 0;
  305. }
  306. xkb = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT)->key->xkbInfo->desc;
  307. act = XkbKeyActionPtr(xkb, keycode, state);
  308. if (act == NULL)
  309. return 0;
  310. if (act->type != XkbSA_SetMods)
  311. return 0;
  312. return keycode;
  313. }
  314. size_t vncReleaseLevelThree(KeyCode *keys, size_t maxKeys)
  315. {
  316. size_t count;
  317. unsigned state, mask;
  318. DeviceIntPtr master;
  319. XkbDescPtr xkb;
  320. unsigned int key;
  321. mask = vncGetLevelThreeMask();
  322. if (mask == 0)
  323. return 0;
  324. state = vncGetKeyboardState();
  325. if (!(state & mask))
  326. return 0;
  327. count = 0;
  328. master = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT);
  329. xkb = master->key->xkbInfo->desc;
  330. for (key = xkb->min_key_code; key <= xkb->max_key_code; key++) {
  331. XkbAction *act;
  332. unsigned char key_mask;
  333. if (!key_is_down(master, key, KEY_PROCESSED))
  334. continue;
  335. act = XkbKeyActionPtr(xkb, key, state);
  336. if (act == NULL)
  337. continue;
  338. if (act->type != XkbSA_SetMods)
  339. continue;
  340. if (act->mods.flags & XkbSA_UseModMapMods)
  341. key_mask = xkb->map->modmap[key];
  342. else
  343. key_mask = act->mods.mask;
  344. if (!(key_mask & mask))
  345. continue;
  346. if (count >= maxKeys)
  347. return 0;
  348. keys[count++] = key;
  349. }
  350. return count;
  351. }
  352. KeyCode vncKeysymToKeycode(KeySym keysym, unsigned state, unsigned *new_state)
  353. {
  354. XkbDescPtr xkb;
  355. unsigned int key; // KeyCode has insufficient range for the loop
  356. KeyCode fallback;
  357. KeySym ks;
  358. unsigned level_three_mask;
  359. if (new_state != NULL)
  360. *new_state = state;
  361. fallback = 0;
  362. xkb = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT)->key->xkbInfo->desc;
  363. for (key = xkb->min_key_code; key <= xkb->max_key_code; key++) {
  364. unsigned int state_out;
  365. KeySym dummy;
  366. size_t fakeIdx;
  367. XkbTranslateKeyCode(xkb, key, state, &state_out, &ks);
  368. if (ks == NoSymbol)
  369. continue;
  370. /*
  371. * Despite every known piece of documentation on
  372. * XkbTranslateKeyCode() stating that mods_rtrn returns
  373. * the unconsumed modifiers, in reality it always
  374. * returns the _potentially consumed_ modifiers.
  375. */
  376. state_out = state & ~state_out;
  377. if (state_out & LockMask)
  378. XkbConvertCase(ks, &dummy, &ks);
  379. if (ks != keysym)
  380. continue;
  381. /*
  382. * Some keys are never sent by a real keyboard and are
  383. * used in the default layouts as a fallback for
  384. * modifiers. Make sure we use them last as some
  385. * applications can be confused by these normally
  386. * unused keys.
  387. */
  388. for (fakeIdx = 0;
  389. fakeIdx < sizeof(fakeKeys)/sizeof(fakeKeys[0]);
  390. fakeIdx++) {
  391. if (key == fakeKeys[fakeIdx]) {
  392. if (fallback == 0)
  393. fallback = key;
  394. break;
  395. }
  396. }
  397. if (fakeIdx < sizeof(fakeKeys)/sizeof(fakeKeys[0]))
  398. continue;
  399. return key;
  400. }
  401. /* Use the fallback key, if one was found */
  402. if (fallback != 0)
  403. return fallback;
  404. if (new_state == NULL)
  405. return 0;
  406. *new_state = (state & ~ShiftMask) |
  407. ((state & ShiftMask) ? 0 : ShiftMask);
  408. key = vncKeysymToKeycode(keysym, *new_state, NULL);
  409. if (key != 0)
  410. return key;
  411. level_three_mask = vncGetLevelThreeMask();
  412. if (level_three_mask == 0)
  413. return 0;
  414. *new_state = (state & ~level_three_mask) |
  415. ((state & level_three_mask) ? 0 : level_three_mask);
  416. key = vncKeysymToKeycode(keysym, *new_state, NULL);
  417. if (key != 0)
  418. return key;
  419. *new_state = (state & ~(ShiftMask | level_three_mask)) |
  420. ((state & ShiftMask) ? 0 : ShiftMask) |
  421. ((state & level_three_mask) ? 0 : level_three_mask);
  422. key = vncKeysymToKeycode(keysym, *new_state, NULL);
  423. if (key != 0)
  424. return key;
  425. return 0;
  426. }
  427. int vncIsAffectedByNumLock(KeyCode keycode)
  428. {
  429. unsigned state;
  430. KeyCode numlock_keycode;
  431. unsigned numlock_mask;
  432. XkbDescPtr xkb;
  433. XkbAction *act;
  434. unsigned group;
  435. XkbKeyTypeRec *type;
  436. /* Group state is still important */
  437. state = vncGetKeyboardState();
  438. state &= ~0xff;
  439. /*
  440. * Not sure if hunting for a virtual modifier called "NumLock",
  441. * or following the keysym Num_Lock is the best approach. We
  442. * try the latter.
  443. */
  444. numlock_keycode = vncKeysymToKeycode(XK_Num_Lock, state, NULL);
  445. if (numlock_keycode == 0)
  446. return 0;
  447. xkb = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT)->key->xkbInfo->desc;
  448. act = XkbKeyActionPtr(xkb, numlock_keycode, state);
  449. if (act == NULL)
  450. return 0;
  451. if (act->type != XkbSA_LockMods)
  452. return 0;
  453. if (act->mods.flags & XkbSA_UseModMapMods)
  454. numlock_mask = xkb->map->modmap[keycode];
  455. else
  456. numlock_mask = act->mods.mask;
  457. group = XkbKeyEffectiveGroup(xkb, keycode, state);
  458. type = XkbKeyKeyType(xkb, keycode, group);
  459. if ((type->mods.mask & numlock_mask) == 0)
  460. return 0;
  461. return 1;
  462. }
  463. KeyCode vncAddKeysym(KeySym keysym, unsigned state)
  464. {
  465. DeviceIntPtr master;
  466. XkbDescPtr xkb;
  467. unsigned int key;
  468. XkbEventCauseRec cause;
  469. XkbChangesRec changes;
  470. int types[1];
  471. KeySym *syms;
  472. KeySym upper, lower;
  473. master = GetMaster(vncKeyboardDev, KEYBOARD_OR_FLOAT);
  474. xkb = master->key->xkbInfo->desc;
  475. for (key = xkb->max_key_code; key >= xkb->min_key_code; key--) {
  476. if (XkbKeyNumGroups(xkb, key) == 0)
  477. break;
  478. }
  479. if (key < xkb->min_key_code)
  480. return 0;
  481. memset(&changes, 0, sizeof(changes));
  482. memset(&cause, 0, sizeof(cause));
  483. XkbSetCauseUnknown(&cause);
  484. /*
  485. * Tools like xkbcomp get confused if there isn't a name
  486. * assigned to the keycode we're trying to use.
  487. */
  488. if (xkb->names && xkb->names->keys &&
  489. (xkb->names->keys[key].name[0] == '\0')) {
  490. xkb->names->keys[key].name[0] = 'I';
  491. xkb->names->keys[key].name[1] = '0' + (key / 100) % 10;
  492. xkb->names->keys[key].name[2] = '0' + (key / 10) % 10;
  493. xkb->names->keys[key].name[3] = '0' + (key / 1) % 10;
  494. changes.names.changed |= XkbKeyNamesMask;
  495. changes.names.first_key = key;
  496. changes.names.num_keys = 1;
  497. }
  498. /* FIXME: Verify that ONE_LEVEL/ALPHABETIC isn't screwed up */
  499. /*
  500. * For keysyms that are affected by Lock, we are better off
  501. * using ALPHABETIC rather than ONE_LEVEL as the latter
  502. * generally cannot produce lower case when Lock is active.
  503. */
  504. XkbConvertCase(keysym, &lower, &upper);
  505. if (upper == lower)
  506. types[XkbGroup1Index] = XkbOneLevelIndex;
  507. else
  508. types[XkbGroup1Index] = XkbAlphabeticIndex;
  509. XkbChangeTypesOfKey(xkb, key, 1, XkbGroup1Mask, types, &changes.map);
  510. syms = XkbKeySymsPtr(xkb,key);
  511. if (upper == lower)
  512. syms[0] = keysym;
  513. else {
  514. syms[0] = lower;
  515. syms[1] = upper;
  516. }
  517. changes.map.changed |= XkbKeySymsMask;
  518. changes.map.first_key_sym = key;
  519. changes.map.num_key_syms = 1;
  520. XkbSendNotification(master, &changes, &cause);
  521. return key;
  522. }
  523. static void vncXkbProcessDeviceEvent(int screenNum,
  524. InternalEvent *event,
  525. DeviceIntPtr dev)
  526. {
  527. unsigned int backupctrls;
  528. XkbControlsPtr ctrls;
  529. if (event->device_event.sourceid != vncKeyboardDev->id) {
  530. dev->public.processInputProc(event, dev);
  531. return;
  532. }
  533. /*
  534. * We need to bypass AccessX since it is timing sensitive and
  535. * the network can cause fake event delays.
  536. */
  537. ctrls = dev->key->xkbInfo->desc->ctrls;
  538. backupctrls = ctrls->enabled_ctrls;
  539. ctrls->enabled_ctrls &= ~XkbAllFilteredEventsMask;
  540. /*
  541. * This flag needs to be set for key repeats to be properly
  542. * respected.
  543. */
  544. if ((event->device_event.type == ET_KeyPress) &&
  545. key_is_down(dev, event->device_event.detail.key, KEY_PROCESSED))
  546. event->device_event.key_repeat = TRUE;
  547. dev->public.processInputProc(event, dev);
  548. ctrls->enabled_ctrls = backupctrls;
  549. }