-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbspwmbar.c
1804 lines (1598 loc) · 43.5 KB
/
bspwmbar.c
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
/* See LICENSE file for copyright and license details. */
#if defined(__linux)
# define _XOPEN_SOURCE 700
# include <alloca.h>
# include <errno.h>
# include <sys/epoll.h>
# include <sys/timerfd.h>
# include <sys/un.h>
#elif defined(__OpenBSD__) || defined(__FreeBSD__)
# include <sys/types.h>
# include <sys/event.h>
# include <sys/time.h>
#endif
/* common libraries */
#include <locale.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <time.h>
#include <unistd.h>
/* XCB */
#include <xcb/xcb.h>
#include <xcb/shm.h>
#include <xcb/xcb_util.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_image.h>
#include <xcb/xcb_renderutil.h>
#include <xcb/randr.h>
#include <ft2build.h>
#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>
#include <cairo/cairo.h>
#include <cairo/cairo-ft.h>
#include <cairo/cairo-xcb.h>
#include <harfbuzz/hb.h>
#include <harfbuzz/hb-ft.h>
/* local headers */
#include "bspwmbar.h"
#include "bspwm.h"
#include "systray.h"
#include "config.h"
#if !defined(VERSION)
# define VERSION "v0.0.0-dev"
#endif
/* epoll max events */
#define MAX_EVENTS 10
/* convert color for cairo */
#define CONVCOL(x) (double)((x) / 255.0)
/* check event and returns true if target is the label */
#define IS_LABEL_EVENT(l,e) (((l).x < (e)->event_x) && ((e)->event_x < (l).x + (l).width))
struct _color_t {
char *name;
uint16_t red;
uint16_t green;
uint16_t blue;
uint32_t pixel;
};
typedef struct {
xcb_pixmap_t pixmap;
xcb_shm_segment_info_t shm_info;
} pixmap_t;
typedef struct {
FT_Face face;
cairo_font_face_t *cairo;
hb_font_t *hb;
} font_t;
typedef struct {
font_t *font;
cairo_glyph_t glyph;
} glyph_font_spec_t;
static glyph_font_spec_t glyph_caches[1024];
typedef struct {
module_option_t *option;
color_t fg, bg;
int x, width;
} label_t;
typedef int desktop_state_t;
typedef struct {
char name[NAME_MAXSZ];
enum {
STATE_FREE = 0,
STATE_FOCUSED = 1 << 1,
STATE_OCCUPIED = 1 << 2,
STATE_URGENT = 1 << 3,
STATE_ACTIVE = 1 << 8
} state;
} desktop_t;
typedef struct {
char name[NAME_MAXSZ];
desktop_t *desktops;
int ndesktop; /* num of desktops */
int cdesktop; /* cap of desktops */
uint8_t is_active;
} monitor_t;
typedef struct {
xcb_window_t win;
int x, y, width, height;
} window_t;
struct _draw_context_t {
window_t xbar;
char monitor_name[NAME_MAXSZ];
xcb_visualtype_t *visual;
xcb_gcontext_t gc;
pixmap_t *buf;
pixmap_t *tmp;
xcb_shm_segment_info_t shm_info;
cairo_t *cr;
int x, width;
label_t left_labels[LENGTH(left_modules)];
label_t right_labels[LENGTH(right_modules)];
};
typedef struct {
/* xcb resources */
xcb_connection_t *xcb;
xcb_screen_t *scr;
xcb_visualid_t visual;
xcb_colormap_t cmap;
/* font */
font_t font;
double font_size;
cairo_font_options_t *font_opt;
FcPattern *pattern;
FcFontSet *set;
/* draw context */
draw_context_t *dcs;
int ndc;
/* base color */
color_t *fg, *bg;
} bspwmbar_t;
/* temporary buffer */
char buf[1024];
static bspwmbar_t bar;
static systray_t *tray;
static poll_fd_t xfd;
#if defined(__linux)
static poll_fd_t timer;
#endif
static FT_Int32 load_flag = FT_LOAD_COLOR | FT_LOAD_NO_BITMAP | FT_LOAD_NO_AUTOHINT;
static FT_Library ftlib;
static color_t **cols;
static int ncol, colcap;
static font_t *fcaches;
static int nfcache = 0;
static int fcachecap = 0;
static int celwidth = 0;
static int graph_maxh = 0;
static int graph_basey = 0;
/* EWMH */
static xcb_ewmh_connection_t ewmh;
static xcb_atom_t xembed_info;
/* Window title cache */
static char *wintitle = NULL;
/* polling fd */
static int pfd = 0;
#if defined(__linux)
static struct epoll_event events[MAX_EVENTS];
#elif defined(__OpenBSD__) || defined(__FreeBSD__)
static struct kevent events[MAX_EVENTS];
#endif
static list_head pollfds;
/* private functions */
static void color_load_hex(const char *, color_t *);
static bool color_load_name(const char *, color_t *);
static void signal_handler(int);
static xcb_window_t get_active_window(uint8_t scrno);
static xcb_visualtype_t *xcb_visualtype_get(xcb_screen_t *);
static bool xcb_shm_support(xcb_connection_t *);
static void xcb_gc_color(xcb_connection_t *, xcb_gcontext_t, color_t *);
static char *get_window_title(xcb_connection_t *, xcb_window_t);
static FT_UInt get_font(FcChar32 rune, font_t **);
static bool load_fonts(const char *);
static void font_destroy(font_t font);
static size_t load_glyphs_from_hb_buffer(draw_context_t *, hb_buffer_t *, font_t *, int *, int, glyph_font_spec_t *, size_t);
static int load_glyphs(draw_context_t *, const char *, glyph_font_spec_t *, int, int *);
static bool xcb_create_pixmap_with_shm(xcb_connection_t *, xcb_screen_t *, xcb_pixmap_t, uint32_t, uint32_t, xcb_shm_segment_info_t *);
static pixmap_t *pixmap_new(xcb_connection_t *, xcb_screen_t *, uint32_t, uint32_t);
static void pixmap_free(pixmap_t *);
static bool dc_init(draw_context_t *, xcb_connection_t *, xcb_screen_t *, int, int, int, int);
static void dc_free(draw_context_t);
static int dc_get_x(draw_context_t *);
static void dc_move_x(draw_context_t *, int);
static void dc_calc_render_pos(draw_context_t *, glyph_font_spec_t *, int);
static void draw_padding(draw_context_t *, int);
static void draw_glyphs(draw_context_t *, color_t *, const glyph_font_spec_t *, int nglyph);
static void render_labels(draw_context_t *, label_t *, size_t);
static void windowtitle_update(xcb_connection_t *, uint8_t);
static void calculate_systray_item_positions(label_t *, module_option_t *);
static void calculate_label_positions(draw_context_t *, label_t *, size_t, int);
static void render();
static int get_baseline();
static bool bspwmbar_init(xcb_connection_t *, xcb_screen_t *);
static void bspwmbar_destroy();
static void poll_init();
static void poll_loop(void (*)());
static void poll_stop();
static poll_result_t xev_handle();
#if defined(__linux)
static poll_result_t timer_reset(int);
#endif
static bool is_change_active_window_event(xcb_property_notify_event_t *);
static void cleanup(xcb_connection_t *);
static void run();
xcb_connection_t *
xcb_connection()
{
return bar.xcb;
}
/**
* color_load_hex() - load a color from hex string
* @colstr: hex string (#RRGGBB)
* @color: (out) loaded color.
*/
void
color_load_hex(const char *colstr, color_t *color)
{
char red[] = { colstr[1], colstr[2], '\0' };
char green[] = { colstr[3], colstr[4], '\0' };
char blue[] = { colstr[5], colstr[6], '\0' };
color->name = strdup(colstr);
color->red = strtol(red, NULL, 16);
color->green = strtol(green, NULL, 16);
color->blue = strtol(blue, NULL, 16);
color->pixel = 0xff000000 | color->red << 16 | color->green << 8 | color->blue;
}
/**
* color_load_name() - load a named color.
* @colstr: color name.
* @color: (out) loaded color.
*
* Return: bool
*/
bool
color_load_name(const char *colstr, color_t *color)
{
xcb_alloc_named_color_cookie_t col_cookie;
xcb_alloc_named_color_reply_t *col_reply;
col_cookie = xcb_alloc_named_color(bar.xcb, bar.cmap, strlen(colstr), colstr);
if (!(col_reply = xcb_alloc_named_color_reply(bar.xcb, col_cookie, NULL)))
return false;
color->name = strdup(colstr);
color->red = col_reply->visual_red;
color->green = col_reply->visual_green;
color->blue = col_reply->visual_blue;
color->pixel = col_reply->pixel;
free(col_reply);
return true;
}
/**
* color_load() - get color_t from color name.
* @colstr: color name.
*
* Return: color_t *
*/
color_t *
color_load(const char *colstr)
{
int i;
/* find color caches */
for (i = 0; i < ncol; i++)
if (!strncmp(cols[i]->name, colstr, strlen(cols[i]->name)))
return cols[i];
if (ncol >= colcap) {
colcap += 5;
cols = realloc(cols, sizeof(color_t *) * colcap);
}
cols[ncol] = calloc(1, sizeof(color_t));
if (colstr[0] == '#' && strlen(colstr) > 6)
color_load_hex(colstr, cols[ncol]);
else
color_load_name(colstr, cols[ncol]);
if (!cols[ncol])
die("color_load(): failed to load color: %s", colstr);
return cols[ncol++];
}
/**
* color_default_fg() - returns default fg color.
*
* Return: Color
*/
color_t *
color_default_fg()
{
return bar.fg;
}
/**
* color_default_bg() - returns default bg color.
*
* Return: Color
*/
color_t *
color_default_bg()
{
return bar.bg;
}
/**
* xcb_visualtype_get() - get visualtype
* @scr: xcb_screen_t *
*
* Return: xcb_visual_type *
*/
xcb_visualtype_t *
xcb_visualtype_get(xcb_screen_t *scr)
{
xcb_visualtype_t *visual_type;
xcb_depth_iterator_t depth_iter;
depth_iter = xcb_screen_allowed_depths_iterator(scr);
for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
xcb_visualtype_iterator_t visual_iter;
visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
for (; visual_iter.rem; xcb_visualtype_next(&visual_iter)) {
if (scr->root_visual == visual_iter.data->visual_id) {
visual_type = visual_iter.data;
return visual_type;
}
}
}
return NULL;
}
/**
* xcb_shm_support() - check shm extension is usable on the X server.
* @xcb: xcb connection.
*
* Return: bool
*/
bool
xcb_shm_support(xcb_connection_t *xcb)
{
xcb_shm_query_version_reply_t *version_reply;
bool supported = (version_reply = xcb_shm_query_version_reply(xcb, xcb_shm_query_version(xcb), NULL)) && version_reply->shared_pixmaps;
free(version_reply);
return supported;
}
/**
* xcb_create_pixmap_with_shm() - create a new pixmap by using shm extension.
* @xcb: xcb connection.
* @scr: screen.
* @pixmap: pixmap id.
* @width: width of pixmap.
* @height: height of pixmap.
* @info: (out) shm segment information of the pixmap.
*
* Return: bool
*/
bool
xcb_create_pixmap_with_shm(xcb_connection_t *xcb, xcb_screen_t *scr, xcb_pixmap_t pixmap, uint32_t width, uint32_t height, xcb_shm_segment_info_t *info)
{
info->shmid = shmget(IPC_PRIVATE, width * height * 4, IPC_CREAT | 0777);
info->shmaddr = shmat(info->shmid, 0, 0);
info->shmseg = xcb_generate_id(xcb);
if (xcb_request_check(xcb, xcb_shm_attach(xcb, info->shmseg, info->shmid, 0))) {
shmctl(info->shmid, IPC_RMID, 0);
shmdt(info->shmaddr);
return false;
}
shmctl(info->shmid, IPC_RMID, 0);
if (xcb_request_check(xcb, xcb_shm_create_pixmap(xcb, pixmap, scr->root, width, height, scr->root_depth, info->shmseg, 0))) {
shmdt(info->shmaddr);
return false;
}
return true;
}
/**
* pixmap_new() - create a new pixmap.
* @xcb: xcb connection.
* @scr: screen.
* @dc: draw context.
* @pixmap: pixmap id.
* @width: width of new pixmap.
* @height: height of new pixmap.
*/
pixmap_t *
pixmap_new(xcb_connection_t *xcb, xcb_screen_t *scr, uint32_t width, uint32_t height)
{
xcb_shm_segment_info_t shm_info = { 0 };
xcb_pixmap_t xcb_pixmap = xcb_generate_id(xcb);
xcb_generic_error_t *err = NULL;
if (xcb_shm_support(xcb)) {
if (!xcb_create_pixmap_with_shm(xcb, scr, xcb_pixmap, width, height, &shm_info))
return NULL;
} else {
if ((err = xcb_request_check(xcb, xcb_create_pixmap_checked(xcb, scr->root_depth, xcb_pixmap, scr->root, width, height)))) {
free(err);
return NULL;
}
}
pixmap_t *pixmap = calloc(1, sizeof(pixmap_t));
pixmap->pixmap = xcb_pixmap;
pixmap->shm_info = shm_info;
return pixmap;
}
/**
* pixmap_free() - free pixmap_t
* @pixmap: pixmap pointer.
*/
void
pixmap_free(pixmap_t *pixmap)
{
if (!pixmap)
return;
if (pixmap->shm_info.shmseg) {
/* if using shm */
xcb_shm_detach(bar.xcb, pixmap->shm_info.shmseg);
shmdt(pixmap->shm_info.shmaddr);
}
xcb_free_pixmap(bar.xcb, pixmap->pixmap);
free(pixmap);
}
/**
* dc_init() - initialize DC.
* @dc: draw context.
* @xcb: xcb connection.
* @scr: screen number.
* @x: window position x.
* @y: window position y.
* @width: window width.
* @height: window height.
*
* Return: bool
*/
bool
dc_init(draw_context_t *dc, xcb_connection_t *xcb, xcb_screen_t *scr, int x,
int y, int width, int height)
{
xcb_configure_window_value_list_t winconf = { 0 };
xcb_create_gc_value_list_t gcv = { 0 };
xcb_render_query_pict_formats_reply_t *pict_reply;
xcb_render_pictforminfo_t *formats;
cairo_surface_t *surface;
window_t *xw = &dc->xbar;
int i = 0;
const uint32_t attrs[] = { bar.bg->pixel, XCB_EVENT_MASK_NO_EVENT };
xw->win = xcb_generate_id(xcb);
xw->x = x;
xw->y = y;
xw->width = width;
xw->height = height;
xcb_create_window(xcb, XCB_COPY_FROM_PARENT, xw->win, scr->root, x, y,
width, height, 0, XCB_COPY_FROM_PARENT,
XCB_COPY_FROM_PARENT,
XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, attrs);
/* set window state */
xcb_atom_t states[] = { ewmh._NET_WM_STATE_STICKY, ewmh._NET_WM_STATE_ABOVE };
xcb_ewmh_set_wm_state(&ewmh, xw->win, LENGTH(states), states);
/* set window type */
xcb_atom_t window_types[] = { ewmh._NET_WM_WINDOW_TYPE_DOCK };
xcb_ewmh_set_wm_window_type(&ewmh, xw->win, LENGTH(window_types), window_types);
/* set window strut */
xcb_ewmh_wm_strut_partial_t strut_partial = {
.left = 0,
.right = 0,
.top = y + height,
.bottom = 0,
.left_start_y = 0,
.left_end_y = 0,
.right_start_y = 0,
.right_end_y = 0,
.top_start_x = x,
.top_end_x = x + width - 1,
.bottom_start_x = 0,
.bottom_end_x = 0,
};
xcb_ewmh_set_wm_strut(&ewmh, xw->win, 0, 0, y + height, 0);
xcb_ewmh_set_wm_strut_partial(&ewmh, xw->win, strut_partial);
/* create graphic context */
dc->visual = xcb_visualtype_get(scr);
/* create pixmap image for rendering */
if (!(dc->buf = pixmap_new(xcb, scr, width, height)))
return false;
if (!(dc->tmp = pixmap_new(xcb, scr, width, height)))
return false;
/* create cairo context */
pict_reply = xcb_render_query_pict_formats_reply(xcb, xcb_render_query_pict_formats(xcb), NULL);
formats = xcb_render_util_find_standard_format(pict_reply, XCB_PICT_STANDARD_RGB_24);
surface = cairo_xcb_surface_create_with_xrender_format(xcb, scr, dc->tmp->pixmap, formats, width, height);
dc->cr = cairo_create(surface);
cairo_set_operator(dc->cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_surface(dc->cr, surface, 0, 0);
cairo_surface_destroy(surface);
free(pict_reply);
/* create gc */
gcv.graphics_exposures = 1;
dc->gc = xcb_generate_id(xcb);
xcb_create_gc_aux(xcb, dc->gc, dc->tmp->pixmap, XCB_GC_GRAPHICS_EXPOSURES, &gcv);
/* set class hint */
xcb_change_property(xcb, XCB_PROP_MODE_REPLACE, xw->win, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, 8, "bspwmbar");
xcb_change_property(xcb, XCB_PROP_MODE_REPLACE, xw->win, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, 17, "bspwmbar\0bspwmbar");
/* create labels from modules */
for (i = 0; i < (int)LENGTH(left_modules); i++)
dc->left_labels[i].option = &left_modules[i];
for (i = 0; i < (int)LENGTH(right_modules); i++)
dc->right_labels[i].option = &right_modules[i];
/* send window rendering request */
winconf.stack_mode = XCB_STACK_MODE_BELOW;
xcb_configure_window_aux(xcb, xw->win, XCB_CONFIG_WINDOW_STACK_MODE, &winconf);
xcb_map_window(xcb, xw->win);
return true;
}
/**
* dc_free() - free resources of DC.
* dc: draw context.
*/
void
dc_free(draw_context_t dc)
{
xcb_free_gc(bar.xcb, dc.gc);
pixmap_free(dc.buf);
pixmap_free(dc.tmp);
xcb_destroy_window(bar.xcb, dc.xbar.win);
cairo_destroy(dc.cr);
}
const char *
draw_context_monitor_name(draw_context_t *dc)
{
return dc->monitor_name;
}
/**
* dc_get_x() - get next rendering position of DC.
* @dc: draw context.
*
* Return: int
*/
int
dc_get_x(draw_context_t *dc)
{
return dc->x;
}
/**
* dc_move_x() - move rendering position by x.
* @dc: draw context.
* @x: distance of movement.
*/
void
dc_move_x(draw_context_t *dc, int x)
{
dc->x += x;
}
/**
* get_active_window() - get active window.
* @dpy: display pointer.
* @scr: screen number.
*
* Return: xcb_window_t
*/
xcb_window_t
get_active_window(uint8_t scrno)
{
xcb_window_t win;
if (xcb_ewmh_get_active_window_reply(&ewmh, xcb_ewmh_get_active_window(&ewmh, scrno), &win, NULL))
return win;
return 0;
}
/**
* get_window_title() - get title of specified win.
* @dpy: display pointer.
* @win: window.
*
* Return: unsigned char *
* The return value needs free after used.
*/
char *
get_window_title(xcb_connection_t *xcb, xcb_window_t win)
{
char *title = NULL;
xcb_get_property_reply_t *reply = NULL;
xcb_ewmh_get_utf8_strings_reply_t utf8_reply = { 0 };
if (xcb_ewmh_get_wm_name_reply(&ewmh, xcb_ewmh_get_wm_name(&ewmh, win), &utf8_reply, NULL)) {
title = strndup(utf8_reply.strings, utf8_reply.strings_len);
xcb_ewmh_get_utf8_strings_reply_wipe(&utf8_reply);
} else if ((reply = xcb_get_property_reply(xcb, xcb_get_property(xcb, 0, win, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, NAME_MAXSZ), NULL))) {
title = strndup(xcb_get_property_value(reply), xcb_get_property_value_length(reply));
free(reply);
}
return title;
}
/**
* windowtitle_update() - update windowtitle() returns value.
* @dpy: display pointer.
* @scr: screen number.
*/
void
windowtitle_update(xcb_connection_t *xcb, uint8_t scrno)
{
xcb_window_t win;
if ((win = get_active_window(scrno))) {
if (wintitle)
free(wintitle);
wintitle = get_window_title(xcb, win);
} else {
/* release wintitle when active window not found */
free(wintitle);
wintitle = NULL;
}
}
/**
* windowtitle() - active window title render function.
* @dc: draw context.
* @opts: module options.
*/
void
windowtitle(draw_context_t *dc, module_option_t *opts)
{
if (!wintitle)
return;
FcChar32 dst;
size_t i = 0, titlelen = strlen(wintitle);
strncpy(buf, wintitle, sizeof(buf));
for (size_t len = 0; i < titlelen && len < opts->title.maxlen; len++)
i += FcUtf8ToUcs4((FcChar8 *)&wintitle[i], &dst, strlen(wintitle) - i);
if (i < strlen(buf))
strncpy(&buf[i], opts->title.ellipsis, sizeof(buf) - i);
draw_text(dc, buf);
}
/**
* get_font() - finds a font that renderable specified rune.
* @rune: FcChar32
*
* Return: FT_Face
*/
FT_UInt
get_font(FcChar32 rune, font_t **font)
{
FcResult result;
FcFontSet *fonts;
FcPattern *pat;
FcCharSet *charset;
FcChar8 *path;
FT_Face face;
int i, idx;
/* Lookup character index with default font. */
if ((idx = FT_Get_Char_Index(bar.font.face, rune))) {
*font = &bar.font;
return idx;
}
/* fallback on font cache */
for (i = 0; i < nfcache; i++) {
if ((idx = FT_Get_Char_Index(fcaches[i].face, rune))) {
*font = &fcaches[i];
return idx;
}
}
/* find font when not found */
if (i >= nfcache) {
if (nfcache >= fcachecap) {
fcachecap += 8;
fcaches = realloc(fcaches, fcachecap * sizeof(font_t));
}
pat = FcPatternDuplicate(bar.pattern);
charset = FcCharSetCreate();
/* find font that contains rune and scalable */
FcCharSetAddChar(charset, rune);
FcPatternAddCharSet(pat, FC_CHARSET, charset);
FcPatternAddBool(pat, FC_SCALABLE, 1);
FcPatternAddBool(pat, FC_COLOR, 1);
FcConfigSubstitute(NULL, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
fonts = FcFontSort(NULL, pat, 1, NULL, &result);
FcPatternDestroy(pat);
FcCharSetDestroy(charset);
if (!fonts)
die("no fonts contain glyph: 0x%x\n", rune);
/* Allocate memory for the new cache entry. */
if (nfcache >= fcachecap) {
fcachecap += 16;
fcaches = realloc(fcaches, fcachecap * sizeof(font_t));
}
/* veirfy matched font */
for (i = 0; i < fonts->nfont; i++) {
pat = fonts->fonts[i];
FcPatternGetString(pat, FC_FILE, 0, &path);
if (FT_New_Face(ftlib, (const char *)path, 0, &face))
die("FT_New_Face failed seeking fallback font: %s\n", path);
if ((idx = FT_Get_Char_Index(face, rune))) {
break;
}
FT_Done_Face(face);
face = NULL;
}
FcFontSetDestroy(fonts);
if (!face)
return 0;
fcaches[nfcache].face = face;
fcaches[nfcache].cairo = cairo_ft_font_face_create_for_ft_face(fcaches[nfcache].face, load_flag);
fcaches[nfcache].hb = hb_ft_font_create(face, NULL);
i = nfcache++;
}
*font = &fcaches[i];
return idx;
}
/**
* load_fonts() - load fonts by specified fontconfig pattern string.
* @patstr: pattern string.
*
* Return:
* 0 - success
* 1 - failure
*/
bool
load_fonts(const char *patstr)
{
double dpi;
FcChar8 *path;
FcPattern *pat = FcNameParse((FcChar8 *)patstr);
if (!pat)
die("loadfonts(): failed parse pattern: %s\n", patstr);
/* get dpi and set to pattern */
dpi = (((double)bar.scr->height_in_pixels * 25.4) / (double)bar.scr->height_in_millimeters);
FcPatternAddDouble(pat, FC_DPI, dpi);
FcPatternAddBool(pat, FC_SCALABLE, 1);
FcConfigSubstitute(NULL, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
FcResult result;
FcPattern *match = FcFontMatch(NULL, pat, &result);
if (!match) {
FcPatternDestroy(pat);
err("loadfonts(): no fonts match pattern: %s\n", patstr);
return false;
}
FcPatternGetString(match, FC_FILE, 0, &path);
if (FT_New_Face(ftlib, (const char *)path, 0, &bar.font.face))
die("FT_New_Face failed seeking fallback font: %s\n", path);
FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &bar.font_size);
FcPatternDestroy(match);
if (!bar.font.face) {
FcPatternDestroy(pat);
err("loadfonts(): failed open font: %s\n", patstr);
return false;
}
bar.font.cairo = cairo_ft_font_face_create_for_ft_face(bar.font.face, load_flag);
bar.font.hb = hb_ft_font_create(bar.font.face, NULL);
bar.pattern = pat;
bar.font_opt = cairo_font_options_create();
cairo_font_options_set_antialias(bar.font_opt, CAIRO_ANTIALIAS_SUBPIXEL);
cairo_font_options_set_subpixel_order(bar.font_opt, CAIRO_SUBPIXEL_ORDER_RGB);
cairo_font_options_set_hint_style(bar.font_opt, CAIRO_HINT_STYLE_SLIGHT);
cairo_font_options_set_hint_metrics(bar.font_opt, CAIRO_HINT_METRICS_ON);
/* padding width */
celwidth = bar.font_size / 2 - 1;
graph_maxh = bar.font_size - (int)bar.font_size % 2;
graph_basey = (BAR_HEIGHT - graph_maxh) / 2;
return true;
}
/**
* get_base_line() - get text rendering baseline.
*
* Return: y offset.
*/
int
get_baseline()
{
return (BAR_HEIGHT - bar.font_size / 2);
}
/**
* dc_calc_render_pos() - calculate render position.
* @dc: DC.
* @glyphs: (in/out) XftCharFontSpec *.
* @nglyph: lenght of glyphs.
*/
void
dc_calc_render_pos(draw_context_t *dc, glyph_font_spec_t *glyphs, int nglyph)
{
int x = dc_get_x(dc);
for (int i = 0; i < nglyph; i++) {
glyphs[i].glyph.x += x;
}
}
/**
* load_glyphs_from_hb_buffer() - load glyphs from hb_buffer_t.
* @dc: draw context.
* @buffer: harfbuzz buffer.
* @font: a font for rendering.
* @x: (out) base x position.
* @y: base y position.
* @glyphs: (out) loaded glyphs.
* @len: max length of glyphs.
*
* Return: size_t
* num of loaded glyphs.
*/
size_t
load_glyphs_from_hb_buffer(draw_context_t *dc, hb_buffer_t *buffer, font_t *font, int *x, int y, glyph_font_spec_t *glyphs, size_t len)
{
cairo_text_extents_t extents;
hb_glyph_info_t *infos;
hb_glyph_position_t *pos;
uint32_t i = 0, ninfo = 0, npos = 0;
cairo_set_font_face(dc->cr, font->cairo);
hb_buffer_guess_segment_properties(buffer);
hb_shape(font->hb, buffer, NULL, 0);
infos = hb_buffer_get_glyph_infos(buffer, &ninfo);
pos = hb_buffer_get_glyph_positions(buffer, &npos);
for (i = 0; i < ninfo && i < len; i++) {
glyphs[i].font = font;
glyphs[i].glyph.index = infos[i].codepoint;
glyphs[i].glyph.x = *x;
glyphs[i].glyph.y = y;
if (pos[i].x_advance) {
*x += pos[i].x_advance / 64;
} else {
cairo_glyph_extents(dc->cr, &glyphs[i].glyph, 1, &extents);
*x += extents.x_advance;
}
}
return i;
}
/**
* load_glyphs() - load XGlyphFontSpec from specified str.
* @dc: draw context.
* @str: utf-8 string.
* @glyphs: (out) XCharFontSpec *.
* @nglyph: length of glyphs.
* @width: (out) rendering width.
*
* Return: number of loaded glyphs.
*/
int
load_glyphs(draw_context_t *dc, const char *str, glyph_font_spec_t *glyphs, int nglyph, int *width)
{
FcChar32 rune = 0;
int i, y, len = 0;
size_t offset = 0, num = 0;
font_t *font = NULL, *prev = NULL;
hb_buffer_t *buffer = NULL;
buffer = hb_buffer_create();
y = get_baseline();
*width = 0;
for (i = 0; offset < strlen(str) && i < nglyph; i++, offset += len) {
len = FcUtf8ToUcs4((FcChar8 *)&str[offset], &rune, strlen(str) - offset);
if (get_font(rune, &font) && prev && prev != font) {
num += load_glyphs_from_hb_buffer(dc, buffer, prev, width, y, &glyphs[num], nglyph - num);
hb_buffer_clear_contents(buffer);
}
prev = font;
hb_buffer_add_codepoints(buffer, &rune, 1, 0, 1);
}
if (prev && hb_buffer_get_length(buffer))
num += load_glyphs_from_hb_buffer(dc, buffer, font, width, y, &glyphs[num], nglyph - num);
hb_buffer_destroy(buffer);
return num;
}
/**
* draw_padding_em() - render padding by em units.
* @dc: DC.
* @num: padding width.
*/
void
draw_padding_em(draw_context_t *dc, double em)
{
draw_padding(dc, celwidth * em);
}
/**
* draw_padding() - render padding.
* @dc: DC.
* @num: padding width.
*/
void
draw_padding(draw_context_t *dc, int num)
{
dc->x += num;
}
/**
* draw_color_text() - render text with color.
* @dc: DC.
* @color: rendering text color.
* @str: rendering text.
*/
void
draw_color_text(draw_context_t *dc, color_t *color, const char *str)
{
int width;
size_t nglyph = load_glyphs(dc, str, glyph_caches, sizeof(glyph_caches), &width);