A macro can be a source of problems, because the compiler has no type on
the arguments to make checks. Using an inline function allows to do those
checks, meaning clearer error messages, it provides clear info in case of
name collision, it is easier to maintain (no need for the hacky '\' for
multi-lines) and the scope of visibility can be controlled more easily (no
need for #undef).

Took opportunity to change a 0 to the constant NoSymbol which is the name
defined by X for this case and another to NULL which is the right way to
set a null pointer in C.

Signed-off-by: Christophe CURIS <christophe.cu...@free.fr>
---
 src/Makefile.am | 12 ++++++++---
 src/xmodifier.c | 65 ++++++++++++++++++++++++++++++++-------------------------
 2 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 76fc18f..8f1afbf 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -90,7 +90,6 @@ wmaker_SOURCES =      \
        xinerama.h \
        xinerama.c \
        xmodifier.h \
-       xmodifier.c \
        xutil.c \
        xutil.h \
        wconfig.h \
@@ -125,9 +124,11 @@ wmaker_SOURCES += osdep_stub.c
 endif
 
 if USE_NESTED_FUNC
-wmaker_SOURCES += misc.c
+wmaker_SOURCES += misc.c \
+       xmodifier.c
 else
-nodist_wmaker_SOURCES = misc.hack_nf.c
+nodist_wmaker_SOURCES = misc.hack_nf.c \
+       xmodifier.hack_nf.c
 
 CLEANFILES = $(nodist_wmaker_SOURCES)
 
@@ -135,6 +136,11 @@ misc.hack_nf.c: misc.c 
$(top_srcdir)/script/nested-func-to-macro.sh
        $(AM_V_GEN)$(top_srcdir)/script/nested-func-to-macro.sh \
                $(srcdir)/misc.c -o $(builddir)/misc.hack_nf.c \
                -f "append_string" -f "append_modifier"
+
+xmodifier.hack_nf.c: xmodifier.c $(top_srcdir)/script/nested-func-to-macro.sh
+       $(AM_V_GEN)$(top_srcdir)/script/nested-func-to-macro.sh \
+               $(srcdir)/xmodifier.c -o $(builddir)/xmodifier.hack_nf.c \
+               -f "modwarn" -f "modbarf" -f "check_modifier"
 endif
 
 
diff --git a/src/xmodifier.c b/src/xmodifier.c
index 641a8e3..bc52573 100644
--- a/src/xmodifier.c
+++ b/src/xmodifier.c
@@ -120,51 +120,61 @@ static void x_reset_modifier_mapping(Display * display)
        int mode_bit = 0;
        XModifierKeymap *x_modifier_keymap = XGetModifierMapping(display);
 
-#define modwarn(name,old,other)                                                
        \
-    wwarning ("%s (0x%x) generates %s, which is generated by %s.",             
\
-    name, code, index_to_name (old), other)
-
-#define modbarf(name,other)                                                    
\
-    wwarning ("%s (0x%x) generates %s, which is nonsensical.",                 
\
-    name, code, other)
-
-#define check_modifier(name,mask)                                              
\
-    if ((1<<modifier_index) != mask)                                           
\
-    wwarning ("%s (0x%x) generates %s, which is nonsensical.",                 
\
-    name, code, index_to_name (modifier_index))
-
 #define store_modifier(name,old)                                               
\
     if (old && old != modifier_index)                                          
\
     wwarning ("%s (0x%x) generates both %s and %s, which is nonsensical.",     
\
     name, code, index_to_name (old),                                           
\
     index_to_name (modifier_index));                                           
\
-    if (modifier_index == ShiftMapIndex) modbarf (name,"ModShift");            
\
-    else if (modifier_index == LockMapIndex) modbarf (name,"ModLock");         
\
-    else if (modifier_index == ControlMapIndex) modbarf (name,"ModControl");   
\
+    if (modifier_index == ShiftMapIndex) { modbarf (name,"ModShift"); } \
+    else if (modifier_index == LockMapIndex) { modbarf (name,"ModLock"); } \
+    else if (modifier_index == ControlMapIndex) { modbarf (name,"ModControl"); 
} \
     else if (sym == XK_Mode_switch)                                            
\
     mode_bit = modifier_index; /* Mode_switch is special, see below... */      
\
-    else if (modifier_index == meta_bit && old != meta_bit)                    
\
+    else if (modifier_index == meta_bit && old != meta_bit) {    \
     modwarn (name, meta_bit, "Meta");                                          
\
-    else if (modifier_index == super_bit && old != super_bit)                  
\
+    } else if (modifier_index == super_bit && old != super_bit) {        \
     modwarn (name, super_bit, "Super");                                        
        \
-    else if (modifier_index == hyper_bit && old != hyper_bit)                  
\
+    } else if (modifier_index == hyper_bit && old != hyper_bit) {        \
     modwarn (name, hyper_bit, "Hyper");                                        
        \
-    else if (modifier_index == alt_bit && old != alt_bit)                      
\
+    } else if (modifier_index == alt_bit && old != alt_bit) {    \
     modwarn (name, alt_bit, "Alt");                                            
\
-    else                                                                       
\
+    } else       \
     old = modifier_index;
 
        mkpm = x_modifier_keymap->max_keypermod;
        for (modifier_index = 0; modifier_index < 8; modifier_index++)
                for (modifier_key = 0; modifier_key < mkpm; modifier_key++) {
                        KeySym last_sym = 0;
+
                        for (column = 0; column < 4; column += 2) {
-                               KeyCode code = 
x_modifier_keymap->modifiermap[modifier_index * mkpm
-                                                                             + 
modifier_key];
-                               KeySym sym = (code ? 
XkbKeycodeToKeysym(display, code, 0, column) : 0);
+                               KeyCode code;
+                               KeySym sym;
+
+                               inline void modwarn(const char *key_name, int 
old_mod, const char *other_key)
+                               {
+                                       wwarning("key %s (0x%x) generates %s, 
which is generated by %s",
+                                                key_name, code, 
index_to_name(old_mod), other_key);
+                               }
+
+                               inline void modbarf(const char *key_name, const 
char *other_mod)
+                               {
+                                       wwarning("key %s (0x%x) generates %s, 
which is nonsensical",
+                                                key_name, code, other_mod);
+                               }
+
+                               inline void check_modifier(const char 
*key_name, int mask)
+                               {
+                                       if ((1 << modifier_index) != mask)
+                                               modbarf(key_name, 
index_to_name(modifier_index));
+                               }
+
+                               code = 
x_modifier_keymap->modifiermap[modifier_index * mkpm + modifier_key];
+                               sym = (code ? XkbKeycodeToKeysym(display, code, 
0, column) : NoSymbol);
+
                                if (sym == last_sym)
                                        continue;
                                last_sym = sym;
+
                                switch (sym) {
                                case XK_Mode_switch:
                                        store_modifier("Mode_switch", mode_bit);
@@ -220,10 +230,6 @@ static void x_reset_modifier_mapping(Display * display)
                                }
                        }
                }
-#undef store_modifier
-#undef check_modifier
-#undef modwarn
-#undef modbarf
 
        /* If there was no Meta key, then try using the Alt key instead.
           If there is both a Meta key and an Alt key, then the Alt key
@@ -238,7 +244,8 @@ static void x_reset_modifier_mapping(Display * display)
           not interpret it as Mode_switch; and interpreting it as both would
           be totally wrong. */
        if (mode_bit) {
-               const char *warn = 0;
+               const char *warn = NULL;
+
                if (mode_bit == meta_bit)
                        warn = "Meta", meta_bit = 0;
                else if (mode_bit == hyper_bit)
-- 
2.1.3


-- 
To unsubscribe, send mail to wmaker-dev-unsubscr...@lists.windowmaker.org.

Reply via email to