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

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