2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
31 static CONSOLE_SCREEN_BUFFER_INFO dummyinfo
=
40 /* The console -- I chose to keep the master and slave
41 * (UNIX) file descriptors around in case they are needed for
42 * ioctls later. The pid is needed to detroy the xterm if needed.
44 typedef struct _CONSOLE
{
46 int master
; /* xterm side of pty */
47 int slave
; /* wine side of pty */
48 int pid
; /* xterm's pid, -1 if no xterm */
49 int flags
; /* CONSOLE_STARTED_FROM */
50 K32OBJ
*file_in
; /* console input */
51 K32OBJ
*file_out
; /* console output */
52 K32OBJ
*file_err
; /* console error */
55 #define CONSOLE_STARTED_FROM (0x1) /* FIXME: this is lame, it should have
56 something to do with sharing... */
58 static void CONSOLE_Destroy( K32OBJ
*obj
);
60 const K32OBJ_OPS CONSOLE_Ops
=
65 NULL
, /* remove_wait */
66 CONSOLE_Destroy
/* destroy */
70 static int wine_openpty(int *master
, int *slave
, char *name
,
71 struct termios
*term
, struct winsize
*winsize
);
75 static void CONSOLE_Destroy(K32OBJ
*obj
)
77 CONSOLE
*console
= (CONSOLE
*)obj
;
78 assert(obj
->type
== K32OBJ_CONSOLE
);
80 obj
->type
= K32OBJ_UNKNOWN
;
82 /* make sure a xterm exists to kill */
83 if (console
->pid
!= -1) {
84 kill(console
->pid
, SIGTERM
);
86 HeapFree(SystemHeap
, 0, console
);
91 /***********************************************************************
92 * SetConsoleCtrlHandler (KERNEL32.459)
94 BOOL32 WINAPI
SetConsoleCtrlHandler(HANDLER_ROUTINE
* func
, BOOL32 a
)
99 /***********************************************************************
100 * CreateConsoleScreenBuffer (KERNEL32.151)
102 HANDLE32 WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
104 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
106 LPVOID lpScreenBufferData
)
108 fprintf(stderr
, "CreateConsoleScreenBuffer(): stub !\n");
109 return INVALID_HANDLE_VALUE32
;
112 /***********************************************************************
113 * GetConsoleScreenBufferInfo (KERNEL32.190)
115 BOOL32 WINAPI
GetConsoleScreenBufferInfo( HANDLE32 hConsoleOutput
,
116 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
120 csbi
->dwCursorPosition
.x
= 0;
121 csbi
->dwCursorPosition
.y
= 0;
122 csbi
->wAttributes
= 0;
123 csbi
->srWindow
.Left
= 0;
124 csbi
->srWindow
.Right
= 79;
125 csbi
->srWindow
.Top
= 0;
126 csbi
->srWindow
.Bottom
= 23;
127 csbi
->dwMaximumWindowSize
.x
= 80;
128 csbi
->dwMaximumWindowSize
.y
= 24;
132 /***********************************************************************
133 * SetConsoleActiveScreenBuffer (KERNEL32.623)
135 BOOL32 WINAPI
SetConsoleActiveScreenBuffer(HANDLE32 hConsoleOutput
)
137 fprintf(stderr
, "SetConsoleActiveScreenBuffer(): stub !\n");
141 /***********************************************************************
142 * GetLargestConsoleWindowSize (KERNEL32.226)
144 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE32 hConsoleOutput
)
146 return (DWORD
)MAKELONG(dummyinfo
.dwMaximumWindowSize
.x
,dummyinfo
.dwMaximumWindowSize
.y
);
149 /***********************************************************************
150 * FreeConsole (KERNEL32.267)
152 BOOL32 WINAPI
FreeConsole(VOID
)
155 PDB32
*pdb
= PROCESS_Current();
160 console
= (CONSOLE
*)pdb
->console
;
162 if (console
== NULL
) {
163 SetLastError(ERROR_INVALID_PARAMETER
);
167 if (console
->file_in
) K32OBJ_DecCount( console
->file_in
);
168 if (console
->file_out
) K32OBJ_DecCount( console
->file_out
);
169 if (console
->file_err
) K32OBJ_DecCount( console
->file_err
);
170 K32OBJ_DecCount( &console
->header
);
178 * It looks like the openpty that comes with glibc in RedHat 5.0
179 * is buggy (second call returns what looks like a dup of 0 and 1
180 * instead of a new pty), this is a generic replacement.
182 static int wine_openpty(int *master
, int *slave
, char *name
,
183 struct termios
*term
, struct winsize
*winsize
)
189 strcpy (pts_name
, "/dev/ptyXY");
191 for (ptr1
= "pqrstuvwxyzPQRST"; *ptr1
!= 0; ptr1
++) {
193 for (ptr2
= "0123456789abcdef"; *ptr2
!= 0; ptr2
++) {
196 if ((fdm
= open(pts_name
, O_RDWR
)) < 0) {
203 if ((fds
= open(pts_name
, O_RDWR
)) < 0) {
211 tcsetattr(*slave
, TCSANOW
, term
);
213 ioctl(*slave
, TIOCSWINSZ
, winsize
);
215 strcpy(name
, pts_name
);
222 static BOOL32
wine_createConsole(int *master
, int *slave
, int *pid
)
230 if (tcgetattr(0, &term
) < 0) return FALSE
;
231 term
.c_lflag
|= ICANON
;
232 term
.c_lflag
&= ~ECHO
;
233 if (wine_openpty(master
, slave
, NULL
, &term
, NULL
) < 0) return FALSE
;
235 if ((*pid
=fork()) == 0) {
236 tcsetattr(*slave
, TCSADRAIN
, &term
);
237 sprintf(buf
, "-Sxx%d", *master
);
238 execlp("xterm", "xterm", buf
, NULL
);
239 fprintf(stderr
, "error creating AllocConsole xterm\n");
243 /* most xterms like to print their window ID when used with -S;
244 * read it and continue before the user has a chance...
245 * NOTE: this is the reason we started xterm with ECHO off,
246 * we'll turn it back on below
249 for (i
=0; c
!='\n'; (status
=read(*slave
, &c
, 1)), i
++) {
250 if (status
== -1 && c
== '\0') {
251 /* wait for xterm to be created */
255 fprintf(stderr
, "can't read xterm WID\n");
260 term
.c_lflag
|= ECHO
;
261 tcsetattr(*master
, TCSADRAIN
, &term
);
268 /***********************************************************************
269 * AllocConsole (KERNEL32.103)
271 * creates an xterm with a pty to our program
273 BOOL32 WINAPI
AllocConsole(VOID
)
279 PDB32
*pdb
= PROCESS_Current();
281 HANDLE32 hIn
, hOut
, hErr
;
283 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
285 SetLastError(ERROR_CANNOT_MAKE
); /* this might not be the right
286 error, but it's a good guess :) */
288 console
= (CONSOLE
*)pdb
->console
;
290 /* we only want to be able to open a console if the process doesn't have one
291 * now or we got the one we have from our parent
292 * - invalid handle comes from when the console was closed via FreeConsole()
293 * - CONSOLE_STARTED_FROM is when this process inherits its console from
296 if (console
&& (console
->flags
& CONSOLE_STARTED_FROM
) == 0) {
297 SetLastError(ERROR_ACCESS_DENIED
);
302 if (!(console
= (CONSOLE
*)HeapAlloc( SystemHeap
, 0, sizeof(*console
))))
308 console
->header
.type
= K32OBJ_CONSOLE
;
309 console
->header
.refcount
= 1;
311 console
->file_in
= NULL
;
312 console
->file_out
= NULL
;
313 console
->file_err
= NULL
;
315 if (wine_createConsole(&master
, &slave
, &pid
) == FALSE
) {
316 K32OBJ_DecCount(&console
->header
);
321 /* save the pid and other info for future use */
322 console
->master
= master
;
323 console
->slave
= slave
;
327 if ((hIn
= FILE_DupUnixHandle(slave
)) == INVALID_HANDLE_VALUE32
)
329 K32OBJ_DecCount(&console
->header
);
333 FILE_SetFileType(hIn
, FILE_TYPE_CHAR
);
335 if ((hOut
= FILE_DupUnixHandle(slave
)) == INVALID_HANDLE_VALUE32
)
338 K32OBJ_DecCount(&console
->header
);
342 FILE_SetFileType(hOut
, FILE_TYPE_CHAR
);
344 if ((hErr
= FILE_DupUnixHandle(slave
)) == INVALID_HANDLE_VALUE32
)
348 K32OBJ_DecCount(&console
->header
);
352 FILE_SetFileType(hErr
, FILE_TYPE_CHAR
);
354 console
->file_in
= HANDLE_GetObjPtr( hIn
, K32OBJ_FILE
, 0 /*FIXME*/ );
355 console
->file_out
= HANDLE_GetObjPtr( hIn
, K32OBJ_FILE
, 0 /*FIXME*/ );
356 console
->file_err
= HANDLE_GetObjPtr( hIn
, K32OBJ_FILE
, 0 /*FIXME*/ );
358 /* associate this console with the process */
359 if (pdb
->console
) K32OBJ_DecCount( pdb
->console
);
360 pdb
->console
= (K32OBJ
*)console
;
362 /* NT resets the STD_*_HANDLEs on console alloc */
363 SetStdHandle(STD_INPUT_HANDLE
, hIn
);
364 SetStdHandle(STD_OUTPUT_HANDLE
, hOut
);
365 SetStdHandle(STD_ERROR_HANDLE
, hErr
);
367 SetLastError(ERROR_SUCCESS
);
373 /***********************************************************************
374 * GetConsoleCP (KERNEL32.226)
376 UINT32 WINAPI
GetConsoleCP(VOID
)
381 /***********************************************************************
382 * GetConsoleOutputCP (KERNEL32.189)
384 UINT32 WINAPI
GetConsoleOutputCP(VOID
)
386 return GetConsoleCP();
389 /***********************************************************************
390 * GetConsoleMode (KERNEL32.188)
392 BOOL32 WINAPI
GetConsoleMode(HANDLE32 hcon
,LPDWORD mode
)
394 *mode
= ENABLE_PROCESSED_INPUT
|
400 /***********************************************************************
401 * SetConsoleMode (KERNEL32.188)
403 BOOL32 WINAPI
SetConsoleMode(HANDLE32 hcon
,DWORD mode
)
405 fprintf(stdnimp
,"SetConsoleMode(%08x,%08lx)\n",hcon
,mode
);
409 /***********************************************************************
410 * GetConsoleTitleA (KERNEL32.191)
412 DWORD WINAPI
GetConsoleTitle32A(LPSTR title
,DWORD size
)
414 lstrcpyn32A(title
,"Console",size
);
415 return strlen("Console");
418 /***********************************************************************
419 * GetConsoleTitleW (KERNEL32.192)
421 DWORD WINAPI
GetConsoleTitle32W(LPWSTR title
,DWORD size
)
423 lstrcpynAtoW(title
,"Console",size
);
424 return strlen("Console");
427 /***********************************************************************
428 * WriteConsoleA (KERNEL32.729)
430 BOOL32 WINAPI
WriteConsole32A( HANDLE32 hConsoleOutput
,
432 DWORD nNumberOfCharsToWrite
,
433 LPDWORD lpNumberOfCharsWritten
,
436 /* FIXME: should I check if this is a console handle? */
437 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
438 lpNumberOfCharsWritten
, NULL
);
441 *lpNumberOfCharsWritten
= fprintf(CONSOLE_console
.conIO
, "%.*s",
442 (int)nNumberOfCharsToWrite
,
444 if (ferror(CONSOLE_console
.conIO
) {
453 /***********************************************************************
454 * WriteConsoleOutputA (KERNEL32.732)
456 BOOL32 WINAPI
WriteConsoleOutput32A( HANDLE32 hConsoleOutput
,
457 LPCHAR_INFO lpBuffer
,
460 LPSMALL_RECT lpWriteRegion
)
465 /***********************************************************************
466 * WriteConsoleW (KERNEL32.577)
468 BOOL32 WINAPI
WriteConsole32W( HANDLE32 hConsoleOutput
,
470 DWORD nNumberOfCharsToWrite
,
471 LPDWORD lpNumberOfCharsWritten
,
475 /* FIXME: should I check if this is a console handle? */
476 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
477 lpNumberOfCharsWritten
, NULL
);
481 LPSTR buf
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpBuffer
);
482 *lpNumberOfCharsWritten
= fprintf(CONSOLE_console
.conIO
, "%.*s",
483 (int)nNumberOfCharsToWrite
, buf
);
484 HeapFree( GetProcessHeap(), 0, buf
);
486 if (ferror(CONSOLE_console
.conIO
) {
495 /***********************************************************************
496 * ReadConsoleA (KERNEL32.419)
498 BOOL32 WINAPI
ReadConsole32A( HANDLE32 hConsoleInput
,
500 DWORD nNumberOfCharsToRead
,
501 LPDWORD lpNumberOfCharsRead
,
504 return ReadFile(hConsoleInput
, lpBuffer
, nNumberOfCharsToRead
,
505 lpNumberOfCharsRead
, NULL
);
508 fgets(lpBuffer
,nNumberOfCharsToRead
, CONSOLE_console
.conIO
);
509 if (ferror(CONSOLE_console
.conIO
) {
513 *lpNumberOfCharsRead
= strlen(lpBuffer
);
519 /***********************************************************************
520 * ReadConsoleW (KERNEL32.427)
522 BOOL32 WINAPI
ReadConsole32W( HANDLE32 hConsoleInput
,
524 DWORD nNumberOfCharsToRead
,
525 LPDWORD lpNumberOfCharsRead
,
528 return ReadFile(hConsoleInput
, lpBuffer
, nNumberOfCharsToRead
,
529 lpNumberOfCharsRead
, NULL
);
532 LPSTR buf
= (LPSTR
)HEAP_xalloc(GetProcessHeap(), 0,
533 nNumberOfCharsToRead
);
534 fgets(buf
, nNumberOfCharsToRead
, CONSOLE_console
.conIO
);
536 if (ferror(CONSOLE_console
.conIO
) {
537 HeapFree( GetProcessHeap(), 0, buf
);
542 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
543 *lpNumberOfCharsRead
= strlen(buf
);
544 HeapFree( GetProcessHeap(), 0, buf
);
550 /***********************************************************************
551 * SetConsoleTitle32A (KERNEL32.476)
553 BOOL32 WINAPI
SetConsoleTitle32A(LPCSTR title
)
555 fprintf(stderr
,"SetConsoleTitle(%s)\n",title
);
559 /***********************************************************************
560 * SetConsoleTitle32W (KERNEL32.477)
562 BOOL32 WINAPI
SetConsoleTitle32W( LPCWSTR title
)
564 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
565 fprintf(stderr
,"SetConsoleTitle(%s)\n",titleA
);
566 HeapFree( GetProcessHeap(), 0, titleA
);
570 /***********************************************************************
571 * FlushConsoleInputBuffer (KERNEL32.132)
573 BOOL32 WINAPI
FlushConsoleInputBuffer(HANDLE32 hConsoleInput
)
575 fprintf(stderr
,"FlushConsoleInputBuffer(%d)\n",hConsoleInput
);
579 BOOL32 WINAPI
SetConsoleCursorPosition(HANDLE32 hcons
,COORD c
)
581 /* x are columns, y rows */
583 fprintf(stderr
,"\r");
585 fprintf(stderr
,"%c[%dC", 0x1B, c
.x
); /* note: 0x1b == ESC */
588 /* handle rest of the cases */
592 /***********************************************************************
593 * GetNumberOfConsoleInputEvents (KERNEL32.246)
595 BOOL32 WINAPI
GetNumberOfConsoleInputEvents(HANDLE32 hcon
,LPDWORD nrofevents
)
601 /***********************************************************************
602 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
604 BOOL32 WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
607 fprintf(stderr
,"GetNumberOfConsoleMouseButtons: STUB returning 2\n");
611 /***********************************************************************
612 * PeekConsoleInputA (KERNEL32.550)
614 BOOL32 WINAPI
PeekConsoleInput32A(HANDLE32 hConsoleInput
,
615 LPINPUT_RECORD pirBuffer
,
622 fprintf(stderr
,"PeekConsoleInput32A: STUB returning TRUE\n");
626 /***********************************************************************
627 * PeekConsoleInputW (KERNEL32.551)
629 BOOL32 WINAPI
PeekConsoleInput32W(HANDLE32 hConsoleInput
,
630 LPINPUT_RECORD pirBuffer
,
637 fprintf(stderr
,"PeekConsoleInput32W: STUB returning TRUE\n");
641 /***********************************************************************
642 * GetConsoleCursorInfo32 (KERNEL32.296)
644 BOOL32 WINAPI
GetConsoleCursorInfo32(HANDLE32 hcon
, LPDWORD cinfo
)
646 cinfo
[0] = 10; /* 10% of character box is cursor. */
647 cinfo
[1] = TRUE
; /* Cursor is visible. */
648 fprintf (stdnimp
, "GetConsoleCursorInfo32 -- STUB!\n");
652 /***********************************************************************
653 * SetConsoleCursorInfo32 (KERNEL32.626)
655 BOOL32 WINAPI
SetConsoleCursorInfo32(HANDLE32 hcon
, LPDWORD cinfo
)
657 fprintf (stdnimp
, "SetConsoleCursorInfo32 -- STUB!\n");