Skip to content
Snippets Groups Projects
Commit 32d35460 authored by deuce's avatar deuce
Browse files

Numerous fixes...

- X mode segfault fixed.
- Curses raw on exit issue fixed (hopefully).
- Mouse support moved into ciolib from uifc.

Some of these changes may have already gotten in... the CVS server has been hateing me lately.
parent 6ea7dfc3
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,9 @@
#define CIOLIB_NO_MACROS
#include "ciolib.h"
#ifndef _WIN32
#ifdef _WIN32
#include "win32cio.h"
#else
#ifndef NO_X
#include "x_cio.h"
#endif
......@@ -65,6 +67,7 @@ int try_x_init(int mode)
{
if(!console_init()) {
cio_api.mode=CIOLIB_MODE_X;
cio_api.mouse=0;
cio_api.puttext=x_puttext;
cio_api.gettext=x_gettext;
cio_api.textattr=x_textattr;
......@@ -79,6 +82,9 @@ int try_x_init(int mode)
cio_api.getch=x_getch;
cio_api.getche=x_getche;
cio_api.textmode=x_textmode;
cio_api.getmouse=NULL;
cio_api.showmouse=NULL;
cio_api.hidemouse=NULL;
return(1);
}
fprintf(stderr,"X init failed\n");
......@@ -104,6 +110,9 @@ int try_curses_init(int mode)
cio_api.getch=curs_getch;
cio_api.getche=curs_getche;
cio_api.textmode=curs_textmode;
cio_api.getmouse=curs_getmouse;
cio_api.showmouse=curs_showmouse;
cio_api.hidemouse=curs_hidemouse;
return(1);
}
fprintf(stderr,"Curses init failed\n");
......@@ -115,6 +124,7 @@ int try_ansi_init(int mode)
{
if(ansi_initciolib(mode)) {
cio_api.mode=CIOLIB_MODE_ANSI;
cio_api.mouse=0;
cio_api.puttext=ansi_puttext;
cio_api.gettext=ansi_gettext;
cio_api.textattr=ansi_textattr;
......@@ -129,6 +139,9 @@ int try_ansi_init(int mode)
cio_api.getch=ansi_getch;
cio_api.getche=ansi_getche;
cio_api.textmode=ansi_textmode;
cio_api.getmouse=NULL;
cio_api.showmouse=NULL;
cio_api.hidemouse=NULL;
return(1);
}
fprintf(stderr,"ANSI init failed\n");
......@@ -144,19 +157,23 @@ int try_conio_init(int mode)
/* This should test for something or other */
if(isatty(fileno(stdout))) {
cio_api.mode=CIOLIB_MODE_CONIO;
cio_api.mouse=1;
cio_api.puttext=puttext;
cio_api.gettext=gettext;
cio_api.textattr=textattr;
cio_api.kbhit=kbhit;
cio_api.kbhit=win32_kbhit;
cio_api.wherey=wherey;
cio_api.wherex=wherex;
cio_api.putch=putch;
cio_api.gotoxy=gotoxy;
cio_api.gettextinfo=gettextinfo;
cio_api.setcursortype=_setcursortype;
cio_api.getch=getch;
cio_api.getche=getche;
cio_api.getch=win32_getch;
cio_api.getche=win32_getche;
cio_api.textmode=textmode;
cio_api.getmouse=win32_getmouse;
cio_api.showmouse=win32_showmouse;
cio_api.hidemouse=win32_hidemouse;
return(1);
}
fprintf(stderr,"CONIO init failed\n");
......@@ -164,7 +181,6 @@ int try_conio_init(int mode)
}
#endif
int initciolib(int mode)
{
switch(mode) {
......@@ -719,3 +735,25 @@ void ciolib_setcursortype(int a)
cio_api.setcursortype(a);
}
int ciolib_getmouse(struct cio_mouse_event *mevent) {
CIOLIB_INIT();
if(cio_api.getmouse!=NULL)
return(cio_api.getmouse(mevent));
return(-1);
}
int ciolib_showmouse(void) {
CIOLIB_INIT();
if(cio_api.showmouse!=NULL)
return(cio_api.showmouse());
}
int ciolib_hidemouse(void) {
CIOLIB_INIT();
if(cio_api.hidemouse!=NULL)
return(cio_api.hidemouse());
}
......@@ -81,8 +81,15 @@ struct text_info {
#endif
struct cio_mouse_event {
int x;
int y;
int button;
};
typedef struct {
int mode;
int mouse;
void (*clreol) (void);
int (*puttext) (int,int,int,int,void *);
int (*gettext) (int,int,int,int,void *);
......@@ -116,6 +123,9 @@ typedef struct {
int (*cputs) (char *);
void (*textbackground) (int);
void (*textcolor) (int);
int (*getmouse) (struct cio_mouse_event *mevent);
int (*hidemouse) (void);
int (*showmouse) (void);
} cioapi_t;
extern cioapi_t cio_api;
......@@ -197,8 +207,11 @@ char *ciolib_getpass(const char *prompt);
#define textmode(a) ciolib_textmode(a)
#define window(a,b,c,d) ciolib_window(a,b,c,d)
#define delline() ciolib_delline()
#define insline ciolib_insline()
#define getpass(a) ciolib_getpass(a);
#define insline() ciolib_insline()
#define getpass(a) ciolib_getpass(a)
#define getmouse(a) ciolib_getmouse(a)
#define hidemouse() ciolib_hidemouse()
#define showmouse() ciolib_showmouse()
#endif
#endif /* Do not add anything after this line */
......@@ -7,6 +7,7 @@
#include "ciolib.h"
#include "curs_cio.h"
#include "keys.h"
static unsigned char curs_nextgetch=0;
const int curs_tabs[10]={9,17,25,33,41,49,57,65,73,80};
......@@ -642,6 +643,12 @@ int curs_initciolib(long inmode)
}
}
mode = inmode;
#ifdef NCURSES_VERSION_MAJOR
if(mousemask(BUTTON1_CLICKED|BUTTON3_CLICKED,NULL)==BUTTON1_CLICKED|BUTTON3_CLICKED)
cio_api.mouse=1;
else
mousemask(0,NULL);
#endif
return(1);
}
......@@ -924,6 +931,11 @@ int curs_getch(void)
ch=0;
break;
case KEY_MOUSE: /* Mouse stuff */
ch=CIO_KEY_MOUSE>>8;
curs_nextgetch=CIO_KEY_MOUSE&0xff;
break;
default:
curs_nextgetch=0xff;
ch=0;
......@@ -949,3 +961,48 @@ int curs_getche(void)
void curs_textmode(int mode)
{
}
int curs_hidemouse(void)
{
#ifdef NCURSES_VERSION_MAJOR
mousemask(0,NULL);
return(0);
#else
return(-1);
#endif
}
int curs_showmouse(void)
{
#ifdef NCURSES_VERSION_MAJOR
if(mousemask(BUTTON1_CLICKED|BUTTON3_CLICKED,NULL)==BUTTON1_CLICKED|BUTTON3_CLICKED)
return(0);
#endif
return(-1);
}
/* cio_get_mouse() */
int curs_getmouse(struct cio_mouse_event *mevent)
{
#ifdef NCURSES_VERSION_MAJOR
MEVENT mevnt;
if(getmouse(&mevnt)==OK) {
mevent->x=mevnt.x;
mevent->y=mevnt.y;
switch(mevnt.bstate) {
case BUTTON1_CLICKED:
mevent->button=1;
break;
case BUTTON3_CLICKED:
mevent->button=2;
break;
}
}
else
return(-1);
return(0);
#else
return(-1);
#endif
}
......@@ -4,6 +4,7 @@
#include "ciolib.h"
#undef getch
#undef ungetch
#undef getmouse
#include "curs_fix.h"
#ifdef __cplusplus
......@@ -26,6 +27,9 @@ void curs_setcursortype(int type);
int curs_getch(void);
int curs_getche(void);
void curs_textmode(int mode);
int curs_getmouse(struct cio_mouse_event *mevent);
int curs_showmouse(void);
int curs_hidemouse(void);
#ifdef __cplusplus
}
#endif
......
/* $Id$ */
#define CIO_KEY_HOME 0x47 << 8
#define CIO_KEY_UP 72 << 8
#define CIO_KEY_END 0x4f << 8
#define CIO_KEY_DOWN 80 << 8
#define CIO_KEY_F(x) (0x3a+x) << 8
#define CIO_KEY_IC 0x52 << 8
#define CIO_KEY_DC 0x53 << 8
#define CIO_KEY_LEFT 0x4b << 8
#define CIO_KEY_RIGHT 0x4d << 8
#define CIO_KEY_PPAGE 0x49 << 8
#define CIO_KEY_NPAGE 0x51 << 8
#define CIO_KEY_MOUSE 0x02ff
#include "conio.h"
#include "ciolib.h"
#include "keys.h"
static struct cio_mouse_event cio_last_button_press;
static struct cio_mouse_event last_mouse_click;
int lastch=0;
int domouse=0;
int win32_kbhit(void)
{
INPUT_RECORD input;
DWORD num=0;
if(lastch)
return(1);
while(1) {
if(!PeekConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &input, 1, &num)
|| !num)
break;
if(input.EventType==KEY_EVENT && input.Event.KeyEvent.bKeyDown)
return(1);
if(domouse) {
if(input.EventType==MOUSE_EVENT) {
if(!input.Event.MouseEvent.dwEventFlags
&& (!input.Event.MouseEvent.dwButtonState
|| input.Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED
|| input.Event.MouseEvent.dwButtonState==RIGHTMOST_BUTTON_PRESSED))
return(1);
}
}
if(ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &input, 1, &num)
&& num) {
continue;
}
}
return(0);
}
int win32_getch()
{
char str[128];
INPUT_RECORD input;
DWORD num=0;
while(1) {
if(lastch) {
int ch;
ch=lastch&0xff;
lastch>>=8;
return(ch);
}
if(!ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &input, 1, &num)
|| !num || (input.EventType!=KEY_EVENT && input.EventType!=MOUSE_EVENT))
continue;
switch(input.EventType) {
case KEY_EVENT:
if(!input.Event.KeyEvent.bKeyDown)
continue;
#if 0
sprintf(str,"keydown=%d\n",input.Event.KeyEvent.bKeyDown);
OutputDebugString(str);
sprintf(str,"repeat=%d\n",input.Event.KeyEvent.wRepeatCount);
OutputDebugString(str);
sprintf(str,"keycode=%x\n",input.Event.KeyEvent.wVirtualKeyCode);
OutputDebugString(str);
sprintf(str,"scancode=%x\n",input.Event.KeyEvent.wVirtualScanCode);
OutputDebugString(str);
sprintf(str,"ascii=%d\n",input.Event.KeyEvent.uChar.AsciiChar);
OutputDebugString(str);
sprintf(str,"dwControlKeyState=%lx\n",input.Event.KeyEvent.dwControlKeyState);
OutputDebugString(str);
#endif
if(input.Event.KeyEvent.uChar.AsciiChar)
lastch=input.Event.KeyEvent.uChar.AsciiChar;
else
lastch=input.Event.KeyEvent.wVirtualScanCode<<8;
break;
case MOUSE_EVENT:
if(domouse) {
if(input.Event.MouseEvent.dwEventFlags!=0)
continue;
if(input.Event.MouseEvent.dwButtonState==0) {
if(cio_last_button_press.button
&& cio_last_button_press.x==input.Event.MouseEvent.dwMousePosition.X
&& cio_last_button_press.y==input.Event.MouseEvent.dwMousePosition.Y) {
memcpy(&last_mouse_click,&cio_last_button_press,sizeof(last_mouse_click));
memset(&cio_last_button_press,0,sizeof(cio_last_button_press));
lastch=CIO_KEY_MOUSE;
break;
}
else {
memset(&cio_last_button_press,0,sizeof(cio_last_button_press));
}
}
else {
memset(&cio_last_button_press,0,sizeof(cio_last_button_press));
switch(input.Event.MouseEvent.dwButtonState) {
case FROM_LEFT_1ST_BUTTON_PRESSED:
cio_last_button_press.x=input.Event.MouseEvent.dwMousePosition.X;
cio_last_button_press.y=input.Event.MouseEvent.dwMousePosition.Y;
cio_last_button_press.button=1;
break;
case RIGHTMOST_BUTTON_PRESSED:
cio_last_button_press.x=input.Event.MouseEvent.dwMousePosition.X;
cio_last_button_press.y=input.Event.MouseEvent.dwMousePosition.Y;
cio_last_button_press.button=2;
break;
}
}
}
}
}
return(0);
}
int win32_getche(void)
{
int ch;
if(ansi_nextchar)
return(ansi_getch());
ch=ansi_getch();
if(ch)
putch(ch);
return(ch);
}
int win32_initciolib(long inmode)
{
if(!isatty(stdin))
return(0);
textmode(mode);
if(!GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &conmode))
return(0);
conmode&=~ENABLE_PROCESSED_INPUT;
conmode|=ENABLE_MOUSE_INPUT;
if(!SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), conmode))
return(0);
cio_api.mouse=1;
memset(&uifc_last_button_press,0,sizeof(uifc_last_button_press));
memset(&last_mouse_click,0,sizeof(last_mouse_click));
return(1);
}
int win32_getmouse(struct cio_mouse_event *mevent) {
memcpy(mevent,&last_mouse_click,sizeof(last_mouse_click));
return(0);
}
int win32_hidemouse(void)
{
domouse=0;
return(0);
}
int win32_showmouse(void)
{
domouse=1;
return(0);
}
#ifndef _WIN32CIO_H_
#define _WIN32CIO_H_
#ifdef __cplusplus
extern "C" {
#endif
int win32_kbhit(void)
int win32_getch()
int win32_getche(void)
#ifdef __cplusplus
}
#endif
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment