Files
lcdproc/server/drivers/glk.c
T

756 lines
18 KiB
C
Raw Normal View History

/*
* MatrixOrbital GLK Graphic Display Driver
*
* http://www.matrixorbital.com
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <sys/errno.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include "lcd.h"
#include "shared/str.h"
#include "glk.h"
#include "glkproto.h"
2005-01-29 18:49:29 +00:00
#include "report.h"
#define GLK_DEFAULT_DEVICE "/dev/lcd"
#define GLK_DEFAULT_SPEED 19200
#define GLK_DEFAULT_CONTRAST 560
//////////////////////////////////////////////////////////////////////////
//////////////////// Matrix Orbital Graphical Driver /////////////////////
//////////////////////////////////////////////////////////////////////////
2006-04-10 11:22:47 +00:00
typedef struct driver_private_data {
char device[256];
GLKDisplay *PortFD;
speed_t speed;
unsigned char *screen_contents;
int fontselected;
int gpo_count;
unsigned char *framebuf;
int width;
int height;
int cellwidth;
int cellheight;
int contrast;
int clearcount;
unsigned char CGRAM[8];
} PrivateData;
2001-12-30 00:15:25 +00:00
// Vars for the server core
MODULE_EXPORT char *api_version = API_VERSION;
MODULE_EXPORT int stay_in_foreground = 1;
MODULE_EXPORT int supports_multiple = 0;
MODULE_EXPORT char *symbol_prefix = "glk_";
////////////////////////////////////////////////////////////
// init() should set up any device-specific stuff, and
// point all the function pointers.
2002-02-21 22:50:12 +00:00
MODULE_EXPORT int
glk_init(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p;
int i;
/* Allocate and store private data */
p = (PrivateData *) calloc(1, sizeof(PrivateData));
if (p == NULL)
return -1;
if (drvthis->store_private_ptr(drvthis, p))
return -1;
/* initialize private data */
p->PortFD = NULL;
p->speed = GLK_DEFAULT_SPEED;
p->screen_contents = NULL;
p->fontselected = -1; // No font selected
p->gpo_count = 0;
p->framebuf = NULL;
p->cellwidth = LCD_DEFAULT_CELLWIDTH;
p->cellheight = LCD_DEFAULT_CELLHEIGHT;
p->contrast = GLK_DEFAULT_SPEED;
p->clearcount = 0;
2005-01-29 18:49:29 +00:00
/* Read config file */
2005-01-29 18:49:29 +00:00
/* What device should be used */
2006-04-10 11:22:47 +00:00
strncpy(p->device, drvthis->config_get_string(drvthis->name, "Device", 0,
GLK_DEFAULT_DEVICE), sizeof(p->device));
p->device[sizeof(p->device)-1] = '\0';
report(RPT_INFO, "%s: using Device %s", drvthis->name, p->device);
2005-01-29 18:49:29 +00:00
/* What speed to use */
2006-04-10 11:22:47 +00:00
p->speed = drvthis->config_get_int(drvthis->name, "Speed", 0, 19200);
2005-01-29 18:49:29 +00:00
2006-04-10 11:22:47 +00:00
if (p->speed == 9600) p->speed = B9600;
else if (p->speed == 19200) p->speed = B19200;
else if (p->speed == 38400) p->speed = B38400;
2005-01-29 18:49:29 +00:00
else {
report(RPT_WARNING, "%s: illegal Speed: %d; must be one of 9600, 19200 or 38400; using default %d",
2006-04-10 11:22:47 +00:00
drvthis->name, p->speed, 19200);
p->speed = B19200;
2005-01-29 18:49:29 +00:00
}
2006-04-10 11:22:47 +00:00
/* Which p->contrast */
p->contrast = drvthis->config_get_int(drvthis->name, "Contrast" , 0 , GLK_DEFAULT_CONTRAST);
if ((p->contrast < 0) || (p->contrast > 1000)) {
report(RPT_WARNING, "%s: Contrast must be between 0 and 1000. Using default %d",
drvthis->name, GLK_DEFAULT_CONTRAST);
2006-04-10 11:22:47 +00:00
p->contrast = GLK_DEFAULT_CONTRAST;
2005-01-29 18:49:29 +00:00
}
/* End of config file parsing */
/* open device */
2006-04-10 11:22:47 +00:00
p->PortFD = glkopen(p->device, p->speed);
if (p->PortFD == NULL) {
report(RPT_ERR, "%s: unable to open device %s", drvthis->name, p->device);
return -1;
}
// Query the module for a device type
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x37, EOF);
i = glkget(p->PortFD);
if (i < 0) {
report(RPT_ERR, "%s: GLK did not respond to READ MODULE TYPE", drvthis->name);
return -1;
}
else {
switch (i) {
case 0x10 : // GLC12232
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 4;
break;
case 0x11 : // GLC12864
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 8;
break;
case 0x12 : // GLC128128
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 16;
break;
case 0x13 : // GLC24064
2006-04-10 11:22:47 +00:00
p->width = 40; p->height = 8; p->gpo_count = 1;
break;
case 0x14 : // GLK12864-25
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 8;
break;
case 0x15 : // GLK24064-25
2006-04-10 11:22:47 +00:00
p->width = 40; p->height = 8; p->gpo_count = 1;
break;
case 0x21 : // GLK128128-25
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 16;
break;
case 0x22 : // GLK12232-25
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 4; p->gpo_count = 2;
break;
case 0x23 : // GLK12232-25SM
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 4; p->gpo_count = 2;
break;
case 0x24 : // GLK12232-25SM-Penguin
2006-04-10 11:22:47 +00:00
p->width = 20; p->height = 4; p->gpo_count = 2;
break;
default :
report(RPT_ERR, "%s: unrecognized module type: 0x%02X", drvthis->name, i);
return -1;
}
}
2006-04-10 11:22:47 +00:00
p->framebuf = malloc(p->width * p->height);
p->screen_contents = malloc(p->width * p->height);
2006-04-10 11:22:47 +00:00
if (p->framebuf == NULL || p->screen_contents == NULL) {
report(RPT_ERR, "%s: Unable to allocate memory for screen buffers", drvthis->name);
2001-12-30 00:15:25 +00:00
glk_close(drvthis);
return -1;
}
2006-04-10 11:22:47 +00:00
memset(p->framebuf, ' ', p->width * p->height);
// glk_clear();
2006-04-10 11:22:47 +00:00
// glkputl(p->PortFD, GLKCommand, 0x58, EOF);
// Enable flow control
2006-04-10 11:22:47 +00:00
glkflow(p->PortFD, 40, 2);
// Set read character timeout to 0
2006-04-10 11:22:47 +00:00
glktimeout(p->PortFD, 0);
// Enable auto-transmit of up/down key events
// This allows us to generate REPEAT keys distinct from
// normal keys using timeouts. (see glk_get_key)
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x7e, 1, GLKCommand, 0x41, EOF);
2006-04-10 11:22:47 +00:00
// Set p->contrast
glk_set_contrast(drvthis, p->contrast);
report(RPT_DEBUG, "%s: init() done", drvthis->name);
return 1;
}
2001-12-30 00:15:25 +00:00
/////////////////////////////////////////////////////////////////
// Close the driver
//
MODULE_EXPORT void
glk_close(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
2001-12-30 00:15:25 +00:00
2006-04-10 11:22:47 +00:00
if (p != NULL) {
if (p->PortFD != NULL)
glkclose(p->PortFD);
2006-04-10 11:22:47 +00:00
if (p->framebuf != NULL)
free(p->framebuf);
if (p->screen_contents != NULL)
free(p->screen_contents);
free(p);
}
drvthis->store_private_ptr(drvthis, NULL);
}
2001-12-30 00:15:25 +00:00
/////////////////////////////////////////////////////////////////
// Returns the display width
//
MODULE_EXPORT int
glk_width (Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
return p->width;
2001-12-30 00:15:25 +00:00
}
/////////////////////////////////////////////////////////////////
// Returns the display height
//
MODULE_EXPORT int
glk_height (Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
return p->height;
}
/////////////////////////////////////////////////////////////////
// Returns the display's cell width
//
MODULE_EXPORT int
glk_cellwidth (Driver *drvthis)
{
PrivateData *p = drvthis->private_data;
return p->cellwidth;
}
/////////////////////////////////////////////////////////////////
// Returns the display's cell height
//
MODULE_EXPORT int
glk_cellheight (Driver *drvthis)
{
PrivateData *p = drvthis->private_data;
return p->cellheight;
2001-12-30 00:15:25 +00:00
}
/////////////////////////////////////////////////////////////////
// Clears the LCD screen
//
#define CLEARCOUNT (1000000)
2001-12-30 00:15:25 +00:00
void glk_clear_forced(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
p->clearcount = CLEARCOUNT;
glkputl(p->PortFD, GLKCommand, 0x58, EOF);
memset(p->screen_contents, ' ', p->width * p->height);
}
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_clear(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
memset(p->framebuf, ' ', p->width * p->height);
if (--p->clearcount < 0)
2001-12-30 00:15:25 +00:00
glk_clear_forced(drvthis);
}
//////////////////////////////////////////////////////////////////
// Flushes all output to the lcd...
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_flush(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
// puts("glk_flush()");
2006-04-10 11:22:47 +00:00
unsigned char *pf = p->framebuf;
unsigned char *qf = p->screen_contents;
int x, y;
2006-04-10 11:22:47 +00:00
unsigned char *ps= NULL;
2001-12-30 00:15:25 +00:00
debug(RPT_DEBUG, "flush()");
2001-12-30 00:15:25 +00:00
2006-04-10 11:22:47 +00:00
for (y = 0; y < p->height; ++y) {
int xs = -1; /* XStart not set */
for (x = 0; x < p->width; ++x) {
if ((*qf == *pf) && (xs >= 0)) {
2001-12-30 00:15:25 +00:00
/* Write accumulated string */
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x79, xs * 6 + 1, y * 8, EOF);
glkputa(p->PortFD, x - xs, ps);
debug(RPT_DEBUG, "flush: Writing at (%d,%d) for %d", xs, y, x - xs);
xs = -1;
2006-04-10 11:22:47 +00:00
} else if ((*qf != *pf) && (xs < 0)) {
2001-12-30 00:15:25 +00:00
/* Start new string of changes */
2006-04-10 11:22:47 +00:00
ps = pf;
xs = x;
}
2006-04-10 11:22:47 +00:00
*qf++ = *pf++; /* Update screen_contents from framebuf */
}
if (xs >= 0) {
2001-12-30 00:15:25 +00:00
/* Write accumulated line */
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x79, xs * 6 + 1, y * 8, EOF);
glkputa(p->PortFD, p->width - xs, ps);
debug(RPT_DEBUG, "flush: Writing at (%d,%d) for %d", xs, y, p->width - xs);
}
} /* For y */
}
/////////////////////////////////////////////////////////////////
// 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).
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_string(Driver *drvthis, int x, int y, char string[])
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
char *s;
debug(RPT_DEBUG, "glk_string(%d, %d, \"%s\")", x, y, string);
2006-04-10 11:22:47 +00:00
if ((y <= 0) || (y > p->height))
return;
2006-04-10 11:22:47 +00:00
for (s = string; (*s != '\0') && (x <= p->width); s++, x++) {
glk_chr(drvthis, x, y, *s);
}
}
/////////////////////////////////////////////////////////////////
// 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).
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_chr(Driver *drvthis, int x, int y, char c)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
int myc = (unsigned char) c;
2006-04-10 11:22:47 +00:00
x--; // Convert 1-based coords to 0-based...
y--;
if (p->fontselected != 2) {
debug(RPT_DEBUG, "Switching to font 2");
/* Select font 2 */
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x31, 2, EOF);
p->fontselected = 2;
/* Set font metrics */
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x32, 1, 0, 1, 1, 32, EOF);
/* Clear the screen */
2001-12-30 00:15:25 +00:00
glk_clear_forced(drvthis);
}
if ((myc >= 0) && (myc <= 15)) {
/* CGRAM */
2006-04-10 11:22:47 +00:00
debug(RPT_DEBUG, "CGRAM changing %d => %d", myc, p->CGRAM[myc & 7]);
myc = p->CGRAM[myc & 7];
} else if ((myc == 255) || (myc == -1)) {
/* Solid block */
myc = 133;
} else if (((myc > 15) && (myc < 32)) || (myc > 143)) {
debug(RPT_DEBUG, "Attempt to write %d to (%d,%d)", myc, x, y);
myc = 133;
}
2001-12-30 00:15:25 +00:00
2006-04-10 11:22:47 +00:00
if ((x >= 0) && (y >= 0) && (x < p->width) && (y < p->height))
p->framebuf[(y * p->width) + x] = myc;
2001-12-30 00:15:25 +00:00
}
/////////////////////////////////////////////////////////////////
2006-04-10 11:22:47 +00:00
// Returns current p->contrast
2001-12-30 00:15:25 +00:00
// This is only the locally stored contrast, the contrast value
// cannot be retrieved from the LCD.
// Value 0 to 1000.
//
MODULE_EXPORT int
glk_get_contrast(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
return p->contrast;
}
//////////////////////////////////////////////////////////////////////
2006-04-10 11:22:47 +00:00
// Sets the p->contrast of the display. Value is 0-255, where 140 is
// what I consider "just right".
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_set_contrast(Driver *drvthis, int promille)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
2001-12-30 00:15:25 +00:00
// Check it
if ((promille < 0) || (promille > 1000))
return;
2001-12-30 00:15:25 +00:00
// Store it
2006-04-10 11:22:47 +00:00
p->contrast = promille;
2001-12-30 00:15:25 +00:00
2006-04-10 11:22:47 +00:00
// Do it: map logical [0, 1000] -> physical [0, 255] for the hardware
debug(RPT_DEBUG, "Contrast: %d", p->contrast);
glkputl(p->PortFD, GLKCommand, 0x50, (int) ((long) promille * 255 / 1000), EOF);
}
//////////////////////////////////////////////////////////////////////
// Turns the lcd backlight on or off...
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_backlight(Driver *drvthis, int on)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
if (on) {
debug(RPT_DEBUG, "Backlight ON");
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x42, 0, EOF);
}
else {
debug(RPT_DEBUG, "Backlight OFF");
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x46, EOF);
}
}
//////////////////////////////////////////////////////////////////////
// Sets general purpose outputs on or off
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_output(Driver *drvthis, int on)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
if (p->gpo_count < 2) {
glkputl(p->PortFD, GLKCommand, ((on) ? 'W' : 'V'), EOF);
}
else {
int i;
2006-04-10 11:22:47 +00:00
for (i = 1; i <= p->gpo_count; ++i, on >>= 1) {
glkputl(p->PortFD, GLKCommand, ((on & 1) ? 'W' : 'V'), i, EOF);
}
}
}
//////////////////////////////////////////////////////////////////////
// Tells the driver to get ready for vertical bargraphs.
//
static void
2001-12-30 00:15:25 +00:00
glk_init_vbar(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
//PrivateData *p = drvthis->private_data;
debug(RPT_DEBUG, "glk_init_vbar()");
}
//////////////////////////////////////////////////////////////////////
// Tells the driver to get ready for horizontal bargraphs.
//
static void
2001-12-30 00:15:25 +00:00
glk_init_hbar(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
//PrivateData *p = drvthis->private_data;
debug(RPT_DEBUG, "glk_init_hbar()");
}
//////////////////////////////////////////////////////////////////////
// Tells the driver to get ready for big numbers, if possible.
//
static void
2001-12-30 00:15:25 +00:00
glk_init_num(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
debug(RPT_DEBUG, "glk_init_num()");
2006-04-10 11:22:47 +00:00
if (p->fontselected != 3) {
/* Select Big Numbers font */
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x31, 3, EOF);
p->fontselected = 3;
/* Set font metrics */
2006-04-10 11:22:47 +00:00
glkputl(p->PortFD, GLKCommand, 0x32, 1, 0, 1, 1, 32, EOF);
/* Clear the screen */
2001-12-30 00:15:25 +00:00
glk_clear_forced(drvthis);
}
}
//////////////////////////////////////////////////////////////////////
// Draws a big (4-row) number.
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_num(Driver *drvthis, int x, int num)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
debug(RPT_DEBUG, "glk_num(%d, %d)", x, num);
2006-04-10 11:22:47 +00:00
if ((num < 0) || (num > 10))
return;
2006-04-10 11:22:47 +00:00
if ((x > 0) && (x <= p->width))
p->framebuf[x-1] = (num >= 10) ? ':' : (num + '0');
}
//////////////////////////////////////////////////////////////////////
// Changes the font data of character n.
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
glk_set_char(Driver *drvthis, int n, char *dat)
{
2006-04-10 11:22:47 +00:00
//PrivateData *p = drvthis->private_data;
debug(RPT_DEBUG, "glk_set_char(%d)", n);
}
/////////////////////////////////////////////////////////////////
// Draws a vertical bar, from the bottom of the screen up.
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
2002-05-23 18:03:36 +00:00
glk_old_vbar(Driver *drvthis, int x, int len)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
int y = p->height;
debug(RPT_DEBUG, "glk_old_vbar(%d, %d)", x, len);
2006-04-10 11:22:47 +00:00
while (len > p->cellheight) {
glk_chr(drvthis, x, y, 255);
--y;
2006-04-10 11:22:47 +00:00
len -= p->cellheight;
}
if (y >= 0) {
int lastc;
switch (len) {
case 0 : return; break; /* Don't output a char */
case 1 : lastc = 138; break; /* One bar */
case 2 : lastc = 139; break;
case 3 : lastc = 140; break;
case 4 : lastc = 141; break;
case 5 : lastc = 142; break;
case 6 : lastc = 143; break;
default: lastc = 133; break;
}
glk_chr(drvthis, x, y, lastc);
}
}
/////////////////////////////////////////////////////////////////
// Draws a horizontal bar to the right.
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
2002-05-23 18:03:36 +00:00
glk_old_hbar(Driver *drvthis, int x, int y, int len)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
debug(RPT_DEBUG, "glk_old_hbar(%d, %d, %d)", x, y, len);
2006-04-10 11:22:47 +00:00
while (len > p->cellwidth) {
glk_chr(drvthis, x, y, 255);
++x;
2006-04-10 11:22:47 +00:00
len -= p->cellwidth;
}
2006-04-10 11:22:47 +00:00
if (x <= p->width) {
int lastc;
switch (len) {
case 0 : lastc = ' '; break;
case 1 : lastc = 134; break; /* One bar */
case 2 : lastc = 135; break;
case 3 : lastc = 136; break;
case 4 : lastc = 137; break;
default: lastc = 133; break;
}
glk_chr(drvthis, x, y, lastc);
}
}
/////////////////////////////////////////////////////////////////
// Sets character 0 to an icon...
//
2001-12-30 00:15:25 +00:00
MODULE_EXPORT void
2002-05-23 18:03:36 +00:00
glk_old_icon(Driver *drvthis, int which, int dest)
{
2002-05-23 18:03:36 +00:00
/* TODO IMPLEMENTATION OF NEW API */
/* any volonteers ? */
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
unsigned char old, new;
2006-04-10 11:22:47 +00:00
unsigned char *pf = p->framebuf;
unsigned char *qf = p->screen_contents;
int count;
debug(RPT_DEBUG, "glk_old_icon(%i, %i)", which, dest);
if ((dest < 0) || (dest > 7)) {
/* Illegal custom character */
return;
}
/* which == 0 => empty heart => 131
* which == 1 => filled heart => 132
* which == 2 => ellipsis => 128
*/
switch (which) {
case 0: new = 131; break;
case 1: new = 132; break;
case 2: new = 128; break;
default: return; /* ERROR */
}
2006-04-10 11:22:47 +00:00
old = p->CGRAM[(int) dest];
p->CGRAM[(int) dest] = new;
/* Replace all old icons with new icon in new frame */
2006-04-10 11:22:47 +00:00
for (count = p->width * p->height; count > 0; count--) {
if (*qf == old) {
debug(RPT_DEBUG, "icon %d to %d at %d", old, new, qf - p->screen_contents);
*pf = new;
}
2006-04-10 11:22:47 +00:00
++qf; ++pf;
}
}
//////////////////////////////////////////////////////////////////////
// Tries to read a character from an input device...
//
// Return NULL for "nothing available".
//
MODULE_EXPORT const char *
glk_get_key(Driver *drvthis)
{
2006-04-10 11:22:47 +00:00
PrivateData *p = drvthis->private_data;
int c;
static int keycode = -1;
static struct timeval lastkey;
struct timeval now;
const char *key = NULL;
2001-12-30 00:15:25 +00:00
debug(RPT_DEBUG, "glk_get_key()");
2006-04-10 11:22:47 +00:00
c = glkgetc(p->PortFD);
if ((c >= 'A') && (c <= 'Z')) {
/* Key down event */
keycode = c;
gettimeofday(&lastkey, NULL);
debug(RPT_DEBUG, "KEY %c at %ld.%06ld", c, lastkey.tv_sec, lastkey.tv_usec);
} else if ((c >= 'a') && (c <= 'z')) {
/* Key up event */
debug(RPT_DEBUG, "KEY %c UP", c);
keycode = -1;
c = 0;
} else {
/* Assume timeout */
c = 0;
if (keycode > 0) {
int msec_diff;
/* A key is down */
gettimeofday(&now, NULL);
msec_diff = (now.tv_sec - lastkey.tv_sec) * 1000;
msec_diff += (now.tv_usec - lastkey.tv_usec) / 1000;
debug(RPT_DEBUG, "KEY %c down for %d msec", keycode, msec_diff);
if (msec_diff > 1000) {
/* Generate repeat event */
c = keycode | 0x20; /* Upper case to lower case */
++lastkey.tv_sec; /* HACK HACK. repeat at 1 sec intervals */
debug(RPT_DEBUG, "KEY %c REPEAT", c);
}
}
}
/* Remap keys according to what LCDproc expects */
switch (c) {
case 'V' : key = "Enter";
break;
case 'P' : key = "Left";
break;
case 'Q' : key = "Right";
break;
case 'L' : key = "Escape";
break;
case 'U' : key = "Up";
break;
case 'K' : key = "Down";
break;
default : break;
// What to do with repeated keys? We currently ignore them.
//case 'v' : c = 'N'; break;
//case 'p' : c = 'O'; break;
//case 'q' : c = 'P'; break;
//case 'l' : c = 'Q'; break;
//case 'u' : c = 'R'; break;
//case 'k' : c = 'S'; break;
}
debug(RPT_DEBUG, "%s_ get_key() returns %s", drvthis->name, (key != NULL) ? key : "<null>");
return key;
}