-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.c
192 lines (176 loc) · 3.73 KB
/
main.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
/* See LICENSE for license details. */
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <time.h>
#include <unistd.h>
#include <libgen.h>
#include <pthread.h>
#include "arg.h"
#define Glyph Glyph_
#define Font Font_
#include "screen.h"
#include "st.h"
static inline ushort sixd_to_16bit(int);
/* Globals */
static int frclen = 0;
ushort
sixd_to_16bit(int x)
{
return x == 0 ? 0 : 0x3737 + 0x2828 * x;
}
void*
read_stdin(void* arg)
{
char str[20] = "\0";
char seq[20] = "\0";
char inchar;
for(;;){
inchar = getchar();
if(inchar == '<') {
/* This is a special character. It looks like <SpeCas>.
*
* 1- Parse SpeCas.
* 2- Send the appropriate ascii sequence.
*
*
* Use od -c to find linux escape sequences.
*/
int i = 0;
while(inchar != '>'){
inchar = getchar();
seq[i] = inchar;
i++;
}
seq[i-1] = '\0';
if (strcmp(seq, "Esc") == 0) {
str[0]= 27;
str[1]= '\0';
}
else if (strcmp(seq, "BckSp") == 0){
strcpy(str, "\177");
}
else if (strcmp(seq, "Del") == 0)
strcpy(str, "\033[3~");
else if (strcmp(seq, "Up") == 0)
strcpy(str, "\033[A");
else if (strcmp(seq, "Down") == 0)
strcpy(str, "\033[B");
else if (strcmp(seq, "Right") == 0)
strcpy(str, "\033[C");
else if (strcmp(seq, "Left") == 0)
strcpy(str, "\033[D");
else if (strcmp(seq, "End") == 0)
strcpy(str, "\033[F");
else if (strcmp(seq, "Home") == 0)
strcpy(str, "\033[H");
else
read_stdin(arg);
} else {
str[0] = inchar;
str[1] = '\0';
}
ttywrite(str, strlen(str), 1);
}
}
void
run(void)
{
int w = win.w, h = win.h;
pthread_t input_thread;
fd_set rfd;
ttynew();
ttyread();
pthread_create(&input_thread, NULL, read_stdin, NULL);
/* Init E-Ink screen */
if (sinit() != 0){
printf("e-Paper init failed\n");
return;
}
unsigned char* frame_buffer = (unsigned char*)malloc(400 / 8 * 300);
char in[20] = "\0";
char* str = (char*)malloc((cols + 10) * sizeof(char));
for (1;;) {
FD_ZERO(&rfd);
FD_SET(cmdfd, &rfd);
if (FD_ISSET(cmdfd, &rfd)) {
ttyread();
// Remove this block to remove console printing.
// ======================
/*
Line* lines = term.line;
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
str[j] = lines[i][j].u;
}
printf("%s\n", str);
}
*/
//=======================
// Uncomment that to display on e-ink.
//==================
pclear(UNCOLORED, frame_buffer);
pdraw_term(term.line, frame_buffer);
sdisplay_frame(frame_buffer);
//==================
}
}
free(str);
free(frame_buffer);
}
int
main(int argc, char *argv[])
{
win.cursor = cursorshape;
ARGBEGIN {
case 'a':
break;
case 'c':
opt_class = EARGF(usage());
break;
case 'e':
if (argc > 0)
--argc, ++argv;
goto run;
case 'f':
opt_font = EARGF(usage());
break;
case 'o':
opt_io = EARGF(usage());
break;
case 'l':
opt_line = EARGF(usage());
break;
case 'n':
opt_name = EARGF(usage());
break;
case 't':
case 'T':
opt_title = EARGF(usage());
break;
case 'w':
opt_embed = EARGF(usage());
break;
case 'v':
die("%s " VERSION " (c) 2010-2016 st engineers\n", argv0);
break;
default:
usage();
} ARGEND;
run:
if (argc > 0) {
/* eat all remaining arguments */
opt_cmd = argv;
if (!opt_title && !opt_line)
opt_title = basename(xstrdup(argv[0]));
}
setlocale(LC_CTYPE, "");
tnew(MAX(cols, 1), MAX(rows, 1));
run();
return 0;
}