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 */
65 static void CONSOLE_Destroy( K32OBJ
*obj
);
67 const K32OBJ_OPS CONSOLE_Ops
=
69 CONSOLE_Destroy
/* destroy */
72 /***********************************************************************
75 static void CONSOLE_Destroy(K32OBJ
*obj
)
77 CONSOLE
*console
= (CONSOLE
*)obj
;
78 assert(obj
->type
== K32OBJ_CONSOLE
);
80 obj
->type
= K32OBJ_UNKNOWN
;
83 HeapFree( SystemHeap
, 0, console
->title
);
84 console
->title
= NULL
;
85 /* if there is a xterm, kill it. */
86 if (console
->pid
!= -1)
87 kill(console
->pid
, SIGTERM
);
88 HeapFree(SystemHeap
, 0, console
);
92 /****************************************************************************
93 * CONSOLE_add_input_record [internal]
95 * Adds an INPUT_RECORD to the CONSOLEs input queue.
98 CONSOLE_add_input_record(CONSOLE
*console
,INPUT_RECORD
*inp
) {
99 console
->irs
= HeapReAlloc(GetProcessHeap(),0,console
->irs
,sizeof(INPUT_RECORD
)*(console
->nrofirs
+1));
100 console
->irs
[console
->nrofirs
++]=*inp
;
103 /****************************************************************************
104 * XTERM_string_to_IR [internal]
106 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
107 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
110 CONSOLE_string_to_IR( HANDLE32 hConsoleInput
,unsigned char *buf
,int len
) {
113 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
,
114 K32OBJ_CONSOLE
, 0, NULL
);
116 for (j
=0;j
<len
;j
++) {
117 unsigned char inchar
= buf
[j
];
119 if (inchar
!=27) { /* no escape -> 'normal' keyboard event */
120 ir
.EventType
= 1; /* Key_event */
122 ir
.Event
.KeyEvent
.bKeyDown
= 1;
123 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
125 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
127 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
130 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(inchar
);
131 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0100)
132 ir
.Event
.KeyEvent
.dwControlKeyState
|=SHIFT_PRESSED
;
133 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0200)
134 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_CTRL_PRESSED
;
135 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0400)
136 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
137 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
138 ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x00ff,
139 0 /* VirtualKeyCodes to ScanCode */
142 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
143 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0x0d;
144 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
146 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= inchar
;
148 /* FIXME: find good values for ^X */
149 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
150 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
154 CONSOLE_add_input_record(console
,&ir
);
155 ir
.Event
.KeyEvent
.bKeyDown
= 0;
156 CONSOLE_add_input_record(console
,&ir
);
160 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
161 ir
.EventType
= 1; /* Key_event */
162 ir
.Event
.KeyEvent
.bKeyDown
= 1;
163 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
165 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
166 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
167 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
169 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
170 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
171 CONSOLE_add_input_record(console
,&ir
);
172 ir
.Event
.KeyEvent
.bKeyDown
= 0;
173 CONSOLE_add_input_record(console
,&ir
);
176 for (k
=j
;k
<len
;k
++) {
177 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
178 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
184 int subid
,scancode
=0;
186 ir
.EventType
= 1; /* Key_event */
187 ir
.Event
.KeyEvent
.bKeyDown
= 1;
188 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
189 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
191 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
192 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
193 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
197 sscanf(&buf
[j
+2],"%d",&subid
);
199 case 2:/*INS */scancode
= 0xe052;break;
200 case 3:/*DEL */scancode
= 0xe053;break;
201 case 6:/*PGDW*/scancode
= 0xe051;break;
202 case 5:/*PGUP*/scancode
= 0xe049;break;
203 case 11:/*F1 */scancode
= 0x003b;break;
204 case 12:/*F2 */scancode
= 0x003c;break;
205 case 13:/*F3 */scancode
= 0x003d;break;
206 case 14:/*F4 */scancode
= 0x003e;break;
207 case 15:/*F5 */scancode
= 0x003f;break;
208 case 17:/*F6 */scancode
= 0x0040;break;
209 case 18:/*F7 */scancode
= 0x0041;break;
210 case 19:/*F8 */scancode
= 0x0042;break;
211 case 20:/*F9 */scancode
= 0x0043;break;
212 case 21:/*F10 */scancode
= 0x0044;break;
213 case 23:/*F11 */scancode
= 0x00d9;break;
214 case 24:/*F12 */scancode
= 0x00da;break;
215 /* FIXME: Shift-Fx */
217 FIXME(console
,"parse ESC[%d~\n",subid
);
221 case 'A': /* Cursor Up */scancode
= 0xe048;break;
222 case 'B': /* Cursor Down */scancode
= 0xe050;break;
223 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
224 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
225 case 'F': /* End */scancode
= 0xe04f;break;
226 case 'H': /* Home */scancode
= 0xe047;break;
228 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
229 * Release (ESCM#<x+'!'><y+'!'>
232 ir
.EventType
= MOUSE_EVENT
;
233 ir
.Event
.MouseEvent
.dwMousePosition
.x
= buf
[k
+2]-'!';
234 ir
.Event
.MouseEvent
.dwMousePosition
.y
= buf
[k
+3]-'!';
236 ir
.Event
.MouseEvent
.dwButtonState
= 0;
238 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
239 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
240 CONSOLE_add_input_record(console
,&ir
);
247 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
248 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
249 CONSOLE_add_input_record(console
,&ir
);
250 ir
.Event
.KeyEvent
.bKeyDown
= 0;
251 CONSOLE_add_input_record(console
,&ir
);
257 K32OBJ_DecCount(&console
->header
);
260 /****************************************************************************
261 * CONSOLE_get_input (internal)
263 * Reads (nonblocking) as much input events as possible and stores them
264 * in an internal queue.
267 CONSOLE_get_input( HANDLE32 handle
)
269 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
276 if (WaitForSingleObject( handle
, 0 )) break;
277 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
278 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
281 CONSOLE_string_to_IR(handle
,buf
,len
);
282 HeapFree(GetProcessHeap(),0,buf
);
285 /****************************************************************************
286 * CONSOLE_drain_input (internal)
288 * Drains 'n' console input events from the queue.
291 CONSOLE_drain_input(CONSOLE
*console
,int n
) {
292 assert(n
<=console
->nrofirs
);
295 memcpy( &console
->irs
[0],
297 console
->nrofirs
*sizeof(INPUT_RECORD
)
299 console
->irs
= HeapReAlloc(
303 console
->nrofirs
*sizeof(INPUT_RECORD
)
309 /******************************************************************************
310 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
313 * func [I] Address of handler function
314 * add [I] Handler to add or remove
321 * James Sutherland (JamesSutherland@gmx.de)
322 * Added global variables console_ignore_ctrl_c and handlers[]
323 * Does not yet do any error checking, or set LastError if failed.
324 * This doesn't yet matter, since these handlers are not yet called...!
326 static unsigned int console_ignore_ctrl_c
= 0;
327 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
328 BOOL32 WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL32 add
)
330 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
331 unsigned int done
= 0;
332 FIXME(console
, "(%p,%i) - no error checking or testing yet\n", func
, add
);
335 console_ignore_ctrl_c
= add
;
341 if (!handlers
[alloc_loop
] && !done
)
343 handlers
[alloc_loop
] = func
;
347 FIXME(console
, "Out of space on CtrlHandler table\n");
353 if (handlers
[alloc_loop
] == func
&& !done
)
355 handlers
[alloc_loop
] = 0;
359 WARN(console
, "Attempt to remove non-installed CtrlHandler %p\n",
367 /******************************************************************************
368 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
371 * dwCtrlEvent [I] Type of event
372 * dwProcessGroupID [I] Process group ID to send event to
375 * Doesn't yet work...!
379 * Failure: False (and *should* [but doesn't] set LastError)
381 BOOL32 WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
382 DWORD dwProcessGroupID
)
384 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
386 ERR( console
, "invalid event %d for PGID %ld\n",
387 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
390 if (dwProcessGroupID
== GetCurrentProcessId() )
392 FIXME( console
, "Attempt to send event %d to self - stub\n",
393 (unsigned short)dwCtrlEvent
);
396 FIXME( console
,"event %d to external PGID %ld - not implemented yet\n",
397 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
402 /******************************************************************************
403 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
406 * dwDesiredAccess [I] Access flag
407 * dwShareMode [I] Buffer share mode
408 * sa [I] Security attributes
409 * dwFlags [I] Type of buffer to create
410 * lpScreenBufferData [I] Reserved
413 * Should call SetLastError
416 * Success: Handle to new console screen buffer
417 * Failure: INVALID_HANDLE_VALUE
419 HANDLE32 WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
420 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
421 DWORD dwFlags
, LPVOID lpScreenBufferData
)
423 FIXME(console
, "(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
424 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
425 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
426 return INVALID_HANDLE_VALUE32
;
430 /***********************************************************************
431 * GetConsoleScreenBufferInfo (KERNEL32.190)
433 BOOL32 WINAPI
GetConsoleScreenBufferInfo( HANDLE32 hConsoleOutput
,
434 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
438 csbi
->dwCursorPosition
.x
= 0;
439 csbi
->dwCursorPosition
.y
= 0;
440 csbi
->wAttributes
= 0;
441 csbi
->srWindow
.Left
= 0;
442 csbi
->srWindow
.Right
= 79;
443 csbi
->srWindow
.Top
= 0;
444 csbi
->srWindow
.Bottom
= 23;
445 csbi
->dwMaximumWindowSize
.x
= 80;
446 csbi
->dwMaximumWindowSize
.y
= 24;
451 /******************************************************************************
452 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
458 BOOL32 WINAPI
SetConsoleActiveScreenBuffer(
459 HANDLE32 hConsoleOutput
) /* [in] Handle to console screen buffer */
461 FIXME(console
, "(%x): stub\n", hConsoleOutput
);
466 /***********************************************************************
467 * GetLargestConsoleWindowSize (KERNEL32.226)
469 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE32 hConsoleOutput
)
471 return (DWORD
)MAKELONG(80,24);
474 /***********************************************************************
475 * FreeConsole (KERNEL32.267)
477 BOOL32 WINAPI
FreeConsole(VOID
)
480 PDB32
*pdb
= PROCESS_Current();
485 console
= (CONSOLE
*)pdb
->console
;
487 if (console
== NULL
) {
488 SetLastError(ERROR_INVALID_PARAMETER
);
492 HANDLE_CloseAll( pdb
, &console
->header
);
494 K32OBJ_DecCount( &console
->header
);
502 * It looks like the openpty that comes with glibc in RedHat 5.0
503 * is buggy (second call returns what looks like a dup of 0 and 1
504 * instead of a new pty), this is a generic replacement.
506 static int CONSOLE_openpty(CONSOLE
*console
, char *name
,
507 struct termios
*term
, struct winsize
*winsize
)
510 struct set_console_fd_request req
;
512 temp
= wine_openpty(&console
->master
, &slave
, name
, term
,
514 console
->infd
= console
->outfd
= slave
;
516 req
.handle
= console
->hread
;
517 CLIENT_SendRequest(REQ_SET_CONSOLE_FD
, dup(slave
), 1,
519 CLIENT_WaitReply( NULL
, NULL
, 0);
520 req
.handle
= console
->hwrite
;
521 CLIENT_SendRequest( REQ_SET_CONSOLE_FD
, dup(slave
), 1,
523 CLIENT_WaitReply( NULL
, NULL
, 0);
525 return temp
; /* The same result as the openpty call */
528 /*************************************************************************
529 * CONSOLE_make_complex [internal]
531 * Turns a CONSOLE kernel object into a complex one.
532 * (switches from output/input using the terminal where WINE was started to
535 * This makes simple commandline tools pipeable, while complex commandline
536 * tools work without getting messed up by debugoutput.
538 * All other functions should work indedependend from this call.
540 * To test for complex console: pid == -1 -> simple, otherwise complex.
542 static BOOL32
CONSOLE_make_complex(HANDLE32 handle
,CONSOLE
*console
)
551 if (console
->pid
!= -1)
552 return TRUE
; /* already complex */
554 MSG("Console: Making console complex (creating an xterm)...\n");
556 if (tcgetattr(0, &term
) < 0) return FALSE
;
557 term
.c_lflag
= ~(ECHO
|ICANON
);
558 if (CONSOLE_openpty(console
, NULL
, &term
, NULL
) < 0) return FALSE
;
560 if ((xpid
=fork()) == 0) {
561 tcsetattr(console
->infd
, TCSADRAIN
, &term
);
562 sprintf(buf
, "-Sxx%d", console
->master
);
563 /* "-fn vga" for VGA font. Harmless if vga is not present:
564 * xterm: unable to open font "vga", trying "fixed"....
566 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
567 ERR(console
, "error creating AllocConsole xterm\n");
572 /* most xterms like to print their window ID when used with -S;
573 * read it and continue before the user has a chance...
575 for (i
=0; c
!='\n'; (status
=read(console
->infd
, &c
, 1)), i
++) {
576 if (status
== -1 && c
== '\0') {
577 /* wait for xterm to be created */
581 ERR(console
, "can't read xterm WID\n");
582 kill(console
->pid
, SIGKILL
);
586 /* enable mouseclicks */
587 sprintf(buf
,"%c[?1001s%c[?1000h",27,27);
588 WriteFile(handle
,buf
,strlen(buf
),&xlen
,NULL
);
589 if (console
->title
) {
590 title
= HeapAlloc(GetProcessHeap(),0,strlen("\033]2;\a")+1+strlen(console
->title
));
591 sprintf(title
,"\033]2;%s\a",console
->title
);
592 WriteFile(handle
,title
,strlen(title
),&xlen
,NULL
);
593 HeapFree(GetProcessHeap(),0,title
);
599 /***********************************************************************
600 * CONSOLE_GetConsoleHandle
601 * returns a 16-bit style console handle
602 * note: only called from _lopen
604 HFILE32
CONSOLE_GetConsoleHandle(VOID
)
606 PDB32
*pdb
= PROCESS_Current();
607 HFILE32 handle
= HFILE_ERROR32
;
610 if (pdb
->console
!= NULL
) {
611 CONSOLE
*console
= (CONSOLE
*)pdb
->console
;
612 handle
= (HFILE32
)HANDLE_Alloc(pdb
, &console
->header
, 0, TRUE
, -1);
618 /***********************************************************************
619 * AllocConsole (KERNEL32.103)
621 * creates an xterm with a pty to our program
623 BOOL32 WINAPI
AllocConsole(VOID
)
625 struct create_console_request req
;
626 struct create_console_reply reply
;
627 PDB32
*pdb
= PROCESS_Current();
629 HANDLE32 hIn
, hOut
, hErr
;
631 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
633 SetLastError(ERROR_CANNOT_MAKE
); /* this might not be the right
634 error, but it's a good guess :) */
636 console
= (CONSOLE
*)pdb
->console
;
638 /* don't create a console if we already have one */
639 if (console
!= NULL
) {
640 SetLastError(ERROR_ACCESS_DENIED
);
645 if (!(console
= (CONSOLE
*)HeapAlloc( SystemHeap
, 0, sizeof(*console
))))
651 console
->header
.type
= K32OBJ_CONSOLE
;
652 console
->header
.refcount
= 1;
654 console
->title
= NULL
;
655 console
->nrofirs
= 0;
656 console
->irs
= HeapAlloc(GetProcessHeap(),0,1);;
657 console
->mode
= ENABLE_PROCESSED_INPUT
660 /* FIXME: we shouldn't probably use hardcoded UNIX values here. */
663 console
->hread
= console
->hwrite
= -1;
665 CLIENT_SendRequest( REQ_CREATE_CONSOLE
, -1, 1, &req
, sizeof(req
) );
666 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
) != ERROR_SUCCESS
)
668 K32OBJ_DecCount(&console
->header
);
672 console
->hread
= reply
.handle_read
;
673 console
->hwrite
= reply
.handle_write
;
675 if ((hIn
= HANDLE_Alloc(pdb
,&console
->header
, 0, TRUE
,
676 reply
.handle_read
)) == INVALID_HANDLE_VALUE32
)
678 CLIENT_CloseHandle( reply
.handle_write
);
679 K32OBJ_DecCount(&console
->header
);
684 if ((hOut
= HANDLE_Alloc(pdb
,&console
->header
, 0, TRUE
,
685 reply
.handle_write
)) == INVALID_HANDLE_VALUE32
)
688 K32OBJ_DecCount(&console
->header
);
693 if (!DuplicateHandle( GetCurrentProcess(), hOut
,
694 GetCurrentProcess(), &hErr
,
695 0, TRUE
, DUPLICATE_SAME_ACCESS
))
699 K32OBJ_DecCount(&console
->header
);
704 if (pdb
->console
) K32OBJ_DecCount( pdb
->console
);
705 pdb
->console
= (K32OBJ
*)console
;
706 K32OBJ_IncCount( pdb
->console
);
708 /* NT resets the STD_*_HANDLEs on console alloc */
709 SetStdHandle(STD_INPUT_HANDLE
, hIn
);
710 SetStdHandle(STD_OUTPUT_HANDLE
, hOut
);
711 SetStdHandle(STD_ERROR_HANDLE
, hErr
);
713 SetLastError(ERROR_SUCCESS
);
715 SetConsoleTitle32A("Wine Console");
720 /******************************************************************************
721 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
726 UINT32 WINAPI
GetConsoleCP(VOID
)
732 /***********************************************************************
733 * GetConsoleOutputCP (KERNEL32.189)
735 UINT32 WINAPI
GetConsoleOutputCP(VOID
)
737 return GetConsoleCP();
740 /***********************************************************************
741 * GetConsoleMode (KERNEL32.188)
743 BOOL32 WINAPI
GetConsoleMode(HANDLE32 hcon
,LPDWORD mode
)
745 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
748 FIXME(console
,"(%d,%p), no console handle passed!\n",hcon
,mode
);
751 *mode
= console
->mode
;
752 K32OBJ_DecCount(&console
->header
);
757 /******************************************************************************
758 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
761 * hcon [I] Handle to console input or screen buffer
762 * mode [I] Input or output mode to set
768 BOOL32 WINAPI
SetConsoleMode( HANDLE32 hcon
, DWORD mode
)
770 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
773 FIXME(console
,"(%d,%ld), no console handle passed!\n",hcon
,mode
);
776 FIXME(console
,"(0x%08x,0x%08lx): stub\n",hcon
,mode
);
777 console
->mode
= mode
;
778 K32OBJ_DecCount(&console
->header
);
783 /***********************************************************************
784 * GetConsoleTitleA (KERNEL32.191)
786 DWORD WINAPI
GetConsoleTitle32A(LPSTR title
,DWORD size
)
788 PDB32
*pdb
= PROCESS_Current();
789 CONSOLE
*console
= (CONSOLE
*)pdb
->console
;
791 if(console
&& console
->title
) {
792 lstrcpyn32A(title
,console
->title
,size
);
793 return strlen(title
);
799 /******************************************************************************
800 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
803 * title [O] Address of buffer for title
804 * size [I] Size of buffer
807 * Success: Length of string copied
810 DWORD WINAPI
GetConsoleTitle32W( LPWSTR title
, DWORD size
)
812 PDB32
*pdb
= PROCESS_Current();
813 CONSOLE
*console
= (CONSOLE
*)pdb
->console
;
814 if(console
&& console
->title
)
816 lstrcpynAtoW(title
,console
->title
,size
);
817 return (lstrlen32W(title
));
823 /***********************************************************************
824 * WriteConsoleA (KERNEL32.729)
826 BOOL32 WINAPI
WriteConsole32A( HANDLE32 hConsoleOutput
,
828 DWORD nNumberOfCharsToWrite
,
829 LPDWORD lpNumberOfCharsWritten
,
832 /* FIXME: should I check if this is a console handle? */
833 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
834 lpNumberOfCharsWritten
, NULL
);
839 if (bufused==curbufsize-1) \
840 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
842 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
844 /***********************************************************************
845 * WriteConsoleOutputA (KERNEL32.732)
847 BOOL32 WINAPI
WriteConsoleOutput32A( HANDLE32 hConsoleOutput
,
848 LPCHAR_INFO lpBuffer
,
851 LPSMALL_RECT lpWriteRegion
)
853 int i
,j
,off
=0,lastattr
=-1;
854 char sbuf
[20],*buffer
=NULL
;
855 int bufused
=0,curbufsize
= 100;
857 const int colormap
[8] = {
861 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleOutput
, K32OBJ_CONSOLE
, 0, NULL
);
864 FIXME(console
,"(%d,...): no console handle!\n",hConsoleOutput
);
867 CONSOLE_make_complex(hConsoleOutput
,console
);
868 buffer
= HeapAlloc(GetProcessHeap(),0,100);;
871 TRACE(console
,"wr: top = %d, bottom=%d, left=%d,right=%d\n",
873 lpWriteRegion
->Bottom
,
878 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
879 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
881 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
882 if (lastattr
!=lpBuffer
[off
].Attributes
) {
883 lastattr
= lpBuffer
[off
].Attributes
;
884 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
886 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
887 colormap
[lastattr
&7],
888 colormap
[(lastattr
&0x70)>>4]
890 /* FIXME: BACKGROUND_INTENSITY */
893 CADD(lpBuffer
[off
].Char
.AsciiChar
);
897 sprintf(sbuf
,"%c[0m",27);SADD(sbuf
);
898 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
899 HeapFree(GetProcessHeap(),0,buffer
);
900 K32OBJ_DecCount(&console
->header
);
904 /***********************************************************************
905 * WriteConsoleW (KERNEL32.577)
907 BOOL32 WINAPI
WriteConsole32W( HANDLE32 hConsoleOutput
,
909 DWORD nNumberOfCharsToWrite
,
910 LPDWORD lpNumberOfCharsWritten
,
914 LPSTR xstring
=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite
);
916 lstrcpynWtoA( xstring
, lpBuffer
,nNumberOfCharsToWrite
);
918 /* FIXME: should I check if this is a console handle? */
919 ret
= WriteFile(hConsoleOutput
, xstring
, nNumberOfCharsToWrite
,
920 lpNumberOfCharsWritten
, NULL
);
921 HeapFree( GetProcessHeap(), 0, xstring
);
926 /***********************************************************************
927 * ReadConsoleA (KERNEL32.419)
929 BOOL32 WINAPI
ReadConsole32A( HANDLE32 hConsoleInput
,
931 DWORD nNumberOfCharsToRead
,
932 LPDWORD lpNumberOfCharsRead
,
935 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
937 LPSTR xbuf
= (LPSTR
)lpBuffer
;
940 SetLastError(ERROR_INVALID_HANDLE
);
941 FIXME(console
,"(%d,...), no console handle!\n",hConsoleInput
);
944 TRACE(console
,"(%d,%p,%ld,%p,%p)\n",
945 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
946 lpNumberOfCharsRead
,lpReserved
948 CONSOLE_get_input(hConsoleInput
);
950 /* FIXME: should we read at least 1 char? The SDK does not say */
951 for (i
=0;(i
<console
->nrofirs
)&&(charsread
<nNumberOfCharsToRead
);i
++) {
952 if (console
->irs
[i
].EventType
!= KEY_EVENT
)
954 if (!console
->irs
[i
].Event
.KeyEvent
.bKeyDown
)
956 *xbuf
++ = console
->irs
[i
].Event
.KeyEvent
.uChar
.AsciiChar
;
959 /* SDK says: Drains all other input events from queue. */
960 CONSOLE_drain_input(console
,i
);
961 if (lpNumberOfCharsRead
)
962 *lpNumberOfCharsRead
= charsread
;
963 K32OBJ_DecCount(&console
->header
);
967 /***********************************************************************
968 * ReadConsoleW (KERNEL32.427)
970 BOOL32 WINAPI
ReadConsole32W( HANDLE32 hConsoleInput
,
972 DWORD nNumberOfCharsToRead
,
973 LPDWORD lpNumberOfCharsRead
,
977 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
979 ret
= ReadConsole32A(
982 nNumberOfCharsToRead
,
987 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
988 HeapFree( GetProcessHeap(), 0, buf
);
993 /******************************************************************************
994 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
997 * hConsoleInput [I] Handle to console input buffer
998 * lpBuffer [O] Address of buffer for read data
999 * nLength [I] Number of records to read
1000 * lpNumberOfEventsRead [O] Address of number of records read
1006 BOOL32 WINAPI
ReadConsoleInput32A(HANDLE32 hConsoleInput
,
1007 LPINPUT_RECORD lpBuffer
,
1008 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
1010 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
1012 TRACE(console
, "(%d,%p,%ld,%p)\n",hConsoleInput
, lpBuffer
, nLength
,
1013 lpNumberOfEventsRead
);
1015 FIXME(console
, "(%d,%p,%ld,%p), No console handle!\n",hConsoleInput
,
1016 lpBuffer
, nLength
, lpNumberOfEventsRead
);
1018 /* Indicate that nothing was read */
1019 *lpNumberOfEventsRead
= 0;
1023 CONSOLE_get_input(hConsoleInput
);
1024 /* SDK: return at least 1 input record */
1025 while (!console
->nrofirs
) {
1028 res
=WaitForSingleObject(hConsoleInput
,0);
1030 case STATUS_TIMEOUT
: continue;
1031 case 0: break; /*ok*/
1032 case WAIT_FAILED
: return 0;/*FIXME: SetLastError?*/
1033 default: break; /*hmm*/
1035 CONSOLE_get_input(hConsoleInput
);
1038 if (nLength
>console
->nrofirs
)
1039 nLength
= console
->nrofirs
;
1040 memcpy(lpBuffer
,console
->irs
,sizeof(INPUT_RECORD
)*nLength
);
1041 if (lpNumberOfEventsRead
)
1042 *lpNumberOfEventsRead
= nLength
;
1043 CONSOLE_drain_input(console
,nLength
);
1044 K32OBJ_DecCount(&console
->header
);
1048 /***********************************************************************
1049 * SetConsoleTitle32A (KERNEL32.476)
1051 * Sets the console title.
1053 * We do not necessarily need to create a complex console for that,
1054 * but should remember the title and set it on creation of the latter.
1055 * (not fixed at this time).
1057 BOOL32 WINAPI
SetConsoleTitle32A(LPCSTR title
)
1059 PDB32
*pdb
= PROCESS_Current();
1062 char titleformat
[]="\033]2;%s\a"; /*this should work for xterms*/
1066 TRACE(console
,"(%s)\n",title
);
1068 console
= (CONSOLE
*)pdb
->console
;
1071 if(console
->title
) /* Free old title, if there is one */
1072 HeapFree( SystemHeap
, 0, console
->title
);
1073 console
->title
= (LPSTR
)HeapAlloc(SystemHeap
, 0,strlen(title
)+1);
1074 if(console
->title
) strcpy(console
->title
,title
);
1075 titlestring
= HeapAlloc(GetProcessHeap(), 0,strlen(title
)+strlen(titleformat
)+1);
1077 K32OBJ_DecCount(&console
->header
);
1081 sprintf(titlestring
,titleformat
,title
);
1082 /* only set title for complex console (own xterm) */
1083 if (console
->pid
!= -1) {
1084 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
),titlestring
,strlen(titlestring
),&written
,NULL
);
1085 if (written
== strlen(titlestring
))
1089 HeapFree( GetProcessHeap(), 0, titlestring
);
1090 K32OBJ_DecCount(&console
->header
);
1095 /******************************************************************************
1096 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1099 * title [I] Address of new title
1102 * This should not be calling the A version
1108 BOOL32 WINAPI
SetConsoleTitle32W( LPCWSTR title
)
1112 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1113 ret
= SetConsoleTitle32A(titleA
);
1114 HeapFree( GetProcessHeap(), 0, titleA
);
1118 /***********************************************************************
1119 * ReadConsoleInput32W (KERNEL32.570)
1121 BOOL32 WINAPI
ReadConsoleInput32W(HANDLE32 hConsoleInput
,
1122 LPINPUT_RECORD lpBuffer
,
1123 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
1125 FIXME(console
, "(%d,%p,%ld,%p): stub\n",hConsoleInput
, lpBuffer
, nLength
,
1126 lpNumberOfEventsRead
);
1130 /***********************************************************************
1131 * FlushConsoleInputBuffer (KERNEL32.132)
1133 BOOL32 WINAPI
FlushConsoleInputBuffer(HANDLE32 hConsoleInput
)
1135 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
1139 CONSOLE_drain_input(console
,console
->nrofirs
);
1140 K32OBJ_DecCount(&console
->header
);
1145 /******************************************************************************
1146 * SetConsoleCursorPosition [KERNEL32.627]
1147 * Sets the cursor position in console
1150 * hConsoleOutput [I] Handle of console screen buffer
1151 * dwCursorPosition [I] New cursor position coordinates
1155 BOOL32 WINAPI
SetConsoleCursorPosition( HANDLE32 hcon
, COORD pos
)
1159 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
1162 FIXME(console
,"(%d,...), no console handle!\n",hcon
);
1165 CONSOLE_make_complex(hcon
,console
);
1166 TRACE(console
, "%d (%dx%d)\n", hcon
, pos
.x
, pos
.y
);
1167 /* x are columns, y rows */
1168 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.y
+1, pos
.x
+1);
1169 /* FIXME: store internal if we start using own console buffers */
1170 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1171 K32OBJ_DecCount(&console
->header
);
1175 /***********************************************************************
1176 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1178 BOOL32 WINAPI
GetNumberOfConsoleInputEvents(HANDLE32 hcon
,LPDWORD nrofevents
)
1180 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hcon
, K32OBJ_CONSOLE
, 0, NULL
);
1183 FIXME(console
,"(%d,%p), no console handle!\n",hcon
,nrofevents
);
1186 CONSOLE_get_input(hcon
);
1187 *nrofevents
= console
->nrofirs
;
1188 K32OBJ_DecCount(&console
->header
);
1192 /***********************************************************************
1193 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1195 BOOL32 WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1197 FIXME(console
,"(%p): stub\n", nrofbuttons
);
1202 /***********************************************************************
1203 * PeekConsoleInputA (KERNEL32.550)
1205 * Gets 'cInRecords' first events (or less) from input queue.
1207 * Does not need a complex console.
1209 BOOL32 WINAPI
PeekConsoleInput32A(HANDLE32 hConsoleInput
,
1210 LPINPUT_RECORD pirBuffer
,
1214 CONSOLE
*console
= (CONSOLE
*)HANDLE_GetObjPtr( PROCESS_Current(), hConsoleInput
, K32OBJ_CONSOLE
, 0, NULL
);
1217 FIXME(console
,"(%d,%p,%ld,%p), No console handle passed!\n",hConsoleInput
, pirBuffer
, cInRecords
, lpcRead
);
1219 /* Indicate that nothing was read */
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
);