win32cio.c 9.02 KB
Newer Older
1
2
#include <windows.h>	/* INPUT_RECORD, etc. */
#include <stdio.h>		/* stdin */
deuce's avatar
deuce committed
3
4
#include "ciolib.h"
#include "keys.h"
5
6
7
#include "win32cio.h"

const int 	cio_tabs[10]={9,17,25,33,41,49,57,65,73,80};
deuce's avatar
deuce committed
8
9
10
11

static struct cio_mouse_event	cio_last_button_press;
static struct cio_mouse_event	last_mouse_click;

12
13
static int lastch=0;
static int domouse=0;
14
15
static int xpos=1;
static int ypos=1;
deuce's avatar
deuce committed
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
static int currattr=7;

WORD DOStoWinAttr(int newattr)
{
	WORD ret=0;

	if(newattr&0x01)
		ret|=FOREGROUND_BLUE;
	if(newattr&0x02)
		ret|=FOREGROUND_GREEN;
	if(newattr&0x04)
		ret|=FOREGROUND_RED;
	if(newattr&0x08)
		ret|=FOREGROUND_INTENSITY;
	if(newattr&0x10)
		ret|=BACKGROUND_BLUE;
	if(newattr&0x20)
		ret|=BACKGROUND_GREEN;
	if(newattr&0x40)
		ret|=BACKGROUND_RED;
	if(newattr&0x80)
		ret|=BACKGROUND_INTENSITY;
	return(ret);
}

unsigned char WintoDOSAttr(WORD newattr)
{
	unsigned char ret=0;

	if(newattr&FOREGROUND_BLUE)
		ret|=0x01;
	if(newattr&FOREGROUND_GREEN)
		ret|=0x02;
	if(newattr&FOREGROUND_RED)
		ret|=0x04;
	if(newattr&FOREGROUND_INTENSITY)
		ret|=0x08;
	if(newattr&BACKGROUND_BLUE)
		ret|=0x10;
	if(newattr&BACKGROUND_GREEN)
		ret|=0x20;
	if(newattr&BACKGROUND_RED)
		ret|=0x40;
	if(newattr&BACKGROUND_INTENSITY)
		ret|=0x80;
	return(ret);
}

deuce's avatar
deuce committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
}

rswindell's avatar
rswindell committed
95
int win32_getch(void)
deuce's avatar
deuce committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
	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;

rswindell's avatar
rswindell committed
179
	ch=win32_getch();
deuce's avatar
deuce committed
180
181
182
183
184
185
186
	if(ch)
		putch(ch);
	return(ch);
}

int win32_initciolib(long inmode)
{
187
188
	DWORD conmode;

rswindell's avatar
rswindell committed
189
	if(!isatty(fileno(stdin)))
deuce's avatar
deuce committed
190
		return(0);
191
	win32_textmode(inmode);
deuce's avatar
deuce committed
192
193
194
195
196
197
198
	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;
199
	memset(&cio_last_button_press,0,sizeof(cio_last_button_press));
deuce's avatar
deuce committed
200
201
202
203
	memset(&last_mouse_click,0,sizeof(last_mouse_click));
	return(1);
}

204
205
int win32_getmouse(struct cio_mouse_event *mevent)
{
deuce's avatar
deuce committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
	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);
}
221

222
void win32_textmode(int mode)
rswindell's avatar
rswindell committed
223
224
225
{
}

226
int win32_gettext(int left, int top, int right, int bottom, void* buf)
rswindell's avatar
rswindell committed
227
{
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	CHAR_INFO *ci;
	int	x;
	int	y;
	COORD	bs;
	COORD	bc;
	SMALL_RECT	reg;
	unsigned char	*bu;

	bu=buf;
	bs.X=right-left+1;
	bs.Y=bottom-top+1;
	bc.X=0;
	bc.Y=0;
	reg.Left=left-1;
	reg.Right=right-1;
	reg.Top=top-1;
	reg.Bottom=bottom-1;
	ci=(CHAR_INFO *)malloc(sizeof(CHAR_INFO)*(bs.X*bs.Y));
	ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),ci,bs,bc,&reg);
	for(y=0;y<=(bottom-top);y++) {
		for(x=0;x<=(right-left);x++) {
			bu[((y*bs.X)+x)*2]=ci[(y*bs.X)+x].Char.AsciiChar;
			bu[(((y*bs.X)+x)*2)+1]=WintoDOSAttr(ci[(y*bs.X)+x].Attributes);
		}
	}
	free(ci);
	return 1;
rswindell's avatar
rswindell committed
255
256
}

257
void win32_gettextinfo(struct text_info* info)
rswindell's avatar
rswindell committed
258
{
259
	CONSOLE_SCREEN_BUFFER_INFO bi;
rswindell's avatar
rswindell committed
260

261
	/* GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&bi); */
rswindell's avatar
rswindell committed
262

263
264
	/* ToDo Fix this! */
	info->currmode=C80;
265
266
	info->curx=xpos;
	info->cury=ypos;
267
	info->attribute=currattr;
268
269
	info->screenheight=25;
	info->screenwidth=80;
rswindell's avatar
rswindell committed
270
271
}

272
void win32_gotoxy(int x, int y)
rswindell's avatar
rswindell committed
273
{
274
	COORD	cp;
rswindell's avatar
rswindell committed
275

276
277
	xpos=x;
	ypos=y;
278
279
280
	cp.X=x-1;
	cp.Y=y-1;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cp);
rswindell's avatar
rswindell committed
281
282
}

283
void win32_highvideo(void)
rswindell's avatar
rswindell committed
284
{
285
	win32_textattr(currattr|0x08);
rswindell's avatar
rswindell committed
286
287
288
}


289
void win32_lowvideo(void)
rswindell's avatar
rswindell committed
290
{
291
	win32_textattr(currattr&0xf8);
rswindell's avatar
rswindell committed
292
293
294
}


295
void win32_normvideo(void)
rswindell's avatar
rswindell committed
296
{
297
	win32_textattr(7);
rswindell's avatar
rswindell committed
298
299
}

300
int win32_puttext(int left, int top, int right, int bottom, void* buf)
rswindell's avatar
rswindell committed
301
{
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
	CHAR_INFO *ci;
	int	x;
	int	y;
	COORD	bs;
	COORD	bc;
	SMALL_RECT	reg;
	unsigned char	*bu;

	bu=buf;
	bs.X=right-left+1;
	bs.Y=bottom-top+1;
	bc.X=0;
	bc.Y=0;
	reg.Left=left-1;
	reg.Right=right-1;
	reg.Top=top-1;
	reg.Bottom=bottom-1;
	ci=(CHAR_INFO *)malloc(sizeof(CHAR_INFO)*(bs.X*bs.Y));
	for(y=0;y<bs.Y;y++) {
		for(x=0;x<bs.X;x++) {
			ci[(y*bs.X)+x].Char.AsciiChar=bu[((y*bs.X)+x)*2];
			ci[(y*bs.X)+x].Attributes=DOStoWinAttr(bu[(((y*bs.X)+x)*2)+1]);
		}
	}
	WriteConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),ci,bs,bc,&reg);
	free(ci);
rswindell's avatar
rswindell committed
328
329
330
	return 1;
}

331
void win32_textattr(int newattr)
rswindell's avatar
rswindell committed
332
{
333
334
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),DOStoWinAttr(newattr));
	currattr=newattr;
rswindell's avatar
rswindell committed
335
336
337
}


338
void win32_textbackground(int newcolor)
rswindell's avatar
rswindell committed
339
{
340
	win32_textattr((currattr&0x0f)|((newcolor&0xf0)<<4));
rswindell's avatar
rswindell committed
341
342
343
}


344
void win32_textcolor(int newcolor)
rswindell's avatar
rswindell committed
345
{
346
	win32_textattr((currattr&0xf0)|((newcolor&0x0f)<<4));
rswindell's avatar
rswindell committed
347
348
}

349
void win32_setcursortype(int type)
rswindell's avatar
rswindell committed
350
{
351
	CONSOLE_CURSOR_INFO	ci;
rswindell's avatar
rswindell committed
352

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
	switch(type) {
		case _NOCURSOR:
			ci.bVisible=FALSE;
			break;
		
		case _SOLIDCURSOR:
			ci.bVisible=TRUE;
			ci.dwSize=100;
			break;
		
		default:	/* Normal cursor */
			ci.bVisible=TRUE;
			ci.dwSize=13;
			break;
	}
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&ci);
rswindell's avatar
rswindell committed
369
370
}

371
int win32_wherex(void)
rswindell's avatar
rswindell committed
372
{
373
	CONSOLE_SCREEN_BUFFER_INFO bi;
rswindell's avatar
rswindell committed
374

375
376
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&bi);
	return bi.dwCursorPosition.X+1;
rswindell's avatar
rswindell committed
377
378
}

379
int win32_wherey(void)
rswindell's avatar
rswindell committed
380
{
381
382
383
384
	CONSOLE_SCREEN_BUFFER_INFO bi;

	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&bi);
	return bi.dwCursorPosition.Y+1;
rswindell's avatar
rswindell committed
385
386
}

387
int win32_putch(int ch)
rswindell's avatar
rswindell committed
388
{
389
390
391
392
393
394
395
396
397
	struct text_info ti;
	WORD sch;
	int i;
	unsigned char buf[2];
	DWORD wr;

	buf[0]=ch;
	buf[1]=currattr;

398
399

	gettextinfo(&ti);
400
401
	switch(ch) {
		case '\r':
402
403
			gotoxy(1,ypos);
			break;
404
		case '\n':
405
406
407
408
409
410
411
412
413
414
			if(ypos==ti.winbottom-ti.wintop+1)
				wscroll();
			else
				gotoxy(xpos,ypos+1);
			break;
		case '\b':
			if(ti.curx>ti.winleft) {
				buf[0]=' ';
				gotoxy(xpos-1,ypos);
				puttext(xpos,ypos,xpos,ypos,buf);
415
			}
416
417
418
419
			break;
		case 7:		/* Bell */
			MessageBeep(MB_OK);
			break;
420
		default:
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
			if(ypos==ti.winbottom-ti.wintop+1
					&& xpos==ti.winright-ti.winleft+1) {
				puttext(xpos,ypos,xpos,ypos,buf);
				gotoxy(1,ypos);
				wscroll();
			}
			else {
				if(xpos==ti.winright-ti.winleft+1) {
					puttext(xpos,ypos,xpos,ypos,buf);
					gotoxy(1,ypos+1);
				}
				else {
					puttext(xpos,ypos,xpos,ypos,buf);
					gotoxy(xpos+1,ypos);
				}
			}
			break;
438
	}
439
440
441
442
443
444
	return(ch);
}

void win32_settitle(const char *title)
{
	SetConsoleTitle(title);
rswindell's avatar
rswindell committed
445
}