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

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