⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 device.c

📁 smallbasic for linux
💻 C
字号:
/**	lowlevel device (OS) I/O*/#include <stdarg.h>#include "sys.h"#include "str.h"#include "var.h"#define	DEVICE_MODULE#include "device.h"#include "osd.h"#include "smbas.h"#if !defined(_PalmOS)	#include <stdio.h>	#include <sys/types.h>	#include <sys/stat.h>	#if !defined(_Win32)		#include <sys/time.h>		// struct timeval		#include <unistd.h>	#else		#include <windows.h>	#endif#endif#if defined(LINUXDSP)int		ossdsp_init(void);void	ossdsp_close(void);void	oss_sound(int frq, int  ms, int vol, int bgplay);void	oss_beep(void);#endif//	Keyboard buffer#define	PCKBSIZE	128static word keybuff[PCKBSIZE];static int	keyhead;static int	keytail;/////////////////////////////////////////////////////////////////////////////// INIT & RESTORE#if defined(_PalmOS)word	os_ver = 0x310;byte	os_color = 0;dword	os_color_depth = 1;/**	os_graphics:*		0 = no graphics (console)*		1 = PalmOS/Win32 EMU*		2 = SVGALib*		3 = Win32*		4 = X*		5 = OFBIS**	os_graf_mx, os_graf_my = graphics display max. x, max. y*/byte	os_graphics = 1;int		os_graf_mx = 160;int		os_graf_my = 160;#elseword	os_ver = 0x400;byte	os_color = 1;dword	os_color_depth = 16;byte	os_graphics = 0;			// CONSOLEint		os_graf_mx = 160;int		os_graf_my = 160;#endifbyte	os_charset = 0;				// UTF8// cacheword	os_cclabs1 = 256;			word	os_ccpass2 = 256;// graphics - window (x1clip,y1clip,x2clip,y2clip)int		dev_x1clip;int		dev_y1clip;int		dev_x2clip;int		dev_y2clip;// graphics - origin xorg,yorgint		dev_xorg;int		dev_yorg;int		dev_fgcolor = 0;int		dev_bgcolor = 15;int		dev_init(int mode, int flags){#if defined(LINUXDSP)	if	( !ossdsp_init() )			fprintf(stderr, "oss: failed\n");#endif	dev_initfs();	dev_fgcolor = 0;	dev_bgcolor = 15;	#if defined(_PalmOS)	osd_devinit();	#else	os_graphics = mode;	if	( mode )	{		if	( osd_devinit() == 0 )			exit(1);		}	#endif	dev_x1clip = dev_y1clip = 0;	dev_x2clip = os_graf_mx;	dev_y2clip = os_graf_my;	dev_xorg = dev_yorg = 0;	if	( os_graphics )	{		osd_settextcolor(dev_fgcolor, dev_bgcolor);		osd_setcolor(dev_fgcolor);		}	setsysvar_int(SYSVAR_OSVER, os_ver);	setsysvar_int(SYSVAR_XMAX, os_graf_mx);	setsysvar_int(SYSVAR_YMAX, os_graf_my);	setsysvar_int(SYSVAR_BPP, os_color_depth);	return 1;}int		dev_restore(){#if defined(LINUXDSP)	ossdsp_close();#endif	dev_closefs();	if	( os_graphics )		osd_devrestore();	return 1;}///////////////////////////////////////////////////////////////////////////////////// GENERIC#if !defined(_PalmOS)  && !defined(_Win32)int		ucns_events(int wait_flag){	fd_set		rfds;	struct timeval	tv;	int			ival;	byte		c;	FD_ZERO(&rfds);	FD_SET(0, &rfds);	tv.tv_sec=0;	tv.tv_usec=0;	ival = select(1, &rfds, NULL, NULL, &tv);	if	( ival || wait_flag )	{		read(0, &c, 1);		if	( c == SB_KEY_BREAK )	// CTRL+C (break)			return -2;		dev_pushkey(c);		return 1;		}	return 0;}#endifint		dev_events(int timeout){	#if	defined(_PalmOS)	return	osd_events(timeout);	#else	if	( os_graphics )		return	osd_events(timeout);	#if !defined(_Win32)	return	ucns_events(timeout);	#else	return 0;	#endif	#endif}void	dev_delay(dword ms){	#if defined(_PalmOS)//	SysTaskDelay(ticks);	dword	start, tps, now;	int		evc;	start = TimGetTicks();	tps   = SysTicksPerSecond();	while ( 1 )	{		switch ( (evc = dev_events(0)) )	{		case	0:	// no event			break;		case	-2:	// break			brun_break();		case	-1:	// break			return;//		default://			return;			}		now = TimGetTicks();					if	( now > (start + (tps * ms) / 1000L) )			return;		}	#elif defined(_Win32)	Sleep(ms);	#else	usleep(ms * 1000);	#endif}///////////////////////////////////////////////////////////////////////////////////// KEYBOARD//	stores a key in keyboard buffervoid	dev_pushkey(word key){	keybuff[keytail] = key;	keytail ++;	if	( keytail >= PCKBSIZE )		keytail = 0;}			//	returns true if there is an key in keyboard bufferint		dev_kbhit(){	int		code;	if ( keytail != keyhead )		return 1;		code = dev_events(0);	if	( code < 0)		brun_break();	return (keytail != keyhead);}//	returns the next key in keyboard buffer (and removes it)extern int		prog_error;			// The last error codeint		dev_getch(){	word	ch = 0;	while ( (dev_kbhit() == 0) && (prog_error == 0) );	ch = keybuff[keyhead];	keyhead ++;	if	( keyhead >= PCKBSIZE )		keyhead = 0;/**** The following code is the Japanese character support.*** BUT*** I don't know what PalmOS send for Japanese characters (1 word (1 event) or 2 bytes (2 events) ?)	if	( os_jis8 && IsJIS1Font(ch) )	{			// Japanese support		if	( keytail != keyhead )	{			if	( IsJIS2Font(keybuff[keyhead]) )	{				ch = (ch << 8) + keybuff[keyhead];				keyhead ++;				if	( keyhead >= PCKBSIZE )					keyhead = 0;				}			}		}*/	return ch;}//	gets a string (INPUT)char	*dev_gets(char *dest, int size){	word		ch = 0, pos;	word		prev_x, prev_y;	int			code;	#if !defined(_PalmOS)	if	( !os_graphics )	{		// gets for console		char	*p;		fgets(dest, size, stdin);		p = dest;		while ( *p )	{			if	( *p == '\r' || *p == '\n' )				*p = '\0';			p ++;			}		return dest;		}	#endif	prev_x = dev_getx();	prev_y = dev_gety();	dev_clreol();	dev_clrkb();	*dest = '\0';	pos = 0;	do	{		// wait for event		while ( (code = dev_events(0)) == 0 );					if	( code < 0 )	{		// BREAK event			*dest = '\0';			brun_break();			return dest;			}		while ( dev_kbhit() )	{	// we have keys			ch = dev_getch();			switch ( ch )	{			case 0: case 10: case 13:	// ignore				break;			case SB_KEY_BACKSPACE: 		// backspace				if	( pos )						pos --;				dest[pos] = '\0';				// redraw				dev_setxy(prev_x, prev_y);				dev_clreol();				dev_print(dest);				break;			case SB_KEY_LEFT:			case SB_KEY_RIGHT:				break;			default:				if	( (ch & 0xFF00) != 0xFF00 )	{		// Not an hardware key					// store it					switch ( os_charset )	{					case	enc_sjis:				// Japan						if	( IsJISFont(ch) )	{								dest[pos] = ch >> 8;							pos ++;							}						ch = ch & 0xFF;						break;					case	enc_big5:				// China						if	( IsBig5Font(ch) )	{								dest[pos] = ch >> 8;							pos ++;							}						ch = ch & 0xFF;						break;					case	enc_gmb:				// Generic multibyte						if	( IsGMBFont(ch) )	{								dest[pos] = ch >> 8;							pos ++;							}						ch = ch & 0xFF;						break;					case	enc_unicode: 			// Unicode						dest[pos] = ch >> 8;						pos ++;						ch = ch & 0xFF;						break;						};					dest[pos] = ch;					pos ++;					dest[pos] = '\0';					}				else					ch = 0;				// chech the size				if	( pos >= (size-2) )					break;				// redraw				if	( ch )	{					dev_setxy(prev_x, prev_y);					dev_clreol();					dev_print(dest);					}				}			}	// dev_kbhit() loop		} while ( ch != '\n' );	dest[pos] = '\0';	dev_setxy(prev_x, prev_y);	dev_clreol();	dev_print(dest);	dev_print("\n");	return dest;	 }void	dev_setpenmode(int enable){	if	( os_graphics )		osd_setpenmode(enable);}int		dev_getpen(int code){	if	( os_graphics )		return osd_getpen(code);	return 0;}/////////////////////////////////////////////////////////////////////////////////////// SCREEN//	clear to eolvoid	dev_clreol(){	dev_print("\033[K");}//	clear keyboard buffervoid	dev_clrkb(){	keyhead = keytail = 0;}int		dev_getx(){	if ( os_graphics )			return osd_getx();	return 0;}int		dev_gety(){	if ( os_graphics )			return osd_gety();	return 0;}//void	dev_setxy(int x, int y){	if ( os_graphics )			osd_setxy(x, y);}void	dev_settextcolor(int fg, int bg){	if ( os_graphics )	{		if	( (fg <= 15) && (bg <= 15) && (fg >= 0) && (bg >= -1) )	{			if	( bg != -1 )					dev_bgcolor = bg;			osd_settextcolor(dev_fgcolor=fg, dev_bgcolor);				}		}}//	prints a stringvoid	dev_print(const char *str){	#if defined(_PalmOS)	osd_write(str);	#else	if ( os_graphics )			osd_write(str);	#if	!defined(_BCB_W32_IDE)	else		printf("%s", str);	#endif	#endif}/**	printf**	WARNING: PalmOS ver is limited to 256 bytes*	WARNING: Win32/Unix ver is limited to 1024 bytes*/void	dev_printf(const char *fmt, ...){	char	*buf;	va_list ap;	va_start(ap, fmt);	#if defined(_PalmOS)	buf = tmp_alloc(256);	StrVPrintF(buf, fmt, ap);	va_end(ap);	#else	buf = tmp_alloc(1024);	vsnprintf(buf, 1024, fmt, ap);	va_end(ap);	#endif	dev_print(buf);	tmp_free(buf);}//	clears the screenvoid	dev_cls(){	#if defined(_PalmOS)	osd_cls();	#else	if	( os_graphics )		osd_cls();	else		printf("%c", 12);	#endif}///////////////////////////////////////////////////////////////////////////////////// GRAPHICSint		dev_textwidth(const char *str){	if	( os_graphics )		return osd_textwidth(str);	return strlen(str);		// console}int		dev_textheight(const char *str){	if	( os_graphics )		return osd_textheight(str);	return 1; // console}void	dev_setcolor(int color){	if ( os_graphics )	{		if	( color <= 15 && color >= 0 )			osd_setcolor(dev_fgcolor = color);		}}void	dev_setpixel(int x, int y){	if ( os_graphics )			osd_setpixel(x,y);}void	dev_line(int x1, int y1, int x2, int y2){	if ( os_graphics )			osd_line(x1, y1, x2, y2);}void	dev_rect(int x1, int y1, int x2, int y2, int fill){	if ( os_graphics )			osd_rect(x1, y1, x2, y2, fill);}//////////////////////////////////////////////////////////////////////////////////////// SOUNDvoid	dev_beep(){	#if defined(_PalmOS)	osd_beep();	#else		#if defined(LINUXDSP)		oss_beep();		#else		if	( os_graphics )			osd_beep();		else			printf("\a");		#endif	#endif}void	dev_sound(int frq, int ms, int vol, int bgplay){	#if defined(_PalmOS)	osd_sound(frq, ms, vol, bgplay);	#else	#if defined(LINUXDSP)	oss_sound(frq, ms, vol, bgplay);	#else	if	( os_graphics )		osd_sound(frq, ms, vol, bgplay);	// Linux only ???	else	{		#if defined(USE_LINCONCODES)		/*		*	Linux console codes - PC Speaker		*/		printf("\033[10;%d]", frq);		printf("\033[11;%d]", ms);		printf("\a");		dev_delay(ms);		printf("\033[10;%d]", 440);		printf("\033[11;%d]", 250);		#else		if	( !bgplay )			dev_delay(ms);		#endif		}	#endif	#endif}/////////////////////////////////////////////////////////////////////////////////////// SYSTEMint		dev_run(const char *src){	#if defined(_PalmOS)	LocalID	lid;	dword	progid;	word	card;	DmSearchStateType	state;	progid  = ((dword) src[0] << 24) + ((dword) src[1] << 16) + ((dword) src[2] << 8) + (dword) src[3];	if	( DmGetNextDatabaseByTypeCreator(true, &state, 0x6170706C, progid, true, &card, &lid) == 0 )		return (SysUIAppSwitch(card, lid, sysAppLaunchCmdNormalLaunch, NULL) == 0);	return 0;	#else	return (system(src) == 0);	#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -