-
Notifications
You must be signed in to change notification settings - Fork 99
/
icewmbg.cc
1360 lines (1264 loc) · 42.3 KB
/
icewmbg.cc
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
#include "config.h"
#include <unistd.h>
#include <signal.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <X11/Xatom.h>
#include "yxapp.h"
#include "yprefs.h"
#include "ytimer.h"
#include "udir.h"
#include "ascii.h"
#include "intl.h"
#include "appnames.h"
#define CFGDEF
#define ICEWMBG
#include "icewmbg_prefs.h"
char const *ApplicationName = nullptr;
typedef ref<YImage> Image;
class Cache {
public:
Cache(bool verb) : verbose(verb) { }
~Cache() { clear(); }
void clear() { iname = null; image = null; }
Image get(mstring name) {
if (iname != name) {
if (verbose) tlog("load %s", name.c_str());
iname = name;
image = YImage::load(iname);
if (image == null && testOnce(iname, __LINE__)) {
tlog(_("Failed to load image '%s'."), iname.c_str());
}
}
return image;
}
private:
mstring iname;
Image image;
bool verbose;
};
class Background: public YXApplication, private YTimerListener {
public:
Background(int *argc, char ***argv, bool verbose = false);
~Background();
void add(const char* name, const char* value, bool append);
bool become(bool replace = false);
void update(bool force = false);
void printOptions();
void sendQuit() { sendClientMessage(_XA_ICEWMBG_QUIT); }
void sendRestart() { sendClientMessage(_XA_ICEWMBG_RESTART); }
void sendShuffle() { sendClientMessage(_XA_ICEWMBG_SHUFFLE); }
int mainLoop();
void clearBackgroundImages() { backgroundImages.clear(); }
void clearBackgroundColors() { backgroundColors.clear(); }
void clearTransparencyImages() { transparencyImages.clear(); }
void clearTransparencyColors() { transparencyColors.clear(); }
void enableSemitransparency(bool enable = true) {
supportSemitransparency = enable;
}
bool haveBackgrounds() { return imageInited; }
private:
virtual bool filterEvent(const XEvent& xev);
virtual void handleSignal(int sig);
virtual bool handleTimer(YTimer* timer);
virtual int handleError(XErrorEvent* xev);
void restart() const;
void changeBackground(bool force);
void sendClientMessage(Atom message) const;
long* getLongProperties(Atom property, int n) const;
int getLongProperty(Atom property) const;
int getWorkspace() const;
int getNumberOfDesktops() const;
void getDesktopGeometry();
void reshuffle();
void randinit(long seed);
void startShuffle();
int randmax(int upper);
int shuffle(int index);
int getBgPid() const {
return getLongProperty(_XA_ICEWMBG_PID);
}
void deleteProperty(Atom property) const {
XDeleteProperty(display(), window(), property);
}
void changeProperty(Atom property, int format, unsigned char* data) const {
XChangeProperty(display(), window(), property, format,
32, PropModeReplace, data, 1);
}
void changeLongProperty(Atom property, unsigned char* data) const {
changeProperty(property, XA_CARDINAL, data);
}
void changeStringProperty(Atom property, char* data) const {
XChangeProperty(display(), window(), property, XA_STRING, 8,
PropModeReplace, (unsigned char *) data, strlen(data));
}
typedef MStringArray Strings;
typedef YArray<YColor> YColors;
void addImage(Strings& images, const char* name, bool append);
ref<YPixmap> renderBackground(Image back, YColor color);
Image getBackgroundImage();
YColor getBackgroundColor();
Image getTransparencyImage();
YColor getTransparencyColor();
Atom atom(const char* name) const;
static Window window() { return desktop->handle(); }
upath getThemeDir();
bool checkWM();
void syncWM();
private:
char** mainArgv;
bool verbose;
bool randInited;
bool themeInited;
bool imageInited;
bool forceUpdate;
Strings backgroundImages;
YColors backgroundColors;
Strings transparencyImages;
YColors transparencyColors;
Cache cache;
upath themeDir;
const int mypid;
int activeWorkspace;
int cycleOffset;
int desktopCount;
unsigned desktopWidth, desktopHeight;
ref<YPixmap> currentTransparencyPixmap;
YColor currentBackgroundColor;
mstring backgroundName;
YArray<int> sequence;
lazy<YTimer> cycleTimer;
lazy<YTimer> checkTimer;
lazy<YTimer> randrTimer;
lazy<YTimer> updateTimer;
Atom _XA_XROOTPMAP_ID;
Atom _XA_XROOTCOLOR_PIXEL;
Atom _XA_NET_CURRENT_DESKTOP;
Atom _XA_NET_DESKTOP_GEOMETRY;
Atom _XA_NET_NUMBER_OF_DESKTOPS;
Atom _XA_NET_SUPPORTING_WM_CHECK;
Atom _XA_NET_WORKAREA;
Atom _XA_ICEWMBG_QUIT;
Atom _XA_ICEWMBG_IMAGE;
Atom _XA_ICEWMBG_SHUFFLE;
Atom _XA_ICEWMBG_RESTART;
Atom _XA_ICEWMBG_PID;
Atom _XA_ICEWM_WINOPT;
};
Background::Background(int *argc, char ***argv, bool verb):
YXApplication(argc, argv),
mainArgv(*argv),
verbose(verb),
randInited(false),
themeInited(false),
imageInited(false),
forceUpdate(false),
cache(verb),
mypid(getpid()),
activeWorkspace(0),
cycleOffset(0),
desktopCount(MAX_WORKSPACES),
desktopWidth(desktop->width()),
desktopHeight(desktop->height()),
currentBackgroundColor(),
_XA_XROOTPMAP_ID(atom("_XROOTPMAP_ID")),
_XA_XROOTCOLOR_PIXEL(atom("_XROOTCOLOR_PIXEL")),
_XA_NET_CURRENT_DESKTOP(atom("_NET_CURRENT_DESKTOP")),
_XA_NET_DESKTOP_GEOMETRY(atom("_NET_DESKTOP_GEOMETRY")),
_XA_NET_NUMBER_OF_DESKTOPS(atom("_NET_NUMBER_OF_DESKTOPS")),
_XA_NET_SUPPORTING_WM_CHECK(atom("_NET_SUPPORTING_WM_CHECK")),
_XA_NET_WORKAREA(atom("_NET_WORKAREA")),
_XA_ICEWMBG_QUIT(atom("_ICEWMBG_QUIT")),
_XA_ICEWMBG_IMAGE(atom("_ICEWMBG_IMAGE")),
_XA_ICEWMBG_SHUFFLE(atom("_ICEWMBG_SHUFFLE")),
_XA_ICEWMBG_RESTART(atom("_ICEWMBG_RESTART")),
_XA_ICEWM_WINOPT(atom("_ICEWM_WINOPTHINT"))
{
char abuf[42];
snprintf(abuf, sizeof abuf, "_ICEWMBG_PID_S%d", screen());
_XA_ICEWMBG_PID = atom(abuf);
desktop->setStyle(YWindow::wsDesktopAware);
desktopCount = Elvis(getNumberOfDesktops(), desktopCount);
catchSignal(SIGHUP);
catchSignal(SIGTERM);
catchSignal(SIGINT);
catchSignal(SIGQUIT);
catchSignal(SIGUSR1);
catchSignal(SIGUSR2);
#ifdef CONFIG_XRANDR
if (xrandr.supported)
XRRSelectInput(xapp->display(), root(), RRScreenChangeNotifyMask);
#endif
}
upath Background::getThemeDir() {
if (themeInited == false && nonempty(themeName)) {
upath path(themeName);
if (path.isAbsolute() == false) {
if (strchr(themeName, '/') == nullptr) {
path = upath("themes") + path + "default.theme";
}
else {
path = upath("themes") + path;
}
path = findConfigFile(path);
}
if (path.fileExists()) {
themeDir = path.parent();
}
else if (path.dirExists()) {
themeDir = path;
}
if (themeDir == null) {
tlog("Could not find theme dir");
themeDir = "";
}
themeInited = true;
}
return themeDir;
}
Background::~Background() {
int pid = getBgPid();
if (supportSemitransparency) {
if (pid <= 0 || pid == mypid) {
if (_XA_XROOTPMAP_ID)
deleteProperty(_XA_XROOTPMAP_ID);
if (_XA_XROOTCOLOR_PIXEL)
deleteProperty(_XA_XROOTCOLOR_PIXEL);
}
}
if (pid == mypid) {
deleteProperty(_XA_ICEWMBG_PID);
deleteProperty(_XA_ICEWMBG_IMAGE);
}
clearBackgroundImages();
clearBackgroundColors();
clearTransparencyImages();
clearTransparencyColors();
}
int Background::mainLoop() {
changeBackground(false);
if (0 < cycleBackgroundsPeriod) {
cycleTimer->setTimer(cycleBackgroundsPeriod * 1000L, this, true);
}
return YXApplication::mainLoop();
}
Atom Background::atom(const char* name) const {
return XInternAtom(display(), name, False);
}
static bool hasImageExt(const char* name) {
const char* dot = strrchr(name, '.');
if (dot) {
const char* ext = dot + 1;
size_t len = strlen(ext);
if (len > 2 && len < 5) {
char low[8];
for (int i = 0; i < int(len); ++i)
low[i] = ASCII::toLower(ext[i]);
low[int(len)] = '\0';
if (len == 3) {
if (strcmp(low, "xpm") == 0 ||
strcmp(low, "png") == 0 ||
strcmp(low, "jpg") == 0 ||
(strcmp(low, "jxl") == 0 && YImage::supportsFormat(low)) ||
(strcmp(low, "jp2") == 0 && YImage::supportsFormat(low)) ||
(strcmp(low, "raw") == 0 && YImage::supportsFormat(low)) ||
(strcmp(low, "svg") == 0 && YImage::supportsFormat(low)) ||
(strcmp(low, "tga") == 0 && YImage::supportsFormat(low)) ||
(strcmp(low, "tif") == 0 && YImage::supportsFormat(low)))
return true;
}
if (len == 4) {
if (strcmp(low, "jpeg") == 0 ||
(strcmp(low, "avif") == 0 && YImage::supportsFormat(low)) ||
(strcmp(low, "tiff") == 0 && YImage::supportsFormat(low)) ||
(strcmp(low, "webp") == 0 && YImage::supportsFormat(low)))
return true;
}
}
}
return false;
}
void Background::addImage(Strings& images, const char* name, bool append) {
if (append == false) {
images.clear();
}
if (images.getCount() < ICEBG_MAX_ARGS) {
const bool permute = (name[0] == '!');
if (permute || (name[0] == '\\' && name[1] == '!')) {
++name;
}
upath path(name);
if (path.name() == "*") {
upath up(path.parent());
if (up.dirExists() && up.hasglob() == false) {
path = up;
}
}
if (path.hasglob()) {
YStringArray list;
if (path.glob(list, "/")) {
if (permute) {
int k = list.getCount();
randinit(k);
while (0 < --k) {
list.swap(k, randmax(k));
}
}
YStringArray::IterType iter = list.iterator();
while (++iter && images.getCount() < ICEBG_MAX_ARGS) {
if (hasImageExt(*iter)) {
upath image(*iter);
images.append(image);
}
}
}
}
else {
bool isAbs = path.isAbsolute();
path = path.expand();
bool xist = (isAbs && path.isReadable());
if (xist == false && isAbs == false) {
upath p(getThemeDir() + path);
if (p.isReadable()) {
path = p;
xist = true;
}
}
if (xist && path.dirExists()) {
int start = images.getCount();
for (udir dir(path); dir.nextFile(); ) {
if (hasImageExt(dir.entry())) {
upath file(path + dir.entry());
images.append(file);
if (images.getCount() >= ICEBG_MAX_ARGS)
break;
}
}
if (permute) {
int k = images.getCount();
randinit(k);
while (start < --k) {
images.swap(k, start + randmax(k - start));
}
}
}
else if (xist && path.fileExists()) {
images.append(path);
}
else if (testOnce(path.string(), __LINE__)) {
tlog(_("Failed to load image '%s'."), path.string());
}
}
}
}
void Background::add(const char* name, const char* value, bool append) {
if (isEmpty(value)) {
}
else if (0 == strcmp(name, "DesktopBackgroundImage")) {
addImage(backgroundImages, value, append);
imageInited = true;
}
else if (0 == strcmp(name, "DesktopBackgroundColor")) {
if (append == false) {
clearBackgroundColors();
}
if (backgroundColors.getCount() < ICEBG_MAX_ARGS) {
backgroundColors.append(YColor(value));
}
}
else if (0 == strcmp(name, "DesktopTransparencyImage")) {
addImage(transparencyImages, value, append);
}
else if (0 == strcmp(name, "DesktopTransparencyColor")) {
if (append == false) {
clearTransparencyColors();
}
if (transparencyColors.getCount() < ICEBG_MAX_ARGS) {
transparencyColors.append(YColor(value));
}
}
else {
warn("Unknown name '%s'.", name);
}
}
void Background::handleSignal(int sig) {
switch (sig) {
case SIGHUP:
restart();
break;
case SIGINT:
case SIGTERM:
case SIGQUIT:
this->exit(1);
break;
case SIGUSR1:
startShuffle();
break;
case SIGUSR2:
tlog("logEvents %s", boolstr(toggleLogEvents()));
break;
default:
YApplication::handleSignal(sig);
break;
}
}
bool Background::handleTimer(YTimer* timer) {
if (timer == cycleTimer) {
cycleOffset += desktopCount;
update(true);
return true;
}
else if (timer == checkTimer) {
if (checkWM())
syncWM();
update(true);
}
else if (timer == randrTimer) {
if (desktop->updateXineramaInfo(desktopWidth, desktopHeight)) {
if (verbose) tlog("desktop change by RandR event: %ux%u",
desktopWidth, desktopHeight);
update(true);
}
}
else if (timer == updateTimer) {
if (cycleTimer && cycleTimer->expires()) {
cycleTimer->startTimer();
cycleOffset += desktopCount;
}
changeBackground(forceUpdate);
forceUpdate = false;
}
return false;
}
int Background::handleError(XErrorEvent* xev) {
return BadRequest;
}
Image Background::getBackgroundImage() {
Image image;
int count = backgroundImages.getCount();
if (count > 0 && activeWorkspace >= 0) {
for (int i = 0; i < count; ++i) {
int k = shuffle((i + activeWorkspace + cycleOffset) % count);
image = cache.get(backgroundImages[k]);
if (image != null) {
backgroundName = backgroundImages[k];
break;
}
}
}
return image;
}
YColor Background::getBackgroundColor() {
int count = backgroundColors.getCount();
return count > 0
? backgroundColors[activeWorkspace % count]
: YColor::black;
}
Image Background::getTransparencyImage() {
Image image;
int count = transparencyImages.getCount();
int numbg = backgroundImages.getCount();
if (count > 0 && numbg > 0 && activeWorkspace >= 0) {
for (int i = 0; i < count; ++i) {
int k = shuffle((i + activeWorkspace + cycleOffset) % numbg) % count;
image = cache.get(transparencyImages[k]);
if (image != null)
break;
}
}
return image;
}
YColor Background::getTransparencyColor() {
int count = transparencyColors.getCount();
return count > 0
? transparencyColors[activeWorkspace % count]
: getBackgroundColor();
}
void Background::randinit(long seed) {
if (randInited == false) {
timeval now = walltime();
srand(unsigned((seed + mypid) * now.tv_sec + now.tv_usec));
randInited = true;
}
}
int Background::randmax(int upper) {
return rand() / (RAND_MAX / (upper + 1) + 1);
}
void Background::reshuffle() {
const int count = backgroundImages.getCount();
sequence.getCount() ? sequence.clear() : randinit(count);
sequence.setCapacity(count);
for (int i = 0; i < count; ++i) {
sequence.append(i);
}
for (int k = count; --k > 0; ) {
sequence.swap(k, randmax(k));
}
}
int Background::shuffle(int index) {
if (shuffleBackgroundImages) {
if (backgroundImages.getCount() != sequence.getCount()) {
reshuffle();
}
return sequence[index];
}
else {
return index;
}
}
void Background::startShuffle() {
if (verbose) tlog("shuffle");
shuffleBackgroundImages = true;
reshuffle();
update(true);
}
void Background::update(bool force) {
forceUpdate |= force;
updateTimer->setTimer(0L, this, true);
}
long* Background::getLongProperties(Atom property, int n) const {
YProperty prop(window(), property, F32, long(n), XA_CARDINAL);
if (prop && unsigned(n) <= prop.size()) {
return prop.retrieve<long>();
}
return nullptr;
}
int Background::getLongProperty(Atom property) const {
long* prop = getLongProperties(property, 1);
int value = prop ? (int) *prop : 0;
XFree(prop);
return value;
}
void Background::getDesktopGeometry() {
long* prop = getLongProperties(_XA_NET_DESKTOP_GEOMETRY, 2);
if (prop) {
desktopWidth = (int) prop[0];
desktopHeight = (int) prop[1];
XFree(prop);
}
}
int Background::getWorkspace() const {
return getLongProperty(_XA_NET_CURRENT_DESKTOP);
}
int Background::getNumberOfDesktops() const {
return getLongProperty(_XA_NET_NUMBER_OF_DESKTOPS);
}
ref<YPixmap> Background::renderBackground(Image back, YColor color) {
if (verbose) tlog("rendering...");
unsigned width = desktopWidth;
unsigned height = desktopHeight;
if (back == null || width == 0 || height == 0) {
if (verbose) tlog("%s null return", __func__);
return null;
}
int numScreens = multiheadBackground ? 1 : desktop->getScreenCount();
if (numScreens == 1) {
if (width == back->width() && height == back->height()) {
if (verbose) tlog("%s numScreens == 1 return", __func__);
return back->renderToPixmap(desktop->depth());
}
}
ref<YPixmap> cBack = YPixmap::create(width, height, desktop->depth());
Graphics g(cBack);
g.setColor(color);
g.fillRect(0, 0, width, height);
if (centerBackground == false && scaleBackground == false) {
ref<YPixmap> pixm = back->renderToPixmap(desktop->depth());
g.fillPixmap(pixm, 0, 0, width, height, 0, 0);
if (verbose) tlog("%s fillPixmap false return", __func__);
return cBack;
}
unsigned zeroWidth(0), zeroHeight(0);
if (numScreens > 1) {
for (int screen = 0; screen < numScreens; ++screen) {
int x(0), y(0);
unsigned w(0), h(0);
desktop->getScreenGeometry(&x, &y, &w, &h, screen);
if (x == 0 && y == 0) {
if (zeroWidth < w)
zeroWidth = w;
if (zeroHeight < h)
zeroHeight = h;
}
}
} else {
zeroWidth = width;
zeroHeight = height;
}
bool zeroDone(false);
Image scaled;
for (int screen = 0; screen < numScreens; ++screen) {
int x(0), y(0);
if (numScreens > 1) {
desktop->getScreenGeometry(&x, &y, &width, &height, screen);
if (x == 0 && y == 0) {
if (zeroDone) {
continue;
}
else {
zeroDone = true;
width = zeroWidth;
height = zeroHeight;
}
}
}
if (verbose) tlog("%s (%d, %d) (%dx%d+%d+%d)", __func__,
desktopWidth, desktopHeight, width, height, x, y);
unsigned bw = back->width();
unsigned bh = back->height();
if (scaleBackground && false == centerBackground) {
if (bh * width < bw * height) {
bw = height * bw / non_zero(bh);
bh = height;
} else {
bh = width * bh / non_zero(bw);
bw = width;
}
}
else if (centerBackground) {
if (bw >= bh && (bw > width || scaleBackground)) {
bh = width * bh / non_zero(bw);
bw = width;
} else if (bh > height || scaleBackground) {
bw = height * bw / non_zero(bh);
bh = height;
}
}
Image todraw;
if (bw != back->width() || bh != back->height()) {
if (scaled == null ||
scaled->width() != bw ||
scaled->height() != bh) {
scaled = back->scale(bw, bh);
}
todraw = scaled;
}
if (todraw == null) {
todraw = back;
}
ref<YPixmap> pixm = todraw->renderToPixmap(g.rdepth());
if (pixm != null) {
if (verbose) {
tlog(" ... @%d,%d %ux%u+%d+%d",
max(0, int(bw - width) / 2),
max(0, int(bh - height) / 2),
min(width, bw),
min(height, bh),
max(x, x + int(width - bw) / 2),
max(y, y + int(height - bh) / 2));
}
g.drawPixmap(pixm,
max(0, int(bw - width) / 2),
max(0, int(bh - height) / 2),
min(width, bw),
min(height, bh),
max(x, x + int(width - bw) / 2),
max(y, y + int(height - bh) / 2));
}
}
return cBack;
}
void Background::changeBackground(bool force) {
activeWorkspace = getWorkspace();
if (verbose)
tlog("update f=%s ws=%d", boolstr(force), activeWorkspace);
Image backgroundImage = getBackgroundImage();
YColor backgroundColor(getBackgroundColor());
if (force == false) {
if (backgroundImage == null &&
backgroundColor == currentBackgroundColor) {
cache.clear();
return;
}
}
unsigned long const bPixel(backgroundColor.pixel());
bool handleBackground = false;
Pixmap bPixmap(None);
ref<YPixmap> back = renderBackground(backgroundImage, backgroundColor);
if (back != null) {
back->forgetImage();
bPixmap = back->pixmap();
XSetWindowBackgroundPixmap(display(), window(), bPixmap);
changeStringProperty(_XA_ICEWMBG_IMAGE, (char *) backgroundName.c_str());
handleBackground = true;
}
if (false == handleBackground) {
back = YPixmap::create(1, 1, desktop->depth());
Graphics g(back);
g.setColorPixel(bPixel);
g.drawPoint(0, 0);
bPixmap = back->pixmap();
XSetWindowBackground(display(), window(), bPixel);
XSetWindowBackgroundPixmap(display(), window(), bPixmap);
deleteProperty(_XA_ICEWMBG_IMAGE);
handleBackground = true;
}
if (handleBackground) {
if (supportSemitransparency &&
_XA_XROOTPMAP_ID &&
_XA_XROOTCOLOR_PIXEL)
{
YColor tColor(getTransparencyColor());
Image trans = getTransparencyImage();
bool samePixmap = trans == backgroundImage;
bool sameColor = tColor == backgroundColor;
currentTransparencyPixmap =
trans == null || (samePixmap && sameColor) ? back :
renderBackground(trans, tColor);
if (currentTransparencyPixmap != null)
currentTransparencyPixmap->forgetImage();
unsigned long tPixel(tColor.pixel());
Pixmap tPixmap(currentTransparencyPixmap != null
? currentTransparencyPixmap->pixmap()
: bPixmap);
changeProperty(_XA_XROOTPMAP_ID, XA_PIXMAP,
(unsigned char *) &tPixmap);
changeProperty(_XA_XROOTCOLOR_PIXEL, XA_CARDINAL,
(unsigned char *) &tPixel);
if (currentTransparencyPixmap == null && tPixmap == bPixmap)
currentTransparencyPixmap = back;
}
}
XClearWindow(display(), window());
XFlush(display());
cache.clear();
if (backgroundImages.getCount() <= 1 &&
backgroundColors.getCount() <= 1)
{
XKillClient(display(), AllTemporary);
XSetCloseDownMode(display(), RetainTemporary);
XSync(display(), False);
XCloseDisplay(display());
_exit(0);
}
// currentBackgroundPixmap = backgroundPixmap;
currentBackgroundColor = backgroundColor;
}
void Background::restart() const {
if (verbose) tlog("restart");
if (mainArgv[0][0] == '/' ||
(strchr(mainArgv[0], '/') != nullptr &&
access(mainArgv[0], X_OK) == 0))
{
execv(mainArgv[0], mainArgv);
fail("execv %s", mainArgv[0]);
}
execvp(ICEWMBGEXE, mainArgv);
fail("execlp %s", ICEWMBGEXE);
}
bool Background::filterEvent(const XEvent &xev) {
if (xev.xany.window != window()) {
/*ignore*/;
}
else if (xev.type == PropertyNotify &&
xev.xproperty.state == PropertyNewValue)
{
if (xev.xproperty.atom == _XA_NET_CURRENT_DESKTOP) {
update();
}
else if (xev.xproperty.atom == _XA_NET_NUMBER_OF_DESKTOPS) {
getNumberOfDesktops();
}
else if (xev.xproperty.atom == _XA_NET_DESKTOP_GEOMETRY) {
unsigned w = desktopWidth, h = desktopHeight;
getDesktopGeometry();
if (w != desktopWidth || h != desktopHeight) {
if (verbose) tlog("desktop change by geometry property: %ux%u",
desktopWidth, desktopHeight);
checkTimer->setTimer(100L, this, true);
}
}
else if (xev.xproperty.atom == _XA_NET_WORKAREA) {
if (desktop->updateXineramaInfo(desktopWidth, desktopHeight)) {
if (verbose) tlog("desktop change by workarea property: %ux%u",
desktopWidth, desktopHeight);
checkTimer->setTimer(100L, this, true);
}
}
else if (xev.xproperty.atom == _XA_ICEWMBG_PID) {
int pid = getBgPid();
if (pid > 0 && pid != mypid) {
this->exit(0);
}
return true;
}
else if (xev.xproperty.atom == _XA_NET_SUPPORTING_WM_CHECK) {
checkTimer->setTimer(100L, this, true);
}
}
else if (xev.type == ClientMessage) {
if (xev.xclient.message_type == _XA_ICEWMBG_QUIT) {
if (xev.xclient.data.l[0] != mypid) {
if (verbose) tlog("quitting");
this->exit(0);
}
return true;
}
if (xev.xclient.message_type == _XA_ICEWMBG_RESTART) {
restart();
return true;
}
if (xev.xclient.message_type == _XA_ICEWMBG_SHUFFLE) {
startShuffle();
return true;
}
}
#ifdef CONFIG_XRANDR
else if (xrandr.isEvent(xev.type, RRScreenChangeNotify)) {
randrTimer->setTimer(0L, this, true);
return true;
}
#endif
return YXApplication::filterEvent(xev);
}
bool Background::checkWM() {
YProperty wchk(window(), _XA_NET_SUPPORTING_WM_CHECK, F32, 1L, XA_WINDOW);
if (wchk) {
YProperty name(*wchk, _XA_NET_WM_NAME, F8, 5L, _XA_UTF8_STRING);
return name && strncmp(name.string(), "IceWM", 5) == 0;
}
return false;
}
void Background::syncWM() {
YProperty wopt(window(), _XA_ICEWM_WINOPT, F8, 99L, _XA_ICEWM_WINOPT);
if (wopt == false) {
wopt.append("\0\0\0", 3);
sync();
}
for (int i = 0; ++i < 10 && wopt; ++i) {
usleep(i * 1000);
wopt.update();
}
}
void Background::sendClientMessage(Atom message) const {
XClientMessageEvent xev = {};
xev.type = ClientMessage;
xev.window = window();
xev.message_type = message;
xev.format = 32;
xev.data.l[0] = mypid;
send(xev, window(), StructureNotifyMask);
sync();
}
bool Background::become(bool replace) {
int pid = getBgPid();
if (pid > 0 && pid != mypid && (replace || kill(pid, 0) != 0)) {
sendQuit();
int waited = 0;
int period = 1000;
int atmost = 1000*1000;
while (pid > 0 && pid != mypid && waited < atmost) {
if (kill(pid, 0) != 0) {
pid = 0;
} else {
usleep(period);
waited += period;
period += 1000;
}
}
}
if (pid <= 0) {
long lpid = mypid;
changeLongProperty(_XA_ICEWMBG_PID, (unsigned char *) &lpid);
sync();
pid = mypid;
}
return pid == mypid;
}
void Background::printOptions() {
for (int k = 0; k < 10 && icewmbg_prefs[k].type; ++k) {
cfoption* o = &icewmbg_prefs[k];
switch (o->type) {
case cfoption::CF_BOOL:
printf("%-26s = %d\n", o->name, *o->v.b.bool_value);
break;
case cfoption::CF_INT:
printf("%-26s = %d\n", o->name, *o->v.i.int_value);
break;
default:
/*ignore*/
break;
}
}
printf("%-26s = ", "DesktopBackgroundImage");
int i = 0;
for (auto s : backgroundImages)
printf("%s%s", i++ ? ", " : "", s.c_str());
printf("\n");
printf("%-26s = ", "DesktopTransparencyImage");
i = 0;
for (auto s : transparencyImages)
printf("%s%s", i++ ? ", " : "", s.c_str());
printf("\n");
}
static const char* get_help_text() {
return _(
"Usage: icewmbg [OPTIONS]\n"
"Where multiple values can be given they are separated by commas.\n"
"When image is a directory then all images from that directory are used.\n"
"\n"
"Options:\n"
" -p, --replace Replace an existing icewmbg.\n"
" -q, --quit Tell the running icewmbg to quit.\n"
" -r, --restart Tell the running icewmbg to restart itself.\n"
" -u, --shuffle Shuffle/reshuffle the list of background images.\n"
"\n"
" -c, --config=FILE Load preferences from FILE.\n"
" -t, --theme=NAME Load the theme with name NAME.\n"
"\n"
" -i, --image=FILE(S) Load background image(s) from FILE(S).\n"
" -k, --color=NAME(S) Use background color(s) from NAME(S).\n"
"\n"
" -s, --semis=FILE(S) Load transparency image(s) from FILE(S).\n"
" -x, --trans=NAME(S) Use transparency color(s) from NAME(S).\n"
"\n"
" -e, --center=0/1 Disable/Enable centering background.\n"
" -a, --scaled=0/1 Disable/Enable scaling background.\n"
" -m, --multi=0/1 Disable/Enable multihead background.\n"
" -y, --cycle=SECONDS Cycle backgrounds every SECONDS.\n"
"\n"
" --display=NAME Use NAME to connect to the X server.\n"
" --sync Synchronize communication with X11 server.\n"
"\n"
" -h, --help Print this usage screen and exit.\n"
" -V, --version Prints version information and exits.\n"
"\n"
"Loads desktop background according to preferences file:\n"