-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxlibutil.cpp
More file actions
778 lines (653 loc) · 21.3 KB
/
xlibutil.cpp
File metadata and controls
778 lines (653 loc) · 21.3 KB
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
#include "xlibutil.h"
Xlibutil::Xlibutil()
{
}
int Xlibutil::xwindowMove(Window window, int x, int y, int w, int h)
{
Display *d = XOpenDisplay(0);
//XMoveWindow(d, window, x, y);
//XResizeWindow(d, window, 770, 600);
//XMoveResizeWindow(d, window, x, y, 600, 600);
//XSetWindowBorderWidth(d, window, 8);
//XMapWindow(d, window);
if (y < 0) y = 0;
Status status;
XEvent xevent;
Atom moveresize;
moveresize = XInternAtom(d, "_NET_MOVERESIZE_WINDOW", False);
if (!moveresize)
{
return -1;
}
xevent.type = ClientMessage;
xevent.xclient.window = window;
xevent.xclient.message_type = moveresize;
xevent.xclient.format = 32;
xevent.xclient.data.l[0] = StaticGravity | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 11);
xevent.xclient.data.l[1] = x;
xevent.xclient.data.l[2] = y;
xevent.xclient.data.l[3] = w;
xevent.xclient.data.l[4] = h;
status = XSendEvent(d, DefaultRootWindow(d), False,
SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
XCloseDisplay(d);
return status;
}
QString Xlibutil::xgetWindowFocused()
{
Window w;
int revert_to;
Display *d = XOpenDisplay(0); //QX11Info::display();
XGetInputFocus(d, &w, &revert_to);
XFlush(d);
XCloseDisplay(d);
return QString(this->xwindowClass(w));
}
void Xlibutil::xwindowClose(Window window)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
//Atom atom = XInternAtom(display, "_NET_DELETE_WINDOW", False);
//XSetWMProtocols(display, window, &atom, 1);
//XDestroyWindow(display, window);
Atom atom = XInternAtom(d, "_NET_CLOSE_WINDOW", False);
XEvent xev;
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = atom;
xev.xclient.format = 32;
xev.xclient.data.l[0] = atom;
xev.xclient.data.l[1] = CurrentTime;
XSendEvent(d, DefaultRootWindow(d), False, SubstructureRedirectMask|SubstructureNotifyMask, &xev);
XFlush(d);
XCloseDisplay(d);
}
void Xlibutil::xreservedSpace(Window window, int h)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
// set in xorg reserved space in desktop
long prop[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
prop[3] = h;
//"_NET_WM_STRUT"
XChangeProperty(d, window,
XInternAtom(d, "_NET_WM_STRUT", False),
XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&prop, 4); //4
XChangeProperty(d, window,
XInternAtom(d, "_NET_WM_STRUT_PARTIAL", False),
XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&prop, 12);
XFlush(d);
XCloseDisplay(d);
}
void Xlibutil::openboxChange(Window window, long atom)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
XClientMessageEvent m;
memset(&m, 0, sizeof(m));
m.type = ClientMessage;
m.send_event = True;
m.display = d;
m.window = window;
m.message_type = XInternAtom(d, "_NET_WM_DESKTOP", False);
m.format = 32;
m.data.l[0] = atom;
XSendEvent(d, DefaultRootWindow(d), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m);
//XSync(display, False);
XFlush(d);
XCloseDisplay(d);
}
void Xlibutil::xchange(Window window, const char * atom)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
Atom tmp = XInternAtom(d, atom, False);
XChangeProperty(d, window,
XInternAtom(d, "_NET_WM_WINDOW_TYPE", False), //_NET_WM_WINDOW_TYPE
XA_ATOM, 32, PropModeReplace, (unsigned char *)&tmp, 1); //XA_ATOM
XFlush(d);
XCloseDisplay(d);
}
void Xlibutil::xchangeProperty(Window window, const char *atom, const char * internalAtom, int format)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
Atom tmp = XInternAtom(d, atom, False);
XChangeProperty(d, window,
XInternAtom(d, internalAtom, False), //_NET_WM_WINDOW_TYPE
format, 8, 0, (const unsigned char*)atom, QString(atom).length()); //XA_ATOM
XFlush(d);
XCloseDisplay(d);
}
void Xlibutil::xchangeProperty(Window window, int atom, const char * internalAtom, int format)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
//Atom tmp = XInternAtom(display, atom, False);
XChangeProperty(d, window,
XInternAtom(d, internalAtom, False), //_NET_WM_WINDOW_TYPE
format, 32, PropModeReplace, (unsigned char*)&atom, 1); //XA_ATOM
XFlush(d);
XCloseDisplay(d);
}
char* Xlibutil::xwindowType(Window window)
{
/*
(:_NET_WM_WINDOW_TYPE_DESKTOP . :desktop)
(:_NET_WM_WINDOW_TYPE_DOCK . :dock)
(:_NET_WM_WINDOW_TYPE_TOOLBAR . :toolbar)
(:_NET_WM_WINDOW_TYPE_MENU . :menu)
(:_NET_WM_WINDOW_TYPE_UTILITY . :utility)
(:_NET_WM_WINDOW_TYPE_SPLASH . :splash)
(:_NET_WM_WINDOW_TYPE_DIALOG . :dialog)
(:_NET_WM_WINDOW_TYPE_NORMAL . :normal))
*/
Display *d = XOpenDisplay(0); //QX11Info::display();
Atom nameAtom = XInternAtom(d, "_NET_WM_WINDOW_TYPE", false);
Atom type;
int format;
unsigned long nitems, after;
unsigned char *data;
int status;
status = XGetWindowProperty(d, window, nameAtom, 0L, LONG_MAX,
false, XA_ATOM, &type, &format,
&nitems, &after, &data);
if (status != Success || nitems == 0)
{
XFlush(d);
XCloseDisplay(d);
return "_NET_WM_WINDOW_TYPE_NORMAL";
}
Atom prop = ((Atom *)data)[0];
data = (unsigned char *)XGetAtomName(d, prop);
XFlush(d);
XCloseDisplay(d);
return (char *)data;
}
unsigned char* Xlibutil::xwindowState(Window window)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
Atom nameAtom = XInternAtom(d,"_NET_WM_STATE", True);
Atom type;
int format;
unsigned long nitems, after;
unsigned char *data = 0;
int status;
status = XGetWindowProperty(d, window, nameAtom, 0L, LONG_MAX,
false, XA_ATOM, &type, &format,
&nitems, &after, &data);
if (status != Success || nitems == 0)
{
XFlush(d);
XCloseDisplay(d);
return 0x0;
}
Atom prop = ((Atom *)data)[0];
data = (unsigned char *)XGetAtomName(d, prop);
XFlush(d);
XCloseDisplay(d);
return data;
}
char* Xlibutil::xwindowClass(Window window)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
int status;
unsigned long nitems;
unsigned char* data;
data = this->windowProperty(d, window, "WM_CLASS", &nitems, &status);
if (status != Success || nitems == 0)
{
XFlush(d);
XCloseDisplay(d);
return "unknow";
}
XFlush(d);
XCloseDisplay(d);
return (char*)data;
}
char* Xlibutil::xwindowName(Window window)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
Atom nameAtom = XInternAtom(d, "_NET_WM_NAME", false);
Atom utf8Atom = XInternAtom(d, "UTF8_STRING", false);
Atom type;
int format;
unsigned long nitems, after;
unsigned char *data;
int status;
status = XGetWindowProperty(d, window, nameAtom, 0, 65536,
false, utf8Atom, &type, &format,
&nitems, &after, &data);
if (status != Success || nitems == 0) return "unknow";
XFlush(d);
XCloseDisplay(d);
return (char *)data;
}
int Xlibutil::xwindowPid(Window window)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
unsigned char *data;
unsigned long nitems;
int window_pid = 0;
int status;
data = this->windowProperty(d, window, "_NET_WM_PID", &nitems, &status);
window_pid = (int) *((unsigned long *)data);
if (status != Success || nitems == 0) return 0;
XFlush(d);
XCloseDisplay(d);
return window_pid;
}
unsigned char* Xlibutil::windowProperty(Display *d, Window window, const char *arg, unsigned long *nitems, int *status)
{
Atom type;
int size, actual_format;
//unsigned long nitems;
unsigned char *data;
/*unsigned long nbytes;*/
unsigned long bytes_after; /* unused */
Atom tmp = XInternAtom(d, arg, False);
// 4096 / 4 , (~0L)
*status = XGetWindowProperty(d, window,tmp, 0, (~0L),
False, AnyPropertyType, &type,
&actual_format, nitems, &bytes_after,
&data);
XFlush(d);
return data;
}
XWindowAttributes Xlibutil::attrWindow(Display *d, Window window)
{
XWindowAttributes attr;
XGetWindowAttributes(d, window, &attr);
XFlush(d);
return attr;
}
void Xlibutil::resizeWindow(Display *d, Window window, int x, int y, unsigned int w, unsigned int h)
{
XMoveResizeWindow(d, window, x, y, w, h);
XFlush(d);
}
void Xlibutil::xactive(Window window)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
XEvent xev;
Atom wm_state = XInternAtom(d, "_NET_ACTIVE_WINDOW", False);
Atom win_min = XInternAtom(d, "_NET_ACTIVE_WINDOW", False);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = win_min;
xev.xclient.data.l[1] = CurrentTime;
XSendEvent(d, DefaultRootWindow(d), False, SubstructureRedirectMask|SubstructureNotifyMask, &xev);
XFlush(d);
XCloseDisplay(d);
}
void Xlibutil::xminimize(Window window)
{
Display *d = XOpenDisplay(0); //QX11Info::display();
int screen;
XWindowAttributes attr = this->attrWindow(d, window);
screen = XScreenNumberOfScreen(attr.screen);
XIconifyWindow(d, window, screen);
XFlush(d);
XCloseDisplay(d);
}
Window* Xlibutil::xwindows(unsigned long *size)
{
/*
Window parent;
Window *children;
Window *children_ = (Window *)malloc(sizeof(Window) * 100);
int error;
int add = 0;
unsigned int len;
error = XQueryTree(display, DefaultRootWindow(display), &DefaultRootWindow(display), &parent, &children, &len);
for (int i = 0; i < len; i++)
{
if (this->xwindowName(children[i]) != "unknow")
{
children_[add] = children[i];
add++;
}
}
*size = add;
return children_;*/
Display *d = XOpenDisplay(0); //QX11Info::display();
if (d)
{
Atom atom = XInternAtom(d, "_NET_CLIENT_LIST", True);
Atom actual_type;
int actual_format;
unsigned long bytes_after;
unsigned char *prop = 0x0;
//4096 / 4
//LONG_MAX
int status = XGetWindowProperty(d, DefaultRootWindow(d), atom, 0, LONG_MAX, False, AnyPropertyType, &actual_type, &actual_format, size, &bytes_after, &prop);
if (status != Success)
{
XFlush(d);
XCloseDisplay(d);
return NULL;
}
//Window *lists = (Window *)((unsigned long *)prop);
XFlush(d);
XCloseDisplay(d);
return (Window *)((unsigned long *)prop);
}
XFlush(d);
XCloseDisplay(d);
return NULL;
}
Window Xlibutil::xwindowID(int pid)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
if (d)
{
Atom atom = XInternAtom(d, "_NET_CLIENT_LIST", True);
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
int status = XGetWindowProperty(d, DefaultRootWindow(d), atom, 0, 4096 / 4, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
Window *lists = (Window *)((unsigned long *)prop);
for (int i = 0; i < nitems; i++)
{
if (pid == this->xwindowPid(lists[i]))
{
XFree(prop);
XFlush(d);
XCloseDisplay(d);
return lists[i];
}
}
}
XFlush(d);
XCloseDisplay(d);
return NULL;
}
Window Xlibutil::xwindowIdByClass(QString wmclass)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
if (d)
{
Atom atom = XInternAtom(d, "_NET_CLIENT_LIST", True);
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
int status = XGetWindowProperty(d, DefaultRootWindow(d), atom, 0, 4096 / 4, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
Window *lists = (Window *)((unsigned long *)prop);
for (int i = 0; i < nitems; i++)
{
if (wmclass == QString(this->xwindowClass(lists[i])).toLower())
{
XFree(prop);
XFlush(d);
XCloseDisplay(d);
return lists[i];
}
}
}
XFlush(d);
XCloseDisplay(d);
return NULL;
}
bool Xlibutil::xsingleActive(QString wmclass)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
Atom atom = XInternAtom(d, "_NET_ACTIVE_WINDOW", True);
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
int status = XGetWindowProperty(d, this->xwindowIdByClass(wmclass), atom, 0, 4096 / 4, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
if ((unsigned char*)prop != 0x0)
{
XFree(prop);
XFlush(d);
XCloseDisplay(d);
return true;
}
XFree(prop);
XFlush(d);
XCloseDisplay(d);
return false;
}
bool Xlibutil::xisActive(QString wmclass)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
if (d)
{
Atom atom = XInternAtom(d, "_NET_ACTIVE_WINDOW", True);
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
int status = XGetWindowProperty(d, DefaultRootWindow(d), atom, 0, 4096 / 4, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
Window *lists = (Window *)((unsigned long *)prop);
for (int i = 0; i < nitems; i++)
{
if (wmclass == QString(this->xwindowClass(lists[i])).toLower())
{
XFree(prop);
XFlush(d);
XCloseDisplay(d);
return 1;
}
}
}
XFlush(d);
XCloseDisplay(d);
return 0;
}
void Xlibutil::xminimizeByClass(QString wmclass)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
if (d)
{
Atom atom = XInternAtom(d, "_NET_CLIENT_LIST", True);
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
int status = XGetWindowProperty(d, DefaultRootWindow(d), atom, 0, 4096 / 4, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
Window *lists = (Window *)((unsigned long *)prop);
for (int i = 0; i < nitems; i++)
{
if (wmclass == QString(this->xwindowClass(lists[i])).toLower())
{
this->xminimize(lists[i]);
}
}
XFree(prop);
XFlush(d);
}
XCloseDisplay(d);
}
void Xlibutil::xactiveByClass(QString wmclass)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
if (d)
{
Atom atom = XInternAtom(d, "_NET_CLIENT_LIST", True);
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
int status = XGetWindowProperty(d, DefaultRootWindow(d), atom, 0, 4096 / 4, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
Window *lists = (Window *)((unsigned long *)prop);
for (int i = 0; i < nitems; i++)
{
if (wmclass == QString(this->xwindowClass(lists[i])).toLower())
{
this->xactive(lists[i]);
}
}
XFlush(d);
XFree(prop);
}
XCloseDisplay(d);
}
bool Xlibutil::xwindowExist(QString wmclass)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
if (d)
{
Atom atom = XInternAtom(d, "_NET_CLIENT_LIST", True);
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop;
int status = XGetWindowProperty(d, DefaultRootWindow(d), atom, 0, 4096 / 4, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
Window *lists = (Window *)((unsigned long *)prop);
for (int i = 0; i < nitems; i++)
{
if (wmclass == QString(this->xwindowClass(lists[i])).toLower())
{
XFree(prop);
XFlush(d);
XCloseDisplay(d);
return true;
}
}
XFlush(d);
XFree(prop);
}
XCloseDisplay(d);
return false;
}
QString Xlibutil::xwindowLauncher(Window window)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
unsigned char *data;
unsigned long nitems;
QString launcher;
int status;
data = this->windowProperty(d, window, "_BAMF_DESKTOP_FILE", &nitems, &status);
launcher = (char *)data;
XFlush(d);
XCloseDisplay(d);
return launcher;
}
void Xlibutil::xaddDesktopFile(int pid, QString arg)
{
Display *d = XOpenDisplay(0);//QX11Info::display();
//unsigned char* deskfile = (unsigned char*)"/usr/share/applications/xfce4-terminal.desktop";
unsigned char* deskfile = (unsigned char*)arg.toUtf8().constData();
int status;
if(d)
{
Atom atom = XInternAtom(d, "_BAMF_DESKTOP_FILE", False);
status = XChangeProperty(d, this->xwindowID(pid), atom, XA_STRING, 8, PropModeReplace, deskfile, arg.length());
XFlush(d);
}
XCloseDisplay(d);
}
QPixmap Xlibutil::xwindowScreenShot(Window window, bool argb)
{
Display *d = XOpenDisplay(0);
QPixmap map;
XWindowAttributes attr;
XGetWindowAttributes(d, window, &attr);
XImage* ximage = XGetImage(d, window, 0, 0, attr.width, attr.height, AllPlanes, ZPixmap);
//XImage *ximage = XGetImage(d, window, 0, 0, attr.width, attr.height, XAllPlanes(), ZPixmap);
/*
if(ximage != NULL)
{
image = QImage((const uchar*) ximage->data, ximage->width, ximage->height, ximage->bytes_per_line, QImage::Format_ARGB32_Premultiplied);
image = image.scaled(QSize(24, 24), Qt::KeepAspectRatio, Qt::SmoothTransformation);
map.fromImage(image);
return map;
}*/
if (ximage == NULL) return map;
if (ximage->data != NULL)
{
QImage image;
if (argb)
{
image = QImage((const uchar *)ximage->data, ximage->width, ximage->height, QImage::Format_ARGB32);
}
else
{
image = QImage((const uchar *)ximage->data, ximage->width, ximage->height, QImage::Format_RGB32);
}
image = image.scaled(QSize(ximage->width, ximage->height), Qt::KeepAspectRatio, Qt::SmoothTransformation);
map.convertFromImage(image);
//map = map.scaled(42, 42, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//map = map.scaled(42, 42, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
}
return map;
}
QPixmap Xlibutil::xwindowIcon(Window window, QSize size, bool smooth)
{
Display *d = XOpenDisplay(0);
Atom type;
int format;
unsigned long bytes_after;
ulong* data;
unsigned long nitems;
QPixmap map;
Atom prop = this->atom("_NET_WM_ICON");
//LONG_MAX
XGetWindowProperty(d, window, prop, 0, UINT32_MAX, False, AnyPropertyType,
&type, &format, &nitems, &bytes_after, (uchar**)&data);
if (data != NULL)
{
int count = 0;
int tam = 0;
QVector<int> sizes = {128, 96, 64, 48, 32, 16};
for (int k = 0; k < sizes.length(); k++)
{
count = 0;
bool stop = false;
for (int i = 0; i < nitems; i++)
{
if (data[i] == sizes[k])
{
tam = sizes[k];
stop = true;
break;
}
count++;
}
if (stop) break;
}
if (tam > 0)
{
QImage image(tam, tam, QImage::Format_ARGB32);
for (int i = 0; i < image.byteCount() / 4; ++i)
{
//uint*
//((uint32_t *)image.bits())[i] = data[i + 2];
((uint32_t *)image.bits())[i] = data[count + 2];
count++;
}
if (smooth)
{
image = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
else
{
image = image.scaled(size, Qt::KeepAspectRatio);
}
map.convertFromImage(image);
//map = map.scaled(42, 42, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//map = map.scaled(42, 42, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
}
}
XFree(data);
XFlush(d);
XCloseDisplay(d);
return map;
}
Atom Xlibutil::atom(const char* atomName)
{
Display *d = XOpenDisplay(0);
Atom atom = XInternAtom(d, atomName, false);
XFlush(d);
XCloseDisplay(d);
return atom;
}