My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
/* gscreendump: sd_xutils.c
*
* Copyright (c) Linux community repr. by <osmoma@online.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

#include <string.h>
#include "sd_xutils.h"
#include "sd_capture.h"
#include "sd_support.h"
#include <X11/extensions/Xdamage.h>

static void argbdata_to_pixdata(long *argb_data, int len, guchar **pixdata);

char *get_window_name(Display *display, Window win, int max_len)
{
static char buf[256];
int len;
*buf = '\0';

#ifdef NO_I18N
char *win_name;
if (XFetchName(display, win, &win_name))
{
if (win_name)
{
len = MIN(strlen(win_name), 255);
strncpy(buf, win_name, len);
buf[len] = '\0';
XFree(win_name);
}
}
#else
XTextProperty tp;
if (XGetWMName(display, win, &tp))
{
if (tp.nitems > 0)
{
int count = 0, ret;
char **list = NULL;
ret = XmbTextPropertyToTextList(display, &tp, &list, &count);
if((ret == Success || ret > 0) && list != NULL)
{
len = MIN(strlen(list[0]), 255);
strncpy(buf, list[0], len);
buf[len] = '\0';
XFreeStringList(list);
}
else
{
strncpy(buf, (const char *)tp.value, MIN(strlen((const char *)tp.value), 255));
}
}
}
#endif

len = strlen(buf);
if (len > max_len && len > 4)
{
buf[max_len--] = '\0';
buf[max_len--] = '.';
buf[max_len--] = '.';
buf[max_len] = '.';
}

/* Do not free the returned value */
return buf;
}


int screenshot_window_is_desktop(GdkWindow *window)
{
/* Return TRUE if window is a desktop.
Credits: This function is from gnome-utils / gnome-screenshot tool.
*/

GdkWindow *root_window = gdk_get_default_root_window ();
GdkWindowTypeHint window_type_hint;

if (window == root_window)
{
return TRUE;
}

window_type_hint = gdk_window_get_type_hint (window);
if (window_type_hint == GDK_WINDOW_TYPE_HINT_DESKTOP)
{
return TRUE;
}
return FALSE;
}

GdkPixbuf *get_window_icon(Display *display, Window w)
{
/* Get icon data for window w from window manager.
Create GdkPixbuf of found icon data and return pixbuf.
Credits: Various sources on the internet (eg. the ROX Desktop) and
http://standards.freedesktop.org/wm-spec/1.3/ar01s05.html#id2523482
*/
Atom ret_type;
gint format;
gulong length, bytes_after;
guchar *atom_data = NULL;

Atom atom_NET_WM_ICON = XInternAtom(display, "_NET_WM_ICON", False);

if (XGetWindowProperty(display, w, atom_NET_WM_ICON, 0, G_MAXLONG, FALSE, AnyPropertyType, &ret_type, &format,
&length, &bytes_after, (guchar **)&atom_data) != Success)
{
return NULL;
}

if (length < 3)
{
XFree(atom_data);
return NULL;
}

guint width = (guint)*atom_data;
guint height = (guint)*(atom_data + sizeof(long));/* sizeof(long)=4 on 32bits OS, 8 on 64bits OS*/

/* Convert argb to rgba (pixmap) format */
guchar *pixdata;
argbdata_to_pixdata((long*)atom_data, width*height, &pixdata);
XFree(atom_data);

GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data((const guchar *)pixdata, GDK_COLORSPACE_RGB, TRUE, 8, width, height, width * 4, free_pixel_data/*free pixdata*/, NULL);

return pixbuf;
}

GdkPixbuf *copy_rectangles_to_pixbuf(GdkPixbuf *image, XRectangle *rectangles, gint rectangle_count)
{
/*
Copy pixels from (shape) rectangles to the image.
Returns a new_image.
Credits: Original code copied and modified from gnome-utils / gnome-screenshot utility.
*/
if ((!GDK_IS_PIXBUF(image)) || (!rectangles) || rectangle_count < 1) return NULL;

gint width = gdk_pixbuf_get_width(image);
gint height = gdk_pixbuf_get_height(image);

gboolean has_alpha = gdk_pixbuf_get_has_alpha(image);

/* Create a new_image of the same size, but add also alpha channel */
GdkPixbuf *new_image = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
if (!GDK_IS_PIXBUF(new_image)) return NULL;

/* Clear it */
gdk_pixbuf_fill(new_image, 0x00000000/*transparent black (at least this works well with PNG mages) */);

guchar *src_pixels = gdk_pixbuf_get_pixels(image);
guchar *dest_pixels = gdk_pixbuf_get_pixels(new_image);

gint src_rowstride = gdk_pixbuf_get_rowstride(image);
gint dest_rowstride = gdk_pixbuf_get_rowstride(new_image);

/* Safety limits */
guchar *max_src = gdk_pixbuf_get_pixels(image) + (height-1) * gdk_pixbuf_get_rowstride(image) + (width-1) * (has_alpha ? 4 : 3);
guchar *max_dest = gdk_pixbuf_get_pixels(new_image) + (height -1) * gdk_pixbuf_get_rowstride(new_image) + (width-1) * 4;

gint rec_x, rec_y;
gint rec_width, rec_height;

/* Copy pixels from all shapes (rectangles) to the new_image */
guint i;
for (i = 0; i < rectangle_count; i++)
{
/* 0-based, relative to the window */
rec_x = rectangles[i].x;
rec_y = rectangles[i].y;

rec_width = rectangles[i].width;
rec_height = rectangles[i].height;

if (rec_x + rec_width > width)
{
rec_width = width - rec_x;
}

if (rec_y + rec_height > height)
{
rec_height = height - rec_x;
}

gint y;
for (y = rec_y; y < rec_y + rec_height; y++)
{
guchar *src, *dest;
gint x;

src = src_pixels + y * src_rowstride + rec_x * (has_alpha ? 4 : 3);
dest = dest_pixels + y * dest_rowstride + rec_x * 4/*has alpha*/;

for (x = 0; x < rec_width; x++)
{
/* Be safe ! */
if (src > max_src) continue;
if (dest > max_dest) continue;

*dest++ = *src++;
*dest++ = *src++;
*dest++ = *src++;

if (has_alpha)
*dest++ = *src++;
else
*dest++ = 255;

}
}
}

/* Return the new image */
return new_image;
}

gboolean has_composite_extension()
{
int event_base, error_base;
Display *display;

display = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());

if (XCompositeQueryExtension(display, &event_base, &error_base ))
{
/* http://ktown.kde.org/~fredrik/composite_howto.html
If we get here the server supports the extension
*/

int major = 0, minor = 2; // The highest version we support
XCompositeQueryVersion(display, &major, &minor );

/* major and minor will now contain the highest version the server supports.
The protocol specifies that the returned version will never be higher
then the one requested. Version 0.2 is the first version to have the
XCompositeNameWindowPixmap() request.
*/
if ( major > 0 || minor >= 2 )
{
/* Has pixmap support */
return TRUE;
}
}

return FALSE;
}

Window find_frame_window(Display *display, Window window)
{
Window window_manager_win;

/* Check first the (parent) window. Is it a frame? */
window_manager_win = find_window_manager_window(window);

if (window_manager_win != None && window_manager_win != window)
{
return window_manager_win;
}

/* But eg. Compiz will fail to find the frame window, so we have to read the window's atom _NET_FRAME_WINDOW. */
Atom atom_NET_FRAME_WINDOW = XInternAtom (display, "_NET_FRAME_WINDOW", FALSE);

get_window_property(display, window , atom_NET_FRAME_WINDOW, &window_manager_win);

/* Got it? */
if (window_manager_win != 0)
return window_manager_win;

return window;
}

Window find_window_manager_window(Window window)
{
Window root, parent, *children;
unsigned int num_children;

do
{
if (XQueryTree (GDK_DISPLAY(), window, &root, &parent, &children, &num_children) == 0)
{
fprintf(stderr, _("Couldn't find the window manager (frame) window."));
return None;
}

if (root == parent) return window;
window = parent;
}
while (1);
}

gboolean get_window_property(Display *display, Window window, Atom atom, Window *ret_data)
{
Atom ret_type;
gint format;
gulong nitems, bytes_after;
guint32 *atom_data;

*ret_data = 0;

if (XGetWindowProperty(display, window, atom, 0, G_MAXLONG, False, XA_WINDOW, &ret_type, &format, &nitems, &bytes_after, (void*)&atom_data) != Success)
{
return FALSE;
}

if (ret_type != XA_WINDOW)
{
XFree (atom_data);
return FALSE;
}

*ret_data = *atom_data;
XFree(atom_data);
return TRUE;
}

#if 0
GdkPixbuf *get_cursor_pixbuf(int cursor_type)
{
GdkCursor *cursor;
GdkPixbuf *cursor_pixbuf;

/* Possible pointer types are:
http://library.gnome.org/devel/gdk/stable/gdk-Cursors.html#GdkCursorType
*/
cursor = gdk_cursor_new_for_display(gdk_display_get_default (), cursor_type);
cursor_pixbuf = gdk_cursor_get_image(cursor);
gdk_cursor_unref(cursor);

return cursor_pixbuf;
}
#endif

GdkPixbuf *get_gdk_cursor_pixbuf(int cursor_type)
{
GdkCursor *cursor;
GdkPixbuf *cursor_pixbuf;

/* Possible pointer types are:
http://library.gnome.org/devel/gdk/stable/gdk-Cursors.html#GdkCursorType
*/
cursor = gdk_cursor_new_for_display(gdk_display_get_default (), cursor_type);
cursor_pixbuf = gdk_cursor_get_image(cursor);
gdk_cursor_unref(cursor);

return cursor_pixbuf;
}

GdkPixbuf *get_cursor_pixbuf(Display *display, GdkWindow *gdk_win, gint *cursor_x, gint *cursor_y, gint *offset_x , gint *offset_y/*x-y hot spot */)
{
GdkWindow *gdk_root = gdk_get_default_root_window();

/* Cursor image */
GdkPixbuf *cursor_pixbuf = NULL;

/* Check if the XFIXES extension is present in the Xorg. */
int event, error;
if (XFixesQueryExtension(display, &event, &error))
{
XFixesCursorImage *cur = XFixesGetCursorImage(display);

*cursor_x = cur->x;
*cursor_y = cur->x;
*offset_x = cur->xhot;
*offset_y = cur->yhot;

/* Convert root co-ordinate (x,y) to child window co-ordinate */
Window child_w;
XTranslateCoordinates(display, GDK_WINDOW_XWINDOW(gdk_root), GDK_WINDOW_XWINDOW(gdk_win), cur->x, cur->y, cursor_x, cursor_y, &child_w);

/* DEBUG:
printf("Root W=%ld, Child W=%ld (Ret child W=%ld): Orig cursor X=%d Orig cursor Y=%d Child cursor X=%d Child cursor Y=%d\n", GDK_WINDOW_XWINDOW(gdk_root),
GDK_WINDOW_XWINDOW(gdk_win), child_w, cur->x, cur->y, cursor_x, cursor_y);
*/

/* Convert raw data to pixel data */
guchar *pixdata;
argbdata_to_pixdata((long*)cur->pixels, cur->width*cur->height, &pixdata);

cursor_pixbuf = gdk_pixbuf_new_from_data((const guchar *)pixdata, GDK_COLORSPACE_RGB, TRUE, 8, cur->width, cur->height, cur->width * 4, free_pixel_data, NULL);
/* Note: pixdata is freed by free_my_data() function.
g_free(pixdata);
*/

/* Free cur */
XFree(cur);
}
else
{
/* Cannot query cursor.
Create a picture of fixed GDK_LEFT_PTR cursor.
*/
cursor_pixbuf = get_gdk_cursor_pixbuf(GDK_LEFT_PTR);

/* TODO: Do not use fixed values.
Calculate these offset.
*/
#define OFFSET_X 6
#define OFFSET_Y 4

*offset_x = OFFSET_X;
*offset_y = OFFSET_Y;

gdk_window_get_pointer(gdk_win, cursor_x, cursor_y, NULL);
}

/* You should g_object_unref() the pixbuf after usage */
return cursor_pixbuf;
}

int window_is_shaped(Display *display, Window window)
{
int bounding, clipping;
gint i1;
guint i2;

XShapeQueryExtents(display, window, &bounding, &i1, &i1, &i2, &i2, &clipping, &i1, &i1, &i2, &i2);
return bounding;
}

int window_is_viewable(Display *display, Window window)
{
XWindowAttributes attr;

int stat = XGetWindowAttributes(display, window, &attr);
/* stat == BadDrawable || stat == BadWindow) */
if (!stat)
return FALSE;

return attr.map_state == IsViewable;
}

Window gtk_window_to_xwindow(GtkWidget *widget)
{
return GDK_WINDOW_XWINDOW(GTK_WIDGET(widget)->window);
}

XColor create_xcolor_from_RGB(Display *display, short red, short green, short blue)
{
XColor color;

color.red = (red * 0xFFFF) / 0xFF;
color.green = (green * 0xFFFF) / 0xFF;
color.blue = (blue * 0xFFFF) / 0xFF;
color.flags = DoRed | DoGreen | DoBlue;

if (!XAllocColor(display, DefaultColormap(display, 0), &color))
{
fprintf(stderr, _("Error: Cannot create Xcolor\n"));
}
return color;
}

int is_inside_rect(XPoint *p, XRectangle *rect)
{
return (p->x >= rect->x && p->x <= rect->x + rect->width) &&
(p->y >= rect->y && p->y <= rect->y + rect->height);
}

void millisleep(guint milliseconds)
{
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds - ts.tv_sec*1000) * 1000000;
nanosleep(&ts, NULL);
}

Display *get_xdisplay()
{
GdkDisplay *gdk_display = gdk_display_get_default();
return GDK_DISPLAY_XDISPLAY(gdk_display);
}

gint get_desktop_for_window(GdkWindow *window)
{
GdkAtom actual_type;
gint actual_format;
gint num_items;
guchar *ret_data_ptr;

GdkAtom atom_net_vm_desktop = gdk_atom_intern("_NET_WM_DESKTOP", FALSE);

gint num = -1;
if (gdk_property_get(window, atom_net_vm_desktop, gdk_atom_intern("CARDINAL", FALSE), 0L, 1L,FALSE,
&actual_type, &actual_format, &num_items, (guchar**)&ret_data_ptr))
{
num = (int)*ret_data_ptr;
g_free(ret_data_ptr);
}

return num;
}

GList *get_all_window_properties(GdkWindow *window)
{
/*
Return a list of all window's atoms (property names).
*/
Atom *all_atoms;
gint atom_count;
gchar *atom_name;

GList *atom_list = NULL;

Window w = GDK_WINDOW_XID(window);
Display *display = GDK_DISPLAY();

all_atoms = XListProperties(display, w, &atom_count);

guint i;
for (i=0; i < atom_count; i++)
{
atom_name = XGetAtomName(display, all_atoms[i]);
atom_list = g_list_append(atom_list, g_strdup(atom_name));
XFree(atom_name);
}
XFree(all_atoms);

/* Notice: You should free the returned list after usage. Use the free_string_list() function */
return atom_list;
}


void bring_window_to_top(GdkWindow *gdk_win, gboolean move_to_desktop)
{
if (move_to_desktop)
{
guint desktop = get_desktop_for_window(gdk_win);
if (desktop != get_active_desktop())
{
set_active_desktop(desktop);
}
}

x11_bring_window_to_top(GDK_WINDOW_XID(gdk_win));
}

void x11_bring_window_to_top(Window window)
{
XEvent xev;
XWindowAttributes wattr;

Display *display = get_xdisplay();

XMapWindow(display, window);
XRaiseWindow(display, window);

memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.display = display;
xev.xclient.window = window;
xev.xclient.message_type = XInternAtom(display, "_NET_ACTIVE_WINDOW", False);
xev.xclient.format = 32;
xev.xclient.data.l[0] = 2L; /* 2 == Message from a window pager */
xev.xclient.data.l[1] = CurrentTime;

XGetWindowAttributes(display, window, &wattr);
XSendEvent(display, wattr.screen->root, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);

XFlush(display);
// XSync(display, False);
}

int is_compiz_wm()
{
return !strcmp(get_window_manager_name(), "compiz");
}

gboolean get_property_data_long(gchar *atom_name, glong **ret_data, gint *ret_length)
{
GdkAtom actual_type;
gint actual_format;

GdkAtom gatom = gdk_atom_intern(atom_name, FALSE);

if (gdk_property_get(gdk_get_default_root_window(), gatom, gdk_atom_intern("CARDINAL", FALSE), 0L, (0xFF), FALSE,
&actual_type, &actual_format, ret_length, (guchar**)ret_data))
{
return TRUE;
}

*ret_data = NULL;
*ret_length = 0;

return FALSE;
}

const char *get_window_manager_name()
{
/* Return name of the window manager.
Eg: "compiz" or "Metacity"
*/
GdkScreen *gdk_def_screen;
gdk_def_screen = gdk_screen_get_default();

return gdk_x11_screen_get_window_manager_name(gdk_def_screen);
}

guint get_number_of_desktops()
{
if (is_compiz_wm())
return compiz_get_number_of_viewports();

return metacity_get_number_of_desktops();

}

guint metacity_get_number_of_desktops()
{

GdkAtom actual_type;
gint actual_format;
gint num_items;
guchar *ret_data_ptr;

GdkAtom atom_net_number_desktops = gdk_atom_intern("_NET_NUMBER_OF_DESKTOPS", FALSE);

guint num = 0;
if (gdk_property_get(gdk_get_default_root_window(), atom_net_number_desktops, gdk_atom_intern("CARDINAL", FALSE), 0L, 1L, FALSE,
&actual_type, &actual_format, &num_items, (guchar**)&ret_data_ptr))
{
num = (int)*ret_data_ptr;
g_free(ret_data_ptr);
}

return num;
}

guint compiz_get_number_of_viewports()
{
guint num_viewports;

/* if (is_compiz_wm())... */

guint viewport_width = 1;
guint viewport_total_width = 0;

glong *data;
gint data_len;

/* An example: _NET_WORKAREA(CARDINAL) = 0, 25, 1680, 1000 */
if (get_property_data_long("_NET_WORKAREA", &data, &data_len))
{
viewport_width = (int)data[2];
g_free(data);
}

/* An example: _NET_DESKTOP_GEOMETRY(CARDINAL) = 8400, 1050 */
if (get_property_data_long("_NET_DESKTOP_GEOMETRY", &data, &data_len))
{
viewport_total_width = data[0];
g_free(data);
}

num_viewports = MAX(viewport_total_width / viewport_width, 1);

return num_viewports;
}

GList *get_desktop_names()
{
if (is_compiz_wm())
return compiz_get_viewport_names();

return metacity_get_desktop_names();
}

GList *metacity_get_desktop_names()
{
GdkAtom actual_type;
gint actual_format;
gint num_items;
guchar *ret_data_ptr = NULL;

GList *list = NULL;

/* Ask the names */
GdkAtom atom_net_desktop_names = gdk_atom_intern("_NET_DESKTOP_NAMES", FALSE);

if (gdk_property_get(gdk_get_default_root_window(),atom_net_desktop_names,
gdk_atom_intern("UTF8_STRING", FALSE), 0, 0xFFFFFF, FALSE, &actual_type, &actual_format, &num_items, &ret_data_ptr))
{
if (num_items > 0)
{
gchar *p = (gchar *)ret_data_ptr;
list = g_list_append(list, g_strdup(p));
guint i;
for (i = 0; i < num_items-1; i++)
{
if (*(ret_data_ptr + i) == '\0')
{
p = (gchar*)ret_data_ptr + i + 1;
list = g_list_append(list, g_strdup(p));
}
}
}
g_free(ret_data_ptr);
}

/* Notice: You should free the list->data values and the list after usage */
return list;
}

GList *compiz_get_viewport_names()
{

/* Compiz window manager will not give the viewports (ehm... desktops) any names,
so we'll set the names to "Compiz viewport #".
*/

/* Number of viewports */
guint num_viewports = compiz_get_number_of_viewports();

GList *list = NULL;

guint i;
for (i=0; i < num_viewports; i++)
{
list = g_list_append(list, g_strdup_printf(_("Compiz viewport %d"), i+1));
}

return list;
}

guint get_active_desktop()
{
if (is_compiz_wm())
return compiz_get_active_viewport();

return metacity_get_active_desktop();
}

guint compiz_get_active_viewport()
{
glong *data;
gint data_len;

guint desktop_width = 0;
guint desktop_viewport = 0;

// Example: _NET_WORKAREA(CARDINAL) = 0, 25, 1680, 1000
if (get_property_data_long("_NET_WORKAREA", &data, &data_len))
{
desktop_width = (int)data[2];
g_free(data);
}

// Example: _NET_DESKTOP_VIEWPORT(CARDINAL) = 5040, 0
if (get_property_data_long("_NET_DESKTOP_VIEWPORT", &data, &data_len))
{
desktop_viewport = (int)data[0];
g_free(data);
}

guint num = 0;

if (desktop_width > 0)
num = desktop_viewport / desktop_width;

return num;
}

guint metacity_get_active_desktop()
{
/* Get the current, active desktop.
The returned value is zero-based: 0, 1, 2...
*/
GdkAtom actual_type;
gint actual_format;
gint num_items;
guchar *ret_data_ptr = NULL;

/* Metacity or other standards respecting window manager */
GdkAtom atom_net_current_desktop = gdk_atom_intern("_NET_CURRENT_DESKTOP", FALSE);

gint num = 0;
if (gdk_property_get(gdk_get_default_root_window(), atom_net_current_desktop, gdk_atom_intern("CARDINAL", FALSE), 0L, 1L, FALSE,
&actual_type, &actual_format, &num_items, (guchar**)&ret_data_ptr))
{
num = *((int*)ret_data_ptr);
g_free(ret_data_ptr);
}

return num;
}

void set_active_desktop(guint desktop_num)
{
/* Set active desktop.
The desktop_num is zero-based: 0, 1, 2...
*/

if (is_compiz_wm())
{
compiz_set_active_viewport(desktop_num);
return;
}

metacity_set_active_desktop(desktop_num);
}

void compiz_set_active_viewport(guint viewport_num)
{
glong *data;
gint data_len;

guint desktop_width = 0;

/* Example: _NET_WORKAREA(CARDINAL) = 0, 25, 1680, 1000 */
if (get_property_data_long("_NET_WORKAREA", &data, &data_len))
{
desktop_width = (int)data[2];
g_free(data);
}

guint total_viewport_width = 0;

/* Example: _NET_DESKTOP_VIEWPORT(CARDINAL) = 5040, 0 */
if (get_property_data_long("_NET_DESKTOP_GEOMETRY", &data, &data_len))
{
total_viewport_width = (int)data[0];
g_free(data);
}

guint new_viewport_width = viewport_num * desktop_width;
if (new_viewport_width > total_viewport_width) return;

Display *display = GDK_DISPLAY();
Window root_win = GDK_WINDOW_XWINDOW(gdk_get_default_root_window());

/* Set _NET_DESKTOP_VIEWPORT(CARDINAL) = ####, 0 */
Atom atom_net_current_desktop = XInternAtom(display, "_NET_DESKTOP_VIEWPORT", False);
XEvent xevent;
xevent.type = ClientMessage;
xevent.xclient.type = ClientMessage;
xevent.xclient.display = display;
xevent.xclient.window = root_win;
xevent.xclient.message_type = atom_net_current_desktop;
xevent.xclient.format = 32;
xevent.xclient.data.l[0] = new_viewport_width;
xevent.xclient.data.l[1] = 0;
xevent.xclient.data.l[2] = CurrentTime;
xevent.xclient.data.l[3] = 0;
xevent.xclient.data.l[4] = 0;
XSendEvent(display, root_win, False, SubstructureNotifyMask | SubstructureRedirectMask, &xevent);

XFlush(display);
return;
}

void metacity_set_active_desktop(guint desktop_num)
{
/* Set active desktop.
The desktop_num is zero-based: 0, 1, 2...
*/

/* Metacity or other standards respecting window manager */
if (desktop_num+1 > get_number_of_desktops()) return;

Display *display = GDK_DISPLAY();
Window root_win = GDK_WINDOW_XWINDOW(gdk_get_default_root_window());

Atom atom_net_current_desktop = XInternAtom(display, "_NET_CURRENT_DESKTOP", False);
XEvent xevent;
xevent.type = ClientMessage;
xevent.xclient.type = ClientMessage;
xevent.xclient.display = display;
xevent.xclient.window = root_win;
xevent.xclient.message_type = atom_net_current_desktop;
xevent.xclient.format = 32;
xevent.xclient.data.l[0] = desktop_num;
xevent.xclient.data.l[1] = CurrentTime;
xevent.xclient.data.l[2] = 0;
xevent.xclient.data.l[3] = 0;
xevent.xclient.data.l[4] = 0;
XSendEvent(display, root_win, False, SubstructureNotifyMask | SubstructureRedirectMask, &xevent);

XFlush(display);
}

Window try_children(Display *display, Window win, Atom WM_STATE)
{
Window root, parent;
Window *children;
unsigned int nchildren;
unsigned int i;
Atom type = None;
int format;
unsigned long nitems, after;
unsigned char *data;
Window inf = 0;

if (!XQueryTree(display, win, &root, &parent, &children, &nchildren)) return 0;

for (i = 0; !inf && (i < nchildren); i++)
{
XGetWindowProperty(display, children[i], WM_STATE, 0, 0, False, AnyPropertyType, &type, &format, &nitems, &after, &data);

if (type)
inf = children[i];
}

/* Check children recursively */
for (i = 0; !inf && (i < nchildren); i++)
inf = try_children(display, children[i], WM_STATE);

if (children) XFree((char *)children);
return inf;
}

Window find_WM_STATE_window(Display *display, Window win)
{
/* Function finds a window, at or below the specified window (win), that has a WM_STATE property.
If such a window is found, it is returned; otherwise the argument window is returned.

This is a direct copy from Xfree86's XmuClientWindow() function.
*/
Atom wm_state;
Atom type = None;
int format;
unsigned long nitems, after;
unsigned char *data;
Window inf;

wm_state = XInternAtom(display, "WM_STATE", True);
if (!wm_state) return win;

XGetWindowProperty(display, win, wm_state, 0, 0, False, AnyPropertyType, &type, &format, &nitems, &after, &data);
if (type) return win;

inf = try_children(display, win, wm_state);

if (!inf) inf = win;

return inf;
}

void set_titlebar_height(GtkWindow *gtk_win, gint height)
{
#if 0
/* TODO:
Something is wrong. Fix this.
*/
gint actual_format, actual_length;
gint *extents = NULL;
GdkAtom atom_net_frame_extents = gdk_atom_intern("_NET_FRAME_EXTENTS", FALSE);
GdkAtom atom_cardinal = gdk_atom_intern("CARDINAL", FALSE);

if (gdk_property_get (GTK_WIDGET(gtk_win)->window, atom_net_frame_extents, atom_cardinal, 0,32, FALSE, NULL,
&actual_format, &actual_length, (guchar**)&extents) && extents)
{
extents[2] = height;

gdk_property_change(GTK_WIDGET(gtk_win)->window, atom_net_frame_extents, atom_cardinal, 32, GDK_PROP_MODE_REPLACE, (guchar*)extents, 4);

g_free(extents);
}
#endif
}

static void argbdata_to_pixdata(long *argb_data, int len, guchar **pixdata)
{
/* I have borrowsed this from GNOME's code base.
Copyright (C) 2002 Havoc Pennington.

The size of long=4 is important here so the variable "i" runs correctly over argb_data.
Long is 4 bytes length on 32bits and 8 on 64bits Linux.
*/
guchar *p;
int i;

*pixdata = g_new(guchar, len * 4);
p = *pixdata;

i = 0;
while (i < len)
{
guint argb;
guint rgba;

argb = argb_data[i];
rgba = (argb << 8) | (argb >> 24);

*p = rgba >> 24;
++p;
*p = (rgba >> 16) & 0xff;
++p;
*p = (rgba >> 8) & 0xff;
++p;
*p = rgba & 0xff;
++p;

i++;
}
}

void free_pixel_data(guchar *pixels, gpointer data)
{
g_free(pixels);
}

guint get_best_delay_for_window_manager()
{
guint delay;
if (is_compiz_wm())
delay = 700;
else
delay = 350;

return delay;
}

Change log

r111 by osmoma on Jun 15, 2009   Diff
Variables are now feeded to the command by
inserting them in the command's header
section. Eg. if the command declaration
contains 2 variables var1 and var2, then
the final command line would look like
this: bash -c 'var1=xxx; var2=yyy; mogrify
...'. The old method used a simple textual
substitution in the command text which may
cause errors.
Go to: 
Project members, sign in to write a code review

Older revisions

r104 by osmoma on Apr 13, 2009   Diff
Gscreendump should now compile and run
on Ubuntu 8.04. Ubuntu 8.04 has
GTK+/GDK version 2.12.9 while some
functions require GTK+ 2.14.0 or
later. Some issues remain unsolved:
...
r102 by osmoma on Apr 12, 2009   Diff
New import after accidental delete
All revisions of this file

File info

Size: 26498 bytes, 1027 lines
Powered by Google Project Hosting