home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
me34exe.zip
/
me
/
misc
/
emx1io.c
next >
Wrap
C/C++ Source or Header
|
1995-01-21
|
11KB
|
389 lines
/*
* OS2IO.C: Terminal IO for OS/2
* J Burnell 3/92 Public Domain
* P McPhee replaced signal handling code (to compile with emx)
* copied set_color and set_cursor stuff from pcfv.c
*/
/*
* The functions in this file negotiate with the operating system for
* characters, and write characters in a barely buffered fashion on the
* display.
*/
#include <stdio.h>
#define INCL_BASE
#define INCL_NOPM
#define INCL_VIO
#define INCL_KBD
#include <os2.h>
#include <time.h>
#include "ed.h"
#include "term.h"
#include <signal.h>
#include <string.h>
#include "me2.h"
#include "config.h"
KBDINFO initialKbdInfo; /* keyboard info */
void t_eeol ();
/*
* This function is called once to set up the terminal device streams.
*/
void ttopen()
{
#ifdef SIG_HANDLER_IS_THERE
PFNSIGHANDLER oldhandler;
#endif
USHORT oldact;
KBDINFO kbdInfo;
#ifdef SIG_HANDLER_IS_THERE
/* turn off control C checking */
DosSetSigHandler((PFNSIGHANDLER) NULL, &oldhandler, &oldact,
SIGA_IGNORE, SIG_CTRLBREAK);
DosSetSigHandler((PFNSIGHANDLER) NULL, &oldhandler, &oldact,
SIGA_IGNORE, SIG_CTRLC);
#else
/* I think this does the same thing */
signal(SIGBREAK, SIG_IGN);
signal(SIGINT, SIG_IGN);
#endif
/* set up the keyboard */
initialKbdInfo.cb = sizeof(initialKbdInfo);
KbdGetStatus(&initialKbdInfo, 0);
kbdInfo = initialKbdInfo;
kbdInfo.fsMask &= ~0x0001; /* not echo on */
kbdInfo.fsMask |= 0x0002; /* echo off */
kbdInfo.fsMask &= ~0x0008; /* cooked mode off */
kbdInfo.fsMask |= 0x0004; /* raw mode */
kbdInfo.fsMask &= ~0x0100; /* shift report off */
KbdSetStatus(&kbdInfo, 0);
}
/*
* This function gets called just before we go back home to the command
* interpreter.
*/
void t_close()
{
/* close the keyboard */
KbdSetStatus(&initialKbdInfo, 0); /* restore original state */
}
/*
* Write a character to the display.
* Use the rawest console output routine that handles backspace, \r and \n.
* On MS-DOS terminal I/O is unbuffered, so we just write the byte out.
*/
void t_putchar(c) unsigned char c;
{
VioWrtTTY ((PCH)&c, 1, 0);
}
/*
* Flush terminal buffer. Does real work where the terminal output is
* buffered up. A no-operation on systems where byte-at-a-time terminal
* I/O is done.
*/
void t_flush() {}
/*
* Read a character from the terminal, performing no editing and doing no
* echo at all.
* Map terminal softkeys, etc to ME keys. Do *NOT* map control keys.
*/
EKeyCode t_getchar()
{
EKeyCode keycode;
KBDKEYINFO keyInfo;
KbdCharIn(&keyInfo, IO_WAIT, 0); /* get a character */
if (keyInfo.chChar == 0 || (keyInfo.chChar == 0xE0 && keyInfo.chChar != 0))
{ if (map_key(keyInfo.chScan | (keyInfo.chChar << 8),&keycode))
return keycode; }
else return (EKeyCode)keyInfo.chChar;
}
/*
* Check to see is a key is waiting.
* Used to interrupt processing if input is pending or to detect if a soft
* key has been hit (the terminal sends multiple characters for each
* softkey).
* Don't use function B because it checks for ^C.
*/
int keywaiting()
{
KBDKEYINFO keyInfo;
KbdPeek(&keyInfo, 0); /* look for a character */
return keyInfo.fbStatus & KBDTRF_FINAL_CHAR_IN;
}
/* Wait for a key to be pressed for no more than sec seconds.
* Use 0 seconds to check to see if a key is in the input que.
* Warning: This rouine is only good for 32K seconds (about 9 hours).
* If sec is a long the limit is still 24 hours.
*/
wait_for_key(sec)
{
long int times_up;
if (sec == 0) return keywaiting();
times_up = time(NULL) +sec;
do
{
if (keywaiting()) return TRUE;
/* you can't poll the keyboard if you don't take a break */
DosSleep(100); /* 100 milliseconds (tenth of a second rule) */
} while (time(NULL) < times_up);
return FALSE;
}
/* Things to note about FastVideo:
* putline() will do all of the writing to the text area (except for
* help messages).
* t_eeol() will only be called for the message line or for help
* messages.
* t_eeop() need only clear the message line. It should use t_eeol() to
* do this.
* t_putchar() will only write to the message line, help messages or
* (puts "message").
* So, this means that the message line is really seprate from the text
* areas and t_putchar() and t_eeol() should use the same color (others
* will use tcolor and mcolor). Default for the message line would be
* tcolor. Help and (puts ...) messages will be written in message
* color (which is (I hope) reasonable).
* PC note: Due to the way the BIOS is done, it is easier to have
* t_putchar() and t_eeol() use the BIOS character attribute as the
* color.
*/
int t_nrow = 24, t_ncol = 0;
int tcolor = 7, mcolor = 96; /* text, modeline color defaults */
int zcolor = 7;
/*
* Called to set up the terminal. Also called after a spawn.
*/
void t_open()
{
VIOCURSORINFO cursorInfo, OldcursorInfo;
ttopen();
if (t_ncol == 0) fv_init(); /* only call fv_init() once */
/* set the cursor to a block */
VioGetCurType (&OldcursorInfo, 0);
cursorInfo.yStart = 1;
cursorInfo.cEnd = 15;
cursorInfo.cx = 1;
cursorInfo.attr = 1;
if (VioSetCurType (&cursorInfo, 0))
VioSetCurType (&OldcursorInfo, 0); /* reset to previous value */
}
int fv_init () {
VIOMODEINFO vioModeInfo;
vioModeInfo.cb = sizeof(vioModeInfo);
VioGetMode (&vioModeInfo, 0);
t_ncol = vioModeInfo.col;
t_nrow = vioModeInfo.row - 1; /* leave room for the message line */
}
void putline(row, buf, attr) /* note: row is 0 relative */
int row,attr; char *buf;
{
VioWrtCharStrAtt ((PCH) buf, t_ncol, row, 0, (PBYTE) &attr, 0);
}
/* Erase to end of page or screen.
* For fast video, each line of the screen (except for the message line)
* will be completely filled by putline() so all this routine needs to
* clear is the message (last) line of the screen.
*/
void t_eeop() { movecursor(t_nrow,0,TRUE); t_eeol(); }
/* Ring the bell. beeper controls the volume: 0 (don't beep), n (some
* volume value).
*/
void t_beep()
{
extern int beeper;
static int direction = 1;
if (beeper) {
if (direction) {
DosBeep(440, beeper * 2);
DosBeep(660, beeper * 2);
DosBeep(880, beeper * 4);
}
else {
DosBeep(880, beeper * 2);
DosBeep(660, beeper * 2);
DosBeep(440, beeper * 4);
}
direction ^= 1;
}
}
void t_move(row,col) int row, col; /* move cursor to (row,col) */
{
VioSetCurPos(row, col, 0);
}
void t_eeol() /* erase from cursor to end of line */
{
CHAR c = ' ';
USHORT CurCol, CurRow;
/* find the current cursor position */
VioGetCurPos ((PUSHORT) &CurRow, (PUSHORT) &CurCol, 0);
VioWrtNChar ((PCH) &c, t_ncol - CurCol, CurRow, CurCol, 0);
}
#include "driver.h"
#define RGB_BITS(bits) ((bits) & 0xF)
#define FOREGROUND_BITS(bits) ((bits) & 0xF)
#define BACKGROUND_BITS(bits) (((bits) >> 4) & 0x7)
#define MAKE_RGB(fg, bg) ( ((fg) & 0xF) | (((bg) & 7) << 4) )
static char *pc_color[] =
{ /* IRGB I can be 1 for forground colors only */
"black", /* 0000 == 0 */
"blue", /* 0001 == 1 */
"green", /* 0010 == 2 */
"cyan", /* 0011 == 3 */
"red", /* 0100 == 4 */
"magenta", /* 0101 == 5 */
"brown", /* 0110 == 6 */
"lightgrey", /* 0111 == 7 */
"darkgrey", /* 1000 == 8 */
"lightblue", /* 1001 == 9 */
"lightgreen", /* 1010 == 10 */
"lightcyan", /* 1011 == 11 */
"lightred", /* 1100 == 12 */
"lightmagenta", /* 1101 == 13 */
"yellow", /* 1110 == 14 */
"white", /* 1111 == 15 */
};
static int color_to_rgb(color_name, rgb, foreground)
char *color_name; int *rgb;
{
int i, max;
strlwr(color_name);
max = foreground ? NITEMS(pc_color) : (NITEMS(pc_color) + 1)/2;
for (i = 0; i < max; i++)
if (0 == strcmp(pc_color[i], color_name))
{
*rgb = i;
return TRUE;
}
return FALSE;
}
static char *rgb_to_color(rgb)
{
return pc_color[RGB_BITS(rgb)];
}
static void do_cursor(char * cursor_shape, int set);
/* "forground:background"
* ":" => no change
* "color" => only change foreground
* "color:" => only change foreground
* ":color" => only change background
* Input:
* color : name of color.
* rgb : current color (incase name of new color is bogus)
* Returns:
* New rgb bits.
*/
static int set_color(color, rgb) char *color;
{
char *ptr, buf[100];
int fg, bg;
strcpy(buf, color);
if (ptr = strchr(buf, ':')) *ptr++ = '\0';
if (!color_to_rgb(buf, &fg, TRUE)) fg = FOREGROUND_BITS(rgb);
if (!ptr || !color_to_rgb(ptr, &bg, FALSE)) bg = BACKGROUND_BITS(rgb);
return MAKE_RGB(fg, bg);
}
static void get_color(buf,rgb) char *buf;
{
strcpy(buf, rgb_to_color(FOREGROUND_BITS(rgb)));
strcat(buf, ":");
strcat(buf, rgb_to_color(BACKGROUND_BITS(rgb)));
}
int do_color(which, color, set) char *color;
{
int *c;
switch(which)
{
case TEXT_COLOR: c = &tcolor; break; /* text color */
case MODELINE_COLOR: c = &mcolor; break; /* modeline color */
case CURSOR_COLOR: /* cursor color */
if (set) return FALSE; /* can't change cursor color */
c = &tcolor; /* it is always the text color */
break;
case CURSOR_SHAPE: do_cursor(color, set); return TRUE;
default: return FALSE; /* boo boo */
}
if (set)
{
int x = *c;
return (x != (*c = set_color(color, x)));
}
else get_color(color, *c);
return FALSE;
}
static void do_cursor(cursor_shape, set) char *cursor_shape;
{
VIOCURSORINFO cursorInfo;
if (set)
{
char *ptr, buf[100];
strcpy(buf, cursor_shape);
if (ptr = strchr(buf, ':')) *ptr++ = '\0';
cursorInfo.yStart = (atoi(buf) & 0xF);
cursorInfo.cEnd = ptr ? (atoi(ptr) & 0xF) : 15;
cursorInfo.cx = 1;
cursorInfo.attr = 1;
VioSetCurType (&cursorInfo, 0);
return;
}
else {
VioSetCurType (&cursorInfo, 0);
sprintf(cursor_shape, "%d:%d", cursorInfo.yStart, cursorInfo.cEnd);
}
}
static MouseInfo mouse_info; /* !!!??? initialize? */
MouseInfo *get_mouse_info()
{
return &mouse_info;
}