- Fixed creation of message queue in hook API routines.
[wine/wine-kai.git] / win32 / console.c
blob0328d432bec003c197d292bc977df951c49e7082
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;
54 INPUT_RECORD *irs; /* buffered input records */
55 int nrofirs;/* nr of buffered input records */
56 } CONSOLE;
58 static void CONSOLE_Destroy( K32OBJ *obj );
60 const K32OBJ_OPS CONSOLE_Ops =
62 CONSOLE_Destroy /* destroy */
65 /***********************************************************************
66 * CONSOLE_Destroy
68 static void CONSOLE_Destroy(K32OBJ *obj)
70 CONSOLE *console = (CONSOLE *)obj;
71 assert(obj->type == K32OBJ_CONSOLE);
73 obj->type = K32OBJ_UNKNOWN;
75 HeapFree(SystemHeap, 0, console);
79 /***********************************************************************
80 * CONSOLE_GetPtr
82 static CONSOLE *CONSOLE_GetPtr( HANDLE32 handle )
84 return (CONSOLE*)HANDLE_GetObjPtr( PROCESS_Current(), handle,
85 K32OBJ_CONSOLE, 0, NULL );
88 /****************************************************************************
89 * CONSOLE_GetInfo
91 static BOOL32 CONSOLE_GetInfo( HANDLE32 handle, struct get_console_info_reply *reply )
93 struct get_console_info_request req;
95 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
96 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
97 return FALSE;
98 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
99 return !CLIENT_WaitSimpleReply( reply, sizeof(*reply), NULL );
103 /****************************************************************************
104 * CONSOLE_add_input_record [internal]
106 * Adds an INPUT_RECORD to the CONSOLEs input queue.
108 static void
109 CONSOLE_add_input_record(CONSOLE *console,INPUT_RECORD *inp) {
110 console->irs = HeapReAlloc(GetProcessHeap(),0,console->irs,sizeof(INPUT_RECORD)*(console->nrofirs+1));
111 console->irs[console->nrofirs++]=*inp;
114 /****************************************************************************
115 * XTERM_string_to_IR [internal]
117 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
118 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
120 static void
121 CONSOLE_string_to_IR( HANDLE32 hConsoleInput,unsigned char *buf,int len) {
122 int j,k;
123 INPUT_RECORD ir;
124 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
126 for (j=0;j<len;j++) {
127 unsigned char inchar = buf[j];
129 if (inchar!=27) { /* no escape -> 'normal' keyboard event */
130 ir.EventType = 1; /* Key_event */
132 ir.Event.KeyEvent.bKeyDown = 1;
133 ir.Event.KeyEvent.wRepeatCount = 0;
135 ir.Event.KeyEvent.dwControlKeyState = 0;
136 if (inchar & 0x80) {
137 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
138 inchar &= ~0x80;
140 ir.Event.KeyEvent.wVirtualKeyCode = VkKeyScan16(inchar);
141 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0100)
142 ir.Event.KeyEvent.dwControlKeyState|=SHIFT_PRESSED;
143 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0200)
144 ir.Event.KeyEvent.dwControlKeyState|=LEFT_CTRL_PRESSED;
145 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0400)
146 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
147 ir.Event.KeyEvent.wVirtualScanCode = MapVirtualKey16(
148 ir.Event.KeyEvent.wVirtualKeyCode & 0x00ff,
149 0 /* VirtualKeyCodes to ScanCode */
151 if (inchar=='\n') {
152 ir.Event.KeyEvent.uChar.AsciiChar = '\r';
153 ir.Event.KeyEvent.wVirtualKeyCode = 0x0d;
154 ir.Event.KeyEvent.wVirtualScanCode = 0x1c;
155 } else {
156 ir.Event.KeyEvent.uChar.AsciiChar = inchar;
157 if (inchar<' ') {
158 /* FIXME: find good values for ^X */
159 ir.Event.KeyEvent.wVirtualKeyCode = 0xdead;
160 ir.Event.KeyEvent.wVirtualScanCode = 0xbeef;
164 CONSOLE_add_input_record(console,&ir);
165 ir.Event.KeyEvent.bKeyDown = 0;
166 CONSOLE_add_input_record(console,&ir);
167 continue;
169 /* inchar is ESC */
170 if ((j==len-1) || (buf[j+1]!='[')) {/* add ESCape on its own */
171 ir.EventType = 1; /* Key_event */
172 ir.Event.KeyEvent.bKeyDown = 1;
173 ir.Event.KeyEvent.wRepeatCount = 0;
175 ir.Event.KeyEvent.wVirtualKeyCode = VkKeyScan16(27);
176 ir.Event.KeyEvent.wVirtualScanCode = MapVirtualKey16(
177 ir.Event.KeyEvent.wVirtualKeyCode,0
179 ir.Event.KeyEvent.dwControlKeyState = 0;
180 ir.Event.KeyEvent.uChar.AsciiChar = 27;
181 CONSOLE_add_input_record(console,&ir);
182 ir.Event.KeyEvent.bKeyDown = 0;
183 CONSOLE_add_input_record(console,&ir);
184 continue;
186 for (k=j;k<len;k++) {
187 if (((buf[k]>='A') && (buf[k]<='Z')) ||
188 ((buf[k]>='a') && (buf[k]<='z')) ||
189 (buf[k]=='~')
191 break;
193 if (k<len) {
194 int subid,scancode=0;
196 ir.EventType = 1; /* Key_event */
197 ir.Event.KeyEvent.bKeyDown = 1;
198 ir.Event.KeyEvent.wRepeatCount = 0;
199 ir.Event.KeyEvent.dwControlKeyState = 0;
201 ir.Event.KeyEvent.wVirtualKeyCode = 0xad; /* FIXME */
202 ir.Event.KeyEvent.wVirtualScanCode = 0xad; /* FIXME */
203 ir.Event.KeyEvent.uChar.AsciiChar = 0;
205 switch (buf[k]) {
206 case '~':
207 sscanf(&buf[j+2],"%d",&subid);
208 switch (subid) {
209 case 2:/*INS */scancode = 0xe052;break;
210 case 3:/*DEL */scancode = 0xe053;break;
211 case 6:/*PGDW*/scancode = 0xe051;break;
212 case 5:/*PGUP*/scancode = 0xe049;break;
213 case 11:/*F1 */scancode = 0x003b;break;
214 case 12:/*F2 */scancode = 0x003c;break;
215 case 13:/*F3 */scancode = 0x003d;break;
216 case 14:/*F4 */scancode = 0x003e;break;
217 case 15:/*F5 */scancode = 0x003f;break;
218 case 17:/*F6 */scancode = 0x0040;break;
219 case 18:/*F7 */scancode = 0x0041;break;
220 case 19:/*F8 */scancode = 0x0042;break;
221 case 20:/*F9 */scancode = 0x0043;break;
222 case 21:/*F10 */scancode = 0x0044;break;
223 case 23:/*F11 */scancode = 0x00d9;break;
224 case 24:/*F12 */scancode = 0x00da;break;
225 /* FIXME: Shift-Fx */
226 default:
227 FIXME(console,"parse ESC[%d~\n",subid);
228 break;
230 break;
231 case 'A': /* Cursor Up */scancode = 0xe048;break;
232 case 'B': /* Cursor Down */scancode = 0xe050;break;
233 case 'D': /* Cursor Left */scancode = 0xe04b;break;
234 case 'C': /* Cursor Right */scancode = 0xe04d;break;
235 case 'F': /* End */scancode = 0xe04f;break;
236 case 'H': /* Home */scancode = 0xe047;break;
237 case 'M':
238 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
239 * Release (ESCM#<x+'!'><y+'!'>
241 if (k<len-3) {
242 ir.EventType = MOUSE_EVENT;
243 ir.Event.MouseEvent.dwMousePosition.x = buf[k+2]-'!';
244 ir.Event.MouseEvent.dwMousePosition.y = buf[k+3]-'!';
245 if (buf[k+1]=='#')
246 ir.Event.MouseEvent.dwButtonState = 0;
247 else
248 ir.Event.MouseEvent.dwButtonState = 1<<(buf[k+1]-' ');
249 ir.Event.MouseEvent.dwEventFlags = 0; /* FIXME */
250 CONSOLE_add_input_record(console,&ir);
251 j=k+3;
253 break;
256 if (scancode) {
257 ir.Event.KeyEvent.wVirtualScanCode = scancode;
258 ir.Event.KeyEvent.wVirtualKeyCode = MapVirtualKey16(scancode,1);
259 CONSOLE_add_input_record(console,&ir);
260 ir.Event.KeyEvent.bKeyDown = 0;
261 CONSOLE_add_input_record(console,&ir);
262 j=k;
263 continue;
267 K32OBJ_DecCount(&console->header);
270 /****************************************************************************
271 * CONSOLE_get_input (internal)
273 * Reads (nonblocking) as much input events as possible and stores them
274 * in an internal queue.
276 static void
277 CONSOLE_get_input( HANDLE32 handle )
279 char *buf = HeapAlloc(GetProcessHeap(),0,1);
280 int len = 0;
282 while (1)
284 DWORD res;
285 char inchar;
286 if (WaitForSingleObject( handle, 0 )) break;
287 if (!ReadFile( handle, &inchar, 1, &res, NULL )) break;
288 buf = HeapReAlloc(GetProcessHeap(),0,buf,len+1);
289 buf[len++]=inchar;
291 CONSOLE_string_to_IR(handle,buf,len);
292 HeapFree(GetProcessHeap(),0,buf);
295 /****************************************************************************
296 * CONSOLE_drain_input (internal)
298 * Drains 'n' console input events from the queue.
300 static void
301 CONSOLE_drain_input(CONSOLE *console,int n) {
302 assert(n<=console->nrofirs);
303 if (n) {
304 console->nrofirs-=n;
305 memcpy( &console->irs[0],
306 &console->irs[n],
307 console->nrofirs*sizeof(INPUT_RECORD)
309 console->irs = HeapReAlloc(
310 GetProcessHeap(),
312 console->irs,
313 console->nrofirs*sizeof(INPUT_RECORD)
319 /******************************************************************************
320 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
322 * PARAMS
323 * func [I] Address of handler function
324 * add [I] Handler to add or remove
326 * RETURNS
327 * Success: TRUE
328 * Failure: FALSE
330 * CHANGED
331 * James Sutherland (JamesSutherland@gmx.de)
332 * Added global variables console_ignore_ctrl_c and handlers[]
333 * Does not yet do any error checking, or set LastError if failed.
334 * This doesn't yet matter, since these handlers are not yet called...!
336 static unsigned int console_ignore_ctrl_c = 0;
337 static HANDLER_ROUTINE *handlers[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
338 BOOL32 WINAPI SetConsoleCtrlHandler( HANDLER_ROUTINE *func, BOOL32 add )
340 unsigned int alloc_loop = sizeof(handlers)/sizeof(HANDLER_ROUTINE *);
341 unsigned int done = 0;
342 FIXME(console, "(%p,%i) - no error checking or testing yet\n", func, add);
343 if (!func)
345 console_ignore_ctrl_c = add;
346 return TRUE;
348 if (add)
350 for (;alloc_loop--;)
351 if (!handlers[alloc_loop] && !done)
353 handlers[alloc_loop] = func;
354 done++;
356 if (!done)
357 FIXME(console, "Out of space on CtrlHandler table\n");
358 return(done);
360 else
362 for (;alloc_loop--;)
363 if (handlers[alloc_loop] == func && !done)
365 handlers[alloc_loop] = 0;
366 done++;
368 if (!done)
369 WARN(console, "Attempt to remove non-installed CtrlHandler %p\n",
370 func);
371 return (done);
373 return (done);
377 /******************************************************************************
378 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
380 * PARAMS
381 * dwCtrlEvent [I] Type of event
382 * dwProcessGroupID [I] Process group ID to send event to
384 * NOTES
385 * Doesn't yet work...!
387 * RETURNS
388 * Success: True
389 * Failure: False (and *should* [but doesn't] set LastError)
391 BOOL32 WINAPI GenerateConsoleCtrlEvent( DWORD dwCtrlEvent,
392 DWORD dwProcessGroupID )
394 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
396 ERR( console, "invalid event %d for PGID %ld\n",
397 (unsigned short)dwCtrlEvent, dwProcessGroupID );
398 return FALSE;
400 if (dwProcessGroupID == GetCurrentProcessId() )
402 FIXME( console, "Attempt to send event %d to self - stub\n",
403 (unsigned short)dwCtrlEvent );
404 return FALSE;
406 FIXME( console,"event %d to external PGID %ld - not implemented yet\n",
407 (unsigned short)dwCtrlEvent, dwProcessGroupID );
408 return FALSE;
412 /******************************************************************************
413 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
415 * PARAMS
416 * dwDesiredAccess [I] Access flag
417 * dwShareMode [I] Buffer share mode
418 * sa [I] Security attributes
419 * dwFlags [I] Type of buffer to create
420 * lpScreenBufferData [I] Reserved
422 * NOTES
423 * Should call SetLastError
425 * RETURNS
426 * Success: Handle to new console screen buffer
427 * Failure: INVALID_HANDLE_VALUE
429 HANDLE32 WINAPI CreateConsoleScreenBuffer( DWORD dwDesiredAccess,
430 DWORD dwShareMode, LPSECURITY_ATTRIBUTES sa,
431 DWORD dwFlags, LPVOID lpScreenBufferData )
433 FIXME(console, "(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess,
434 dwShareMode, sa, dwFlags, lpScreenBufferData);
435 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
436 return INVALID_HANDLE_VALUE32;
440 /***********************************************************************
441 * GetConsoleScreenBufferInfo (KERNEL32.190)
443 BOOL32 WINAPI GetConsoleScreenBufferInfo( HANDLE32 hConsoleOutput,
444 LPCONSOLE_SCREEN_BUFFER_INFO csbi )
446 csbi->dwSize.x = 80;
447 csbi->dwSize.y = 24;
448 csbi->dwCursorPosition.x = 0;
449 csbi->dwCursorPosition.y = 0;
450 csbi->wAttributes = 0;
451 csbi->srWindow.Left = 0;
452 csbi->srWindow.Right = 79;
453 csbi->srWindow.Top = 0;
454 csbi->srWindow.Bottom = 23;
455 csbi->dwMaximumWindowSize.x = 80;
456 csbi->dwMaximumWindowSize.y = 24;
457 return TRUE;
461 /******************************************************************************
462 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
464 * RETURNS
465 * Success: TRUE
466 * Failure: FALSE
468 BOOL32 WINAPI SetConsoleActiveScreenBuffer(
469 HANDLE32 hConsoleOutput) /* [in] Handle to console screen buffer */
471 FIXME(console, "(%x): stub\n", hConsoleOutput);
472 return FALSE;
476 /***********************************************************************
477 * GetLargestConsoleWindowSize (KERNEL32.226)
479 DWORD WINAPI GetLargestConsoleWindowSize( HANDLE32 hConsoleOutput )
481 return (DWORD)MAKELONG(80,24);
484 /***********************************************************************
485 * FreeConsole (KERNEL32.267)
487 BOOL32 WINAPI FreeConsole(VOID)
490 PDB32 *pdb = PROCESS_Current();
491 CONSOLE *console;
493 SYSTEM_LOCK();
495 console = (CONSOLE *)pdb->console;
497 if (console == NULL) {
498 SetLastError(ERROR_INVALID_PARAMETER);
499 return FALSE;
502 CLIENT_SendRequest( REQ_FREE_CONSOLE, -1, 0 );
503 if (CLIENT_WaitReply( NULL, NULL, 0 ) != ERROR_SUCCESS)
505 K32OBJ_DecCount(&console->header);
506 SYSTEM_UNLOCK();
507 return FALSE;
510 HANDLE_CloseAll( pdb, &console->header );
511 K32OBJ_DecCount( &console->header );
512 pdb->console = NULL;
513 SYSTEM_UNLOCK();
514 return TRUE;
518 /*************************************************************************
519 * CONSOLE_OpenHandle
521 * Open a handle to the current process console.
523 HANDLE32 CONSOLE_OpenHandle( BOOL32 output, DWORD access, LPSECURITY_ATTRIBUTES sa )
525 struct open_console_request req;
526 struct open_console_reply reply;
527 CONSOLE *console;
528 HANDLE32 handle;
530 req.output = output;
531 req.access = access;
532 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
533 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
534 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
535 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
537 SYSTEM_LOCK();
538 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
540 SYSTEM_UNLOCK();
541 return FALSE;
543 console->header.type = K32OBJ_CONSOLE;
544 console->header.refcount = 1;
545 console->nrofirs = 0;
546 console->irs = HeapAlloc(GetProcessHeap(),0,1);;
547 handle = HANDLE_Alloc( PROCESS_Current(), &console->header, req.access,
548 req.inherit, reply.handle );
549 SYSTEM_UNLOCK();
550 K32OBJ_DecCount(&console->header);
551 return handle;
555 /*************************************************************************
556 * CONSOLE_make_complex [internal]
558 * Turns a CONSOLE kernel object into a complex one.
559 * (switches from output/input using the terminal where WINE was started to
560 * its own xterm).
562 * This makes simple commandline tools pipeable, while complex commandline
563 * tools work without getting messed up by debugoutput.
565 * All other functions should work indedependend from this call.
567 * To test for complex console: pid == 0 -> simple, otherwise complex.
569 static BOOL32 CONSOLE_make_complex(HANDLE32 handle)
571 struct set_console_fd_request req;
572 struct get_console_info_reply info;
573 struct termios term;
574 char buf[256];
575 char c = '\0';
576 int status = 0;
577 int i,xpid,master,slave;
578 DWORD xlen;
580 if (!CONSOLE_GetInfo( handle, &info )) return FALSE;
581 if (info.pid) return TRUE; /* already complex */
583 MSG("Console: Making console complex (creating an xterm)...\n");
585 if (tcgetattr(0, &term) < 0) return FALSE;
586 term.c_lflag = ~(ECHO|ICANON);
588 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
589 K32OBJ_CONSOLE, 0 )) == -1)
590 return FALSE;
592 if (wine_openpty(&master, &slave, NULL, &term, NULL) < 0)
593 return FALSE;
595 if ((xpid=fork()) == 0) {
596 tcsetattr(slave, TCSADRAIN, &term);
597 sprintf(buf, "-Sxx%d", master);
598 /* "-fn vga" for VGA font. Harmless if vga is not present:
599 * xterm: unable to open font "vga", trying "fixed"....
601 execlp("xterm", "xterm", buf, "-fn","vga",NULL);
602 ERR(console, "error creating AllocConsole xterm\n");
603 exit(1);
606 req.pid = xpid;
607 CLIENT_SendRequest( REQ_SET_CONSOLE_FD, dup(slave), 1, &req, sizeof(req) );
608 CLIENT_WaitReply( NULL, NULL, 0 );
610 /* most xterms like to print their window ID when used with -S;
611 * read it and continue before the user has a chance...
613 for (i=0; c!='\n'; (status=read(slave, &c, 1)), i++) {
614 if (status == -1 && c == '\0') {
615 /* wait for xterm to be created */
616 usleep(100);
618 if (i > 10000) {
619 ERR(console, "can't read xterm WID\n");
620 kill(xpid, SIGKILL);
621 return FALSE;
624 /* enable mouseclicks */
625 sprintf(buf,"%c[?1001s%c[?1000h",27,27);
626 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
628 if (GetConsoleTitle32A( buf, sizeof(buf) ))
630 WriteFile(handle,"\033]2;",4,&xlen,NULL);
631 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
632 WriteFile(handle,"\a",1,&xlen,NULL);
634 return TRUE;
639 /***********************************************************************
640 * AllocConsole (KERNEL32.103)
642 * creates an xterm with a pty to our program
644 BOOL32 WINAPI AllocConsole(VOID)
646 struct open_console_request req;
647 struct open_console_reply reply;
648 PDB32 *pdb = PROCESS_Current();
649 CONSOLE *console;
650 HANDLE32 hIn, hOut, hErr;
652 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
654 console = (CONSOLE *)pdb->console;
656 /* don't create a console if we already have one */
657 if (console != NULL) {
658 SetLastError(ERROR_ACCESS_DENIED);
659 SYSTEM_UNLOCK();
660 return FALSE;
663 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
665 SYSTEM_UNLOCK();
666 return FALSE;
669 console->header.type = K32OBJ_CONSOLE;
670 console->header.refcount = 1;
671 console->nrofirs = 0;
672 console->irs = HeapAlloc(GetProcessHeap(),0,1);;
674 CLIENT_SendRequest( REQ_ALLOC_CONSOLE, -1, 0 );
675 if (CLIENT_WaitReply( NULL, NULL, 0 ) != ERROR_SUCCESS)
677 K32OBJ_DecCount(&console->header);
678 SYSTEM_UNLOCK();
679 return FALSE;
682 req.output = 0;
683 req.access = GENERIC_READ | GENERIC_WRITE;
684 req.inherit = FALSE;
685 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
686 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
688 K32OBJ_DecCount(&console->header);
689 SYSTEM_UNLOCK();
690 return FALSE;
692 if ((hIn = HANDLE_Alloc(pdb,&console->header, req.access,
693 FALSE, reply.handle)) == INVALID_HANDLE_VALUE32)
695 K32OBJ_DecCount(&console->header);
696 SYSTEM_UNLOCK();
697 return FALSE;
700 req.output = 1;
701 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
702 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
704 CloseHandle(hIn);
705 K32OBJ_DecCount(&console->header);
706 SYSTEM_UNLOCK();
707 return FALSE;
709 if ((hOut = HANDLE_Alloc(pdb,&console->header, req.access,
710 FALSE, reply.handle)) == INVALID_HANDLE_VALUE32)
712 CloseHandle(hIn);
713 K32OBJ_DecCount(&console->header);
714 SYSTEM_UNLOCK();
715 return FALSE;
718 if (!DuplicateHandle( GetCurrentProcess(), hOut,
719 GetCurrentProcess(), &hErr,
720 0, TRUE, DUPLICATE_SAME_ACCESS ))
722 CloseHandle(hIn);
723 CloseHandle(hOut);
724 K32OBJ_DecCount(&console->header);
725 SYSTEM_UNLOCK();
726 return FALSE;
729 if (pdb->console) K32OBJ_DecCount( pdb->console );
730 pdb->console = (K32OBJ *)console;
731 K32OBJ_IncCount( pdb->console );
733 /* NT resets the STD_*_HANDLEs on console alloc */
734 SetStdHandle(STD_INPUT_HANDLE, hIn);
735 SetStdHandle(STD_OUTPUT_HANDLE, hOut);
736 SetStdHandle(STD_ERROR_HANDLE, hErr);
738 SetLastError(ERROR_SUCCESS);
739 SYSTEM_UNLOCK();
740 SetConsoleTitle32A("Wine Console");
741 return TRUE;
745 /******************************************************************************
746 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
748 * RETURNS
749 * Code page code
751 UINT32 WINAPI GetConsoleCP(VOID)
753 return GetACP();
757 /***********************************************************************
758 * GetConsoleOutputCP (KERNEL32.189)
760 UINT32 WINAPI GetConsoleOutputCP(VOID)
762 return GetConsoleCP();
765 /***********************************************************************
766 * GetConsoleMode (KERNEL32.188)
768 BOOL32 WINAPI GetConsoleMode(HANDLE32 hcon,LPDWORD mode)
770 struct get_console_mode_request req;
771 struct get_console_mode_reply reply;
773 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
774 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
775 return FALSE;
776 CLIENT_SendRequest( REQ_GET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
777 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
778 *mode = reply.mode;
779 return TRUE;
783 /******************************************************************************
784 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
786 * PARAMS
787 * hcon [I] Handle to console input or screen buffer
788 * mode [I] Input or output mode to set
790 * RETURNS
791 * Success: TRUE
792 * Failure: FALSE
794 BOOL32 WINAPI SetConsoleMode( HANDLE32 hcon, DWORD mode )
796 struct set_console_mode_request req;
798 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
799 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
800 return FALSE;
801 req.mode = mode;
802 CLIENT_SendRequest( REQ_SET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
803 return !CLIENT_WaitReply( NULL, NULL, 0 );
807 /***********************************************************************
808 * GetConsoleTitleA (KERNEL32.191)
810 DWORD WINAPI GetConsoleTitle32A(LPSTR title,DWORD size)
812 struct get_console_info_request req;
813 struct get_console_info_reply reply;
814 int len;
815 DWORD ret = 0;
816 HANDLE32 hcon;
818 if ((hcon = CreateFile32A( "CONOUT$", GENERIC_READ, 0, NULL,
819 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE32)
820 return 0;
821 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
822 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
824 CloseHandle( hcon );
825 return 0;
827 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
828 if (!CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), title, size ))
830 if (len > sizeof(reply)+size) title[size-1] = 0;
831 ret = strlen(title);
833 CloseHandle( hcon );
834 return ret;
838 /******************************************************************************
839 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
841 * PARAMS
842 * title [O] Address of buffer for title
843 * size [I] Size of buffer
845 * RETURNS
846 * Success: Length of string copied
847 * Failure: 0
849 DWORD WINAPI GetConsoleTitle32W( LPWSTR title, DWORD size )
851 char *tmp;
852 DWORD ret;
854 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, size ))) return 0;
855 ret = GetConsoleTitle32A( tmp, size );
856 lstrcpyAtoW( title, tmp );
857 HeapFree( GetProcessHeap(), 0, tmp );
858 return ret;
862 /***********************************************************************
863 * WriteConsoleA (KERNEL32.729)
865 BOOL32 WINAPI WriteConsole32A( HANDLE32 hConsoleOutput,
866 LPCVOID lpBuffer,
867 DWORD nNumberOfCharsToWrite,
868 LPDWORD lpNumberOfCharsWritten,
869 LPVOID lpReserved )
871 /* FIXME: should I check if this is a console handle? */
872 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
873 lpNumberOfCharsWritten, NULL);
877 #define CADD(c) \
878 if (bufused==curbufsize-1) \
879 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
880 buffer[bufused++]=c;
881 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
883 /***********************************************************************
884 * WriteConsoleOutputA (KERNEL32.732)
886 BOOL32 WINAPI WriteConsoleOutput32A( HANDLE32 hConsoleOutput,
887 LPCHAR_INFO lpBuffer,
888 COORD dwBufferSize,
889 COORD dwBufferCoord,
890 LPSMALL_RECT lpWriteRegion)
892 int i,j,off=0,lastattr=-1;
893 char sbuf[20],*buffer=NULL;
894 int bufused=0,curbufsize = 100;
895 DWORD res;
896 const int colormap[8] = {
897 0,4,2,6,
898 1,5,3,7,
900 CONSOLE_make_complex(hConsoleOutput);
901 buffer = HeapAlloc(GetProcessHeap(),0,100);;
902 curbufsize = 100;
904 TRACE(console,"wr: top = %d, bottom=%d, left=%d,right=%d\n",
905 lpWriteRegion->Top,
906 lpWriteRegion->Bottom,
907 lpWriteRegion->Left,
908 lpWriteRegion->Right
911 for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
912 sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
913 SADD(sbuf);
914 for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
915 if (lastattr!=lpBuffer[off].Attributes) {
916 lastattr = lpBuffer[off].Attributes;
917 sprintf(sbuf,"%c[0;%s3%d;4%dm",
919 (lastattr & FOREGROUND_INTENSITY)?"1;":"",
920 colormap[lastattr&7],
921 colormap[(lastattr&0x70)>>4]
923 /* FIXME: BACKGROUND_INTENSITY */
924 SADD(sbuf);
926 CADD(lpBuffer[off].Char.AsciiChar);
927 off++;
930 sprintf(sbuf,"%c[0m",27);SADD(sbuf);
931 WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
932 HeapFree(GetProcessHeap(),0,buffer);
933 return TRUE;
936 /***********************************************************************
937 * WriteConsoleW (KERNEL32.577)
939 BOOL32 WINAPI WriteConsole32W( HANDLE32 hConsoleOutput,
940 LPCVOID lpBuffer,
941 DWORD nNumberOfCharsToWrite,
942 LPDWORD lpNumberOfCharsWritten,
943 LPVOID lpReserved )
945 BOOL32 ret;
946 LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
948 lstrcpynWtoA( xstring, lpBuffer,nNumberOfCharsToWrite);
950 /* FIXME: should I check if this is a console handle? */
951 ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
952 lpNumberOfCharsWritten, NULL);
953 HeapFree( GetProcessHeap(), 0, xstring );
954 return ret;
958 /***********************************************************************
959 * ReadConsoleA (KERNEL32.419)
961 BOOL32 WINAPI ReadConsole32A( HANDLE32 hConsoleInput,
962 LPVOID lpBuffer,
963 DWORD nNumberOfCharsToRead,
964 LPDWORD lpNumberOfCharsRead,
965 LPVOID lpReserved )
967 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
968 int i,charsread = 0;
969 LPSTR xbuf = (LPSTR)lpBuffer;
971 if (!console) {
972 SetLastError(ERROR_INVALID_HANDLE);
973 FIXME(console,"(%d,...), no console handle!\n",hConsoleInput);
974 return FALSE;
976 TRACE(console,"(%d,%p,%ld,%p,%p)\n",
977 hConsoleInput,lpBuffer,nNumberOfCharsToRead,
978 lpNumberOfCharsRead,lpReserved
980 CONSOLE_get_input(hConsoleInput);
982 /* FIXME: should we read at least 1 char? The SDK does not say */
983 for (i=0;(i<console->nrofirs)&&(charsread<nNumberOfCharsToRead);i++) {
984 if (console->irs[i].EventType != KEY_EVENT)
985 continue;
986 if (!console->irs[i].Event.KeyEvent.bKeyDown)
987 continue;
988 *xbuf++ = console->irs[i].Event.KeyEvent.uChar.AsciiChar;
989 charsread++;
991 /* SDK says: Drains all other input events from queue. */
992 CONSOLE_drain_input(console,i);
993 if (lpNumberOfCharsRead)
994 *lpNumberOfCharsRead = charsread;
995 K32OBJ_DecCount(&console->header);
996 return TRUE;
999 /***********************************************************************
1000 * ReadConsoleW (KERNEL32.427)
1002 BOOL32 WINAPI ReadConsole32W( HANDLE32 hConsoleInput,
1003 LPVOID lpBuffer,
1004 DWORD nNumberOfCharsToRead,
1005 LPDWORD lpNumberOfCharsRead,
1006 LPVOID lpReserved )
1008 BOOL32 ret;
1009 LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
1011 ret = ReadConsole32A(
1012 hConsoleInput,
1013 buf,
1014 nNumberOfCharsToRead,
1015 lpNumberOfCharsRead,
1016 lpReserved
1018 if (ret)
1019 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
1020 HeapFree( GetProcessHeap(), 0, buf );
1021 return ret;
1025 /******************************************************************************
1026 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
1028 * PARAMS
1029 * hConsoleInput [I] Handle to console input buffer
1030 * lpBuffer [O] Address of buffer for read data
1031 * nLength [I] Number of records to read
1032 * lpNumberOfEventsRead [O] Address of number of records read
1034 * RETURNS
1035 * Success: TRUE
1036 * Failure: FALSE
1038 BOOL32 WINAPI ReadConsoleInput32A(HANDLE32 hConsoleInput,
1039 LPINPUT_RECORD lpBuffer,
1040 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1042 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
1044 TRACE(console, "(%d,%p,%ld,%p)\n",hConsoleInput, lpBuffer, nLength,
1045 lpNumberOfEventsRead);
1046 if (!console) {
1047 FIXME(console, "(%d,%p,%ld,%p), No console handle!\n",hConsoleInput,
1048 lpBuffer, nLength, lpNumberOfEventsRead);
1050 /* Indicate that nothing was read */
1051 *lpNumberOfEventsRead = 0;
1053 return FALSE;
1055 CONSOLE_get_input(hConsoleInput);
1056 /* SDK: return at least 1 input record */
1057 while (!console->nrofirs) {
1058 DWORD res;
1060 res=WaitForSingleObject(hConsoleInput,0);
1061 switch (res) {
1062 case STATUS_TIMEOUT: continue;
1063 case 0: break; /*ok*/
1064 case WAIT_FAILED: return 0;/*FIXME: SetLastError?*/
1065 default: break; /*hmm*/
1067 CONSOLE_get_input(hConsoleInput);
1070 if (nLength>console->nrofirs)
1071 nLength = console->nrofirs;
1072 memcpy(lpBuffer,console->irs,sizeof(INPUT_RECORD)*nLength);
1073 if (lpNumberOfEventsRead)
1074 *lpNumberOfEventsRead = nLength;
1075 CONSOLE_drain_input(console,nLength);
1076 K32OBJ_DecCount(&console->header);
1077 return TRUE;
1080 /***********************************************************************
1081 * SetConsoleTitle32A (KERNEL32.476)
1083 * Sets the console title.
1085 * We do not necessarily need to create a complex console for that,
1086 * but should remember the title and set it on creation of the latter.
1087 * (not fixed at this time).
1089 BOOL32 WINAPI SetConsoleTitle32A(LPCSTR title)
1091 #if 0
1092 PDB32 *pdb = PROCESS_Current();
1093 CONSOLE *console;
1094 DWORD written;
1095 char titleformat[]="\033]2;%s\a"; /*this should work for xterms*/
1096 LPSTR titlestring;
1097 BOOL32 ret=FALSE;
1099 TRACE(console,"(%s)\n",title);
1101 console = (CONSOLE *)pdb->console;
1102 if (!console)
1103 return FALSE;
1104 if(console->title) /* Free old title, if there is one */
1105 HeapFree( SystemHeap, 0, console->title );
1106 console->title = (LPSTR)HeapAlloc(SystemHeap, 0,strlen(title)+1);
1107 if(console->title) strcpy(console->title,title);
1108 titlestring = HeapAlloc(GetProcessHeap(), 0,strlen(title)+strlen(titleformat)+1);
1109 if (!titlestring) {
1110 K32OBJ_DecCount(&console->header);
1111 return FALSE;
1114 sprintf(titlestring,titleformat,title);
1115 #if 0
1116 /* only set title for complex console (own xterm) */
1117 if (console->pid != -1) {
1118 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),titlestring,strlen(titlestring),&written,NULL);
1119 if (written == strlen(titlestring))
1120 ret =TRUE;
1121 } else
1122 ret = TRUE;
1123 #endif
1124 HeapFree( GetProcessHeap(), 0, titlestring );
1125 K32OBJ_DecCount(&console->header);
1126 return ret;
1130 #endif
1132 struct set_console_info_request req;
1133 struct get_console_info_reply info;
1134 HANDLE32 hcon;
1135 DWORD written;
1137 if ((hcon = CreateFile32A( "CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
1138 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE32)
1139 return FALSE;
1140 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
1141 K32OBJ_CONSOLE, GENERIC_WRITE )) == -1)
1142 goto error;
1143 req.mask = SET_CONSOLE_INFO_TITLE;
1144 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 2, &req, sizeof(req),
1145 title, strlen(title)+1 );
1146 if (CLIENT_WaitReply( NULL, NULL, 0 )) goto error;
1147 if (CONSOLE_GetInfo( hcon, &info ) && info.pid)
1149 /* only set title for complex console (own xterm) */
1150 WriteFile( hcon, "\033]2;", 4, &written, NULL );
1151 WriteFile( hcon, title, strlen(title), &written, NULL );
1152 WriteFile( hcon, "\a", 1, &written, NULL );
1154 return TRUE;
1155 error:
1156 CloseHandle( hcon );
1157 return FALSE;
1161 /******************************************************************************
1162 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1164 * PARAMS
1165 * title [I] Address of new title
1167 * NOTES
1168 * This should not be calling the A version
1170 * RETURNS
1171 * Success: TRUE
1172 * Failure: FALSE
1174 BOOL32 WINAPI SetConsoleTitle32W( LPCWSTR title )
1176 BOOL32 ret;
1178 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1179 ret = SetConsoleTitle32A(titleA);
1180 HeapFree( GetProcessHeap(), 0, titleA );
1181 return ret;
1184 /***********************************************************************
1185 * ReadConsoleInput32W (KERNEL32.570)
1187 BOOL32 WINAPI ReadConsoleInput32W(HANDLE32 hConsoleInput,
1188 LPINPUT_RECORD lpBuffer,
1189 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1191 FIXME(console, "(%d,%p,%ld,%p): stub\n",hConsoleInput, lpBuffer, nLength,
1192 lpNumberOfEventsRead);
1193 return 0;
1196 /***********************************************************************
1197 * FlushConsoleInputBuffer (KERNEL32.132)
1199 BOOL32 WINAPI FlushConsoleInputBuffer(HANDLE32 hConsoleInput)
1201 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
1203 if (!console)
1204 return FALSE;
1205 CONSOLE_drain_input(console,console->nrofirs);
1206 K32OBJ_DecCount(&console->header);
1207 return TRUE;
1211 /******************************************************************************
1212 * SetConsoleCursorPosition [KERNEL32.627]
1213 * Sets the cursor position in console
1215 * PARAMS
1216 * hConsoleOutput [I] Handle of console screen buffer
1217 * dwCursorPosition [I] New cursor position coordinates
1219 * RETURNS STD
1221 BOOL32 WINAPI SetConsoleCursorPosition( HANDLE32 hcon, COORD pos )
1223 char xbuf[20];
1224 DWORD xlen;
1226 CONSOLE_make_complex(hcon);
1227 TRACE(console, "%d (%dx%d)\n", hcon, pos.x , pos.y );
1228 /* x are columns, y rows */
1229 sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1230 /* FIXME: store internal if we start using own console buffers */
1231 WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1232 return TRUE;
1235 /***********************************************************************
1236 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1238 BOOL32 WINAPI GetNumberOfConsoleInputEvents(HANDLE32 hcon,LPDWORD nrofevents)
1240 CONSOLE *console = CONSOLE_GetPtr( hcon );
1242 if (!console) {
1243 FIXME(console,"(%d,%p), no console handle!\n",hcon,nrofevents);
1244 return FALSE;
1246 CONSOLE_get_input(hcon);
1247 *nrofevents = console->nrofirs;
1248 K32OBJ_DecCount(&console->header);
1249 return TRUE;
1252 /***********************************************************************
1253 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1255 BOOL32 WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1257 FIXME(console,"(%p): stub\n", nrofbuttons);
1258 *nrofbuttons = 2;
1259 return TRUE;
1262 /***********************************************************************
1263 * PeekConsoleInputA (KERNEL32.550)
1265 * Gets 'cInRecords' first events (or less) from input queue.
1267 * Does not need a complex console.
1269 BOOL32 WINAPI PeekConsoleInput32A(HANDLE32 hConsoleInput,
1270 LPINPUT_RECORD pirBuffer,
1271 DWORD cInRecords,
1272 LPDWORD lpcRead)
1274 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
1276 if (!console) {
1277 FIXME(console,"(%d,%p,%ld,%p), No console handle passed!\n",hConsoleInput, pirBuffer, cInRecords, lpcRead);
1279 /* Indicate that nothing was read */
1280 *lpcRead = 0;
1282 return FALSE;
1284 TRACE(console,"(%d,%p,%ld,%p)\n",hConsoleInput, pirBuffer, cInRecords, lpcRead);
1285 CONSOLE_get_input(hConsoleInput);
1286 if (cInRecords>console->nrofirs)
1287 cInRecords = console->nrofirs;
1288 if (pirBuffer)
1289 memcpy(pirBuffer,console->irs,cInRecords*sizeof(INPUT_RECORD));
1290 if (lpcRead)
1291 *lpcRead = cInRecords;
1292 K32OBJ_DecCount(&console->header);
1293 return TRUE;
1296 /***********************************************************************
1297 * PeekConsoleInputW (KERNEL32.551)
1299 BOOL32 WINAPI PeekConsoleInput32W(HANDLE32 hConsoleInput,
1300 LPINPUT_RECORD pirBuffer,
1301 DWORD cInRecords,
1302 LPDWORD lpcRead)
1304 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1305 return PeekConsoleInput32A(hConsoleInput,pirBuffer,cInRecords,lpcRead);
1309 /******************************************************************************
1310 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1312 * PARAMS
1313 * hcon [I] Handle to console screen buffer
1314 * cinfo [O] Address of cursor information
1316 * RETURNS
1317 * Success: TRUE
1318 * Failure: FALSE
1320 BOOL32 WINAPI GetConsoleCursorInfo32( HANDLE32 hcon,
1321 LPCONSOLE_CURSOR_INFO cinfo )
1323 struct get_console_info_reply reply;
1325 if (!CONSOLE_GetInfo( hcon, &reply )) return FALSE;
1326 if (cinfo)
1328 cinfo->dwSize = reply.cursor_size;
1329 cinfo->bVisible = reply.cursor_visible;
1331 return TRUE;
1335 /******************************************************************************
1336 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1338 * RETURNS
1339 * Success: TRUE
1340 * Failure: FALSE
1342 BOOL32 WINAPI SetConsoleCursorInfo32(
1343 HANDLE32 hcon, /* [in] Handle to console screen buffer */
1344 LPCONSOLE_CURSOR_INFO cinfo) /* [in] Address of cursor information */
1346 struct set_console_info_request req;
1347 char buf[8];
1348 DWORD xlen;
1350 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
1351 K32OBJ_CONSOLE, GENERIC_WRITE )) == -1)
1352 return FALSE;
1353 CONSOLE_make_complex(hcon);
1354 sprintf(buf,"\033[?25%c",cinfo->bVisible?'h':'l');
1355 WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1357 req.cursor_size = cinfo->dwSize;
1358 req.cursor_visible = cinfo->bVisible;
1359 req.mask = SET_CONSOLE_INFO_CURSOR;
1360 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
1361 return !CLIENT_WaitReply( NULL, NULL, 0 );
1365 /******************************************************************************
1366 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1368 * RETURNS
1369 * Success: TRUE
1370 * Failure: FALSE
1372 BOOL32 WINAPI SetConsoleWindowInfo(
1373 HANDLE32 hcon, /* [in] Handle to console screen buffer */
1374 BOOL32 bAbsolute, /* [in] Coordinate type flag */
1375 LPSMALL_RECT window) /* [in] Address of new window rectangle */
1377 FIXME(console, "(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1378 return TRUE;
1382 /******************************************************************************
1383 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1385 * Sets the foreground and background color attributes of characters
1386 * written to the screen buffer.
1388 * RETURNS
1389 * Success: TRUE
1390 * Failure: FALSE
1392 BOOL32 WINAPI SetConsoleTextAttribute32(HANDLE32 hConsoleOutput,WORD wAttr)
1394 const int colormap[8] = {
1395 0,4,2,6,
1396 1,5,3,7,
1398 DWORD xlen;
1399 char buffer[20];
1401 TRACE(console,"(%d,%d)\n",hConsoleOutput,wAttr);
1402 sprintf(buffer,"%c[0;%s3%d;4%dm",
1404 (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1405 colormap[wAttr&7],
1406 colormap[(wAttr&0x70)>>4]
1408 WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1409 return TRUE;
1413 /******************************************************************************
1414 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1416 * PARAMS
1417 * hConsoleOutput [I] Handle to console screen buffer
1418 * dwSize [I] New size in character rows and cols
1420 * RETURNS
1421 * Success: TRUE
1422 * Failure: FALSE
1424 BOOL32 WINAPI SetConsoleScreenBufferSize( HANDLE32 hConsoleOutput,
1425 COORD dwSize )
1427 FIXME(console, "(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1428 return TRUE;
1432 /******************************************************************************
1433 * FillConsoleOutputCharacterA [KERNEL32.242]
1435 * PARAMS
1436 * hConsoleOutput [I] Handle to screen buffer
1437 * cCharacter [I] Character to write
1438 * nLength [I] Number of cells to write to
1439 * dwCoord [I] Coords of first cell
1440 * lpNumCharsWritten [O] Pointer to number of cells written
1442 * RETURNS
1443 * Success: TRUE
1444 * Failure: FALSE
1446 BOOL32 WINAPI FillConsoleOutputCharacterA(
1447 HANDLE32 hConsoleOutput,
1448 BYTE cCharacter,
1449 DWORD nLength,
1450 COORD dwCoord,
1451 LPDWORD lpNumCharsWritten)
1453 long count;
1454 DWORD xlen;
1456 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1457 for(count=0;count<nLength;count++)
1458 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1459 *lpNumCharsWritten = nLength;
1460 return TRUE;
1464 /******************************************************************************
1465 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1467 * PARAMS
1468 * hConsoleOutput [I] Handle to screen buffer
1469 * cCharacter [I] Character to write
1470 * nLength [I] Number of cells to write to
1471 * dwCoord [I] Coords of first cell
1472 * lpNumCharsWritten [O] Pointer to number of cells written
1474 * RETURNS
1475 * Success: TRUE
1476 * Failure: FALSE
1478 BOOL32 WINAPI FillConsoleOutputCharacterW(HANDLE32 hConsoleOutput,
1479 WCHAR cCharacter,
1480 DWORD nLength,
1481 COORD dwCoord,
1482 LPDWORD lpNumCharsWritten)
1484 long count;
1485 DWORD xlen;
1487 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1488 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1489 * first
1491 for(count=0;count<nLength;count++)
1492 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1493 *lpNumCharsWritten = nLength;
1494 return TRUE;
1498 /******************************************************************************
1499 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1501 * PARAMS
1502 * hConsoleOutput [I] Handle to screen buffer
1503 * wAttribute [I] Color attribute to write
1504 * nLength [I] Number of cells to write to
1505 * dwCoord [I] Coords of first cell
1506 * lpNumAttrsWritten [O] Pointer to number of cells written
1508 * RETURNS
1509 * Success: TRUE
1510 * Failure: FALSE
1512 BOOL32 WINAPI FillConsoleOutputAttribute( HANDLE32 hConsoleOutput,
1513 WORD wAttribute, DWORD nLength, COORD dwCoord,
1514 LPDWORD lpNumAttrsWritten)
1516 FIXME(console, "(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1517 wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1518 *lpNumAttrsWritten = nLength;
1519 return TRUE;
1522 /******************************************************************************
1523 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1525 * BUGS
1526 * Unimplemented
1528 BOOL32 WINAPI ReadConsoleOutputCharacter32A(HANDLE32 hConsoleOutput,
1529 LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1531 FIXME(console, "(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1532 dword,coord.x,coord.y,lpdword);
1533 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1534 return FALSE;
1538 /******************************************************************************
1539 * ScrollConsoleScreenBuffer [KERNEL32.612]
1541 * BUGS
1542 * Unimplemented
1544 BOOL32 WINAPI ScrollConsoleScreenBuffer( HANDLE32 hConsoleOutput,
1545 LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1546 COORD dwDestOrigin, LPCHAR_INFO lpFill)
1548 FIXME(console, "(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1549 lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1550 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1551 return FALSE;