xkbActions.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /************************************************************
  2. Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
  3. Permission to use, copy, modify, and distribute this
  4. software and its documentation for any purpose and without
  5. fee is hereby granted, provided that the above copyright
  6. notice appear in all copies and that both that copyright
  7. notice and this permission notice appear in supporting
  8. documentation, and that the name of Silicon Graphics not be
  9. used in advertising or publicity pertaining to distribution
  10. of the software without specific prior written permission.
  11. Silicon Graphics makes no representation about the suitability
  12. of this software for any purpose. It is provided "as is"
  13. without any express or implied warranty.
  14. SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  16. AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  17. GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  18. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  20. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
  21. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. ********************************************************/
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include <X11/X.h>
  29. #include <X11/Xproto.h>
  30. #include <X11/keysym.h>
  31. #include "misc.h"
  32. #include "inputstr.h"
  33. #include "exevents.h"
  34. #include "eventstr.h"
  35. #include <xkbsrv.h>
  36. #include "xkb.h"
  37. #include <ctype.h>
  38. #include "mi.h"
  39. #include "mipointer.h"
  40. #include "inpututils.h"
  41. #define EXTENSION_EVENT_BASE 64
  42. DevPrivateKeyRec xkbDevicePrivateKeyRec;
  43. static void XkbFakePointerMotion(DeviceIntPtr dev, unsigned flags, int x,
  44. int y);
  45. void
  46. xkbUnwrapProc(DeviceIntPtr device, DeviceHandleProc proc, void *data)
  47. {
  48. xkbDeviceInfoPtr xkbPrivPtr = XKBDEVICEINFO(device);
  49. ProcessInputProc backupproc;
  50. if (xkbPrivPtr->unwrapProc)
  51. xkbPrivPtr->unwrapProc = NULL;
  52. UNWRAP_PROCESS_INPUT_PROC(device, xkbPrivPtr, backupproc);
  53. proc(device, data);
  54. COND_WRAP_PROCESS_INPUT_PROC(device, xkbPrivPtr, backupproc, xkbUnwrapProc);
  55. }
  56. Bool
  57. XkbInitPrivates(void)
  58. {
  59. return dixRegisterPrivateKey(&xkbDevicePrivateKeyRec, PRIVATE_DEVICE,
  60. sizeof(xkbDeviceInfoRec));
  61. }
  62. void
  63. XkbSetExtension(DeviceIntPtr device, ProcessInputProc proc)
  64. {
  65. xkbDeviceInfoPtr xkbPrivPtr = XKBDEVICEINFO(device);
  66. WRAP_PROCESS_INPUT_PROC(device, xkbPrivPtr, proc, xkbUnwrapProc);
  67. }
  68. /***====================================================================***/
  69. static XkbAction
  70. _FixUpAction(XkbDescPtr xkb, XkbAction *act)
  71. {
  72. static XkbAction fake;
  73. if (XkbIsPtrAction(act) &&
  74. (!(xkb->ctrls->enabled_ctrls & XkbMouseKeysMask))) {
  75. fake.type = XkbSA_NoAction;
  76. return fake;
  77. }
  78. if (xkb->ctrls->enabled_ctrls & XkbStickyKeysMask) {
  79. if (act->any.type == XkbSA_SetMods) {
  80. fake.mods.type = XkbSA_LatchMods;
  81. fake.mods.mask = act->mods.mask;
  82. if (XkbAX_NeedOption(xkb->ctrls, XkbAX_LatchToLockMask))
  83. fake.mods.flags = XkbSA_ClearLocks | XkbSA_LatchToLock;
  84. else
  85. fake.mods.flags = XkbSA_ClearLocks;
  86. return fake;
  87. }
  88. if (act->any.type == XkbSA_SetGroup) {
  89. fake.group.type = XkbSA_LatchGroup;
  90. if (XkbAX_NeedOption(xkb->ctrls, XkbAX_LatchToLockMask))
  91. fake.group.flags = XkbSA_ClearLocks | XkbSA_LatchToLock;
  92. else
  93. fake.group.flags = XkbSA_ClearLocks;
  94. XkbSASetGroup(&fake.group, XkbSAGroup(&act->group));
  95. return fake;
  96. }
  97. }
  98. return *act;
  99. }
  100. static XkbAction
  101. XkbGetKeyAction(XkbSrvInfoPtr xkbi, XkbStatePtr xkbState, CARD8 key)
  102. {
  103. int effectiveGroup;
  104. int col;
  105. XkbDescPtr xkb;
  106. XkbKeyTypePtr type;
  107. XkbAction *pActs;
  108. static XkbAction fake;
  109. xkb = xkbi->desc;
  110. if (!XkbKeyHasActions(xkb, key) || !XkbKeycodeInRange(xkb, key)) {
  111. fake.type = XkbSA_NoAction;
  112. return fake;
  113. }
  114. pActs = XkbKeyActionsPtr(xkb, key);
  115. col = 0;
  116. effectiveGroup = XkbGetEffectiveGroup(xkbi, xkbState, key);
  117. if (effectiveGroup != XkbGroup1Index)
  118. col += (effectiveGroup * XkbKeyGroupsWidth(xkb, key));
  119. type = XkbKeyKeyType(xkb, key, effectiveGroup);
  120. if (type->map != NULL) {
  121. register unsigned i, mods;
  122. register XkbKTMapEntryPtr entry;
  123. mods = xkbState->mods & type->mods.mask;
  124. for (entry = type->map, i = 0; i < type->map_count; i++, entry++) {
  125. if ((entry->active) && (entry->mods.mask == mods)) {
  126. col += entry->level;
  127. break;
  128. }
  129. }
  130. }
  131. if (pActs[col].any.type == XkbSA_NoAction)
  132. return pActs[col];
  133. fake = _FixUpAction(xkb, &pActs[col]);
  134. return fake;
  135. }
  136. static XkbAction
  137. XkbGetButtonAction(DeviceIntPtr kbd, DeviceIntPtr dev, int button)
  138. {
  139. XkbAction fake;
  140. if ((dev->button) && (dev->button->xkb_acts)) {
  141. if (dev->button->xkb_acts[button - 1].any.type != XkbSA_NoAction) {
  142. fake = _FixUpAction(kbd->key->xkbInfo->desc,
  143. &dev->button->xkb_acts[button - 1]);
  144. return fake;
  145. }
  146. }
  147. fake.any.type = XkbSA_NoAction;
  148. return fake;
  149. }
  150. /***====================================================================***/
  151. #define SYNTHETIC_KEYCODE 1
  152. #define BTN_ACT_FLAG 0x100
  153. static int
  154. _XkbFilterSetState(XkbSrvInfoPtr xkbi,
  155. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  156. {
  157. if (filter->keycode == 0) { /* initial press */
  158. filter->keycode = keycode;
  159. filter->active = 1;
  160. filter->filterOthers = ((pAction->mods.mask & XkbSA_ClearLocks) != 0);
  161. filter->priv = 0;
  162. filter->filter = _XkbFilterSetState;
  163. if (pAction->type == XkbSA_SetMods) {
  164. filter->upAction = *pAction;
  165. xkbi->setMods = pAction->mods.mask;
  166. }
  167. else {
  168. xkbi->groupChange = XkbSAGroup(&pAction->group);
  169. if (pAction->group.flags & XkbSA_GroupAbsolute)
  170. xkbi->groupChange -= xkbi->state.base_group;
  171. filter->upAction = *pAction;
  172. XkbSASetGroup(&filter->upAction.group, xkbi->groupChange);
  173. }
  174. }
  175. else if (filter->keycode == keycode) {
  176. if (filter->upAction.type == XkbSA_SetMods) {
  177. xkbi->clearMods = filter->upAction.mods.mask;
  178. if (filter->upAction.mods.flags & XkbSA_ClearLocks) {
  179. xkbi->state.locked_mods &= ~filter->upAction.mods.mask;
  180. }
  181. }
  182. else {
  183. if (filter->upAction.group.flags & XkbSA_ClearLocks) {
  184. xkbi->state.locked_group = 0;
  185. }
  186. xkbi->groupChange = -XkbSAGroup(&filter->upAction.group);
  187. }
  188. filter->active = 0;
  189. }
  190. else {
  191. filter->upAction.mods.flags &= ~XkbSA_ClearLocks;
  192. filter->filterOthers = 0;
  193. }
  194. return 1;
  195. }
  196. #define LATCH_KEY_DOWN 1
  197. #define LATCH_PENDING 2
  198. static int
  199. _XkbFilterLatchState(XkbSrvInfoPtr xkbi,
  200. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  201. {
  202. if (filter->keycode == 0) { /* initial press */
  203. AccessXCancelRepeatKey(xkbi,keycode);
  204. filter->keycode = keycode;
  205. filter->active = 1;
  206. filter->filterOthers = 1;
  207. filter->priv = LATCH_KEY_DOWN;
  208. filter->filter = _XkbFilterLatchState;
  209. if (pAction->type == XkbSA_LatchMods) {
  210. filter->upAction = *pAction;
  211. xkbi->setMods = pAction->mods.mask;
  212. }
  213. else {
  214. xkbi->groupChange = XkbSAGroup(&pAction->group);
  215. if (pAction->group.flags & XkbSA_GroupAbsolute)
  216. xkbi->groupChange -= xkbi->state.base_group;
  217. filter->upAction = *pAction;
  218. XkbSASetGroup(&filter->upAction.group, xkbi->groupChange);
  219. }
  220. }
  221. else if (pAction && (filter->priv == LATCH_PENDING)) {
  222. if (((1 << pAction->type) & XkbSA_BreakLatch) != 0) {
  223. filter->active = 0;
  224. /* If one latch is broken, all latches are broken, so it's no use
  225. to find out which particular latch this filter tracks. */
  226. xkbi->state.latched_mods = 0;
  227. xkbi->state.latched_group = 0;
  228. }
  229. }
  230. else if (filter->keycode == keycode && filter->priv != LATCH_PENDING){
  231. /* The test above for LATCH_PENDING skips subsequent releases of the
  232. key after it has been released first time and the latch became
  233. pending. */
  234. XkbControlsPtr ctrls = xkbi->desc->ctrls;
  235. int needBeep = ((ctrls->enabled_ctrls & XkbStickyKeysMask) &&
  236. XkbAX_NeedFeedback(ctrls, XkbAX_StickyKeysFBMask));
  237. if (filter->upAction.type == XkbSA_LatchMods) {
  238. unsigned char mask = filter->upAction.mods.mask;
  239. unsigned char common;
  240. xkbi->clearMods = mask;
  241. /* ClearLocks */
  242. common = mask & xkbi->state.locked_mods;
  243. if ((filter->upAction.mods.flags & XkbSA_ClearLocks) && common) {
  244. mask &= ~common;
  245. xkbi->state.locked_mods &= ~common;
  246. if (needBeep)
  247. XkbDDXAccessXBeep(xkbi->device, _BEEP_STICKY_UNLOCK,
  248. XkbStickyKeysMask);
  249. }
  250. /* LatchToLock */
  251. common = mask & xkbi->state.latched_mods;
  252. if ((filter->upAction.mods.flags & XkbSA_LatchToLock) && common) {
  253. unsigned char newlocked;
  254. mask &= ~common;
  255. newlocked = common & ~xkbi->state.locked_mods;
  256. if(newlocked){
  257. xkbi->state.locked_mods |= newlocked;
  258. if (needBeep)
  259. XkbDDXAccessXBeep(xkbi->device, _BEEP_STICKY_LOCK,
  260. XkbStickyKeysMask);
  261. }
  262. xkbi->state.latched_mods &= ~common;
  263. }
  264. /* Latch remaining modifiers, if any. */
  265. if (mask) {
  266. xkbi->state.latched_mods |= mask;
  267. filter->priv = LATCH_PENDING;
  268. if (needBeep)
  269. XkbDDXAccessXBeep(xkbi->device, _BEEP_STICKY_LATCH,
  270. XkbStickyKeysMask);
  271. }
  272. }
  273. else {
  274. xkbi->groupChange = -XkbSAGroup(&filter->upAction.group);
  275. /* ClearLocks */
  276. if ((filter->upAction.group.flags & XkbSA_ClearLocks) &&
  277. (xkbi->state.locked_group)) {
  278. xkbi->state.locked_group = 0;
  279. if (needBeep)
  280. XkbDDXAccessXBeep(xkbi->device, _BEEP_STICKY_UNLOCK,
  281. XkbStickyKeysMask);
  282. }
  283. /* LatchToLock */
  284. else if ((filter->upAction.group.flags & XkbSA_LatchToLock)
  285. && (xkbi->state.latched_group)) {
  286. xkbi->state.locked_group += XkbSAGroup(&filter->upAction.group);
  287. xkbi->state.latched_group -= XkbSAGroup(&filter->upAction.group);
  288. if(XkbSAGroup(&filter->upAction.group) && needBeep)
  289. XkbDDXAccessXBeep(xkbi->device, _BEEP_STICKY_LOCK,
  290. XkbStickyKeysMask);
  291. }
  292. /* Latch group */
  293. else if(XkbSAGroup(&filter->upAction.group)){
  294. xkbi->state.latched_group += XkbSAGroup(&filter->upAction.group);
  295. filter->priv = LATCH_PENDING;
  296. if (needBeep)
  297. XkbDDXAccessXBeep(xkbi->device, _BEEP_STICKY_LATCH,
  298. XkbStickyKeysMask);
  299. }
  300. }
  301. if (filter->priv != LATCH_PENDING)
  302. filter->active = 0;
  303. }
  304. else if (pAction && (filter->priv == LATCH_KEY_DOWN)) {
  305. /* Latch was broken before it became pending: degrade to a
  306. SetMods/SetGroup. */
  307. if (filter->upAction.type == XkbSA_LatchMods)
  308. filter->upAction.type = XkbSA_SetMods;
  309. else
  310. filter->upAction.type = XkbSA_SetGroup;
  311. filter->filter = _XkbFilterSetState;
  312. filter->priv = 0;
  313. return filter->filter(xkbi, filter, keycode, pAction);
  314. }
  315. return 1;
  316. }
  317. static int
  318. _XkbFilterLockState(XkbSrvInfoPtr xkbi,
  319. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  320. {
  321. if (pAction && (pAction->type == XkbSA_LockGroup)) {
  322. if (pAction->group.flags & XkbSA_GroupAbsolute)
  323. xkbi->state.locked_group = XkbSAGroup(&pAction->group);
  324. else
  325. xkbi->state.locked_group += XkbSAGroup(&pAction->group);
  326. return 1;
  327. }
  328. if (filter->keycode == 0) { /* initial press */
  329. filter->keycode = keycode;
  330. filter->active = 1;
  331. filter->filterOthers = 0;
  332. filter->priv = xkbi->state.locked_mods & pAction->mods.mask;
  333. filter->filter = _XkbFilterLockState;
  334. filter->upAction = *pAction;
  335. if (!(filter->upAction.mods.flags & XkbSA_LockNoLock))
  336. xkbi->state.locked_mods |= pAction->mods.mask;
  337. xkbi->setMods = pAction->mods.mask;
  338. }
  339. else if (filter->keycode == keycode) {
  340. filter->active = 0;
  341. xkbi->clearMods = filter->upAction.mods.mask;
  342. if (!(filter->upAction.mods.flags & XkbSA_LockNoUnlock))
  343. xkbi->state.locked_mods &= ~filter->priv;
  344. }
  345. return 1;
  346. }
  347. #define ISO_KEY_DOWN 0
  348. #define NO_ISO_LOCK 1
  349. static int
  350. _XkbFilterISOLock(XkbSrvInfoPtr xkbi,
  351. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  352. {
  353. if (filter->keycode == 0) { /* initial press */
  354. CARD8 flags = pAction->iso.flags;
  355. filter->keycode = keycode;
  356. filter->active = 1;
  357. filter->filterOthers = 1;
  358. filter->priv = ISO_KEY_DOWN;
  359. filter->upAction = *pAction;
  360. filter->filter = _XkbFilterISOLock;
  361. if (flags & XkbSA_ISODfltIsGroup) {
  362. xkbi->groupChange = XkbSAGroup(&pAction->iso);
  363. xkbi->setMods = 0;
  364. }
  365. else {
  366. xkbi->setMods = pAction->iso.mask;
  367. xkbi->groupChange = 0;
  368. }
  369. if ((!(flags & XkbSA_ISONoAffectMods)) && (xkbi->state.base_mods)) {
  370. filter->priv = NO_ISO_LOCK;
  371. xkbi->state.locked_mods ^= xkbi->state.base_mods;
  372. }
  373. if ((!(flags & XkbSA_ISONoAffectGroup)) && (xkbi->state.base_group)) {
  374. /* 6/22/93 (ef) -- lock groups if group key is down first */
  375. }
  376. if (!(flags & XkbSA_ISONoAffectPtr)) {
  377. /* 6/22/93 (ef) -- lock mouse buttons if they're down */
  378. }
  379. }
  380. else if (filter->keycode == keycode) {
  381. CARD8 flags = filter->upAction.iso.flags;
  382. if (flags & XkbSA_ISODfltIsGroup) {
  383. xkbi->groupChange = -XkbSAGroup(&filter->upAction.iso);
  384. xkbi->clearMods = 0;
  385. if (filter->priv == ISO_KEY_DOWN)
  386. xkbi->state.locked_group += XkbSAGroup(&filter->upAction.iso);
  387. }
  388. else {
  389. xkbi->clearMods = filter->upAction.iso.mask;
  390. xkbi->groupChange = 0;
  391. if (filter->priv == ISO_KEY_DOWN)
  392. xkbi->state.locked_mods ^= filter->upAction.iso.mask;
  393. }
  394. filter->active = 0;
  395. }
  396. else if (pAction) {
  397. CARD8 flags = filter->upAction.iso.flags;
  398. switch (pAction->type) {
  399. case XkbSA_SetMods:
  400. case XkbSA_LatchMods:
  401. if (!(flags & XkbSA_ISONoAffectMods)) {
  402. pAction->type = XkbSA_LockMods;
  403. filter->priv = NO_ISO_LOCK;
  404. }
  405. break;
  406. case XkbSA_SetGroup:
  407. case XkbSA_LatchGroup:
  408. if (!(flags & XkbSA_ISONoAffectGroup)) {
  409. pAction->type = XkbSA_LockGroup;
  410. filter->priv = NO_ISO_LOCK;
  411. }
  412. break;
  413. case XkbSA_PtrBtn:
  414. if (!(flags & XkbSA_ISONoAffectPtr)) {
  415. pAction->type = XkbSA_LockPtrBtn;
  416. filter->priv = NO_ISO_LOCK;
  417. }
  418. break;
  419. case XkbSA_SetControls:
  420. if (!(flags & XkbSA_ISONoAffectCtrls)) {
  421. pAction->type = XkbSA_LockControls;
  422. filter->priv = NO_ISO_LOCK;
  423. }
  424. break;
  425. }
  426. }
  427. return 1;
  428. }
  429. static CARD32
  430. _XkbPtrAccelExpire(OsTimerPtr timer, CARD32 now, void *arg)
  431. {
  432. XkbSrvInfoPtr xkbi = (XkbSrvInfoPtr) arg;
  433. XkbControlsPtr ctrls = xkbi->desc->ctrls;
  434. int dx, dy;
  435. if (xkbi->mouseKey == 0)
  436. return 0;
  437. if (xkbi->mouseKeysAccel) {
  438. if ((xkbi->mouseKeysCounter) < ctrls->mk_time_to_max) {
  439. double step;
  440. xkbi->mouseKeysCounter++;
  441. step = xkbi->mouseKeysCurveFactor *
  442. pow((double) xkbi->mouseKeysCounter, xkbi->mouseKeysCurve);
  443. if (xkbi->mouseKeysDX < 0)
  444. dx = floor(((double) xkbi->mouseKeysDX) * step);
  445. else
  446. dx = ceil(((double) xkbi->mouseKeysDX) * step);
  447. if (xkbi->mouseKeysDY < 0)
  448. dy = floor(((double) xkbi->mouseKeysDY) * step);
  449. else
  450. dy = ceil(((double) xkbi->mouseKeysDY) * step);
  451. }
  452. else {
  453. dx = xkbi->mouseKeysDX * ctrls->mk_max_speed;
  454. dy = xkbi->mouseKeysDY * ctrls->mk_max_speed;
  455. }
  456. if (xkbi->mouseKeysFlags & XkbSA_MoveAbsoluteX)
  457. dx = xkbi->mouseKeysDX;
  458. if (xkbi->mouseKeysFlags & XkbSA_MoveAbsoluteY)
  459. dy = xkbi->mouseKeysDY;
  460. }
  461. else {
  462. dx = xkbi->mouseKeysDX;
  463. dy = xkbi->mouseKeysDY;
  464. }
  465. XkbFakePointerMotion(xkbi->device, xkbi->mouseKeysFlags, dx, dy);
  466. return xkbi->desc->ctrls->mk_interval;
  467. }
  468. static int
  469. _XkbFilterPointerMove(XkbSrvInfoPtr xkbi,
  470. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  471. {
  472. int x, y;
  473. Bool accel;
  474. if (filter->keycode == 0) { /* initial press */
  475. filter->keycode = keycode;
  476. filter->active = 1;
  477. filter->filterOthers = 0;
  478. filter->priv = 0;
  479. filter->filter = _XkbFilterPointerMove;
  480. filter->upAction = *pAction;
  481. xkbi->mouseKeysCounter = 0;
  482. xkbi->mouseKey = keycode;
  483. accel = ((pAction->ptr.flags & XkbSA_NoAcceleration) == 0);
  484. x = XkbPtrActionX(&pAction->ptr);
  485. y = XkbPtrActionY(&pAction->ptr);
  486. XkbFakePointerMotion(xkbi->device, pAction->ptr.flags, x, y);
  487. AccessXCancelRepeatKey(xkbi, keycode);
  488. xkbi->mouseKeysAccel = accel &&
  489. (xkbi->desc->ctrls->enabled_ctrls & XkbMouseKeysAccelMask);
  490. xkbi->mouseKeysFlags = pAction->ptr.flags;
  491. xkbi->mouseKeysDX = XkbPtrActionX(&pAction->ptr);
  492. xkbi->mouseKeysDY = XkbPtrActionY(&pAction->ptr);
  493. xkbi->mouseKeyTimer = TimerSet(xkbi->mouseKeyTimer, 0,
  494. xkbi->desc->ctrls->mk_delay,
  495. _XkbPtrAccelExpire, (void *) xkbi);
  496. }
  497. else if (filter->keycode == keycode) {
  498. filter->active = 0;
  499. if (xkbi->mouseKey == keycode) {
  500. xkbi->mouseKey = 0;
  501. xkbi->mouseKeyTimer = TimerSet(xkbi->mouseKeyTimer, 0, 0,
  502. NULL, NULL);
  503. }
  504. }
  505. return 0;
  506. }
  507. static int
  508. _XkbFilterPointerBtn(XkbSrvInfoPtr xkbi,
  509. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  510. {
  511. if (filter->keycode == 0) { /* initial press */
  512. int button = pAction->btn.button;
  513. if (button == XkbSA_UseDfltButton)
  514. button = xkbi->desc->ctrls->mk_dflt_btn;
  515. filter->keycode = keycode;
  516. filter->active = 1;
  517. filter->filterOthers = 0;
  518. filter->priv = 0;
  519. filter->filter = _XkbFilterPointerBtn;
  520. filter->upAction = *pAction;
  521. filter->upAction.btn.button = button;
  522. switch (pAction->type) {
  523. case XkbSA_LockPtrBtn:
  524. if (((xkbi->lockedPtrButtons & (1 << button)) == 0) &&
  525. ((pAction->btn.flags & XkbSA_LockNoLock) == 0)) {
  526. xkbi->lockedPtrButtons |= (1 << button);
  527. AccessXCancelRepeatKey(xkbi, keycode);
  528. XkbFakeDeviceButton(xkbi->device, 1, button);
  529. filter->upAction.type = XkbSA_NoAction;
  530. }
  531. break;
  532. case XkbSA_PtrBtn:
  533. {
  534. register int i, nClicks;
  535. AccessXCancelRepeatKey(xkbi, keycode);
  536. if (pAction->btn.count > 0) {
  537. nClicks = pAction->btn.count;
  538. for (i = 0; i < nClicks; i++) {
  539. XkbFakeDeviceButton(xkbi->device, 1, button);
  540. XkbFakeDeviceButton(xkbi->device, 0, button);
  541. }
  542. filter->upAction.type = XkbSA_NoAction;
  543. }
  544. else
  545. XkbFakeDeviceButton(xkbi->device, 1, button);
  546. }
  547. break;
  548. case XkbSA_SetPtrDflt:
  549. {
  550. XkbControlsPtr ctrls = xkbi->desc->ctrls;
  551. XkbControlsRec old;
  552. xkbControlsNotify cn;
  553. old = *ctrls;
  554. AccessXCancelRepeatKey(xkbi, keycode);
  555. switch (pAction->dflt.affect) {
  556. case XkbSA_AffectDfltBtn:
  557. if (pAction->dflt.flags & XkbSA_DfltBtnAbsolute)
  558. ctrls->mk_dflt_btn = XkbSAPtrDfltValue(&pAction->dflt);
  559. else {
  560. ctrls->mk_dflt_btn += XkbSAPtrDfltValue(&pAction->dflt);
  561. if (ctrls->mk_dflt_btn > 5)
  562. ctrls->mk_dflt_btn = 5;
  563. else if (ctrls->mk_dflt_btn < 1)
  564. ctrls->mk_dflt_btn = 1;
  565. }
  566. break;
  567. default:
  568. ErrorF
  569. ("Attempt to change unknown pointer default (%d) ignored\n",
  570. pAction->dflt.affect);
  571. break;
  572. }
  573. if (XkbComputeControlsNotify(xkbi->device,
  574. &old, xkbi->desc->ctrls, &cn, FALSE)) {
  575. cn.keycode = keycode;
  576. /* XXX: what about DeviceKeyPress? */
  577. cn.eventType = KeyPress;
  578. cn.requestMajor = 0;
  579. cn.requestMinor = 0;
  580. XkbSendControlsNotify(xkbi->device, &cn);
  581. }
  582. }
  583. break;
  584. }
  585. }
  586. else if (filter->keycode == keycode) {
  587. int button = filter->upAction.btn.button;
  588. switch (filter->upAction.type) {
  589. case XkbSA_LockPtrBtn:
  590. if (((filter->upAction.btn.flags & XkbSA_LockNoUnlock) != 0) ||
  591. ((xkbi->lockedPtrButtons & (1 << button)) == 0)) {
  592. break;
  593. }
  594. xkbi->lockedPtrButtons &= ~(1 << button);
  595. if (IsMaster(xkbi->device)) {
  596. XkbMergeLockedPtrBtns(xkbi->device);
  597. /* One SD still has lock set, don't post event */
  598. if ((xkbi->lockedPtrButtons & (1 << button)) != 0)
  599. break;
  600. }
  601. /* fallthrough */
  602. case XkbSA_PtrBtn:
  603. XkbFakeDeviceButton(xkbi->device, 0, button);
  604. break;
  605. }
  606. filter->active = 0;
  607. }
  608. return 0;
  609. }
  610. static int
  611. _XkbFilterControls(XkbSrvInfoPtr xkbi,
  612. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  613. {
  614. XkbControlsRec old;
  615. XkbControlsPtr ctrls;
  616. DeviceIntPtr kbd;
  617. unsigned int change;
  618. XkbEventCauseRec cause;
  619. kbd = xkbi->device;
  620. ctrls = xkbi->desc->ctrls;
  621. old = *ctrls;
  622. if (filter->keycode == 0) { /* initial press */
  623. filter->keycode = keycode;
  624. filter->active = 1;
  625. filter->filterOthers = 0;
  626. change = XkbActionCtrls(&pAction->ctrls);
  627. filter->priv = change;
  628. filter->filter = _XkbFilterControls;
  629. filter->upAction = *pAction;
  630. if (pAction->type == XkbSA_LockControls) {
  631. filter->priv = (ctrls->enabled_ctrls & change);
  632. change &= ~ctrls->enabled_ctrls;
  633. }
  634. if (change) {
  635. xkbControlsNotify cn;
  636. XkbSrvLedInfoPtr sli;
  637. ctrls->enabled_ctrls |= change;
  638. if (XkbComputeControlsNotify(kbd, &old, ctrls, &cn, FALSE)) {
  639. cn.keycode = keycode;
  640. /* XXX: what about DeviceKeyPress? */
  641. cn.eventType = KeyPress;
  642. cn.requestMajor = 0;
  643. cn.requestMinor = 0;
  644. XkbSendControlsNotify(kbd, &cn);
  645. }
  646. XkbSetCauseKey(&cause, keycode, KeyPress);
  647. /* If sticky keys were disabled, clear all locks and latches */
  648. if ((old.enabled_ctrls & XkbStickyKeysMask) &&
  649. (!(ctrls->enabled_ctrls & XkbStickyKeysMask))) {
  650. XkbClearAllLatchesAndLocks(kbd, xkbi, FALSE, &cause);
  651. }
  652. sli = XkbFindSrvLedInfo(kbd, XkbDfltXIClass, XkbDfltXIId, 0);
  653. XkbUpdateIndicators(kbd, sli->usesControls, TRUE, NULL, &cause);
  654. if (XkbAX_NeedFeedback(ctrls, XkbAX_FeatureFBMask))
  655. XkbDDXAccessXBeep(kbd, _BEEP_FEATURE_ON, change);
  656. }
  657. }
  658. else if (filter->keycode == keycode) {
  659. change = filter->priv;
  660. if (change) {
  661. xkbControlsNotify cn;
  662. XkbSrvLedInfoPtr sli;
  663. ctrls->enabled_ctrls &= ~change;
  664. if (XkbComputeControlsNotify(kbd, &old, ctrls, &cn, FALSE)) {
  665. cn.keycode = keycode;
  666. cn.eventType = KeyRelease;
  667. cn.requestMajor = 0;
  668. cn.requestMinor = 0;
  669. XkbSendControlsNotify(kbd, &cn);
  670. }
  671. XkbSetCauseKey(&cause, keycode, KeyRelease);
  672. /* If sticky keys were disabled, clear all locks and latches */
  673. if ((old.enabled_ctrls & XkbStickyKeysMask) &&
  674. (!(ctrls->enabled_ctrls & XkbStickyKeysMask))) {
  675. XkbClearAllLatchesAndLocks(kbd, xkbi, FALSE, &cause);
  676. }
  677. sli = XkbFindSrvLedInfo(kbd, XkbDfltXIClass, XkbDfltXIId, 0);
  678. XkbUpdateIndicators(kbd, sli->usesControls, TRUE, NULL, &cause);
  679. if (XkbAX_NeedFeedback(ctrls, XkbAX_FeatureFBMask))
  680. XkbDDXAccessXBeep(kbd, _BEEP_FEATURE_OFF, change);
  681. }
  682. filter->keycode = 0;
  683. filter->active = 0;
  684. }
  685. return 1;
  686. }
  687. static int
  688. _XkbFilterActionMessage(XkbSrvInfoPtr xkbi,
  689. XkbFilterPtr filter,
  690. unsigned keycode, XkbAction *pAction)
  691. {
  692. XkbMessageAction *pMsg;
  693. DeviceIntPtr kbd;
  694. if ((filter->keycode != 0) && (filter->keycode != keycode))
  695. return 1;
  696. /* This can happen if the key repeats, and the state (modifiers or group)
  697. changes meanwhile. */
  698. if ((filter->keycode == keycode) && pAction &&
  699. (pAction->type != XkbSA_ActionMessage))
  700. return 1;
  701. kbd = xkbi->device;
  702. if (filter->keycode == 0) { /* initial press */
  703. pMsg = &pAction->msg;
  704. if ((pMsg->flags & XkbSA_MessageOnRelease) ||
  705. ((pMsg->flags & XkbSA_MessageGenKeyEvent) == 0)) {
  706. filter->keycode = keycode;
  707. filter->active = 1;
  708. filter->filterOthers = 0;
  709. filter->priv = 0;
  710. filter->filter = _XkbFilterActionMessage;
  711. filter->upAction = *pAction;
  712. }
  713. if (pMsg->flags & XkbSA_MessageOnPress) {
  714. xkbActionMessage msg;
  715. msg.keycode = keycode;
  716. msg.press = 1;
  717. msg.keyEventFollows =
  718. ((pMsg->flags & XkbSA_MessageGenKeyEvent) != 0);
  719. memcpy((char *) msg.message, (char *) pMsg->message,
  720. XkbActionMessageLength);
  721. XkbSendActionMessage(kbd, &msg);
  722. }
  723. return ((pAction->msg.flags & XkbSA_MessageGenKeyEvent) != 0);
  724. }
  725. else if (filter->keycode == keycode) {
  726. pMsg = &filter->upAction.msg;
  727. if (pAction == NULL) {
  728. if (pMsg->flags & XkbSA_MessageOnRelease) {
  729. xkbActionMessage msg;
  730. msg.keycode = keycode;
  731. msg.press = 0;
  732. msg.keyEventFollows =
  733. ((pMsg->flags & XkbSA_MessageGenKeyEvent) != 0);
  734. memcpy((char *) msg.message, (char *) pMsg->message,
  735. XkbActionMessageLength);
  736. XkbSendActionMessage(kbd, &msg);
  737. }
  738. filter->keycode = 0;
  739. filter->active = 0;
  740. return ((pMsg->flags & XkbSA_MessageGenKeyEvent) != 0);
  741. } else if (memcmp(pMsg, pAction, 8) == 0) {
  742. /* Repeat: If we send the same message, avoid multiple messages
  743. on release from piling up. */
  744. filter->keycode = 0;
  745. filter->active = 0;
  746. }
  747. }
  748. return 1;
  749. }
  750. static int
  751. _XkbFilterRedirectKey(XkbSrvInfoPtr xkbi,
  752. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  753. {
  754. DeviceEvent ev;
  755. int x, y;
  756. XkbStateRec old, old_prev;
  757. unsigned mods, mask;
  758. xkbDeviceInfoPtr xkbPrivPtr = XKBDEVICEINFO(xkbi->device);
  759. ProcessInputProc backupproc;
  760. if ((filter->keycode != 0) && (filter->keycode != keycode))
  761. return 1;
  762. /* This can happen if the key repeats, and the state (modifiers or group)
  763. changes meanwhile. */
  764. if ((filter->keycode == keycode) && pAction &&
  765. (pAction->type != XkbSA_RedirectKey))
  766. return 1;
  767. /* never actually used uninitialised, but gcc isn't smart enough
  768. * to work that out. */
  769. memset(&old, 0, sizeof(old));
  770. memset(&old_prev, 0, sizeof(old_prev));
  771. memset(&ev, 0, sizeof(ev));
  772. GetSpritePosition(xkbi->device, &x, &y);
  773. ev.header = ET_Internal;
  774. ev.length = sizeof(DeviceEvent);
  775. ev.time = GetTimeInMillis();
  776. ev.root_x = x;
  777. ev.root_y = y;
  778. /* redirect actions do not work across devices, therefore the following is
  779. * correct: */
  780. ev.deviceid = xkbi->device->id;
  781. /* filter->priv must be set up by the caller for the initial press. */
  782. ev.sourceid = filter->priv;
  783. if (filter->keycode == 0) { /* initial press */
  784. if ((pAction->redirect.new_key < xkbi->desc->min_key_code) ||
  785. (pAction->redirect.new_key > xkbi->desc->max_key_code)) {
  786. return 1;
  787. }
  788. filter->keycode = keycode;
  789. filter->active = 1;
  790. filter->filterOthers = 0;
  791. filter->filter = _XkbFilterRedirectKey;
  792. filter->upAction = *pAction;
  793. ev.type = ET_KeyPress;
  794. ev.detail.key = pAction->redirect.new_key;
  795. mask = XkbSARedirectVModsMask(&pAction->redirect);
  796. mods = XkbSARedirectVMods(&pAction->redirect);
  797. if (mask)
  798. XkbVirtualModsToReal(xkbi->desc, mask, &mask);
  799. if (mods)
  800. XkbVirtualModsToReal(xkbi->desc, mods, &mods);
  801. mask |= pAction->redirect.mods_mask;
  802. mods |= pAction->redirect.mods;
  803. if (mask || mods) {
  804. old = xkbi->state;
  805. old_prev = xkbi->prev_state;
  806. xkbi->state.base_mods &= ~mask;
  807. xkbi->state.base_mods |= (mods & mask);
  808. xkbi->state.latched_mods &= ~mask;
  809. xkbi->state.latched_mods |= (mods & mask);
  810. xkbi->state.locked_mods &= ~mask;
  811. xkbi->state.locked_mods |= (mods & mask);
  812. XkbComputeDerivedState(xkbi);
  813. xkbi->prev_state = xkbi->state;
  814. }
  815. UNWRAP_PROCESS_INPUT_PROC(xkbi->device, xkbPrivPtr, backupproc);
  816. xkbi->device->public.processInputProc((InternalEvent *) &ev,
  817. xkbi->device);
  818. COND_WRAP_PROCESS_INPUT_PROC(xkbi->device, xkbPrivPtr, backupproc,
  819. xkbUnwrapProc);
  820. if (mask || mods) {
  821. xkbi->state = old;
  822. xkbi->prev_state = old_prev;
  823. }
  824. return 0;
  825. }
  826. else {
  827. /* If it is a key release, or we redirect to another key, release the
  828. previous new_key. Otherwise, repeat. */
  829. ev.detail.key = filter->upAction.redirect.new_key;
  830. if (pAction == NULL || ev.detail.key != pAction->redirect.new_key) {
  831. ev.type = ET_KeyRelease;
  832. filter->active = 0;
  833. }
  834. else {
  835. ev.type = ET_KeyPress;
  836. ev.key_repeat = TRUE;
  837. }
  838. mask = XkbSARedirectVModsMask(&filter->upAction.redirect);
  839. mods = XkbSARedirectVMods(&filter->upAction.redirect);
  840. if (mask)
  841. XkbVirtualModsToReal(xkbi->desc, mask, &mask);
  842. if (mods)
  843. XkbVirtualModsToReal(xkbi->desc, mods, &mods);
  844. mask |= filter->upAction.redirect.mods_mask;
  845. mods |= filter->upAction.redirect.mods;
  846. if (mask || mods) {
  847. old = xkbi->state;
  848. old_prev = xkbi->prev_state;
  849. xkbi->state.base_mods &= ~mask;
  850. xkbi->state.base_mods |= (mods & mask);
  851. xkbi->state.latched_mods &= ~mask;
  852. xkbi->state.latched_mods |= (mods & mask);
  853. xkbi->state.locked_mods &= ~mask;
  854. xkbi->state.locked_mods |= (mods & mask);
  855. XkbComputeDerivedState(xkbi);
  856. xkbi->prev_state = xkbi->state;
  857. }
  858. UNWRAP_PROCESS_INPUT_PROC(xkbi->device, xkbPrivPtr, backupproc);
  859. xkbi->device->public.processInputProc((InternalEvent *) &ev,
  860. xkbi->device);
  861. COND_WRAP_PROCESS_INPUT_PROC(xkbi->device, xkbPrivPtr, backupproc,
  862. xkbUnwrapProc);
  863. if (mask || mods) {
  864. xkbi->state = old;
  865. xkbi->prev_state = old_prev;
  866. }
  867. /* We return 1 in case we have sent a release event because the new_key
  868. has changed. Then, subsequently, we will call this function again
  869. with the same pAction, which will create the press for the new
  870. new_key. */
  871. return (pAction && ev.detail.key != pAction->redirect.new_key);
  872. }
  873. }
  874. static int
  875. _XkbFilterSwitchScreen(XkbSrvInfoPtr xkbi,
  876. XkbFilterPtr filter,
  877. unsigned keycode, XkbAction *pAction)
  878. {
  879. DeviceIntPtr dev = xkbi->device;
  880. if (dev == inputInfo.keyboard)
  881. return 0;
  882. if (filter->keycode == 0) { /* initial press */
  883. filter->keycode = keycode;
  884. filter->active = 1;
  885. filter->filterOthers = 0;
  886. filter->filter = _XkbFilterSwitchScreen;
  887. AccessXCancelRepeatKey(xkbi, keycode);
  888. XkbDDXSwitchScreen(dev, keycode, pAction);
  889. return 0;
  890. }
  891. else if (filter->keycode == keycode) {
  892. filter->active = 0;
  893. return 0;
  894. }
  895. return 1;
  896. }
  897. static int
  898. _XkbFilterXF86Private(XkbSrvInfoPtr xkbi,
  899. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  900. {
  901. DeviceIntPtr dev = xkbi->device;
  902. if (dev == inputInfo.keyboard)
  903. return 0;
  904. if (filter->keycode == 0) { /* initial press */
  905. filter->keycode = keycode;
  906. filter->active = 1;
  907. filter->filterOthers = 0;
  908. filter->filter = _XkbFilterXF86Private;
  909. XkbDDXPrivate(dev, keycode, pAction);
  910. return 0;
  911. }
  912. else if (filter->keycode == keycode) {
  913. filter->active = 0;
  914. return 0;
  915. }
  916. return 1;
  917. }
  918. static int
  919. _XkbFilterDeviceBtn(XkbSrvInfoPtr xkbi,
  920. XkbFilterPtr filter, unsigned keycode, XkbAction *pAction)
  921. {
  922. if (xkbi->device == inputInfo.keyboard)
  923. return 0;
  924. if (filter->keycode == 0) { /* initial press */
  925. DeviceIntPtr dev;
  926. int button;
  927. _XkbLookupButtonDevice(&dev, pAction->devbtn.device, serverClient,
  928. DixUnknownAccess, &button);
  929. if (!dev || !dev->public.on)
  930. return 1;
  931. button = pAction->devbtn.button;
  932. if ((button < 1) || (button > dev->button->numButtons))
  933. return 1;
  934. filter->keycode = keycode;
  935. filter->active = 1;
  936. filter->filterOthers = 0;
  937. filter->priv = 0;
  938. filter->filter = _XkbFilterDeviceBtn;
  939. filter->upAction = *pAction;
  940. switch (pAction->type) {
  941. case XkbSA_LockDeviceBtn:
  942. if ((pAction->devbtn.flags & XkbSA_LockNoLock) ||
  943. BitIsOn(dev->button->down, button))
  944. return 0;
  945. XkbFakeDeviceButton(dev, TRUE, button);
  946. filter->upAction.type = XkbSA_NoAction;
  947. break;
  948. case XkbSA_DeviceBtn:
  949. if (pAction->devbtn.count > 0) {
  950. int nClicks, i;
  951. nClicks = pAction->btn.count;
  952. for (i = 0; i < nClicks; i++) {
  953. XkbFakeDeviceButton(dev, TRUE, button);
  954. XkbFakeDeviceButton(dev, FALSE, button);
  955. }
  956. filter->upAction.type = XkbSA_NoAction;
  957. }
  958. else
  959. XkbFakeDeviceButton(dev, TRUE, button);
  960. break;
  961. }
  962. }
  963. else if (filter->keycode == keycode) {
  964. DeviceIntPtr dev;
  965. int button;
  966. filter->active = 0;
  967. _XkbLookupButtonDevice(&dev, filter->upAction.devbtn.device,
  968. serverClient, DixUnknownAccess, &button);
  969. if (!dev || !dev->public.on)
  970. return 1;
  971. button = filter->upAction.btn.button;
  972. switch (filter->upAction.type) {
  973. case XkbSA_LockDeviceBtn:
  974. if ((filter->upAction.devbtn.flags & XkbSA_LockNoUnlock) ||
  975. !BitIsOn(dev->button->down, button))
  976. return 0;
  977. XkbFakeDeviceButton(dev, FALSE, button);
  978. break;
  979. case XkbSA_DeviceBtn:
  980. XkbFakeDeviceButton(dev, FALSE, button);
  981. break;
  982. }
  983. filter->active = 0;
  984. }
  985. return 0;
  986. }
  987. static XkbFilterPtr
  988. _XkbNextFreeFilter(XkbSrvInfoPtr xkbi)
  989. {
  990. register int i;
  991. if (xkbi->szFilters == 0) {
  992. xkbi->szFilters = 4;
  993. xkbi->filters = calloc(xkbi->szFilters, sizeof(XkbFilterRec));
  994. /* 6/21/93 (ef) -- XXX! deal with allocation failure */
  995. }
  996. for (i = 0; i < xkbi->szFilters; i++) {
  997. if (!xkbi->filters[i].active) {
  998. xkbi->filters[i].keycode = 0;
  999. return &xkbi->filters[i];
  1000. }
  1001. }
  1002. xkbi->szFilters *= 2;
  1003. xkbi->filters = realloc(xkbi->filters,
  1004. xkbi->szFilters * sizeof(XkbFilterRec));
  1005. /* 6/21/93 (ef) -- XXX! deal with allocation failure */
  1006. memset(&xkbi->filters[xkbi->szFilters / 2], 0,
  1007. (xkbi->szFilters / 2) * sizeof(XkbFilterRec));
  1008. return &xkbi->filters[xkbi->szFilters / 2];
  1009. }
  1010. static int
  1011. _XkbApplyFilters(XkbSrvInfoPtr xkbi, unsigned kc, XkbAction *pAction)
  1012. {
  1013. register int i, send;
  1014. send = 1;
  1015. for (i = 0; i < xkbi->szFilters; i++) {
  1016. if ((xkbi->filters[i].active) && (xkbi->filters[i].filter))
  1017. send =
  1018. ((*xkbi->filters[i].filter) (xkbi, &xkbi->filters[i], kc,
  1019. pAction)
  1020. && send);
  1021. }
  1022. return send;
  1023. }
  1024. static int
  1025. _XkbEnsureStateChange(XkbSrvInfoPtr xkbi)
  1026. {
  1027. Bool genStateNotify = FALSE;
  1028. /* The state may change, so if we're not in the middle of sending a state
  1029. * notify, prepare for it */
  1030. if ((xkbi->flags & _XkbStateNotifyInProgress) == 0) {
  1031. xkbi->prev_state = xkbi->state;
  1032. xkbi->flags |= _XkbStateNotifyInProgress;
  1033. genStateNotify = TRUE;
  1034. }
  1035. return genStateNotify;
  1036. }
  1037. static void
  1038. _XkbApplyState(DeviceIntPtr dev, Bool genStateNotify, int evtype, int key)
  1039. {
  1040. XkbSrvInfoPtr xkbi = dev->key->xkbInfo;
  1041. int changed;
  1042. XkbComputeDerivedState(xkbi);
  1043. changed = XkbStateChangedFlags(&xkbi->prev_state, &xkbi->state);
  1044. if (genStateNotify) {
  1045. if (changed) {
  1046. xkbStateNotify sn;
  1047. sn.keycode = key;
  1048. sn.eventType = evtype;
  1049. sn.requestMajor = sn.requestMinor = 0;
  1050. sn.changed = changed;
  1051. XkbSendStateNotify(dev, &sn);
  1052. }
  1053. xkbi->flags &= ~_XkbStateNotifyInProgress;
  1054. }
  1055. changed = XkbIndicatorsToUpdate(dev, changed, FALSE);
  1056. if (changed) {
  1057. XkbEventCauseRec cause;
  1058. XkbSetCauseKey(&cause, key, evtype);
  1059. XkbUpdateIndicators(dev, changed, FALSE, NULL, &cause);
  1060. }
  1061. }
  1062. void
  1063. XkbPushLockedStateToSlaves(DeviceIntPtr master, int evtype, int key)
  1064. {
  1065. DeviceIntPtr dev;
  1066. Bool genStateNotify;
  1067. nt_list_for_each_entry(dev, inputInfo.devices, next) {
  1068. if (!dev->key || GetMaster(dev, MASTER_KEYBOARD) != master)
  1069. continue;
  1070. genStateNotify = _XkbEnsureStateChange(dev->key->xkbInfo);
  1071. dev->key->xkbInfo->state.locked_mods =
  1072. master->key->xkbInfo->state.locked_mods;
  1073. _XkbApplyState(dev, genStateNotify, evtype, key);
  1074. }
  1075. }
  1076. void
  1077. XkbHandleActions(DeviceIntPtr dev, DeviceIntPtr kbd, DeviceEvent *event)
  1078. {
  1079. int key, bit, i;
  1080. XkbSrvInfoPtr xkbi;
  1081. KeyClassPtr keyc;
  1082. int sendEvent;
  1083. Bool genStateNotify;
  1084. XkbAction act;
  1085. XkbFilterPtr filter;
  1086. Bool keyEvent;
  1087. Bool pressEvent;
  1088. ProcessInputProc backupproc;
  1089. xkbDeviceInfoPtr xkbPrivPtr = XKBDEVICEINFO(dev);
  1090. keyc = kbd->key;
  1091. xkbi = keyc->xkbInfo;
  1092. key = event->detail.key;
  1093. genStateNotify = _XkbEnsureStateChange(xkbi);
  1094. xkbi->clearMods = xkbi->setMods = 0;
  1095. xkbi->groupChange = 0;
  1096. sendEvent = 1;
  1097. keyEvent = ((event->type == ET_KeyPress) || (event->type == ET_KeyRelease));
  1098. pressEvent = ((event->type == ET_KeyPress) ||
  1099. (event->type == ET_ButtonPress));
  1100. if (pressEvent) {
  1101. if (keyEvent)
  1102. act = XkbGetKeyAction(xkbi, &xkbi->state, key);
  1103. else {
  1104. act = XkbGetButtonAction(kbd, dev, key);
  1105. key |= BTN_ACT_FLAG;
  1106. }
  1107. sendEvent = _XkbApplyFilters(xkbi, key, &act);
  1108. if (sendEvent) {
  1109. switch (act.type) {
  1110. case XkbSA_SetMods:
  1111. case XkbSA_SetGroup:
  1112. filter = _XkbNextFreeFilter(xkbi);
  1113. sendEvent = _XkbFilterSetState(xkbi, filter, key, &act);
  1114. break;
  1115. case XkbSA_LatchMods:
  1116. case XkbSA_LatchGroup:
  1117. filter = _XkbNextFreeFilter(xkbi);
  1118. sendEvent = _XkbFilterLatchState(xkbi, filter, key, &act);
  1119. break;
  1120. case XkbSA_LockMods:
  1121. case XkbSA_LockGroup:
  1122. filter = _XkbNextFreeFilter(xkbi);
  1123. sendEvent = _XkbFilterLockState(xkbi, filter, key, &act);
  1124. break;
  1125. case XkbSA_ISOLock:
  1126. filter = _XkbNextFreeFilter(xkbi);
  1127. sendEvent = _XkbFilterISOLock(xkbi, filter, key, &act);
  1128. break;
  1129. case XkbSA_MovePtr:
  1130. filter = _XkbNextFreeFilter(xkbi);
  1131. sendEvent = _XkbFilterPointerMove(xkbi, filter, key, &act);
  1132. break;
  1133. case XkbSA_PtrBtn:
  1134. case XkbSA_LockPtrBtn:
  1135. case XkbSA_SetPtrDflt:
  1136. filter = _XkbNextFreeFilter(xkbi);
  1137. sendEvent = _XkbFilterPointerBtn(xkbi, filter, key, &act);
  1138. break;
  1139. case XkbSA_Terminate:
  1140. sendEvent = XkbDDXTerminateServer(dev, key, &act);
  1141. break;
  1142. case XkbSA_SwitchScreen:
  1143. filter = _XkbNextFreeFilter(xkbi);
  1144. sendEvent = _XkbFilterSwitchScreen(xkbi, filter, key, &act);
  1145. break;
  1146. case XkbSA_SetControls:
  1147. case XkbSA_LockControls:
  1148. filter = _XkbNextFreeFilter(xkbi);
  1149. sendEvent = _XkbFilterControls(xkbi, filter, key, &act);
  1150. break;
  1151. case XkbSA_ActionMessage:
  1152. filter = _XkbNextFreeFilter(xkbi);
  1153. sendEvent = _XkbFilterActionMessage(xkbi, filter, key, &act);
  1154. break;
  1155. case XkbSA_RedirectKey:
  1156. filter = _XkbNextFreeFilter(xkbi);
  1157. /* redirect actions must create a new DeviceEvent. The
  1158. * source device id for this event cannot be obtained from
  1159. * xkbi, so we pass it here explicitly. The field deviceid
  1160. * equals to xkbi->device->id. */
  1161. filter->priv = event->sourceid;
  1162. sendEvent = _XkbFilterRedirectKey(xkbi, filter, key, &act);
  1163. break;
  1164. case XkbSA_DeviceBtn:
  1165. case XkbSA_LockDeviceBtn:
  1166. filter = _XkbNextFreeFilter(xkbi);
  1167. sendEvent = _XkbFilterDeviceBtn(xkbi, filter, key, &act);
  1168. break;
  1169. case XkbSA_XFree86Private:
  1170. filter = _XkbNextFreeFilter(xkbi);
  1171. sendEvent = _XkbFilterXF86Private(xkbi, filter, key, &act);
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. else {
  1177. if (!keyEvent)
  1178. key |= BTN_ACT_FLAG;
  1179. sendEvent = _XkbApplyFilters(xkbi, key, NULL);
  1180. }
  1181. if (xkbi->groupChange != 0)
  1182. xkbi->state.base_group += xkbi->groupChange;
  1183. if (xkbi->setMods) {
  1184. for (i = 0, bit = 1; xkbi->setMods; i++, bit <<= 1) {
  1185. if (xkbi->setMods & bit) {
  1186. keyc->modifierKeyCount[i]++;
  1187. xkbi->state.base_mods |= bit;
  1188. xkbi->setMods &= ~bit;
  1189. }
  1190. }
  1191. }
  1192. if (xkbi->clearMods) {
  1193. for (i = 0, bit = 1; xkbi->clearMods; i++, bit <<= 1) {
  1194. if (xkbi->clearMods & bit) {
  1195. keyc->modifierKeyCount[i]--;
  1196. if (keyc->modifierKeyCount[i] <= 0) {
  1197. xkbi->state.base_mods &= ~bit;
  1198. keyc->modifierKeyCount[i] = 0;
  1199. }
  1200. xkbi->clearMods &= ~bit;
  1201. }
  1202. }
  1203. }
  1204. if (sendEvent) {
  1205. DeviceIntPtr tmpdev;
  1206. if (keyEvent)
  1207. tmpdev = dev;
  1208. else
  1209. tmpdev = GetMaster(dev, POINTER_OR_FLOAT);
  1210. UNWRAP_PROCESS_INPUT_PROC(tmpdev, xkbPrivPtr, backupproc);
  1211. dev->public.processInputProc((InternalEvent *) event, tmpdev);
  1212. COND_WRAP_PROCESS_INPUT_PROC(tmpdev, xkbPrivPtr,
  1213. backupproc, xkbUnwrapProc);
  1214. }
  1215. else if (keyEvent) {
  1216. FixKeyState(event, dev);
  1217. }
  1218. _XkbApplyState(dev, genStateNotify, event->type, key);
  1219. XkbPushLockedStateToSlaves(dev, event->type, key);
  1220. }
  1221. int
  1222. XkbLatchModifiers(DeviceIntPtr pXDev, CARD8 mask, CARD8 latches)
  1223. {
  1224. XkbSrvInfoPtr xkbi;
  1225. XkbFilterPtr filter;
  1226. XkbAction act;
  1227. unsigned clear;
  1228. if (pXDev && pXDev->key && pXDev->key->xkbInfo) {
  1229. xkbi = pXDev->key->xkbInfo;
  1230. clear = (mask & (~latches));
  1231. xkbi->state.latched_mods &= ~clear;
  1232. /* Clear any pending latch to locks.
  1233. */
  1234. act.type = XkbSA_NoAction;
  1235. _XkbApplyFilters(xkbi, SYNTHETIC_KEYCODE, &act);
  1236. act.type = XkbSA_LatchMods;
  1237. act.mods.flags = 0;
  1238. act.mods.mask = mask & latches;
  1239. filter = _XkbNextFreeFilter(xkbi);
  1240. _XkbFilterLatchState(xkbi, filter, SYNTHETIC_KEYCODE, &act);
  1241. _XkbFilterLatchState(xkbi, filter, SYNTHETIC_KEYCODE,
  1242. (XkbAction *) NULL);
  1243. return Success;
  1244. }
  1245. return BadValue;
  1246. }
  1247. int
  1248. XkbLatchGroup(DeviceIntPtr pXDev, int group)
  1249. {
  1250. XkbSrvInfoPtr xkbi;
  1251. XkbFilterPtr filter;
  1252. XkbAction act;
  1253. if (pXDev && pXDev->key && pXDev->key->xkbInfo) {
  1254. xkbi = pXDev->key->xkbInfo;
  1255. act.type = XkbSA_LatchGroup;
  1256. act.group.flags = 0;
  1257. XkbSASetGroup(&act.group, group);
  1258. filter = _XkbNextFreeFilter(xkbi);
  1259. _XkbFilterLatchState(xkbi, filter, SYNTHETIC_KEYCODE, &act);
  1260. _XkbFilterLatchState(xkbi, filter, SYNTHETIC_KEYCODE,
  1261. (XkbAction *) NULL);
  1262. return Success;
  1263. }
  1264. return BadValue;
  1265. }
  1266. /***====================================================================***/
  1267. void
  1268. XkbClearAllLatchesAndLocks(DeviceIntPtr dev,
  1269. XkbSrvInfoPtr xkbi,
  1270. Bool genEv, XkbEventCausePtr cause)
  1271. {
  1272. XkbStateRec os;
  1273. xkbStateNotify sn;
  1274. sn.changed = 0;
  1275. os = xkbi->state;
  1276. if (os.latched_mods) { /* clear all latches */
  1277. XkbLatchModifiers(dev, ~0, 0);
  1278. sn.changed |= XkbModifierLatchMask;
  1279. }
  1280. if (os.latched_group) {
  1281. XkbLatchGroup(dev, 0);
  1282. sn.changed |= XkbGroupLatchMask;
  1283. }
  1284. if (os.locked_mods) {
  1285. xkbi->state.locked_mods = 0;
  1286. sn.changed |= XkbModifierLockMask;
  1287. }
  1288. if (os.locked_group) {
  1289. xkbi->state.locked_group = 0;
  1290. sn.changed |= XkbGroupLockMask;
  1291. }
  1292. if (genEv && sn.changed) {
  1293. CARD32 changed;
  1294. XkbComputeDerivedState(xkbi);
  1295. sn.keycode = cause->kc;
  1296. sn.eventType = cause->event;
  1297. sn.requestMajor = cause->mjr;
  1298. sn.requestMinor = cause->mnr;
  1299. sn.changed = XkbStateChangedFlags(&os, &xkbi->state);
  1300. XkbSendStateNotify(dev, &sn);
  1301. changed = XkbIndicatorsToUpdate(dev, sn.changed, FALSE);
  1302. if (changed) {
  1303. XkbUpdateIndicators(dev, changed, TRUE, NULL, cause);
  1304. }
  1305. }
  1306. return;
  1307. }
  1308. /*
  1309. * The event is injected into the event processing, not the EQ. Thus,
  1310. * ensure that we restore the master after the event sequence to the
  1311. * original set of classes. Otherwise, the master remains on the XTEST
  1312. * classes and drops events that don't fit into the XTEST layout (e.g.
  1313. * events with more than 2 valuators).
  1314. *
  1315. * FIXME: EQ injection in the processing stage is not designed for, so this
  1316. * is a rather awkward hack. The event list returned by GetPointerEvents()
  1317. * and friends is always prefixed with a DCE if the last _posted_ device was
  1318. * different. For normal events, this sequence then resets the master during
  1319. * the processing stage. Since we inject the PointerKey events in the
  1320. * processing stage though, we need to manually reset to restore the
  1321. * previous order, because the events already in the EQ must be sent for the
  1322. * right device.
  1323. * So we post-fix the event list we get from GPE with a DCE back to the
  1324. * previous slave device.
  1325. *
  1326. * First one on drinking island wins!
  1327. */
  1328. static void
  1329. InjectPointerKeyEvents(DeviceIntPtr dev, int type, int button, int flags,
  1330. ValuatorMask *mask)
  1331. {
  1332. ScreenPtr pScreen;
  1333. InternalEvent *events;
  1334. int nevents, i;
  1335. DeviceIntPtr ptr, mpointer, lastSlave = NULL;
  1336. Bool saveWait;
  1337. if (IsMaster(dev)) {
  1338. mpointer = GetMaster(dev, MASTER_POINTER);
  1339. lastSlave = mpointer->lastSlave;
  1340. ptr = GetXTestDevice(mpointer);
  1341. }
  1342. else if (IsFloating(dev))
  1343. ptr = dev;
  1344. else
  1345. return;
  1346. events = InitEventList(GetMaximumEventsNum() + 1);
  1347. OsBlockSignals();
  1348. pScreen = miPointerGetScreen(ptr);
  1349. saveWait = miPointerSetWaitForUpdate(pScreen, FALSE);
  1350. nevents = GetPointerEvents(events, ptr, type, button, flags, mask);
  1351. if (IsMaster(dev) && (lastSlave && lastSlave != ptr))
  1352. UpdateFromMaster(&events[nevents], lastSlave, DEVCHANGE_POINTER_EVENT,
  1353. &nevents);
  1354. miPointerSetWaitForUpdate(pScreen, saveWait);
  1355. OsReleaseSignals();
  1356. for (i = 0; i < nevents; i++)
  1357. mieqProcessDeviceEvent(ptr, &events[i], NULL);
  1358. FreeEventList(events, GetMaximumEventsNum());
  1359. }
  1360. static void
  1361. XkbFakePointerMotion(DeviceIntPtr dev, unsigned flags, int x, int y)
  1362. {
  1363. ValuatorMask mask;
  1364. int gpe_flags = 0;
  1365. /* ignore attached SDs */
  1366. if (!IsMaster(dev) && !IsFloating(dev))
  1367. return;
  1368. if (flags & XkbSA_MoveAbsoluteX || flags & XkbSA_MoveAbsoluteY)
  1369. gpe_flags = POINTER_ABSOLUTE;
  1370. else
  1371. gpe_flags = POINTER_RELATIVE;
  1372. valuator_mask_set_range(&mask, 0, 2, (int[]) {
  1373. x, y});
  1374. InjectPointerKeyEvents(dev, MotionNotify, 0, gpe_flags, &mask);
  1375. }
  1376. void
  1377. XkbFakeDeviceButton(DeviceIntPtr dev, Bool press, int button)
  1378. {
  1379. DeviceIntPtr ptr;
  1380. int down;
  1381. /* If dev is a slave device, and the SD is attached, do nothing. If we'd
  1382. * post through the attached master pointer we'd get duplicate events.
  1383. *
  1384. * if dev is a master keyboard, post through the XTEST device
  1385. *
  1386. * if dev is a floating slave, post through the device itself.
  1387. */
  1388. if (IsMaster(dev)) {
  1389. DeviceIntPtr mpointer = GetMaster(dev, MASTER_POINTER);
  1390. ptr = GetXTestDevice(mpointer);
  1391. }
  1392. else if (IsFloating(dev))
  1393. ptr = dev;
  1394. else
  1395. return;
  1396. down = button_is_down(ptr, button, BUTTON_PROCESSED);
  1397. if (press == down)
  1398. return;
  1399. InjectPointerKeyEvents(dev, press ? ButtonPress : ButtonRelease,
  1400. button, 0, NULL);
  1401. }