]> git.tdb.fi Git - geometrycompositor.git/commitdiff
Refresh the screen only when there's no events to process
authorMikko Rasa <tdb@tdb.fi>
Fri, 11 Dec 2015 16:09:12 +0000 (18:09 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 11 Dec 2015 16:32:41 +0000 (18:32 +0200)
source/main.c

index 35328543cbbb4cfc80329c8a531870ab6690aaa0..3f788848c119c6b230185a3878652566dd7214fd 100644 (file)
@@ -42,6 +42,7 @@ typedef struct CompositedScreen
        CompositedWindow *windows;
        unsigned nwindows;
        unsigned windows_capacity;
+       int dirty;
 } CompositedScreen;
 
 typedef struct Compositor
@@ -53,6 +54,7 @@ typedef struct Compositor
        PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
        PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT;
        PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT;
+       int dirty;
 } Compositor;
 
 static const char *vshader =
@@ -303,6 +305,18 @@ CompositedScreen *find_screen_by_root(Compositor *compositor, Window root)
        return NULL;
 }
 
+CompositedScreen *find_screen_by_window(Compositor *compositor, Window w)
+{
+       unsigned i, j;
+
+       for(i=0; i<compositor->nscreens; ++i)
+               for(j=0; j<compositor->screens[i].nwindows; ++j)
+                       if(compositor->screens[i].windows[j].window==w)
+                               return &compositor->screens[i];
+
+       return NULL;
+}
+
 int initialize_screen(Compositor *compositor, unsigned number)
 {
        CompositedScreen *screen;
@@ -456,10 +470,27 @@ void process_unmap_event(Compositor *compositor, XUnmapEvent *event)
                window->map_state = IsUnviewable;
 }
 
-void process_event(Compositor *compositor)
+void process_damage_event(Compositor *compositor, XDamageNotifyEvent *event)
+{
+       CompositedScreen *screen = find_screen_by_window(compositor, event->drawable);
+       if(!screen)
+               return;
+
+       screen->dirty = 1;
+       compositor->dirty = 1;
+}
+
+int process_event(Compositor *compositor)
 {
        XEvent event;
-       XNextEvent(compositor->display, &event);
+       if(compositor->dirty)
+       {
+               if(!XCheckMaskEvent(compositor->display, -1, &event))
+                       return 0;
+       }
+       else
+               XNextEvent(compositor->display, &event);
+
        switch(event.type)
        {
        case CreateNotify:
@@ -472,9 +503,13 @@ void process_event(Compositor *compositor)
                process_unmap_event(compositor, &event.xunmap);
                break;
        default:
-               if(event.type!=compositor->damage_event+XDamageNotify)
+               if(event.type==compositor->damage_event+XDamageNotify)
+                       process_damage_event(compositor, (XDamageNotifyEvent *)&event);
+               else
                        printf("Event %d\n", event.type);
        }
+
+       return 1;
 }
 
 void refresh_screens(Compositor *compositor)
@@ -508,7 +543,11 @@ void refresh_screens(Compositor *compositor)
                }
                XUngrabServer(compositor->display);
                glXSwapBuffers(compositor->display, screen->glx_window);
+
+               screen->dirty = 0;
        }
+
+       compositor->dirty = 0;
 }
 
 int main()
@@ -520,8 +559,8 @@ int main()
 
        while(1)
        {
-               process_event(&compositor);
-               refresh_screens(&compositor);
+               if(!process_event(&compositor))
+                       refresh_screens(&compositor);
        }
 
        shutdown_compositor(&compositor);