Browse Source

Fix logic for numlock and update coding style (#16536)

The problem from which Numlock is set to Off when setOn is called.

Signed-off-by: Nobuhiro Iwamatsu <iwamatsu@nigauri.org>

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/slim/trunk@194 7c53e7cc-98ea-0310-8f1f-a0b24da60408
iwamatsu 12 years ago
parent
commit
b6109fa1b8
3 changed files with 30 additions and 25 deletions
  1. 2 2
      app.cpp
  2. 24 21
      numlock.cpp
  3. 4 2
      numlock.h

+ 2 - 2
app.cpp

@@ -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;

+ 24 - 21
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
@@ -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 );
@@ -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 ) {
@@ -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>

+ 4 - 2
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
@@ -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