diff options
-rw-r--r-- | config.h | 22 | ||||
-rw-r--r-- | st.c | 168 |
2 files changed, 89 insertions, 101 deletions
@@ -6,7 +6,7 @@ | |||
6 | #define LINESPACE 1 /* additional pixel between each line */ | 6 | #define LINESPACE 1 /* additional pixel between each line */ |
7 | 7 | ||
8 | /* Terminal colors */ | 8 | /* Terminal colors */ |
9 | static char* colorname[] = { | 9 | static const char *colorname[] = { |
10 | "black", | 10 | "black", |
11 | "red", | 11 | "red", |
12 | "green", | 12 | "green", |
@@ -26,14 +26,14 @@ static char* colorname[] = { | |||
26 | 26 | ||
27 | 27 | ||
28 | /* special keys */ | 28 | /* special keys */ |
29 | static Key key[] = { | 29 | static const char *key[] = { |
30 | { XK_Delete, "\033[3~" }, | 30 | [XK_Delete] = "\033[3~", |
31 | { XK_Home, "\033[1~" }, | 31 | [XK_Home] = "\033[1~", |
32 | { XK_End, "\033[4~" }, | 32 | [XK_End] = "\033[4~", |
33 | { XK_Prior, "\033[5~" }, | 33 | [XK_Prior] = "\033[5~", |
34 | { XK_Next, "\033[6~" }, | 34 | [XK_Next] = "\033[6~", |
35 | { XK_Left, "\033[D" }, | 35 | [XK_Left] = "\033[D", |
36 | { XK_Right, "\033[C" }, | 36 | [XK_Right] = "\033[C", |
37 | { XK_Up, "\033[A" }, | 37 | [XK_Up] = "\033[A", |
38 | { XK_Down, "\033[B" }, | 38 | [XK_Down] = "\033[B", |
39 | }; | 39 | }; |
@@ -1,5 +1,5 @@ | |||
1 | /* See LICENSE for licence details. */ | 1 | /* See LICENSE for licence details. */ |
2 | #define _XOPEN_SOURCE | 2 | #define _XOPEN_SOURCE 600 |
3 | #include <ctype.h> | 3 | #include <ctype.h> |
4 | #include <errno.h> | 4 | #include <errno.h> |
5 | #include <fcntl.h> | 5 | #include <fcntl.h> |
@@ -107,61 +107,68 @@ typedef struct { | |||
107 | GC gc; | 107 | GC gc; |
108 | } DC; | 108 | } DC; |
109 | 109 | ||
110 | void die(const char *errstr, ...); | 110 | static void die(const char *errstr, ...); |
111 | void draw(int); | 111 | static void draw(int); |
112 | void execsh(void); | 112 | static void execsh(void); |
113 | void sigchld(int); | 113 | static void sigchld(int); |
114 | char* kmap(KeySym); | 114 | static void run(void); |
115 | void kpress(XKeyEvent *); | 115 | |
116 | void resize(XEvent *); | 116 | static int escaddc(char); |
117 | void run(void); | 117 | static int escfinal(char); |
118 | 118 | static void escdump(void); | |
119 | int escaddc(char); | 119 | static void eschandle(void); |
120 | int escfinal(char); | 120 | static void escparse(void); |
121 | void escdump(void); | 121 | static void escreset(void); |
122 | void eschandle(void); | 122 | |
123 | void escparse(void); | 123 | static void tclearregion(int, int, int, int); |
124 | void escreset(void); | 124 | static void tcpos(int); |
125 | 125 | static void tcursor(int); | |
126 | void tclearregion(int, int, int, int); | 126 | static void tdeletechar(int); |
127 | void tcpos(int); | 127 | static void tdeleteline(int); |
128 | void tcursor(int); | 128 | static void tdump(void); |
129 | void tdeletechar(int); | 129 | static void tinsertblank(int); |
130 | void tdeleteline(int); | 130 | static void tinsertblankline(int); |
131 | void tdump(void); | 131 | static void tmoveto(int, int); |
132 | void tinsertblank(int); | 132 | static void tnew(int, int); |
133 | void tinsertblankline(int); | 133 | static void tnewline(void); |
134 | void tmoveto(int, int); | 134 | static void tputc(char); |
135 | void tnew(int, int); | 135 | static void tputs(char*, int); |
136 | void tnewline(void); | 136 | static void tresize(int, int); |
137 | void tputc(char); | 137 | static void tscroll(void); |
138 | void tputs(char*, int); | 138 | static void tsetattr(int*, int); |
139 | void tresize(int, int); | 139 | static void tsetchar(char); |
140 | void tscroll(void); | 140 | static void tsetscroll(int, int); |
141 | void tsetattr(int*, int); | 141 | |
142 | void tsetchar(char); | 142 | static void ttynew(void); |
143 | void tsetscroll(int, int); | 143 | static void ttyread(void); |
144 | 144 | static void ttyresize(int, int); | |
145 | void ttynew(void); | 145 | static void ttywrite(const char *, size_t); |
146 | void ttyread(void); | 146 | |
147 | void ttyresize(int, int); | 147 | static unsigned long xgetcol(const char *); |
148 | void ttywrite(char *, size_t); | 148 | static void xclear(int, int, int, int); |
149 | 149 | static void xcursor(int); | |
150 | unsigned long xgetcol(const char *); | 150 | static void xdrawc(int, int, Glyph); |
151 | void xclear(int, int, int, int); | 151 | static void xinit(void); |
152 | void xcursor(int); | 152 | static void xscroll(void); |
153 | void xdrawc(int, int, Glyph); | 153 | |
154 | void xinit(void); | 154 | static void expose(XEvent *); |
155 | void xscroll(void); | 155 | static void kpress(XEvent *); |
156 | static void resize(XEvent *); | ||
157 | |||
158 | static void (*handler[LASTEvent])(XEvent *) = { | ||
159 | [KeyPress] = kpress, | ||
160 | [Expose] = expose, | ||
161 | [ConfigureNotify] = resize | ||
162 | }; | ||
156 | 163 | ||
157 | /* Globals */ | 164 | /* Globals */ |
158 | DC dc; | 165 | static DC dc; |
159 | XWindow xw; | 166 | static XWindow xw; |
160 | Term term; | 167 | static Term term; |
161 | Escseq escseq; | 168 | static Escseq escseq; |
162 | int cmdfd; | 169 | static int cmdfd; |
163 | pid_t pid; | 170 | static pid_t pid; |
164 | int running; | 171 | static int running; |
165 | 172 | ||
166 | void | 173 | void |
167 | die(const char *errstr, ...) { | 174 | die(const char *errstr, ...) { |
@@ -259,7 +266,7 @@ ttyread(void) { | |||
259 | } | 266 | } |
260 | 267 | ||
261 | void | 268 | void |
262 | ttywrite(char *s, size_t n) { | 269 | ttywrite(const char *s, size_t n) { |
263 | if(write(cmdfd, s, n) == -1) | 270 | if(write(cmdfd, s, n) == -1) |
264 | die("write error on tty: %s\n", SERRNO); | 271 | die("write error on tty: %s\n", SERRNO); |
265 | } | 272 | } |
@@ -997,29 +1004,25 @@ draw(int redraw_all) { | |||
997 | xcursor(CSdraw); | 1004 | xcursor(CSdraw); |
998 | } | 1005 | } |
999 | 1006 | ||
1000 | char* | 1007 | void |
1001 | kmap(KeySym k) { | 1008 | expose(XEvent *ev) { |
1002 | int i; | 1009 | draw(SCredraw); |
1003 | for(i = 0; i < LEN(key); i++) | ||
1004 | if(key[i].k == k) | ||
1005 | return (char*)key[i].s; | ||
1006 | return NULL; | ||
1007 | } | 1010 | } |
1008 | 1011 | ||
1009 | void | 1012 | void |
1010 | kpress(XKeyEvent *e) { | 1013 | kpress(XEvent *ev) { |
1014 | XKeyEvent *e = &ev->xkey; | ||
1011 | KeySym ksym; | 1015 | KeySym ksym; |
1012 | char buf[32]; | 1016 | char buf[32]; |
1013 | int len; | 1017 | int len; |
1014 | int meta; | 1018 | int meta; |
1015 | int shift; | 1019 | int shift; |
1016 | char* skmap; | ||
1017 | 1020 | ||
1018 | meta = e->state & Mod1Mask; | 1021 | meta = e->state & Mod1Mask; |
1019 | shift = e->state & ShiftMask; | 1022 | shift = e->state & ShiftMask; |
1020 | len = XLookupString(e, buf, sizeof(buf), &ksym, NULL); | 1023 | len = XLookupString(e, buf, sizeof(buf), &ksym, NULL); |
1021 | if(skmap = kmap(ksym)) | 1024 | if(key[ksym]) |
1022 | ttywrite(skmap, strlen(skmap)); | 1025 | ttywrite(key[ksym], strlen(key[ksym])); |
1023 | else if(len > 0) { | 1026 | else if(len > 0) { |
1024 | buf[sizeof(buf)-1] = '\0'; | 1027 | buf[sizeof(buf)-1] = '\0'; |
1025 | if(meta && len == 1) | 1028 | if(meta && len == 1) |
@@ -1054,7 +1057,6 @@ resize(XEvent *e) { | |||
1054 | 1057 | ||
1055 | void | 1058 | void |
1056 | run(void) { | 1059 | run(void) { |
1057 | int ret; | ||
1058 | XEvent ev; | 1060 | XEvent ev; |
1059 | fd_set rfd; | 1061 | fd_set rfd; |
1060 | int xfd = XConnectionNumber(xw.dis); | 1062 | int xfd = XConnectionNumber(xw.dis); |
@@ -1062,39 +1064,25 @@ run(void) { | |||
1062 | running = 1; | 1064 | running = 1; |
1063 | XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask); | 1065 | XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask); |
1064 | XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */ | 1066 | XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */ |
1065 | 1067 | ||
1066 | while(running) { | 1068 | while(running) { |
1067 | FD_ZERO(&rfd); | 1069 | FD_ZERO(&rfd); |
1068 | FD_SET(cmdfd, &rfd); | 1070 | FD_SET(cmdfd, &rfd); |
1069 | FD_SET(xfd, &rfd); | 1071 | FD_SET(xfd, &rfd); |
1070 | XFlush(xw.dis); | 1072 | if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) { |
1071 | ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL); | 1073 | if(errno == EINTR) |
1072 | 1074 | continue; | |
1073 | if(ret < 0) | ||
1074 | die("select failed: %s\n", SERRNO); | 1075 | die("select failed: %s\n", SERRNO); |
1075 | |||
1076 | if(FD_ISSET(xfd, &rfd)) { | ||
1077 | while(XPending(xw.dis)) { | ||
1078 | XNextEvent(xw.dis, &ev); | ||
1079 | switch (ev.type) { | ||
1080 | default: | ||
1081 | break; | ||
1082 | case KeyPress: | ||
1083 | kpress(&ev.xkey); | ||
1084 | break; | ||
1085 | case Expose: | ||
1086 | draw(SCredraw); | ||
1087 | break; | ||
1088 | case ConfigureNotify: | ||
1089 | resize(&ev); | ||
1090 | break; | ||
1091 | } | ||
1092 | } | ||
1093 | } | 1076 | } |
1094 | if(FD_ISSET(cmdfd, &rfd)) { | 1077 | if(FD_ISSET(cmdfd, &rfd)) { |
1095 | ttyread(); | 1078 | ttyread(); |
1096 | draw(SCupdate); | 1079 | draw(SCupdate); |
1097 | } | 1080 | } |
1081 | while(XPending(xw.dis)) { | ||
1082 | XNextEvent(xw.dis, &ev); | ||
1083 | if(handler[ev.type]) | ||
1084 | (handler[ev.type])(&ev); | ||
1085 | } | ||
1098 | } | 1086 | } |
1099 | } | 1087 | } |
1100 | 1088 | ||