/[rdesktop]/jpeg/rdesktop/trunk/ewmhints.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /jpeg/rdesktop/trunk/ewmhints.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1413 - (hide annotations)
Mon Jun 18 12:00:34 2007 UTC (17 years ago) by ossman_
Original Path: sourceforge.net/trunk/rdesktop/ewmhints.c
File MIME type: text/plain
File size: 13801 byte(s)
Implement support for icons in SeamlessRDP.

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

  ViewVC Help
Powered by ViewVC 1.1.26