1
/* $Id: drv_Curses.c 975 2009-01-18 11:16:20Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_Curses.c $
4
* pure ncurses based text driver
6
* Copyright (C) 2004 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* based on the old Curses/Text driver which is
10
* Copyright (C) 2001 Leopold Toetsch <lt@toetsch.at>
12
* This file is part of LCD4Linux.
14
* LCD4Linux is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2, or (at your option)
19
* LCD4Linux is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34
* struct DRIVER drv_Curses
53
#include "widget_text.h"
54
#include "widget_bar.h"
55
#include "widget_keypad.h"
57
#include "drv_generic_text.h"
58
#include "drv_generic_keypad.h"
61
static char Name[] = "Curses";
63
static WINDOW *w = NULL;
64
static WINDOW *e = NULL;
69
/****************************************/
70
/*** hardware dependant functions ***/
71
/****************************************/
73
static void drv_Curs_clear(void)
81
static void drv_Curs_write(const int row, const int col, const char *data, const int len)
86
while ((p = strpbrk(data, "\r\n")) != NULL) {
93
mvwprintw(w, row + 1, col + 1, "%.*s", l, data);
94
wmove(w, DROWS + 1, 0);
100
static void drv_Curs_defchar(const __attribute__ ((unused))
101
int ascii, const __attribute__ ((unused))
102
unsigned char *buffer)
108
/* ncures scroll SIGSEGVs on my system, so this is a workaroud */
110
int curses_error(char *buffer)
112
static int lines = 0;
113
static char *lb[100];
120
/* replace \r, \n with underscores */
121
while ((p = strpbrk(buffer, "\r\n")) != NULL) {
125
if (lines >= EROWS) {
127
for (i = 1; i <= EROWS; i++) {
135
lb[lines] = strdup(buffer);
136
for (i = start; i <= lines; i++) {
137
mvwprintw(e, i + 1, 1, "%s", lb[i]);
142
mvwprintw(e, 0, 3, "Stderr:");
152
static int drv_Curs_start(const char *section, const int quiet)
156
if (!running_foreground) {
157
error("%s: You want me to display on /dev/null? Sorry, I can't ...", Name);
158
error("%s: Maybe you want me to run in foreground? Try '-F'", Name);
162
s = cfg_get(section, "Size", "20x4");
163
if (s == NULL || *s == '\0') {
164
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
168
if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DROWS < 1 || DCOLS < 1) {
169
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
177
debug("%s: curses thinks that COLS=%d LINES=%d", Name, COLS, LINES);
178
w = newwin(DROWS + 2, DCOLS + 2, 0, 0);
182
EROWS = LINES - DROWS - 3;
185
debug("EROWS=%d", EROWS);
188
e = newwin(EROWS, COLS, DROWS + 3, 0);
191
mvwprintw(e, 0, 3, "Stderr:");
200
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
201
if (drv_generic_text_greet(buffer, NULL)) {
210
static void drv_Curs_timer(void __attribute__ ((unused)) * notused)
217
drv_generic_keypad_press(c);
221
static int drv_Curs_keypad(const int num)
228
val += WIDGET_KEY_PRESSED;
229
val += WIDGET_KEY_UP;
233
val += WIDGET_KEY_PRESSED;
234
val += WIDGET_KEY_DOWN;
238
val += WIDGET_KEY_PRESSED;
239
val += WIDGET_KEY_LEFT;
243
val += WIDGET_KEY_PRESSED;
244
val += WIDGET_KEY_RIGHT;
247
debug("Unbound Key '%d'", num);
255
/****************************************/
257
/****************************************/
259
/* none at the moment... */
262
/****************************************/
263
/*** widget callbacks ***/
264
/****************************************/
266
/* using drv_generic_text_draw(W) */
267
/* using drv_generic_text_bar_draw(W) */
268
/* using drv_generic_keypad_draw(W) */
271
/****************************************/
272
/*** exported functions ***/
273
/****************************************/
277
int drv_Curs_list(void)
279
printf("pure ncurses based text driver");
284
/* initialize driver & display */
285
int drv_Curs_init(const char *section, const int quiet)
290
info("%s: %s", Name, "$Rev: 975 $");
292
/* display preferences */
293
XRES = 1; /* pixel width of one char */
294
YRES = 1; /* pixel height of one char */
295
CHARS = 0; /* number of user-defineable characters */
296
CHAR0 = 0; /* ASCII of first user-defineable char */
297
GOTO_COST = 0; /* number of bytes a goto command requires */
299
/* real worker functions */
300
drv_generic_text_real_write = drv_Curs_write;
301
drv_generic_text_real_defchar = drv_Curs_defchar;
302
drv_generic_keypad_real_press = drv_Curs_keypad;
304
/* regularly process display answers */
305
timer_add(drv_Curs_timer, NULL, 100, 0);
308
if ((ret = drv_Curs_start(section, quiet)) != 0) {
312
/* initialize generic text driver */
313
if ((ret = drv_generic_text_init(section, Name)) != 0)
316
/* initialize generic bar driver */
317
if ((ret = drv_generic_text_bar_init(1)) != 0)
320
/* initialize generic key pad driver */
321
if ((ret = drv_generic_keypad_init(section, Name)) != 0)
324
/* add fixed chars to the bar driver */
325
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
326
drv_generic_text_bar_add_segment(255, 255, 255, '*'); /* asterisk */
328
/* register text widget */
330
wc.draw = drv_generic_text_draw;
331
widget_register(&wc);
333
/* register bar widget */
335
wc.draw = drv_generic_text_bar_draw;
336
widget_register(&wc);
338
/* register plugins */
339
/* none at the moment... */
345
/* close driver & display */
346
int drv_Curs_quit(const int quiet)
349
info("%s: shutting down.", Name);
351
drv_generic_text_quit();
352
drv_generic_keypad_quit();
359
drv_generic_text_greet("goodbye!", NULL);
368
DRIVER drv_Curses = {
370
.list = drv_Curs_list,
371
.init = drv_Curs_init,
372
.quit = drv_Curs_quit,