1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/****************************************************************************
NumLockX
$Id: main.c,v 1.10 2001/04/30 20:55:20 seli Exp $
Copyright (C) 2000-2001 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2001 Oswald Buddenhagen <ossi@kde.org>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
****************************************************************************/
#define __main_C
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#ifdef HAVE_XTEST
#include <X11/extensions/XTest.h>
#endif
#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif
#include <X11/keysym.h>
void usage( const char* argv0 )
{
printf( "NumLockX " VERSION "\n"
"(C) 2000-2001 Lubos Lunak <l.lunak@kde.org>\n"
"(C) 2001 Oswald Buddenhagen <ossi@kde.org>\n\n"
"Usage: %s [on|off|toggle|status]\n"
"on - turns NumLock on in X ( default )\n"
"off - turns NumLock off in X\n"
"toggle - toggles the NumLock on and off in X\n"
"status - gets the NumLock status\n"
"\n"
, argv0 );
}
Display* dpy;
/* the XKB stuff is based on code created by Oswald Buddenhagen <ossi@kde.org> */
#ifdef HAVE_XKB
int xkb_init()
{
int xkb_opcode, xkb_event, xkb_error;
int xkb_lmaj = XkbMajorVersion;
int xkb_lmin = XkbMinorVersion;
return XkbLibraryVersion( &xkb_lmaj, &xkb_lmin )
&& XkbQueryExtension( dpy, &xkb_opcode, &xkb_event, &xkb_error,
&xkb_lmaj, &xkb_lmin );
}
unsigned int xkb_mask_modifier( XkbDescPtr xkb, const char *name )
{
int i;
if( !xkb || !xkb->names )
return 0;
for( i = 0;
i < XkbNumVirtualMods;
i++ )
{
char* modStr = XGetAtomName( xkb->dpy, xkb->names->vmods[i] );
if( modStr != NULL && strcmp(name, modStr) == 0 )
{
unsigned int mask;
XkbVirtualModsToReal( xkb, 1 << i, &mask );
return mask;
}
}
return 0;
}
unsigned int xkb_numlock_mask()
{
XkbDescPtr xkb;
if(( xkb = XkbGetKeyboard( dpy, XkbAllComponentsMask, XkbUseCoreKbd )) != NULL )
{
unsigned int mask = xkb_mask_modifier( xkb, "NumLock" );
XkbFreeKeyboard( xkb, 0, True );
return mask;
}
return 0;
}
int xkb_set_on()
{
unsigned int mask;
if( !xkb_init())
return 0;
mask = xkb_numlock_mask();
if( mask == 0 )
return 0;
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, mask);
return 1;
}
int xkb_set_off()
{
unsigned int mask;
if( !xkb_init())
return 0;
mask = xkb_numlock_mask();
if( mask == 0 )
return 0;
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, 0);
return 1;
}
int xkb_toggle()
{
unsigned int mask;
unsigned int numlockState;
XkbStateRec xkbState;
if( !xkb_init())
return 0;
mask = xkb_numlock_mask();
if( mask == 0 )
return 0;
XkbGetState( dpy, XkbUseCoreKbd, &xkbState);
numlockState = xkbState.locked_mods & mask;
if (numlockState)
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, 0);
else
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, mask);
return 1;
}
int xkb_status()
{
unsigned int mask;
unsigned int numlockState;
XkbStateRec xkbState;
if( !xkb_init())
return 0;
mask = xkb_numlock_mask();
if( mask == 0 )
return 0;
XkbGetState( dpy, XkbUseCoreKbd, &xkbState);
numlockState = xkbState.locked_mods & mask;
if (numlockState)
printf("Numlock is on\n");
else
printf("Numlock is off\n");
return 1;
}
#endif
#ifdef HAVE_XTEST
int xtest_get_numlock_state()
{
int i;
int numlock_mask = 0;
Window dummy1, dummy2;
int dummy3, dummy4, dummy5, dummy6;
unsigned int mask;
XModifierKeymap* map = XGetModifierMapping( dpy );
KeyCode numlock_keycode = XKeysymToKeycode( dpy, XK_Num_Lock );
if( numlock_keycode == NoSymbol )
return 0;
for( i = 0;
i < 8;
++i )
{
if( map->modifiermap[ map->max_keypermod * i ] == numlock_keycode )
numlock_mask = 1 << i;
}
XQueryPointer( dpy, DefaultRootWindow( dpy ), &dummy1, &dummy2,
&dummy3, &dummy4, &dummy5, &dummy6, &mask );
XFreeModifiermap( map );
return mask & numlock_mask;
}
void xtest_change_numlock()
{
XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, XK_Num_Lock ), True, CurrentTime );
XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, XK_Num_Lock ), False, CurrentTime );
}
void xtest_set_on()
{
if( !xtest_get_numlock_state())
xtest_change_numlock();
}
void xtest_set_off()
{
if( xtest_get_numlock_state())
xtest_change_numlock();
}
void xtest_toggle()
{
xtest_change_numlock();
}
void xtest_status()
{
if( xtest_get_numlock_state())
printf("Numlock is on\n");
else
printf("Numlock is off\n");
}
#endif
void numlock_set_on()
{
#ifdef HAVE_XKB
if( xkb_set_on())
return;
#endif
#ifdef HAVE_XTEST
xtest_set_on();
#endif
}
void numlock_set_off()
{
#ifdef HAVE_XKB
if( xkb_set_off())
return;
#endif
#ifdef HAVE_XTEST
xtest_set_off();
#endif
}
void numlock_toggle()
{
#ifdef HAVE_XKB
if( xkb_toggle())
return;
#endif
#ifdef HAVE_XTEST
xtest_toggle();
#endif
}
void numlock_status()
{
#ifdef HAVE_XKB
if( xkb_status())
return;
#endif
#ifdef HAVE_XTEST
xtest_status();
#endif
}
int main( int argc, char* argv[] )
{
if( argc > 2 )
{
usage( argv[ 0 ] );
return 1;
}
dpy = XOpenDisplay( NULL );
if( dpy == NULL )
{
fprintf( stderr, "Error opening display!\n" );
return 1;
}
if( argc == 1 )
numlock_set_on();
else if( strcmp( argv[ 1 ], "on" ) == 0 )
numlock_set_on();
else if( strcmp( argv[ 1 ], "off" ) == 0 )
numlock_set_off();
else if( strcmp( argv[ 1 ], "toggle" ) == 0 )
numlock_toggle();
else if( strcmp( argv[ 1 ], "status" ) == 0 )
numlock_status();
else
{
usage( argv[ 0 ] );
XCloseDisplay( dpy );
return 2;
}
XCloseDisplay( dpy );
return 0;
}
|