1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <unistd.h> |
---|
4 | #include <termios.h> |
---|
5 | #include <fcntl.h> |
---|
6 | #include <string.h> |
---|
7 | #include <sys/errno.h> |
---|
8 | #ifdef xBSD |
---|
9 | #include <ncurses.h> |
---|
10 | #else |
---|
11 | #include <curses.h> |
---|
12 | #endif |
---|
13 | |
---|
14 | #include "../../shared/str.h" |
---|
15 | |
---|
16 | #include "lcd.h" |
---|
17 | #include "curses_drv.h" |
---|
18 | #include "drv_base.h" |
---|
19 | |
---|
20 | |
---|
21 | lcd_logical_driver *curses_drv; |
---|
22 | |
---|
23 | int PAD=255; |
---|
24 | int ELLIPSIS=7; |
---|
25 | |
---|
26 | ////////////////////////////////////////////////////////////////////////// |
---|
27 | ////////////////////// For Curses Terminal Output //////////////////////// |
---|
28 | ////////////////////////////////////////////////////////////////////////// |
---|
29 | |
---|
30 | static char icon_char = '@'; |
---|
31 | |
---|
32 | |
---|
33 | int curses_drv_init(struct lcd_logical_driver *driver, char *args) |
---|
34 | { |
---|
35 | char *argv[64]; |
---|
36 | int argc; |
---|
37 | int i,j; |
---|
38 | |
---|
39 | |
---|
40 | argc = get_args(argv, args, 64); |
---|
41 | |
---|
42 | for(i=0; i<argc; i++) |
---|
43 | { |
---|
44 | if(0 == strcmp(argv[i], "-f") || |
---|
45 | 0 == strcmp(argv[i], "--forecolor")) |
---|
46 | { |
---|
47 | if(i + 1 > argc) { |
---|
48 | fprintf(stderr, "curses_init: %s requires an argument\n", |
---|
49 | argv[i]); |
---|
50 | return -1; |
---|
51 | } |
---|
52 | // TODO: color display in curses driver! |
---|
53 | printf("Sorry, colors not yet implemented...\n"); |
---|
54 | //strcpy(device, argv[++i]); |
---|
55 | } |
---|
56 | else if(0 == strcmp(argv[i], "-b") || |
---|
57 | 0 == strcmp(argv[i], "--backcolor")) |
---|
58 | { |
---|
59 | if(i + 1 > argc) { |
---|
60 | fprintf(stderr, "curses_init: %s requires an argument\n", |
---|
61 | argv[i]); |
---|
62 | return -1; |
---|
63 | } |
---|
64 | // TODO: color display in curses driver! |
---|
65 | printf("Sorry, colors not yet implemented...\n"); |
---|
66 | //strcpy(device, argv[++i]); |
---|
67 | } |
---|
68 | else if(0 == strcmp(argv[i], "-B") || |
---|
69 | 0 == strcmp(argv[i], "--backlight")) |
---|
70 | { |
---|
71 | if(i + 1 > argc) { |
---|
72 | fprintf(stderr, "curses_init: %s requires an argument\n", |
---|
73 | argv[i]); |
---|
74 | return -1; |
---|
75 | } |
---|
76 | // TODO: color display in curses driver! |
---|
77 | printf("Sorry, colors not yet implemented...\n"); |
---|
78 | //strcpy(device, argv[++i]); |
---|
79 | } |
---|
80 | else if(0 == strcmp(argv[i], "-h") || |
---|
81 | 0 == strcmp(argv[i], "--help")) |
---|
82 | { |
---|
83 | printf("LCDproc [n]curses driver\n" |
---|
84 | "\t-f\t--forecolor\tChange the foreground color\n" |
---|
85 | "\t-b\t--backcolor\tChange the backlight's \"off\" color\n" |
---|
86 | "\t-B\t--backlight\tChange the backlight's \"on\" color\n" |
---|
87 | "\t-h\t--help\t\tShow this help information\n"); |
---|
88 | return -1; |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | printf("Invalid parameter: %s\n", argv[i]); |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | curses_drv = driver; |
---|
99 | |
---|
100 | // Init curses... |
---|
101 | initscr(); |
---|
102 | cbreak(); |
---|
103 | noecho(); |
---|
104 | nodelay(stdscr, TRUE); |
---|
105 | nonl(); |
---|
106 | intrflush(stdscr, FALSE); |
---|
107 | keypad(stdscr, TRUE); |
---|
108 | |
---|
109 | curses_drv_clear(); |
---|
110 | |
---|
111 | // Override output functions... |
---|
112 | driver->clear = curses_drv_clear; |
---|
113 | driver->string = curses_drv_string; |
---|
114 | driver->chr = curses_drv_chr; |
---|
115 | driver->vbar = curses_drv_vbar; |
---|
116 | driver->init_vbar = NULL; |
---|
117 | driver->hbar = curses_drv_hbar; |
---|
118 | driver->init_hbar = NULL; |
---|
119 | driver->num = curses_drv_num; |
---|
120 | driver->init_num = curses_drv_init_num; |
---|
121 | |
---|
122 | driver->init = curses_drv_init; |
---|
123 | driver->close = curses_drv_close; |
---|
124 | driver->flush = curses_drv_flush; |
---|
125 | driver->flush_box = curses_drv_flush_box; |
---|
126 | driver->contrast = NULL; |
---|
127 | driver->backlight = NULL; |
---|
128 | driver->set_char = NULL; |
---|
129 | driver->icon = curses_drv_icon; |
---|
130 | driver->draw_frame = curses_drv_draw_frame; |
---|
131 | |
---|
132 | driver->getkey = curses_drv_getkey; |
---|
133 | |
---|
134 | // Change the character used for padding the title bars... |
---|
135 | PAD = '#'; |
---|
136 | // Change the character used for "..." |
---|
137 | ELLIPSIS = '~'; |
---|
138 | |
---|
139 | return 200; // 200 is arbitrary. (must be 1 or more) |
---|
140 | } |
---|
141 | |
---|
142 | void curses_drv_close() |
---|
143 | { |
---|
144 | // Close curses |
---|
145 | clear(); |
---|
146 | move(0,0); |
---|
147 | refresh(); |
---|
148 | endwin(); |
---|
149 | |
---|
150 | drv_base_close(); |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | ///////////////////////////////////////////////////////////////// |
---|
155 | // Clears the LCD screen |
---|
156 | // |
---|
157 | void curses_drv_clear() |
---|
158 | { |
---|
159 | clear(); |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | ///////////////////////////////////////////////////////////////// |
---|
165 | // Prints a string on the lcd display, at position (x,y). The |
---|
166 | // upper-left is (1,1), and the lower right should be (20,4). |
---|
167 | // |
---|
168 | void curses_drv_string(int x, int y, char string[]) |
---|
169 | { |
---|
170 | int i; |
---|
171 | unsigned char *c; |
---|
172 | |
---|
173 | for(i=0; string[i]; i++) |
---|
174 | { |
---|
175 | c = &string[i]; |
---|
176 | switch(*c) |
---|
177 | { |
---|
178 | case 0: *c = icon_char; break; |
---|
179 | case 255: *c = '#'; break; |
---|
180 | } |
---|
181 | } |
---|
182 | mvaddstr(y-1, x-1, string); |
---|
183 | } |
---|
184 | |
---|
185 | ///////////////////////////////////////////////////////////////// |
---|
186 | // Prints a character on the lcd display, at position (x,y). The |
---|
187 | // upper-left is (1,1), and the lower right should be (20,4). |
---|
188 | // |
---|
189 | void curses_drv_chr(int x, int y, char c) |
---|
190 | { |
---|
191 | switch(c) |
---|
192 | { |
---|
193 | case 0: c = icon_char; break; |
---|
194 | case -1: c = '#'; break; |
---|
195 | } |
---|
196 | |
---|
197 | mvaddch(y-1, x-1, c); |
---|
198 | } |
---|
199 | |
---|
200 | ///////////////////////////////////////////////////////////////// |
---|
201 | // Sets up for big numbers. |
---|
202 | // |
---|
203 | void curses_drv_init_num() |
---|
204 | { |
---|
205 | } |
---|
206 | |
---|
207 | ///////////////////////////////////////////////////////////////// |
---|
208 | // Writes a big number. |
---|
209 | // |
---|
210 | void curses_drv_num(int x, int num) |
---|
211 | { |
---|
212 | char c; |
---|
213 | int y, dx; |
---|
214 | |
---|
215 | c='0'+num; |
---|
216 | // printf(&c,"%1d",num); |
---|
217 | |
---|
218 | for(y=1; y<5; y++) |
---|
219 | for(dx=0; dx<3; dx++) |
---|
220 | curses_drv_chr(x+dx, y, c); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | ///////////////////////////////////////////////////////////////// |
---|
225 | // Draws a vertical bar; erases entire column onscreen. |
---|
226 | // |
---|
227 | void curses_drv_vbar(int x, int len) |
---|
228 | { |
---|
229 | char map[]="_.,,ooO8"; |
---|
230 | |
---|
231 | int y; |
---|
232 | for(y=lcd.hgt; y > 0 && len>0; y--) |
---|
233 | { |
---|
234 | if(len >= lcd.cellhgt) curses_drv_chr(x, y, '8'); |
---|
235 | else curses_drv_chr(x, y, map[len-1]); |
---|
236 | |
---|
237 | len -= lcd.cellhgt; |
---|
238 | } |
---|
239 | |
---|
240 | // move(4-len/lcd.cellhgt, x-1); |
---|
241 | // vline(0, len/lcd.cellhgt); |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | ///////////////////////////////////////////////////////////////// |
---|
246 | // Draws a horizontal bar to the right. |
---|
247 | // |
---|
248 | void curses_drv_hbar(int x, int y, int len) |
---|
249 | { |
---|
250 | for(; x<=lcd.wid && len>0; x++) |
---|
251 | { |
---|
252 | if(len>=lcd.cellwid) |
---|
253 | curses_drv_chr(x, y, '='); |
---|
254 | else |
---|
255 | curses_drv_chr(x, y, '-'); |
---|
256 | |
---|
257 | len -= lcd.cellwid; |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | |
---|
262 | // move(y-1, x-1); |
---|
263 | // hline(0, len/lcd.cellwid); |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | ///////////////////////////////////////////////////////////////// |
---|
268 | // Sets character 0 to an icon... |
---|
269 | // |
---|
270 | void curses_drv_icon(int which, char dest) |
---|
271 | { |
---|
272 | if(dest == 0) |
---|
273 | switch(which) |
---|
274 | { |
---|
275 | case 0: icon_char = '+'; break; |
---|
276 | case 1: icon_char = '*'; break; |
---|
277 | default: icon_char = '#'; break; |
---|
278 | } |
---|
279 | |
---|
280 | } |
---|
281 | |
---|
282 | ////////////////////////////////////////////////////////////////// |
---|
283 | // Flushes all output to the lcd... |
---|
284 | // |
---|
285 | void curses_drv_flush() |
---|
286 | { |
---|
287 | curses_drv_draw_frame(curses_drv->framebuf); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | void curses_drv_flush_box(int lft, int top, int rgt, int bot) |
---|
292 | { |
---|
293 | curses_drv_flush(); |
---|
294 | } |
---|
295 | |
---|
296 | |
---|
297 | void curses_drv_draw_frame(char *dat) |
---|
298 | { |
---|
299 | // border(0, 0, 0, 0, 0, 0, lcd.wid+1, lcd.hgt+1); |
---|
300 | |
---|
301 | refresh(); |
---|
302 | |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | |
---|
307 | char curses_drv_getkey() |
---|
308 | { |
---|
309 | int i; |
---|
310 | |
---|
311 | i = getch(); |
---|
312 | |
---|
313 | if(i == KEY_LEFT) return 'D'; |
---|
314 | if(i == KEY_UP) return 'B'; |
---|
315 | if(i == KEY_DOWN) return 'C'; |
---|
316 | if(i == KEY_RIGHT) return 'A'; |
---|
317 | |
---|
318 | if(i != ERR) return i; |
---|
319 | else return 0; |
---|
320 | |
---|
321 | } |
---|
322 | |
---|