Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbeyj@wpi.edu>
[wine] / win32 / console.c
1 /*
2  * Win32 kernel functions
3  *
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  */
9
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 ?)
16  */
17 /* Reference applications:
18  * -  IDA (interactive disassembler) full version 3.75. Works.
19  * -  LYNX/W32. Works mostly, some keys crash it.
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <termios.h>
25 #include <string.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>
35
36 #include "winbase.h"
37 #include "wine/winuser16.h"
38 #include "wine/keyboard16.h"
39 #include "thread.h"
40 #include "async.h"
41 #include "file.h"
42 #include "process.h"
43 #include "winerror.h"
44 #include "wincon.h"
45 #include "heap.h"
46 #include "debugtools.h"
47
48 #include "server/request.h"
49 #include "server.h"
50
51 DEFAULT_DEBUG_CHANNEL(console)
52
53
54 /* FIXME:  Should be in an internal header file.  OK, so which one?
55    Used by CONSOLE_makecomplex. */
56 FILE *wine_openpty(int *master, int *slave, char *name,
57                    struct termios *term, struct winsize *winsize);
58
59 /****************************************************************************
60  *              CONSOLE_GetInfo
61  */
62 static BOOL CONSOLE_GetInfo( HANDLE handle, struct get_console_info_reply *reply )
63 {
64     struct get_console_info_request req;
65
66     req.handle = handle;
67     CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
68     return !CLIENT_WaitSimpleReply( reply, sizeof(*reply), NULL );
69 }
70
71 /****************************************************************************
72  *              XTERM_string_to_IR                      [internal]
73  *
74  * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
75  * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
76  */
77 static void
78 CONSOLE_string_to_IR( HANDLE hConsoleInput,unsigned char *buf,int len) {
79     int                 j,k;
80     INPUT_RECORD        ir;
81     DWORD               junk;
82
83     for (j=0;j<len;j++) {
84         unsigned char inchar = buf[j];
85
86         if (inchar!=27) { /* no escape -> 'normal' keyboard event */
87             ir.EventType = 1; /* Key_event */
88
89             ir.Event.KeyEvent.bKeyDown          = 1;
90             ir.Event.KeyEvent.wRepeatCount      = 0;
91
92             ir.Event.KeyEvent.dwControlKeyState = 0;
93             if (inchar & 0x80) {
94                 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
95                 inchar &= ~0x80;
96             }
97             ir.Event.KeyEvent.wVirtualKeyCode = VkKeyScan16(inchar);
98             if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0100)
99                 ir.Event.KeyEvent.dwControlKeyState|=SHIFT_PRESSED;
100             if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0200)
101                 ir.Event.KeyEvent.dwControlKeyState|=LEFT_CTRL_PRESSED;
102             if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0400)
103                 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
104             ir.Event.KeyEvent.wVirtualScanCode = MapVirtualKey16(
105                 ir.Event.KeyEvent.wVirtualKeyCode & 0x00ff,
106                 0 /* VirtualKeyCodes to ScanCode */
107             );
108             ir.Event.KeyEvent.uChar.AsciiChar = inchar;
109
110             if (inchar==127) { /* backspace */
111                 ir.Event.KeyEvent.uChar.AsciiChar = '\b'; /* FIXME: hmm */
112                 ir.Event.KeyEvent.wVirtualScanCode = 0x0e;
113                 ir.Event.KeyEvent.wVirtualKeyCode = VK_BACK;
114             } else {
115                 if ((inchar=='\n')||(inchar=='\r')) {
116                     ir.Event.KeyEvent.uChar.AsciiChar   = '\r';
117                     ir.Event.KeyEvent.wVirtualKeyCode   = VK_RETURN;
118                     ir.Event.KeyEvent.wVirtualScanCode  = 0x1c;
119                     ir.Event.KeyEvent.dwControlKeyState = 0;
120                 } else {
121                     if (inchar<' ') {
122                         /* FIXME: find good values for ^X */
123                         ir.Event.KeyEvent.wVirtualKeyCode = 0xdead;
124                         ir.Event.KeyEvent.wVirtualScanCode = 0xbeef;
125                     } 
126                 }
127             }
128
129             assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
130             ir.Event.KeyEvent.bKeyDown = 0;
131             assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
132             continue;
133         }
134         /* inchar is ESC */
135         if ((j==len-1) || (buf[j+1]!='[')) {/* add ESCape on its own */
136             ir.EventType = 1; /* Key_event */
137             ir.Event.KeyEvent.bKeyDown          = 1;
138             ir.Event.KeyEvent.wRepeatCount      = 0;
139
140             ir.Event.KeyEvent.wVirtualKeyCode   = VkKeyScan16(27);
141             ir.Event.KeyEvent.wVirtualScanCode  = MapVirtualKey16(
142                 ir.Event.KeyEvent.wVirtualKeyCode,0
143             );
144             ir.Event.KeyEvent.dwControlKeyState = 0;
145             ir.Event.KeyEvent.uChar.AsciiChar   = 27;
146             assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
147             ir.Event.KeyEvent.bKeyDown = 0;
148             assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
149             continue;
150         }
151         for (k=j;k<len;k++) {
152             if (((buf[k]>='A') && (buf[k]<='Z')) ||
153                 ((buf[k]>='a') && (buf[k]<='z')) ||
154                  (buf[k]=='~')
155             )
156                 break;
157         }
158         if (k<len) {
159             int subid,scancode=0;
160
161             ir.EventType                        = 1; /* Key_event */
162             ir.Event.KeyEvent.bKeyDown          = 1;
163             ir.Event.KeyEvent.wRepeatCount      = 0;
164             ir.Event.KeyEvent.dwControlKeyState = 0;
165
166             ir.Event.KeyEvent.wVirtualKeyCode   = 0xad; /* FIXME */
167             ir.Event.KeyEvent.wVirtualScanCode  = 0xad; /* FIXME */
168             ir.Event.KeyEvent.uChar.AsciiChar   = 0;
169
170             switch (buf[k]) {
171             case '~':
172                 sscanf(&buf[j+2],"%d",&subid);
173                 switch (subid) {
174                 case  2:/*INS */scancode = 0xe052;break;
175                 case  3:/*DEL */scancode = 0xe053;break;
176                 case  6:/*PGDW*/scancode = 0xe051;break;
177                 case  5:/*PGUP*/scancode = 0xe049;break;
178                 case 11:/*F1  */scancode = 0x003b;break;
179                 case 12:/*F2  */scancode = 0x003c;break;
180                 case 13:/*F3  */scancode = 0x003d;break;
181                 case 14:/*F4  */scancode = 0x003e;break;
182                 case 15:/*F5  */scancode = 0x003f;break;
183                 case 17:/*F6  */scancode = 0x0040;break;
184                 case 18:/*F7  */scancode = 0x0041;break;
185                 case 19:/*F8  */scancode = 0x0042;break;
186                 case 20:/*F9  */scancode = 0x0043;break;
187                 case 21:/*F10 */scancode = 0x0044;break;
188                 case 23:/*F11 */scancode = 0x00d9;break;
189                 case 24:/*F12 */scancode = 0x00da;break;
190                 /* FIXME: Shift-Fx */
191                 default:
192                         FIXME("parse ESC[%d~\n",subid);
193                         break;
194                 }
195                 break;
196             case 'A': /* Cursor Up    */scancode = 0xe048;break;
197             case 'B': /* Cursor Down  */scancode = 0xe050;break;
198             case 'D': /* Cursor Left  */scancode = 0xe04b;break;
199             case 'C': /* Cursor Right */scancode = 0xe04d;break;
200             case 'F': /* End          */scancode = 0xe04f;break;
201             case 'H': /* Home         */scancode = 0xe047;break;
202             case 'M':
203                 /* Mouse Button Press  (ESCM<button+'!'><x+'!'><y+'!'>) or
204                  *              Release (ESCM#<x+'!'><y+'!'>
205                  */
206                 if (k<len-3) {
207                     ir.EventType                        = MOUSE_EVENT;
208                     ir.Event.MouseEvent.dwMousePosition.x = buf[k+2]-'!';
209                     ir.Event.MouseEvent.dwMousePosition.y = buf[k+3]-'!';
210                     if (buf[k+1]=='#')
211                         ir.Event.MouseEvent.dwButtonState = 0;
212                     else
213                         ir.Event.MouseEvent.dwButtonState = 1<<(buf[k+1]-' ');
214                     ir.Event.MouseEvent.dwEventFlags      = 0; /* FIXME */
215                     assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk));
216                     j=k+3;
217                 }
218                 break;
219             case 'c':
220                 j=k;
221                 break;
222             }
223             if (scancode) {
224                 ir.Event.KeyEvent.wVirtualScanCode = scancode;
225                 ir.Event.KeyEvent.wVirtualKeyCode = MapVirtualKey16(scancode,1);
226                 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
227                 ir.Event.KeyEvent.bKeyDown              = 0;
228                 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
229                 j=k;
230                 continue;
231             }
232         }
233     }
234 }
235
236 /****************************************************************************
237  *              CONSOLE_get_input               (internal)
238  *
239  * Reads (nonblocking) as much input events as possible and stores them
240  * in an internal queue.
241  */
242 static void
243 CONSOLE_get_input( HANDLE handle, BOOL blockwait )
244 {
245     char        *buf = HeapAlloc(GetProcessHeap(),0,1);
246     int         len = 0;
247
248     while (1)
249     {
250         DWORD res;
251         char inchar;
252         if (WaitForSingleObject( handle, 0 )) break;
253         if (!ReadFile( handle, &inchar, 1, &res, NULL )) break;
254         if (!res) /* res 0 but readable means EOF? Hmm. */
255                 break;
256         buf = HeapReAlloc(GetProcessHeap(),0,buf,len+1);
257         buf[len++]=inchar;
258     }
259     CONSOLE_string_to_IR(handle,buf,len);
260     HeapFree(GetProcessHeap(),0,buf);
261 }
262
263 /******************************************************************************
264  * SetConsoleCtrlHandler [KERNEL32.459]  Adds function to calling process list
265  *
266  * PARAMS
267  *    func [I] Address of handler function
268  *    add  [I] Handler to add or remove
269  *
270  * RETURNS
271  *    Success: TRUE
272  *    Failure: FALSE
273  *
274  * CHANGED
275  * James Sutherland (JamesSutherland@gmx.de)
276  * Added global variables console_ignore_ctrl_c and handlers[]
277  * Does not yet do any error checking, or set LastError if failed.
278  * This doesn't yet matter, since these handlers are not yet called...!
279  */
280 static unsigned int console_ignore_ctrl_c = 0;
281 static HANDLER_ROUTINE *handlers[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
282 BOOL WINAPI SetConsoleCtrlHandler( HANDLER_ROUTINE *func, BOOL add )
283 {
284   unsigned int alloc_loop = sizeof(handlers)/sizeof(HANDLER_ROUTINE *);
285   unsigned int done = 0;
286   FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
287   if (!func)
288     {
289       console_ignore_ctrl_c = add;
290       return TRUE;
291     }
292   if (add)
293       {
294         for (;alloc_loop--;)
295           if (!handlers[alloc_loop] && !done)
296             {
297               handlers[alloc_loop] = func;
298               done++;
299             }
300         if (!done)
301            FIXME("Out of space on CtrlHandler table\n");
302         return(done);
303       }
304     else
305       {
306         for (;alloc_loop--;)
307           if (handlers[alloc_loop] == func && !done)
308             {
309               handlers[alloc_loop] = 0;
310               done++;
311             }
312         if (!done)
313            WARN("Attempt to remove non-installed CtrlHandler %p\n",
314                 func);
315         return (done);
316       }
317     return (done);
318 }
319
320
321 /******************************************************************************
322  * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
323  *
324  * PARAMS
325  *    dwCtrlEvent        [I] Type of event
326  *    dwProcessGroupID   [I] Process group ID to send event to
327  *
328  * NOTES
329  *    Doesn't yet work...!
330  *
331  * RETURNS
332  *    Success: True
333  *    Failure: False (and *should* [but doesn't] set LastError)
334  */
335 BOOL WINAPI GenerateConsoleCtrlEvent( DWORD dwCtrlEvent,
336                                         DWORD dwProcessGroupID )
337 {
338   if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
339     {
340       ERR("invalid event %d for PGID %ld\n", 
341            (unsigned short)dwCtrlEvent, dwProcessGroupID );
342       return FALSE;
343     }
344   if (dwProcessGroupID == GetCurrentProcessId() )
345     {
346       FIXME("Attempt to send event %d to self - stub\n",
347              (unsigned short)dwCtrlEvent );
348       return FALSE;
349     }
350   FIXME("event %d to external PGID %ld - not implemented yet\n",
351          (unsigned short)dwCtrlEvent, dwProcessGroupID );
352   return FALSE;
353 }
354
355
356 /******************************************************************************
357  * CreateConsoleScreenBuffer [KERNEL32.151]  Creates a console screen buffer
358  *
359  * PARAMS
360  *    dwDesiredAccess    [I] Access flag
361  *    dwShareMode        [I] Buffer share mode
362  *    sa                 [I] Security attributes
363  *    dwFlags            [I] Type of buffer to create
364  *    lpScreenBufferData [I] Reserved
365  *
366  * NOTES
367  *    Should call SetLastError
368  *
369  * RETURNS
370  *    Success: Handle to new console screen buffer
371  *    Failure: INVALID_HANDLE_VALUE
372  */
373 HANDLE WINAPI CreateConsoleScreenBuffer( DWORD dwDesiredAccess,
374                 DWORD dwShareMode, LPSECURITY_ATTRIBUTES sa,
375                 DWORD dwFlags, LPVOID lpScreenBufferData )
376 {
377     FIXME("(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess,
378           dwShareMode, sa, dwFlags, lpScreenBufferData);
379     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
380     return INVALID_HANDLE_VALUE;
381 }
382
383
384 /***********************************************************************
385  *           GetConsoleScreenBufferInfo   (KERNEL32.190)
386  */
387 BOOL WINAPI GetConsoleScreenBufferInfo( HANDLE hConsoleOutput,
388                                           LPCONSOLE_SCREEN_BUFFER_INFO csbi )
389 {
390     csbi->dwSize.x = 80;
391     csbi->dwSize.y = 24;
392     csbi->dwCursorPosition.x = 0;
393     csbi->dwCursorPosition.y = 0;
394     csbi->wAttributes = 0;
395     csbi->srWindow.Left = 0;
396     csbi->srWindow.Right        = 79;
397     csbi->srWindow.Top  = 0;
398     csbi->srWindow.Bottom       = 23;
399     csbi->dwMaximumWindowSize.x = 80;
400     csbi->dwMaximumWindowSize.y = 24;
401     return TRUE;
402 }
403
404
405 /******************************************************************************
406  * SetConsoleActiveScreenBuffer [KERNEL32.623]  Sets buffer to current console
407  *
408  * RETURNS
409  *    Success: TRUE
410  *    Failure: FALSE
411  */
412 BOOL WINAPI SetConsoleActiveScreenBuffer(
413     HANDLE hConsoleOutput) /* [in] Handle to console screen buffer */
414 {
415     FIXME("(%x): stub\n", hConsoleOutput);
416     return FALSE;
417 }
418
419
420 /***********************************************************************
421  *            GetLargestConsoleWindowSize   (KERNEL32.226)
422  */
423 DWORD WINAPI GetLargestConsoleWindowSize( HANDLE hConsoleOutput )
424 {
425     return (DWORD)MAKELONG(80,24);
426 }
427
428 /***********************************************************************
429  *            FreeConsole (KERNEL32.267)
430  */
431 BOOL WINAPI FreeConsole(VOID)
432 {
433     struct free_console_request req;
434     CLIENT_SendRequest( REQ_FREE_CONSOLE, -1, 1, &req, sizeof(req) );
435     return !CLIENT_WaitReply( NULL, NULL, 0 );
436 }
437
438
439 /*************************************************************************
440  *              CONSOLE_OpenHandle
441  *
442  * Open a handle to the current process console.
443  */
444 HANDLE CONSOLE_OpenHandle( BOOL output, DWORD access, LPSECURITY_ATTRIBUTES sa )
445 {
446     struct open_console_request req;
447     struct open_console_reply reply;
448
449     req.output  = output;
450     req.access  = access;
451     req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
452     CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
453     SetLastError(0);
454     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
455     return reply.handle;
456 }
457
458
459 /*************************************************************************
460  *              CONSOLE_make_complex                    [internal]
461  *
462  * Turns a CONSOLE kernel object into a complex one.
463  * (switches from output/input using the terminal where WINE was started to 
464  * its own xterm).
465  * 
466  * This makes simple commandline tools pipeable, while complex commandline 
467  * tools work without getting messed up by debugoutput.
468  * 
469  * All other functions should work indedependend from this call.
470  *
471  * To test for complex console: pid == 0 -> simple, otherwise complex.
472  */
473 static BOOL CONSOLE_make_complex(HANDLE handle)
474 {
475         struct set_console_fd_request req;
476         struct get_console_info_reply info;
477         struct termios term;
478         char buf[256];
479         char c = '\0';
480         int status = 0;
481         int i,xpid,master,slave;
482         DWORD   xlen;
483
484         if (!CONSOLE_GetInfo( handle, &info )) return FALSE;
485         if (info.pid) return TRUE; /* already complex */
486
487         MESSAGE("Console: Making console complex (creating an xterm)...\n");
488
489         if (tcgetattr(0, &term) < 0) {
490                 /* ignore failure, or we can't run from a script */
491         }
492         term.c_lflag = ~(ECHO|ICANON);
493
494         if (wine_openpty(&master, &slave, NULL, &term, NULL) < 0)
495             return FALSE;
496
497         if ((xpid=fork()) == 0) {
498                 tcsetattr(slave, TCSADRAIN, &term);
499                 sprintf(buf, "-Sxx%d", master);
500                 /* "-fn vga" for VGA font. Harmless if vga is not present:
501                  *  xterm: unable to open font "vga", trying "fixed".... 
502                  */
503                 execlp("xterm", "xterm", buf, "-fn","vga",NULL);
504                 ERR("error creating AllocConsole xterm\n");
505                 exit(1);
506         }
507
508         req.handle = handle;
509         req.pid = xpid;
510         CLIENT_SendRequest( REQ_SET_CONSOLE_FD, dup(slave), 1, &req, sizeof(req) );
511         CLIENT_WaitReply( NULL, NULL, 0 );
512
513         /* most xterms like to print their window ID when used with -S;
514          * read it and continue before the user has a chance...
515          */
516         for (i=0; c!='\n'; (status=read(slave, &c, 1)), i++) {
517                 if (status == -1 && c == '\0') {
518                                 /* wait for xterm to be created */
519                         usleep(100);
520                 }
521                 if (i > 10000) {
522                         ERR("can't read xterm WID\n");
523                         kill(xpid, SIGKILL);
524                         return FALSE;
525                 }
526         }
527         /* enable mouseclicks */
528         sprintf(buf,"%c[?1001s%c[?1000h",27,27);
529         WriteFile(handle,buf,strlen(buf),&xlen,NULL);
530         
531         if (GetConsoleTitleA( buf, sizeof(buf) ))
532         {
533             WriteFile(handle,"\033]2;",4,&xlen,NULL);
534             WriteFile(handle,buf,strlen(buf),&xlen,NULL);
535             WriteFile(handle,"\a",1,&xlen,NULL);
536         }
537         return TRUE;
538
539 }
540
541
542 /***********************************************************************
543  *            AllocConsole (KERNEL32.103)
544  *
545  * creates an xterm with a pty to our program
546  */
547 BOOL WINAPI AllocConsole(VOID)
548 {
549         struct open_console_request req;
550         struct open_console_reply reply;
551         HANDLE hIn, hOut, hErr;
552         DWORD   ret;
553
554         TRACE("()\n");
555         CLIENT_SendRequest( REQ_ALLOC_CONSOLE, -1, 1, &req, sizeof(req) );
556         ret = CLIENT_WaitReply( NULL, NULL, 0 );
557         if (ret != ERROR_SUCCESS) {
558                 /* Hmm, error returned by server when we already have an
559                  * opened console. however, we might have inherited it(?)
560                  * and our handles are wrong? puzzling -MM 990330
561                  */
562                 if (ret!=ERROR_ACCESS_DENIED) {
563                         ERR(" failed to allocate console: %ld\n",ret);
564                         return FALSE;
565                 }
566         }
567
568         req.output = 0;
569         req.access = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
570         req.inherit = FALSE;
571         CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
572         ret =CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ); 
573         if (ret != ERROR_SUCCESS)
574         {
575             /* FIXME: free console */
576             ERR(" open console error %ld\n",ret);
577             return FALSE;
578         }
579         hIn = reply.handle;
580
581         req.output = 1;
582         CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
583         if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
584         {
585             CloseHandle(hIn);
586             /* FIXME: free console */
587             return FALSE;
588         }
589         hOut = reply.handle;
590
591         if (!DuplicateHandle( GetCurrentProcess(), hOut,
592                               GetCurrentProcess(), &hErr,
593                               0, TRUE, DUPLICATE_SAME_ACCESS ))
594         {
595             CloseHandle(hIn);
596             CloseHandle(hOut);
597             return FALSE;
598         }
599
600         /* NT resets the STD_*_HANDLEs on console alloc */
601         SetStdHandle(STD_INPUT_HANDLE, hIn);
602         SetStdHandle(STD_OUTPUT_HANDLE, hOut);
603         SetStdHandle(STD_ERROR_HANDLE, hErr);
604
605         SetLastError(ERROR_SUCCESS);
606         SetConsoleTitleA("Wine Console");
607         return TRUE;
608 }
609
610
611 /******************************************************************************
612  * GetConsoleCP [KERNEL32.295]  Returns the OEM code page for the console
613  *
614  * RETURNS
615  *    Code page code
616  */
617 UINT WINAPI GetConsoleCP(VOID)
618 {
619     return GetACP();
620 }
621
622
623 /***********************************************************************
624  *            GetConsoleOutputCP   (KERNEL32.189)
625  */
626 UINT WINAPI GetConsoleOutputCP(VOID)
627 {
628     return GetConsoleCP();
629 }
630
631 /***********************************************************************
632  *            GetConsoleMode   (KERNEL32.188)
633  */
634 BOOL WINAPI GetConsoleMode(HANDLE hcon,LPDWORD mode)
635 {
636     struct get_console_mode_request req;
637     struct get_console_mode_reply reply;
638
639     req.handle = hcon;
640     CLIENT_SendRequest( REQ_GET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
641     if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
642     *mode = reply.mode;
643     return TRUE;
644 }
645
646
647 /******************************************************************************
648  * SetConsoleMode [KERNEL32.628]  Sets input mode of console's input buffer
649  *
650  * PARAMS
651  *    hcon [I] Handle to console input or screen buffer
652  *    mode [I] Input or output mode to set
653  *
654  * RETURNS
655  *    Success: TRUE
656  *    Failure: FALSE
657  */
658 BOOL WINAPI SetConsoleMode( HANDLE hcon, DWORD mode )
659 {
660     struct set_console_mode_request req;
661
662     req.handle = hcon;
663     req.mode = mode;
664     CLIENT_SendRequest( REQ_SET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
665     return !CLIENT_WaitReply( NULL, NULL, 0 );
666 }
667
668
669 /***********************************************************************
670  *            GetConsoleTitleA   (KERNEL32.191)
671  */
672 DWORD WINAPI GetConsoleTitleA(LPSTR title,DWORD size)
673 {
674     struct get_console_info_request req;
675     struct get_console_info_reply reply;
676     int len;
677     DWORD ret = 0;
678     HANDLE hcon;
679
680     if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ, 0, NULL,
681                                OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
682         return 0;
683     req.handle = hcon;
684     CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
685     if (!CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), title, size ))
686     {
687         if (len > sizeof(reply)+size) title[size-1] = 0;
688         ret = strlen(title);
689     }
690     CloseHandle( hcon );
691     return ret;
692 }
693
694
695 /******************************************************************************
696  * GetConsoleTitle32W [KERNEL32.192]  Retrieves title string for console
697  *
698  * PARAMS
699  *    title [O] Address of buffer for title
700  *    size  [I] Size of buffer
701  *
702  * RETURNS
703  *    Success: Length of string copied
704  *    Failure: 0
705  */
706 DWORD WINAPI GetConsoleTitleW( LPWSTR title, DWORD size )
707 {
708     char *tmp;
709     DWORD ret;
710
711     if (!(tmp = HeapAlloc( GetProcessHeap(), 0, size ))) return 0;
712     ret = GetConsoleTitleA( tmp, size );
713     lstrcpyAtoW( title, tmp );
714     HeapFree( GetProcessHeap(), 0, tmp );
715     return ret;
716 }
717
718
719 /***********************************************************************
720  *            WriteConsoleA   (KERNEL32.729)
721  */
722 BOOL WINAPI WriteConsoleA( HANDLE hConsoleOutput,
723                                LPCVOID lpBuffer,
724                                DWORD nNumberOfCharsToWrite,
725                                LPDWORD lpNumberOfCharsWritten,
726                                LPVOID lpReserved )
727 {
728         /* FIXME: should I check if this is a console handle? */
729         return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
730                          lpNumberOfCharsWritten, NULL);
731 }
732
733
734 #define CADD(c)                                                         \
735         if (bufused==curbufsize-1)                                      \
736             buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
737         buffer[bufused++]=c;
738 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
739
740 /***********************************************************************
741  *            WriteConsoleOutputA   (KERNEL32.732)
742  */
743 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput,
744                                      LPCHAR_INFO lpBuffer,
745                                      COORD dwBufferSize,
746                                      COORD dwBufferCoord,
747                                      LPSMALL_RECT lpWriteRegion)
748 {
749     int i,j,off=0,lastattr=-1;
750     char        sbuf[20],*buffer=NULL;
751     int         bufused=0,curbufsize = 100;
752     DWORD       res;
753     const int colormap[8] = {
754         0,4,2,6,
755         1,5,3,7,
756     };
757     CONSOLE_make_complex(hConsoleOutput);
758     buffer = HeapAlloc(GetProcessHeap(),0,100);;
759     curbufsize = 100;
760
761     TRACE("wr: top = %d, bottom=%d, left=%d,right=%d\n",
762         lpWriteRegion->Top,
763         lpWriteRegion->Bottom,
764         lpWriteRegion->Left,
765         lpWriteRegion->Right
766     );
767
768     for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
769         sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
770         SADD(sbuf);
771         for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
772             if (lastattr!=lpBuffer[off].Attributes) {
773                 lastattr = lpBuffer[off].Attributes;
774                 sprintf(sbuf,"%c[0;%s3%d;4%dm",
775                         27,
776                         (lastattr & FOREGROUND_INTENSITY)?"1;":"",
777                         colormap[lastattr&7],
778                         colormap[(lastattr&0x70)>>4]
779                 );
780                 /* FIXME: BACKGROUND_INTENSITY */
781                 SADD(sbuf);
782             }
783             CADD(lpBuffer[off].Char.AsciiChar);
784             off++;
785         }
786     }
787     sprintf(sbuf,"%c[0m",27);SADD(sbuf);
788     WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
789     HeapFree(GetProcessHeap(),0,buffer);
790     return TRUE;
791 }
792
793 /***********************************************************************
794  *            WriteConsoleW   (KERNEL32.577)
795  */
796 BOOL WINAPI WriteConsoleW( HANDLE hConsoleOutput,
797                                LPCVOID lpBuffer,
798                                DWORD nNumberOfCharsToWrite,
799                                LPDWORD lpNumberOfCharsWritten,
800                                LPVOID lpReserved )
801 {
802         BOOL ret;
803         LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
804
805         lstrcpynWtoA( xstring,  lpBuffer,nNumberOfCharsToWrite);
806
807         /* FIXME: should I check if this is a console handle? */
808         ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
809                          lpNumberOfCharsWritten, NULL);
810         HeapFree( GetProcessHeap(), 0, xstring );
811         return ret;
812 }
813
814
815 /***********************************************************************
816  *            ReadConsoleA   (KERNEL32.419)
817  */
818 BOOL WINAPI ReadConsoleA( HANDLE hConsoleInput,
819                               LPVOID lpBuffer,
820                               DWORD nNumberOfCharsToRead,
821                               LPDWORD lpNumberOfCharsRead,
822                               LPVOID lpReserved )
823 {
824     int         charsread = 0;
825     LPSTR       xbuf = (LPSTR)lpBuffer;
826     struct read_console_input_request req;
827     INPUT_RECORD        ir;
828
829     TRACE("(%d,%p,%ld,%p,%p)\n",
830             hConsoleInput,lpBuffer,nNumberOfCharsToRead,
831             lpNumberOfCharsRead,lpReserved
832     );
833
834     CONSOLE_get_input(hConsoleInput,FALSE);
835
836     req.handle = hConsoleInput;
837     req.count = 1;
838     req.flush = 1;
839
840     /* FIXME: should we read at least 1 char? The SDK does not say */
841     while (charsread<nNumberOfCharsToRead)
842     {
843         int len;
844
845         CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
846         if (CLIENT_WaitReply( &len, NULL, 1, &ir, sizeof(ir) ))
847             return FALSE;
848         assert( !(len % sizeof(ir)) );
849         if (!len) break;
850         if (!ir.Event.KeyEvent.bKeyDown)
851                 continue;
852         if (ir.EventType != KEY_EVENT)
853                 continue;
854         *xbuf++ = ir.Event.KeyEvent.uChar.AsciiChar; 
855         charsread++;
856     }
857     if (lpNumberOfCharsRead)
858         *lpNumberOfCharsRead = charsread;
859     return TRUE;
860 }
861
862 /***********************************************************************
863  *            ReadConsoleW   (KERNEL32.427)
864  */
865 BOOL WINAPI ReadConsoleW( HANDLE hConsoleInput,
866                               LPVOID lpBuffer,
867                               DWORD nNumberOfCharsToRead,
868                               LPDWORD lpNumberOfCharsRead,
869                               LPVOID lpReserved )
870 {
871     BOOL ret;
872     LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
873
874     ret = ReadConsoleA(
875         hConsoleInput,
876         buf,
877         nNumberOfCharsToRead,
878         lpNumberOfCharsRead,
879         lpReserved
880     );
881     if (ret)
882         lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
883     HeapFree( GetProcessHeap(), 0, buf );
884     return ret;
885 }
886
887
888 /******************************************************************************
889  * ReadConsoleInput32A [KERNEL32.569]  Reads data from a console
890  *
891  * PARAMS
892  *    hConsoleInput        [I] Handle to console input buffer
893  *    lpBuffer             [O] Address of buffer for read data
894  *    nLength              [I] Number of records to read
895  *    lpNumberOfEventsRead [O] Address of number of records read
896  *
897  * RETURNS
898  *    Success: TRUE
899  *    Failure: FALSE
900  */
901 BOOL WINAPI ReadConsoleInputA(HANDLE hConsoleInput,
902                                   LPINPUT_RECORD lpBuffer,
903                                   DWORD nLength, LPDWORD lpNumberOfEventsRead)
904 {
905     struct read_console_input_request req;
906     int len;
907
908     req.handle = hConsoleInput;
909     req.count = nLength;
910     req.flush = 1;
911
912     /* loop until we get at least one event */
913     for (;;)
914     {
915         CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
916         if (CLIENT_WaitReply( &len, NULL, 1, lpBuffer, nLength * sizeof(*lpBuffer) ))
917             return FALSE;
918         assert( !(len % sizeof(INPUT_RECORD)) );
919         if (len) break;
920         CONSOLE_get_input(hConsoleInput,TRUE);
921         /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
922     }
923     if (lpNumberOfEventsRead) *lpNumberOfEventsRead = len / sizeof(INPUT_RECORD);
924     return TRUE;
925 }
926
927
928 /***********************************************************************
929  *            ReadConsoleInput32W   (KERNEL32.570)
930  */
931 BOOL WINAPI ReadConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer,
932                                    DWORD count, LPDWORD read )
933 {
934     /* FIXME: Fix this if we get UNICODE input. */
935     return ReadConsoleInputA( handle, buffer, count, read );
936 }
937
938
939 /***********************************************************************
940  *            FlushConsoleInputBuffer   (KERNEL32.132)
941  */
942 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
943 {
944     struct read_console_input_request req;
945     int len;
946
947     req.handle = handle;
948     req.count = -1;  /* get all records */
949     req.flush = 1;
950     CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
951     return !CLIENT_WaitReply( &len, NULL, 0 );
952 }
953
954
955 /***********************************************************************
956  *            PeekConsoleInputA   (KERNEL32.550)
957  *
958  * Gets 'count' first events (or less) from input queue.
959  *
960  * Does not need a complex console.
961  */
962 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer,
963                                    DWORD count, LPDWORD read )
964 {
965     struct read_console_input_request req;
966     int len;
967
968     CONSOLE_get_input(handle,FALSE);
969     req.handle = handle;
970     req.count = count;
971     req.flush = 0;
972
973     CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
974     if (CLIENT_WaitReply( &len, NULL, 1, buffer, count * sizeof(*buffer) ))
975         return FALSE;
976     assert( !(len % sizeof(INPUT_RECORD)) );
977     if (read) *read = len / sizeof(INPUT_RECORD);
978     return TRUE;
979 }
980
981
982 /***********************************************************************
983  *            PeekConsoleInputW   (KERNEL32.551)
984  */
985 BOOL WINAPI PeekConsoleInputW(HANDLE hConsoleInput,
986                                   LPINPUT_RECORD pirBuffer,
987                                   DWORD cInRecords,
988                                   LPDWORD lpcRead)
989 {
990     /* FIXME: Hmm. Fix this if we get UNICODE input. */
991     return PeekConsoleInputA(hConsoleInput,pirBuffer,cInRecords,lpcRead);
992 }
993
994
995 /******************************************************************************
996  * WriteConsoleInput32A [KERNEL32.730]  Write data to a console input buffer
997  *
998  */
999 BOOL WINAPI WriteConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
1000                                     DWORD count, LPDWORD written )
1001 {
1002     struct write_console_input_request req;
1003     struct write_console_input_reply reply;
1004
1005     req.handle = handle;
1006     req.count = count;
1007     CLIENT_SendRequest( REQ_WRITE_CONSOLE_INPUT, -1, 2, &req, sizeof(req),
1008                         buffer, count * sizeof(*buffer) );
1009     if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
1010     if (written) *written = reply.written;
1011     return TRUE;
1012 }
1013
1014
1015 /***********************************************************************
1016  *            SetConsoleTitle32A   (KERNEL32.476)
1017  *
1018  * Sets the console title.
1019  *
1020  * We do not necessarily need to create a complex console for that,
1021  * but should remember the title and set it on creation of the latter.
1022  * (not fixed at this time).
1023  */
1024 BOOL WINAPI SetConsoleTitleA(LPCSTR title)
1025 {
1026     struct set_console_info_request req;
1027     struct get_console_info_reply info;
1028     HANDLE hcon;
1029     DWORD written;
1030
1031     if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
1032                                OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
1033         return FALSE;
1034     req.handle = hcon;
1035     req.mask = SET_CONSOLE_INFO_TITLE;
1036     CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 2, &req, sizeof(req),
1037                         title, strlen(title)+1 );
1038     if (CLIENT_WaitReply( NULL, NULL, 0 )) goto error;
1039     if (CONSOLE_GetInfo( hcon, &info ) && info.pid)
1040     {
1041         /* only set title for complex console (own xterm) */
1042         WriteFile( hcon, "\033]2;", 4, &written, NULL );
1043         WriteFile( hcon, title, strlen(title), &written, NULL );
1044         WriteFile( hcon, "\a", 1, &written, NULL );
1045     }
1046     CloseHandle( hcon );
1047     return TRUE;
1048  error:
1049     CloseHandle( hcon );
1050     return FALSE;
1051 }
1052
1053
1054 /******************************************************************************
1055  * SetConsoleTitle32W [KERNEL32.477]  Sets title bar string for console
1056  *
1057  * PARAMS
1058  *    title [I] Address of new title
1059  *
1060  * NOTES
1061  *    This should not be calling the A version
1062  *
1063  * RETURNS
1064  *    Success: TRUE
1065  *    Failure: FALSE
1066  */
1067 BOOL WINAPI SetConsoleTitleW( LPCWSTR title )
1068 {
1069     BOOL ret;
1070
1071     LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1072     ret = SetConsoleTitleA(titleA);
1073     HeapFree( GetProcessHeap(), 0, titleA );
1074     return ret;
1075 }
1076
1077 /******************************************************************************
1078  * SetConsoleCursorPosition [KERNEL32.627]
1079  * Sets the cursor position in console
1080  *
1081  * PARAMS
1082  *    hConsoleOutput   [I] Handle of console screen buffer
1083  *    dwCursorPosition [I] New cursor position coordinates
1084  *
1085  * RETURNS STD
1086  */
1087 BOOL WINAPI SetConsoleCursorPosition( HANDLE hcon, COORD pos )
1088 {
1089     char        xbuf[20];
1090     DWORD       xlen;
1091
1092     /* make console complex only if we change lines, not just in the line */
1093     if (pos.y)
1094         CONSOLE_make_complex(hcon);
1095
1096     TRACE("%d (%dx%d)\n", hcon, pos.x , pos.y );
1097     /* x are columns, y rows */
1098     if (pos.y) 
1099         /* full screen cursor absolute positioning */
1100         sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1101     else
1102         /* relative cursor positioning in line (\r to go to 0) */
1103         sprintf(xbuf,"\r%c[%dC", 0x1B, pos.x);
1104     /* FIXME: store internal if we start using own console buffers */
1105     WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1106     return TRUE;
1107 }
1108
1109 /***********************************************************************
1110  *            GetNumberOfConsoleInputEvents   (KERNEL32.246)
1111  */
1112 BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hcon,LPDWORD nrofevents)
1113 {
1114     CONSOLE_get_input(hcon,FALSE);
1115     *nrofevents = 1; /* UMM */
1116     return TRUE;
1117 }
1118
1119 /***********************************************************************
1120  *            GetNumberOfConsoleMouseButtons   (KERNEL32.358)
1121  */
1122 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1123 {
1124     FIXME("(%p): stub\n", nrofbuttons);
1125     *nrofbuttons = 2;
1126     return TRUE;
1127 }
1128
1129 /******************************************************************************
1130  * GetConsoleCursorInfo32 [KERNEL32.296]  Gets size and visibility of console
1131  *
1132  * PARAMS
1133  *    hcon  [I] Handle to console screen buffer
1134  *    cinfo [O] Address of cursor information
1135  *
1136  * RETURNS
1137  *    Success: TRUE
1138  *    Failure: FALSE
1139  */
1140 BOOL WINAPI GetConsoleCursorInfo( HANDLE hcon,
1141                                       LPCONSOLE_CURSOR_INFO cinfo )
1142 {
1143     struct get_console_info_reply reply;
1144
1145     if (!CONSOLE_GetInfo( hcon, &reply )) return FALSE;
1146     if (cinfo)
1147     {
1148         cinfo->dwSize = reply.cursor_size;
1149         cinfo->bVisible = reply.cursor_visible;
1150     }
1151     return TRUE;
1152 }
1153
1154
1155 /******************************************************************************
1156  * SetConsoleCursorInfo32 [KERNEL32.626]  Sets size and visibility of cursor
1157  *
1158  * RETURNS
1159  *    Success: TRUE
1160  *    Failure: FALSE
1161  */
1162 BOOL WINAPI SetConsoleCursorInfo( 
1163     HANDLE hcon,                /* [in] Handle to console screen buffer */
1164     LPCONSOLE_CURSOR_INFO cinfo)  /* [in] Address of cursor information */
1165 {
1166     struct set_console_info_request req;
1167     char        buf[8];
1168     DWORD       xlen;
1169
1170     req.handle = hcon;
1171     CONSOLE_make_complex(hcon);
1172     sprintf(buf,"\033[?25%c",cinfo->bVisible?'h':'l');
1173     WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1174
1175     req.cursor_size    = cinfo->dwSize;
1176     req.cursor_visible = cinfo->bVisible;
1177     req.mask           = SET_CONSOLE_INFO_CURSOR;
1178     CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
1179     return !CLIENT_WaitReply( NULL, NULL, 0 );
1180 }
1181
1182
1183 /******************************************************************************
1184  * SetConsoleWindowInfo [KERNEL32.634]  Sets size and position of console
1185  *
1186  * RETURNS
1187  *    Success: TRUE
1188  *    Failure: FALSE
1189  */
1190 BOOL WINAPI SetConsoleWindowInfo(
1191     HANDLE hcon,       /* [in] Handle to console screen buffer */
1192     BOOL bAbsolute,    /* [in] Coordinate type flag */
1193     LPSMALL_RECT window) /* [in] Address of new window rectangle */
1194 {
1195     FIXME("(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1196     return TRUE;
1197 }
1198
1199
1200 /******************************************************************************
1201  * SetConsoleTextAttribute32 [KERNEL32.631]  Sets colors for text
1202  *
1203  * Sets the foreground and background color attributes of characters
1204  * written to the screen buffer.
1205  *
1206  * RETURNS
1207  *    Success: TRUE
1208  *    Failure: FALSE
1209  */
1210 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput,WORD wAttr)
1211 {
1212     const int colormap[8] = {
1213         0,4,2,6,
1214         1,5,3,7,
1215     };
1216     DWORD xlen;
1217     char buffer[20];
1218
1219     TRACE("(%d,%d)\n",hConsoleOutput,wAttr);
1220     sprintf(buffer,"%c[0;%s3%d;4%dm",
1221         27,
1222         (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1223         colormap[wAttr&7],
1224         colormap[(wAttr&0x70)>>4]
1225     );
1226     WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1227     return TRUE;
1228 }
1229
1230
1231 /******************************************************************************
1232  * SetConsoleScreenBufferSize [KERNEL32.630]  Changes size of console 
1233  *
1234  * PARAMS
1235  *    hConsoleOutput [I] Handle to console screen buffer
1236  *    dwSize         [I] New size in character rows and cols
1237  *
1238  * RETURNS
1239  *    Success: TRUE
1240  *    Failure: FALSE
1241  */
1242 BOOL WINAPI SetConsoleScreenBufferSize( HANDLE hConsoleOutput, 
1243                                           COORD dwSize )
1244 {
1245     FIXME("(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1246     return TRUE;
1247 }
1248
1249
1250 /******************************************************************************
1251  * FillConsoleOutputCharacterA [KERNEL32.242]
1252  *
1253  * PARAMS
1254  *    hConsoleOutput    [I] Handle to screen buffer
1255  *    cCharacter        [I] Character to write
1256  *    nLength           [I] Number of cells to write to
1257  *    dwCoord           [I] Coords of first cell
1258  *    lpNumCharsWritten [O] Pointer to number of cells written
1259  *
1260  * RETURNS
1261  *    Success: TRUE
1262  *    Failure: FALSE
1263  */
1264 BOOL WINAPI FillConsoleOutputCharacterA(
1265     HANDLE hConsoleOutput,
1266     BYTE cCharacter,
1267     DWORD nLength,
1268     COORD dwCoord,
1269     LPDWORD lpNumCharsWritten)
1270 {
1271     long        count;
1272     DWORD       xlen;
1273
1274     SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1275     for(count=0;count<nLength;count++)
1276         WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1277     *lpNumCharsWritten = nLength;
1278     return TRUE;
1279 }
1280
1281
1282 /******************************************************************************
1283  * FillConsoleOutputCharacterW [KERNEL32.243]  Writes characters to console
1284  *
1285  * PARAMS
1286  *    hConsoleOutput    [I] Handle to screen buffer
1287  *    cCharacter        [I] Character to write
1288  *    nLength           [I] Number of cells to write to
1289  *    dwCoord           [I] Coords of first cell
1290  *    lpNumCharsWritten [O] Pointer to number of cells written
1291  *
1292  * RETURNS
1293  *    Success: TRUE
1294  *    Failure: FALSE
1295  */
1296 BOOL WINAPI FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1297                                             WCHAR cCharacter,
1298                                             DWORD nLength,
1299                                            COORD dwCoord, 
1300                                             LPDWORD lpNumCharsWritten)
1301 {
1302     long        count;
1303     DWORD       xlen;
1304
1305     SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1306     /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1307      * first 
1308      */
1309     for(count=0;count<nLength;count++)
1310         WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1311     *lpNumCharsWritten = nLength;
1312     return TRUE;
1313 }
1314
1315
1316 /******************************************************************************
1317  * FillConsoleOutputAttribute [KERNEL32.241]  Sets attributes for console
1318  *
1319  * PARAMS
1320  *    hConsoleOutput    [I] Handle to screen buffer
1321  *    wAttribute        [I] Color attribute to write
1322  *    nLength           [I] Number of cells to write to
1323  *    dwCoord           [I] Coords of first cell
1324  *    lpNumAttrsWritten [O] Pointer to number of cells written
1325  *
1326  * RETURNS
1327  *    Success: TRUE
1328  *    Failure: FALSE
1329  */
1330 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, 
1331               WORD wAttribute, DWORD nLength, COORD dwCoord, 
1332               LPDWORD lpNumAttrsWritten)
1333 {
1334     FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1335           wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1336     *lpNumAttrsWritten = nLength;
1337     return TRUE;
1338 }
1339
1340 /******************************************************************************
1341  * ReadConsoleOutputCharacter32A [KERNEL32.573]
1342  * 
1343  * BUGS
1344  *   Unimplemented
1345  */
1346 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, 
1347               LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1348 {
1349     FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1350           dword,coord.x,coord.y,lpdword);
1351     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1352     return FALSE;
1353 }
1354
1355
1356 /******************************************************************************
1357  * ScrollConsoleScreenBuffer [KERNEL32.612]
1358  * 
1359  * BUGS
1360  *   Unimplemented
1361  */
1362 BOOL WINAPI ScrollConsoleScreenBuffer( HANDLE hConsoleOutput, 
1363               LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1364               COORD dwDestOrigin, LPCHAR_INFO lpFill)
1365 {
1366     FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1367           lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1368     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1369     return FALSE;
1370 }