1 /*
2  * Guifications - The end all, be all, toaster popup plugin
3  * Copyright (C) 2003-2004 Gary Kramlich
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 /*
21   2005.07.22 Modified by Hong Jen Yee (PCMan)
22   This piece of code detecting working area is got from Guifications, a plug-in for Gaim.
23 */
24 
25 # include <gdk/gdk.h>
26 # include <gdk/gdkx.h>
27 # include <X11/Xlib.h>
28 # include <X11/Xutil.h>
29 # include <X11/Xatom.h>
30 
31 gboolean
gf_display_get_workarea(GdkRectangle * rect)32 gf_display_get_workarea(GdkRectangle *rect) {
33 	Atom xa_desktops, xa_current, xa_workarea, xa_type;
34 	Display *x_display;
35 	Window x_root;
36 	guint32 current = 0;
37 	gulong *workareas, len, fill;
38 	guchar *data;
39 	gint format;
40 
41 	GdkDisplay *g_display;
42 	GdkScreen *g_screen;
43 	Screen *x_screen;
44 
45 	/* get the gdk display */
46 	g_display = gdk_display_get_default();
47 	if(!g_display)
48 		return FALSE;
49 
50 	/* get the x display from the gdk display */
51 	x_display = gdk_x11_display_get_xdisplay(g_display);
52 	if(!x_display)
53 		return FALSE;
54 
55 	/* get the screen according to the prefs */
56 	g_screen = gdk_display_get_default_screen(g_display);
57 	if(!g_screen)
58 		return FALSE;
59 
60 	/* get the x screen from the gdk screen */
61 	x_screen = gdk_x11_screen_get_xscreen(g_screen);
62 	if(!x_screen)
63 		return FALSE;
64 
65 	/* get the root window from the screen */
66 	x_root = XRootWindowOfScreen(x_screen);
67 
68 	/* find the _NET_NUMBER_OF_DESKTOPS atom */
69 	xa_desktops = XInternAtom(x_display, "_NET_NUMBER_OF_DESKTOPS", True);
70 	if(xa_desktops == None)
71 		return FALSE;
72 
73 	/* get the number of desktops */
74 	if(XGetWindowProperty(x_display, x_root, xa_desktops, 0, 1, False,
75 						  XA_CARDINAL, &xa_type, &format, &len, &fill,
76 						  &data) != Success)
77 	{
78 		return FALSE;
79 	}
80 
81 	if(!data)
82 		return FALSE;
83 
84 	XFree(data);
85 
86 	/* find the _NET_CURRENT_DESKTOP atom */
87 	xa_current = XInternAtom(x_display, "_NET_CURRENT_DESKTOP", True);
88 	if(xa_current == None)
89 		return FALSE;
90 
91 	/* get the current desktop */
92 	if(XGetWindowProperty(x_display, x_root, xa_current, 0, 1, False,
93 						  XA_CARDINAL, &xa_type, &format, &len, &fill,
94 						  &data) != Success)
95 	{
96 		return FALSE;
97 	}
98 
99 	if(!data)
100 		return FALSE;
101 
102 	current = *(guint32 *)data;
103 	XFree(data);
104 
105 	/* find the _NET_WORKAREA atom */
106 	xa_workarea = XInternAtom(x_display, "_NET_WORKAREA", True);
107 	if(xa_workarea == None)
108 		return FALSE;
109 
110 	if(XGetWindowProperty(x_display, x_root, xa_workarea, 0, (glong)(4 * 32),
111 						  False, AnyPropertyType, &xa_type, &format, &len,
112 						  &fill, &data) != Success)
113 	{
114 		return FALSE;
115 	}
116 
117 	/* make sure the type and format are good */
118 	if(xa_type == None || format == 0)
119 		return FALSE;
120 
121 	/* make sure we don't have any leftovers */
122 	if(fill)
123 		return FALSE;
124 
125 	/* make sure len divides evenly by 4 */
126 	if(len % 4)
127 		return FALSE;
128 
129 	/* it's good, lets use it */
130 	workareas = (gulong *)(guint32 *)data;
131 
132 	rect->x = (guint32)workareas[current * 4];
133 	rect->y = (guint32)workareas[current * 4 + 1];
134 	rect->width = (guint32)workareas[current * 4 + 2];
135 	rect->height = (guint32)workareas[current * 4 + 3];
136 
137 	/* clean up our memory */
138 	XFree(data);
139 
140 	return TRUE;
141 }
142 
get_desktop_working_area(GdkRectangle * area)143 void get_desktop_working_area( GdkRectangle* area )
144 {
145 	if( !gf_display_get_workarea(area) )
146 	{
147 		area->x = 0;
148 		area->y = 0;
149 		area->width = gdk_screen_width();
150 		area->height = gdk_screen_height();
151 	}
152 }
153