1999-12-16 09:20:22 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-12-09 23:15:14 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <sys/errno.h>
1999-12-16 09:20:22 +00:00
#ifdef HAVE_NCURSES_H
1999-12-09 23:15:14 +00:00
#include <ncurses.h>
#else
#include <curses.h>
#endif
#include "../../shared/str.h"
#include "lcd.h"
#include "curses_drv.h"
#include "drv_base.h"
lcd_logical_driver * curses_drv ;
2000-03-30 20:20:41 +00:00
int PAD = 255 ;
int ELLIPSIS = 7 ;
1999-12-09 23:15:14 +00:00
//////////////////////////////////////////////////////////////////////////
////////////////////// For Curses Terminal Output ////////////////////////
//////////////////////////////////////////////////////////////////////////
static char icon_char = '@' ;
2000-03-30 20:20:41 +00:00
int
curses_drv_init ( struct lcd_logical_driver * driver , char * args )
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
char * argv [ 64 ];
int argc ;
int i , j ;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
argc = get_args ( argv , args , 64 );
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
for ( i = 0 ; i < argc ; i ++ ) {
if ( 0 == strcmp ( argv [ i ], "-f" ) || 0 == strcmp ( argv [ i ], "--forecolor" )) {
if ( i + 1 > argc ) {
fprintf ( stderr , "curses_init: %s requires an argument \n " , argv [ i ]);
return - 1 ;
}
// TODO: color display in curses driver!
printf ( "Sorry, colors not yet implemented... \n " );
//strcpy(device, argv[++i]);
} else if ( 0 == strcmp ( argv [ i ], "-b" ) || 0 == strcmp ( argv [ i ], "--backcolor" )) {
if ( i + 1 > argc ) {
fprintf ( stderr , "curses_init: %s requires an argument \n " , argv [ i ]);
return - 1 ;
}
// TODO: color display in curses driver!
printf ( "Sorry, colors not yet implemented... \n " );
//strcpy(device, argv[++i]);
} else if ( 0 == strcmp ( argv [ i ], "-B" ) || 0 == strcmp ( argv [ i ], "--backlight" )) {
if ( i + 1 > argc ) {
fprintf ( stderr , "curses_init: %s requires an argument \n " , argv [ i ]);
return - 1 ;
}
// TODO: color display in curses driver!
printf ( "Sorry, colors not yet implemented... \n " );
//strcpy(device, argv[++i]);
} else if ( 0 == strcmp ( argv [ i ], "-h" ) || 0 == strcmp ( argv [ i ], "--help" )) {
printf ( "LCDproc [n]curses driver \n " " \t -f \t --forecolor \t Change the foreground color \n " " \t -b \t --backcolor \t Change the backlight's \" off \" color \n " " \t -B \t --backlight \t Change the backlight's \" on \" color \n " " \t -h \t --help \t\t Show this help information \n " );
return - 1 ;
} else {
printf ( "Invalid parameter: %s \n " , argv [ i ]);
}
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
curses_drv = driver ;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
// Init curses...
initscr ();
cbreak ();
noecho ();
nodelay ( stdscr , TRUE );
nonl ();
intrflush ( stdscr , FALSE );
keypad ( stdscr , TRUE );
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
curses_drv_clear ();
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
// Override output functions...
driver -> clear = curses_drv_clear ;
driver -> string = curses_drv_string ;
driver -> chr = curses_drv_chr ;
driver -> vbar = curses_drv_vbar ;
driver -> init_vbar = NULL ;
driver -> hbar = curses_drv_hbar ;
driver -> init_hbar = NULL ;
driver -> num = curses_drv_num ;
driver -> init_num = curses_drv_init_num ;
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
driver -> init = curses_drv_init ;
driver -> close = curses_drv_close ;
driver -> flush = curses_drv_flush ;
driver -> flush_box = curses_drv_flush_box ;
driver -> contrast = NULL ;
driver -> backlight = NULL ;
driver -> set_char = NULL ;
driver -> icon = curses_drv_icon ;
driver -> draw_frame = curses_drv_draw_frame ;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
driver -> getkey = curses_drv_getkey ;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
// Change the character used for padding the title bars...
PAD = '#' ;
// Change the character used for "..."
ELLIPSIS = '~' ;
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
return 200 ; // 200 is arbitrary. (must be 1 or more)
1999-12-09 23:15:14 +00:00
}
2000-03-30 20:20:41 +00:00
void
curses_drv_close ()
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
// Close curses
clear ();
move ( 0 , 0 );
refresh ();
endwin ();
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
drv_base_close ();
1999-12-09 23:15:14 +00:00
}
/////////////////////////////////////////////////////////////////
// Clears the LCD screen
//
2000-03-30 20:20:41 +00:00
void
curses_drv_clear ()
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
clear ();
2000-03-30 20:20:41 +00:00
1999-12-09 23:15:14 +00:00
}
/////////////////////////////////////////////////////////////////
// Prints a string on the lcd display, at position (x,y). The
// upper-left is (1,1), and the lower right should be (20,4).
//
2000-03-30 20:20:41 +00:00
void
curses_drv_string ( int x , int y , char string [])
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int i ;
unsigned char * c ;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
for ( i = 0 ; string [ i ]; i ++ ) {
c = & string [ i ];
switch ( * c ) {
case 0 :
* c = icon_char ;
break ;
case 255 :
* c = '#' ;
break ;
}
}
mvaddstr ( y - 1 , x - 1 , string );
1999-12-09 23:15:14 +00:00
}
/////////////////////////////////////////////////////////////////
// Prints a character on the lcd display, at position (x,y). The
// upper-left is (1,1), and the lower right should be (20,4).
//
2000-03-30 20:20:41 +00:00
void
curses_drv_chr ( int x , int y , char c )
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
switch ( c ) {
case 0 :
c = icon_char ;
break ;
case - 1 :
c = '#' ;
break ;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
mvaddch ( y - 1 , x - 1 , c );
1999-12-09 23:15:14 +00:00
}
/////////////////////////////////////////////////////////////////
// Sets up for big numbers.
//
2000-03-30 20:20:41 +00:00
void
curses_drv_init_num ()
1999-12-09 23:15:14 +00:00
{
}
/////////////////////////////////////////////////////////////////
// Writes a big number.
//
2000-03-30 20:20:41 +00:00
void
curses_drv_num ( int x , int num )
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
char c ;
int y , dx ;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
c = '0' + num ;
1999-12-09 23:15:14 +00:00
// printf(&c,"%1d",num);
2000-04-03 22:13:57 +00:00
for ( y = 1 ; y < 5 ; y ++ )
for ( dx = 0 ; dx < 3 ; dx ++ )
curses_drv_chr ( x + dx , y , c );
1999-12-09 23:15:14 +00:00
}
/////////////////////////////////////////////////////////////////
// Draws a vertical bar; erases entire column onscreen.
//
2000-03-30 20:20:41 +00:00
void
curses_drv_vbar ( int x , int len )
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
char map [] = "_.,,ooO8" ;
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
int y ;
for ( y = lcd . hgt ; y > 0 && len > 0 ; y -- ) {
if ( len >= lcd . cellhgt )
curses_drv_chr ( x , y , '8' );
else
curses_drv_chr ( x , y , map [ len - 1 ]);
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
len -= lcd . cellhgt ;
}
1999-12-09 23:15:14 +00:00
// move(4-len/lcd.cellhgt, x-1);
// vline(0, len/lcd.cellhgt);
2000-03-30 20:20:41 +00:00
1999-12-09 23:15:14 +00:00
}
/////////////////////////////////////////////////////////////////
// Draws a horizontal bar to the right.
//
2000-03-30 20:20:41 +00:00
void
curses_drv_hbar ( int x , int y , int len )
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
for (; x <= lcd . wid && len > 0 ; x ++ ) {
if ( len >= lcd . cellwid )
curses_drv_chr ( x , y , '=' );
else
curses_drv_chr ( x , y , '-' );
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
len -= lcd . cellwid ;
}
1999-12-09 23:15:14 +00:00
// move(y-1, x-1);
// hline(0, len/lcd.cellwid);
}
/////////////////////////////////////////////////////////////////
// Sets character 0 to an icon...
//
2000-03-30 20:20:41 +00:00
void
curses_drv_icon ( int which , char dest )
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
if ( dest == 0 )
switch ( which ) {
case 0 :
icon_char = '+' ;
break ;
case 1 :
icon_char = '*' ;
break ;
default :
icon_char = '#' ;
break ;
}
2000-03-30 20:20:41 +00:00
1999-12-09 23:15:14 +00:00
}
//////////////////////////////////////////////////////////////////
// Flushes all output to the lcd...
//
2000-03-30 20:20:41 +00:00
void
curses_drv_flush ()
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
curses_drv_draw_frame ( curses_drv -> framebuf );
1999-12-09 23:15:14 +00:00
}
2000-03-30 20:20:41 +00:00
void
curses_drv_flush_box ( int lft , int top , int rgt , int bot )
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
curses_drv_flush ();
1999-12-09 23:15:14 +00:00
}
2000-03-30 20:20:41 +00:00
void
curses_drv_draw_frame ( char * dat )
1999-12-09 23:15:14 +00:00
{
// border(0, 0, 0, 0, 0, 0, lcd.wid+1, lcd.hgt+1);
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
refresh ();
1999-12-09 23:15:14 +00:00
}
2000-03-30 20:20:41 +00:00
char
curses_drv_getkey ()
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int i ;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
i = getch ();
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
if ( i == KEY_LEFT )
return 'D' ;
if ( i == KEY_UP )
return 'B' ;
if ( i == KEY_DOWN )
return 'C' ;
if ( i == KEY_RIGHT )
return 'A' ;
2000-03-30 20:20:41 +00:00
2000-04-03 22:13:57 +00:00
if ( i != ERR )
return i ;
else
return 0 ;
2000-03-30 20:20:41 +00:00
1999-12-09 23:15:14 +00:00
}