add more spacing
[personal-kdebase.git] / apps / konsole / src / XKB.cpp
blob990b65202810958b2fcb3c5d2c4dc2ab0c840fcf
1 /*
2 Originally comes from NumLockX http://dforce.sh.charactervut.characterz/~seli/en/numlockx
4 NumLockX
6 Copyright 2000-2001 Lubos Lunak <l.lunak@kde.org>
7 Copyright 2001 Oswald Buddenhagen <ossi@kde.org>
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
27 ****************************************************************************/
29 #include <config-konsole.h>
31 #if defined(HAVE_XKB)
32 #include <QtGui/QX11Info>
35 #include <X11/Xlib.h>
37 #define explicit myexplicit
38 #include <X11/XKBlib.h>
39 #undef explicit
41 #include <X11/keysym.h>
43 /* the XKB stuff is based on code created by Oswald Buddenhagen <ossi@kde.org> */
44 int xkb_init()
46 int xkb_opcode, xkb_event, xkb_error;
47 int xkb_lmaj = XkbMajorVersion;
48 int xkb_lmin = XkbMinorVersion;
49 return XkbLibraryVersion( &xkb_lmaj, &xkb_lmin )
50 && XkbQueryExtension( QX11Info::display(), &xkb_opcode, &xkb_event, &xkb_error,
51 &xkb_lmaj, &xkb_lmin );
54 #if 0
55 // This method doesn't work in all cases. The atom "ScrollLock" doesn't seem
56 // to exist on all XFree versions (at least it's not here with my 3.3.6) - DF
57 static unsigned int xkb_mask_modifier( XkbDescPtr xkb, const char *name )
59 int i;
60 if( !xkb || !xkb->names )
61 return 0;
63 Atom atom = XInternAtom( xkb->dpy, name, true );
64 if (atom == None)
65 return 0;
67 for( i = 0;
68 i < XkbNumVirtualMods;
69 i++ )
71 if (atom == xkb->names->vmods[i] )
73 unsigned int mask;
74 XkbVirtualModsToReal( xkb, 1 << i, &mask );
75 return mask;
78 return 0;
81 static unsigned int xkb_scrolllock_mask()
83 XkbDescPtr xkb;
84 if(( xkb = XkbGetKeyboard( QX11Info::display(), XkbAllComponentsMask, XkbUseCoreKbd )) != NULL )
86 unsigned int mask = xkb_mask_modifier( xkb, "ScrollLock" );
87 XkbFreeKeyboard( xkb, 0, True );
88 return mask;
90 return 0;
93 #else
94 unsigned int xkb_scrolllock_mask()
96 int scrolllock_mask = 0;
97 XModifierKeymap* map = XGetModifierMapping( QX11Info::display() );
98 KeyCode scrolllock_keycode = XKeysymToKeycode( QX11Info::display(), XK_Scroll_Lock );
99 if( scrolllock_keycode == NoSymbol ) {
100 XFreeModifiermap(map);
101 return 0;
103 for( int i = 0;
104 i < 8;
105 ++i )
107 if( map->modifiermap[ map->max_keypermod * i ] == scrolllock_keycode )
108 scrolllock_mask += 1 << i;
111 XFreeModifiermap(map);
112 return scrolllock_mask;
114 #endif
117 unsigned int scrolllock_mask = 0;
119 int xkb_set_on()
121 if (!scrolllock_mask)
123 if( !xkb_init())
124 return 0;
125 scrolllock_mask = xkb_scrolllock_mask();
126 if( scrolllock_mask == 0 )
127 return 0;
129 XkbLockModifiers ( QX11Info::display(), XkbUseCoreKbd, scrolllock_mask, scrolllock_mask);
130 return 1;
133 int xkb_set_off()
135 if (!scrolllock_mask)
137 if( !xkb_init())
138 return 0;
139 scrolllock_mask = xkb_scrolllock_mask();
140 if( scrolllock_mask == 0 )
141 return 0;
143 XkbLockModifiers ( QX11Info::display(), XkbUseCoreKbd, scrolllock_mask, 0);
144 return 1;
147 void scrolllock_set_on()
149 xkb_set_on();
152 void scrolllock_set_off()
154 xkb_set_off();
157 #endif // defined(HAVE_XKB)