VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.3/ewmhints.c@ 55121

Last change on this file since 55121 was 55121, checked in by vboxsync, 10 years ago

rdesktop 1.8.3 unmodified

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