From db746cb97ac89be5bce083d016481bd51c9bb29c Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 22 Dec 2011 23:17:22 +0100 Subject: [PATCH] gtk-tpdfv-screen: Added module to abstract access to different screens. For now, it provides a function to determine a list of all screens (independent of actual GdkScreen and monitor number). --- src/Makefile.am | 2 +- src/gtk-tpdfv-screen.c | 146 +++++++++++++++++++++++++++++++++++++++++ src/gtk-tpdfv.h | 23 +++++++ src/tpdfview.c | 8 +++ 4 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/gtk-tpdfv-screen.c diff --git a/src/Makefile.am b/src/Makefile.am index 3615d5d..5700b90 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,7 +6,7 @@ lib_LTLIBRARIES = libtpdfv.la BUILT_SOURCES = tpdfv_features.h libtpdfv_la_SOURCES = tpdfv.c tpdfv.h tpdfv_features.h \ - gtk-tpdfv.c gtk-tpdfv.h + gtk-tpdfv.c gtk-tpdfv-screen.c gtk-tpdfv.h libtpdfv_la_CFLAGS = $(AM_CFLAGS) \ @CAIRO_CFLAGS@ \ @GTK2_CFLAGS@ \ diff --git a/src/gtk-tpdfv-screen.c b/src/gtk-tpdfv-screen.c new file mode 100644 index 0000000..a118d79 --- /dev/null +++ b/src/gtk-tpdfv-screen.c @@ -0,0 +1,146 @@ +/* + * tpdfview - src/gtk-tpdfv-screen.c + * Copyright (C) 2011 Sebastian 'tokkee' Harl + * 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 +#include + +#include + +#include + +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 : */ + diff --git a/src/gtk-tpdfv.h b/src/gtk-tpdfv.h index f97224c..44ffd71 100644 --- a/src/gtk-tpdfv.h +++ b/src/gtk-tpdfv.h @@ -118,6 +118,29 @@ gtk_tpdfv_scroll_left(GtkWidget *widget); void gtk_tpdfv_scroll_right(GtkWidget *widget); +/* + * Screen handling. + */ + +struct gtk_tpdfv_screens; +typedef struct gtk_tpdfv_screens gtk_tpdfv_screens_t; + +/* + * gtk_tpdfv_get_screens: + * Get a list of available screens. + */ +gtk_tpdfv_screens_t * +gtk_tpdfv_screens_init(const gchar *display_name); +void +gtk_tpdfv_screens_destroy(gtk_tpdfv_screens_t *screens); + +/* + * gtk_tpdfv_screens_number: + * Get the number of screens. + */ +gint +gtk_tpdfv_screens_number(const gtk_tpdfv_screens_t *screens); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/tpdfview.c b/src/tpdfview.c index b98332d..b15bad3 100644 --- a/src/tpdfview.c +++ b/src/tpdfview.c @@ -188,6 +188,8 @@ key_press(GtkWidget *window, GdkEventKey *event, gpointer data) int main(int argc, char **argv) { + gtk_tpdfv_screens_t *screens; + GtkWidget *win = NULL; GtkWidget *tpdfv = NULL; @@ -232,6 +234,10 @@ main(int argc, char **argv) if (! win) return 1; + screens = gtk_tpdfv_screens_init(/* display = */ NULL); + if (! screens) + return 1; + snprintf(win_title, sizeof(win_title), "tpdfview: %s", basename(filename)); gtk_window_set_title(GTK_WINDOW(win), win_title); @@ -250,6 +256,8 @@ main(int argc, char **argv) gtk_widget_show_all(win); gtk_main(); + + gtk_tpdfv_screens_destroy(screens); return 0; } /* main */ -- 2.30.2