numlock.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* SLiM - Simple Login Manager
  2. Copyright (C) 2004-06 Simone Rota <sip@varlock.com>
  3. Copyright (C) 2004-06 Johannes Winkelmann <jw@tks6.net>
  4. Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
  5. This program 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. Code adapted from NumLockX, look at the end of this file for
  10. the original Copyright information.
  11. */
  12. #include "numlock.h"
  13. #include <string.h>
  14. NumLock::NumLock() {
  15. }
  16. int NumLock::xkb_init(Display* dpy) {
  17. int xkb_opcode, xkb_event, xkb_error;
  18. int xkb_lmaj = XkbMajorVersion;
  19. int xkb_lmin = XkbMinorVersion;
  20. return XkbLibraryVersion( &xkb_lmaj, &xkb_lmin )
  21. && XkbQueryExtension( dpy, &xkb_opcode, &xkb_event, &xkb_error,
  22. &xkb_lmaj, &xkb_lmin );
  23. }
  24. unsigned int NumLock::xkb_mask_modifier( XkbDescPtr xkb, const char *name ) {
  25. int i;
  26. if( !xkb || !xkb->names )
  27. return 0;
  28. for( i = 0; i < XkbNumVirtualMods; i++ ) {
  29. char* modStr = XGetAtomName( xkb->dpy, xkb->names->vmods[i] );
  30. if( modStr != NULL && strcmp(name, modStr) == 0 ) {
  31. unsigned int mask;
  32. XkbVirtualModsToReal( xkb, 1 << i, &mask );
  33. return mask;
  34. }
  35. }
  36. return 0;
  37. }
  38. unsigned int NumLock::xkb_numlock_mask(Display* dpy) {
  39. XkbDescPtr xkb;
  40. xkb = XkbGetKeyboard( dpy, XkbAllComponentsMask, XkbUseCoreKbd );
  41. if( xkb != NULL ) {
  42. unsigned int mask = xkb_mask_modifier( xkb, "NumLock" );
  43. XkbFreeKeyboard( xkb, 0, True );
  44. return mask;
  45. }
  46. return 0;
  47. }
  48. void NumLock::control_numlock(Display *dpy, bool flag) {
  49. unsigned int mask;
  50. if( !xkb_init(dpy) )
  51. return;
  52. mask = xkb_numlock_mask(dpy);
  53. if( mask == 0 )
  54. return;
  55. if( flag == true )
  56. XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, mask);
  57. else
  58. XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, 0);
  59. }
  60. void NumLock::setOn(Display *dpy) {
  61. control_numlock(dpy, true);
  62. }
  63. void NumLock::setOff(Display *dpy) {
  64. control_numlock(dpy, false);
  65. }
  66. /*
  67. Copyright (C) 2000-2001 Lubos Lunak <l.lunak@kde.org>
  68. Copyright (C) 2001 Oswald Buddenhagen <ossi@kde.org>
  69. Permission is hereby granted, free of charge, to any person obtaining a
  70. copy of this software and associated documentation files (the "Software"),
  71. to deal in the Software without restriction, including without limitation
  72. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  73. and/or sell copies of the Software, and to permit persons to whom the
  74. Software is furnished to do so, subject to the following conditions:
  75. The above copyright notice and this permission notice shall be included in
  76. all copies or substantial portions of the Software.
  77. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  78. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  79. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  80. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  81. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  82. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  83. DEALINGS IN THE SOFTWARE.
  84. */