Detect Alt-Q to quit using xcb_keysyms.h library.
[free-sw/xcb/demo] / neko / xcbneko.c
index 5eff1ce..bd7b706 100644 (file)
@@ -27,6 +27,7 @@
 #include <X11/XCB/xcb_aux.h>           /* XCBAuxGetScreen */
 #include <X11/XCB/xcb_icccm.h>
 #include <X11/XCB/xcb_atom.h>          /* STRING atom */
+#include <X11/XCB/xcb_keysyms.h>
 
 typedef enum { False, True } Bool;
 
@@ -148,6 +149,7 @@ unsigned long  theBlackPixel;
 unsigned long  theWhitePixel;
 XCBWINDOW         theWindow;
 XCBCURSOR         theCursor;
+XCBKeySymbols  *theKeySyms;
 
 static unsigned int  WindowWidth;
 static unsigned int  WindowHeight;
@@ -509,7 +511,6 @@ void  InitBitmapAndGCs(void) {
   
   /* later: XCBFreePixmap( c, bitmap ); */
   /* later: XCBFreeGC( c, gc ); */
-  XCBSync( xc, NULL );
 }
 
 void
@@ -697,7 +698,13 @@ InitScreen( char *DisplayName, char *theGeometry, char *theTitle, Bool iconicSta
 
   InitBitmapAndGCs();
 
-  XCBSync(xc, NULL);
+  XCBFlush(xc);
+
+  /* latency: ask for keysyms now, and receive them later */
+  theKeySyms = XCBKeySymbolsAlloc( xc );
+
+  /* later: XCBKeySymbolsFree( keysyms ); */
+  /* later: XCBRefreshKeyboardMapping ( keysyms, mappingEvent ); */
 }
 
 
@@ -744,7 +751,7 @@ SetNekoState( int SetValue )
 }
 
 /* FillRct.c */
-/*   Xlib does merging of requests, but the Sync and frequent DrawGC changes
+/*   Xlib does merging of requests, but the Flush and frequent DrawGC changes
      defeat this mechanism */
 
 void
@@ -767,7 +774,7 @@ DrawNeko( int x, int y, XCBGCONTEXT DrawGC )
 
   XCBPolyFillRectangle( xc, drawable, DrawGC, 1, &rect );
 
-  XCBSync( xc, NULL );
+  XCBFlush( xc );
 
   NekoLastX = x;
   NekoLastY = y;
@@ -782,7 +789,7 @@ void  RedrawNeko(void) {
 
   XCBPolyFillRectangle( xc, drawable, NekoLastGC, 1, &rect );
 
-  XCBSync( xc, NULL );
+  XCBFlush( xc );
 }
 
 
@@ -1076,36 +1083,17 @@ void  DisplayCharacters() {
 Bool
 ProcessKeyPress( XCBKeyPressEvent *theKeyEvent )
 {
-#if TODO    
   Bool ReturnState = True;
 
-  int                  Length;
-  char         theKeyBuffer[ AVAIL_KEYBUF + 1 ];
-    int                        theKeyBufferMaxLen = AVAIL_KEYBUF;
-  KeySym               theKeySym;
-  XComposeStatus       theComposeStatus;
-
-  Length = XLookupString( theKeyEvent,
-                                                theKeyBuffer, theKeyBufferMaxLen,
-                                                &theKeySym, &theComposeStatus );
-
-  if ( Length > 0 ) {
-       switch ( theKeyBuffer[ 0 ] ) {
-       case 'q':
-       case 'Q':
-         if ( theKeyEvent->state & XCBModMask1 ) {     /* META (Alt) %-!< */
-               ReturnState = False;
-         }
-         break;
-       default:
-         break;
-       }
-  }
-#else
-  /* quit on any key */
-  Bool ReturnState = False;
-#endif
+  /* quit on Meta-Q (Alt-Q) */
+  XCBKEYSYM theKeySym;
+
+  /* last param is "int col". What? add enumeration to xcb_keysyms.h */
+  theKeySym = XCBKeyPressLookupKeysym( theKeySyms, theKeyEvent, 1 );
 
+  /* KeySym XK_Q == 'Q' */
+  if (theKeySym.id == 'Q' && (theKeyEvent->state & XCBModMask1))
+    ReturnState = False;
 
 #ifdef DEBUG
   if ( EventState == DEBUG_MOVE ) {