Watcom C/386 9.0 getch() and kbhit() + PC-CURSES1.3-2 + sc 6.21

From OS2World.com Wiki
Jump to: navigation, search

From: fredw@mks.com (Fred Walter)

Subject: Watcom C/386 9.0 getch() and kbhit() + PC-CURSES1.3-2 + sc 6.21

Organization: Mortice Kern Systems Inc., Waterloo, Ontario, CANADA

Date: Fri, 10 Jul 1992 16:26:26 GMT

Watcom C/386 9.0's implimentation of getch() and kbhit() are broken. Watcom tech support knows this, and it will be fixed in the next release. If you don't want to wait until the next release to use these functions you could use the ones that I wrote (which I've tacked onto the end of this posting).

fred

------------------------------------------------------------------------------
/*
 * getch()
 *
 * Implimention of the above library call.
 *
 * Replaces the broken implimentation of getch() found in the Watcom C/386 9.0
 * libraries. Note: Watcom's ungetch() probably won't work with my new getch().
 *
 * Copyright July 1992 by G. R. (Fred) Walter
 */

#include <stdio.h>
#include <stdlib.h>
#define INCL_KBD
#include <os2.h>

static char        _getch_flag = 0; 
static KBDKEYINFO *_getch_keyinfo = NULL;

int
getch()
{
	if (_getch_keyinfo == NULL) {
		_getch_keyinfo = (KBDKEYINFO *)malloc(sizeof(KBDKEYINFO));
		if (_getch_keyinfo == NULL) {
			fprintf(stderr, "Out of memory error\n");
			exit(1);
		}
	}
			
	if (_getch_flag != 0) {
		_getch_flag = 0;
		return (_getch_keyinfo->chScan);
	}

	if (KbdCharIn(_getch_keyinfo, IO_WAIT, 0) != 0)
		return (EOF);

	if (_getch_keyinfo->chChar == 0x00 || _getch_keyinfo->chChar == 0xE0) {
		_getch_keyinfo->chChar = 0;
		_getch_flag = 1;
	}

	return (_getch_keyinfo->chChar);
}
------------------------------------------------------------------------------
/*
 * kbhit()
 *
 * Implimention of the above library call.
 *
 * Replaces the broken implimentation of kbhit() found in the Watcom C/386 9.0
 * libraries.
 *
 * Copyright July 1992 by G. R. (Fred) Walter
 */

#include <stdio.h>
#include <stdlib.h>
#define INCL_KBD
#include <os2.h>

static KBDKEYINFO *_kbhit_keyinfo = NULL;

int
kbhit()
{
	if (_kbhit_keyinfo == NULL) {
		_kbhit_keyinfo = (KBDKEYINFO *)malloc(sizeof(KBDKEYINFO));
		if (_kbhit_keyinfo == NULL) {
			fprintf(stderr, "Out of memory error\n");
			exit(1);
		}
	}
			
	if (KbdPeek(_kbhit_keyinfo, 0) != 0)
		return (0);

	if (_kbhit_keyinfo->fbStatus & 0x40)
		return (1);
	else
		return (0);
}
-- 
Disclaimer
everything I write is my *personal* opinion and does not represent or reflect the opinion of the company which employs me.