2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
7 * Copyright 1998 Marcus Meissner
11 * - Completely lacks SCREENBUFFER interface.
12 * - No abstraction for something other than xterm.
13 * - Key input translation shouldn't use VkKeyScan and MapVirtualKey, since
14 * they are window (USER) driver dependend.
15 * - Output sometimes is buffered (We switched off buffering by ~ICANON ?)
17 /* Reference applications:
18 * - IDA (interactive disassembler) full version 3.75. Works.
19 * - LYNX/W32. Works mostly, some keys crash it.
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
32 #include <sys/errno.h>
47 #include "server/request.h"
50 /* The CONSOLE kernel32 Object */
51 typedef struct _CONSOLE
{
54 CONSOLE_CURSOR_INFO cinfo
;
56 int master
; /* xterm side of pty */
58 int hread
, hwrite
; /* server handles (hack) */
59 int pid
; /* xterm's pid, -1 if no xterm */
60 LPSTR title
; /* title of console */
61 INPUT_RECORD
*irs
; /* buffered input records */
62 int nrofirs
;/* nr of buffered input records */
63 THREAD_QUEUE wait_queue
;
66 static void CONSOLE_Destroy( K32OBJ
*obj
);
68 const K32OBJ_OPS CONSOLE_Ops
=
70 CONSOLE_Destroy
/* destroy */
73 /***********************************************************************
76 static void CONSOLE_Destroy(K32OBJ
*obj
)
78 CONSOLE
*console
= (CONSOLE
*)obj
;
79 assert(obj
->type
== K32OBJ_CONSOLE
);
81 obj
->type
= K32OBJ_UNKNOWN
;
84 HeapFree( SystemHeap
, 0, console
->title
);
85 console
->title
= NULL
;
86 /* if there is a xterm, kill it. */
87 if (console
->pid
!= -1)
88 kill(console
->pid
, SIGTERM
);
89 HeapFree(SystemHeap
, 0, console
);
93 /****************************************************************************
94 * CONSOLE_add_input_record [internal]
96 * Adds an INPUT_RECORD to the CONSOLEs input queue.
99 CONSOLE_add_input_record(CONSOLE
*console
,INPUT_RECORD
*inp
) {
100 console
->irs
= HeapReAlloc(GetProcessHeap(),0,console
->irs
,sizeof(INPUT_RECORD
)*(console
->nrofirs
+1));
101 console
->irs
[console
->nrofirs
++]=*inp
;
104 /****************************************************************************
105 * XTERM_string_to_IR [internal]
107 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
108 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
111 CONSOLE_string_to_IR( HANDLE32 hConsoleInput
,unsigned char *buf
,int len
) {
114 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
,
115 K32OBJ_CONSOLE
, 0, NULL
);
117 for (j
=0;j
<len
;j
++) {
118 unsigned char inchar
= buf
[j
];
120 if (inchar
!=27) { /* no escape -> 'normal' keyboard event */
121 ir
.EventType
= 1; /* Key_event */
123 ir
.Event
.KeyEvent
.bKeyDown
= 1;
124 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
126 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
128 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
131 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(inchar
);
132 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0100)
133 ir
.Event
.KeyEvent
.dwControlKeyState
|=SHIFT_PRESSED
;
134 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0200)
135 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_CTRL_PRESSED
;
136 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0400)
137 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
138 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
139 ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x00ff,
140 0 /* VirtualKeyCodes to ScanCode */
143 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
144 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0x0d;
145 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
147 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= inchar
;
149 /* FIXME: find good values for ^X */
150 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
151 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
155 CONSOLE_add_input_record(console
,&ir
);
156 ir
.Event
.KeyEvent
.bKeyDown
= 0;
157 CONSOLE_add_input_record(console
,&ir
);
161 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
162 ir
.EventType
= 1; /* Key_event */
163 ir
.Event
.KeyEvent
.bKeyDown
= 1;
164 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
166 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
167 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
168 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
170 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
171 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
172 CONSOLE_add_input_record(console
,&ir
);
173 ir
.Event
.KeyEvent
.bKeyDown
= 0;
174 CONSOLE_add_input_record(console
,&ir
);
177 for (k
=j
;k
<len
;k
++) {
178 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
179 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
185 int subid
,scancode
=0;
187 ir
.EventType
= 1; /* Key_event */
188 ir
.Event
.KeyEvent
.bKeyDown
= 1;
189 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
190 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
192 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
193 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
194 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
198 sscanf(&buf
[j
+2],"%d",&subid
);
200 case 2:/*INS */scancode
= 0xe052;break;
201 case 3:/*DEL */scancode
= 0xe053;break;
202 case 6:/*PGDW*/scancode
= 0xe051;break;
203 case 5:/*PGUP*/scancode
= 0xe049;break;
204 case 11:/*F1 */scancode
= 0x003b;break;
205 case 12:/*F2 */scancode
= 0x003c;break;
206 case 13:/*F3 */scancode
= 0x003d;break;
207 case 14:/*F4 */scancode
= 0x003e;break;
208 case 15:/*F5 */scancode
= 0x003f;break;
209 case 17:/*F6 */scancode
= 0x0040;break;
210 case 18:/*F7 */scancode
= 0x0041;break;
211 case 19:/*F8 */scancode
= 0x0042;break;
212 case 20:/*F9 */scancode
= 0x0043;break;
213 case 21:/*F10 */scancode
= 0x0044;break;
214 case 23:/*F11 */scancode
= 0x00d9;break;
215 case 24:/*F12 */scancode
= 0x00da;break;
216 /* FIXME: Shift-Fx */
218 FIXME(console
,"parse ESC[%d~\n",subid
);
222 case 'A': /* Cursor Up */scancode
= 0xe048;break;
223 case 'B': /* Cursor Down */scancode
= 0xe050;break;
224 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
225 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
226 case 'F': /* End */scancode
= 0xe04f;break;
227 case 'H': /* Home */scancode
= 0xe047;break;
229 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
230 * Release (ESCM#<x+'!'><y+'!'>
233 ir
.EventType
= MOUSE_EVENT
;
234 ir
.Event
.MouseEvent
.dwMousePosition
.x
= buf
[k
+2]-'!';
235 ir
.Event
.MouseEvent
.dwMousePosition
.y
= buf
[k
+3]-'!';
237 ir
.Event
.MouseEvent
.dwButtonState
= 0;
239 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
240 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
241 CONSOLE_add_input_record(console
,&ir
);
248 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
249 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
250 CONSOLE_add_input_record(console
,&ir
);
251 ir
.Event
.KeyEvent
.bKeyDown
= 0;
252 CONSOLE_add_input_record(console
,&ir
);
258 K32OBJ_DecCount(&console
->header
);
261 /****************************************************************************
262 * CONSOLE_get_input (internal)
264 * Reads (nonblocking) as much input events as possible and stores them
265 * in an internal queue.
268 CONSOLE_get_input( HANDLE32 handle
)
270 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
277 if (!WaitForSingleObject( handle
, 0 )) break;
278 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
279 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
282 CONSOLE_string_to_IR(handle
,buf
,len
);
283 HeapFree(GetProcessHeap(),0,buf
);
286 /****************************************************************************
287 * CONSOLE_drain_input (internal)
289 * Drains 'n' console input events from the queue.
292 CONSOLE_drain_input(CONSOLE
*console
,int n
) {
293 assert(n
<=console
->nrofirs
);
296 memcpy( &console
->irs
[0],
298 console
->nrofirs
*sizeof(INPUT_RECORD
)
300 console
->irs
= HeapReAlloc(
304 console
->nrofirs
*sizeof(INPUT_RECORD
)
310 /******************************************************************************
311 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
314 * func [I] Address of handler function
315 * add [I] Handler to add or remove
322 * James Sutherland (JamesSutherland@gmx.de)
323 * Added global variables console_ignore_ctrl_c and handlers[]
324 * Does not yet do any error checking, or set LastError if failed.
325 * This doesn't yet matter, since these handlers are not yet called...!
327 static unsigned int console_ignore_ctrl_c
= 0;
328 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
329 BOOL32 WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL32 add
)
331 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
332 unsigned int done
= 0;
333 FIXME(console
, "(%p,%i) - no error checking or testing yet\n", func
, add
);
336 console_ignore_ctrl_c
= add
;
342 if (!handlers
[alloc_loop
] && !done
)
344 handlers
[alloc_loop
] = func
;
348 FIXME(console
, "Out of space on CtrlHandler table\n");
354 if (handlers
[alloc_loop
] == func
&& !done
)
356 handlers
[alloc_loop
] = 0;
360 WARN(console
, "Attempt to remove non-installed CtrlHandler %p\n",
368 /******************************************************************************
369 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
372 * dwCtrlEvent [I] Type of event
373 * dwProcessGroupID [I] Process group ID to send event to
376 * Doesn't yet work...!
380 * Failure: False (and *should* [but doesn't] set LastError)
382 BOOL32 WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
383 DWORD dwProcessGroupID
)
385 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
387 ERR( console
, "invalid event %d for PGID %ld\n",
388 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
391 if (dwProcessGroupID
== GetCurrentProcessId() )
393 FIXME( console
, "Attempt to send event %d to self - stub\n",
394 (unsigned short)dwCtrlEvent
);
397 FIXME( console
,"event %d to external PGID %ld - not implemented yet\n",
398 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
403 /******************************************************************************
404 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
407 * dwDesiredAccess [I] Access flag
408 * dwShareMode [I] Buffer share mode
409 * sa [I] Security attributes
410 * dwFlags [I] Type of buffer to create
411 * lpScreenBufferData [I] Reserved
414 * Should call SetLastError
417 * Success: Handle to new console screen buffer
418 * Failure: INVALID_HANDLE_VALUE
420 HANDLE32 WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
421 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
422 DWORD dwFlags
, LPVOID lpScreenBufferData
)
424 FIXME(console
, "(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
425 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
426 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
427 return INVALID_HANDLE_VALUE32
;
431 /***********************************************************************
432 * GetConsoleScreenBufferInfo (KERNEL32.190)
434 BOOL32 WINAPI
GetConsoleScreenBufferInfo( HANDLE32 hConsoleOutput
,
435 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
439 csbi
->dwCursorPosition
.x
= 0;
440 csbi
->dwCursorPosition
.y
= 0;
441 csbi
->wAttributes
= 0;
442 csbi
->srWindow
.Left
= 0;
443 csbi
->srWindow
.Right
= 79;
444 csbi
->srWindow
.Top
= 0;
445 csbi
->srWindow
.Bottom
= 23;
446 csbi
->dwMaximumWindowSize
.x
= 80;
447 csbi
->dwMaximumWindowSize
.y
= 24;
452 /******************************************************************************
453 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
459 BOOL32 WINAPI
SetConsoleActiveScreenBuffer(
460 HANDLE32 hConsoleOutput
) /* [in] Handle to console screen buffer */
462 FIXME(console
, "(%x): stub\n", hConsoleOutput
);
467 /***********************************************************************
468 * GetLargestConsoleWindowSize (KERNEL32.226)
470 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE32 hConsoleOutput
)
472 return (DWORD
)MAKELONG(80,24);
475 /***********************************************************************
476 * FreeConsole (KERNEL32.267)
478 BOOL32 WINAPI
FreeConsole(VOID
)
481 PDB32
*pdb
= PROCESS_Current();
486 console
= (CONSOLE
*)pdb
->console
;
488 if (console
== NULL
) {
489 SetLastError(ERROR_INVALID_PARAMETER
);
493 HANDLE_CloseAll( pdb
, &console
->header
);
495 K32OBJ_DecCount( &console
->header
);
503 * It looks like the openpty that comes with glibc in RedHat 5.0
504 * is buggy (second call returns what looks like a dup of 0 and 1
505 * instead of a new pty), this is a generic replacement.
507 static int CONSOLE_openpty(CONSOLE
*console
, char *name
,
508 struct termios
*term
, struct winsize
*winsize
)
513 struct set_console_fd_request req
;
515 strcpy (pts_name
, "/dev/ptyXY");
517 for (ptr1
= "pqrstuvwxyzPQRST"; *ptr1
!= 0; ptr1
++) {
519 for (ptr2
= "0123456789abcdef"; *ptr2
!= 0; ptr2
++) {
522 if ((fdm
= open(pts_name
, O_RDWR
)) < 0) {
529 if ((fds
= open(pts_name
, O_RDWR
)) < 0) {
533 console
->master
= fdm
;
534 console
->infd
= console
->outfd
= fds
;
535 req
.handle
= console
->hread
;
536 CLIENT_SendRequest( REQ_SET_CONSOLE_FD
, dup(fds
), 1, &req
, sizeof(req
) );
537 CLIENT_WaitReply( NULL
, NULL
, 0 );
538 req
.handle
= console
->hwrite
;
539 CLIENT_SendRequest( REQ_SET_CONSOLE_FD
, dup(fds
), 1, &req
, sizeof(req
) );
540 CLIENT_WaitReply( NULL
, NULL
, 0 );
543 tcsetattr(console
->infd
, TCSANOW
, term
);
545 ioctl(console
->outfd
, TIOCSWINSZ
, winsize
);
547 strcpy(name
, pts_name
);
554 /*************************************************************************
555 * CONSOLE_make_complex [internal]
557 * Turns a CONSOLE kernel object into a complex one.
558 * (switches from output/input using the terminal where WINE was started to
561 * This makes simple commandline tools pipeable, while complex commandline
562 * tools work without getting messed up by debugoutput.
564 * All other functions should work indedependend from this call.
566 * To test for complex console: pid == -1 -> simple, otherwise complex.
568 static BOOL32
CONSOLE_make_complex(HANDLE32 handle
,CONSOLE
*console
)
577 if (console
->pid
!= -1)
578 return TRUE
; /* already complex */
580 MSG("Console: Making console complex (creating an xterm)...\n");
582 if (tcgetattr(0, &term
) < 0) return FALSE
;
583 term
.c_lflag
= ~(ECHO
|ICANON
);
584 if (CONSOLE_openpty(console
, NULL
, &term
, NULL
) < 0) return FALSE
;
586 if ((xpid
=fork()) == 0) {
587 tcsetattr(console
->infd
, TCSADRAIN
, &term
);
588 sprintf(buf
, "-Sxx%d", console
->master
);
589 /* "-fn vga" for VGA font. Harmless if vga is not present:
590 * xterm: unable to open font "vga", trying "fixed"....
592 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
593 ERR(console
, "error creating AllocConsole xterm\n");
598 /* most xterms like to print their window ID when used with -S;
599 * read it and continue before the user has a chance...
601 for (i
=0; c
!='\n'; (status
=read(console
->infd
, &c
, 1)), i
++) {
602 if (status
== -1 && c
== '\0') {
603 /* wait for xterm to be created */
607 ERR(console
, "can't read xterm WID\n");
608 kill(console
->pid
, SIGKILL
);
612 /* enable mouseclicks */
613 sprintf(buf
,"%c[?1001s%c[?1000h",27,27);
614 WriteFile(handle
,buf
,strlen(buf
),&xlen
,NULL
);
619 /***********************************************************************
620 * CONSOLE_GetConsoleHandle
621 * returns a 16-bit style console handle
622 * note: only called from _lopen
624 HFILE32
CONSOLE_GetConsoleHandle(VOID
)
626 PDB32
*pdb
= PROCESS_Current();
627 HFILE32 handle
= HFILE_ERROR32
;
630 if (pdb
->console
!= NULL
) {
631 CONSOLE
*console
= (CONSOLE
*)pdb
->console
;
632 handle
= (HFILE32
)HANDLE_Alloc(pdb
, &console
->header
, 0, TRUE
, -1);
638 /***********************************************************************
639 * AllocConsole (KERNEL32.103)
641 * creates an xterm with a pty to our program
643 BOOL32 WINAPI
AllocConsole(VOID
)
645 struct create_console_request req
;
646 struct create_console_reply reply
;
648 PDB32
*pdb
= PROCESS_Current();
650 HANDLE32 hIn
, hOut
, hErr
;
652 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
654 SetLastError(ERROR_CANNOT_MAKE
); /* this might not be the right
655 error, but it's a good guess :) */
657 console
= (CONSOLE
*)pdb
->console
;
659 /* don't create a console if we already have one */
660 if (console
!= NULL
) {
661 SetLastError(ERROR_ACCESS_DENIED
);
666 if (!(console
= (CONSOLE
*)HeapAlloc( SystemHeap
, 0, sizeof(*console
))))
672 console
->header
.type
= K32OBJ_CONSOLE
;
673 console
->header
.refcount
= 1;
675 console
->title
= NULL
;
676 console
->nrofirs
= 0;
677 console
->wait_queue
= NULL
;
678 console
->irs
= HeapAlloc(GetProcessHeap(),0,1);;
679 console
->mode
= ENABLE_PROCESSED_INPUT
682 /* FIXME: we shouldn't probably use hardcoded UNIX values here. */
685 console
->hread
= console
->hwrite
= -1;
687 CLIENT_SendRequest( REQ_CREATE_CONSOLE
, -1, 1, &req
, sizeof(req
) );
688 if (CLIENT_WaitReply( &len
, NULL
, 1, &reply
, sizeof(reply
) ) != ERROR_SUCCESS
)
690 K32OBJ_DecCount(&console
->header
);
694 CHECK_LEN( len
, sizeof(reply
) );
695 console
->hread
= reply
.handle_read
;
696 console
->hwrite
= reply
.handle_write
;
698 if ((hIn
= HANDLE_Alloc(pdb
,&console
->header
, 0, TRUE
,
699 reply
.handle_read
)) == INVALID_HANDLE_VALUE32
)
701 CLIENT_CloseHandle( reply
.handle_write
);
702 K32OBJ_DecCount(&console
->header
);
707 if ((hOut
= HANDLE_Alloc(pdb
,&console
->header
, 0, TRUE
,
708 reply
.handle_write
)) == INVALID_HANDLE_VALUE32
)
711 K32OBJ_DecCount(&console
->header
);
716 if (!DuplicateHandle( GetCurrentProcess(), hOut
,
717 GetCurrentProcess(), &hErr
,
718 0, TRUE
, DUPLICATE_SAME_ACCESS
))
722 K32OBJ_DecCount(&console
->header
);
727 if (pdb
->console
) K32OBJ_DecCount( pdb
->console
);
728 pdb
->console
= (K32OBJ
*)console
;
729 K32OBJ_IncCount( pdb
->console
);
731 /* NT resets the STD_*_HANDLEs on console alloc */
732 SetStdHandle(STD_INPUT_HANDLE
, hIn
);
733 SetStdHandle(STD_OUTPUT_HANDLE
, hOut
);
734 SetStdHandle(STD_ERROR_HANDLE
, hErr
);
736 SetLastError(ERROR_SUCCESS
);
738 SetConsoleTitle32A("Wine Console");
743 /******************************************************************************
744 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
749 UINT32 WINAPI
GetConsoleCP(VOID
)
755 /***********************************************************************
756 * GetConsoleOutputCP (KERNEL32.189)
758 UINT32 WINAPI
GetConsoleOutputCP(VOID
)
760 return GetConsoleCP();
763 /***********************************************************************
764 * GetConsoleMode (KERNEL32.188)
766 BOOL32 WINAPI
GetConsoleMode(HANDLE32 hcon
,LPDWORD mode
)
768 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
771 FIXME(console
,"(%d,%p), no console handle passed!\n",hcon
,mode
);
774 *mode
= console
->mode
;
775 K32OBJ_DecCount(&console
->header
);
780 /******************************************************************************
781 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
784 * hcon [I] Handle to console input or screen buffer
785 * mode [I] Input or output mode to set
791 BOOL32 WINAPI
SetConsoleMode( HANDLE32 hcon
, DWORD mode
)
793 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
796 FIXME(console
,"(%d,%ld), no console handle passed!\n",hcon
,mode
);
799 FIXME(console
,"(0x%08x,0x%08lx): stub\n",hcon
,mode
);
800 console
->mode
= mode
;
801 K32OBJ_DecCount(&console
->header
);
806 /***********************************************************************
807 * GetConsoleTitleA (KERNEL32.191)
809 DWORD WINAPI
GetConsoleTitle32A(LPSTR title
,DWORD size
)
811 PDB32
*pdb
= PROCESS_Current();
812 CONSOLE
*console
= (CONSOLE
*)pdb
->console
;
814 if(console
&& console
->title
) {
815 lstrcpyn32A(title
,console
->title
,size
);
816 return strlen(title
);
822 /******************************************************************************
823 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
826 * title [O] Address of buffer for title
827 * size [I] Size of buffer
830 * Success: Length of string copied
833 DWORD WINAPI
GetConsoleTitle32W( LPWSTR title
, DWORD size
)
835 PDB32
*pdb
= PROCESS_Current();
836 CONSOLE
*console
= (CONSOLE
*)pdb
->console
;
837 if(console
&& console
->title
)
839 lstrcpynAtoW(title
,console
->title
,size
);
840 return (lstrlen32W(title
));
846 /***********************************************************************
847 * WriteConsoleA (KERNEL32.729)
849 BOOL32 WINAPI
WriteConsole32A( HANDLE32 hConsoleOutput
,
851 DWORD nNumberOfCharsToWrite
,
852 LPDWORD lpNumberOfCharsWritten
,
855 /* FIXME: should I check if this is a console handle? */
856 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
857 lpNumberOfCharsWritten
, NULL
);
862 if (bufused==curbufsize-1) \
863 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
865 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
867 /***********************************************************************
868 * WriteConsoleOutputA (KERNEL32.732)
870 BOOL32 WINAPI
WriteConsoleOutput32A( HANDLE32 hConsoleOutput
,
871 LPCHAR_INFO lpBuffer
,
874 LPSMALL_RECT lpWriteRegion
)
876 int i
,j
,off
=0,lastattr
=-1;
877 char sbuf
[20],*buffer
=NULL
;
878 int bufused
=0,curbufsize
= 100;
880 const int colormap
[8] = {
884 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleOutput
, K32OBJ_CONSOLE
, 0, NULL
);
887 FIXME(console
,"(%d,...): no console handle!\n",hConsoleOutput
);
890 CONSOLE_make_complex(hConsoleOutput
,console
);
891 buffer
= HeapAlloc(GetProcessHeap(),0,100);;
894 TRACE(console
,"wr: top = %d, bottom=%d, left=%d,right=%d\n",
896 lpWriteRegion
->Bottom
,
901 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
902 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
904 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
905 if (lastattr
!=lpBuffer
[off
].Attributes
) {
906 lastattr
= lpBuffer
[off
].Attributes
;
907 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
909 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
910 colormap
[lastattr
&7],
911 colormap
[(lastattr
&0x70)>>4]
913 /* FIXME: BACKGROUND_INTENSITY */
916 CADD(lpBuffer
[off
].Char
.AsciiChar
);
920 sprintf(sbuf
,"%c[0m",27);SADD(sbuf
);
921 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
922 HeapFree(GetProcessHeap(),0,buffer
);
923 K32OBJ_DecCount(&console
->header
);
927 /***********************************************************************
928 * WriteConsoleW (KERNEL32.577)
930 BOOL32 WINAPI
WriteConsole32W( HANDLE32 hConsoleOutput
,
932 DWORD nNumberOfCharsToWrite
,
933 LPDWORD lpNumberOfCharsWritten
,
937 LPSTR xstring
=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite
);
939 lstrcpynWtoA( xstring
, lpBuffer
,nNumberOfCharsToWrite
);
941 /* FIXME: should I check if this is a console handle? */
942 ret
= WriteFile(hConsoleOutput
, xstring
, nNumberOfCharsToWrite
,
943 lpNumberOfCharsWritten
, NULL
);
944 HeapFree( GetProcessHeap(), 0, xstring
);
949 /***********************************************************************
950 * ReadConsoleA (KERNEL32.419)
952 BOOL32 WINAPI
ReadConsole32A( HANDLE32 hConsoleInput
,
954 DWORD nNumberOfCharsToRead
,
955 LPDWORD lpNumberOfCharsRead
,
958 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
960 LPSTR xbuf
= (LPSTR
)lpBuffer
;
963 SetLastError(ERROR_INVALID_HANDLE
);
964 FIXME(console
,"(%d,...), no console handle!\n",hConsoleInput
);
967 TRACE(console
,"(%d,%p,%ld,%p,%p)\n",
968 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
969 lpNumberOfCharsRead
,lpReserved
971 CONSOLE_get_input(hConsoleInput
);
973 /* FIXME: should this drain everything from the input queue and just
974 * put the keypresses in the buffer? Needs further thought.
976 for (i
=0;(i
<console
->nrofirs
)&&(charsread
<nNumberOfCharsToRead
);i
++) {
977 if (console
->irs
[i
].EventType
!= KEY_EVENT
)
979 if (!console
->irs
[i
].Event
.KeyEvent
.bKeyDown
)
981 *xbuf
++ = console
->irs
[i
].Event
.KeyEvent
.uChar
.AsciiChar
;
984 CONSOLE_drain_input(console
,i
);
985 if (lpNumberOfCharsRead
)
986 *lpNumberOfCharsRead
= charsread
;
987 K32OBJ_DecCount(&console
->header
);
991 /***********************************************************************
992 * ReadConsoleW (KERNEL32.427)
994 BOOL32 WINAPI
ReadConsole32W( HANDLE32 hConsoleInput
,
996 DWORD nNumberOfCharsToRead
,
997 LPDWORD lpNumberOfCharsRead
,
1001 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
1003 ret
= ReadConsole32A(
1006 nNumberOfCharsToRead
,
1007 lpNumberOfCharsRead
,
1011 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
1012 HeapFree( GetProcessHeap(), 0, buf
);
1017 /******************************************************************************
1018 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
1021 * hConsoleInput [I] Handle to console input buffer
1022 * lpBuffer [O] Address of buffer for read data
1023 * nLength [I] Number of records to read
1024 * lpNumberOfEventsRead [O] Address of number of records read
1030 BOOL32 WINAPI
ReadConsoleInput32A(HANDLE32 hConsoleInput
,
1031 LPINPUT_RECORD lpBuffer
,
1032 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
1034 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
1036 TRACE(console
, "(%d,%p,%ld,%p)\n",hConsoleInput
, lpBuffer
, nLength
,
1037 lpNumberOfEventsRead
);
1039 FIXME(console
, "(%d,%p,%ld,%p), No console handle!\n",hConsoleInput
,
1040 lpBuffer
, nLength
, lpNumberOfEventsRead
);
1043 CONSOLE_get_input(hConsoleInput
);
1044 if (nLength
>console
->nrofirs
)
1045 nLength
= console
->nrofirs
;
1046 memcpy(lpBuffer
,console
->irs
,sizeof(INPUT_RECORD
)*nLength
);
1047 if (lpNumberOfEventsRead
)
1048 *lpNumberOfEventsRead
= nLength
;
1049 CONSOLE_drain_input(console
,nLength
);
1050 K32OBJ_DecCount(&console
->header
);
1054 /***********************************************************************
1055 * SetConsoleTitle32A (KERNEL32.476)
1057 * Sets the console title.
1059 * We do not necessarily need to create a complex console for that,
1060 * but should remember the title and set it on creation of the latter.
1061 * (not fixed at this time).
1063 BOOL32 WINAPI
SetConsoleTitle32A(LPCSTR title
)
1065 PDB32
*pdb
= PROCESS_Current();
1068 char titleformat
[]="\033]2;%s\a"; /*this should work for xterms*/
1072 TRACE(console
,"(%s)\n",title
);
1074 console
= (CONSOLE
*)pdb
->console
;
1077 if(console
->title
) /* Free old title, if there is one */
1078 HeapFree( SystemHeap
, 0, console
->title
);
1079 console
->title
= (LPSTR
)HeapAlloc(SystemHeap
, 0,strlen(title
)+1);
1080 if(console
->title
) strcpy(console
->title
,title
);
1081 titlestring
= HeapAlloc(GetProcessHeap(), 0,strlen(title
)+strlen(titleformat
)+1);
1083 K32OBJ_DecCount(&console
->header
);
1087 sprintf(titlestring
,titleformat
,title
);
1088 /* FIXME: hmm, should use WriteFile probably... */
1089 /*CONSOLE_Write(&console->header,titlestring,strlen(titlestring),&written,NULL);*/
1090 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
),titlestring
,strlen(titlestring
),&written
,NULL
);
1091 if (written
== strlen(titlestring
))
1093 HeapFree( GetProcessHeap(), 0, titlestring
);
1094 K32OBJ_DecCount(&console
->header
);
1099 /******************************************************************************
1100 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1103 * title [I] Address of new title
1106 * This should not be calling the A version
1112 BOOL32 WINAPI
SetConsoleTitle32W( LPCWSTR title
)
1116 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1117 ret
= SetConsoleTitle32A(titleA
);
1118 HeapFree( GetProcessHeap(), 0, titleA
);
1122 /***********************************************************************
1123 * ReadConsoleInput32W (KERNEL32.570)
1125 BOOL32 WINAPI
ReadConsoleInput32W(HANDLE32 hConsoleInput
,
1126 LPINPUT_RECORD lpBuffer
,
1127 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
1129 FIXME(console
, "(%d,%p,%ld,%p): stub\n",hConsoleInput
, lpBuffer
, nLength
,
1130 lpNumberOfEventsRead
);
1134 /***********************************************************************
1135 * FlushConsoleInputBuffer (KERNEL32.132)
1137 BOOL32 WINAPI
FlushConsoleInputBuffer(HANDLE32 hConsoleInput
)
1139 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
1143 CONSOLE_drain_input(console
,console
->nrofirs
);
1144 K32OBJ_DecCount(&console
->header
);
1149 /******************************************************************************
1150 * SetConsoleCursorPosition [KERNEL32.627]
1151 * Sets the cursor position in console
1154 * hConsoleOutput [I] Handle of console screen buffer
1155 * dwCursorPosition [I] New cursor position coordinates
1159 BOOL32 WINAPI
SetConsoleCursorPosition( HANDLE32 hcon
, COORD pos
)
1163 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
1166 FIXME(console
,"(%d,...), no console handle!\n",hcon
);
1169 CONSOLE_make_complex(hcon
,console
);
1170 TRACE(console
, "%d (%dx%d)\n", hcon
, pos
.x
, pos
.y
);
1171 /* x are columns, y rows */
1172 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.y
+1, pos
.x
+1);
1173 /* FIXME: store internal if we start using own console buffers */
1174 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1175 K32OBJ_DecCount(&console
->header
);
1179 /***********************************************************************
1180 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1182 BOOL32 WINAPI
GetNumberOfConsoleInputEvents(HANDLE32 hcon
,LPDWORD nrofevents
)
1184 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
1187 FIXME(console
,"(%d,%p), no console handle!\n",hcon
,nrofevents
);
1190 CONSOLE_get_input(hcon
);
1191 *nrofevents
= console
->nrofirs
;
1192 K32OBJ_DecCount(&console
->header
);
1196 /***********************************************************************
1197 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1199 BOOL32 WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1201 FIXME(console
,"(%p): stub\n", nrofbuttons
);
1206 /***********************************************************************
1207 * PeekConsoleInputA (KERNEL32.550)
1209 * Gets 'cInRecords' first events (or less) from input queue.
1211 * Does not need a complex console.
1213 BOOL32 WINAPI
PeekConsoleInput32A(HANDLE32 hConsoleInput
,
1214 LPINPUT_RECORD pirBuffer
,
1218 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
1221 FIXME(console
,"(%d,%p,%ld,%p), No console handle passed!\n",hConsoleInput
, pirBuffer
, cInRecords
, lpcRead
);
1224 TRACE(console
,"(%d,%p,%ld,%p)\n",hConsoleInput
, pirBuffer
, cInRecords
, lpcRead
);
1225 CONSOLE_get_input(hConsoleInput
);
1226 if (cInRecords
>console
->nrofirs
)
1227 cInRecords
= console
->nrofirs
;
1229 memcpy(pirBuffer
,console
->irs
,cInRecords
*sizeof(INPUT_RECORD
));
1231 *lpcRead
= cInRecords
;
1232 K32OBJ_DecCount(&console
->header
);
1236 /***********************************************************************
1237 * PeekConsoleInputW (KERNEL32.551)
1239 BOOL32 WINAPI
PeekConsoleInput32W(HANDLE32 hConsoleInput
,
1240 LPINPUT_RECORD pirBuffer
,
1244 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1245 return PeekConsoleInput32A(hConsoleInput
,pirBuffer
,cInRecords
,lpcRead
);
1249 /******************************************************************************
1250 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1253 * hcon [I] Handle to console screen buffer
1254 * cinfo [O] Address of cursor information
1260 BOOL32 WINAPI
GetConsoleCursorInfo32( HANDLE32 hcon
,
1261 LPCONSOLE_CURSOR_INFO cinfo
)
1263 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
1266 FIXME(console
, "(%x,%p), no console handle!\n", hcon
, cinfo
);
1269 TRACE(console
, "(%x,%p)\n", hcon
, cinfo
);
1272 *cinfo
= console
->cinfo
;
1273 K32OBJ_DecCount(&console
->header
);
1278 /******************************************************************************
1279 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1285 BOOL32 WINAPI
SetConsoleCursorInfo32(
1286 HANDLE32 hcon
, /* [in] Handle to console screen buffer */
1287 LPCONSOLE_CURSOR_INFO cinfo
) /* [in] Address of cursor information */
1291 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
1293 TRACE(console
, "(%x,%ld,%i): stub\n", hcon
,cinfo
->dwSize
,cinfo
->bVisible
);
1296 CONSOLE_make_complex(hcon
,console
);
1297 sprintf(buf
,"%c[?25%c",27,cinfo
->bVisible
?'h':'l');
1298 WriteFile(hcon
,buf
,strlen(buf
),&xlen
,NULL
);
1299 console
->cinfo
= *cinfo
;
1300 K32OBJ_DecCount(&console
->header
);
1305 /******************************************************************************
1306 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1312 BOOL32 WINAPI
SetConsoleWindowInfo(
1313 HANDLE32 hcon
, /* [in] Handle to console screen buffer */
1314 BOOL32 bAbsolute
, /* [in] Coordinate type flag */
1315 LPSMALL_RECT window
) /* [in] Address of new window rectangle */
1317 FIXME(console
, "(%x,%d,%p): stub\n", hcon
, bAbsolute
, window
);
1322 /******************************************************************************
1323 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1325 * Sets the foreground and background color attributes of characters
1326 * written to the screen buffer.
1332 BOOL32 WINAPI
SetConsoleTextAttribute32(HANDLE32 hConsoleOutput
,WORD wAttr
)
1334 const int colormap
[8] = {
1341 TRACE(console
,"(%d,%d)\n",hConsoleOutput
,wAttr
);
1342 sprintf(buffer
,"%c[0;%s3%d;4%dm",
1344 (wAttr
& FOREGROUND_INTENSITY
)?"1;":"",
1346 colormap
[(wAttr
&0x70)>>4]
1348 WriteFile(hConsoleOutput
,buffer
,strlen(buffer
),&xlen
,NULL
);
1353 /******************************************************************************
1354 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1357 * hConsoleOutput [I] Handle to console screen buffer
1358 * dwSize [I] New size in character rows and cols
1364 BOOL32 WINAPI
SetConsoleScreenBufferSize( HANDLE32 hConsoleOutput
,
1367 FIXME(console
, "(%d,%dx%d): stub\n",hConsoleOutput
,dwSize
.x
,dwSize
.y
);
1372 /******************************************************************************
1373 * FillConsoleOutputCharacterA [KERNEL32.242]
1376 * hConsoleOutput [I] Handle to screen buffer
1377 * cCharacter [I] Character to write
1378 * nLength [I] Number of cells to write to
1379 * dwCoord [I] Coords of first cell
1380 * lpNumCharsWritten [O] Pointer to number of cells written
1386 BOOL32 WINAPI
FillConsoleOutputCharacterA(
1387 HANDLE32 hConsoleOutput
,
1391 LPDWORD lpNumCharsWritten
)
1396 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1397 for(count
=0;count
<nLength
;count
++)
1398 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1399 *lpNumCharsWritten
= nLength
;
1404 /******************************************************************************
1405 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1408 * hConsoleOutput [I] Handle to screen buffer
1409 * cCharacter [I] Character to write
1410 * nLength [I] Number of cells to write to
1411 * dwCoord [I] Coords of first cell
1412 * lpNumCharsWritten [O] Pointer to number of cells written
1418 BOOL32 WINAPI
FillConsoleOutputCharacterW(HANDLE32 hConsoleOutput
,
1422 LPDWORD lpNumCharsWritten
)
1427 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1428 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1431 for(count
=0;count
<nLength
;count
++)
1432 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1433 *lpNumCharsWritten
= nLength
;
1438 /******************************************************************************
1439 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1442 * hConsoleOutput [I] Handle to screen buffer
1443 * wAttribute [I] Color attribute to write
1444 * nLength [I] Number of cells to write to
1445 * dwCoord [I] Coords of first cell
1446 * lpNumAttrsWritten [O] Pointer to number of cells written
1452 BOOL32 WINAPI
FillConsoleOutputAttribute( HANDLE32 hConsoleOutput
,
1453 WORD wAttribute
, DWORD nLength
, COORD dwCoord
,
1454 LPDWORD lpNumAttrsWritten
)
1456 FIXME(console
, "(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput
,
1457 wAttribute
,nLength
,dwCoord
.x
,dwCoord
.y
,lpNumAttrsWritten
);
1458 *lpNumAttrsWritten
= nLength
;
1462 /******************************************************************************
1463 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1468 BOOL32 WINAPI
ReadConsoleOutputCharacter32A(HANDLE32 hConsoleOutput
,
1469 LPSTR lpstr
, DWORD dword
, COORD coord
, LPDWORD lpdword
)
1471 FIXME(console
, "(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput
,lpstr
,
1472 dword
,coord
.x
,coord
.y
,lpdword
);
1473 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1478 /******************************************************************************
1479 * ScrollConsoleScreenBuffer [KERNEL32.612]
1484 BOOL32 WINAPI
ScrollConsoleScreenBuffer( HANDLE32 hConsoleOutput
,
1485 LPSMALL_RECT lpScrollRect
, LPSMALL_RECT lpClipRect
,
1486 COORD dwDestOrigin
, LPCHAR_INFO lpFill
)
1488 FIXME(console
, "(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput
,lpScrollRect
,
1489 lpClipRect
,dwDestOrigin
.x
,dwDestOrigin
.y
,lpFill
);
1490 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);