-
Notifications
You must be signed in to change notification settings - Fork 1
/
st.c
1385 lines (1161 loc) · 32.6 KB
/
st.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 for licence details. */
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xft/Xft.h>
#include <fontconfig/fontconfig.h>
#include <gio/gio.h>
#include "term.h"
#define USAGE \
"st " VERSION " (c) 2010-2013 st engineers\n" \
"usage: st [-v] [-c class] [-g geometry] [-o file]" \
" [-t title] [-w windowid] [-e command ...]\n"
/* XEMBED messages */
#define XEMBED_FOCUS_IN 4
#define XEMBED_FOCUS_OUT 5
/* Arbitrary sizes */
#define XK_ANY_MOD UINT_MAX
#define XK_NO_MOD 0
#define XK_SWITCH_MOD (1<<13)
/* macros */
#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
struct st_font {
XftFont *match;
FcFontSet *set;
FcPattern *pattern;
};
struct st_fontcache {
XftFont *font;
unsigned c;
enum {
FRC_NORMAL,
FRC_ITALIC,
FRC_BOLD,
FRC_ITALICBOLD
} flags;
};
struct st_key {
KeySym k;
unsigned mask;
char s[ESC_BUF_SIZ];
/* three valued logic variables: 0 indifferent, 1 on, -1 off */
signed char appkey; /* application keypad */
signed char appcursor; /* application cursor */
signed char crlf; /* crlf mode */
};
union st_arg {
int i;
unsigned int ui;
float f;
const void *v;
};
struct st_window;
struct st_shortcut {
unsigned int mod;
KeySym keysym;
void (*func) (struct st_window *, const union st_arg *);
const union st_arg arg;
};
/* function definitions used in config.h */
static void clippaste(struct st_window *, const union st_arg *);
static void selpaste(struct st_window *, const union st_arg *);
static void numlock(struct st_window *, const union st_arg *);
static void xzoom(struct st_window *, const union st_arg *);
/* Config.h for applying patches and the configuration. */
#include "config.h"
struct st_window {
struct st_term term;
/* Graphic info */
XftColor col[ARRAY_SIZE(colorname) < 256 ? 256 : ARRAY_SIZE(colorname)];
GC gc;
Display *dpy;
Colormap cmap;
Window win;
Drawable buf;
Atom xembed;
Atom wmdeletewin;
XIM xim;
XIC xic;
XftDraw *draw;
Visual *vis;
Atom selection;
char *default_title;
char *class;
char *embed;
GSettings *settings;
unsigned borderpx;
unsigned fps;
unsigned doubleclicktimeout;
unsigned tripleclicktimeout;
struct st_font font, bfont, ifont, ibfont;
int fontzoom;
struct st_fontcache fontcache[32];
int scr;
bool isfixed; /* is fixed geometry? */
int fx, fy; /* fixed geometry */
struct coord winsize;
struct coord fixedsize; /* kill? */
struct coord charsize;
struct timeval mousedown_time;
struct coord mousedown_pos;
struct timeval mouseup[3];
unsigned sel_type;
unsigned mousedown:1;
unsigned visible:1;
unsigned focused:1;
};
/* X utility code */
static unsigned short sixd_to_16bit(int x)
{
return x == 0 ? 0 : 0x3737 + 0x2828 * x;
}
static bool match(unsigned mask, unsigned state)
{
state &= ~(ignoremod);
if (mask == XK_NO_MOD && state)
return false;
if (mask != XK_ANY_MOD && mask != XK_NO_MOD && !state)
return false;
if ((state & mask) != state)
return false;
return true;
}
static int xsetcolorname(struct st_term *term,
int x, const char *name)
{
struct st_window *xw = container_of(term, struct st_window, term);
XRenderColor color = {.alpha = 0xffff };
XftColor colour;
if (x < 0 || x > ARRAY_SIZE(colorname))
return -1;
if (!name) {
if (16 <= x && x < 16 + 216) {
int r = (x - 16) / 36, g = ((x - 16) % 36) / 6, b =
(x - 16) % 6;
color.red = sixd_to_16bit(r);
color.green = sixd_to_16bit(g);
color.blue = sixd_to_16bit(b);
if (!XftColorAllocValue(xw->dpy, xw->vis,
xw->cmap, &color, &colour))
return 0; /* something went wrong */
xw->col[x] = colour;
return 1;
} else if (16 + 216 <= x && x < 256) {
color.red = color.green = color.blue =
0x0808 + 0x0a0a * (x - (16 + 216));
if (!XftColorAllocValue(xw->dpy, xw->vis,
xw->cmap, &color, &colour))
return 0; /* something went wrong */
xw->col[x] = colour;
return 1;
} else {
name = colorname[x];
}
}
if (!XftColorAllocName(xw->dpy, xw->vis, xw->cmap, name, &colour))
return 0;
xw->col[x] = colour;
return 1;
}
static void xsettitle(struct st_term *term, char *title)
{
struct st_window *xw = container_of(term, struct st_window, term);
if (!title)
title = xw->default_title;
XTextProperty prop;
Xutf8TextListToTextProperty(xw->dpy, &title, 1, XUTF8StringStyle,
&prop);
XSetWMName(xw->dpy, xw->win, &prop);
}
static void xseturgency(struct st_term *term, int add)
{
struct st_window *xw = container_of(term, struct st_window, term);
if (xw->focused)
return;
XWMHints *h = XGetWMHints(xw->dpy, xw->win);
h->flags =
add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
XSetWMHints(xw->dpy, xw->win, h);
XFree(h);
}
static void xsetsel(struct st_window *xw)
{
Atom clipboard;
XSetSelectionOwner(xw->dpy, XA_PRIMARY, xw->win, CurrentTime);
clipboard = XInternAtom(xw->dpy, "CLIPBOARD", 0);
XSetSelectionOwner(xw->dpy, clipboard, xw->win, CurrentTime);
}
/* Selection code */
static void selnotify(struct st_window *xw, XEvent *e)
{
unsigned long nitems, ofs, rem;
int format;
unsigned char *data;
Atom type;
ofs = 0;
do {
if (XGetWindowProperty
(xw->dpy, xw->win, XA_PRIMARY, ofs, BUFSIZ / 4, False,
AnyPropertyType, &type, &format, &nitems, &rem,
&data)) {
fprintf(stderr, "Clipboard allocation failed\n");
return;
}
ttywrite(&xw->term, (const char *) data, nitems * format / 8);
XFree(data);
/* number of 32-bit chunks returned */
ofs += nitems * format / 32;
} while (rem > 0);
}
static void selpaste(struct st_window *xw, const union st_arg *dummy)
{
XConvertSelection(xw->dpy, XA_PRIMARY, xw->selection,
XA_PRIMARY, xw->win, CurrentTime);
}
static void clippaste(struct st_window *xw, const union st_arg *dummy)
{
Atom clipboard;
clipboard = XInternAtom(xw->dpy, "CLIPBOARD", 0);
XConvertSelection(xw->dpy, clipboard, xw->selection,
XA_PRIMARY, xw->win, CurrentTime);
}
static void selclear(struct st_window *xw, XEvent *e)
{
term_sel_stop(&xw->term);
}
static void selrequest(struct st_window *xw, XEvent *e)
{
struct st_selection *sel = &xw->term.sel;
XSelectionRequestEvent *xsre;
XSelectionEvent xev;
Atom xa_targets, string;
xsre = (XSelectionRequestEvent *) e;
xev.type = SelectionNotify;
xev.requestor = xsre->requestor;
xev.selection = xsre->selection;
xev.target = xsre->target;
xev.time = xsre->time;
/* reject */
xev.property = None;
xa_targets = XInternAtom(xw->dpy, "TARGETS", 0);
if (xsre->target == xa_targets) {
/* respond with the supported type */
string = xw->selection;
XChangeProperty(xsre->display, xsre->requestor,
xsre->property, XA_ATOM, 32,
PropModeReplace, (unsigned char *) & string, 1);
xev.property = xsre->property;
} else if (xsre->target == xw->selection && sel->clip != NULL) {
XChangeProperty(xsre->display, xsre->requestor,
xsre->property, xsre->target, 8,
PropModeReplace, (unsigned char *) sel->clip,
strlen(sel->clip));
xev.property = xsre->property;
}
/* all done, send a notification to the listener */
if (!XSendEvent
(xsre->display, xsre->requestor, True, 0, (XEvent *) & xev))
fprintf(stderr, "Error sending SelectionNotify event\n");
}
/* Screen drawing code */
static void xclear(struct st_window *xw, XftColor *color,
struct coord pos, unsigned charlen,
bool clear_border)
{
unsigned x1 = xw->charsize.x * pos.x + xw->borderpx;
unsigned x2 = xw->charsize.x * charlen;
unsigned y1 = xw->charsize.y * pos.y + xw->borderpx;
unsigned y2 = xw->charsize.y;
if (clear_border) {
if (!pos.x) {
x2 += x1;
x1 = 0;
}
if (pos.x + charlen == xw->term.size.x)
x2 = xw->winsize.x - x1;
if (!pos.y) {
y2 += y1;
y1 = 0;
}
if (pos.y + 1 == xw->term.size.y)
y2 = xw->winsize.y - y1;
}
/* Clean up the region we want to draw to. */
XftDrawRect(xw->draw, color, x1, y1, x2, y2);
}
static XftColor *reverse_color(struct st_window *xw, XftColor *color,
XftColor *def, XftColor *defreverse,
XftColor *reverse)
{
if (color == def) {
return defreverse;
} else {
XRenderColor t;
t.red = ~color->color.red;
t.green = ~color->color.green;
t.blue = ~color->color.blue;
t.alpha = color->color.alpha;
XftColorAllocValue(xw->dpy, xw->vis, xw->cmap, &t, reverse);
return reverse;
}
}
static XftFont *find_font(struct st_window *xw, struct st_font *font,
unsigned flags, long u8char)
{
FcFontSet *fcsets[] = { font->set };
FcPattern *fcpattern, *fontpattern;
FcCharSet *fccharset;
FcResult fcres;
XftFont *xfont;
struct st_fontcache *fc;
/* Search the font cache. */
for (fc = xw->fontcache;
fc < xw->fontcache + ARRAY_SIZE(xw->fontcache) && fc->font;
fc++)
if (fc->flags == flags && fc->c == u8char)
return fc->font;
/*
* Nothing was found in the cache. Now use
* some dozen of Fontconfig calls to get the
* font for one single character.
*/
fcpattern = FcPatternDuplicate(font->pattern);
fccharset = FcCharSetCreate();
FcCharSetAddChar(fccharset, u8char);
FcPatternAddCharSet(fcpattern, FC_CHARSET,
fccharset);
FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
FcConfigSubstitute(0, fcpattern, FcMatchPattern);
FcDefaultSubstitute(fcpattern);
fontpattern = FcFontSetMatch(NULL, fcsets, 1, fcpattern, &fcres);
xfont = XftFontOpenPattern(xw->dpy, fontpattern);
fc = &xw->fontcache[ARRAY_SIZE(xw->fontcache) - 1];
if (fc->font)
XftFontClose(xw->dpy, fc->font);
fc = xw->fontcache;
memmove(fc + 1, fc,
(ARRAY_SIZE(xw->fontcache) - 1) * sizeof(*fc));
fc->font = xfont;
fc->c = u8char;
fc->flags = flags;
FcCharSetDestroy(fccharset);
FcPatternDestroy(fcpattern);
return xfont;
}
static void do_xdraw_glyphs(struct st_window *xw, struct coord pos,
struct st_glyph base, struct st_glyph *glyphs,
unsigned nglyphs, struct st_font *font,
unsigned frcflags, XftColor *fg)
{
unsigned winx = xw->borderpx + pos.x * xw->charsize.x, xp = winx;
unsigned winy = xw->borderpx + pos.y * xw->charsize.y;
unsigned xglyphs[1024], nxglyphs = 0, i = 0, ucs;
while (i < nglyphs) {
bool found = true;
while (i < nglyphs && nxglyphs < ARRAY_SIZE(xglyphs)) {
ucs = glyphs[i].c ?: ' ';
xglyphs[nxglyphs] = XftCharIndex(xw->dpy, font->match, ucs);
if (!xglyphs[nxglyphs]) {
found = false;
break;
}
nxglyphs++;
i++;
}
if (nxglyphs) {
XftDrawGlyphs(xw->draw, fg, font->match, xp,
winy + font->match->ascent,
xglyphs, nxglyphs);
xp += xw->charsize.x * nxglyphs;
nxglyphs = 0;
}
if (!found) {
unsigned glyph;
XftFont *xfont;
xfont = find_font(xw, font, frcflags, ucs);
if (xfont) {
glyph = XftCharIndex(xw->dpy, xfont, ucs);
} else {
xfont = font->match;
glyph = XftCharIndex(xw->dpy, xfont, 0xFFFD) ?:
XftCharIndex(xw->dpy, xfont, ' ');
}
XftDrawGlyphs(xw->draw, fg, xfont, xp,
winy + xfont->ascent, &glyph, 1);
xp += xw->charsize.x;
i++;
}
}
if (base.underline)
XftDrawRect(xw->draw, fg, winx, winy + font->match->ascent + 1,
nglyphs * xw->charsize.x, 1);
}
static void xdraw_glyphs(struct st_window *xw, struct coord pos,
struct st_glyph base, struct st_glyph *glyphs,
unsigned nglyphs, bool clear_border)
{
unsigned frcflags = FRC_NORMAL;
struct st_font *font = &xw->font;
XftColor *fg = &xw->col[base.fg];
XftColor *bg = &xw->col[base.bg];
XftColor revfg, revbg;
if (base.bold) {
if (BETWEEN(base.fg, 0, 7)) {
/* basic system colors */
fg = &xw->col[base.fg + 8];
} else if (BETWEEN(base.fg, 16, 195)) {
/* 256 colors */
fg = &xw->col[base.fg + 36];
} else if (BETWEEN(base.fg, 232, 251)) {
/* greyscale */
fg = &xw->col[base.fg + 4];
}
/*
* Those ranges will not be brightened:
* 8 - 15 – bright system colors
* 196 - 231 – highest 256 color cube
* 252 - 255 – brightest colors in greyscale
*/
font = &xw->bfont;
frcflags = FRC_BOLD;
}
if (base.italic) {
font = &xw->ifont;
frcflags = FRC_ITALIC;
}
if (base.italic && base.bold) {
font = &xw->ibfont;
frcflags = FRC_ITALICBOLD;
}
if (xw->term.reverse) {
fg = reverse_color(xw, fg, &xw->col[defaultfg],
&xw->col[defaultbg], &revfg);
bg = reverse_color(xw, bg, &xw->col[defaultbg],
&xw->col[defaultfg], &revbg);
}
if (base.reverse)
swap(bg, fg);
xclear(xw, bg, pos, nglyphs, clear_border);
do_xdraw_glyphs(xw, pos, base, glyphs, nglyphs, font, frcflags, fg);
}
static void xdrawcursor(struct st_window *xw)
{
struct st_glyph g;
if (xw->term.hide)
return;
g.c = term_pos(&xw->term, xw->term.c.pos)->c;
g.cmp = 0;
g.fg = defaultbg;
g.bg = defaultcs;
g.reverse = xw->term.reverse;
if (g.reverse) {
unsigned t = g.fg;
g.fg = g.bg;
g.bg = t;
}
if (xw->focused) {
xdraw_glyphs(xw, xw->term.c.pos, g, &g, 1, false);
} else {
XSetForeground(xw->dpy, xw->gc, xw->col[defaultcs].pixel);
XDrawRectangle(xw->dpy, xw->buf, xw->gc,
xw->borderpx + xw->term.c.pos.x * xw->charsize.x,
xw->borderpx + xw->term.c.pos.y * xw->charsize.y,
xw->charsize.x, xw->charsize.y);
}
}
static struct st_glyph sel_glyph(struct st_window *xw, unsigned x, unsigned y)
{
struct st_glyph ret = xw->term.line[y][x];
if (term_selected(&xw->term.sel, x, y))
ret.reverse ^= 1;
return ret;
}
static void draw(struct st_window *xw)
{
struct coord pos;
xw->term.dirty = false;
for (pos.y = 0; pos.y < xw->term.size.y; pos.y++) {
pos.x = 0;
while (pos.x < xw->term.size.x) {
unsigned x2 = pos.x + 1;
struct st_glyph base = sel_glyph(xw, pos.x, pos.y);
while (x2 < xw->term.size.x &&
base.cmp == sel_glyph(xw, x2, pos.y).cmp)
x2++;
xdraw_glyphs(xw, pos, base,
term_pos(&xw->term, pos), x2 - pos.x,
true);
pos.x = x2;
}
}
xdrawcursor(xw);
XCopyArea(xw->dpy, xw->buf, xw->win, xw->gc,
0, 0, xw->winsize.x, xw->winsize.y, 0, 0);
XSetForeground(xw->dpy, xw->gc,
xw->col[xw->term.reverse ? defaultfg : defaultbg].pixel);
XFlush(xw->dpy);
}
/* Keyboard input */
static char *kmap(struct st_term *term, KeySym k, unsigned state)
{
unsigned mask;
struct st_key *kp;
int i;
/* Check for mapped keys out of X11 function keys. */
for (i = 0; i < ARRAY_SIZE(mappedkeys); i++) {
if (mappedkeys[i] == k)
break;
}
if (i == ARRAY_SIZE(mappedkeys)) {
if ((k & 0xFFFF) < 0xFD00)
return NULL;
}
for (kp = key; kp < key + ARRAY_SIZE(key); kp++) {
mask = kp->mask;
if (kp->k != k)
continue;
if (!match(mask, state))
continue;
if (kp->appkey > 0) {
if (!term->appkeypad)
continue;
if (term->numlock && kp->appkey == 2)
continue;
} else if (kp->appkey < 0 && term->appkeypad)
continue;
if ((kp->appcursor < 0 && term->appcursor) ||
(kp->appcursor > 0 && !term->appcursor))
continue;
if ((kp->crlf < 0 && term->crlf) ||
(kp->crlf > 0 && !term->crlf))
continue;
return kp->s;
}
return NULL;
}
static void kpress(struct st_window *xw, XEvent *ev)
{
XKeyEvent *e = &ev->xkey;
KeySym ksym;
char xstr[31], buf[32], *customkey, *cp = buf;
int len;
Status status;
const struct st_shortcut *bp;
if (xw->term.kbdlock)
return;
len = XmbLookupString(xw->xic, e, xstr, sizeof(xstr),
&ksym, &status);
e->state &= ~Mod2Mask;
/* 1. shortcuts */
for (bp = shortcuts; bp < shortcuts + ARRAY_SIZE(shortcuts); bp++) {
if (ksym == bp->keysym && match(bp->mod, e->state)) {
bp->func(xw, &(bp->arg));
return;
}
}
/* 2. custom keys from config.h */
if ((customkey = kmap(&xw->term, ksym, e->state))) {
len = strlen(customkey);
memcpy(buf, customkey, len);
/* 2. hardcoded (overrides X lookup) */
} else {
if (len == 0)
return;
if (len == 1 && e->state & Mod1Mask)
*cp++ = '\033';
memcpy(cp, xstr, len);
len = cp - buf + len;
}
ttywrite(&xw->term, buf, len);
if (xw->term.echo)
term_echo(&xw->term, buf, len);
}
/* Mouse code */
static struct coord mouse_pos(struct st_window *xw, XEvent *ev)
{
return (struct coord) {
.x = min((ev->xbutton.x - xw->borderpx) / xw->charsize.x,
xw->term.size.x - 1),
.y = min((ev->xbutton.y - xw->borderpx) / xw->charsize.y,
xw->term.size.y - 1),
};
}
static void bpress(struct st_window *xw, XEvent *ev)
{
struct st_term *term = &xw->term;
if (term->mousebtn || term->mousemotion) {
term_mousereport(term, mouse_pos(xw, ev),
ev->xbutton.type,
ev->xbutton.button,
ev->xbutton.state);
return;
}
switch (ev->xbutton.button) {
case Button1:
gettimeofday(&xw->mousedown_time, NULL);
xw->mousedown_pos = mouse_pos(xw, ev);
xw->mousedown = 1;
/* Clear previous selection */
term_sel_stop(term);
/* Get ready for another */
xw->sel_type = SEL_REGULAR;
for (unsigned i = 1; i < ARRAY_SIZE(selmasks); i++)
if (match(selmasks[i],
ev->xbutton.state & ~Button1Mask)) {
xw->sel_type = i;
break;
}
break;
case Button4:
ttywrite(term, "\031", 1);
break;
case Button5:
ttywrite(term, "\005", 1);
break;
}
}
static void brelease(struct st_window *xw, XEvent *ev)
{
struct st_term *term = &xw->term;
struct st_selection *sel = &term->sel;
if (term->mousebtn || term->mousemotion) {
term_mousereport(term, mouse_pos(xw, ev),
ev->xbutton.type,
ev->xbutton.button,
ev->xbutton.state);
return;
}
switch (ev->xbutton.button) {
case Button1:
xw->mousedown = 0;
memmove(xw->mouseup + 1,
xw->mouseup, sizeof(struct timeval) * 2);
gettimeofday(xw->mouseup, NULL);
struct coord end = mouse_pos(xw, ev);
if (!(xw->mousedown_pos.x == end.x &&
xw->mousedown_pos.y == end.y))
term_sel_update(term, xw->sel_type,
xw->mousedown_pos, end);
else if (TIMEDIFF(xw->mouseup[0], xw->mouseup[2]) <
xw->tripleclicktimeout)
term_sel_line(term, end);
else if (TIMEDIFF(xw->mouseup[0], xw->mouseup[1]) <
xw->doubleclicktimeout)
term_sel_word(term, end);
else if (TIMEDIFF(xw->mouseup[0], xw->mousedown_time) <
xw->doubleclicktimeout)
term_sel_stop(term);
if (sel->clip)
xsetsel(xw);
break;
case Button2:
selpaste(xw, NULL);
break;
}
}
static void bmotion(struct st_window *xw, XEvent *ev)
{
struct st_term *term = &xw->term;
if (term->mousebtn || term->mousemotion) {
term_mousereport(term, mouse_pos(xw, ev),
ev->xbutton.type,
ev->xbutton.button,
ev->xbutton.state);
return;
}
if (xw->mousedown)
term_sel_update(term, xw->sel_type,
xw->mousedown_pos, mouse_pos(xw, ev));
}
/* Resizing code */
static void xresize(struct st_window *xw, int col, int row)
{
XFreePixmap(xw->dpy, xw->buf);
xw->buf = XCreatePixmap(xw->dpy, xw->win,
xw->winsize.x, xw->winsize.y,
DefaultDepth(xw->dpy, xw->scr));
XSetForeground(xw->dpy, xw->gc,
xw->col[xw->term.reverse ? defaultfg : defaultbg].
pixel);
XFillRectangle(xw->dpy, xw->buf, xw->gc, 0, 0,
xw->winsize.x, xw->winsize.y);
XftDrawChange(xw->draw, xw->buf);
}
static void cresize(struct st_window *xw, unsigned width, unsigned height)
{
struct coord size;
if (width != 0)
xw->winsize.x = width;
if (height != 0)
xw->winsize.y = height;
size.x = (xw->winsize.x - 2 * xw->borderpx) / xw->charsize.x;
size.y = (xw->winsize.y - 2 * xw->borderpx) / xw->charsize.y;
/* XXX: should probably be elsewhere */
xw->term.ttysize.x = max(1U, size.x * xw->charsize.x);
xw->term.ttysize.y = max(1U, size.y * xw->charsize.y);
term_resize(&xw->term, size);
xresize(xw, size.x, size.y);
}
static void resize(struct st_window *xw, XEvent *ev)
{
if (ev->xconfigure.width == xw->winsize.x &&
ev->xconfigure.height == xw->winsize.y)
return;
cresize(xw, ev->xconfigure.width, ev->xconfigure.height);
}
/* Start of st */
static void xloadcolors(struct st_window *xw)
{
int i, r, g, b;
XRenderColor color = {.alpha = 0xffff };
/* load colors [0-15] colors and [256-ARRAY_SIZE(colorname)[ (config.h) */
for (i = 0; i < ARRAY_SIZE(colorname); i++) {
if (!colorname[i])
continue;
if (!XftColorAllocName(xw->dpy, xw->vis, xw->cmap,
colorname[i], &xw->col[i]))
die("Could not allocate color '%s'\n", colorname[i]);
}
/* load colors [16-255] ; same colors as xterm */
for (i = 16, r = 0; r < 6; r++) {
for (g = 0; g < 6; g++) {
for (b = 0; b < 6; b++) {
color.red = sixd_to_16bit(r);
color.green = sixd_to_16bit(g);
color.blue = sixd_to_16bit(b);
if (!XftColorAllocValue(xw->dpy, xw->vis,
xw->cmap, &color,
&xw->col[i]))
die("Could not allocate color %d\n", i);
i++;
}
}
}
for (r = 0; r < 24; r++, i++) {
color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
if (!XftColorAllocValue(xw->dpy, xw->vis, xw->cmap,
&color, &xw->col[i]))
die("Could not allocate color %d\n", i);
}
}
static void xhints(struct st_window *xw)
{
XClassHint class = { xw->class, TERMNAME };
XWMHints wm = {.flags = InputHint,.input = 1 };
XSizeHints *sizeh = NULL;
sizeh = XAllocSizeHints();
if (xw->isfixed == False) {
sizeh->flags = PSize | PResizeInc | PBaseSize;
sizeh->width = xw->winsize.x;
sizeh->height = xw->winsize.y;
sizeh->width_inc = xw->charsize.x;
sizeh->height_inc = xw->charsize.y;
sizeh->base_height = 2 * xw->borderpx;
sizeh->base_width = 2 * xw->borderpx;
} else {
sizeh->flags = PMaxSize | PMinSize;
sizeh->min_width = sizeh->max_width = xw->fixedsize.x;
sizeh->min_height = sizeh->max_height = xw->fixedsize.y;
}
XSetWMProperties(xw->dpy, xw->win, NULL, NULL, NULL,
0, sizeh, &wm, &class);
XFree(sizeh);
}
static int xloadfont(struct st_window *xw, struct st_font *f,
FcPattern *pattern)
{
FcPattern *match;
FcResult result;
match = FcFontMatch(NULL, pattern, &result);
if (!match)
return 1;
if (!(f->set = FcFontSort(0, match, FcTrue, 0, &result))) {
FcPatternDestroy(match);
return 1;
}
if (!(f->match = XftFontOpenPattern(xw->dpy, match))) {
FcPatternDestroy(match);
return 1;
}
f->pattern = FcPatternDuplicate(pattern);
return 0;
}
static void xloadfonts(struct st_window *xw, int zoom)
{
FcPattern *pattern;
double pixelsize;
char *fontstr = g_settings_get_string(xw->settings, "font");
if (fontstr[0] == '-')
pattern = XftXlfdParse(fontstr, False, False);
else
pattern = FcNameParse((FcChar8 *) fontstr);
if (!pattern)
die("st: can't open font %s\n", fontstr);
FcConfigSubstitute(0, pattern, FcMatchPattern);
FcDefaultSubstitute(pattern);
if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE,
0, &pixelsize) == FcResultMatch)
FcPatternDel(pattern, FC_PIXEL_SIZE);
else
/*
* Default font size is 12, if none given. This is to
* have a known usedfontsize value.
*/
pixelsize = 12;
pixelsize *= exp((double) zoom / 8);
FcPatternAddDouble(pattern, FC_PIXEL_SIZE, pixelsize);
if (xloadfont(xw, &xw->font, pattern))
die("st: can't open font %s\n", fontstr);
/* Setting character width and height. */
xw->charsize.x = xw->font.match->max_advance_width;
xw->charsize.y = xw->font.match->height;