Release 980215
[wine/multimedia.git] / win32 / console.c
blob2396f3443b826e91c6b09031f4e494b070007398
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <pty.h>
13 #include <termios.h>
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <signal.h>
20 #include <assert.h>
21 #include "windows.h"
22 #include "k32obj.h"
23 #include "file.h"
24 #include "process.h"
25 #include "winerror.h"
26 #include "wincon.h"
27 #include "heap.h"
28 #include "stddebug.h"
29 #include "debug.h"
31 static CONSOLE_SCREEN_BUFFER_INFO dummyinfo =
33 {80, 24},
34 {0, 0},
36 {0, 0, 79, 23},
37 {80, 24}
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 {
45 K32OBJ header;
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 */
53 } CONSOLE;
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 =
62 NULL, /* signaled */
63 NULL, /* satisfied */
64 NULL, /* add_wait */
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)
96 return 0;
99 /***********************************************************************
100 * CreateConsoleScreenBuffer (KERNEL32.151)
102 HANDLE32 WINAPI CreateConsoleScreenBuffer( DWORD dwDesiredAccess,
103 DWORD dwShareMode,
104 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
105 DWORD dwFlags,
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 )
118 csbi->dwSize.x = 80;
119 csbi->dwSize.y = 24;
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;
129 return TRUE;
132 /***********************************************************************
133 * SetConsoleActiveScreenBuffer (KERNEL32.623)
135 BOOL32 WINAPI SetConsoleActiveScreenBuffer(HANDLE32 hConsoleOutput)
137 fprintf(stderr, "SetConsoleActiveScreenBuffer(): stub !\n");
138 return 0;
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();
156 CONSOLE *console;
158 SYSTEM_LOCK();
160 console = (CONSOLE *)pdb->console;
162 if (console == NULL) {
163 SetLastError(ERROR_INVALID_PARAMETER);
164 return FALSE;
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 );
171 pdb->console = NULL;
172 SYSTEM_UNLOCK();
173 return TRUE;
177 /**
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)
185 int fdm, fds;
186 char *ptr1, *ptr2;
187 char pts_name[512];
189 strcpy (pts_name, "/dev/ptyXY");
191 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
192 pts_name[8] = *ptr1;
193 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
194 pts_name[9] = *ptr2;
196 if ((fdm = open(pts_name, O_RDWR)) < 0) {
197 if (errno == ENOENT)
198 return -1;
199 else
200 continue;
202 pts_name[5] = 't';
203 if ((fds = open(pts_name, O_RDWR)) < 0) {
204 pts_name[5] = 'p';
205 continue;
207 *master = fdm;
208 *slave = fds;
210 if (term != NULL)
211 tcsetattr(*slave, TCSANOW, term);
212 if (winsize != NULL)
213 ioctl(*slave, TIOCSWINSZ, winsize);
214 if (name != NULL)
215 strcpy(name, pts_name);
216 return fds;
219 return -1;
222 static BOOL32 wine_createConsole(int *master, int *slave, int *pid)
224 struct termios term;
225 char buf[1024];
226 char c = '\0';
227 int status = 0;
228 int i;
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");
240 exit(1);
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 */
252 usleep(100);
254 if (i > 10000) {
255 fprintf(stderr, "can't read xterm WID\n");
256 kill(*pid, SIGKILL);
257 return FALSE;
260 term.c_lflag |= ECHO;
261 tcsetattr(*master, TCSADRAIN, &term);
263 return TRUE;
268 /***********************************************************************
269 * AllocConsole (KERNEL32.103)
271 * creates an xterm with a pty to our program
273 BOOL32 WINAPI AllocConsole(VOID)
276 int master;
277 int slave;
278 int pid;
279 PDB32 *pdb = PROCESS_Current();
280 CONSOLE *console;
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
294 * its parent
296 if (console && (console->flags & CONSOLE_STARTED_FROM) == 0) {
297 SetLastError(ERROR_ACCESS_DENIED);
298 SYSTEM_UNLOCK();
299 return FALSE;
302 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
304 SYSTEM_UNLOCK();
305 return FALSE;
308 console->header.type = K32OBJ_CONSOLE;
309 console->header.refcount = 1;
310 console->pid = -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);
317 SYSTEM_UNLOCK();
318 return FALSE;
321 /* save the pid and other info for future use */
322 console->master = master;
323 console->slave = slave;
324 console->pid = pid;
325 console->flags = 0;
327 if ((hIn = FILE_DupUnixHandle(slave)) == INVALID_HANDLE_VALUE32)
329 K32OBJ_DecCount(&console->header);
330 SYSTEM_UNLOCK();
331 return FALSE;
333 FILE_SetFileType(hIn, FILE_TYPE_CHAR);
335 if ((hOut = FILE_DupUnixHandle(slave)) == INVALID_HANDLE_VALUE32)
337 CloseHandle(hIn);
338 K32OBJ_DecCount(&console->header);
339 SYSTEM_UNLOCK();
340 return FALSE;
342 FILE_SetFileType(hOut, FILE_TYPE_CHAR);
344 if ((hErr = FILE_DupUnixHandle(slave)) == INVALID_HANDLE_VALUE32)
346 CloseHandle(hIn);
347 CloseHandle(hOut);
348 K32OBJ_DecCount(&console->header);
349 SYSTEM_UNLOCK();
350 return FALSE;
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);
368 SYSTEM_UNLOCK();
369 return TRUE;
373 /***********************************************************************
374 * GetConsoleCP (KERNEL32.226)
376 UINT32 WINAPI GetConsoleCP(VOID)
378 return GetACP();
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 |
395 ENABLE_LINE_INPUT |
396 ENABLE_ECHO_INPUT;
397 return TRUE;
400 /***********************************************************************
401 * SetConsoleMode (KERNEL32.188)
403 BOOL32 WINAPI SetConsoleMode(HANDLE32 hcon,DWORD mode)
405 fprintf(stdnimp,"SetConsoleMode(%08x,%08lx)\n",hcon,mode);
406 return TRUE;
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,
431 LPCVOID lpBuffer,
432 DWORD nNumberOfCharsToWrite,
433 LPDWORD lpNumberOfCharsWritten,
434 LPVOID lpReserved )
436 /* FIXME: should I check if this is a console handle? */
437 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
438 lpNumberOfCharsWritten, NULL);
440 #ifdef OLD
441 *lpNumberOfCharsWritten = fprintf(CONSOLE_console.conIO, "%.*s",
442 (int)nNumberOfCharsToWrite,
443 (LPSTR)lpBuffer );
444 if (ferror(CONSOLE_console.conIO) {
445 clearerr();
446 return FALSE;
449 return TRUE;
450 #endif
453 /***********************************************************************
454 * WriteConsoleOutputA (KERNEL32.732)
456 BOOL32 WINAPI WriteConsoleOutput32A( HANDLE32 hConsoleOutput,
457 LPCHAR_INFO lpBuffer,
458 COORD dwBufferSize,
459 COORD dwBufferCoord,
460 LPSMALL_RECT lpWriteRegion)
462 return FALSE;
465 /***********************************************************************
466 * WriteConsoleW (KERNEL32.577)
468 BOOL32 WINAPI WriteConsole32W( HANDLE32 hConsoleOutput,
469 LPCVOID lpBuffer,
470 DWORD nNumberOfCharsToWrite,
471 LPDWORD lpNumberOfCharsWritten,
472 LPVOID lpReserved )
475 /* FIXME: should I check if this is a console handle? */
476 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
477 lpNumberOfCharsWritten, NULL);
480 #ifdef OLD
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) {
487 clearerr();
488 return FALSE;
491 return TRUE;
492 #endif
495 /***********************************************************************
496 * ReadConsoleA (KERNEL32.419)
498 BOOL32 WINAPI ReadConsole32A( HANDLE32 hConsoleInput,
499 LPVOID lpBuffer,
500 DWORD nNumberOfCharsToRead,
501 LPDWORD lpNumberOfCharsRead,
502 LPVOID lpReserved )
504 return ReadFile(hConsoleInput, lpBuffer, nNumberOfCharsToRead,
505 lpNumberOfCharsRead, NULL);
507 #ifdef OLD
508 fgets(lpBuffer,nNumberOfCharsToRead, CONSOLE_console.conIO);
509 if (ferror(CONSOLE_console.conIO) {
510 clearerr();
511 return FALSE;
513 *lpNumberOfCharsRead = strlen(lpBuffer);
514 return TRUE;
515 #endif
519 /***********************************************************************
520 * ReadConsoleW (KERNEL32.427)
522 BOOL32 WINAPI ReadConsole32W( HANDLE32 hConsoleInput,
523 LPVOID lpBuffer,
524 DWORD nNumberOfCharsToRead,
525 LPDWORD lpNumberOfCharsRead,
526 LPVOID lpReserved )
528 return ReadFile(hConsoleInput, lpBuffer, nNumberOfCharsToRead,
529 lpNumberOfCharsRead, NULL);
531 #ifdef OLD
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 );
538 clearerr();
539 return FALSE;
542 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
543 *lpNumberOfCharsRead = strlen(buf);
544 HeapFree( GetProcessHeap(), 0, buf );
545 #endif
546 return TRUE;
550 /***********************************************************************
551 * SetConsoleTitle32A (KERNEL32.476)
553 BOOL32 WINAPI SetConsoleTitle32A(LPCSTR title)
555 fprintf(stderr,"SetConsoleTitle(%s)\n",title);
556 return TRUE;
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 );
567 return TRUE;
570 /***********************************************************************
571 * FlushConsoleInputBuffer (KERNEL32.132)
573 BOOL32 WINAPI FlushConsoleInputBuffer(HANDLE32 hConsoleInput)
575 fprintf(stderr,"FlushConsoleInputBuffer(%d)\n",hConsoleInput);
576 return TRUE;
579 BOOL32 WINAPI SetConsoleCursorPosition(HANDLE32 hcons,COORD c)
581 /* x are columns, y rows */
582 if (!c.y) {
583 fprintf(stderr,"\r");
584 if (c.x)
585 fprintf(stderr,"%c[%dC", 0x1B, c.x); /* note: 0x1b == ESC */
586 return TRUE;
588 /* handle rest of the cases */
589 return FALSE;
592 /***********************************************************************
593 * GetNumberOfConsoleInputEvents (KERNEL32.246)
595 BOOL32 WINAPI GetNumberOfConsoleInputEvents(HANDLE32 hcon,LPDWORD nrofevents)
597 *nrofevents = 0;
598 return TRUE;
601 /***********************************************************************
602 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
604 BOOL32 WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
606 *nrofbuttons = 2;
607 fprintf(stderr,"GetNumberOfConsoleMouseButtons: STUB returning 2\n");
608 return TRUE;
611 /***********************************************************************
612 * PeekConsoleInputA (KERNEL32.550)
614 BOOL32 WINAPI PeekConsoleInput32A(HANDLE32 hConsoleInput,
615 LPINPUT_RECORD pirBuffer,
616 DWORD cInRecords,
617 LPDWORD lpcRead)
619 pirBuffer = NULL;
620 cInRecords = 0;
621 *lpcRead = 0;
622 fprintf(stderr,"PeekConsoleInput32A: STUB returning TRUE\n");
623 return TRUE;
626 /***********************************************************************
627 * PeekConsoleInputW (KERNEL32.551)
629 BOOL32 WINAPI PeekConsoleInput32W(HANDLE32 hConsoleInput,
630 LPINPUT_RECORD pirBuffer,
631 DWORD cInRecords,
632 LPDWORD lpcRead)
634 pirBuffer = NULL;
635 cInRecords = 0;
636 *lpcRead = 0;
637 fprintf(stderr,"PeekConsoleInput32W: STUB returning TRUE\n");
638 return TRUE;
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");
649 return TRUE;
652 /***********************************************************************
653 * SetConsoleCursorInfo32 (KERNEL32.626)
655 BOOL32 WINAPI SetConsoleCursorInfo32(HANDLE32 hcon, LPDWORD cinfo)
657 fprintf (stdnimp, "SetConsoleCursorInfo32 -- STUB!\n");
658 return TRUE;