multiscreen-stuff.c 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *   multiscreen-stuff: Xinerama (and in the future multidisplay)
 *   support for the panel
 *
 *   Copyright (C) 2001 George Lebl <jirka@5z.com>
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <config.h>

25 26 27 28 29 30 31
#include <X11/X.h>
#include <X11/Xlib.h>
#include <gdk/gdkx.h>
#ifdef HAVE_LIBXINERAMA
#include <X11/extensions/Xinerama.h>
#endif

32 33
#include "multiscreen-stuff.h"

Mark McLoughlin's avatar
Mark McLoughlin committed
34 35 36
#include "foobar-widget.h"
#include "basep-widget.h"

37 38 39 40 41 42 43 44
/* some globals */
static int		screens = 1;
static GdkRectangle *	rectangles = NULL;
static gboolean		initialized = FALSE;

void
multiscreen_init (void)
{
45 46 47 48
#ifdef HAVE_LIBXINERAMA
	gboolean have_xinerama = FALSE;
#endif

49 50 51
	if (initialized)
		return;

52 53 54 55 56 57 58 59 60 61
	if (g_getenv ("FAKE_XINERAMA_PANEL") != NULL) {
		/* fake xinerama setup for debugging */
		screens = 2;
		rectangles = g_new0 (GdkRectangle, 2);
		rectangles[1].x = 0;
		rectangles[1].y = 0;
		rectangles[1].width = gdk_screen_width () / 2;
		rectangles[1].height = gdk_screen_height () / 2;
		rectangles[0].x = gdk_screen_width () / 2;
		rectangles[0].y = gdk_screen_height () / 2;
62 63
		rectangles[0].width = gdk_screen_width () - rectangles[0].x;
		rectangles[0].height = gdk_screen_height () - rectangles[0].y;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

		initialized = TRUE;

		return;
	}

#ifdef HAVE_LIBXINERAMA
	gdk_flush ();
	gdk_error_trap_push ();
	have_xinerama = XineramaIsActive (GDK_DISPLAY ());
	gdk_flush ();
	if (gdk_error_trap_pop () != 0)
		have_xinerama = FALSE;

	if (have_xinerama) {
		int screen_num, i;
		XineramaScreenInfo *xscreens =
			XineramaQueryScreens (GDK_DISPLAY (),
					      &screen_num);


		if (screen_num <= 0) {
			/* EEEEEK!, should never happen */
			goto no_xinerama;
		}

		rectangles = g_new0 (GdkRectangle, screen_num);
		screens = screen_num;

		for (i = 0; i < screen_num; i++) {
			rectangles[i].x = xscreens[i].x_org;
			rectangles[i].y = xscreens[i].y_org;
			rectangles[i].width = xscreens[i].width;
			rectangles[i].height = xscreens[i].height;
		}

		XFree (xscreens);
	} else
#endif
	{
#ifdef HAVE_LIBXINERAMA
no_xinerama:
#endif
107 108 109 110 111 112 113 114
		/* no xinerama */
		screens = 1;
		rectangles = g_new0 (GdkRectangle, 1);
		rectangles[0].x = 0;
		rectangles[0].y = 0;
		rectangles[0].width = gdk_screen_width ();
		rectangles[0].height = gdk_screen_height ();
	}
115 116 117 118 119 120 121

	initialized = TRUE;
}

int
multiscreen_screens (void)
{
122
	g_return_val_if_fail (initialized, 1);
123 124 125 126 127 128 129 130

	return screens;
}

int
multiscreen_x (int screen)
{
	g_return_val_if_fail (initialized, 0);
131
	g_return_val_if_fail (screen >= 0, 0);
132

133 134 135 136
	if (screen < screens)
		return rectangles[screen].x;
	else
		return gdk_screen_width () + 10;
137 138 139 140 141 142
}

int
multiscreen_y (int screen)
{
	g_return_val_if_fail (initialized, 0);
143
	g_return_val_if_fail (screen >= 0, 0);
144

145 146 147 148
	if (screen < screens)
		return rectangles[screen].y;
	else
		return gdk_screen_height () + 10;
149 150 151 152 153 154
}

int
multiscreen_width (int screen)
{
	g_return_val_if_fail (initialized, 0);
155
	g_return_val_if_fail (screen >= 0, 0);
156

157 158 159 160
	if (screen < screens)
		return rectangles[screen].width;
	else
		return gdk_screen_width ();
161 162 163 164 165 166
}

int
multiscreen_height (int screen)
{
	g_return_val_if_fail (initialized, 0);
167
	g_return_val_if_fail (screen >= 0, 0);
168

169 170 171 172
	if (screen < screens)
		return rectangles[screen].height;
	else
		return gdk_screen_height ();
173
}
174 175 176 177 178 179 180 181 182 183 184 185 186 187

int
multiscreen_screen_from_pos (int x, int y)
{
	int i;
	for (i = 0; i < screens; i++) {
		if (x >= rectangles[i].x &&
		    x <= rectangles[i].x + rectangles[i].width &&
		    y >= rectangles[i].y &&
		    y <= rectangles[i].y + rectangles[i].height)
			return i;
	}
	return -1;
}
188 189 190 191 192

int
multiscreen_screen_from_panel (GtkWidget *widget)
{
	g_return_val_if_fail (widget != NULL, 0);
193
	g_return_val_if_fail (BASEP_IS_WIDGET (widget) ||
194
			      FOOBAR_IS_WIDGET (widget), 0);
195
	
196
	if (BASEP_IS_WIDGET (widget))
197
		return BASEP_WIDGET (widget)->screen;
198
	else if (FOOBAR_IS_WIDGET (widget))
199 200 201 202
		return FOOBAR_WIDGET (widget)->screen;
	else
		return 0;
}