Merge branch 'master' of git+ssh://git.freedesktop.org/git/xcb
[free-sw/xcb/demo] / neko / xcbneko.c
index 5eff1ce..f254e2b 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;
 
@@ -140,14 +141,14 @@ char        *fgColor, *bgColor;
 
 static char  *ProgramName;
 
-/*Display        *theDisplay;*/
 XCBConnection  *xc;
 XCBSCREEN      *theScreen;             /* instead of macro(theDisplay, int theScreen) */
-unsigned int   theDepth;
 unsigned long  theBlackPixel;
 unsigned long  theWhitePixel;
 XCBWINDOW         theWindow;
 XCBCURSOR         theCursor;
+XCBKeySymbols  *theKeySyms;
+XCBATOM deleteWindowAtom;
 
 static unsigned int  WindowWidth;
 static unsigned int  WindowHeight;
@@ -422,17 +423,23 @@ XCBPIXMAP CreatePixmapFromBitmapData( XCBConnection *c,
   /*  must swap and pad the data if bit/byte_order isn't LSB (Mac) */
   
   /* Mac X Server: byte_order=bit_order=MSB, unit=32, padding=32 */
-  long bufLen = (w+7)/8*h;
+  long bpl = (w+7)/8;
+  long pad = XCBGetSetup(c)->bitmap_format_scanline_pad;
+  long bpd = ROUNDUP(w, pad)>>3;
+  long bufLen = bpd * h;
   BYTE buf[1024];
   if (XCBGetSetup(c)->bitmap_format_scanline_unit == 32 &&
       XCBGetSetup(c)->bitmap_format_bit_order == XCBImageOrderMSBFirst &&
       XCBGetSetup(c)->image_byte_order == XCBImageOrderMSBFirst)
   {
-    long bpl = (w+7)/8;
-    long pad = XCBGetSetup(c)->bitmap_format_scanline_pad;
-    long bpd = ROUNDUP(w, pad)>>3;
-    SwapBits((unsigned char *)data, (unsigned char *)buf, bpl, bpl, bpd, h);
-    bufLen = bpd * h;
+    SwapBits((unsigned char *)data, buf, bpl, bpl, bpd, h);
+  }
+  else if (bpl != bpd)
+  {
+    int i;
+    BYTE *src = (BYTE *)data, *dest = buf;
+    for (i=0; i<h; i++, dest += bpd, src += bpl)
+      memcpy(dest, src, bpl);
   }
   else
     memcpy(buf, data, bufLen);
@@ -509,7 +516,19 @@ void  InitBitmapAndGCs(void) {
   
   /* later: XCBFreePixmap( c, bitmap ); */
   /* later: XCBFreeGC( c, gc ); */
-  XCBSync( xc, NULL );
+}
+
+XCBATOM
+GetAtom(XCBConnection *c, const char *atomName)
+{
+       XCBATOM atom = { XCBNone };
+       XCBInternAtomRep *r = XCBInternAtomReply(c,
+               XCBInternAtom(c, 0, strlen(atomName), atomName), NULL);
+       if (r) {
+               atom = r->atom;
+               free(r);
+       }
+       return atom;
 }
 
 void
@@ -517,6 +536,7 @@ InitScreen( char *DisplayName, char *theGeometry, char *theTitle, Bool iconicSta
 {
   XCBPIXMAP            theCursorSource;
   XCBPIXMAP            theCursorMask;
+  unsigned int   theDepth;
   XCBCOLORMAP          theColormap;
   int screen;
   
@@ -628,19 +648,13 @@ InitScreen( char *DisplayName, char *theGeometry, char *theTitle, Bool iconicSta
                theScreen->root_visual, /* CopyFromParent */
                theWindowMask, theWindowAttributes );
 
+       /* new: obey the window-delete protocol, look for XCBClientMessage */
+       deleteWindowAtom = GetAtom(xc, "WM_DELETE_WINDOW");
+       SetWMProtocols( xc, theWindow, 1, &deleteWindowAtom );
+
        theIconPixmap = CreateBitmapFromData( xc, theWindow,
                                                                                  icon_bits, icon_width, icon_height );
-       
 #ifdef TODO_ICCCM
-    /* Um... there is no function to send the hints...
-       WMHints              theWMHints;
-       WMHintsSetIconPixmap( &theHints, theIconPixmap );
-
-       if ( iconicState )
-         WMHintsSetIconic( &theHints );
-       else
-         WMHintsSetNormal( &theHints );
-    */
        theWMHints.icon_pixmap = theIconPixmap;
        
        if ( iconicState )
@@ -651,36 +665,37 @@ InitScreen( char *DisplayName, char *theGeometry, char *theTitle, Bool iconicSta
        theWMHints.flags = IconPixmapHint | StateHint;
        
        XSetWMHints( theDisplay, theWindow, &theWMHints );
-#endif
+#else
+    /* Um... there is no function to send the hints...
+       WMHints              theWMHints;
+       WMHintsSetIconPixmap( &theHints, theIconPixmap );
 
-#ifdef TODO_ICCCM
-       /*
-       SizeHints *hints = AllocSizeHints();
-       SizeHintsSetPosition(hints, WindowPointX, WindowPointY);
-       SizeHintsSetSize(hints, WindowWidth, WindowHeight);
-       SetWMNormalHints(xc, theWindow, hints);
-       FreeSizeHints(hints);
-       */
-       XSizeHints            theSizeHints;
-
-       theSizeHints.flags = PPosition | PSize;
-       theSizeHints.x = WindowPointX;
-       theSizeHints.y = WindowPointY;
-       theSizeHints.width = WindowWidth;
-       theSizeHints.height = WindowHeight;
-       
-       XSetNormalHints( theDisplay, theWindow, &theSizeHints );
+       if ( iconicState )
+         WMHintsSetIconic( &theHints );
+       else
+         WMHintsSetNormal( &theHints );
+         
+       Now what?
+    */
 #endif
 
+       /* why hide the structure? */
+       SizeHints *theSizeHints = AllocSizeHints();
+
+       /* need enum for second param (user specified) */
+       SizeHintsSetPosition(theSizeHints, 0, WindowPointX, WindowPointY);
+       SizeHintsSetSize(theSizeHints, 0, WindowWidth, WindowHeight);
+
+       SetWMNormalHints(xc, theWindow, theSizeHints);
+
+       FreeSizeHints(theSizeHints);
+
        /* Um, why do I have to specify the encoding in this API? */
        SetWMName( xc, theWindow, STRING, strlen(theTitle), theTitle );
        SetWMIconName( xc, theWindow, STRING, strlen(theTitle), theTitle );
 
        XCBMapWindow( xc, theWindow );
 
-    /* moved to the CreateWindow attribute list */
-       /* XSelectInput( theDisplay, theWindow, EVENT_MASK ); */
-
   }
   
 #ifdef TODO
@@ -697,7 +712,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 +765,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 +788,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 +803,7 @@ void  RedrawNeko(void) {
 
   XCBPolyFillRectangle( xc, drawable, NekoLastGC, 1, &rect );
 
-  XCBSync( xc, NULL );
+  XCBFlush( xc );
 }
 
 
@@ -1076,36 +1097,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 ) {
@@ -1166,6 +1168,11 @@ void  NekoAdjust(void) {
        NekoY = WindowHeight - BITMAP_HEIGHT;
 }
 
+int IsDeleteMessage(XCBClientMessageEvent *msg)
+{
+       return msg->data.data32[0] == deleteWindowAtom.xid;
+}
+
 Bool  ProcessEvent(void) {
   XCBGenericEvent *theEvent;
   XCBConfigureNotifyEvent *theConfigureNotification;
@@ -1176,8 +1183,9 @@ Bool  ProcessEvent(void) {
   
   switch ( EventState ) {
   case NORMAL_STATE:
-    while ( NULL != (theEvent = XCBPollForEvent( xc, &error )) ) {  /*while ( XCheckMaskEvent( theDisplay, EVENT_MASK, &theEvent ) ) {*/
-         switch ( theEvent->response_type ) {
+    while ( ContinueState &&
+            NULL != (theEvent = XCBPollForEvent( xc, &error )) ) {  /*while ( XCheckMaskEvent( theDisplay, EVENT_MASK, &theEvent ) ) {*/
+         switch ( theEvent->response_type & 0x7f ) {
          case XCBConfigureNotify:
            theConfigureNotification = (XCBConfigureNotifyEvent *)theEvent;
                WindowWidth = theConfigureNotification->width;
@@ -1197,26 +1205,24 @@ Bool  ProcessEvent(void) {
                break;
          case XCBKeyPress:
                ContinueState = ProcessKeyPress( (XCBKeyPressEvent *)theEvent );
-               if ( !ContinueState ) {
-                 free(theEvent);
-                 return( ContinueState );
-               }
                break;
          case XCBButtonPress:
            theButtonPress = (XCBButtonPressEvent *)theEvent;
-               if ( theButtonPress->detail.id == 3 ) { /* xbutton.button */
-                 free(theEvent);
-                 return( False );
-               }
+               ContinueState = ( theButtonPress->detail.id != 3 );     /* xbutton.button */
                break;
+         /* new: handle ClientMessage */
+         case XCBClientMessage:
+           ContinueState = !IsDeleteMessage((XCBClientMessageEvent *)theEvent);
+           break;
          default:
                /* Unknown Event */
+               /*printf("event type:%x\n", (int)theEvent->response_type);*/
                break;
          }
          free(theEvent);
          if (error != 0)
                return False;
-       }
+       } /* end while */
        break;
 #ifdef DEBUG
   case DEBUG_LIST: