numlock.cpp 3.3 KB

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