Skip to content

Commit

Permalink
Fix logic for numlock and update coding style (#16536)
Browse files Browse the repository at this point in the history
The problem from which Numlock is set to Off when setOn is called.

Signed-off-by: Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
  • Loading branch information
iwamatsu committed Feb 13, 2012
1 parent 3dfcdd8 commit 0333c59
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app.cpp
Expand Up @@ -928,9 +928,9 @@ int App::StartServer() {

string numlock = cfg->getOption("numlock");
if (numlock == "on") {
NumLock::setOn();
NumLock::setOn(Dpy);
} else if (numlock == "off") {
NumLock::setOff();
NumLock::setOff(Dpy);
}

delete args;
Expand Down
45 changes: 24 additions & 21 deletions numlock.cpp
@@ -1,6 +1,7 @@
/* SLiM - Simple Login Manager
Copyright (C) 2004-06 Simone Rota <sip@varlock.com>
Copyright (C) 2004-06 Johannes Winkelmann <jw@tks6.net>
Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -22,6 +23,7 @@ int NumLock::xkb_init(Display* dpy) {
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 );
Expand All @@ -30,7 +32,8 @@ int NumLock::xkb_init(Display* dpy) {
unsigned int NumLock::xkb_mask_modifier( XkbDescPtr xkb, const char *name ) {
int i;
if( !xkb || !xkb->names )
return 0;
return 0;

for( i = 0; i < XkbNumVirtualMods; i++ ) {
char* modStr = XGetAtomName( xkb->dpy, xkb->names->vmods[i] );
if( modStr != NULL && strcmp(name, modStr) == 0 ) {
Expand All @@ -44,39 +47,39 @@ unsigned int NumLock::xkb_mask_modifier( XkbDescPtr xkb, const char *name ) {

unsigned int NumLock::xkb_numlock_mask(Display* dpy) {
XkbDescPtr xkb;
if(( xkb = XkbGetKeyboard( dpy, XkbAllComponentsMask, XkbUseCoreKbd )) != NULL ) {

xkb = XkbGetKeyboard( dpy, XkbAllComponentsMask, XkbUseCoreKbd );
if( xkb != NULL ) {
unsigned int mask = xkb_mask_modifier( xkb, "NumLock" );
XkbFreeKeyboard( xkb, 0, True );
return mask;
}
return 0;
}
void NumLock::setOn() {

void NumLock::control_numlock(Display *dpy, bool flag) {
unsigned int mask;
Display* dpy = XOpenDisplay( NULL );
if( !xkb_init(dpy))
return;
mask = xkb_numlock_mask(dpy);
if( mask == 0 )
return;
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, mask);
XCloseDisplay( dpy );
}

void NumLock::setOff() {
unsigned int mask;
Display* dpy = XOpenDisplay( NULL );
if( !xkb_init(dpy))
if( !xkb_init(dpy) )
return;

mask = xkb_numlock_mask(dpy);
if( mask == 0 )
return;
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, 0);
XCloseDisplay( dpy );

if( flag == true )
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, 0);
else
XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, mask);
}

void NumLock::setOn(Display *dpy) {
control_numlock(dpy, true);
}

void NumLock::setOff(Display *dpy) {
control_numlock(dpy, false);
}



/*
Copyright (C) 2000-2001 Lubos Lunak <l.lunak@kde.org>
Expand Down
6 changes: 4 additions & 2 deletions numlock.h
@@ -1,6 +1,7 @@
/* SLiM - Simple Login Manager
Copyright (C) 2004-06 Simone Rota <sip@varlock.com>
Copyright (C) 2004-06 Johannes Winkelmann <jw@tks6.net>
Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -19,13 +20,14 @@ class NumLock {

public:
NumLock();
static void setOn();
static void setOff();
static void setOn(Display *dpy);
static void setOff(Display *dpy);

private:
static int xkb_init(Display* dpy);
static unsigned int xkb_mask_modifier( XkbDescPtr xkb, const char *name );
static unsigned int xkb_numlock_mask(Display* dpy);
static void control_numlock(Display *dpy, bool flag);
};

#endif

0 comments on commit 0333c59

Please sign in to comment.