demo: tri: Add window destroy processing
authorCourtney Goeltzenleuchter <courtney@LunarG.com>
Fri, 7 Nov 2014 22:17:03 +0000 (15:17 -0700)
committerCourtney Goeltzenleuchter <courtney@LunarG.com>
Wed, 26 Nov 2014 17:03:11 +0000 (10:03 -0700)
demos/tri.c

index 8b1f85771de2508ca473c0fc3650b21cd58d4cbf..b6b6bdc37f83dc1b964771f3ff388192ea0e13bb 100644 (file)
@@ -76,6 +76,7 @@ struct demo {
     XGL_CMD_BUFFER cmd;
 
     xcb_window_t window;
+    xcb_intern_atom_reply_t *atom_wm_delete_window;
 
     bool quit;
     XGL_UINT current_buffer;
@@ -723,6 +724,12 @@ static void demo_handle_event(struct demo *demo,
     case XCB_EXPOSE:
         demo_draw(demo);
         break;
+    case XCB_CLIENT_MESSAGE:
+        if((*(xcb_client_message_event_t*)event).data.data32[0] ==
+           (*demo->atom_wm_delete_window).atom) {
+            demo->quit = true;
+        }
+        break;
     case XCB_KEY_RELEASE:
         {
             const xcb_key_release_event_t *key =
@@ -732,6 +739,9 @@ static void demo_handle_event(struct demo *demo,
                 demo->quit = true;
         }
         break;
+    case XCB_DESTROY_NOTIFY:
+        demo->quit = true;
+        break;
     default:
         break;
     }
@@ -761,7 +771,8 @@ static void demo_create_window(struct demo *demo)
     value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
     value_list[0] = demo->screen->black_pixel;
     value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
-                    XCB_EVENT_MASK_EXPOSURE;
+                    XCB_EVENT_MASK_EXPOSURE |
+                    XCB_EVENT_MASK_STRUCTURE_NOTIFY;
 
     xcb_create_window(demo->connection,
             XCB_COPY_FROM_PARENT,
@@ -771,6 +782,19 @@ static void demo_create_window(struct demo *demo)
             demo->screen->root_visual,
             value_mask, value_list);
 
+    /* Magic code that will send notification when window is destroyed */
+    xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
+                                                      "WM_PROTOCOLS");
+    xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
+
+    xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
+    demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
+
+    xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
+                        demo->window, (*reply).atom, 4, 32, 1,
+                        &(*demo->atom_wm_delete_window).atom);
+    free(reply);
+
     xcb_map_window(demo->connection, demo->window);
 }