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 | |
---|
9 | #include "LL.h" |
---|
10 | |
---|
11 | #include "lcd.h" |
---|
12 | |
---|
13 | #include "drv_base.h" |
---|
14 | |
---|
15 | #ifdef MTXORB_DRV |
---|
16 | #include "MtxOrb.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #ifdef TEXT_DRV |
---|
20 | #include "text.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #ifdef DEBUG_DRV |
---|
24 | #include "debug.h" |
---|
25 | #endif |
---|
26 | |
---|
27 | #ifdef CURSES_DRV |
---|
28 | #include "curses_drv.h" |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef HD44780_DRV |
---|
32 | #include "hd44780.h" |
---|
33 | #endif |
---|
34 | |
---|
35 | // TODO: Make a Windows server, and clients...? |
---|
36 | |
---|
37 | |
---|
38 | lcd_logical_driver lcd; |
---|
39 | |
---|
40 | lcd_physical_driver drivers[] = |
---|
41 | { |
---|
42 | { "base", drv_base_init, }, |
---|
43 | #ifdef MTXORB_DRV |
---|
44 | { "MtxOrb", MtxOrb_init, }, |
---|
45 | #endif |
---|
46 | #ifdef HD44780_DRV |
---|
47 | { "HD44780", HD44780_init, }, |
---|
48 | #endif |
---|
49 | #ifdef TEXT_DRV |
---|
50 | { "text", text_init, }, |
---|
51 | #endif |
---|
52 | #ifdef DEBUG_DRV |
---|
53 | { "debug", debug_init, }, |
---|
54 | #endif |
---|
55 | #ifdef CURSES_DRV |
---|
56 | { "curses", curses_drv_init, }, |
---|
57 | #endif |
---|
58 | { NULL, NULL, }, |
---|
59 | |
---|
60 | }; |
---|
61 | |
---|
62 | LL *list; |
---|
63 | |
---|
64 | //////////////////////////////////////////////////////////// |
---|
65 | // This sets up which driver to use and initializes stuff. |
---|
66 | // |
---|
67 | int lcd_init(char *args) |
---|
68 | { |
---|
69 | // int i; |
---|
70 | |
---|
71 | list = LL_new(); |
---|
72 | if(!list) |
---|
73 | { |
---|
74 | printf("Error allocating driver list.\n"); |
---|
75 | return -1; |
---|
76 | } |
---|
77 | |
---|
78 | lcd_drv_init(NULL, NULL); |
---|
79 | |
---|
80 | return lcd_add_driver("base", args); |
---|
81 | |
---|
82 | /* |
---|
83 | drv_base_init(args); |
---|
84 | |
---|
85 | for(i=0; drivers[i].name; i++) |
---|
86 | { |
---|
87 | if(!strcmp(driver, drivers[i].name)) |
---|
88 | { |
---|
89 | return drivers[i].init(args); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | printf("Invalid driver: %s\n", driver); |
---|
94 | return -1; |
---|
95 | */ |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | // TODO: lcd_remove_driver() |
---|
101 | |
---|
102 | int lcd_add_driver(char *driver, char *args) |
---|
103 | { |
---|
104 | int i; |
---|
105 | |
---|
106 | lcd_logical_driver *add; |
---|
107 | |
---|
108 | for(i=0; drivers[i].name; i++) |
---|
109 | { |
---|
110 | |
---|
111 | //printf("Checking driver: %s\n", drivers[i].name); |
---|
112 | |
---|
113 | if(0 == strcmp(driver, drivers[i].name)) |
---|
114 | { |
---|
115 | |
---|
116 | //printf("Found driver: %s (%s)\n", drivers[i].name, driver); |
---|
117 | |
---|
118 | add = malloc(sizeof(lcd_logical_driver)); |
---|
119 | if(!add) |
---|
120 | { |
---|
121 | printf("Couldn't allocate driver \"%s\".\n", driver); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | //printf("Allocated driver\n"); |
---|
125 | memset(add, 0, sizeof(lcd_logical_driver)); |
---|
126 | |
---|
127 | add->wid = lcd.wid; add->hgt = lcd.hgt; |
---|
128 | add->cellwid = lcd.cellwid; add->cellhgt = lcd.cellhgt; |
---|
129 | |
---|
130 | //printf("LCD driver info:\n\twid: %i\thgt: %i\n", |
---|
131 | // add->wid, add->hgt); |
---|
132 | |
---|
133 | add->framebuf = malloc(add->wid * add->hgt); |
---|
134 | |
---|
135 | if(!add->framebuf) |
---|
136 | { |
---|
137 | printf("Couldn't allocate framebuffer for driver \"%s\".\n", |
---|
138 | driver); |
---|
139 | free(add); |
---|
140 | return -1; |
---|
141 | } |
---|
142 | //printf("Allocated frame buffer\n"); |
---|
143 | |
---|
144 | LL_Push(list, (void *)add); |
---|
145 | |
---|
146 | return drivers[i].init(add, args); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | return -1; |
---|
151 | } |
---|
152 | |
---|
153 | // TODO: Put lcd_shutdown in the shutdown function... |
---|
154 | int lcd_shutdown() |
---|
155 | { |
---|
156 | lcd_logical_driver *del; |
---|
157 | |
---|
158 | LL_Rewind(list); |
---|
159 | do { |
---|
160 | del = (lcd_logical_driver *)LL_DeleteNode(list); |
---|
161 | if(del->close) del->close(); |
---|
162 | free(del); |
---|
163 | } while(LL_Length(list) > 0); |
---|
164 | |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | int lcd_drv_init(struct lcd_logical_driver *driver, char *args) |
---|
171 | { |
---|
172 | // printf("lcd_drv_init()\n"); |
---|
173 | |
---|
174 | lcd.wid = 20; |
---|
175 | lcd.hgt = 4; |
---|
176 | |
---|
177 | lcd.framebuf = NULL; |
---|
178 | /* |
---|
179 | if(!lcd.framebuf) |
---|
180 | lcd.framebuf = malloc(lcd.wid * lcd.hgt); |
---|
181 | |
---|
182 | if(!lcd.framebuf) |
---|
183 | { |
---|
184 | lcd_drv_close(); |
---|
185 | return -1; |
---|
186 | } |
---|
187 | memset(lcd.framebuf, ' ', lcd.wid*lcd.hgt); |
---|
188 | */ |
---|
189 | // Debugging... |
---|
190 | // if(lcd.framebuf) printf("Frame buffer: %i\n", (int)lcd.framebuf); |
---|
191 | |
---|
192 | lcd.cellwid = 5; |
---|
193 | lcd.cellhgt = 8; |
---|
194 | |
---|
195 | // Set up these wrapper functions... |
---|
196 | lcd.clear = lcd_drv_clear; |
---|
197 | lcd.string = lcd_drv_string; |
---|
198 | lcd.chr = lcd_drv_chr; |
---|
199 | lcd.vbar = lcd_drv_vbar; |
---|
200 | lcd.hbar = lcd_drv_hbar; |
---|
201 | lcd.init_num = lcd_drv_init_num; |
---|
202 | lcd.num = lcd_drv_num; |
---|
203 | |
---|
204 | lcd.init = lcd_drv_init; |
---|
205 | lcd.close = lcd_drv_close; |
---|
206 | lcd.flush = lcd_drv_flush; |
---|
207 | lcd.flush_box = lcd_drv_flush_box; |
---|
208 | lcd.contrast = lcd_drv_contrast; |
---|
209 | lcd.backlight = lcd_drv_backlight; |
---|
210 | lcd.set_char = lcd_drv_set_char; |
---|
211 | lcd.icon = lcd_drv_icon; |
---|
212 | lcd.init_vbar = lcd_drv_init_vbar; |
---|
213 | lcd.init_hbar = lcd_drv_init_hbar; |
---|
214 | lcd.draw_frame = lcd_drv_draw_frame; |
---|
215 | |
---|
216 | lcd.getkey = lcd_drv_getkey; |
---|
217 | |
---|
218 | |
---|
219 | return 1; // 1 is arbitrary. (must be 1 or more) |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | ////////////////////////////////////////////////////////////////////// |
---|
225 | // All functions below here call their respective driver functions... |
---|
226 | // |
---|
227 | // The way it works is this: |
---|
228 | // It loops through the list of drivers, calling each one to keep |
---|
229 | // them all synchronized. During this loop... |
---|
230 | // - First, set the framebuffer to point to the drivers' framebuffer. |
---|
231 | // (this ensures that whatever driver gets called will operate on |
---|
232 | // the correct buffer) |
---|
233 | // - Then, it checks to see what sort of call to make. |
---|
234 | // driver->func() > 0 means it should call the driver function. |
---|
235 | // driver->func() == NULL means it should not make a call. |
---|
236 | // driver->func() == -1 means it should call the generic driver. |
---|
237 | ////////////////////////////////////////////////////////////////////// |
---|
238 | |
---|
239 | |
---|
240 | void lcd_drv_close() |
---|
241 | { |
---|
242 | lcd_logical_driver *driver; |
---|
243 | |
---|
244 | LL_Rewind(list); |
---|
245 | do { |
---|
246 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
247 | if(driver) |
---|
248 | { |
---|
249 | lcd.framebuf = driver->framebuf; |
---|
250 | |
---|
251 | if(driver->close > 0) |
---|
252 | driver->close(); |
---|
253 | else if((int)driver->close == -1) |
---|
254 | { |
---|
255 | drv_base->close(); |
---|
256 | } |
---|
257 | } |
---|
258 | } while(LL_Next(list) == 0); |
---|
259 | |
---|
260 | } |
---|
261 | |
---|
262 | void lcd_drv_clear() |
---|
263 | { |
---|
264 | lcd_logical_driver *driver; |
---|
265 | |
---|
266 | LL_Rewind(list); |
---|
267 | do { |
---|
268 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
269 | if(driver) |
---|
270 | { |
---|
271 | lcd.framebuf = driver->framebuf; |
---|
272 | |
---|
273 | if(driver->clear > 0) |
---|
274 | driver->clear(); |
---|
275 | else if((int)driver->clear == -1) |
---|
276 | { |
---|
277 | drv_base->clear(); |
---|
278 | } |
---|
279 | } |
---|
280 | } while(LL_Next(list) == 0); |
---|
281 | |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | void lcd_drv_flush() |
---|
286 | { |
---|
287 | lcd_logical_driver *driver; |
---|
288 | |
---|
289 | LL_Rewind(list); |
---|
290 | do { |
---|
291 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
292 | if(driver) |
---|
293 | { |
---|
294 | lcd.framebuf = driver->framebuf; |
---|
295 | |
---|
296 | if(driver->flush > 0) |
---|
297 | driver->flush(); |
---|
298 | else if((int)driver->flush == -1) |
---|
299 | { |
---|
300 | drv_base->flush(); |
---|
301 | } |
---|
302 | } |
---|
303 | } while(LL_Next(list) == 0); |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | void lcd_drv_string(int x, int y, char string[]) |
---|
308 | { |
---|
309 | lcd_logical_driver *driver; |
---|
310 | |
---|
311 | LL_Rewind(list); |
---|
312 | do { |
---|
313 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
314 | if(driver) |
---|
315 | { |
---|
316 | lcd.framebuf = driver->framebuf; |
---|
317 | |
---|
318 | if(driver->string > 0) |
---|
319 | driver->string(x,y,string); |
---|
320 | else if((int)driver->string == -1) |
---|
321 | { |
---|
322 | drv_base->string(x,y,string); |
---|
323 | } |
---|
324 | } |
---|
325 | } while(LL_Next(list) == 0); |
---|
326 | } |
---|
327 | |
---|
328 | void lcd_drv_chr(int x, int y, char c) |
---|
329 | { |
---|
330 | lcd_logical_driver *driver; |
---|
331 | |
---|
332 | LL_Rewind(list); |
---|
333 | do { |
---|
334 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
335 | if(driver) |
---|
336 | { |
---|
337 | lcd.framebuf = driver->framebuf; |
---|
338 | |
---|
339 | if(driver->chr > 0) |
---|
340 | driver->chr(x,y,c); |
---|
341 | else if((int)driver->chr == -1) |
---|
342 | { |
---|
343 | drv_base->chr(x,y,c); |
---|
344 | } |
---|
345 | } |
---|
346 | } while(LL_Next(list) == 0); |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | void lcd_drv_contrast(int contrast) |
---|
352 | { |
---|
353 | lcd_logical_driver *driver; |
---|
354 | |
---|
355 | LL_Rewind(list); |
---|
356 | do { |
---|
357 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
358 | if(driver) |
---|
359 | { |
---|
360 | lcd.framebuf = driver->framebuf; |
---|
361 | |
---|
362 | if(driver->contrast > 0) |
---|
363 | driver->contrast(contrast); |
---|
364 | else if((int)driver->contrast == -1) |
---|
365 | { |
---|
366 | drv_base->contrast(contrast); |
---|
367 | } |
---|
368 | } |
---|
369 | } while(LL_Next(list) == 0); |
---|
370 | } |
---|
371 | |
---|
372 | void lcd_drv_backlight(int on) |
---|
373 | { |
---|
374 | lcd_logical_driver *driver; |
---|
375 | |
---|
376 | LL_Rewind(list); |
---|
377 | do { |
---|
378 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
379 | if(driver) |
---|
380 | { |
---|
381 | lcd.framebuf = driver->framebuf; |
---|
382 | |
---|
383 | if(driver->backlight > 0) |
---|
384 | driver->backlight(on); |
---|
385 | else if((int)driver->backlight == -1) |
---|
386 | { |
---|
387 | drv_base->backlight(on); |
---|
388 | } |
---|
389 | } |
---|
390 | } while(LL_Next(list) == 0); |
---|
391 | } |
---|
392 | |
---|
393 | void lcd_drv_init_vbar() |
---|
394 | { |
---|
395 | lcd_logical_driver *driver; |
---|
396 | |
---|
397 | LL_Rewind(list); |
---|
398 | do { |
---|
399 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
400 | if(driver) |
---|
401 | { |
---|
402 | lcd.framebuf = driver->framebuf; |
---|
403 | |
---|
404 | if(driver->init_vbar > 0) |
---|
405 | driver->init_vbar(); |
---|
406 | else if((int)driver->init_vbar == -1) |
---|
407 | { |
---|
408 | drv_base->init_vbar(); |
---|
409 | } |
---|
410 | } |
---|
411 | } while(LL_Next(list) == 0); |
---|
412 | } |
---|
413 | |
---|
414 | void lcd_drv_init_hbar() |
---|
415 | { |
---|
416 | lcd_logical_driver *driver; |
---|
417 | |
---|
418 | LL_Rewind(list); |
---|
419 | do { |
---|
420 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
421 | if(driver) |
---|
422 | { |
---|
423 | lcd.framebuf = driver->framebuf; |
---|
424 | |
---|
425 | if(driver->init_hbar > 0) |
---|
426 | driver->init_hbar(); |
---|
427 | else if((int)driver->init_hbar == -1) |
---|
428 | { |
---|
429 | drv_base->init_hbar(); |
---|
430 | } |
---|
431 | } |
---|
432 | } while(LL_Next(list) == 0); |
---|
433 | } |
---|
434 | |
---|
435 | void lcd_drv_init_num() |
---|
436 | { |
---|
437 | lcd_logical_driver *driver; |
---|
438 | |
---|
439 | LL_Rewind(list); |
---|
440 | do { |
---|
441 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
442 | if(driver) |
---|
443 | { |
---|
444 | lcd.framebuf = driver->framebuf; |
---|
445 | |
---|
446 | if(driver->init_num > 0) |
---|
447 | driver->init_num(); |
---|
448 | else if((int)driver->init_num == -1) |
---|
449 | { |
---|
450 | drv_base->init_num(); |
---|
451 | } |
---|
452 | } |
---|
453 | } while(LL_Next(list) == 0); |
---|
454 | } |
---|
455 | |
---|
456 | void lcd_drv_num(int x, int num) |
---|
457 | { |
---|
458 | lcd_logical_driver *driver; |
---|
459 | |
---|
460 | LL_Rewind(list); |
---|
461 | do { |
---|
462 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
463 | if(driver) |
---|
464 | { |
---|
465 | lcd.framebuf = driver->framebuf; |
---|
466 | |
---|
467 | if(driver->num > 0) |
---|
468 | driver->num(x,num); |
---|
469 | else if((int)driver->num == -1) |
---|
470 | { |
---|
471 | drv_base->num(x,num); |
---|
472 | } |
---|
473 | } |
---|
474 | } while(LL_Next(list) == 0); |
---|
475 | } |
---|
476 | |
---|
477 | void lcd_drv_set_char(int n, char *dat) |
---|
478 | { |
---|
479 | lcd_logical_driver *driver; |
---|
480 | |
---|
481 | LL_Rewind(list); |
---|
482 | do { |
---|
483 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
484 | if(driver) |
---|
485 | { |
---|
486 | lcd.framebuf = driver->framebuf; |
---|
487 | |
---|
488 | if(driver->set_char > 0) |
---|
489 | driver->set_char(n,dat); |
---|
490 | else if((int)driver->set_char == -1) |
---|
491 | { |
---|
492 | drv_base->set_char(n,dat); |
---|
493 | } |
---|
494 | } |
---|
495 | } while(LL_Next(list) == 0); |
---|
496 | } |
---|
497 | |
---|
498 | void lcd_drv_vbar(int x, int len) |
---|
499 | { |
---|
500 | lcd_logical_driver *driver; |
---|
501 | |
---|
502 | LL_Rewind(list); |
---|
503 | do { |
---|
504 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
505 | if(driver) |
---|
506 | { |
---|
507 | lcd.framebuf = driver->framebuf; |
---|
508 | |
---|
509 | if(driver->vbar > 0) |
---|
510 | driver->vbar(x,len); |
---|
511 | else if((int)driver->vbar == -1) |
---|
512 | { |
---|
513 | drv_base->vbar(x,len); |
---|
514 | } |
---|
515 | } |
---|
516 | } while(LL_Next(list) == 0); |
---|
517 | } |
---|
518 | |
---|
519 | void lcd_drv_hbar(int x, int y, int len) |
---|
520 | { |
---|
521 | lcd_logical_driver *driver; |
---|
522 | |
---|
523 | LL_Rewind(list); |
---|
524 | do { |
---|
525 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
526 | if(driver) |
---|
527 | { |
---|
528 | lcd.framebuf = driver->framebuf; |
---|
529 | |
---|
530 | if(driver->hbar > 0) |
---|
531 | driver->hbar(x,y,len); |
---|
532 | else if((int)driver->hbar == -1) |
---|
533 | { |
---|
534 | drv_base->hbar(x,y,len); |
---|
535 | } |
---|
536 | } |
---|
537 | } while(LL_Next(list) == 0); |
---|
538 | } |
---|
539 | |
---|
540 | |
---|
541 | void lcd_drv_icon(int which, char dest) |
---|
542 | { |
---|
543 | lcd_logical_driver *driver; |
---|
544 | |
---|
545 | LL_Rewind(list); |
---|
546 | do { |
---|
547 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
548 | if(driver) |
---|
549 | { |
---|
550 | lcd.framebuf = driver->framebuf; |
---|
551 | |
---|
552 | if(driver->icon > 0) |
---|
553 | driver->icon(which,dest); |
---|
554 | else if((int)driver->icon == -1) |
---|
555 | { |
---|
556 | drv_base->icon(which,dest); |
---|
557 | } |
---|
558 | } |
---|
559 | } while(LL_Next(list) == 0); |
---|
560 | } |
---|
561 | |
---|
562 | |
---|
563 | void lcd_drv_flush_box(int lft, int top, int rgt, int bot) |
---|
564 | { |
---|
565 | lcd_logical_driver *driver; |
---|
566 | |
---|
567 | LL_Rewind(list); |
---|
568 | do { |
---|
569 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
570 | if(driver) |
---|
571 | { |
---|
572 | lcd.framebuf = driver->framebuf; |
---|
573 | |
---|
574 | if(driver->flush_box > 0) |
---|
575 | driver->flush_box(lft,top,rgt,bot); |
---|
576 | else if((int)driver->flush_box == -1) |
---|
577 | { |
---|
578 | drv_base->flush_box(lft,top,rgt,bot); |
---|
579 | } |
---|
580 | } |
---|
581 | } while(LL_Next(list) == 0); |
---|
582 | |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | // TODO: Check whether lcd.draw_frame() should really take a framebuffer |
---|
587 | // TODO: as an argument, or if it should always use lcd.framebuf |
---|
588 | void lcd_drv_draw_frame(char *dat) |
---|
589 | { |
---|
590 | lcd_logical_driver *driver; |
---|
591 | |
---|
592 | LL_Rewind(list); |
---|
593 | do { |
---|
594 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
595 | if(driver) |
---|
596 | { |
---|
597 | lcd.framebuf = driver->framebuf; |
---|
598 | |
---|
599 | if(driver->draw_frame > 0) |
---|
600 | driver->draw_frame(dat); |
---|
601 | else if((int)driver->draw_frame == -1) |
---|
602 | { |
---|
603 | drv_base->draw_frame(dat); |
---|
604 | } |
---|
605 | } |
---|
606 | } while(LL_Next(list) == 0); |
---|
607 | |
---|
608 | } |
---|
609 | |
---|
610 | |
---|
611 | char lcd_drv_getkey() |
---|
612 | { |
---|
613 | lcd_logical_driver *driver; |
---|
614 | |
---|
615 | char key; |
---|
616 | |
---|
617 | LL_Rewind(list); |
---|
618 | do { |
---|
619 | driver = (lcd_logical_driver *)LL_Get(list); |
---|
620 | if(driver) |
---|
621 | { |
---|
622 | lcd.framebuf = driver->framebuf; |
---|
623 | |
---|
624 | if(driver->getkey > 0) |
---|
625 | { |
---|
626 | key = driver->getkey(); |
---|
627 | if(key) return key; |
---|
628 | } |
---|
629 | else if((int)driver->getkey == -1) |
---|
630 | { |
---|
631 | key = drv_base->getkey(); |
---|
632 | if(key) return key; |
---|
633 | } |
---|
634 | } |
---|
635 | } while(LL_Next(list) == 0); |
---|
636 | |
---|
637 | return 0; |
---|
638 | } |
---|