Added DebugBreak.
[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 alloc_console_request req;
550     struct alloc_console_reply reply;
551     HANDLE hStderr;
552
553     TRACE("()\n");
554     req.access  = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
555     req.inherit = FALSE;
556     CLIENT_SendRequest( REQ_ALLOC_CONSOLE, -1, 1, &req, sizeof(req) );
557     if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
558         return FALSE;
559
560     if (!DuplicateHandle( GetCurrentProcess(), reply.handle_out, GetCurrentProcess(), &hStderr,
561                           0, TRUE, DUPLICATE_SAME_ACCESS ))
562     {
563         CloseHandle( reply.handle_in );
564         CloseHandle( reply.handle_out );
565         FreeConsole();
566         return FALSE;
567     }
568
569     /* NT resets the STD_*_HANDLEs on console alloc */
570     SetStdHandle( STD_INPUT_HANDLE, reply.handle_in );
571     SetStdHandle( STD_OUTPUT_HANDLE, reply.handle_out );
572     SetStdHandle( STD_ERROR_HANDLE, hStderr );
573
574     SetLastError(ERROR_SUCCESS);
575     SetConsoleTitleA("Wine Console");
576     return TRUE;
577 }
578
579
580 /******************************************************************************
581  * GetConsoleCP [KERNEL32.295]  Returns the OEM code page for the console
582  *
583  * RETURNS
584  *    Code page code
585  */
586 UINT WINAPI GetConsoleCP(VOID)
587 {
588     return GetACP();
589 }
590
591
592 /***********************************************************************
593  *            GetConsoleOutputCP   (KERNEL32.189)
594  */
595 UINT WINAPI GetConsoleOutputCP(VOID)
596 {
597     return GetConsoleCP();
598 }
599
600 /***********************************************************************
601  *            GetConsoleMode   (KERNEL32.188)
602  */
603 BOOL WINAPI GetConsoleMode(HANDLE hcon,LPDWORD mode)
604 {
605     struct get_console_mode_request req;
606     struct get_console_mode_reply reply;
607
608     req.handle = hcon;
609     CLIENT_SendRequest( REQ_GET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
610     if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
611     *mode = reply.mode;
612     return TRUE;
613 }
614
615
616 /******************************************************************************
617  * SetConsoleMode [KERNEL32.628]  Sets input mode of console's input buffer
618  *
619  * PARAMS
620  *    hcon [I] Handle to console input or screen buffer
621  *    mode [I] Input or output mode to set
622  *
623  * RETURNS
624  *    Success: TRUE
625  *    Failure: FALSE
626  */
627 BOOL WINAPI SetConsoleMode( HANDLE hcon, DWORD mode )
628 {
629     struct set_console_mode_request req;
630
631     req.handle = hcon;
632     req.mode = mode;
633     CLIENT_SendRequest( REQ_SET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
634     return !CLIENT_WaitReply( NULL, NULL, 0 );
635 }
636
637
638 /***********************************************************************
639  *            GetConsoleTitleA   (KERNEL32.191)
640  */
641 DWORD WINAPI GetConsoleTitleA(LPSTR title,DWORD size)
642 {
643     struct get_console_info_request req;
644     struct get_console_info_reply reply;
645     int len;
646     DWORD ret = 0;
647     HANDLE hcon;
648
649     if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ, 0, NULL,
650                                OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
651         return 0;
652     req.handle = hcon;
653     CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
654     if (!CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), title, size ))
655     {
656         if (len > sizeof(reply)+size) title[size-1] = 0;
657         ret = strlen(title);
658     }
659     CloseHandle( hcon );
660     return ret;
661 }
662
663
664 /******************************************************************************
665  * GetConsoleTitle32W [KERNEL32.192]  Retrieves title string for console
666  *
667  * PARAMS
668  *    title [O] Address of buffer for title
669  *    size  [I] Size of buffer
670  *
671  * RETURNS
672  *    Success: Length of string copied
673  *    Failure: 0
674  */
675 DWORD WINAPI GetConsoleTitleW( LPWSTR title, DWORD size )
676 {
677     char *tmp;
678     DWORD ret;
679
680     if (!(tmp = HeapAlloc( GetProcessHeap(), 0, size ))) return 0;
681     ret = GetConsoleTitleA( tmp, size );
682     lstrcpyAtoW( title, tmp );
683     HeapFree( GetProcessHeap(), 0, tmp );
684     return ret;
685 }
686
687
688 /***********************************************************************
689  *            WriteConsoleA   (KERNEL32.729)
690  */
691 BOOL WINAPI WriteConsoleA( HANDLE hConsoleOutput,
692                                LPCVOID lpBuffer,
693                                DWORD nNumberOfCharsToWrite,
694                                LPDWORD lpNumberOfCharsWritten,
695                                LPVOID lpReserved )
696 {
697         /* FIXME: should I check if this is a console handle? */
698         return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
699                          lpNumberOfCharsWritten, NULL);
700 }
701
702
703 #define CADD(c)                                                         \
704         if (bufused==curbufsize-1)                                      \
705             buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
706         buffer[bufused++]=c;
707 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
708
709 /***********************************************************************
710  *            WriteConsoleOutputA   (KERNEL32.732)
711  */
712 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput,
713                                      LPCHAR_INFO lpBuffer,
714                                      COORD dwBufferSize,
715                                      COORD dwBufferCoord,
716                                      LPSMALL_RECT lpWriteRegion)
717 {
718     int i,j,off=0,lastattr=-1;
719     char        sbuf[20],*buffer=NULL;
720     int         bufused=0,curbufsize = 100;
721     DWORD       res;
722     const int colormap[8] = {
723         0,4,2,6,
724         1,5,3,7,
725     };
726     CONSOLE_make_complex(hConsoleOutput);
727     buffer = HeapAlloc(GetProcessHeap(),0,100);;
728     curbufsize = 100;
729
730     TRACE("wr: top = %d, bottom=%d, left=%d,right=%d\n",
731         lpWriteRegion->Top,
732         lpWriteRegion->Bottom,
733         lpWriteRegion->Left,
734         lpWriteRegion->Right
735     );
736
737     for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
738         sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
739         SADD(sbuf);
740         for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
741             if (lastattr!=lpBuffer[off].Attributes) {
742                 lastattr = lpBuffer[off].Attributes;
743                 sprintf(sbuf,"%c[0;%s3%d;4%dm",
744                         27,
745                         (lastattr & FOREGROUND_INTENSITY)?"1;":"",
746                         colormap[lastattr&7],
747                         colormap[(lastattr&0x70)>>4]
748                 );
749                 /* FIXME: BACKGROUND_INTENSITY */
750                 SADD(sbuf);
751             }
752             CADD(lpBuffer[off].Char.AsciiChar);
753             off++;
754         }
755     }
756     sprintf(sbuf,"%c[0m",27);SADD(sbuf);
757     WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
758     HeapFree(GetProcessHeap(),0,buffer);
759     return TRUE;
760 }
761
762 /***********************************************************************
763  *            WriteConsoleW   (KERNEL32.577)
764  */
765 BOOL WINAPI WriteConsoleW( HANDLE hConsoleOutput,
766                                LPCVOID lpBuffer,
767                                DWORD nNumberOfCharsToWrite,
768                                LPDWORD lpNumberOfCharsWritten,
769                                LPVOID lpReserved )
770 {
771         BOOL ret;
772         LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
773
774         lstrcpynWtoA( xstring,  lpBuffer,nNumberOfCharsToWrite);
775
776         /* FIXME: should I check if this is a console handle? */
777         ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
778                          lpNumberOfCharsWritten, NULL);
779         HeapFree( GetProcessHeap(), 0, xstring );
780         return ret;
781 }
782
783
784 /***********************************************************************
785  *            ReadConsoleA   (KERNEL32.419)
786  */
787 BOOL WINAPI ReadConsoleA( HANDLE hConsoleInput,
788                               LPVOID lpBuffer,
789                               DWORD nNumberOfCharsToRead,
790                               LPDWORD lpNumberOfCharsRead,
791                               LPVOID lpReserved )
792 {
793     int         charsread = 0;
794     LPSTR       xbuf = (LPSTR)lpBuffer;
795     struct read_console_input_request req;
796     struct read_console_input_reply reply;
797     INPUT_RECORD        ir;
798
799     TRACE("(%d,%p,%ld,%p,%p)\n",
800             hConsoleInput,lpBuffer,nNumberOfCharsToRead,
801             lpNumberOfCharsRead,lpReserved
802     );
803
804     CONSOLE_get_input(hConsoleInput,FALSE);
805
806     req.handle = hConsoleInput;
807     req.count = 1;
808     req.flush = 1;
809
810     /* FIXME: should we read at least 1 char? The SDK does not say */
811     while (charsread<nNumberOfCharsToRead)
812     {
813         int len;
814
815         CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
816         if (CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), &ir, sizeof(ir) ))
817             return FALSE;
818         len -= sizeof(reply);
819         assert( !(len % sizeof(ir)) );
820         if (!len) break;
821         if (!ir.Event.KeyEvent.bKeyDown)
822                 continue;
823         if (ir.EventType != KEY_EVENT)
824                 continue;
825         *xbuf++ = ir.Event.KeyEvent.uChar.AsciiChar; 
826         charsread++;
827     }
828     if (lpNumberOfCharsRead)
829         *lpNumberOfCharsRead = charsread;
830     return TRUE;
831 }
832
833 /***********************************************************************
834  *            ReadConsoleW   (KERNEL32.427)
835  */
836 BOOL WINAPI ReadConsoleW( HANDLE hConsoleInput,
837                               LPVOID lpBuffer,
838                               DWORD nNumberOfCharsToRead,
839                               LPDWORD lpNumberOfCharsRead,
840                               LPVOID lpReserved )
841 {
842     BOOL ret;
843     LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
844
845     ret = ReadConsoleA(
846         hConsoleInput,
847         buf,
848         nNumberOfCharsToRead,
849         lpNumberOfCharsRead,
850         lpReserved
851     );
852     if (ret)
853         lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
854     HeapFree( GetProcessHeap(), 0, buf );
855     return ret;
856 }
857
858
859 /******************************************************************************
860  * ReadConsoleInput32A [KERNEL32.569]  Reads data from a console
861  *
862  * PARAMS
863  *    hConsoleInput        [I] Handle to console input buffer
864  *    lpBuffer             [O] Address of buffer for read data
865  *    nLength              [I] Number of records to read
866  *    lpNumberOfEventsRead [O] Address of number of records read
867  *
868  * RETURNS
869  *    Success: TRUE
870  *    Failure: FALSE
871  */
872 BOOL WINAPI ReadConsoleInputA(HANDLE hConsoleInput,
873                                   LPINPUT_RECORD lpBuffer,
874                                   DWORD nLength, LPDWORD lpNumberOfEventsRead)
875 {
876     struct read_console_input_request req;
877     struct read_console_input_reply reply;
878     int len;
879
880     req.handle = hConsoleInput;
881     req.count = nLength;
882     req.flush = 1;
883
884     /* loop until we get at least one event */
885     for (;;)
886     {
887         CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
888         if (CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply),
889                               lpBuffer, nLength * sizeof(*lpBuffer) ))
890             return FALSE;
891         len -= sizeof(reply);
892         assert( !(len % sizeof(INPUT_RECORD)) );
893         if (len) break;
894         CONSOLE_get_input(hConsoleInput,TRUE);
895         /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
896     }
897     if (lpNumberOfEventsRead) *lpNumberOfEventsRead = len / sizeof(INPUT_RECORD);
898     return TRUE;
899 }
900
901
902 /***********************************************************************
903  *            ReadConsoleInput32W   (KERNEL32.570)
904  */
905 BOOL WINAPI ReadConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer,
906                                    DWORD count, LPDWORD read )
907 {
908     /* FIXME: Fix this if we get UNICODE input. */
909     return ReadConsoleInputA( handle, buffer, count, read );
910 }
911
912
913 /***********************************************************************
914  *            FlushConsoleInputBuffer   (KERNEL32.132)
915  */
916 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
917 {
918     struct read_console_input_request req;
919     int len;
920
921     req.handle = handle;
922     req.count = -1;  /* get all records */
923     req.flush = 1;
924     CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
925     return !CLIENT_WaitReply( &len, NULL, 0 );
926 }
927
928
929 /***********************************************************************
930  *            PeekConsoleInputA   (KERNEL32.550)
931  *
932  * Gets 'count' first events (or less) from input queue.
933  *
934  * Does not need a complex console.
935  */
936 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer,
937                                    DWORD count, LPDWORD read )
938 {
939     struct read_console_input_request req;
940     struct read_console_input_reply reply;
941     int len;
942
943     CONSOLE_get_input(handle,FALSE);
944     req.handle = handle;
945     req.count = count;
946     req.flush = 0;
947
948     CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
949     if (CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply),
950                           buffer, count * sizeof(*buffer) ))
951         return FALSE;
952     len -= sizeof(reply);
953     assert( !(len % sizeof(INPUT_RECORD)) );
954     if (read) *read = len / sizeof(INPUT_RECORD);
955     return TRUE;
956 }
957
958
959 /***********************************************************************
960  *            PeekConsoleInputW   (KERNEL32.551)
961  */
962 BOOL WINAPI PeekConsoleInputW(HANDLE hConsoleInput,
963                                   LPINPUT_RECORD pirBuffer,
964                                   DWORD cInRecords,
965                                   LPDWORD lpcRead)
966 {
967     /* FIXME: Hmm. Fix this if we get UNICODE input. */
968     return PeekConsoleInputA(hConsoleInput,pirBuffer,cInRecords,lpcRead);
969 }
970
971
972 /******************************************************************************
973  * WriteConsoleInput32A [KERNEL32.730]  Write data to a console input buffer
974  *
975  */
976 BOOL WINAPI WriteConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
977                                     DWORD count, LPDWORD written )
978 {
979     struct write_console_input_request req;
980     struct write_console_input_reply reply;
981
982     req.handle = handle;
983     req.count = count;
984     CLIENT_SendRequest( REQ_WRITE_CONSOLE_INPUT, -1, 2, &req, sizeof(req),
985                         buffer, count * sizeof(*buffer) );
986     if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
987     if (written) *written = reply.written;
988     return TRUE;
989 }
990
991
992 /***********************************************************************
993  *            SetConsoleTitle32A   (KERNEL32.476)
994  *
995  * Sets the console title.
996  *
997  * We do not necessarily need to create a complex console for that,
998  * but should remember the title and set it on creation of the latter.
999  * (not fixed at this time).
1000  */
1001 BOOL WINAPI SetConsoleTitleA(LPCSTR title)
1002 {
1003     struct set_console_info_request req;
1004     struct get_console_info_reply info;
1005     HANDLE hcon;
1006     DWORD written;
1007
1008     if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
1009                                OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
1010         return FALSE;
1011     req.handle = hcon;
1012     req.mask = SET_CONSOLE_INFO_TITLE;
1013     CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 2, &req, sizeof(req),
1014                         title, strlen(title)+1 );
1015     if (CLIENT_WaitReply( NULL, NULL, 0 )) goto error;
1016     if (CONSOLE_GetInfo( hcon, &info ) && info.pid)
1017     {
1018         /* only set title for complex console (own xterm) */
1019         WriteFile( hcon, "\033]2;", 4, &written, NULL );
1020         WriteFile( hcon, title, strlen(title), &written, NULL );
1021         WriteFile( hcon, "\a", 1, &written, NULL );
1022     }
1023     CloseHandle( hcon );
1024     return TRUE;
1025  error:
1026     CloseHandle( hcon );
1027     return FALSE;
1028 }
1029
1030
1031 /******************************************************************************
1032  * SetConsoleTitle32W [KERNEL32.477]  Sets title bar string for console
1033  *
1034  * PARAMS
1035  *    title [I] Address of new title
1036  *
1037  * NOTES
1038  *    This should not be calling the A version
1039  *
1040  * RETURNS
1041  *    Success: TRUE
1042  *    Failure: FALSE
1043  */
1044 BOOL WINAPI SetConsoleTitleW( LPCWSTR title )
1045 {
1046     BOOL ret;
1047
1048     LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1049     ret = SetConsoleTitleA(titleA);
1050     HeapFree( GetProcessHeap(), 0, titleA );
1051     return ret;
1052 }
1053
1054 /******************************************************************************
1055  * SetConsoleCursorPosition [KERNEL32.627]
1056  * Sets the cursor position in console
1057  *
1058  * PARAMS
1059  *    hConsoleOutput   [I] Handle of console screen buffer
1060  *    dwCursorPosition [I] New cursor position coordinates
1061  *
1062  * RETURNS STD
1063  */
1064 BOOL WINAPI SetConsoleCursorPosition( HANDLE hcon, COORD pos )
1065 {
1066     char        xbuf[20];
1067     DWORD       xlen;
1068
1069     /* make console complex only if we change lines, not just in the line */
1070     if (pos.y)
1071         CONSOLE_make_complex(hcon);
1072
1073     TRACE("%d (%dx%d)\n", hcon, pos.x , pos.y );
1074     /* x are columns, y rows */
1075     if (pos.y) 
1076         /* full screen cursor absolute positioning */
1077         sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1078     else
1079         /* relative cursor positioning in line (\r to go to 0) */
1080         sprintf(xbuf,"\r%c[%dC", 0x1B, pos.x);
1081     /* FIXME: store internal if we start using own console buffers */
1082     WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1083     return TRUE;
1084 }
1085
1086 /***********************************************************************
1087  *            GetNumberOfConsoleInputEvents   (KERNEL32.246)
1088  */
1089 BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hcon,LPDWORD nrofevents)
1090 {
1091     CONSOLE_get_input(hcon,FALSE);
1092     *nrofevents = 1; /* UMM */
1093     return TRUE;
1094 }
1095
1096 /***********************************************************************
1097  *            GetNumberOfConsoleMouseButtons   (KERNEL32.358)
1098  */
1099 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1100 {
1101     FIXME("(%p): stub\n", nrofbuttons);
1102     *nrofbuttons = 2;
1103     return TRUE;
1104 }
1105
1106 /******************************************************************************
1107  * GetConsoleCursorInfo32 [KERNEL32.296]  Gets size and visibility of console
1108  *
1109  * PARAMS
1110  *    hcon  [I] Handle to console screen buffer
1111  *    cinfo [O] Address of cursor information
1112  *
1113  * RETURNS
1114  *    Success: TRUE
1115  *    Failure: FALSE
1116  */
1117 BOOL WINAPI GetConsoleCursorInfo( HANDLE hcon,
1118                                       LPCONSOLE_CURSOR_INFO cinfo )
1119 {
1120     struct get_console_info_reply reply;
1121
1122     if (!CONSOLE_GetInfo( hcon, &reply )) return FALSE;
1123     if (cinfo)
1124     {
1125         cinfo->dwSize = reply.cursor_size;
1126         cinfo->bVisible = reply.cursor_visible;
1127     }
1128     return TRUE;
1129 }
1130
1131
1132 /******************************************************************************
1133  * SetConsoleCursorInfo32 [KERNEL32.626]  Sets size and visibility of cursor
1134  *
1135  * RETURNS
1136  *    Success: TRUE
1137  *    Failure: FALSE
1138  */
1139 BOOL WINAPI SetConsoleCursorInfo( 
1140     HANDLE hcon,                /* [in] Handle to console screen buffer */
1141     LPCONSOLE_CURSOR_INFO cinfo)  /* [in] Address of cursor information */
1142 {
1143     struct set_console_info_request req;
1144     char        buf[8];
1145     DWORD       xlen;
1146
1147     req.handle = hcon;
1148     CONSOLE_make_complex(hcon);
1149     sprintf(buf,"\033[?25%c",cinfo->bVisible?'h':'l');
1150     WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1151
1152     req.cursor_size    = cinfo->dwSize;
1153     req.cursor_visible = cinfo->bVisible;
1154     req.mask           = SET_CONSOLE_INFO_CURSOR;
1155     CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
1156     return !CLIENT_WaitReply( NULL, NULL, 0 );
1157 }
1158
1159
1160 /******************************************************************************
1161  * SetConsoleWindowInfo [KERNEL32.634]  Sets size and position of console
1162  *
1163  * RETURNS
1164  *    Success: TRUE
1165  *    Failure: FALSE
1166  */
1167 BOOL WINAPI SetConsoleWindowInfo(
1168     HANDLE hcon,       /* [in] Handle to console screen buffer */
1169     BOOL bAbsolute,    /* [in] Coordinate type flag */
1170     LPSMALL_RECT window) /* [in] Address of new window rectangle */
1171 {
1172     FIXME("(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1173     return TRUE;
1174 }
1175
1176
1177 /******************************************************************************
1178  * SetConsoleTextAttribute32 [KERNEL32.631]  Sets colors for text
1179  *
1180  * Sets the foreground and background color attributes of characters
1181  * written to the screen buffer.
1182  *
1183  * RETURNS
1184  *    Success: TRUE
1185  *    Failure: FALSE
1186  */
1187 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput,WORD wAttr)
1188 {
1189     const int colormap[8] = {
1190         0,4,2,6,
1191         1,5,3,7,
1192     };
1193     DWORD xlen;
1194     char buffer[20];
1195
1196     TRACE("(%d,%d)\n",hConsoleOutput,wAttr);
1197     sprintf(buffer,"%c[0;%s3%d;4%dm",
1198         27,
1199         (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1200         colormap[wAttr&7],
1201         colormap[(wAttr&0x70)>>4]
1202     );
1203     WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1204     return TRUE;
1205 }
1206
1207
1208 /******************************************************************************
1209  * SetConsoleScreenBufferSize [KERNEL32.630]  Changes size of console 
1210  *
1211  * PARAMS
1212  *    hConsoleOutput [I] Handle to console screen buffer
1213  *    dwSize         [I] New size in character rows and cols
1214  *
1215  * RETURNS
1216  *    Success: TRUE
1217  *    Failure: FALSE
1218  */
1219 BOOL WINAPI SetConsoleScreenBufferSize( HANDLE hConsoleOutput, 
1220                                           COORD dwSize )
1221 {
1222     FIXME("(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1223     return TRUE;
1224 }
1225
1226
1227 /******************************************************************************
1228  * FillConsoleOutputCharacterA [KERNEL32.242]
1229  *
1230  * PARAMS
1231  *    hConsoleOutput    [I] Handle to screen buffer
1232  *    cCharacter        [I] Character to write
1233  *    nLength           [I] Number of cells to write to
1234  *    dwCoord           [I] Coords of first cell
1235  *    lpNumCharsWritten [O] Pointer to number of cells written
1236  *
1237  * RETURNS
1238  *    Success: TRUE
1239  *    Failure: FALSE
1240  */
1241 BOOL WINAPI FillConsoleOutputCharacterA(
1242     HANDLE hConsoleOutput,
1243     BYTE cCharacter,
1244     DWORD nLength,
1245     COORD dwCoord,
1246     LPDWORD lpNumCharsWritten)
1247 {
1248     long        count;
1249     DWORD       xlen;
1250
1251     SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1252     for(count=0;count<nLength;count++)
1253         WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1254     *lpNumCharsWritten = nLength;
1255     return TRUE;
1256 }
1257
1258
1259 /******************************************************************************
1260  * FillConsoleOutputCharacterW [KERNEL32.243]  Writes characters to console
1261  *
1262  * PARAMS
1263  *    hConsoleOutput    [I] Handle to screen buffer
1264  *    cCharacter        [I] Character to write
1265  *    nLength           [I] Number of cells to write to
1266  *    dwCoord           [I] Coords of first cell
1267  *    lpNumCharsWritten [O] Pointer to number of cells written
1268  *
1269  * RETURNS
1270  *    Success: TRUE
1271  *    Failure: FALSE
1272  */
1273 BOOL WINAPI FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1274                                             WCHAR cCharacter,
1275                                             DWORD nLength,
1276                                            COORD dwCoord, 
1277                                             LPDWORD lpNumCharsWritten)
1278 {
1279     long        count;
1280     DWORD       xlen;
1281
1282     SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1283     /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1284      * first 
1285      */
1286     for(count=0;count<nLength;count++)
1287         WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1288     *lpNumCharsWritten = nLength;
1289     return TRUE;
1290 }
1291
1292
1293 /******************************************************************************
1294  * FillConsoleOutputAttribute [KERNEL32.241]  Sets attributes for console
1295  *
1296  * PARAMS
1297  *    hConsoleOutput    [I] Handle to screen buffer
1298  *    wAttribute        [I] Color attribute to write
1299  *    nLength           [I] Number of cells to write to
1300  *    dwCoord           [I] Coords of first cell
1301  *    lpNumAttrsWritten [O] Pointer to number of cells written
1302  *
1303  * RETURNS
1304  *    Success: TRUE
1305  *    Failure: FALSE
1306  */
1307 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, 
1308               WORD wAttribute, DWORD nLength, COORD dwCoord, 
1309               LPDWORD lpNumAttrsWritten)
1310 {
1311     FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1312           wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1313     *lpNumAttrsWritten = nLength;
1314     return TRUE;
1315 }
1316
1317 /******************************************************************************
1318  * ReadConsoleOutputCharacter32A [KERNEL32.573]
1319  * 
1320  * BUGS
1321  *   Unimplemented
1322  */
1323 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, 
1324               LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1325 {
1326     FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1327           dword,coord.x,coord.y,lpdword);
1328     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1329     return FALSE;
1330 }
1331
1332
1333 /******************************************************************************
1334  * ScrollConsoleScreenBuffer [KERNEL32.612]
1335  * 
1336  * BUGS
1337  *   Unimplemented
1338  */
1339 BOOL WINAPI ScrollConsoleScreenBuffer( HANDLE hConsoleOutput, 
1340               LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1341               COORD dwDestOrigin, LPCHAR_INFO lpFill)
1342 {
1343     FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1344           lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1345     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1346     return FALSE;
1347 }