Modify NE_FindTypeSection and NE_FindResourceFromType so as to be
[wine.git] / win32 / console.c
blob085724d82cd2f68857b6e7a370c949fbecb5acd2
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 if (!res) /* res 0 but readable means EOF? Hmm. */
289 break;
290 buf = HeapReAlloc(GetProcessHeap(),0,buf,len+1);
291 buf[len++]=inchar;
293 CONSOLE_string_to_IR(handle,buf,len);
294 HeapFree(GetProcessHeap(),0,buf);
297 /****************************************************************************
298 * CONSOLE_drain_input (internal)
300 * Drains 'n' console input events from the queue.
302 static void
303 CONSOLE_drain_input(CONSOLE *console,int n) {
304 assert(n<=console->nrofirs);
305 if (n) {
306 console->nrofirs-=n;
307 memcpy( &console->irs[0],
308 &console->irs[n],
309 console->nrofirs*sizeof(INPUT_RECORD)
311 console->irs = HeapReAlloc(
312 GetProcessHeap(),
314 console->irs,
315 console->nrofirs*sizeof(INPUT_RECORD)
321 /******************************************************************************
322 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
324 * PARAMS
325 * func [I] Address of handler function
326 * add [I] Handler to add or remove
328 * RETURNS
329 * Success: TRUE
330 * Failure: FALSE
332 * CHANGED
333 * James Sutherland (JamesSutherland@gmx.de)
334 * Added global variables console_ignore_ctrl_c and handlers[]
335 * Does not yet do any error checking, or set LastError if failed.
336 * This doesn't yet matter, since these handlers are not yet called...!
338 static unsigned int console_ignore_ctrl_c = 0;
339 static HANDLER_ROUTINE *handlers[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
340 BOOL32 WINAPI SetConsoleCtrlHandler( HANDLER_ROUTINE *func, BOOL32 add )
342 unsigned int alloc_loop = sizeof(handlers)/sizeof(HANDLER_ROUTINE *);
343 unsigned int done = 0;
344 FIXME(console, "(%p,%i) - no error checking or testing yet\n", func, add);
345 if (!func)
347 console_ignore_ctrl_c = add;
348 return TRUE;
350 if (add)
352 for (;alloc_loop--;)
353 if (!handlers[alloc_loop] && !done)
355 handlers[alloc_loop] = func;
356 done++;
358 if (!done)
359 FIXME(console, "Out of space on CtrlHandler table\n");
360 return(done);
362 else
364 for (;alloc_loop--;)
365 if (handlers[alloc_loop] == func && !done)
367 handlers[alloc_loop] = 0;
368 done++;
370 if (!done)
371 WARN(console, "Attempt to remove non-installed CtrlHandler %p\n",
372 func);
373 return (done);
375 return (done);
379 /******************************************************************************
380 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
382 * PARAMS
383 * dwCtrlEvent [I] Type of event
384 * dwProcessGroupID [I] Process group ID to send event to
386 * NOTES
387 * Doesn't yet work...!
389 * RETURNS
390 * Success: True
391 * Failure: False (and *should* [but doesn't] set LastError)
393 BOOL32 WINAPI GenerateConsoleCtrlEvent( DWORD dwCtrlEvent,
394 DWORD dwProcessGroupID )
396 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
398 ERR( console, "invalid event %d for PGID %ld\n",
399 (unsigned short)dwCtrlEvent, dwProcessGroupID );
400 return FALSE;
402 if (dwProcessGroupID == GetCurrentProcessId() )
404 FIXME( console, "Attempt to send event %d to self - stub\n",
405 (unsigned short)dwCtrlEvent );
406 return FALSE;
408 FIXME( console,"event %d to external PGID %ld - not implemented yet\n",
409 (unsigned short)dwCtrlEvent, dwProcessGroupID );
410 return FALSE;
414 /******************************************************************************
415 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
417 * PARAMS
418 * dwDesiredAccess [I] Access flag
419 * dwShareMode [I] Buffer share mode
420 * sa [I] Security attributes
421 * dwFlags [I] Type of buffer to create
422 * lpScreenBufferData [I] Reserved
424 * NOTES
425 * Should call SetLastError
427 * RETURNS
428 * Success: Handle to new console screen buffer
429 * Failure: INVALID_HANDLE_VALUE
431 HANDLE32 WINAPI CreateConsoleScreenBuffer( DWORD dwDesiredAccess,
432 DWORD dwShareMode, LPSECURITY_ATTRIBUTES sa,
433 DWORD dwFlags, LPVOID lpScreenBufferData )
435 FIXME(console, "(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess,
436 dwShareMode, sa, dwFlags, lpScreenBufferData);
437 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
438 return INVALID_HANDLE_VALUE32;
442 /***********************************************************************
443 * GetConsoleScreenBufferInfo (KERNEL32.190)
445 BOOL32 WINAPI GetConsoleScreenBufferInfo( HANDLE32 hConsoleOutput,
446 LPCONSOLE_SCREEN_BUFFER_INFO csbi )
448 csbi->dwSize.x = 80;
449 csbi->dwSize.y = 24;
450 csbi->dwCursorPosition.x = 0;
451 csbi->dwCursorPosition.y = 0;
452 csbi->wAttributes = 0;
453 csbi->srWindow.Left = 0;
454 csbi->srWindow.Right = 79;
455 csbi->srWindow.Top = 0;
456 csbi->srWindow.Bottom = 23;
457 csbi->dwMaximumWindowSize.x = 80;
458 csbi->dwMaximumWindowSize.y = 24;
459 return TRUE;
463 /******************************************************************************
464 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
466 * RETURNS
467 * Success: TRUE
468 * Failure: FALSE
470 BOOL32 WINAPI SetConsoleActiveScreenBuffer(
471 HANDLE32 hConsoleOutput) /* [in] Handle to console screen buffer */
473 FIXME(console, "(%x): stub\n", hConsoleOutput);
474 return FALSE;
478 /***********************************************************************
479 * GetLargestConsoleWindowSize (KERNEL32.226)
481 DWORD WINAPI GetLargestConsoleWindowSize( HANDLE32 hConsoleOutput )
483 return (DWORD)MAKELONG(80,24);
486 /***********************************************************************
487 * FreeConsole (KERNEL32.267)
489 BOOL32 WINAPI FreeConsole(VOID)
492 PDB32 *pdb = PROCESS_Current();
493 CONSOLE *console;
495 SYSTEM_LOCK();
497 console = (CONSOLE *)pdb->console;
499 if (console == NULL) {
500 SetLastError(ERROR_INVALID_PARAMETER);
501 return FALSE;
504 CLIENT_SendRequest( REQ_FREE_CONSOLE, -1, 0 );
505 if (CLIENT_WaitReply( NULL, NULL, 0 ) != ERROR_SUCCESS)
507 K32OBJ_DecCount(&console->header);
508 SYSTEM_UNLOCK();
509 return FALSE;
512 HANDLE_CloseAll( pdb, &console->header );
513 K32OBJ_DecCount( &console->header );
514 pdb->console = NULL;
515 SYSTEM_UNLOCK();
516 return TRUE;
520 /*************************************************************************
521 * CONSOLE_OpenHandle
523 * Open a handle to the current process console.
525 HANDLE32 CONSOLE_OpenHandle( BOOL32 output, DWORD access, LPSECURITY_ATTRIBUTES sa )
527 struct open_console_request req;
528 struct open_console_reply reply;
529 CONSOLE *console;
530 HANDLE32 handle;
532 req.output = output;
533 req.access = access;
534 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
535 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
536 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
537 if (reply.handle == -1) return INVALID_HANDLE_VALUE32;
539 SYSTEM_LOCK();
540 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
542 SYSTEM_UNLOCK();
543 return FALSE;
545 console->header.type = K32OBJ_CONSOLE;
546 console->header.refcount = 1;
547 console->nrofirs = 0;
548 console->irs = HeapAlloc(GetProcessHeap(),0,1);;
549 handle = HANDLE_Alloc( PROCESS_Current(), &console->header, req.access,
550 req.inherit, reply.handle );
551 SYSTEM_UNLOCK();
552 K32OBJ_DecCount(&console->header);
553 return handle;
557 /*************************************************************************
558 * CONSOLE_make_complex [internal]
560 * Turns a CONSOLE kernel object into a complex one.
561 * (switches from output/input using the terminal where WINE was started to
562 * its own xterm).
564 * This makes simple commandline tools pipeable, while complex commandline
565 * tools work without getting messed up by debugoutput.
567 * All other functions should work indedependend from this call.
569 * To test for complex console: pid == 0 -> simple, otherwise complex.
571 static BOOL32 CONSOLE_make_complex(HANDLE32 handle)
573 struct set_console_fd_request req;
574 struct get_console_info_reply info;
575 struct termios term;
576 char buf[256];
577 char c = '\0';
578 int status = 0;
579 int i,xpid,master,slave;
580 DWORD xlen;
582 if (!CONSOLE_GetInfo( handle, &info )) return FALSE;
583 if (info.pid) return TRUE; /* already complex */
585 MSG("Console: Making console complex (creating an xterm)...\n");
587 if (tcgetattr(0, &term) < 0) {
588 /* ignore failure, or we can't run from a script */
590 term.c_lflag = ~(ECHO|ICANON);
592 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
593 K32OBJ_CONSOLE, 0 )) == -1)
594 return FALSE;
596 if (wine_openpty(&master, &slave, NULL, &term, NULL) < 0)
597 return FALSE;
599 if ((xpid=fork()) == 0) {
600 tcsetattr(slave, TCSADRAIN, &term);
601 sprintf(buf, "-Sxx%d", master);
602 /* "-fn vga" for VGA font. Harmless if vga is not present:
603 * xterm: unable to open font "vga", trying "fixed"....
605 execlp("xterm", "xterm", buf, "-fn","vga",NULL);
606 ERR(console, "error creating AllocConsole xterm\n");
607 exit(1);
610 req.pid = xpid;
611 CLIENT_SendRequest( REQ_SET_CONSOLE_FD, dup(slave), 1, &req, sizeof(req) );
612 CLIENT_WaitReply( NULL, NULL, 0 );
614 /* most xterms like to print their window ID when used with -S;
615 * read it and continue before the user has a chance...
617 for (i=0; c!='\n'; (status=read(slave, &c, 1)), i++) {
618 if (status == -1 && c == '\0') {
619 /* wait for xterm to be created */
620 usleep(100);
622 if (i > 10000) {
623 ERR(console, "can't read xterm WID\n");
624 kill(xpid, SIGKILL);
625 return FALSE;
628 /* enable mouseclicks */
629 sprintf(buf,"%c[?1001s%c[?1000h",27,27);
630 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
632 if (GetConsoleTitle32A( buf, sizeof(buf) ))
634 WriteFile(handle,"\033]2;",4,&xlen,NULL);
635 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
636 WriteFile(handle,"\a",1,&xlen,NULL);
638 return TRUE;
643 /***********************************************************************
644 * AllocConsole (KERNEL32.103)
646 * creates an xterm with a pty to our program
648 BOOL32 WINAPI AllocConsole(VOID)
650 struct open_console_request req;
651 struct open_console_reply reply;
652 PDB32 *pdb = PROCESS_Current();
653 CONSOLE *console;
654 HANDLE32 hIn, hOut, hErr;
656 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
658 console = (CONSOLE *)pdb->console;
660 /* don't create a console if we already have one */
661 if (console != NULL) {
662 SetLastError(ERROR_ACCESS_DENIED);
663 SYSTEM_UNLOCK();
664 return FALSE;
667 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
669 SYSTEM_UNLOCK();
670 return FALSE;
673 console->header.type = K32OBJ_CONSOLE;
674 console->header.refcount = 1;
675 console->nrofirs = 0;
676 console->irs = HeapAlloc(GetProcessHeap(),0,1);;
678 CLIENT_SendRequest( REQ_ALLOC_CONSOLE, -1, 0 );
679 if (CLIENT_WaitReply( NULL, NULL, 0 ) != ERROR_SUCCESS)
681 K32OBJ_DecCount(&console->header);
682 SYSTEM_UNLOCK();
683 return FALSE;
686 req.output = 0;
687 req.access = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
688 req.inherit = FALSE;
689 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
690 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
692 K32OBJ_DecCount(&console->header);
693 SYSTEM_UNLOCK();
694 return FALSE;
696 if ((hIn = HANDLE_Alloc(pdb,&console->header, req.access,
697 FALSE, reply.handle)) == INVALID_HANDLE_VALUE32)
699 K32OBJ_DecCount(&console->header);
700 SYSTEM_UNLOCK();
701 return FALSE;
704 req.output = 1;
705 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
706 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
708 CloseHandle(hIn);
709 K32OBJ_DecCount(&console->header);
710 SYSTEM_UNLOCK();
711 return FALSE;
713 if ((hOut = HANDLE_Alloc(pdb,&console->header, req.access,
714 FALSE, reply.handle)) == INVALID_HANDLE_VALUE32)
716 CloseHandle(hIn);
717 K32OBJ_DecCount(&console->header);
718 SYSTEM_UNLOCK();
719 return FALSE;
722 if (!DuplicateHandle( GetCurrentProcess(), hOut,
723 GetCurrentProcess(), &hErr,
724 0, TRUE, DUPLICATE_SAME_ACCESS ))
726 CloseHandle(hIn);
727 CloseHandle(hOut);
728 K32OBJ_DecCount(&console->header);
729 SYSTEM_UNLOCK();
730 return FALSE;
733 if (pdb->console) K32OBJ_DecCount( pdb->console );
734 pdb->console = (K32OBJ *)console;
735 K32OBJ_IncCount( pdb->console );
737 /* NT resets the STD_*_HANDLEs on console alloc */
738 SetStdHandle(STD_INPUT_HANDLE, hIn);
739 SetStdHandle(STD_OUTPUT_HANDLE, hOut);
740 SetStdHandle(STD_ERROR_HANDLE, hErr);
742 SetLastError(ERROR_SUCCESS);
743 SYSTEM_UNLOCK();
744 SetConsoleTitle32A("Wine Console");
745 return TRUE;
749 /******************************************************************************
750 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
752 * RETURNS
753 * Code page code
755 UINT32 WINAPI GetConsoleCP(VOID)
757 return GetACP();
761 /***********************************************************************
762 * GetConsoleOutputCP (KERNEL32.189)
764 UINT32 WINAPI GetConsoleOutputCP(VOID)
766 return GetConsoleCP();
769 /***********************************************************************
770 * GetConsoleMode (KERNEL32.188)
772 BOOL32 WINAPI GetConsoleMode(HANDLE32 hcon,LPDWORD mode)
774 struct get_console_mode_request req;
775 struct get_console_mode_reply reply;
777 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
778 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
779 return FALSE;
780 CLIENT_SendRequest( REQ_GET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
781 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
782 *mode = reply.mode;
783 return TRUE;
787 /******************************************************************************
788 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
790 * PARAMS
791 * hcon [I] Handle to console input or screen buffer
792 * mode [I] Input or output mode to set
794 * RETURNS
795 * Success: TRUE
796 * Failure: FALSE
798 BOOL32 WINAPI SetConsoleMode( HANDLE32 hcon, DWORD mode )
800 struct set_console_mode_request req;
802 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
803 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
804 return FALSE;
805 req.mode = mode;
806 CLIENT_SendRequest( REQ_SET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
807 return !CLIENT_WaitReply( NULL, NULL, 0 );
811 /***********************************************************************
812 * GetConsoleTitleA (KERNEL32.191)
814 DWORD WINAPI GetConsoleTitle32A(LPSTR title,DWORD size)
816 struct get_console_info_request req;
817 struct get_console_info_reply reply;
818 int len;
819 DWORD ret = 0;
820 HANDLE32 hcon;
822 if ((hcon = CreateFile32A( "CONOUT$", GENERIC_READ, 0, NULL,
823 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE32)
824 return 0;
825 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
826 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
828 CloseHandle( hcon );
829 return 0;
831 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
832 if (!CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), title, size ))
834 if (len > sizeof(reply)+size) title[size-1] = 0;
835 ret = strlen(title);
837 CloseHandle( hcon );
838 return ret;
842 /******************************************************************************
843 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
845 * PARAMS
846 * title [O] Address of buffer for title
847 * size [I] Size of buffer
849 * RETURNS
850 * Success: Length of string copied
851 * Failure: 0
853 DWORD WINAPI GetConsoleTitle32W( LPWSTR title, DWORD size )
855 char *tmp;
856 DWORD ret;
858 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, size ))) return 0;
859 ret = GetConsoleTitle32A( tmp, size );
860 lstrcpyAtoW( title, tmp );
861 HeapFree( GetProcessHeap(), 0, tmp );
862 return ret;
866 /***********************************************************************
867 * WriteConsoleA (KERNEL32.729)
869 BOOL32 WINAPI WriteConsole32A( HANDLE32 hConsoleOutput,
870 LPCVOID lpBuffer,
871 DWORD nNumberOfCharsToWrite,
872 LPDWORD lpNumberOfCharsWritten,
873 LPVOID lpReserved )
875 /* FIXME: should I check if this is a console handle? */
876 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
877 lpNumberOfCharsWritten, NULL);
881 #define CADD(c) \
882 if (bufused==curbufsize-1) \
883 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
884 buffer[bufused++]=c;
885 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
887 /***********************************************************************
888 * WriteConsoleOutputA (KERNEL32.732)
890 BOOL32 WINAPI WriteConsoleOutput32A( HANDLE32 hConsoleOutput,
891 LPCHAR_INFO lpBuffer,
892 COORD dwBufferSize,
893 COORD dwBufferCoord,
894 LPSMALL_RECT lpWriteRegion)
896 int i,j,off=0,lastattr=-1;
897 char sbuf[20],*buffer=NULL;
898 int bufused=0,curbufsize = 100;
899 DWORD res;
900 const int colormap[8] = {
901 0,4,2,6,
902 1,5,3,7,
904 CONSOLE_make_complex(hConsoleOutput);
905 buffer = HeapAlloc(GetProcessHeap(),0,100);;
906 curbufsize = 100;
908 TRACE(console,"wr: top = %d, bottom=%d, left=%d,right=%d\n",
909 lpWriteRegion->Top,
910 lpWriteRegion->Bottom,
911 lpWriteRegion->Left,
912 lpWriteRegion->Right
915 for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
916 sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
917 SADD(sbuf);
918 for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
919 if (lastattr!=lpBuffer[off].Attributes) {
920 lastattr = lpBuffer[off].Attributes;
921 sprintf(sbuf,"%c[0;%s3%d;4%dm",
923 (lastattr & FOREGROUND_INTENSITY)?"1;":"",
924 colormap[lastattr&7],
925 colormap[(lastattr&0x70)>>4]
927 /* FIXME: BACKGROUND_INTENSITY */
928 SADD(sbuf);
930 CADD(lpBuffer[off].Char.AsciiChar);
931 off++;
934 sprintf(sbuf,"%c[0m",27);SADD(sbuf);
935 WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
936 HeapFree(GetProcessHeap(),0,buffer);
937 return TRUE;
940 /***********************************************************************
941 * WriteConsoleW (KERNEL32.577)
943 BOOL32 WINAPI WriteConsole32W( HANDLE32 hConsoleOutput,
944 LPCVOID lpBuffer,
945 DWORD nNumberOfCharsToWrite,
946 LPDWORD lpNumberOfCharsWritten,
947 LPVOID lpReserved )
949 BOOL32 ret;
950 LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
952 lstrcpynWtoA( xstring, lpBuffer,nNumberOfCharsToWrite);
954 /* FIXME: should I check if this is a console handle? */
955 ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
956 lpNumberOfCharsWritten, NULL);
957 HeapFree( GetProcessHeap(), 0, xstring );
958 return ret;
962 /***********************************************************************
963 * ReadConsoleA (KERNEL32.419)
965 BOOL32 WINAPI ReadConsole32A( HANDLE32 hConsoleInput,
966 LPVOID lpBuffer,
967 DWORD nNumberOfCharsToRead,
968 LPDWORD lpNumberOfCharsRead,
969 LPVOID lpReserved )
971 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
972 int i,charsread = 0;
973 LPSTR xbuf = (LPSTR)lpBuffer;
975 if (!console) {
976 SetLastError(ERROR_INVALID_HANDLE);
977 FIXME(console,"(%d,...), no console handle!\n",hConsoleInput);
978 return FALSE;
980 TRACE(console,"(%d,%p,%ld,%p,%p)\n",
981 hConsoleInput,lpBuffer,nNumberOfCharsToRead,
982 lpNumberOfCharsRead,lpReserved
984 CONSOLE_get_input(hConsoleInput);
986 /* FIXME: should we read at least 1 char? The SDK does not say */
987 for (i=0;(i<console->nrofirs)&&(charsread<nNumberOfCharsToRead);i++) {
988 if (console->irs[i].EventType != KEY_EVENT)
989 continue;
990 if (!console->irs[i].Event.KeyEvent.bKeyDown)
991 continue;
992 *xbuf++ = console->irs[i].Event.KeyEvent.uChar.AsciiChar;
993 charsread++;
995 /* SDK says: Drains all other input events from queue. */
996 CONSOLE_drain_input(console,i);
997 if (lpNumberOfCharsRead)
998 *lpNumberOfCharsRead = charsread;
999 K32OBJ_DecCount(&console->header);
1000 return TRUE;
1003 /***********************************************************************
1004 * ReadConsoleW (KERNEL32.427)
1006 BOOL32 WINAPI ReadConsole32W( HANDLE32 hConsoleInput,
1007 LPVOID lpBuffer,
1008 DWORD nNumberOfCharsToRead,
1009 LPDWORD lpNumberOfCharsRead,
1010 LPVOID lpReserved )
1012 BOOL32 ret;
1013 LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
1015 ret = ReadConsole32A(
1016 hConsoleInput,
1017 buf,
1018 nNumberOfCharsToRead,
1019 lpNumberOfCharsRead,
1020 lpReserved
1022 if (ret)
1023 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
1024 HeapFree( GetProcessHeap(), 0, buf );
1025 return ret;
1029 /******************************************************************************
1030 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
1032 * PARAMS
1033 * hConsoleInput [I] Handle to console input buffer
1034 * lpBuffer [O] Address of buffer for read data
1035 * nLength [I] Number of records to read
1036 * lpNumberOfEventsRead [O] Address of number of records read
1038 * RETURNS
1039 * Success: TRUE
1040 * Failure: FALSE
1042 BOOL32 WINAPI ReadConsoleInput32A(HANDLE32 hConsoleInput,
1043 LPINPUT_RECORD lpBuffer,
1044 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1046 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
1048 TRACE(console, "(%d,%p,%ld,%p)\n",hConsoleInput, lpBuffer, nLength,
1049 lpNumberOfEventsRead);
1050 if (!console) {
1051 FIXME(console, "(%d,%p,%ld,%p), No console handle!\n",hConsoleInput,
1052 lpBuffer, nLength, lpNumberOfEventsRead);
1054 /* Indicate that nothing was read */
1055 *lpNumberOfEventsRead = 0;
1057 return FALSE;
1059 CONSOLE_get_input(hConsoleInput);
1060 /* SDK: return at least 1 input record */
1061 while (!console->nrofirs) {
1062 DWORD res;
1064 res=WaitForSingleObject(hConsoleInput,0);
1065 switch (res) {
1066 case STATUS_TIMEOUT: continue;
1067 case 0: break; /*ok*/
1068 case WAIT_FAILED: return 0;/*FIXME: SetLastError?*/
1069 default: break; /*hmm*/
1071 CONSOLE_get_input(hConsoleInput);
1074 if (nLength>console->nrofirs)
1075 nLength = console->nrofirs;
1076 memcpy(lpBuffer,console->irs,sizeof(INPUT_RECORD)*nLength);
1077 if (lpNumberOfEventsRead)
1078 *lpNumberOfEventsRead = nLength;
1079 CONSOLE_drain_input(console,nLength);
1080 K32OBJ_DecCount(&console->header);
1081 return TRUE;
1084 /***********************************************************************
1085 * SetConsoleTitle32A (KERNEL32.476)
1087 * Sets the console title.
1089 * We do not necessarily need to create a complex console for that,
1090 * but should remember the title and set it on creation of the latter.
1091 * (not fixed at this time).
1093 BOOL32 WINAPI SetConsoleTitle32A(LPCSTR title)
1095 #if 0
1096 PDB32 *pdb = PROCESS_Current();
1097 CONSOLE *console;
1098 DWORD written;
1099 char titleformat[]="\033]2;%s\a"; /*this should work for xterms*/
1100 LPSTR titlestring;
1101 BOOL32 ret=FALSE;
1103 TRACE(console,"(%s)\n",title);
1105 console = (CONSOLE *)pdb->console;
1106 if (!console)
1107 return FALSE;
1108 if(console->title) /* Free old title, if there is one */
1109 HeapFree( SystemHeap, 0, console->title );
1110 console->title = (LPSTR)HeapAlloc(SystemHeap, 0,strlen(title)+1);
1111 if(console->title) strcpy(console->title,title);
1112 titlestring = HeapAlloc(GetProcessHeap(), 0,strlen(title)+strlen(titleformat)+1);
1113 if (!titlestring) {
1114 K32OBJ_DecCount(&console->header);
1115 return FALSE;
1118 sprintf(titlestring,titleformat,title);
1119 #if 0
1120 /* only set title for complex console (own xterm) */
1121 if (console->pid != -1) {
1122 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),titlestring,strlen(titlestring),&written,NULL);
1123 if (written == strlen(titlestring))
1124 ret =TRUE;
1125 } else
1126 ret = TRUE;
1127 #endif
1128 HeapFree( GetProcessHeap(), 0, titlestring );
1129 K32OBJ_DecCount(&console->header);
1130 return ret;
1134 #endif
1136 struct set_console_info_request req;
1137 struct get_console_info_reply info;
1138 HANDLE32 hcon;
1139 DWORD written;
1141 if ((hcon = CreateFile32A( "CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
1142 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE32)
1143 return FALSE;
1144 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
1145 K32OBJ_CONSOLE, GENERIC_WRITE )) == -1)
1146 goto error;
1147 req.mask = SET_CONSOLE_INFO_TITLE;
1148 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 2, &req, sizeof(req),
1149 title, strlen(title)+1 );
1150 if (CLIENT_WaitReply( NULL, NULL, 0 )) goto error;
1151 if (CONSOLE_GetInfo( hcon, &info ) && info.pid)
1153 /* only set title for complex console (own xterm) */
1154 WriteFile( hcon, "\033]2;", 4, &written, NULL );
1155 WriteFile( hcon, title, strlen(title), &written, NULL );
1156 WriteFile( hcon, "\a", 1, &written, NULL );
1158 return TRUE;
1159 error:
1160 CloseHandle( hcon );
1161 return FALSE;
1165 /******************************************************************************
1166 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1168 * PARAMS
1169 * title [I] Address of new title
1171 * NOTES
1172 * This should not be calling the A version
1174 * RETURNS
1175 * Success: TRUE
1176 * Failure: FALSE
1178 BOOL32 WINAPI SetConsoleTitle32W( LPCWSTR title )
1180 BOOL32 ret;
1182 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1183 ret = SetConsoleTitle32A(titleA);
1184 HeapFree( GetProcessHeap(), 0, titleA );
1185 return ret;
1188 /***********************************************************************
1189 * ReadConsoleInput32W (KERNEL32.570)
1191 BOOL32 WINAPI ReadConsoleInput32W(HANDLE32 hConsoleInput,
1192 LPINPUT_RECORD lpBuffer,
1193 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1195 FIXME(console, "(%d,%p,%ld,%p): stub\n",hConsoleInput, lpBuffer, nLength,
1196 lpNumberOfEventsRead);
1197 return 0;
1200 /***********************************************************************
1201 * FlushConsoleInputBuffer (KERNEL32.132)
1203 BOOL32 WINAPI FlushConsoleInputBuffer(HANDLE32 hConsoleInput)
1205 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
1207 if (!console)
1208 return FALSE;
1209 CONSOLE_drain_input(console,console->nrofirs);
1210 K32OBJ_DecCount(&console->header);
1211 return TRUE;
1215 /******************************************************************************
1216 * SetConsoleCursorPosition [KERNEL32.627]
1217 * Sets the cursor position in console
1219 * PARAMS
1220 * hConsoleOutput [I] Handle of console screen buffer
1221 * dwCursorPosition [I] New cursor position coordinates
1223 * RETURNS STD
1225 BOOL32 WINAPI SetConsoleCursorPosition( HANDLE32 hcon, COORD pos )
1227 char xbuf[20];
1228 DWORD xlen;
1230 /* make console complex only if we change lines, not just in the line */
1231 if (pos.y)
1232 CONSOLE_make_complex(hcon);
1234 TRACE(console, "%d (%dx%d)\n", hcon, pos.x , pos.y );
1235 /* x are columns, y rows */
1236 if (pos.y)
1237 /* full screen cursor absolute positioning */
1238 sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1239 else
1240 /* relative cursor positioning in line (\r to go to 0) */
1241 sprintf(xbuf,"\r%c[%dC", 0x1B, pos.x);
1242 /* FIXME: store internal if we start using own console buffers */
1243 WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1244 return TRUE;
1247 /***********************************************************************
1248 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1250 BOOL32 WINAPI GetNumberOfConsoleInputEvents(HANDLE32 hcon,LPDWORD nrofevents)
1252 CONSOLE *console = CONSOLE_GetPtr( hcon );
1254 if (!console) {
1255 FIXME(console,"(%d,%p), no console handle!\n",hcon,nrofevents);
1256 return FALSE;
1258 CONSOLE_get_input(hcon);
1259 *nrofevents = console->nrofirs;
1260 K32OBJ_DecCount(&console->header);
1261 return TRUE;
1264 /***********************************************************************
1265 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1267 BOOL32 WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1269 FIXME(console,"(%p): stub\n", nrofbuttons);
1270 *nrofbuttons = 2;
1271 return TRUE;
1274 /***********************************************************************
1275 * PeekConsoleInputA (KERNEL32.550)
1277 * Gets 'cInRecords' first events (or less) from input queue.
1279 * Does not need a complex console.
1281 BOOL32 WINAPI PeekConsoleInput32A(HANDLE32 hConsoleInput,
1282 LPINPUT_RECORD pirBuffer,
1283 DWORD cInRecords,
1284 LPDWORD lpcRead)
1286 CONSOLE *console = CONSOLE_GetPtr( hConsoleInput );
1288 if (!console) {
1289 FIXME(console,"(%d,%p,%ld,%p), No console handle passed!\n",hConsoleInput, pirBuffer, cInRecords, lpcRead);
1291 /* Indicate that nothing was read */
1292 *lpcRead = 0;
1294 return FALSE;
1296 TRACE(console,"(%d,%p,%ld,%p)\n",hConsoleInput, pirBuffer, cInRecords, lpcRead);
1297 CONSOLE_get_input(hConsoleInput);
1298 if (cInRecords>console->nrofirs)
1299 cInRecords = console->nrofirs;
1300 if (pirBuffer)
1301 memcpy(pirBuffer,console->irs,cInRecords*sizeof(INPUT_RECORD));
1302 if (lpcRead)
1303 *lpcRead = cInRecords;
1304 K32OBJ_DecCount(&console->header);
1305 return TRUE;
1308 /***********************************************************************
1309 * PeekConsoleInputW (KERNEL32.551)
1311 BOOL32 WINAPI PeekConsoleInput32W(HANDLE32 hConsoleInput,
1312 LPINPUT_RECORD pirBuffer,
1313 DWORD cInRecords,
1314 LPDWORD lpcRead)
1316 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1317 return PeekConsoleInput32A(hConsoleInput,pirBuffer,cInRecords,lpcRead);
1321 /******************************************************************************
1322 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1324 * PARAMS
1325 * hcon [I] Handle to console screen buffer
1326 * cinfo [O] Address of cursor information
1328 * RETURNS
1329 * Success: TRUE
1330 * Failure: FALSE
1332 BOOL32 WINAPI GetConsoleCursorInfo32( HANDLE32 hcon,
1333 LPCONSOLE_CURSOR_INFO cinfo )
1335 struct get_console_info_reply reply;
1337 if (!CONSOLE_GetInfo( hcon, &reply )) return FALSE;
1338 if (cinfo)
1340 cinfo->dwSize = reply.cursor_size;
1341 cinfo->bVisible = reply.cursor_visible;
1343 return TRUE;
1347 /******************************************************************************
1348 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1350 * RETURNS
1351 * Success: TRUE
1352 * Failure: FALSE
1354 BOOL32 WINAPI SetConsoleCursorInfo32(
1355 HANDLE32 hcon, /* [in] Handle to console screen buffer */
1356 LPCONSOLE_CURSOR_INFO cinfo) /* [in] Address of cursor information */
1358 struct set_console_info_request req;
1359 char buf[8];
1360 DWORD xlen;
1362 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
1363 K32OBJ_CONSOLE, GENERIC_WRITE )) == -1)
1364 return FALSE;
1365 CONSOLE_make_complex(hcon);
1366 sprintf(buf,"\033[?25%c",cinfo->bVisible?'h':'l');
1367 WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1369 req.cursor_size = cinfo->dwSize;
1370 req.cursor_visible = cinfo->bVisible;
1371 req.mask = SET_CONSOLE_INFO_CURSOR;
1372 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
1373 return !CLIENT_WaitReply( NULL, NULL, 0 );
1377 /******************************************************************************
1378 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1380 * RETURNS
1381 * Success: TRUE
1382 * Failure: FALSE
1384 BOOL32 WINAPI SetConsoleWindowInfo(
1385 HANDLE32 hcon, /* [in] Handle to console screen buffer */
1386 BOOL32 bAbsolute, /* [in] Coordinate type flag */
1387 LPSMALL_RECT window) /* [in] Address of new window rectangle */
1389 FIXME(console, "(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1390 return TRUE;
1394 /******************************************************************************
1395 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1397 * Sets the foreground and background color attributes of characters
1398 * written to the screen buffer.
1400 * RETURNS
1401 * Success: TRUE
1402 * Failure: FALSE
1404 BOOL32 WINAPI SetConsoleTextAttribute32(HANDLE32 hConsoleOutput,WORD wAttr)
1406 const int colormap[8] = {
1407 0,4,2,6,
1408 1,5,3,7,
1410 DWORD xlen;
1411 char buffer[20];
1413 TRACE(console,"(%d,%d)\n",hConsoleOutput,wAttr);
1414 sprintf(buffer,"%c[0;%s3%d;4%dm",
1416 (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1417 colormap[wAttr&7],
1418 colormap[(wAttr&0x70)>>4]
1420 WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1421 return TRUE;
1425 /******************************************************************************
1426 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1428 * PARAMS
1429 * hConsoleOutput [I] Handle to console screen buffer
1430 * dwSize [I] New size in character rows and cols
1432 * RETURNS
1433 * Success: TRUE
1434 * Failure: FALSE
1436 BOOL32 WINAPI SetConsoleScreenBufferSize( HANDLE32 hConsoleOutput,
1437 COORD dwSize )
1439 FIXME(console, "(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1440 return TRUE;
1444 /******************************************************************************
1445 * FillConsoleOutputCharacterA [KERNEL32.242]
1447 * PARAMS
1448 * hConsoleOutput [I] Handle to screen buffer
1449 * cCharacter [I] Character to write
1450 * nLength [I] Number of cells to write to
1451 * dwCoord [I] Coords of first cell
1452 * lpNumCharsWritten [O] Pointer to number of cells written
1454 * RETURNS
1455 * Success: TRUE
1456 * Failure: FALSE
1458 BOOL32 WINAPI FillConsoleOutputCharacterA(
1459 HANDLE32 hConsoleOutput,
1460 BYTE cCharacter,
1461 DWORD nLength,
1462 COORD dwCoord,
1463 LPDWORD lpNumCharsWritten)
1465 long count;
1466 DWORD xlen;
1468 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1469 for(count=0;count<nLength;count++)
1470 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1471 *lpNumCharsWritten = nLength;
1472 return TRUE;
1476 /******************************************************************************
1477 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1479 * PARAMS
1480 * hConsoleOutput [I] Handle to screen buffer
1481 * cCharacter [I] Character to write
1482 * nLength [I] Number of cells to write to
1483 * dwCoord [I] Coords of first cell
1484 * lpNumCharsWritten [O] Pointer to number of cells written
1486 * RETURNS
1487 * Success: TRUE
1488 * Failure: FALSE
1490 BOOL32 WINAPI FillConsoleOutputCharacterW(HANDLE32 hConsoleOutput,
1491 WCHAR cCharacter,
1492 DWORD nLength,
1493 COORD dwCoord,
1494 LPDWORD lpNumCharsWritten)
1496 long count;
1497 DWORD xlen;
1499 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1500 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1501 * first
1503 for(count=0;count<nLength;count++)
1504 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1505 *lpNumCharsWritten = nLength;
1506 return TRUE;
1510 /******************************************************************************
1511 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1513 * PARAMS
1514 * hConsoleOutput [I] Handle to screen buffer
1515 * wAttribute [I] Color attribute to write
1516 * nLength [I] Number of cells to write to
1517 * dwCoord [I] Coords of first cell
1518 * lpNumAttrsWritten [O] Pointer to number of cells written
1520 * RETURNS
1521 * Success: TRUE
1522 * Failure: FALSE
1524 BOOL32 WINAPI FillConsoleOutputAttribute( HANDLE32 hConsoleOutput,
1525 WORD wAttribute, DWORD nLength, COORD dwCoord,
1526 LPDWORD lpNumAttrsWritten)
1528 FIXME(console, "(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1529 wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1530 *lpNumAttrsWritten = nLength;
1531 return TRUE;
1534 /******************************************************************************
1535 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1537 * BUGS
1538 * Unimplemented
1540 BOOL32 WINAPI ReadConsoleOutputCharacter32A(HANDLE32 hConsoleOutput,
1541 LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1543 FIXME(console, "(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1544 dword,coord.x,coord.y,lpdword);
1545 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1546 return FALSE;
1550 /******************************************************************************
1551 * ScrollConsoleScreenBuffer [KERNEL32.612]
1553 * BUGS
1554 * Unimplemented
1556 BOOL32 WINAPI ScrollConsoleScreenBuffer( HANDLE32 hConsoleOutput,
1557 LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1558 COORD dwDestOrigin, LPCHAR_INFO lpFill)
1560 FIXME(console, "(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1561 lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1562 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1563 return FALSE;