-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay-x11-xcb.c
2030 lines (1840 loc) · 50.2 KB
/
display-x11-xcb.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
/*
* Copyright Neil Brown ©2021-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* X11 display driver for edlib, using xcb, cairopango, libxkbcommon etc.
*
* A different connection to the server will be created for each
* display. Maybe that can be optimised one day.
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <xcb/xcb.h>
#include <stdarg.h>
#include <sys/wait.h>
#ifndef __CHECKER__
#include <xcb/xkb.h>
#else
/* xkb.h has a 'long' in an enum :-( */
enum {
XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY,
XCB_XKB_EVENT_TYPE_MAP_NOTIFY,
XCB_XKB_NEW_KEYBOARD_NOTIFY,
XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
XCB_XKB_MAP_PART_MODIFIER_MAP,
XCB_XKB_STATE_PART_MODIFIER_LOCK,
XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS,
XCB_XKB_STATE_PART_GROUP_BASE,
XCB_XKB_MAP_PART_KEY_ACTIONS,
XCB_XKB_STATE_PART_GROUP_LATCH,
XCB_XKB_MAP_PART_VIRTUAL_MODS,
XCB_XKB_STATE_PART_GROUP_LOCK,
XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP,
XCB_XKB_NKN_DETAIL_KEYCODES,
XCB_XKB_MAP_PART_KEY_TYPES,
XCB_XKB_MAP_PART_KEY_SYMS,
XCB_XKB_STATE_PART_MODIFIER_BASE,
XCB_XKB_STATE_PART_MODIFIER_LATCH,
XCB_XKB_MAP_NOTIFY,
XCB_XKB_STATE_NOTIFY,
};
typedef uint16_t xcb_xkb_device_spec_t;
typedef struct xcb_xkb_select_events_details_t {
uint16_t affectNewKeyboard;
uint16_t newKeyboardDetails;
uint16_t affectState;
uint16_t stateDetails;
/* and other fields */
} xcb_xkb_select_events_details_t;
typedef struct xcb_xkb_new_keyboard_notify_event_t {
uint8_t deviceID;
uint16_t changed;
/* and other fields */
} xcb_xkb_new_keyboard_notify_event_t;
typedef struct xcb_xkb_state_notify_event_t {
uint8_t deviceID;
uint8_t baseMods;
uint8_t latchedMods;
uint8_t lockedMods;
int16_t baseGroup;
int16_t latchedGroup;
uint8_t lockedGroup;
/* and other fields */
} xcb_xkb_state_notify_event_t;
typedef struct xcb_xkb_map_notify_event_t {
uint8_t deviceID;
} xcb_xkb_map_notify_event_t;
xcb_void_cookie_t
xcb_xkb_select_events_aux_checked(xcb_connection_t *c,
xcb_xkb_device_spec_t deviceSpec,
uint16_t affectWhich,
uint16_t clear,
uint16_t selectAll,
uint16_t affectMap,
uint16_t map,
const xcb_xkb_select_events_details_t *details);
#endif
#include <xcb/xcbext.h>
#include <ctype.h>
#include <math.h>
#include <locale.h>
#include <cairo.h>
#include <cairo-xcb.h>
#include <wand/MagickWand.h>
#ifdef __CHECKER__
// enums confuse sparse...
#define MagickBooleanType int
#endif
#ifndef __CHECKER__
#include <pango/pango.h>
#include <pango/pangocairo.h>
#else
typedef struct PangoFontDescription {} PangoFontDescription;
typedef struct PangoLayout {} PangoLayout;
typedef struct PangoContext {} PangoContext;
typedef struct PangoFontMetrics {} PangoFontMetrics;
typedef struct PangoRectangle { int x,y,width,height;} PangoRectangle;
typedef enum { PANGO_STYLE_NORMAL, PANGO_STYLE_OBLIQUE, PANGO_STYLE_ITALIC
} PangoStyle;
typedef enum { PANGO_VARIANT_NORMAL, PANGO_VARIANT_SMALL_CAPS } PangoVariant;
typedef enum { PANGO_WEIGHT_NORMAL, PANGO_WEIGHT_BOLD } PangoWeight;
PangoFontDescription *pango_font_description_new(void);
void pango_font_description_set_family_static(PangoFontDescription*, char*);
void pango_font_description_set_family(PangoFontDescription*, char*);
void pango_font_description_set_size(PangoFontDescription*, int);
void pango_font_description_set_style(PangoFontDescription*, PangoStyle);
void pango_font_description_set_variant(PangoFontDescription*, PangoVariant);
void pango_font_description_set_weight(PangoFontDescription*, PangoWeight);
#define PANGO_SCALE (1024)
PangoLayout *pango_cairo_create_layout(cairo_t*);
void g_object_unref(PangoLayout*);
PangoContext *pango_cairo_create_context(cairo_t *);
void pango_cairo_show_layout(cairo_t *, PangoLayout *);
PangoFontMetrics *pango_context_get_metrics(PangoContext*, PangoFontDescription*, void*);
void pango_font_description_free(PangoFontDescription*);
int pango_font_metrics_get_approximate_char_width(PangoFontMetrics *);
int pango_font_metrics_get_ascent(PangoFontMetrics *);
int pango_font_metrics_get_descent(PangoFontMetrics *);
void pango_font_metrics_unref(PangoFontMetrics *);
PangoContext* pango_layout_get_context(PangoLayout *);
int pango_layout_get_baseline(PangoLayout *);
void pango_layout_get_extents(PangoLayout *, PangoRectangle *, PangoRectangle *);
void pango_layout_get_pixel_extents(PangoLayout *, PangoRectangle *, PangoRectangle *);
void pango_layout_set_font_description(PangoLayout *, PangoFontDescription *);
void pango_layout_set_text(PangoLayout*, const char *, int);
void pango_layout_xy_to_index(PangoLayout*, int, int, int*, int*);
void pango_layout_index_to_pos(PangoLayout*, int, PangoRectangle*);
#endif
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <xkbcommon/xkbcommon-x11.h>
#include "xcb.h"
#undef True
#undef False
#define PANE_DATA_TYPE struct xcb_data
#include "core.h"
enum my_atoms {
a_NONE = 0,
a_WM_STATE, a_STATE_FULLSCREEN,
a_WM_NAME, a_NET_WM_NAME,
a_WM_ICON_NAME, a_NET_WM_ICON_NAME,
a_WM_PROTOCOLS, a_WM_DELETE_WINDOW,
a_NET_WM_PING,
a_NET_WM_ICON,
a_WM_CLIENT_MACHINE,
a_UTF8_STRING,
NR_ATOMS
};
static const char *atom_names[NR_ATOMS] = {
[a_NONE] = "NONE",
[a_WM_STATE] = "_NET_WM_STATE",
[a_STATE_FULLSCREEN] = "_NET_WM_STATE_FULLSCREEN",
[a_WM_NAME] = "WM_NAME",
[a_NET_WM_NAME] = "_NET_WM_NAME",
[a_WM_ICON_NAME] = "WM_ICON_NAME",
[a_NET_WM_ICON_NAME] = "_NET_WM_ICON_NAME",
[a_WM_PROTOCOLS] = "WM_PROTOCOLS",
[a_WM_DELETE_WINDOW] = "WM_DELETE_WINDOW",
[a_NET_WM_PING] = "_NET_WM_PING",
[a_NET_WM_ICON] = "_NET_WM_ICON",
[a_WM_CLIENT_MACHINE] = "WM_CLIENT_MACHINE",
[a_UTF8_STRING] = "UTF8_STRING",
};
struct rgb {
double r,g,b;
};
struct xcb_data {
xcb_connection_t *conn safe;
char *display safe;
char *disp_auth;
const xcb_setup_t *setup safe;
const xcb_screen_t *screen safe;
xcb_atom_t atoms[NR_ATOMS];
long last_event;
xcb_window_t win;
xcb_visualtype_t *visual;
cairo_t *cairo safe;
cairo_surface_t *surface safe;
PangoFontDescription *fd safe;
int charwidth, lineheight;
cairo_region_t *need_update;
bool motion_blocked;
bool in_focus;
struct xkb_context *xkb;
uint8_t first_xkb_event;
int32_t xkb_device_id;
struct xkb_state *xkb_state;
struct xkb_compose_state *compose_state;
struct xkb_compose_table *compose_table;
struct xkb_keymap *xkb_keymap;
struct pids {
pid_t pid;
struct pids *next;
} *pids;
/* FIXME use hash?? */
struct panes {
struct panes *next;
struct pane *p safe;
cairo_rectangle_int_t r;
cairo_t *ctx;
struct rgb bg;
xcb_pixmap_t draw;
cairo_surface_t *surface;
cairo_region_t *need_update;
} *panes;
};
#include "core-pane.h"
/* panes->r.x is NEVER_DRAWN if the pane has not been drawn */
#define NEVER_DRAWN (-60000)
static struct map *xcb_map;
DEF_LOOKUP_CMD(xcb_handle, xcb_map);
static struct panes *get_pixmap(struct pane *home safe,
struct pane *p safe)
{
struct xcb_data *xd = home->data;
struct panes **pp, *ps;
for (pp = &xd->panes; (ps = *pp) != NULL; pp = &(*pp)->next) {
if (ps->p != p)
continue;
if (ps->r.width == p->w && ps->r.height == p->h)
return ps;
*pp = ps->next;
if (ps->r.x != NEVER_DRAWN) {
if (!xd->need_update)
xd->need_update = cairo_region_create();
cairo_region_union_rectangle(xd->need_update, &ps->r);
}
if (ps->ctx)
cairo_destroy(ps->ctx);
if (ps->surface)
cairo_surface_destroy(ps->surface);
if (ps->draw)
xcb_free_pixmap(xd->conn, ps->draw);
free(ps);
break;
}
alloc(ps, pane);
ps->p = p;
ps->r.x = ps->r.y = NEVER_DRAWN;
ps->r.width = p->w;
ps->r.height = p->h;
ps->bg.r = ps->bg.g = ps->bg.b = 0;
pane_add_notify(home, p, "Notify:Close");
ps->next = *pp;
*pp = ps;
return ps;
}
static void instantiate_pixmap(struct xcb_data *xd safe,
struct panes *ps safe)
{
ps->draw = xcb_generate_id(xd->conn);
xcb_create_pixmap(xd->conn, xd->screen->root_depth, ps->draw,
xd->win, ps->r.width, ps->r.height);
ps->surface = cairo_xcb_surface_create(
xd->conn, ps->draw, xd->visual, ps->r.width, ps->r.height);
if (!ps->surface)
goto free_ps;
ps->ctx = cairo_create(ps->surface);
if (!ps->ctx)
goto free_surface;
cairo_set_source_rgb(ps->ctx, ps->bg.r, ps->bg.g, ps->bg.b);
cairo_paint(ps->ctx);
return;
free_surface:
cairo_surface_destroy(ps->surface);
ps->surface = NULL;
free_ps:
xcb_free_pixmap(xd->conn, ps->draw);
ps->draw = 0;
}
static struct panes *find_pixmap(struct xcb_data *xd safe, struct pane *p safe,
int *xp safe, int *yp safe)
{
int x = 0, y = 0;
struct panes *ret = NULL;
while (!ret && p->parent != p) {
struct panes *ps;
for (ps = xd->panes; ps ; ps = ps->next)
if (ps->p == p) {
ret = ps;
break;
}
if (!ret) {
x += p->x;
y += p->y;
p = p->parent;
}
}
*xp = x;
*yp = y;
return ret;
}
static inline double cvt(int i)
{
return (float)i / 1000.0;
}
static void parse_attrs(
struct pane *home safe, const char *cattrs, int scale,
struct rgb *fgp, struct rgb *bgp, bool *underline,
PangoFontDescription **fdp)
{
char *attrs = strdup(cattrs ?: "");
char *ap = attrs;
char *word;
char *fg = NULL, *bg = NULL;
bool ul = False;
bool inv = False;
int size = 12*1000;
PangoFontDescription *fd = NULL;
PangoStyle style = PANGO_STYLE_NORMAL;
PangoVariant variant = PANGO_VARIANT_NORMAL;
PangoWeight weight = PANGO_WEIGHT_NORMAL;
if (fdp) {
fd = pango_font_description_new();
*fdp = fd;
pango_font_description_set_family_static(fd, "monospace");
}
while ((word = strsep(&ap, ",")) != NULL) {
if (fd && strstarts(word, "family:"))
pango_font_description_set_family(fd, word+7);
if (strcmp(word, "large") == 0)
size = 14 * 1000;
if (strcmp(word, "small") == 0)
size = 9 * 1000;
if (isdigit(word[0])) {
char *end = NULL;
double s = strtod(word, &end);
if (end && end != word && !*end)
size = trunc(s * 1000.0);
else
size = 10*1000;
}
if (strcmp(word, "oblique") == 0)
style = PANGO_STYLE_OBLIQUE;
if (strcmp(word, "italic") == 0)
style = PANGO_STYLE_ITALIC;
if (strcmp(word, "normal") == 0)
style = PANGO_STYLE_NORMAL;
if (strcmp(word, "small-caps") == 0)
variant = PANGO_VARIANT_SMALL_CAPS;
if (strcmp(word, "bold") == 0)
weight = PANGO_WEIGHT_BOLD;
if (strcmp(word, "nobold") == 0)
weight = PANGO_WEIGHT_NORMAL;
if (strstarts(word, "fg:"))
fg = word + 3;
if (strstarts(word, "bg:"))
bg = word + 3;
if (strcmp(word, "inverse") == 0)
inv = True;
if (strcmp(word, "noinverse") == 0)
inv = False;
if (strcmp(word, "underline") == 0)
ul = True;
if (strcmp(word, "nounderline") == 0)
ul = False;
}
if (inv) {
char *t = bg;
bg = fg;
fg = t;
if (!fg)
fg = "white";
if (!bg)
bg = "black";
} else if (!fg)
fg = "black";
if (fg && fgp) {
struct call_return ret = call_ret(all, "colour:map", home,
0, NULL, fg);
fgp->r = cvt(ret.i);
fgp->g = cvt(ret.i2);
fgp->b = cvt(ret.x);
} else if (fgp)
fgp->g = -1;
if (bg && bgp) {
struct call_return ret = call_ret(all, "colour:map", home,
0, NULL, bg);
bgp->r = cvt(ret.i);
bgp->g = cvt(ret.i2);
bgp->b = cvt(ret.x);
} else if (bgp)
bgp->g = -1;
if (fd) {
pango_font_description_set_size(fd, PANGO_SCALE * size /1000 * scale / 1000);
if (style != PANGO_STYLE_NORMAL)
pango_font_description_set_style(fd, style);
if (variant != PANGO_VARIANT_NORMAL)
pango_font_description_set_variant(fd, variant);
if (weight != PANGO_WEIGHT_NORMAL)
pango_font_description_set_weight(fd, weight);
}
if (underline)
*underline = ul;
free(attrs);
}
DEF_CB(cnt_disp)
{
struct call_return *cr = container_of(ci->comm, struct call_return, c);
cr->i += 1;
return 1;
}
DEF_CMD_CLOSED(xcb_close_display)
{
/* If this is only display, then refuse to close this one */
struct call_return cr;
char *nc = pane_attr_get(ci->home, "no-close");
if (nc) {
call("Message", ci->focus, 0, NULL, nc);
return 1;
}
cr.c = cnt_disp;
cr.i = 0;
call_comm("editor:notify:all-displays", ci->focus, &cr.c);
if (cr.i > 1)
return Efallthrough;
else
call("Message", ci->focus, 0, NULL,
"Cannot close only window.");
return 1;
}
static void wait_for(struct xcb_data *xd safe)
{
struct pids **pp = &xd->pids;
while (*pp) {
struct pids *p = *pp;
if (waitpid(p->pid, NULL, WNOHANG) > 0) {
*pp = p->next;
free(p);
} else
pp = &p->next;
}
}
DEF_CMD(xcb_external_viewer)
{
struct xcb_data *xd = ci->home->data;
const char *path = ci->str;
struct pids *p;
int pid;
int fd;
if (!path)
return Enoarg;
switch (pid = fork()) {
case -1:
return Efail;
case 0: /* Child */
setenv("DISPLAY", xd->display, 1);
if (xd->disp_auth)
setenv("XAUTHORITY", xd->disp_auth, 1);
fd = open("/dev/null", O_RDWR);
if (fd) {
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
}
execlp("xdg-open", "xdg-open", path, NULL);
exit(1);
default: /* parent */
p = malloc(sizeof(*p));
p->pid = pid;
p->next = xd->pids;
xd->pids = p;
break;
}
wait_for(xd);
return 1;
}
DEF_CMD(xcb_fullscreen)
{
struct xcb_data *xd = ci->home->data;
xcb_client_message_event_t msg = {};
msg.response_type = XCB_CLIENT_MESSAGE;
msg.format = 32;
msg.window = xd->win;
msg.type = xd->atoms[a_WM_STATE];
if (ci->num > 0)
msg.data.data32[0] = 1; /* ADD */
else
msg.data.data32[0] = 0; /* REMOVE */
msg.data.data32[1] = xd->atoms[a_STATE_FULLSCREEN];
msg.data.data32[2] = 0;
msg.data.data32[3] = 1; /* source indicator */
xcb_send_event(xd->conn, 0, xd->screen->root,
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
(void*)&msg);
xcb_flush(xd->conn);
return 1;
}
static void panes_free(struct xcb_data *xd safe)
{
while (xd->panes) {
struct panes *ps = xd->panes;
xd->panes = ps->next;
if (ps->ctx)
cairo_destroy(ps->ctx);
if (ps->surface)
cairo_surface_destroy(ps->surface);
if (ps->draw)
xcb_free_pixmap(xd->conn, ps->draw);
free(ps);
}
}
static void kbd_free(struct xcb_data *xd safe);
DEF_CMD_CLOSED(xcb_close)
{
struct xcb_data *xd = ci->home->data;
xcb_destroy_window(xd->conn, xd->win);
kbd_free(xd);
panes_free(xd);
pango_font_description_free(xd->fd);
cairo_destroy(xd->cairo);
cairo_device_finish(cairo_surface_get_device(xd->surface));
cairo_surface_destroy(xd->surface);
free(xd->display);
free(xd->disp_auth);
xcb_disconnect(xd->conn);
if (xd->need_update)
cairo_region_destroy(xd->need_update);
return 1;
}
DEF_CMD(xcb_clear)
{
struct xcb_data *xd = ci->home->data;
const char *attr = ci->str;
struct panes *src = NULL, *dest;
struct rgb bg;
int x=0, y=0;
cairo_rectangle_int_t r;
if (attr) {
parse_attrs(ci->home, attr, PANGO_SCALE, NULL, &bg, NULL, NULL);
if (bg.g < 0)
bg.r = bg.g = bg.b = 1.0;
} else {
src = find_pixmap(xd, ci->focus->parent, &x, &y);
x += ci->focus->x;
y += ci->focus->y;
if (!src)
bg.r = bg.g = bg.b = 1.0;
else if (src->bg.g >= 0)
bg = src->bg;
else if (src->surface == NULL)
bg.r = bg.g = bg.b = 1.0;
else
bg.g = -1;
}
dest = get_pixmap(ci->home, ci->focus);
if (!dest)
return 1;
if (bg.g >= 0) {
if (dest->ctx) {
cairo_set_source_rgb(dest->ctx, bg.r, bg.g, bg.b);
cairo_paint(dest->ctx);
}
dest->bg = bg;
} else if (src) {
if (!dest->ctx)
instantiate_pixmap(xd, dest);
if (dest->ctx) {
cairo_set_source_surface(dest->ctx, src->surface, -x, -y);
cairo_paint(dest->ctx);
dest->bg.g = -1;
}
}
pane_damaged(ci->home, DAMAGED_POSTORDER);
if (!dest->need_update)
dest->need_update = cairo_region_create();
r.x = 0;
r.y = 0;
r.width = ci->focus->w;
r.height = ci->focus->h;
cairo_region_union_rectangle(dest->need_update, &r);
return 1;
}
DEF_CMD(xcb_text_size)
{
struct xcb_data *xd = ci->home->data;
const char *attr = ci->str2 ?: "";
const char *str = ci->str ?: "";
int scale = ci->num2;
PangoLayout *layout;
PangoFontDescription *fd;
PangoRectangle log;
int baseline;
int max_bytes;
if (scale <= 0)
scale = 1000;
if (!utf8_valid(str))
str = "*INV*";
parse_attrs(ci->home, attr, scale, NULL, NULL, NULL, &fd);
/* If we use an empty string, line-height is wrong */
layout = pango_cairo_create_layout(xd->cairo);
pango_layout_set_text(layout, *str ? str : "M", -1);
pango_layout_set_font_description(layout, fd);
pango_layout_get_pixel_extents(layout, NULL, &log);
baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
if (ci->num < 0)
max_bytes = 0;
else if (log.width <= ci->num)
max_bytes = strlen(str);
else
pango_layout_xy_to_index(layout, PANGO_SCALE*ci->num,
baseline, &max_bytes, NULL);
comm_call(ci->comm2, "cb", ci->focus, max_bytes, NULL, NULL,
baseline, NULL, NULL,
str && *str ? log.width : 0,
log.height);
pango_font_description_free(fd);
g_object_unref(layout);
return 1;
}
DEF_CMD(xcb_draw_text)
{
struct xcb_data *xd = ci->home->data;
const char *str = ci->str;
const char *attr = ci->str2;
int scale = 1000;
struct panes *ps;
cairo_t *ctx;
PangoLayout *layout;
PangoFontDescription *fd;
PangoRectangle log;
struct rgb fg, bg;
bool ul;
int baseline;
int xo = 0, yo = 0;
int x,y;
if (!str)
return Enoarg;
ps = find_pixmap(xd, ci->focus, &xo, &yo);
if (!ps)
return Einval;
if (!ps->ctx)
instantiate_pixmap(xd, ps);
ps->bg.g = -1;
ctx = ps->ctx;
if (!ctx)
return Efail;
if (!utf8_valid(str))
str = "*INV*";
pane_damaged(ci->home, DAMAGED_POSTORDER);
if (ci->num2 > 0)
scale = ci->num2 * 10 / xd->charwidth;
parse_attrs(ci->home, attr, scale, &fg, &bg, &ul, &fd);
x = ci->x + xo;
y = ci->y + yo;
layout = pango_cairo_create_layout(ctx);
pango_layout_set_text(layout, str, -1);
pango_layout_set_font_description(layout, fd);
pango_layout_get_pixel_extents(layout, NULL, &log);
baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
cairo_save(ctx);
if (bg.g >= 0) {
cairo_set_source_rgb(ctx, bg.r, bg.g, bg.b);
cairo_rectangle(ctx, x+log.x, y - baseline + log.y,
log.width, log.height);
cairo_fill(ctx);
}
cairo_set_source_rgb(ctx, fg.r, fg.g, fg.b);
if (ul) {
/* Draw an underline */
cairo_rectangle(ctx, x+log.x, y+2+log.y,
log.width, 1);
cairo_fill(ctx);
}
cairo_move_to(ctx, x, y - baseline);
pango_cairo_show_layout(ctx, layout);
cairo_stroke(ctx);
if (ci->num >= 0) {
/* draw a cursor - outline box if not in-focus,
* inverse-video if it is.
*/
PangoRectangle curs;
bool in_focus = xd->in_focus;
struct pane *f = ci->focus;
double cx, cy, cw, ch;
pango_layout_index_to_pos(layout, ci->num, &curs);
if (curs.width <= 0) {
/* EOL?*/
pango_layout_set_text(layout, "M", 1);
pango_layout_get_extents(layout, NULL, &log);
curs.width = log.width;
}
while (in_focus && f->parent->parent != f &&
f->parent != ci->home) {
if (f->parent->focus != f && f->z >= 0)
in_focus = False;
f = f->parent;
}
if (!in_focus) {
/* Just an fg:rectangle around the fg:text */
/* Add half to x,y as stroke is either side of the line */
cx = x * PANGO_SCALE + curs.x + PANGO_SCALE/2;
cy = (y - baseline) * PANGO_SCALE + curs.y + PANGO_SCALE/2;
ch = curs.height - PANGO_SCALE;
cw = curs.width - PANGO_SCALE;
cairo_rectangle(ctx, cx/PANGO_SCALE, cy/PANGO_SCALE,
cw/PANGO_SCALE, ch/PANGO_SCALE);
cairo_set_line_width(ctx, 1.0);
cairo_stroke(ctx);
} else {
/* solid fd:block with txt in bg color */
cairo_rectangle(ctx,
x+curs.x/PANGO_SCALE,
y-baseline+curs.y/PANGO_SCALE,
curs.width / PANGO_SCALE,
curs.height / PANGO_SCALE);
cairo_fill(ctx);
if (ci->num < (int)strlen(str)) {
const char *cp = str + ci->num;
get_utf8(&cp, NULL);
pango_layout_set_text(layout, str + ci->num,
cp - (str + ci->num));
if (bg.g >= 0)
cairo_set_source_rgb(ctx, bg.r, bg.g, bg.b);
else
cairo_set_source_rgb(ctx, 1.0, 1.0, 1.0);
cairo_move_to(ctx,
x + curs.x / PANGO_SCALE,
y - baseline + curs.y / PANGO_SCALE);
pango_cairo_show_layout(ctx, layout);
}
}
}
cairo_restore(ctx);
pango_font_description_free(fd);
g_object_unref(layout);
return 1;
}
struct di_info {
struct command c;
MagickWand *wd safe;
int x,y,w,h;
int xo, yo;
struct panes *ps safe;
};
DEF_CB(xcb_draw_image_cb)
{
struct di_info *dii = container_of(ci->comm, struct di_info, c);
int stride;
int fmt[2];
unsigned char *buf;
cairo_surface_t *surface;
switch (ci->key[0]) {
case 'w': /* width */
return MagickGetImageWidth(dii->wd);
case 'h': /* height */
return MagickGetImageHeight(dii->wd);
case 's': /* scale */
MagickResizeImage(dii->wd, ci->num, ci->num2, BoxFilter, 1.0);
return 1;
case 'c': /* crop or cursor */
if (ci->key[1] != 'u') {
/* crop */
dii->x = ci->x;
dii->y = ci->y;
dii->w = ci->num;
dii->h = ci->num2;
return 1;
} else {
/* cursor */
cairo_rectangle(dii->ps->ctx,
ci->x + dii->xo, ci->y + dii->yo,
ci->num, ci->num2);
cairo_set_line_width(dii->ps->ctx, 1.0);
cairo_set_source_rgb(dii->ps->ctx, 1.0, 0.0, 0.0);
cairo_stroke(dii->ps->ctx);
return 1;
}
case 'd': /* draw */
if (dii->w <= 0 || dii->h <= 0)
return Efail;
stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24,
dii->w);
buf = malloc(dii->h * stride);
// Cairo expects 32bit values with A in the high byte, then RGB.
// Magick provides 8bit values in the order requests.
// So depending on byte order, a different string is needed
fmt[0] = ('A'<<24) | ('R' << 16) | ('G' << 8) | ('B' << 0);
fmt[1] = 0;
MagickExportImagePixels(dii->wd, dii->x, dii->y,
dii->w, dii->h,
(char*)fmt, CharPixel, buf);
surface = cairo_image_surface_create_for_data(
buf, CAIRO_FORMAT_ARGB32, dii->w, dii->h, stride);
cairo_set_source_surface(dii->ps->ctx, surface,
ci->num + dii->xo, ci->num2 + dii->yo);
cairo_paint(dii->ps->ctx);
cairo_surface_destroy(surface);
free(buf);
return 1;
default:
return Efail;
}
}
DEF_CMD(xcb_draw_image)
{
/* 'str' identifies the image. Options are:
* file:filename - load file from fs
* comm:command - run command collecting bytes
* 'str2' and numbers are handled by Draw:scale-image.
*/
struct xcb_data *xd = ci->home->data;
struct di_info dii;
MagickWand *wd = NULL;
struct panes *ps;
if (!ci->str)
return Enoarg;
ps = find_pixmap(xd, ci->focus, &dii.xo, &dii.yo);
if (!ps)
return Einval;
dii.ps = ps;
if (!dii.ps->ctx)
instantiate_pixmap(xd, dii.ps);
dii.ps->bg.g = -1;
if (!dii.ps->ctx)
return Efail;
if (strstarts(ci->str, "file:")) {
MagickBooleanType status;
wd = NewMagickWand();
status = MagickReadImage(wd, ci->str + 5);
if (status == MagickFalse) {
DestroyMagickWand(wd);
return Efail;
}
} else if (strstarts(ci->str, "comm:")) {
MagickBooleanType status;
struct call_return cr;
wd = NewMagickWand();
cr = call_ret(bytes, ci->str+5, ci->focus);
if (!cr.s) {
DestroyMagickWand(wd);
return Efail;
}
status = MagickReadImageBlob(wd, cr.s, cr.i);
free(cr.s);
if (status == MagickFalse) {
DestroyMagickWand(wd);
return Efail;
}
}
if (!wd)
return Einval;
MagickAutoOrientImage(wd);
dii.c = xcb_draw_image_cb;
dii.wd = wd;
dii.w = dii.h = dii.x = dii.y = dii.xo = dii.yo = 0;
call("Draw:scale-image", ci->focus,
ci->num, NULL, NULL, ci->num2, NULL, ci->str2,
ci->x, ci->y, &dii.c);
DestroyMagickWand(wd);
pane_damaged(ci->home, DAMAGED_POSTORDER);
return 1;
}
DEF_CMD(xcb_image_size)
{
MagickBooleanType status;
MagickWand *wd;
int ih, iw;
if (!ci->str)
return Enoarg;
if (strstarts(ci->str, "file:")) {
wd = NewMagickWand();
status = MagickReadImage(wd, ci->str + 5);
if (status == MagickFalse) {
DestroyMagickWand(wd);
return Efail;
}
} else if (strstarts(ci->str, "comm:")) {
struct call_return cr;
wd = NewMagickWand();
cr = call_ret(bytes, ci->str+5, ci->focus);
if (!cr.s) {
DestroyMagickWand(wd);
return Efail;
}
status = MagickReadImageBlob(wd, cr.s, cr.i);
free(cr.s);
if (status == MagickFalse) {
DestroyMagickWand(wd);
return Efail;
}
} else
return Einval;
MagickAutoOrientImage(wd);
ih = MagickGetImageHeight(wd);
iw = MagickGetImageWidth(wd);
DestroyMagickWand(wd);
comm_call(ci->comm2, "callback:size", ci->focus,
0, NULL, NULL, 0, NULL, NULL,
iw, ih);
return 1;
}
static struct panes *sort_split(struct panes *p)
{
/* consider 'p' to be a list of panes with
* ordered subsets (ordered by p->abs_z).
* Remove every other such subset and return them
* linked together.
* If p is ordered, this means we return NULL.
*/
struct panes *ret, **end = &ret;
struct panes *next;
for (; p && p->next; p = next) {