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
|
/****************************************************************************
****************************************************************************/
#include "jwm.h"
#include "icon.h"
#include "client.h"
#include "render.h"
#include "main.h"
#include "image.h"
#include "misc.h"
#include "hint.h"
#include "color.h"
static int iconSize = 0;
#ifdef USE_ICONS
#include "x.xpm"
#define HASH_SIZE 64
typedef struct IconPathNode {
char *path;
struct IconPathNode *next;
} IconPathNode;
static IconNode **iconHash;
static IconPathNode *iconPaths;
static IconPathNode *iconPathsTail;
static GC iconGC;
static void SetIconSize();
static void DoDestroyIcon(int index, IconNode *icon);
static void ReadNetWMIcon(ClientNode *np);
static IconNode *GetDefaultIcon();
static IconNode *CreateIconFromData(const char *name, char **data);
static IconNode *CreateIconFromFile(const char *fileName);
static IconNode *CreateIconFromBinary(const unsigned long *data,
unsigned int length);
static IconNode *LoadNamedIconHelper(const char *name, const char *path);
static IconNode *LoadSuffixedIcon(const char *path, const char *name,
const char *suffix);
static ScaledIconNode *GetScaledIcon(IconNode *icon, int width, int height);
static void InsertIcon(IconNode *icon);
static IconNode *FindIcon(const char *name);
static int GetHash(const char *str);
/****************************************************************************
* Must be initialized before parsing the configuration.
****************************************************************************/
void InitializeIcons() {
int x;
iconPaths = NULL;
iconPathsTail = NULL;
iconHash = Allocate(sizeof(IconNode*) * HASH_SIZE);
for(x = 0; x < HASH_SIZE; x++) {
iconHash[x] = NULL;
}
}
/****************************************************************************
****************************************************************************/
void StartupIcons() {
XGCValues gcValues;
unsigned long gcMask;
gcMask = GCGraphicsExposures;
gcValues.graphics_exposures = False;
iconGC = JXCreateGC(display, rootWindow, gcMask, &gcValues);
QueryRenderExtension();
}
/****************************************************************************
****************************************************************************/
void ShutdownIcons() {
int x;
for(x = 0; x < HASH_SIZE; x++) {
while(iconHash[x]) {
DoDestroyIcon(x, iconHash[x]);
}
}
JXFreeGC(display, iconGC);
}
/****************************************************************************
****************************************************************************/
void DestroyIcons() {
IconPathNode *pn;
while(iconPaths) {
pn = iconPaths->next;
Release(iconPaths->path);
Release(iconPaths);
iconPaths = pn;
}
iconPathsTail = NULL;
if(iconHash) {
Release(iconHash);
iconHash = NULL;
}
}
/****************************************************************************
****************************************************************************/
void SetIconSize() {
XIconSize size;
if(!iconSize) {
/* FIXME: compute values based on the sizes we can actually use. */
iconSize = 32;
size.min_width = iconSize;
size.min_height = iconSize;
size.max_width = iconSize;
size.max_height = iconSize;
size.width_inc = iconSize;
size.height_inc = iconSize;
JXSetIconSizes(display, rootWindow, &size, 1);
}
}
/****************************************************************************
****************************************************************************/
void AddIconPath(char *path) {
IconPathNode *ip;
int length;
int addSep;
if(!path) {
return;
}
Trim(path);
length = strlen(path);
if(path[length - 1] != '/') {
addSep = 1;
} else {
addSep = 0;
}
ip = Allocate(sizeof(IconPathNode));
ip->path = Allocate(length + addSep + 1);
strcpy(ip->path, path);
if(addSep) {
ip->path[length] = '/';
ip->path[length + 1] = 0;
}
ExpandPath(&ip->path);
ip->next = NULL;
if(iconPathsTail) {
iconPathsTail->next = ip;
} else {
iconPaths = ip;
}
iconPathsTail = ip;
}
/****************************************************************************
****************************************************************************/
void PutIcon(IconNode *icon, Drawable d, int x, int y,
int width, int height) {
ScaledIconNode *node;
Assert(icon);
node = GetScaledIcon(icon, width, height);
if(node) {
if(PutScaledRenderIcon(icon, node, d, x, y)) {
return;
}
if(node->image != None) {
if(node->mask != None) {
JXSetClipOrigin(display, iconGC, x, y);
JXSetClipMask(display, iconGC, node->mask);
}
JXCopyArea(display, node->image, d, iconGC, 0, 0,
node->width, node->height, x, y);
if(node->mask != None) {
JXSetClipMask(display, iconGC, None);
JXSetClipOrigin(display, iconGC, 0, 0);
}
}
}
}
/****************************************************************************
****************************************************************************/
void LoadIcon(ClientNode *np) {
IconPathNode *ip;
Assert(np);
SetIconSize();
DestroyIcon(np->icon);
np->icon = NULL;
/* Attempt to read _NET_WM_ICON for an icon */
ReadNetWMIcon(np);
if(np->icon) {
return;
}
/* Attempt to find an icon for this program in the icon directory */
if(np->instanceName) {
for(ip = iconPaths; ip; ip = ip->next) {
#ifdef USE_PNG
np->icon = LoadSuffixedIcon(ip->path, np->instanceName, ".png");
if(np->icon) {
return;
}
#endif
#ifdef USE_XPM
np->icon = LoadSuffixedIcon(ip->path, np->instanceName, ".xpm");
if(np->icon) {
return;
}
#endif
}
}
/* Load the default icon */
np->icon = GetDefaultIcon();
}
/****************************************************************************
****************************************************************************/
IconNode *LoadSuffixedIcon(const char *path, const char *name,
const char *suffix) {
IconNode *result;
ImageNode *image;
char *iconName;
Assert(path);
Assert(name);
Assert(suffix);
iconName = Allocate(strlen(name)
+ strlen(path) + strlen(suffix) + 1);
strcpy(iconName, path);
strcat(iconName, name);
strcat(iconName, suffix);
result = FindIcon(iconName);
if(result) {
Release(iconName);
return result;
}
image = LoadImage(iconName);
if(image) {
result = CreateIcon();
result->name = iconName;
result->image = image;
InsertIcon(result);
return result;
} else {
Release(iconName);
return NULL;
}
}
/****************************************************************************
****************************************************************************/
IconNode *LoadNamedIcon(const char *name) {
IconPathNode *ip;
IconNode *icon;
Assert(name);
SetIconSize();
if(name[0] == '/') {
return CreateIconFromFile(name);
} else {
for(ip = iconPaths; ip; ip = ip->next) {
icon = LoadNamedIconHelper(name, ip->path);
if(icon) {
return icon;
}
}
return NULL;
}
}
/****************************************************************************
****************************************************************************/
IconNode *LoadNamedIconHelper(const char *name, const char *path) {
IconNode *result;
char *temp;
temp = AllocateStack(strlen(name) + strlen(path) + 1);
strcpy(temp, path);
strcat(temp, name);
result = CreateIconFromFile(temp);
ReleaseStack(temp);
return result;
}
/****************************************************************************
****************************************************************************/
void ReadNetWMIcon(ClientNode *np) {
unsigned long count;
int status;
unsigned long extra;
Atom realType;
int realFormat;
unsigned char *data;
status = JXGetWindowProperty(display, np->window, atoms[ATOM_NET_WM_ICON],
0, 256 * 256 * 4, False, XA_CARDINAL, &realType, &realFormat, &count,
&extra, &data);
if(status == Success && data) {
np->icon = CreateIconFromBinary((unsigned long*)data, count);
JXFree(data);
}
}
/****************************************************************************
****************************************************************************/
IconNode *GetDefaultIcon() {
return CreateIconFromData("default", x_xpm);
}
/****************************************************************************
****************************************************************************/
IconNode *CreateIconFromData(const char *name, char **data) {
ImageNode *image;
IconNode *result;
Assert(name);
Assert(data);
/* Check if this icon has already been loaded */
result = FindIcon(name);
if(result) {
return result;
}
image = LoadImageFromData(data);
if(image) {
result = CreateIcon();
result->name = CopyString(name);
result->image = image;
InsertIcon(result);
return result;
} else {
return NULL;
}
}
/****************************************************************************
****************************************************************************/
IconNode *CreateIconFromFile(const char *fileName) {
ImageNode *image;
IconNode *result;
if(!fileName) {
return NULL;
}
/* Check if this icon has already been loaded */
result = FindIcon(fileName);
if(result) {
return result;
}
image = LoadImage(fileName);
if(image) {
result = CreateIcon();
result->name = CopyString(fileName);
result->image = image;
InsertIcon(result);
return result;
} else {
return NULL;
}
}
/****************************************************************************
****************************************************************************/
ScaledIconNode *GetScaledIcon(IconNode *icon, int rwidth, int rheight) {
XColor color;
ScaledIconNode *np;
GC maskGC;
int x, y;
int index;
int alpha;
double scalex, scaley;
double srcx, srcy;
double ratio;
int nwidth, nheight;
Assert(icon);
Assert(icon->image);
if(rwidth == 0) {
rwidth = icon->image->width;
}
if(rheight == 0) {
rheight = icon->image->height;
}
ratio = (double)icon->image->width / icon->image->height;
nwidth = Min(rwidth, rheight * ratio);
nheight = Min(rheight, nwidth / ratio);
nwidth = nheight * ratio;
if(nwidth < 1) {
nwidth = 1;
}
if(nheight < 1) {
nheight = 1;
}
/* Check if this size already exists. */
for(np = icon->nodes; np; np = np->next) {
if(np->width == nwidth && np->height == nheight) {
return np;
}
}
/* See if we can use XRender to create the icon. */
np = CreateScaledRenderIcon(icon, nwidth, nheight);
if(np) {
return np;
}
/* Create a new ScaledIconNode the old-fashioned way. */
np = Allocate(sizeof(ScaledIconNode));
np->width = nwidth;
np->height = nheight;
np->next = icon->nodes;
#ifdef USE_XRENDER
np->imagePicture = None;
np->maskPicture = None;
#endif
icon->nodes = np;
np->mask = JXCreatePixmap(display, rootWindow, nwidth, nheight, 1);
maskGC = JXCreateGC(display, np->mask, 0, NULL);
np->image = JXCreatePixmap(display, rootWindow, nwidth, nheight, rootDepth);
scalex = (double)icon->image->width / nwidth;
scaley = (double)icon->image->height / nheight;
srcy = 0.0;
for(y = 0; y < nheight; y++) {
srcx = 0.0;
for(x = 0; x < nwidth; x++) {
index = (int)srcy * icon->image->width + (int)srcx;
alpha = (icon->image->data[index] >> 24) & 0xFFUL;
if(alpha >= 128) {
color.red = ((icon->image->data[index] >> 16) & 0xFFUL) * 257;
color.green = ((icon->image->data[index] >> 8) & 0xFFUL) * 257;
color.blue = (icon->image->data[index] & 0xFFUL) * 257;
GetColor(&color);
JXSetForeground(display, rootGC, color.pixel);
JXDrawPoint(display, np->image, rootGC, x, y);
JXSetForeground(display, maskGC, 1);
} else {
JXSetForeground(display, maskGC, 0);
}
JXDrawPoint(display, np->mask, maskGC, x, y);
srcx += scalex;
}
srcy += scaley;
}
JXFreeGC(display, maskGC);
return np;
}
/****************************************************************************
****************************************************************************/
IconNode *CreateIconFromBinary(const unsigned long *input,
unsigned int length) {
unsigned long height, width;
IconNode *result;
unsigned long *data;
unsigned int x;
if(!input) {
return NULL;
}
width = input[0];
height = input[1];
if(width * height + 2 > length) {
Debug("invalid image size: %d x %d + 2 > %d", width, height, length);
return NULL;
} else if(width == 0 || height == 0) {
Debug("invalid image size: %d x %d", width, height);
return NULL;
}
result = CreateIcon();
result->image = Allocate(sizeof(ImageNode));
result->image->width = width;
result->image->height = height;
result->image->data = Allocate(width * height * sizeof(unsigned long));
data = (unsigned long*)result->image->data;
/* Note: the data types here might be of different sizes. */
for(x = 0; x < width * height; x++) {
data[x] = input[x + 2];
}
/* Don't insert this icon since it is transient. */
return result;
}
/****************************************************************************
****************************************************************************/
IconNode *CreateIcon() {
IconNode *icon;
icon = Allocate(sizeof(IconNode));
icon->name = NULL;
icon->image = NULL;
icon->nodes = NULL;
icon->next = NULL;
icon->prev = NULL;
return icon;
}
/****************************************************************************
****************************************************************************/
void DoDestroyIcon(int index, IconNode *icon) {
ScaledIconNode *np;
if(icon) {
while(icon->nodes) {
np = icon->nodes->next;
#ifdef USE_XRENDER
if(icon->nodes->imagePicture != None) {
JXRenderFreePicture(display, icon->nodes->imagePicture);
}
if(icon->nodes->maskPicture != None) {
JXRenderFreePicture(display, icon->nodes->maskPicture);
}
#endif
if(icon->nodes->image != None) {
JXFreePixmap(display, icon->nodes->image);
}
if(icon->nodes->mask != None) {
JXFreePixmap(display, icon->nodes->mask);
}
Release(icon->nodes);
icon->nodes = np;
}
if(icon->name) {
Release(icon->name);
}
DestroyImage(icon->image);
if(icon->prev) {
icon->prev->next = icon->next;
} else {
iconHash[index] = icon->next;
}
if(icon->next) {
icon->next->prev = icon->prev;
}
Release(icon);
}
}
/****************************************************************************
****************************************************************************/
void DestroyIcon(IconNode *icon) {
int index;
if(icon && !icon->name) {
index = GetHash(icon->name);
DoDestroyIcon(index, icon);
}
}
/****************************************************************************
****************************************************************************/
void InsertIcon(IconNode *icon) {
int index;
Assert(icon);
Assert(icon->name);
index = GetHash(icon->name);
icon->prev = NULL;
if(iconHash[index]) {
iconHash[index]->prev = icon;
}
icon->next = iconHash[index];
iconHash[index] = icon;
}
/****************************************************************************
****************************************************************************/
IconNode *FindIcon(const char *name) {
IconNode *icon;
int index;
index = GetHash(name);
icon = iconHash[index];
while(icon) {
if(!strcmp(icon->name, name)) {
return icon;
}
icon = icon->next;
}
return NULL;
}
/****************************************************************************
****************************************************************************/
int GetHash(const char *str) {
int x;
int hash = 0;
if(!str || !str[0]) {
return hash % HASH_SIZE;
}
for(x = 0; str[x]; x++) {
hash = (hash + str[x]) % HASH_SIZE;
}
return hash;
}
#endif /* USE_ICONS */
|