Hacks to support server-side console. Should be redone properly
[wine/multimedia.git] / win32 / console.c
blobdad241f7f9bfaec6afd23ea9721b33dc8fd22c61
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
7 * Copyright 1998 Marcus Meissner
8 */
10 /* FIXME:
11 * - Completely lacks SCREENBUFFER interface.
12 * - No abstraction for something other than xterm.
13 * - Key input translation shouldn't use VkKeyScan and MapVirtualKey, since
14 * they are window (USER) driver dependend.
15 * - Output sometimes is buffered (We switched off buffering by ~ICANON ?)
17 /* Reference applications:
18 * - IDA (interactive disassembler) full version 3.75. Works.
19 * - LYNX/W32. Works mostly, some keys crash it.
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <termios.h>
25 #include <strings.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <sys/errno.h>
33 #include <signal.h>
34 #include <assert.h>
36 #include "windows.h"
37 #include "k32obj.h"
38 #include "thread.h"
39 #include "async.h"
40 #include "file.h"
41 #include "process.h"
42 #include "winerror.h"
43 #include "wincon.h"
44 #include "heap.h"
45 #include "debug.h"
47 #include "server/request.h"
48 #include "server.h"
50 /* The CONSOLE kernel32 Object */
51 typedef struct _CONSOLE {
52 K32OBJ header;
53 DWORD mode;
54 CONSOLE_CURSOR_INFO cinfo;
56 int master; /* xterm side of pty */
57 int infd,outfd;
58 int hread, hwrite; /* server handles (hack) */
59 int pid; /* xterm's pid, -1 if no xterm */
60 LPSTR title; /* title of console */
61 INPUT_RECORD *irs; /* buffered input records */
62 int nrofirs;/* nr of buffered input records */
63 THREAD_QUEUE wait_queue;
64 } CONSOLE;
66 static void CONSOLE_AddWait(K32OBJ *ptr, DWORD thread_id);
67 static void CONSOLE_RemoveWait(K32OBJ *ptr, DWORD thread_id);
68 static BOOL32 CONSOLE_Satisfied(K32OBJ *ptr, DWORD thread_id);
69 static BOOL32 CONSOLE_Signaled(K32OBJ *ptr, DWORD thread_id);
70 static void CONSOLE_Destroy( K32OBJ *obj );
71 static BOOL32 CONSOLE_Write(K32OBJ *ptr, LPCVOID lpBuffer,
72 DWORD nNumberOfChars, LPDWORD lpNumberOfChars,
73 LPOVERLAPPED lpOverlapped);
74 static BOOL32 CONSOLE_Read(K32OBJ *ptr, LPVOID lpBuffer,
75 DWORD nNumberOfChars, LPDWORD lpNumberOfChars,
76 LPOVERLAPPED lpOverlapped);
78 const K32OBJ_OPS CONSOLE_Ops =
80 CONSOLE_Signaled, /* signaled */
81 CONSOLE_Satisfied, /* satisfied */
82 CONSOLE_AddWait, /* add_wait */
83 CONSOLE_RemoveWait, /* remove_wait */
84 CONSOLE_Read, /* read */
85 CONSOLE_Write, /* write */
86 CONSOLE_Destroy /* destroy */
89 /***********************************************************************
90 * CONSOLE_Destroy
92 static void CONSOLE_Destroy(K32OBJ *obj)
94 CONSOLE *console = (CONSOLE *)obj;
95 assert(obj->type == K32OBJ_CONSOLE);
97 obj->type = K32OBJ_UNKNOWN;
99 if (console->title)
100 HeapFree( SystemHeap, 0, console->title );
101 console->title = NULL;
102 /* if there is a xterm, kill it. */
103 if (console->pid != -1)
104 kill(console->pid, SIGTERM);
105 HeapFree(SystemHeap, 0, console);
109 /***********************************************************************
110 * CONSOLE_Read
112 * NOTES
113 * lpOverlapped is ignored
115 static BOOL32 CONSOLE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
116 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
118 CONSOLE *console = (CONSOLE *)ptr;
119 int result;
121 TRACE(console, "%p %p %ld\n", ptr, lpBuffer, nNumberOfChars);
122 *lpNumberOfChars = 0;
123 if ((result = read(console->infd, lpBuffer, nNumberOfChars)) == -1) {
124 FILE_SetDosError();
125 return FALSE;
127 *lpNumberOfChars = result;
128 return TRUE;
132 /***********************************************************************
133 * CONSOLE_Write
135 * NOTES
136 * lpOverlapped is ignored
138 static BOOL32 CONSOLE_Write(K32OBJ *ptr, LPCVOID lpBuffer,
139 DWORD nNumberOfChars,
140 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
142 CONSOLE *console = (CONSOLE *)ptr;
143 int result;
145 TRACE(console, "%p %p %ld\n", ptr, lpBuffer, nNumberOfChars);
146 *lpNumberOfChars = 0;
148 * I assume this loop around EAGAIN is here because
149 * win32 doesn't have interrupted system calls
152 for (;;)
154 result = write(console->outfd, lpBuffer, nNumberOfChars);
155 if (result != -1) {
156 *lpNumberOfChars = result;
157 return TRUE;
159 if (errno != EINTR) {
160 FILE_SetDosError();
161 return FALSE;
166 /****************************************************************************
167 * CONSOLE_add_input_record [internal]
169 * Adds an INPUT_RECORD to the CONSOLEs input queue.
171 static void
172 CONSOLE_add_input_record(CONSOLE *console,INPUT_RECORD *inp) {
173 console->irs = HeapReAlloc(GetProcessHeap(),0,console->irs,sizeof(INPUT_RECORD)*(console->nrofirs+1));
174 console->irs[console->nrofirs++]=*inp;
177 /****************************************************************************
178 * XTERM_string_to_IR [internal]
180 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
181 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
183 static void
184 CONSOLE_string_to_IR(CONSOLE *console,unsigned char *buf,int len) {
185 int j,k;
186 INPUT_RECORD ir;
188 for (j=0;j<len;j++) {
189 unsigned char inchar = buf[j];
191 if (inchar!=27) { /* no escape -> 'normal' keyboard event */
192 ir.EventType = 1; /* Key_event */
194 ir.Event.KeyEvent.bKeyDown = 1;
195 ir.Event.KeyEvent.wRepeatCount = 0;
197 ir.Event.KeyEvent.dwControlKeyState = 0;
198 if (inchar & 0x80) {
199 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
200 inchar &= ~0x80;
202 ir.Event.KeyEvent.wVirtualKeyCode = VkKeyScan16(inchar);
203 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0100)
204 ir.Event.KeyEvent.dwControlKeyState|=SHIFT_PRESSED;
205 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0200)
206 ir.Event.KeyEvent.dwControlKeyState|=LEFT_CTRL_PRESSED;
207 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0400)
208 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
209 ir.Event.KeyEvent.wVirtualScanCode = MapVirtualKey16(
210 ir.Event.KeyEvent.wVirtualKeyCode & 0x00ff,
211 0 /* VirtualKeyCodes to ScanCode */
213 if (inchar=='\n') {
214 ir.Event.KeyEvent.uChar.AsciiChar = '\r';
215 ir.Event.KeyEvent.wVirtualKeyCode = 0x0d;
216 ir.Event.KeyEvent.wVirtualScanCode = 0x1c;
217 } else {
218 ir.Event.KeyEvent.uChar.AsciiChar = inchar;
219 if (inchar<' ') {
220 /* FIXME: find good values for ^X */
221 ir.Event.KeyEvent.wVirtualKeyCode = 0xdead;
222 ir.Event.KeyEvent.wVirtualScanCode = 0xbeef;
226 CONSOLE_add_input_record(console,&ir);
227 ir.Event.KeyEvent.bKeyDown = 0;
228 CONSOLE_add_input_record(console,&ir);
229 continue;
231 /* inchar is ESC */
232 if ((j==len-1) || (buf[j+1]!='[')) {/* add ESCape on its own */
233 ir.EventType = 1; /* Key_event */
234 ir.Event.KeyEvent.bKeyDown = 1;
235 ir.Event.KeyEvent.wRepeatCount = 0;
237 ir.Event.KeyEvent.wVirtualKeyCode = VkKeyScan16(27);
238 ir.Event.KeyEvent.wVirtualScanCode = MapVirtualKey16(
239 ir.Event.KeyEvent.wVirtualKeyCode,0
241 ir.Event.KeyEvent.dwControlKeyState = 0;
242 ir.Event.KeyEvent.uChar.AsciiChar = 27;
243 CONSOLE_add_input_record(console,&ir);
244 ir.Event.KeyEvent.bKeyDown = 0;
245 CONSOLE_add_input_record(console,&ir);
246 continue;
248 for (k=j;k<len;k++) {
249 if (((buf[k]>='A') && (buf[k]<='Z')) ||
250 ((buf[k]>='a') && (buf[k]<='z')) ||
251 (buf[k]=='~')
253 break;
255 if (k<len) {
256 int subid,scancode=0;
258 ir.EventType = 1; /* Key_event */
259 ir.Event.KeyEvent.bKeyDown = 1;
260 ir.Event.KeyEvent.wRepeatCount = 0;
261 ir.Event.KeyEvent.dwControlKeyState = 0;
263 ir.Event.KeyEvent.wVirtualKeyCode = 0xad; /* FIXME */
264 ir.Event.KeyEvent.wVirtualScanCode = 0xad; /* FIXME */
265 ir.Event.KeyEvent.uChar.AsciiChar = 0;
267 switch (buf[k]) {
268 case '~':
269 sscanf(&buf[j+2],"%d",&subid);
270 switch (subid) {
271 case 2:/*INS */scancode = 0xe052;break;
272 case 3:/*DEL */scancode = 0xe053;break;
273 case 6:/*PGDW*/scancode = 0xe051;break;
274 case 5:/*PGUP*/scancode = 0xe049;break;
275 case 11:/*F1 */scancode = 0x003b;break;
276 case 12:/*F2 */scancode = 0x003c;break;
277 case 13:/*F3 */scancode = 0x003d;break;
278 case 14:/*F4 */scancode = 0x003e;break;
279 case 15:/*F5 */scancode = 0x003f;break;
280 case 17:/*F6 */scancode = 0x0040;break;
281 case 18:/*F7 */scancode = 0x0041;break;
282 case 19:/*F8 */scancode = 0x0042;break;
283 case 20:/*F9 */scancode = 0x0043;break;
284 case 21:/*F10 */scancode = 0x0044;break;
285 case 23:/*F11 */scancode = 0x00d9;break;
286 case 24:/*F12 */scancode = 0x00da;break;
287 /* FIXME: Shift-Fx */
288 default:
289 FIXME(console,"parse ESC[%d~\n",subid);
290 break;
292 break;
293 case 'A': /* Cursor Up */scancode = 0xe048;break;
294 case 'B': /* Cursor Down */scancode = 0xe050;break;
295 case 'D': /* Cursor Left */scancode = 0xe04b;break;
296 case 'C': /* Cursor Right */scancode = 0xe04d;break;
297 case 'F': /* End */scancode = 0xe04f;break;
298 case 'H': /* Home */scancode = 0xe047;break;
299 case 'M':
300 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
301 * Release (ESCM#<x+'!'><y+'!'>
303 if (k<len-3) {
304 ir.EventType = MOUSE_EVENT;
305 ir.Event.MouseEvent.dwMousePosition.x = buf[k+2]-'!';
306 ir.Event.MouseEvent.dwMousePosition.y = buf[k+3]-'!';
307 if (buf[k+1]=='#')
308 ir.Event.MouseEvent.dwButtonState = 0;
309 else
310 ir.Event.MouseEvent.dwButtonState = 1<<(buf[k+1]-' ');
311 ir.Event.MouseEvent.dwEventFlags = 0; /* FIXME */
312 CONSOLE_add_input_record(console,&ir);
313 j=k+3;
315 break;
318 if (scancode) {
319 ir.Event.KeyEvent.wVirtualScanCode = scancode;
320 ir.Event.KeyEvent.wVirtualKeyCode = MapVirtualKey16(scancode,1);
321 CONSOLE_add_input_record(console,&ir);
322 ir.Event.KeyEvent.bKeyDown = 0;
323 CONSOLE_add_input_record(console,&ir);
324 j=k;
325 continue;
330 /****************************************************************************
331 * CONSOLE_get_input (internal)
333 * Reads (nonblocking) as much input events as possible and stores them
334 * in an internal queue.
335 * (not necessarily XTERM dependend, but UNIX filedescriptor...)
337 static void
338 CONSOLE_get_input(CONSOLE *console) {
339 char *buf = HeapAlloc(GetProcessHeap(),0,1);
340 int len = 0;
342 while (1) {
343 fd_set infds;
344 DWORD res;
345 struct timeval tv;
346 char inchar;
348 FD_ZERO(&infds);
349 FD_SET(console->infd,&infds);
350 memset(&tv,0,sizeof(tv));
351 if (select(console->infd+1,&infds,NULL,NULL,&tv)<1)
352 break; /* no input there */
354 if (!FD_ISSET(console->infd,&infds))
355 break;
356 if (!CONSOLE_Read((K32OBJ*)console,&inchar,1,&res,NULL))
357 break;
358 buf = HeapReAlloc(GetProcessHeap(),0,buf,len+1);
359 buf[len++]=inchar;
361 CONSOLE_string_to_IR(console,buf,len);
362 HeapFree(GetProcessHeap(),0,buf);
365 /****************************************************************************
366 * CONSOLE_drain_input (internal)
368 * Drains 'n' console input events from the queue.
370 static void
371 CONSOLE_drain_input(CONSOLE *console,int n) {
372 assert(n<=console->nrofirs);
373 if (n) {
374 console->nrofirs-=n;
375 memcpy( &console->irs[0],
376 &console->irs[n],
377 console->nrofirs*sizeof(INPUT_RECORD)
379 console->irs = HeapReAlloc(
380 GetProcessHeap(),
382 console->irs,
383 console->nrofirs*sizeof(INPUT_RECORD)
388 /***********************************************************************
389 * CONSOLE_async_handler [internal]
391 static void
392 CONSOLE_async_handler(int unixfd,void *private) {
393 CONSOLE *console = (CONSOLE*)private;
395 SYNC_WakeUp(&console->wait_queue,INFINITE32);
398 /***********************************************************************
399 * CONSOLE_Signaled [internal]
401 * Checks if we can read something. (Hmm, what about writing ?)
403 static BOOL32
404 CONSOLE_Signaled(K32OBJ *ptr,DWORD tid) {
405 CONSOLE *console =(CONSOLE*)ptr;
407 if (ptr->type!= K32OBJ_CONSOLE)
408 return FALSE;
409 CONSOLE_get_input(console);
410 if (console->nrofirs!=0)
411 return TRUE;
412 /* addref console */
413 return FALSE;
416 /***********************************************************************
417 * CONSOLE_AddWait [internal]
419 * Add thread to our waitqueue (so we can signal it if we have input).
421 static void CONSOLE_AddWait(K32OBJ *ptr, DWORD thread_id)
423 CONSOLE *console = (CONSOLE *)ptr;
425 /* register our unix filedescriptors for async IO */
426 if (!console->wait_queue)
427 ASYNC_RegisterFD(console->infd,CONSOLE_async_handler,console);
428 THREAD_AddQueue( &console->wait_queue, THREAD_ID_TO_THDB(thread_id) );
431 /***********************************************************************
432 * CONSOLE_RemoveWait [internal]
434 * Remove thread from our waitqueue.
436 static void CONSOLE_RemoveWait(K32OBJ *ptr, DWORD thread_id)
438 CONSOLE *console = (CONSOLE *)ptr;
440 THREAD_RemoveQueue( &console->wait_queue, THREAD_ID_TO_THDB(thread_id) );
441 if (!console->wait_queue)
442 ASYNC_UnregisterFD(console->infd,CONSOLE_async_handler);
445 /***********************************************************************
446 * CONSOLE_Satisfied [internal]
448 * Condition from last Signaled is satisfied and will be used now.
450 static BOOL32 CONSOLE_Satisfied(K32OBJ *ptr, DWORD thread_id)
452 return FALSE;
456 /******************************************************************************
457 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
459 * PARAMS
460 * func [I] Address of handler function
461 * add [I] Handler to add or remove
463 * RETURNS
464 * Success: TRUE
465 * Failure: FALSE
467 * CHANGED
468 * James Sutherland (JamesSutherland@gmx.de)
469 * Added global variables console_ignore_ctrl_c and handlers[]
470 * Does not yet do any error checking, or set LastError if failed.
471 * This doesn't yet matter, since these handlers are not yet called...!
473 static unsigned int console_ignore_ctrl_c = 0;
474 static HANDLER_ROUTINE *handlers[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
475 BOOL32 WINAPI SetConsoleCtrlHandler( HANDLER_ROUTINE *func, BOOL32 add )
477 unsigned int alloc_loop = sizeof(handlers)/sizeof(HANDLER_ROUTINE *);
478 unsigned int done = 0;
479 FIXME(console, "(%p,%i) - no error checking or testing yet\n", func, add);
480 if (!func)
482 console_ignore_ctrl_c = add;
483 return TRUE;
485 if (add)
487 for (;alloc_loop--;)
488 if (!handlers[alloc_loop] && !done)
490 handlers[alloc_loop] = func;
491 done++;
493 if (!done)
494 FIXME(console, "Out of space on CtrlHandler table\n");
495 return(done);
497 else
499 for (;alloc_loop--;)
500 if (handlers[alloc_loop] == func && !done)
502 handlers[alloc_loop] = 0;
503 done++;
505 if (!done)
506 WARN(console, "Attempt to remove non-installed CtrlHandler %p\n",
507 func);
508 return (done);
510 return (done);
514 /******************************************************************************
515 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
517 * PARAMS
518 * dwCtrlEvent [I] Type of event
519 * dwProcessGroupID [I] Process group ID to send event to
521 * NOTES
522 * Doesn't yet work...!
524 * RETURNS
525 * Success: True
526 * Failure: False (and *should* [but doesn't] set LastError)
528 BOOL32 WINAPI GenerateConsoleCtrlEvent( DWORD dwCtrlEvent,
529 DWORD dwProcessGroupID )
531 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
533 ERR( console, "invalid event %d for PGID %ld\n",
534 (unsigned short)dwCtrlEvent, dwProcessGroupID );
535 return FALSE;
537 if (dwProcessGroupID == GetCurrentProcessId() )
539 FIXME( console, "Attempt to send event %d to self - stub\n",
540 (unsigned short)dwCtrlEvent );
541 return FALSE;
543 FIXME( console,"event %d to external PGID %ld - not implemented yet\n",
544 (unsigned short)dwCtrlEvent, dwProcessGroupID );
545 return FALSE;
549 /******************************************************************************
550 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
552 * PARAMS
553 * dwDesiredAccess [I] Access flag
554 * dwShareMode [I] Buffer share mode
555 * sa [I] Security attributes
556 * dwFlags [I] Type of buffer to create
557 * lpScreenBufferData [I] Reserved
559 * NOTES
560 * Should call SetLastError
562 * RETURNS
563 * Success: Handle to new console screen buffer
564 * Failure: INVALID_HANDLE_VALUE
566 HANDLE32 WINAPI CreateConsoleScreenBuffer( DWORD dwDesiredAccess,
567 DWORD dwShareMode, LPSECURITY_ATTRIBUTES sa,
568 DWORD dwFlags, LPVOID lpScreenBufferData )
570 FIXME(console, "(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess,
571 dwShareMode, sa, dwFlags, lpScreenBufferData);
572 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
573 return INVALID_HANDLE_VALUE32;
577 /***********************************************************************
578 * GetConsoleScreenBufferInfo (KERNEL32.190)
580 BOOL32 WINAPI GetConsoleScreenBufferInfo( HANDLE32 hConsoleOutput,
581 LPCONSOLE_SCREEN_BUFFER_INFO csbi )
583 csbi->dwSize.x = 80;
584 csbi->dwSize.y = 24;
585 csbi->dwCursorPosition.x = 0;
586 csbi->dwCursorPosition.y = 0;
587 csbi->wAttributes = 0;
588 csbi->srWindow.Left = 0;
589 csbi->srWindow.Right = 79;
590 csbi->srWindow.Top = 0;
591 csbi->srWindow.Bottom = 23;
592 csbi->dwMaximumWindowSize.x = 80;
593 csbi->dwMaximumWindowSize.y = 24;
594 return TRUE;
598 /******************************************************************************
599 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
601 * RETURNS
602 * Success: TRUE
603 * Failure: FALSE
605 BOOL32 WINAPI SetConsoleActiveScreenBuffer(
606 HANDLE32 hConsoleOutput) /* [in] Handle to console screen buffer */
608 FIXME(console, "(%x): stub\n", hConsoleOutput);
609 return FALSE;
613 /***********************************************************************
614 * GetLargestConsoleWindowSize (KERNEL32.226)
616 DWORD WINAPI GetLargestConsoleWindowSize( HANDLE32 hConsoleOutput )
618 return (DWORD)MAKELONG(80,24);
621 /***********************************************************************
622 * FreeConsole (KERNEL32.267)
624 BOOL32 WINAPI FreeConsole(VOID)
627 PDB32 *pdb = PROCESS_Current();
628 CONSOLE *console;
630 SYSTEM_LOCK();
632 console = (CONSOLE *)pdb->console;
634 if (console == NULL) {
635 SetLastError(ERROR_INVALID_PARAMETER);
636 return FALSE;
639 HANDLE_CloseAll( pdb, &console->header );
641 K32OBJ_DecCount( &console->header );
642 pdb->console = NULL;
643 SYSTEM_UNLOCK();
644 return TRUE;
648 /**
649 * It looks like the openpty that comes with glibc in RedHat 5.0
650 * is buggy (second call returns what looks like a dup of 0 and 1
651 * instead of a new pty), this is a generic replacement.
653 static int CONSOLE_openpty(CONSOLE *console, char *name,
654 struct termios *term, struct winsize *winsize)
656 int fdm, fds;
657 char *ptr1, *ptr2;
658 char pts_name[512];
659 struct set_console_fd_request req;
661 strcpy (pts_name, "/dev/ptyXY");
663 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
664 pts_name[8] = *ptr1;
665 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
666 pts_name[9] = *ptr2;
668 if ((fdm = open(pts_name, O_RDWR)) < 0) {
669 if (errno == ENOENT)
670 return -1;
671 else
672 continue;
674 pts_name[5] = 't';
675 if ((fds = open(pts_name, O_RDWR)) < 0) {
676 pts_name[5] = 'p';
677 continue;
679 console->master = fdm;
680 console->infd = console->outfd = fds;
681 req.handle = console->hread;
682 CLIENT_SendRequest( REQ_SET_CONSOLE_FD, dup(fds), 1, &req, sizeof(req) );
683 CLIENT_WaitReply( NULL, NULL, 0 );
684 req.handle = console->hwrite;
685 CLIENT_SendRequest( REQ_SET_CONSOLE_FD, dup(fds), 1, &req, sizeof(req) );
686 CLIENT_WaitReply( NULL, NULL, 0 );
688 if (term != NULL)
689 tcsetattr(console->infd, TCSANOW, term);
690 if (winsize != NULL)
691 ioctl(console->outfd, TIOCSWINSZ, winsize);
692 if (name != NULL)
693 strcpy(name, pts_name);
694 return fds;
697 return -1;
700 /*************************************************************************
701 * CONSOLE_make_complex [internal]
703 * Turns a CONSOLE kernel object into a complex one.
704 * (switches from output/input using the terminal where WINE was started to
705 * its own xterm).
707 * This makes simple commandline tools pipeable, while complex commandline
708 * tools work without getting messed up by debugoutput.
710 * All other functions should work indedependend from this call.
712 * To test for complex console: pid == -1 -> simple, otherwise complex.
714 static BOOL32 CONSOLE_make_complex(CONSOLE *console)
716 struct termios term;
717 char buf[30];
718 char c = '\0';
719 int status = 0;
720 int i,xpid;
721 DWORD xlen;
723 if (console->pid != -1)
724 return TRUE; /* already complex */
726 MSG("Console: Making console complex (creating an xterm)...\n");
728 if (tcgetattr(0, &term) < 0) return FALSE;
729 term.c_lflag = ~(ECHO|ICANON);
730 if (CONSOLE_openpty(console, NULL, &term, NULL) < 0) return FALSE;
732 if ((xpid=fork()) == 0) {
733 tcsetattr(console->infd, TCSADRAIN, &term);
734 sprintf(buf, "-Sxx%d", console->master);
735 /* "-fn vga" for VGA font. Harmless if vga is not present:
736 * xterm: unable to open font "vga", trying "fixed"....
738 execlp("xterm", "xterm", buf, "-fn","vga",NULL);
739 ERR(console, "error creating AllocConsole xterm\n");
740 exit(1);
742 console->pid = xpid;
744 /* most xterms like to print their window ID when used with -S;
745 * read it and continue before the user has a chance...
747 for (i=0; c!='\n'; (status=read(console->infd, &c, 1)), i++) {
748 if (status == -1 && c == '\0') {
749 /* wait for xterm to be created */
750 usleep(100);
752 if (i > 10000) {
753 ERR(console, "can't read xterm WID\n");
754 kill(console->pid, SIGKILL);
755 return FALSE;
758 /* enable mouseclicks */
759 sprintf(buf,"%c[?1001s%c[?1000h",27,27);
760 CONSOLE_Write(&console->header,buf,strlen(buf),&xlen,NULL);
761 return TRUE;
765 /***********************************************************************
766 * CONSOLE_GetConsoleHandle
767 * returns a 16-bit style console handle
768 * note: only called from _lopen
770 HFILE32 CONSOLE_GetConsoleHandle(VOID)
772 PDB32 *pdb = PROCESS_Current();
773 HFILE32 handle = HFILE_ERROR32;
775 SYSTEM_LOCK();
776 if (pdb->console != NULL) {
777 CONSOLE *console = (CONSOLE *)pdb->console;
778 handle = (HFILE32)HANDLE_Alloc(pdb, &console->header, 0, TRUE, -1);
780 SYSTEM_UNLOCK();
781 return handle;
784 /***********************************************************************
785 * AllocConsole (KERNEL32.103)
787 * creates an xterm with a pty to our program
789 BOOL32 WINAPI AllocConsole(VOID)
791 struct create_console_request req;
792 struct create_console_reply reply;
793 int len;
794 PDB32 *pdb = PROCESS_Current();
795 CONSOLE *console;
796 HANDLE32 hIn, hOut, hErr;
798 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
800 SetLastError(ERROR_CANNOT_MAKE); /* this might not be the right
801 error, but it's a good guess :) */
803 console = (CONSOLE *)pdb->console;
805 /* don't create a console if we already have one */
806 if (console != NULL) {
807 SetLastError(ERROR_ACCESS_DENIED);
808 SYSTEM_UNLOCK();
809 return FALSE;
812 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
814 SYSTEM_UNLOCK();
815 return FALSE;
818 console->header.type = K32OBJ_CONSOLE;
819 console->header.refcount = 1;
820 console->pid = -1;
821 console->title = NULL;
822 console->nrofirs = 0;
823 console->wait_queue = NULL;
824 console->irs = HeapAlloc(GetProcessHeap(),0,1);;
825 console->mode = ENABLE_PROCESSED_INPUT
826 | ENABLE_LINE_INPUT
827 | ENABLE_ECHO_INPUT;
828 /* FIXME: we shouldn't probably use hardcoded UNIX values here. */
829 console->infd = 0;
830 console->outfd = 1;
831 console->hread = console->hwrite = -1;
833 CLIENT_SendRequest( REQ_CREATE_CONSOLE, -1, 1, &req, sizeof(req) );
834 if (CLIENT_WaitReply( &len, NULL, 1, &reply, sizeof(reply) ) != ERROR_SUCCESS)
836 K32OBJ_DecCount(&console->header);
837 SYSTEM_UNLOCK();
838 return FALSE;
840 CHECK_LEN( len, sizeof(reply) );
841 console->hread = reply.handle_read;
842 console->hwrite = reply.handle_write;
844 if ((hIn = HANDLE_Alloc(pdb,&console->header, 0, TRUE,
845 reply.handle_read)) == INVALID_HANDLE_VALUE32)
847 CLIENT_CloseHandle( reply.handle_write );
848 K32OBJ_DecCount(&console->header);
849 SYSTEM_UNLOCK();
850 return FALSE;
853 if ((hOut = HANDLE_Alloc(pdb,&console->header, 0, TRUE,
854 reply.handle_write)) == INVALID_HANDLE_VALUE32)
856 CloseHandle(hIn);
857 K32OBJ_DecCount(&console->header);
858 SYSTEM_UNLOCK();
859 return FALSE;
862 if (!DuplicateHandle( GetCurrentProcess(), hOut,
863 GetCurrentProcess(), &hErr,
864 0, TRUE, DUPLICATE_SAME_ACCESS ))
866 CloseHandle(hIn);
867 CloseHandle(hOut);
868 K32OBJ_DecCount(&console->header);
869 SYSTEM_UNLOCK();
870 return FALSE;
873 if (pdb->console) K32OBJ_DecCount( pdb->console );
874 pdb->console = (K32OBJ *)console;
875 K32OBJ_IncCount( pdb->console );
877 /* NT resets the STD_*_HANDLEs on console alloc */
878 SetStdHandle(STD_INPUT_HANDLE, hIn);
879 SetStdHandle(STD_OUTPUT_HANDLE, hOut);
880 SetStdHandle(STD_ERROR_HANDLE, hErr);
882 SetLastError(ERROR_SUCCESS);
883 SYSTEM_UNLOCK();
884 SetConsoleTitle32A("Wine Console");
885 return TRUE;
889 /******************************************************************************
890 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
892 * RETURNS
893 * Code page code
895 UINT32 WINAPI GetConsoleCP(VOID)
897 return GetACP();
901 /***********************************************************************
902 * GetConsoleOutputCP (KERNEL32.189)
904 UINT32 WINAPI GetConsoleOutputCP(VOID)
906 return GetConsoleCP();
909 /***********************************************************************
910 * GetConsoleMode (KERNEL32.188)
912 BOOL32 WINAPI GetConsoleMode(HANDLE32 hcon,LPDWORD mode)
914 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hcon, K32OBJ_CONSOLE, 0, NULL);
916 if (!console) {
917 FIXME(console,"(%d,%p), no console handle passed!\n",hcon,mode);
918 return FALSE;
920 *mode = console->mode;
921 K32OBJ_DecCount(&console->header);
922 return TRUE;
926 /******************************************************************************
927 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
929 * PARAMS
930 * hcon [I] Handle to console input or screen buffer
931 * mode [I] Input or output mode to set
933 * RETURNS
934 * Success: TRUE
935 * Failure: FALSE
937 BOOL32 WINAPI SetConsoleMode( HANDLE32 hcon, DWORD mode )
939 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hcon, K32OBJ_CONSOLE, 0, NULL);
941 if (!console) {
942 FIXME(console,"(%d,%ld), no console handle passed!\n",hcon,mode);
943 return FALSE;
945 FIXME(console,"(0x%08x,0x%08lx): stub\n",hcon,mode);
946 console->mode = mode;
947 K32OBJ_DecCount(&console->header);
948 return TRUE;
952 /***********************************************************************
953 * GetConsoleTitleA (KERNEL32.191)
955 DWORD WINAPI GetConsoleTitle32A(LPSTR title,DWORD size)
957 PDB32 *pdb = PROCESS_Current();
958 CONSOLE *console= (CONSOLE *)pdb->console;
960 if(console && console->title) {
961 lstrcpyn32A(title,console->title,size);
962 return strlen(title);
964 return 0;
968 /******************************************************************************
969 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
971 * PARAMS
972 * title [O] Address of buffer for title
973 * size [I] Size of buffer
975 * RETURNS
976 * Success: Length of string copied
977 * Failure: 0
979 DWORD WINAPI GetConsoleTitle32W( LPWSTR title, DWORD size )
981 PDB32 *pdb = PROCESS_Current();
982 CONSOLE *console= (CONSOLE *)pdb->console;
983 if(console && console->title)
985 lstrcpynAtoW(title,console->title,size);
986 return (lstrlen32W(title));
988 return 0;
992 /***********************************************************************
993 * WriteConsoleA (KERNEL32.729)
995 BOOL32 WINAPI WriteConsole32A( HANDLE32 hConsoleOutput,
996 LPCVOID lpBuffer,
997 DWORD nNumberOfCharsToWrite,
998 LPDWORD lpNumberOfCharsWritten,
999 LPVOID lpReserved )
1001 /* FIXME: should I check if this is a console handle? */
1002 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
1003 lpNumberOfCharsWritten, NULL);
1007 #define CADD(c) \
1008 if (bufused==curbufsize-1) \
1009 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
1010 buffer[bufused++]=c;
1011 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
1013 /***********************************************************************
1014 * WriteConsoleOutputA (KERNEL32.732)
1016 BOOL32 WINAPI WriteConsoleOutput32A( HANDLE32 hConsoleOutput,
1017 LPCHAR_INFO lpBuffer,
1018 COORD dwBufferSize,
1019 COORD dwBufferCoord,
1020 LPSMALL_RECT lpWriteRegion)
1022 int i,j,off=0,lastattr=-1;
1023 char sbuf[20],*buffer=NULL;
1024 int bufused=0,curbufsize = 100;
1025 DWORD res;
1026 const int colormap[8] = {
1027 0,4,2,6,
1028 1,5,3,7,
1030 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleOutput, K32OBJ_CONSOLE, 0, NULL);
1032 if (!console) {
1033 FIXME(console,"(%d,...): no console handle!\n",hConsoleOutput);
1034 return FALSE;
1036 CONSOLE_make_complex(console);
1037 buffer = HeapAlloc(GetProcessHeap(),0,100);;
1038 curbufsize = 100;
1040 TRACE(console,"wr: top = %d, bottom=%d, left=%d,right=%d\n",
1041 lpWriteRegion->Top,
1042 lpWriteRegion->Bottom,
1043 lpWriteRegion->Left,
1044 lpWriteRegion->Right
1047 for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
1048 sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
1049 SADD(sbuf);
1050 for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
1051 if (lastattr!=lpBuffer[off].Attributes) {
1052 lastattr = lpBuffer[off].Attributes;
1053 sprintf(sbuf,"%c[0;%s3%d;4%dm",
1055 (lastattr & FOREGROUND_INTENSITY)?"1;":"",
1056 colormap[lastattr&7],
1057 colormap[(lastattr&0x70)>>4]
1059 /* FIXME: BACKGROUND_INTENSITY */
1060 SADD(sbuf);
1062 CADD(lpBuffer[off].Char.AsciiChar);
1063 off++;
1066 sprintf(sbuf,"%c[0m",27);SADD(sbuf);
1067 WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
1068 HeapFree(GetProcessHeap(),0,buffer);
1069 K32OBJ_DecCount(&console->header);
1070 return TRUE;
1073 /***********************************************************************
1074 * WriteConsoleW (KERNEL32.577)
1076 BOOL32 WINAPI WriteConsole32W( HANDLE32 hConsoleOutput,
1077 LPCVOID lpBuffer,
1078 DWORD nNumberOfCharsToWrite,
1079 LPDWORD lpNumberOfCharsWritten,
1080 LPVOID lpReserved )
1082 BOOL32 ret;
1083 LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
1085 lstrcpynWtoA( xstring, lpBuffer,nNumberOfCharsToWrite);
1087 /* FIXME: should I check if this is a console handle? */
1088 ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
1089 lpNumberOfCharsWritten, NULL);
1090 HeapFree( GetProcessHeap(), 0, xstring );
1091 return ret;
1095 /***********************************************************************
1096 * ReadConsoleA (KERNEL32.419)
1098 BOOL32 WINAPI ReadConsole32A( HANDLE32 hConsoleInput,
1099 LPVOID lpBuffer,
1100 DWORD nNumberOfCharsToRead,
1101 LPDWORD lpNumberOfCharsRead,
1102 LPVOID lpReserved )
1104 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput, K32OBJ_CONSOLE, 0, NULL);
1105 int i,charsread = 0;
1106 LPSTR xbuf = (LPSTR)lpBuffer;
1108 if (!console) {
1109 SetLastError(ERROR_INVALID_HANDLE);
1110 FIXME(console,"(%d,...), no console handle!\n",hConsoleInput);
1111 return FALSE;
1113 TRACE(console,"(%d,%p,%ld,%p,%p)\n",
1114 hConsoleInput,lpBuffer,nNumberOfCharsToRead,
1115 lpNumberOfCharsRead,lpReserved
1117 CONSOLE_get_input(console);
1119 /* FIXME: should this drain everything from the input queue and just
1120 * put the keypresses in the buffer? Needs further thought.
1122 for (i=0;(i<console->nrofirs)&&(charsread<nNumberOfCharsToRead);i++) {
1123 if (console->irs[i].EventType != KEY_EVENT)
1124 continue;
1125 if (!console->irs[i].Event.KeyEvent.bKeyDown)
1126 continue;
1127 *xbuf++ = console->irs[i].Event.KeyEvent.uChar.AsciiChar;
1128 charsread++;
1130 CONSOLE_drain_input(console,i);
1131 if (lpNumberOfCharsRead)
1132 *lpNumberOfCharsRead = charsread;
1133 K32OBJ_DecCount(&console->header);
1134 return TRUE;
1137 /***********************************************************************
1138 * ReadConsoleW (KERNEL32.427)
1140 BOOL32 WINAPI ReadConsole32W( HANDLE32 hConsoleInput,
1141 LPVOID lpBuffer,
1142 DWORD nNumberOfCharsToRead,
1143 LPDWORD lpNumberOfCharsRead,
1144 LPVOID lpReserved )
1146 BOOL32 ret;
1147 LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
1149 ret = ReadConsole32A(
1150 hConsoleInput,
1151 buf,
1152 nNumberOfCharsToRead,
1153 lpNumberOfCharsRead,
1154 lpReserved
1156 if (ret)
1157 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
1158 HeapFree( GetProcessHeap(), 0, buf );
1159 return ret;
1163 /******************************************************************************
1164 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
1166 * PARAMS
1167 * hConsoleInput [I] Handle to console input buffer
1168 * lpBuffer [O] Address of buffer for read data
1169 * nLength [I] Number of records to read
1170 * lpNumberOfEventsRead [O] Address of number of records read
1172 * RETURNS
1173 * Success: TRUE
1174 * Failure: FALSE
1176 BOOL32 WINAPI ReadConsoleInput32A(HANDLE32 hConsoleInput,
1177 LPINPUT_RECORD lpBuffer,
1178 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1180 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput, K32OBJ_CONSOLE, 0, NULL);
1182 TRACE(console, "(%d,%p,%ld,%p)\n",hConsoleInput, lpBuffer, nLength,
1183 lpNumberOfEventsRead);
1184 if (!console) {
1185 FIXME(console, "(%d,%p,%ld,%p), No console handle!\n",hConsoleInput,
1186 lpBuffer, nLength, lpNumberOfEventsRead);
1187 return FALSE;
1189 CONSOLE_get_input(console);
1190 if (nLength>console->nrofirs)
1191 nLength = console->nrofirs;
1192 memcpy(lpBuffer,console->irs,sizeof(INPUT_RECORD)*nLength);
1193 if (lpNumberOfEventsRead)
1194 *lpNumberOfEventsRead = nLength;
1195 CONSOLE_drain_input(console,nLength);
1196 K32OBJ_DecCount(&console->header);
1197 return TRUE;
1200 /***********************************************************************
1201 * SetConsoleTitle32A (KERNEL32.476)
1203 * Sets the console title.
1205 * We do not necessarily need to create a complex console for that,
1206 * but should remember the title and set it on creation of the latter.
1207 * (not fixed at this time).
1209 BOOL32 WINAPI SetConsoleTitle32A(LPCSTR title)
1211 PDB32 *pdb = PROCESS_Current();
1212 CONSOLE *console;
1213 DWORD written;
1214 char titleformat[]="\033]2;%s\a"; /*this should work for xterms*/
1215 LPSTR titlestring;
1216 BOOL32 ret=FALSE;
1218 TRACE(console,"(%s)\n",title);
1220 console = (CONSOLE *)pdb->console;
1221 if (!console)
1222 return FALSE;
1223 if(console->title) /* Free old title, if there is one */
1224 HeapFree( SystemHeap, 0, console->title );
1225 console->title = (LPSTR)HeapAlloc(SystemHeap, 0,strlen(title)+1);
1226 if(console->title) strcpy(console->title,title);
1227 titlestring = HeapAlloc(GetProcessHeap(), 0,strlen(title)+strlen(titleformat)+1);
1228 if (!titlestring) {
1229 K32OBJ_DecCount(&console->header);
1230 return FALSE;
1233 sprintf(titlestring,titleformat,title);
1234 /* FIXME: hmm, should use WriteFile probably... */
1235 CONSOLE_Write(&console->header,titlestring,strlen(titlestring),&written,NULL);
1236 if (written == strlen(titlestring))
1237 ret =TRUE;
1238 HeapFree( GetProcessHeap(), 0, titlestring );
1239 K32OBJ_DecCount(&console->header);
1240 return ret;
1244 /******************************************************************************
1245 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1247 * PARAMS
1248 * title [I] Address of new title
1250 * NOTES
1251 * This should not be calling the A version
1253 * RETURNS
1254 * Success: TRUE
1255 * Failure: FALSE
1257 BOOL32 WINAPI SetConsoleTitle32W( LPCWSTR title )
1259 BOOL32 ret;
1261 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1262 ret = SetConsoleTitle32A(titleA);
1263 HeapFree( GetProcessHeap(), 0, titleA );
1264 return ret;
1267 /***********************************************************************
1268 * ReadConsoleInput32W (KERNEL32.570)
1270 BOOL32 WINAPI ReadConsoleInput32W(HANDLE32 hConsoleInput,
1271 LPINPUT_RECORD lpBuffer,
1272 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1274 FIXME(console, "(%d,%p,%ld,%p): stub\n",hConsoleInput, lpBuffer, nLength,
1275 lpNumberOfEventsRead);
1276 return 0;
1279 /***********************************************************************
1280 * FlushConsoleInputBuffer (KERNEL32.132)
1282 BOOL32 WINAPI FlushConsoleInputBuffer(HANDLE32 hConsoleInput)
1284 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput, K32OBJ_CONSOLE, 0, NULL);
1286 if (!console)
1287 return FALSE;
1288 CONSOLE_drain_input(console,console->nrofirs);
1289 K32OBJ_DecCount(&console->header);
1290 return TRUE;
1294 /******************************************************************************
1295 * SetConsoleCursorPosition [KERNEL32.627]
1296 * Sets the cursor position in console
1298 * PARAMS
1299 * hConsoleOutput [I] Handle of console screen buffer
1300 * dwCursorPosition [I] New cursor position coordinates
1302 * RETURNS STD
1304 BOOL32 WINAPI SetConsoleCursorPosition( HANDLE32 hcon, COORD pos )
1306 char xbuf[20];
1307 DWORD xlen;
1308 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hcon, K32OBJ_CONSOLE, 0, NULL);
1310 if (!console) {
1311 FIXME(console,"(%d,...), no console handle!\n",hcon);
1312 return FALSE;
1314 CONSOLE_make_complex(console);
1315 TRACE(console, "%d (%dx%d)\n", hcon, pos.x , pos.y );
1316 /* x are columns, y rows */
1317 sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1318 /* FIXME: store internal if we start using own console buffers */
1319 WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1320 K32OBJ_DecCount(&console->header);
1321 return TRUE;
1324 /***********************************************************************
1325 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1327 BOOL32 WINAPI GetNumberOfConsoleInputEvents(HANDLE32 hcon,LPDWORD nrofevents)
1329 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hcon, K32OBJ_CONSOLE, 0, NULL);
1331 if (!console) {
1332 FIXME(console,"(%d,%p), no console handle!\n",hcon,nrofevents);
1333 return FALSE;
1335 CONSOLE_get_input(console);
1336 *nrofevents = console->nrofirs;
1337 K32OBJ_DecCount(&console->header);
1338 return TRUE;
1341 /***********************************************************************
1342 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1344 BOOL32 WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1346 FIXME(console,"(%p): stub\n", nrofbuttons);
1347 *nrofbuttons = 2;
1348 return TRUE;
1351 /***********************************************************************
1352 * PeekConsoleInputA (KERNEL32.550)
1354 * Gets 'cInRecords' first events (or less) from input queue.
1356 * Does not need a complex console.
1358 BOOL32 WINAPI PeekConsoleInput32A(HANDLE32 hConsoleInput,
1359 LPINPUT_RECORD pirBuffer,
1360 DWORD cInRecords,
1361 LPDWORD lpcRead)
1363 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput, K32OBJ_CONSOLE, 0, NULL);
1365 if (!console) {
1366 FIXME(console,"(%d,%p,%ld,%p), No console handle passed!\n",hConsoleInput, pirBuffer, cInRecords, lpcRead);
1367 return FALSE;
1369 TRACE(console,"(%d,%p,%ld,%p)\n",hConsoleInput, pirBuffer, cInRecords, lpcRead);
1370 CONSOLE_get_input(console);
1371 if (cInRecords>console->nrofirs)
1372 cInRecords = console->nrofirs;
1373 if (pirBuffer)
1374 memcpy(pirBuffer,console->irs,cInRecords*sizeof(INPUT_RECORD));
1375 if (lpcRead)
1376 *lpcRead = cInRecords;
1377 K32OBJ_DecCount(&console->header);
1378 return TRUE;
1381 /***********************************************************************
1382 * PeekConsoleInputW (KERNEL32.551)
1384 BOOL32 WINAPI PeekConsoleInput32W(HANDLE32 hConsoleInput,
1385 LPINPUT_RECORD pirBuffer,
1386 DWORD cInRecords,
1387 LPDWORD lpcRead)
1389 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1390 return PeekConsoleInput32A(hConsoleInput,pirBuffer,cInRecords,lpcRead);
1394 /******************************************************************************
1395 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1397 * PARAMS
1398 * hcon [I] Handle to console screen buffer
1399 * cinfo [O] Address of cursor information
1401 * RETURNS
1402 * Success: TRUE
1403 * Failure: FALSE
1405 BOOL32 WINAPI GetConsoleCursorInfo32( HANDLE32 hcon,
1406 LPCONSOLE_CURSOR_INFO cinfo )
1408 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hcon, K32OBJ_CONSOLE, 0, NULL);
1410 if (!console) {
1411 FIXME(console, "(%x,%p), no console handle!\n", hcon, cinfo);
1412 return FALSE;
1414 TRACE(console, "(%x,%p)\n", hcon, cinfo);
1415 if (!cinfo)
1416 return FALSE;
1417 *cinfo = console->cinfo;
1418 K32OBJ_DecCount(&console->header);
1419 return TRUE;
1423 /******************************************************************************
1424 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1426 * RETURNS
1427 * Success: TRUE
1428 * Failure: FALSE
1430 BOOL32 WINAPI SetConsoleCursorInfo32(
1431 HANDLE32 hcon, /* [in] Handle to console screen buffer */
1432 LPCONSOLE_CURSOR_INFO cinfo) /* [in] Address of cursor information */
1434 char buf[8];
1435 DWORD xlen;
1436 CONSOLE *console = (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), hcon, K32OBJ_CONSOLE, 0, NULL);
1438 TRACE(console, "(%x,%ld,%i): stub\n", hcon,cinfo->dwSize,cinfo->bVisible);
1439 if (!console)
1440 return FALSE;
1441 CONSOLE_make_complex(console);
1442 sprintf(buf,"%c[?25%c",27,cinfo->bVisible?'h':'l');
1443 WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1444 console->cinfo = *cinfo;
1445 K32OBJ_DecCount(&console->header);
1446 return TRUE;
1450 /******************************************************************************
1451 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1453 * RETURNS
1454 * Success: TRUE
1455 * Failure: FALSE
1457 BOOL32 WINAPI SetConsoleWindowInfo(
1458 HANDLE32 hcon, /* [in] Handle to console screen buffer */
1459 BOOL32 bAbsolute, /* [in] Coordinate type flag */
1460 LPSMALL_RECT window) /* [in] Address of new window rectangle */
1462 FIXME(console, "(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1463 return TRUE;
1467 /******************************************************************************
1468 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1470 * Sets the foreground and background color attributes of characters
1471 * written to the screen buffer.
1473 * RETURNS
1474 * Success: TRUE
1475 * Failure: FALSE
1477 BOOL32 WINAPI SetConsoleTextAttribute32(HANDLE32 hConsoleOutput,WORD wAttr)
1479 const int colormap[8] = {
1480 0,4,2,6,
1481 1,5,3,7,
1483 DWORD xlen;
1484 char buffer[20];
1486 TRACE(console,"(%d,%d)\n",hConsoleOutput,wAttr);
1487 sprintf(buffer,"%c[0;%s3%d;4%dm",
1489 (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1490 colormap[wAttr&7],
1491 colormap[(wAttr&0x70)>>4]
1493 WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1494 return TRUE;
1498 /******************************************************************************
1499 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1501 * PARAMS
1502 * hConsoleOutput [I] Handle to console screen buffer
1503 * dwSize [I] New size in character rows and cols
1505 * RETURNS
1506 * Success: TRUE
1507 * Failure: FALSE
1509 BOOL32 WINAPI SetConsoleScreenBufferSize( HANDLE32 hConsoleOutput,
1510 COORD dwSize )
1512 FIXME(console, "(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1513 return TRUE;
1517 /******************************************************************************
1518 * FillConsoleOutputCharacterA [KERNEL32.242]
1520 * PARAMS
1521 * hConsoleOutput [I] Handle to screen buffer
1522 * cCharacter [I] Character to write
1523 * nLength [I] Number of cells to write to
1524 * dwCoord [I] Coords of first cell
1525 * lpNumCharsWritten [O] Pointer to number of cells written
1527 * RETURNS
1528 * Success: TRUE
1529 * Failure: FALSE
1531 BOOL32 WINAPI FillConsoleOutputCharacterA(
1532 HANDLE32 hConsoleOutput,
1533 BYTE cCharacter,
1534 DWORD nLength,
1535 COORD dwCoord,
1536 LPDWORD lpNumCharsWritten)
1538 long count;
1539 DWORD xlen;
1541 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1542 for(count=0;count<nLength;count++)
1543 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1544 *lpNumCharsWritten = nLength;
1545 return TRUE;
1549 /******************************************************************************
1550 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1552 * PARAMS
1553 * hConsoleOutput [I] Handle to screen buffer
1554 * cCharacter [I] Character to write
1555 * nLength [I] Number of cells to write to
1556 * dwCoord [I] Coords of first cell
1557 * lpNumCharsWritten [O] Pointer to number of cells written
1559 * RETURNS
1560 * Success: TRUE
1561 * Failure: FALSE
1563 BOOL32 WINAPI FillConsoleOutputCharacterW(HANDLE32 hConsoleOutput,
1564 WCHAR cCharacter,
1565 DWORD nLength,
1566 COORD dwCoord,
1567 LPDWORD lpNumCharsWritten)
1569 long count;
1570 DWORD xlen;
1572 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1573 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1574 * first
1576 for(count=0;count<nLength;count++)
1577 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1578 *lpNumCharsWritten = nLength;
1579 return TRUE;
1583 /******************************************************************************
1584 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1586 * PARAMS
1587 * hConsoleOutput [I] Handle to screen buffer
1588 * wAttribute [I] Color attribute to write
1589 * nLength [I] Number of cells to write to
1590 * dwCoord [I] Coords of first cell
1591 * lpNumAttrsWritten [O] Pointer to number of cells written
1593 * RETURNS
1594 * Success: TRUE
1595 * Failure: FALSE
1597 BOOL32 WINAPI FillConsoleOutputAttribute( HANDLE32 hConsoleOutput,
1598 WORD wAttribute, DWORD nLength, COORD dwCoord,
1599 LPDWORD lpNumAttrsWritten)
1601 FIXME(console, "(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1602 wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1603 *lpNumAttrsWritten = nLength;
1604 return TRUE;
1607 /******************************************************************************
1608 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1610 * BUGS
1611 * Unimplemented
1613 BOOL32 WINAPI ReadConsoleOutputCharacter32A(HANDLE32 hConsoleOutput,
1614 LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1616 FIXME(console, "(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1617 dword,coord.x,coord.y,lpdword);
1618 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1619 return FALSE;
1623 /******************************************************************************
1624 * ScrollConsoleScreenBuffer [KERNEL32.612]
1626 * BUGS
1627 * Unimplemented
1629 BOOL32 WINAPI ScrollConsoleScreenBuffer( HANDLE32 hConsoleOutput,
1630 LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1631 COORD dwDestOrigin, LPCHAR_INFO lpFill)
1633 FIXME(console, "(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1634 lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1635 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1636 return FALSE;