move more display code into the display file/struct
authorDana Jansens <danakj@orodu.net>
Tue, 4 Mar 2008 21:25:47 +0000 (16:25 -0500)
committerDana Jansens <danakj@orodu.net>
Tue, 4 Mar 2008 21:25:47 +0000 (16:25 -0500)
dcompmgr.c
display.c
display.h
screen.c
screen.h

index 542f1ad..2c1c50c 100644 (file)
@@ -13,41 +13,6 @@ typedef struct {
     int foo;
 } d_options_t;
 
-static gint
-all_screens(d_display_t *dpy, d_screen_t **list)
-{
-    static const xcb_setup_t *setup;
-    xcb_screen_iterator_t it;
-    int count, i;
-    d_screen_t sc;
-
-    setup = xcb_get_setup(dpy->conn);
-
-    count = i = 0;
-    for (it = xcb_setup_roots_iterator(setup); it.rem; xcb_screen_next(&it)) {
-        sc.super = *it.data;
-        sc.dpy = dpy;
-        sc.num = i++;
-        if (screen_register(&sc)) {
-            ++count;
-            *list = realloc(*list, sizeof(d_screen_t)*count);
-            (*list)[count-1] = sc;
-            printf(_("managing screen %d\n"), sc.num);
-        }
-    }
-    return count;
-}
-
-static d_screen_t*
-screen_from_root(d_screen_t *list, int n, xcb_window_t root)
-{
-    int i;
-    for (i = 0; i < n; ++i)
-        if (list->super.root == root) return &list[i];
-    g_assert_not_reached();
-    return NULL;
-}
-
 static
 void read_options(int argc, char **argv, d_options_t *opts)
 {
@@ -58,8 +23,6 @@ int
 main(int argc, char **argv)
 {
     d_display_t         *dpy;
-    d_screen_t          *screens = NULL;
-    int                  nscreens;
     xcb_generic_event_t *ev;
     d_options_t          opts;
 
@@ -98,8 +61,7 @@ main(int argc, char **argv)
         return 1;
     }
 
-    nscreens = all_screens(dpy, &screens);
-    if (nscreens < 1) {
+    if (!display_claim_screens(dpy)) {
         printf(_("found no screens to run on\n"));
         display_unref(dpy);
         return 0;
@@ -121,7 +83,7 @@ main(int argc, char **argv)
             d_screen_t *sc;
 
             cev = (xcb_create_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, cev->parent);
+            sc = display_screen_from_root(dpy, cev->parent);
             if (!sc) break;
             screen_add_window(sc, cev->window);
             break;
@@ -133,7 +95,7 @@ main(int argc, char **argv)
             d_window_t *w;
 
             dev = (xcb_destroy_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, dev->event);
+            sc = display_screen_from_root(dpy, dev->event);
             if (!sc) break;
             w = screen_find_window(sc, dev->window);
             w->hide(w);
@@ -147,7 +109,7 @@ main(int argc, char **argv)
             d_window_t *w;
 
             rev = (xcb_reparent_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, rev->event);
+            sc = display_screen_from_root(dpy, rev->event);
             if (!sc) break;
             w = screen_find_window(sc, rev->window);
             if (rev->parent == sc->super.root)
@@ -165,7 +127,7 @@ main(int argc, char **argv)
             d_window_t *w;
 
             mev = (xcb_map_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, mev->event);
+            sc = display_screen_from_root(dpy, mev->event);
             if (!sc) break;
             w = screen_find_window(sc, mev->window);
             window_show(w);
@@ -178,7 +140,7 @@ main(int argc, char **argv)
             d_window_t *w;
 
             mev = (xcb_unmap_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, mev->event);
+            sc = display_screen_from_root(dpy, mev->event);
             if (!sc) break;
             w = screen_find_window(sc, mev->window);
             window_hide(w);
index 1bc3d80..5891fd4 100644 (file)
--- a/display.c
+++ b/display.c
@@ -1,4 +1,6 @@
 #include "display.h"
+#include "screen.h"
+#include "gettext.h"
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -171,6 +173,8 @@ display_open(const char *name)
         dpy = malloc(sizeof(d_display_t));
         dpy->conn = conn;
         dpy->ref = 1;
+        dpy->screens = NULL;
+        dpy->nscreens = 0;
 
         query_statics(dpy);
     }
@@ -188,6 +192,7 @@ display_unref(d_display_t *dpy)
 {
     if (dpy && --dpy->ref == 0) {
         xcb_disconnect(dpy->conn);
+        free(dpy->screens);
         free(dpy);
     }
 }
@@ -266,3 +271,40 @@ display_error(d_display_t *dpy, xcb_generic_error_t *ev)
 
     abort();
 }
+
+int
+display_claim_screens(d_display_t *dpy)
+{
+    static const xcb_setup_t *setup;
+    xcb_screen_iterator_t it;
+    int i;
+    d_screen_t sc;
+
+    setup = xcb_get_setup(dpy->conn);
+
+    i = 0;
+    for (it = xcb_setup_roots_iterator(setup); it.rem; xcb_screen_next(&it)) {
+        sc.super = *it.data;
+        sc.dpy = dpy;
+        sc.num = i++;
+        if (screen_register(dpy, i, &sc)) {
+            ++dpy->nscreens;
+            dpy->screens = realloc(dpy->screens,
+                                   sizeof(d_screen_t)*dpy->nscreens);
+            dpy->screens[dpy->nscreens-1] = sc;
+            printf(_("managing screen %d\n"), sc.num);
+        }
+    }
+    return dpy->nscreens;
+}
+
+d_screen_t*
+display_screen_from_root(d_display_t *dpy, xcb_window_t root)
+{
+    int i;
+    for (i = 0; i < dpy->nscreens; ++i)
+        if (dpy->screens[i].super.root == root) return &dpy->screens[i];
+    g_assert_not_reached();
+    return NULL;
+}
+
index 8551708..41bc4e0 100644 (file)
--- a/display.h
+++ b/display.h
@@ -17,6 +17,8 @@ typedef struct d_display_ext {
 typedef struct d_display {
     xcb_connection_t *conn;
     int               ref;
+    struct d_screen  *screens;
+    int               nscreens;
 
     d_display_ext_t xfixes;
     d_display_ext_t render;
@@ -54,6 +56,10 @@ d_display_t* display_open(const char *name);
 void display_ref(d_display_t *dpy);
 void display_unref(d_display_t *dpy);
 
+int display_claim_screens(d_display_t *dpy);
+
+struct d_screen* display_screen_from_root(d_display_t *dpy, xcb_window_t root);
+
 void display_error(d_display_t *dpy, xcb_generic_error_t *ev);
 
 #endif
index 533f07f..dd637d4 100644 (file)
--- a/screen.c
+++ b/screen.c
@@ -17,7 +17,7 @@ static void screen_init(d_screen_t *sc);
 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
 
 gboolean
-screen_register(d_screen_t *sc)
+screen_register(struct d_display *dpy, int num, d_screen_t *sc)
 {
     char *name;
     xcb_window_t w;
@@ -28,6 +28,8 @@ screen_register(d_screen_t *sc)
     uint32_t event_mask;
     gboolean taken, ret;
 
+    sc->dpy = dpy;
+    sc->num = num;
     w = xcb_generate_id(sc->dpy->conn);
     event_mask = SELECTION_MASK;
     xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
index 96b1ab1..0dbf0b9 100644 (file)
--- a/screen.h
+++ b/screen.h
@@ -22,7 +22,7 @@ typedef struct d_screen {
 /*! Tries to register on the screen given by @sc.  If it succeeds, it fills
     in @sc and returns TRUE, otherwise it returns FALSE.
 */
-gboolean screen_register(d_screen_t *sc);
+gboolean screen_register(struct d_display *dpy, int num, d_screen_t *sc);
 
 void screen_add_window(d_screen_t *sc, xcb_window_t wid);
 void screen_remove_window(d_screen_t *sc, struct d_window *w);