1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
|
4 |
Support functions for Extended Window Manager Hints, |
5 |
http://www.freedesktop.org/standards/wm-spec.html |
6 |
|
7 |
Copyright (C) Matthew Chapman 1999-2002 |
8 |
Copyright (C) Peter Astrand <peter@cendio.se> 2003 |
9 |
|
10 |
This program is free software; you can redistribute it and/or modify |
11 |
it under the terms of the GNU General Public License as published by |
12 |
the Free Software Foundation; either version 2 of the License, or |
13 |
(at your option) any later version. |
14 |
|
15 |
This program is distributed in the hope that it will be useful, |
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
GNU General Public License for more details. |
19 |
|
20 |
You should have received a copy of the GNU General Public License |
21 |
along with this program; if not, write to the Free Software |
22 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
23 |
*/ |
24 |
|
25 |
#include <X11/Xlib.h> |
26 |
#include "rdesktop.h" |
27 |
|
28 |
extern Display *g_display; |
29 |
|
30 |
/* |
31 |
Get window property value (32 bit format) |
32 |
Returns zero on success, -1 on error |
33 |
*/ |
34 |
static int |
35 |
get_property_value(char *propname, long max_length, |
36 |
unsigned long *nitems_return, unsigned char **prop_return) |
37 |
{ |
38 |
int result; |
39 |
Atom property; |
40 |
Atom actual_type_return; |
41 |
int actual_format_return; |
42 |
unsigned long bytes_after_return; |
43 |
|
44 |
property = XInternAtom(g_display, propname, True); |
45 |
if (property == None) |
46 |
{ |
47 |
fprintf(stderr, "Atom %s does not exist\n", propname); |
48 |
return (-1); |
49 |
} |
50 |
|
51 |
result = XGetWindowProperty(g_display, DefaultRootWindow(g_display), property, 0, /* long_offset */ |
52 |
max_length, /* long_length */ |
53 |
False, /* delete */ |
54 |
AnyPropertyType, /* req_type */ |
55 |
&actual_type_return, |
56 |
&actual_format_return, |
57 |
nitems_return, &bytes_after_return, prop_return); |
58 |
|
59 |
if (result != Success) |
60 |
{ |
61 |
fprintf(stderr, "XGetWindowProperty failed\n"); |
62 |
return (-1); |
63 |
} |
64 |
|
65 |
if (actual_type_return == None || actual_format_return == 0) |
66 |
{ |
67 |
fprintf(stderr, "Root window is missing property %s\n", propname); |
68 |
return (-1); |
69 |
} |
70 |
|
71 |
if (bytes_after_return) |
72 |
{ |
73 |
fprintf(stderr, "%s is too big for me\n", propname); |
74 |
return (-1); |
75 |
} |
76 |
|
77 |
if (actual_format_return != 32) |
78 |
{ |
79 |
fprintf(stderr, "%s has bad format\n", propname); |
80 |
return (-1); |
81 |
} |
82 |
|
83 |
return (0); |
84 |
} |
85 |
|
86 |
/* |
87 |
Get current desktop number |
88 |
Returns -1 on error |
89 |
*/ |
90 |
static int |
91 |
get_current_desktop(void) |
92 |
{ |
93 |
unsigned long nitems_return; |
94 |
unsigned char *prop_return; |
95 |
int current_desktop; |
96 |
|
97 |
if (get_property_value("_NET_CURRENT_DESKTOP", 1, &nitems_return, |
98 |
&prop_return) < 0) |
99 |
return (-1); |
100 |
|
101 |
if (nitems_return != 1) |
102 |
{ |
103 |
fprintf(stderr, "_NET_CURRENT_DESKTOP has bad length\n"); |
104 |
return (-1); |
105 |
} |
106 |
|
107 |
current_desktop = *prop_return; |
108 |
XFree(prop_return); |
109 |
return current_desktop; |
110 |
} |
111 |
|
112 |
/* |
113 |
Get workarea geometry |
114 |
Returns zero on success, -1 on error |
115 |
*/ |
116 |
|
117 |
int |
118 |
get_current_workarea(uint32 * x, uint32 * y, uint32 * width, uint32 * height) |
119 |
{ |
120 |
int current_desktop; |
121 |
unsigned long nitems_return; |
122 |
unsigned char *prop_return; |
123 |
uint32 *return_words; |
124 |
const uint32 net_workarea_x_offset = 0; |
125 |
const uint32 net_workarea_y_offset = 1; |
126 |
const uint32 net_workarea_width_offset = 2; |
127 |
const uint32 net_workarea_height_offset = 3; |
128 |
const uint32 max_prop_length = 32 * 4; /* Max 32 desktops */ |
129 |
|
130 |
if (get_property_value("_NET_WORKAREA", max_prop_length, &nitems_return, |
131 |
&prop_return) < 0) |
132 |
return (-1); |
133 |
|
134 |
if (nitems_return % 4) |
135 |
{ |
136 |
fprintf(stderr, "_NET_WORKAREA has odd length\n"); |
137 |
return (-1); |
138 |
} |
139 |
|
140 |
current_desktop = get_current_desktop(); |
141 |
|
142 |
if (current_desktop < 0) |
143 |
return -1; |
144 |
|
145 |
return_words = (uint32 *)prop_return; |
146 |
|
147 |
*x = return_words[current_desktop * 4 + net_workarea_x_offset]; |
148 |
*y = return_words[current_desktop * 4 + net_workarea_y_offset]; |
149 |
*width = return_words[current_desktop * 4 + net_workarea_width_offset]; |
150 |
*height = return_words[current_desktop * 4 + net_workarea_height_offset]; |
151 |
|
152 |
XFree(prop_return); |
153 |
|
154 |
return (0); |
155 |
|
156 |
} |