Code

gtk-tpdfv-screen: Added module to abstract access to different screens.
[tpdfview.git] / src / gtk-tpdfv-screen.c
diff --git a/src/gtk-tpdfv-screen.c b/src/gtk-tpdfv-screen.c
new file mode 100644 (file)
index 0000000..a118d79
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * tpdfview - src/gtk-tpdfv-screen.c
+ * Copyright (C) 2011 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * This module provides an abstraction layer for virtual screens.
+ */
+
+#include "gtk-tpdfv.h"
+
+#include <assert.h>
+#include <errno.h>
+
+#include <gtk/gtk.h>
+
+#include <stdlib.h>
+
+typedef struct {
+       GdkScreen *screen;
+       gint n_monitor;
+
+       gint x;
+       gint y;
+       gint width;
+       gint height;
+} screen_t;
+
+struct gtk_tpdfv_screens {
+       gint      num_screens;
+       screen_t *screens;
+};
+
+/*
+ * Private helper functions.
+ */
+
+
+/*
+ * Public API.
+ */
+
+gtk_tpdfv_screens_t *
+gtk_tpdfv_screens_init(const gchar *display_name)
+{
+       gtk_tpdfv_screens_t *screens;
+
+       GdkDisplay *gdk_disp;
+       gint screens_count;
+
+       gint i;
+
+       if (display_name)
+               gdk_disp = gdk_display_open(display_name);
+       else
+               gdk_disp = gdk_display_get_default();
+
+       if (! gdk_disp)
+               return NULL;
+
+       screens = (gtk_tpdfv_screens_t *)malloc(sizeof(*screens));
+       if (! screens)
+               return NULL;
+
+       screens->screens     = NULL;
+       screens->num_screens = 0;
+
+       screens_count = gdk_display_get_n_screens(gdk_disp);
+       for (i = 0; i < screens_count; ++i) {
+               GdkScreen *gdk_screen = gdk_display_get_screen(gdk_disp, i);
+
+               gint monitors_count = gdk_screen_get_n_monitors(gdk_screen);
+               gint j;
+
+               for (j = 0; j < monitors_count; ++j) {
+                       screen_t *screen;
+
+                       GdkRectangle geo;
+
+                       screen = (screen_t *)realloc(screens->screens,
+                                       (size_t)(screens->num_screens + 1) * sizeof(*screen));
+                       if (! screen) {
+                               if (screens->screens)
+                                       free(screens->screens);
+                               free(screens);
+                               return NULL;
+                       }
+
+                       screens->screens = screen;
+                       screen = screens->screens + screens->num_screens;
+                       ++screens->num_screens;
+
+                       screen->screen = gdk_screen;
+                       screen->n_monitor = j;
+
+                       gdk_screen_get_monitor_geometry(gdk_screen, j, &geo);
+                       screen->x = geo.x; screen->y = geo.y;
+                       screen->width = geo.width; screen->height = geo.height;
+               }
+       }
+       return screens;
+} /* gtk_tpdfv_new */
+
+void
+gtk_tpdfv_screens_destroy(gtk_tpdfv_screens_t *screens)
+{
+       if (! screens)
+               return;
+
+       if (screens->screens)
+               free(screens->screens);
+       free(screens);
+} /* gtk_tpdfv_destroy_screens */
+
+gint
+gtk_tpdfv_screens_number(const gtk_tpdfv_screens_t *screens)
+{
+       if (! screens)
+               return -1;
+       return screens->num_screens;
+} /* gtk_tpdfv_screens_number */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+