VirtualBox

source: vbox/trunk/src/VBox/RDP/client/ewmhints.c@ 36555

Last change on this file since 36555 was 33656, checked in by vboxsync, 14 years ago

*: rebrand Sun (L)GPL disclaimers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3
4 Support functions for Extended Window Manager Hints,
5 http://www.freedesktop.org/wiki/Standards_2fwm_2dspec
6
7 Copyright 2005 Peter Astrand <astrand@cendio.se> for Cendio AB
8 Copyright 2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
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/*
26 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
27 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
28 * the General Public License version 2 (GPLv2) at this time for any software where
29 * a choice of GPL license versions is made available with the language indicating
30 * that GPLv2 or any later version may be used, or where a choice of which version
31 * of the GPL is applied is otherwise unspecified.
32 */
33
34#include <X11/Xlib.h>
35#include <X11/Xatom.h>
36#include <X11/Xutil.h>
37#include "rdesktop.h"
38
39#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
40#define _NET_WM_STATE_ADD 1 /* add/set property */
41#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
42
43extern Display *g_display;
44
45static Atom g_net_wm_state_maximized_vert_atom, g_net_wm_state_maximized_horz_atom,
46 g_net_wm_state_hidden_atom, g_net_wm_name_atom, g_utf8_string_atom,
47 g_net_wm_state_skip_taskbar_atom, g_net_wm_state_skip_pager_atom,
48 g_net_wm_state_modal_atom, g_net_wm_icon_atom, g_net_wm_state_above_atom;
49
50Atom g_net_wm_state_atom, g_net_wm_desktop_atom;
51
52/*
53 Get window property value (32 bit format)
54 Returns zero on success, -1 on error
55*/
56static int
57get_property_value(Window wnd, char *propname, long max_length,
58 unsigned long *nitems_return, unsigned char **prop_return, int nowarn)
59{
60 int result;
61 Atom property;
62 Atom actual_type_return;
63 int actual_format_return;
64 unsigned long bytes_after_return;
65
66 property = XInternAtom(g_display, propname, True);
67 if (property == None)
68 {
69 fprintf(stderr, "Atom %s does not exist\n", propname);
70 return (-1);
71 }
72
73 result = XGetWindowProperty(g_display, wnd, property, 0, /* long_offset */
74 max_length, /* long_length */
75 False, /* delete */
76 AnyPropertyType, /* req_type */
77 &actual_type_return,
78 &actual_format_return,
79 nitems_return, &bytes_after_return, prop_return);
80
81 if (result != Success)
82 {
83 fprintf(stderr, "XGetWindowProperty failed\n");
84 return (-1);
85 }
86
87 if (actual_type_return == None || actual_format_return == 0)
88 {
89 if (!nowarn)
90 fprintf(stderr, "Window is missing property %s\n", propname);
91 return (-1);
92 }
93
94 if (bytes_after_return)
95 {
96 fprintf(stderr, "%s is too big for me\n", propname);
97 return (-1);
98 }
99
100 if (actual_format_return != 32)
101 {
102 fprintf(stderr, "%s has bad format\n", propname);
103 return (-1);
104 }
105
106 return (0);
107}
108
109/*
110 Get current desktop number
111 Returns -1 on error
112*/
113static int
114get_current_desktop(void)
115{
116 unsigned long nitems_return;
117 unsigned char *prop_return;
118 int current_desktop;
119
120 if (get_property_value
121 (DefaultRootWindow(g_display), "_NET_CURRENT_DESKTOP", 1, &nitems_return,
122 &prop_return, 0) < 0)
123 return (-1);
124
125 if (nitems_return != 1)
126 {
127 fprintf(stderr, "_NET_CURRENT_DESKTOP has bad length\n");
128 return (-1);
129 }
130
131 current_desktop = *prop_return;
132 XFree(prop_return);
133 return current_desktop;
134}
135
136/*
137 Get workarea geometry
138 Returns zero on success, -1 on error
139 */
140
141int
142get_current_workarea(uint32 * x, uint32 * y, uint32 * width, uint32 * height)
143{
144 int current_desktop;
145 unsigned long nitems_return;
146 unsigned char *prop_return;
147 uint32 *return_words;
148 const uint32 net_workarea_x_offset = 0;
149 const uint32 net_workarea_y_offset = 1;
150 const uint32 net_workarea_width_offset = 2;
151 const uint32 net_workarea_height_offset = 3;
152 const uint32 max_prop_length = 32 * 4; /* Max 32 desktops */
153
154 if (get_property_value
155 (DefaultRootWindow(g_display), "_NET_WORKAREA", max_prop_length, &nitems_return,
156 &prop_return, 0) < 0)
157 return (-1);
158
159 if (nitems_return % 4)
160 {
161 fprintf(stderr, "_NET_WORKAREA has odd length\n");
162 return (-1);
163 }
164
165 current_desktop = get_current_desktop();
166
167 if (current_desktop < 0)
168 return -1;
169
170 return_words = (uint32 *) prop_return;
171
172 *x = return_words[current_desktop * 4 + net_workarea_x_offset];
173 *y = return_words[current_desktop * 4 + net_workarea_y_offset];
174 *width = return_words[current_desktop * 4 + net_workarea_width_offset];
175 *height = return_words[current_desktop * 4 + net_workarea_height_offset];
176
177 XFree(prop_return);
178
179 return (0);
180
181}
182
183
184
185void
186ewmh_init()
187{
188 /* FIXME: Use XInternAtoms */
189 g_net_wm_state_maximized_vert_atom =
190 XInternAtom(g_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
191 g_net_wm_state_maximized_horz_atom =
192 XInternAtom(g_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
193 g_net_wm_state_hidden_atom = XInternAtom(g_display, "_NET_WM_STATE_HIDDEN", False);
194 g_net_wm_state_skip_taskbar_atom =
195 XInternAtom(g_display, "_NET_WM_STATE_SKIP_TASKBAR", False);
196 g_net_wm_state_skip_pager_atom = XInternAtom(g_display, "_NET_WM_STATE_SKIP_PAGER", False);
197 g_net_wm_state_modal_atom = XInternAtom(g_display, "_NET_WM_STATE_MODAL", False);
198 g_net_wm_state_above_atom = XInternAtom(g_display, "_NET_WM_STATE_ABOVE", False);
199 g_net_wm_state_atom = XInternAtom(g_display, "_NET_WM_STATE", False);
200 g_net_wm_desktop_atom = XInternAtom(g_display, "_NET_WM_DESKTOP", False);
201 g_net_wm_name_atom = XInternAtom(g_display, "_NET_WM_NAME", False);
202 g_net_wm_icon_atom = XInternAtom(g_display, "_NET_WM_ICON", False);
203 g_utf8_string_atom = XInternAtom(g_display, "UTF8_STRING", False);
204}
205
206
207/*
208 Get the window state: normal/minimized/maximized.
209*/
210#ifndef MAKE_PROTO
211int
212ewmh_get_window_state(Window w)
213{
214 unsigned long nitems_return;
215 unsigned char *prop_return;
216 uint32 *return_words;
217 unsigned long item;
218 RD_BOOL maximized_vert, maximized_horz, hidden;
219
220 maximized_vert = maximized_horz = hidden = False;
221
222 if (get_property_value(w, "_NET_WM_STATE", 64, &nitems_return, &prop_return, 0) < 0)
223 return SEAMLESSRDP_NORMAL;
224
225 return_words = (uint32 *) prop_return;
226
227 for (item = 0; item < nitems_return; item++)
228 {
229 if (return_words[item] == g_net_wm_state_maximized_vert_atom)
230 maximized_vert = True;
231 if (return_words[item] == g_net_wm_state_maximized_horz_atom)
232 maximized_horz = True;
233 if (return_words[item] == g_net_wm_state_hidden_atom)
234 hidden = True;
235 }
236
237 XFree(prop_return);
238
239 if (maximized_vert && maximized_horz)
240 return SEAMLESSRDP_MAXIMIZED;
241 else if (hidden)
242 return SEAMLESSRDP_MINIMIZED;
243 else
244 return SEAMLESSRDP_NORMAL;
245}
246
247static int
248ewmh_modify_state(Window wnd, int add, Atom atom1, Atom atom2)
249{
250 Status status;
251 XEvent xevent;
252
253 int result;
254 unsigned long nitems;
255 unsigned char *props;
256 uint32 state = WithdrawnState;
257
258 /* The spec states that the window manager must respect any
259 _NET_WM_STATE attributes on a withdrawn window. In order words, we
260 modify the attributes directly for withdrawn windows and ask the WM
261 to do it for active windows. */
262 result = get_property_value(wnd, "WM_STATE", 64, &nitems, &props, 1);
263 if ((result >= 0) && nitems)
264 {
265 state = *(uint32 *) props;
266 XFree(props);
267 }
268
269 if (state == WithdrawnState)
270 {
271 if (add)
272 {
273 Atom atoms[2];
274
275 atoms[0] = atom1;
276 nitems = 1;
277 if (atom2)
278 {
279 atoms[1] = atom2;
280 nitems = 2;
281 }
282
283 XChangeProperty(g_display, wnd, g_net_wm_state_atom, XA_ATOM,
284 32, PropModeAppend, (unsigned char *) atoms, nitems);
285 }
286 else
287 {
288 Atom *atoms;
289 int i;
290
291 if (get_property_value(wnd, "_NET_WM_STATE", 64, &nitems, &props, 1) < 0)
292 return 0;
293
294 atoms = (Atom *) props;
295
296 for (i = 0; i < nitems; i++)
297 {
298 if ((atoms[i] == atom1) || (atom2 && (atoms[i] == atom2)))
299 {
300 if (i != (nitems - 1))
301 memmove(&atoms[i], &atoms[i + 1],
302 sizeof(Atom) * (nitems - i - 1));
303 nitems--;
304 i--;
305 }
306 }
307
308 XChangeProperty(g_display, wnd, g_net_wm_state_atom, XA_ATOM,
309 32, PropModeReplace, (unsigned char *) atoms, nitems);
310
311 XFree(props);
312 }
313
314 return 0;
315 }
316
317 xevent.type = ClientMessage;
318 xevent.xclient.window = wnd;
319 xevent.xclient.message_type = g_net_wm_state_atom;
320 xevent.xclient.format = 32;
321 if (add)
322 xevent.xclient.data.l[0] = _NET_WM_STATE_ADD;
323 else
324 xevent.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
325 xevent.xclient.data.l[1] = atom1;
326 xevent.xclient.data.l[2] = atom2;
327 xevent.xclient.data.l[3] = 0;
328 xevent.xclient.data.l[4] = 0;
329 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
330 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
331 if (!status)
332 return -1;
333
334 return 0;
335}
336
337/*
338 Set the window state: normal/minimized/maximized.
339 Returns -1 on failure.
340*/
341int
342ewmh_change_state(Window wnd, int state)
343{
344 /*
345 * Deal with the max atoms
346 */
347 if (state == SEAMLESSRDP_MAXIMIZED)
348 {
349 if (ewmh_modify_state
350 (wnd, 1, g_net_wm_state_maximized_vert_atom,
351 g_net_wm_state_maximized_horz_atom) < 0)
352 return -1;
353 }
354 else
355 {
356 if (ewmh_modify_state
357 (wnd, 0, g_net_wm_state_maximized_vert_atom,
358 g_net_wm_state_maximized_horz_atom) < 0)
359 return -1;
360 }
361
362 return 0;
363}
364
365
366int
367ewmh_get_window_desktop(Window wnd)
368{
369 unsigned long nitems_return;
370 unsigned char *prop_return;
371 int desktop;
372
373 if (get_property_value(wnd, "_NET_WM_DESKTOP", 1, &nitems_return, &prop_return, 0) < 0)
374 return (-1);
375
376 if (nitems_return != 1)
377 {
378 fprintf(stderr, "_NET_WM_DESKTOP has bad length\n");
379 return (-1);
380 }
381
382 desktop = *prop_return;
383 XFree(prop_return);
384 return desktop;
385}
386
387
388int
389ewmh_move_to_desktop(Window wnd, unsigned int desktop)
390{
391 Status status;
392 XEvent xevent;
393
394 xevent.type = ClientMessage;
395 xevent.xclient.window = wnd;
396 xevent.xclient.message_type = g_net_wm_desktop_atom;
397 xevent.xclient.format = 32;
398 xevent.xclient.data.l[0] = desktop;
399 xevent.xclient.data.l[1] = 0;
400 xevent.xclient.data.l[2] = 0;
401 xevent.xclient.data.l[3] = 0;
402 xevent.xclient.data.l[4] = 0;
403 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
404 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
405 if (!status)
406 return -1;
407
408 return 0;
409}
410
411void
412ewmh_set_wm_name(Window wnd, const char *title)
413{
414 int len;
415
416 len = strlen(title);
417 XChangeProperty(g_display, wnd, g_net_wm_name_atom, g_utf8_string_atom,
418 8, PropModeReplace, (unsigned char *) title, len);
419}
420
421
422int
423ewmh_set_window_popup(Window wnd)
424{
425 if (ewmh_modify_state
426 (wnd, 1, g_net_wm_state_skip_taskbar_atom, g_net_wm_state_skip_pager_atom) < 0)
427 return -1;
428 return 0;
429}
430
431int
432ewmh_set_window_modal(Window wnd)
433{
434 if (ewmh_modify_state(wnd, 1, g_net_wm_state_modal_atom, 0) < 0)
435 return -1;
436 return 0;
437}
438
439void
440ewmh_set_icon(Window wnd, int width, int height, const char *rgba_data)
441{
442 unsigned long nitems, i;
443 unsigned char *props;
444 uint32 *cur_set, *new_set;
445 uint32 *icon;
446
447 cur_set = NULL;
448 new_set = NULL;
449
450 if (get_property_value(wnd, "_NET_WM_ICON", 10000, &nitems, &props, 1) >= 0)
451 {
452 cur_set = (uint32 *) props;
453
454 for (i = 0; i < nitems;)
455 {
456 if (cur_set[i] == width && cur_set[i + 1] == height)
457 break;
458
459 i += 2 + cur_set[i] * cur_set[i + 1];
460 }
461
462 if (i != nitems)
463 icon = cur_set + i;
464 else
465 {
466 new_set = xmalloc((nitems + width * height + 2) * 4);
467 memcpy(new_set, cur_set, nitems * 4);
468 icon = new_set + nitems;
469 nitems += width * height + 2;
470 }
471 }
472 else
473 {
474 new_set = xmalloc((width * height + 2) * 4);
475 icon = new_set;
476 nitems = width * height + 2;
477 }
478
479 icon[0] = width;
480 icon[1] = height;
481
482 /* Convert RGBA -> ARGB */
483 for (i = 0; i < width * height; i++)
484 {
485 icon[i + 2] =
486 rgba_data[i * 4 + 3] << 24 |
487 ((rgba_data[i * 4 + 0] << 16) & 0x00FF0000) |
488 ((rgba_data[i * 4 + 1] << 8) & 0x0000FF00) |
489 ((rgba_data[i * 4 + 2] << 0) & 0x000000FF);
490 }
491
492 XChangeProperty(g_display, wnd, g_net_wm_icon_atom, XA_CARDINAL, 32,
493 PropModeReplace, (unsigned char *) (new_set ? new_set : cur_set), nitems);
494
495 if (cur_set)
496 XFree(cur_set);
497 if (new_set)
498 xfree(new_set);
499}
500
501void
502ewmh_del_icon(Window wnd, int width, int height)
503{
504 unsigned long nitems, i, icon_size;
505 unsigned char *props;
506 uint32 *cur_set, *new_set;
507
508 cur_set = NULL;
509 new_set = NULL;
510
511 if (get_property_value(wnd, "_NET_WM_ICON", 10000, &nitems, &props, 1) < 0)
512 return;
513
514 cur_set = (uint32 *) props;
515
516 for (i = 0; i < nitems;)
517 {
518 if (cur_set[i] == width && cur_set[i + 1] == height)
519 break;
520
521 i += 2 + cur_set[i] * cur_set[i + 1];
522 }
523
524 if (i == nitems)
525 goto out;
526
527 icon_size = width * height + 2;
528 new_set = xmalloc((nitems - icon_size) * 4);
529
530 if (i != 0)
531 memcpy(new_set, cur_set, i * 4);
532 if (i != nitems - icon_size)
533 memcpy(new_set + i * 4, cur_set + i * 4 + icon_size, nitems - icon_size);
534
535 nitems -= icon_size;
536
537 XChangeProperty(g_display, wnd, g_net_wm_icon_atom, XA_CARDINAL, 32,
538 PropModeReplace, (unsigned char *) new_set, nitems);
539
540 xfree(new_set);
541
542 out:
543 XFree(cur_set);
544}
545
546int
547ewmh_set_window_above(Window wnd)
548{
549 if (ewmh_modify_state(wnd, 1, g_net_wm_state_above_atom, 0) < 0)
550 return -1;
551 return 0;
552}
553
554#endif /* MAKE_PROTO */
555
556
557#if 0
558
559/* FIXME: _NET_MOVERESIZE_WINDOW is for pagers, not for
560 applications. We should implement _NET_WM_MOVERESIZE instead */
561
562int
563ewmh_net_moveresize_window(Window wnd, int x, int y, int width, int height)
564{
565 Status status;
566 XEvent xevent;
567 Atom moveresize;
568
569 moveresize = XInternAtom(g_display, "_NET_MOVERESIZE_WINDOW", False);
570 if (!moveresize)
571 {
572 return -1;
573 }
574
575 xevent.type = ClientMessage;
576 xevent.xclient.window = wnd;
577 xevent.xclient.message_type = moveresize;
578 xevent.xclient.format = 32;
579 xevent.xclient.data.l[0] = StaticGravity | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 11);
580 xevent.xclient.data.l[1] = x;
581 xevent.xclient.data.l[2] = y;
582 xevent.xclient.data.l[3] = width;
583 xevent.xclient.data.l[4] = height;
584
585 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
586 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
587 if (!status)
588 return -1;
589 return 0;
590}
591
592#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette