2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
13 #include <sys/ioctl.h>
14 #include <sys/types.h>
29 static CONSOLE_SCREEN_BUFFER_INFO dummyinfo =
38 /* The console -- I chose to keep the master and slave
39 * (UNIX) file descriptors around in case they are needed for
40 * ioctls later. The pid is needed to destroy the xterm on close
42 typedef struct _CONSOLE {
44 int master; /* xterm side of pty */
45 int slave; /* wine side of pty */
46 int pid; /* xterm's pid, -1 if no xterm */
47 LPSTR title; /* title of console */
51 static void CONSOLE_Destroy( K32OBJ *obj );
52 static BOOL32 CONSOLE_Write(K32OBJ *ptr, LPCVOID lpBuffer,
53 DWORD nNumberOfChars, LPDWORD lpNumberOfChars,
54 LPOVERLAPPED lpOverlapped);
55 static BOOL32 CONSOLE_Read(K32OBJ *ptr, LPVOID lpBuffer,
56 DWORD nNumberOfChars, LPDWORD lpNumberOfChars,
57 LPOVERLAPPED lpOverlapped);
58 static int wine_openpty(int *master, int *slave, char *name,
59 struct termios *term, struct winsize *winsize);
60 static BOOL32 wine_createConsole(int *master, int *slave, int *pid);
62 const K32OBJ_OPS CONSOLE_Ops =
67 NULL, /* remove_wait */
68 CONSOLE_Read, /* read */
69 CONSOLE_Write, /* write */
70 CONSOLE_Destroy /* destroy */
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 /* make sure a xterm exists to kill */
87 if (console->pid != -1) {
88 kill(console->pid, SIGTERM);
90 HeapFree(SystemHeap, 0, console);
94 /* lpOverlapped is ignored */
95 static BOOL32 CONSOLE_Read(K32OBJ *ptr, LPVOID lpBuffer, DWORD nNumberOfChars,
96 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
98 CONSOLE *console = (CONSOLE *)ptr;
101 TRACE(console, "%p %p %ld\n", ptr, lpBuffer,
104 *lpNumberOfChars = 0;
106 if ((result = read(console->slave, lpBuffer, nNumberOfChars)) == -1) {
110 *lpNumberOfChars = result;
115 /* lpOverlapped is ignored */
116 static BOOL32 CONSOLE_Write(K32OBJ *ptr, LPCVOID lpBuffer,
117 DWORD nNumberOfChars,
118 LPDWORD lpNumberOfChars, LPOVERLAPPED lpOverlapped)
120 CONSOLE *console = (CONSOLE *)ptr;
123 TRACE(console, "%p %p %ld\n", ptr, lpBuffer,
126 *lpNumberOfChars = 0;
129 * I assume this loop around EAGAIN is here because
130 * win32 doesn't have interrupted system calls
135 result = write(console->slave, lpBuffer, nNumberOfChars);
137 *lpNumberOfChars = result;
140 if (errno != EINTR) {
150 /***********************************************************************
151 * SetConsoleCtrlHandler (KERNEL32.459)
153 BOOL32 WINAPI SetConsoleCtrlHandler(HANDLER_ROUTINE * func, BOOL32 a)
158 /***********************************************************************
159 * CreateConsoleScreenBuffer (KERNEL32.151)
161 HANDLE32 WINAPI CreateConsoleScreenBuffer( DWORD dwDesiredAccess,
163 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
165 LPVOID lpScreenBufferData)
167 FIXME(console, "(...): stub !\n");
168 return INVALID_HANDLE_VALUE32;
171 /***********************************************************************
172 * GetConsoleScreenBufferInfo (KERNEL32.190)
174 BOOL32 WINAPI GetConsoleScreenBufferInfo( HANDLE32 hConsoleOutput,
175 LPCONSOLE_SCREEN_BUFFER_INFO csbi )
179 csbi->dwCursorPosition.x = 0;
180 csbi->dwCursorPosition.y = 0;
181 csbi->wAttributes = 0;
182 csbi->srWindow.Left = 0;
183 csbi->srWindow.Right = 79;
184 csbi->srWindow.Top = 0;
185 csbi->srWindow.Bottom = 23;
186 csbi->dwMaximumWindowSize.x = 80;
187 csbi->dwMaximumWindowSize.y = 24;
191 /***********************************************************************
192 * SetConsoleActiveScreenBuffer (KERNEL32.623)
194 BOOL32 WINAPI SetConsoleActiveScreenBuffer(HANDLE32 hConsoleOutput)
196 FIXME(console, "(%x): stub!\n", hConsoleOutput);
200 /***********************************************************************
201 * GetLargestConsoleWindowSize (KERNEL32.226)
203 DWORD WINAPI GetLargestConsoleWindowSize( HANDLE32 hConsoleOutput )
205 return (DWORD)MAKELONG(dummyinfo.dwMaximumWindowSize.x,dummyinfo.dwMaximumWindowSize.y);
208 /***********************************************************************
209 * FreeConsole (KERNEL32.267)
211 BOOL32 WINAPI FreeConsole(VOID)
214 PDB32 *pdb = PROCESS_Current();
219 console = (CONSOLE *)pdb->console;
221 if (console == NULL) {
222 SetLastError(ERROR_INVALID_PARAMETER);
226 PROCESS_CloseObjHandles(pdb, &console->header);
228 K32OBJ_DecCount( &console->header );
236 * It looks like the openpty that comes with glibc in RedHat 5.0
237 * is buggy (second call returns what looks like a dup of 0 and 1
238 * instead of a new pty), this is a generic replacement.
240 static int wine_openpty(int *master, int *slave, char *name,
241 struct termios *term, struct winsize *winsize)
247 strcpy (pts_name, "/dev/ptyXY");
249 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
251 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
254 if ((fdm = open(pts_name, O_RDWR)) < 0) {
261 if ((fds = open(pts_name, O_RDWR)) < 0) {
269 tcsetattr(*slave, TCSANOW, term);
271 ioctl(*slave, TIOCSWINSZ, winsize);
273 strcpy(name, pts_name);
280 static BOOL32 wine_createConsole(int *master, int *slave, int *pid)
288 if (tcgetattr(0, &term) < 0) return FALSE;
289 term.c_lflag |= ICANON;
290 term.c_lflag &= ~ECHO;
291 if (wine_openpty(master, slave, NULL, &term, NULL) < 0) return FALSE;
293 if ((*pid=fork()) == 0) {
294 tcsetattr(*slave, TCSADRAIN, &term);
295 sprintf(buf, "-Sxx%d", *master);
296 execlp("xterm", "xterm", buf, NULL);
297 ERR(console, "error creating AllocConsole xterm\n");
301 /* most xterms like to print their window ID when used with -S;
302 * read it and continue before the user has a chance...
303 * NOTE: this is the reason we started xterm with ECHO off,
304 * we'll turn it back on below
307 for (i=0; c!='\n'; (status=read(*slave, &c, 1)), i++) {
308 if (status == -1 && c == '\0') {
309 /* wait for xterm to be created */
313 WARN(console, "can't read xterm WID\n");
318 term.c_lflag |= ECHO;
319 tcsetattr(*master, TCSADRAIN, &term);
325 /***********************************************************************
326 * CONSOLE_GetConsoleHandle
327 * returns a 16-bit style console handle
328 * note: only called from _lopen
330 HFILE32 CONSOLE_GetConsoleHandle(VOID)
332 PDB32 *pdb = PROCESS_Current();
333 HFILE32 handle = HFILE_ERROR32;
336 if (pdb->console != NULL) {
337 CONSOLE *console = (CONSOLE *)pdb->console;
338 handle = (HFILE32)HANDLE_Alloc(&console->header, 0, TRUE);
344 /***********************************************************************
345 * AllocConsole (KERNEL32.103)
347 * creates an xterm with a pty to our program
349 BOOL32 WINAPI AllocConsole(VOID)
355 PDB32 *pdb = PROCESS_Current();
357 HANDLE32 hIn, hOut, hErr;
359 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
361 SetLastError(ERROR_CANNOT_MAKE); /* this might not be the right
362 error, but it's a good guess :) */
364 console = (CONSOLE *)pdb->console;
366 /* don't create a console if we already have one */
367 if (console != NULL) {
368 SetLastError(ERROR_ACCESS_DENIED);
373 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
379 console->header.type = K32OBJ_CONSOLE;
380 console->header.refcount = 1;
382 console->title = NULL;
384 if (wine_createConsole(&master, &slave, &pid) == FALSE) {
385 K32OBJ_DecCount(&console->header);
390 /* save the pid and other info for future use */
391 console->master = master;
392 console->slave = slave;
395 if ((hIn = HANDLE_Alloc(&console->header, 0, TRUE)) == INVALID_HANDLE_VALUE32)
397 K32OBJ_DecCount(&console->header);
402 if ((hOut = HANDLE_Alloc(&console->header, 0, TRUE)) == INVALID_HANDLE_VALUE32)
405 K32OBJ_DecCount(&console->header);
411 if ((hErr = HANDLE_Alloc(&console->header, 0, TRUE)) == INVALID_HANDLE_VALUE32)
415 K32OBJ_DecCount(&console->header);
420 /* associate this console with the process */
421 if (pdb->console) K32OBJ_DecCount( pdb->console );
422 pdb->console = (K32OBJ *)console;
424 /* NT resets the STD_*_HANDLEs on console alloc */
425 SetStdHandle(STD_INPUT_HANDLE, hIn);
426 SetStdHandle(STD_OUTPUT_HANDLE, hOut);
427 SetStdHandle(STD_ERROR_HANDLE, hErr);
429 SetLastError(ERROR_SUCCESS);
431 SetConsoleTitle32A("Wine Console");
436 /***********************************************************************
437 * GetConsoleCP (KERNEL32.226)
439 UINT32 WINAPI GetConsoleCP(VOID)
444 /***********************************************************************
445 * GetConsoleOutputCP (KERNEL32.189)
447 UINT32 WINAPI GetConsoleOutputCP(VOID)
449 return GetConsoleCP();
452 /***********************************************************************
453 * GetConsoleMode (KERNEL32.188)
455 BOOL32 WINAPI GetConsoleMode(HANDLE32 hcon,LPDWORD mode)
457 *mode = ENABLE_PROCESSED_INPUT |
463 /***********************************************************************
464 * SetConsoleMode (KERNEL32.628)
466 BOOL32 WINAPI SetConsoleMode(HANDLE32 hcon,DWORD mode)
468 FIXME(console,"(%08x,%08lx): stub\n",hcon,mode);
472 /***********************************************************************
473 * GetConsoleTitleA (KERNEL32.191)
475 DWORD WINAPI GetConsoleTitle32A(LPSTR title,DWORD size)
477 PDB32 *pdb = PROCESS_Current();
478 CONSOLE *console= (CONSOLE *)pdb->console;
480 if(console && console->title)
482 lstrcpyn32A(title,console->title,size);
483 return strlen(title);
488 /***********************************************************************
489 * GetConsoleTitleW (KERNEL32.192)
491 DWORD WINAPI GetConsoleTitle32W(LPWSTR title,DWORD size)
493 PDB32 *pdb = PROCESS_Current();
494 CONSOLE *console= (CONSOLE *)pdb->console;
495 if(console && console->title)
497 lstrcpynAtoW(title,console->title,size);
498 return (lstrlen32W(title));
503 /***********************************************************************
504 * WriteConsoleA (KERNEL32.729)
506 BOOL32 WINAPI WriteConsole32A( HANDLE32 hConsoleOutput,
508 DWORD nNumberOfCharsToWrite,
509 LPDWORD lpNumberOfCharsWritten,
512 /* FIXME: should I check if this is a console handle? */
513 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
514 lpNumberOfCharsWritten, NULL);
517 /***********************************************************************
518 * WriteConsoleOutputA (KERNEL32.732)
520 BOOL32 WINAPI WriteConsoleOutput32A( HANDLE32 hConsoleOutput,
521 LPCHAR_INFO lpBuffer,
524 LPSMALL_RECT lpWriteRegion)
529 /***********************************************************************
530 * WriteConsoleW (KERNEL32.577)
532 BOOL32 WINAPI WriteConsole32W( HANDLE32 hConsoleOutput,
534 DWORD nNumberOfCharsToWrite,
535 LPDWORD lpNumberOfCharsWritten,
539 LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
541 lstrcpynWtoA( xstring, lpBuffer,nNumberOfCharsToWrite);
543 /* FIXME: should I check if this is a console handle? */
544 ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
545 lpNumberOfCharsWritten, NULL);
546 HeapFree( GetProcessHeap(), 0, xstring );
551 /***********************************************************************
552 * ReadConsoleA (KERNEL32.419)
554 BOOL32 WINAPI ReadConsole32A( HANDLE32 hConsoleInput,
556 DWORD nNumberOfCharsToRead,
557 LPDWORD lpNumberOfCharsRead,
560 return ReadFile(hConsoleInput, lpBuffer, nNumberOfCharsToRead,
561 lpNumberOfCharsRead, NULL);
564 fgets(lpBuffer,nNumberOfCharsToRead, CONSOLE_console.conIO);
565 if (ferror(CONSOLE_console.conIO) {
569 *lpNumberOfCharsRead = strlen(lpBuffer);
575 /***********************************************************************
576 * ReadConsoleW (KERNEL32.427)
578 BOOL32 WINAPI ReadConsole32W( HANDLE32 hConsoleInput,
580 DWORD nNumberOfCharsToRead,
581 LPDWORD lpNumberOfCharsRead,
585 LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0,
586 nNumberOfCharsToRead);
587 ret = ReadFile(hConsoleInput, buf, nNumberOfCharsToRead,
588 lpNumberOfCharsRead, NULL);
589 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
590 *lpNumberOfCharsRead = strlen(buf);
591 HeapFree( GetProcessHeap(), 0, buf );
595 LPSTR buf = (LPSTR)HEAP_xalloc(GetProcessHeap(), 0,
596 nNumberOfCharsToRead);
597 fgets(buf, nNumberOfCharsToRead, CONSOLE_console.conIO);
599 if (ferror(CONSOLE_console.conIO) {
600 HeapFree( GetProcessHeap(), 0, buf );
605 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
606 *lpNumberOfCharsRead = strlen(buf);
607 HeapFree( GetProcessHeap(), 0, buf );
613 /***********************************************************************
614 * SetConsoleTitle32A (KERNEL32.476)
616 BOOL32 WINAPI SetConsoleTitle32A(LPCSTR title)
618 PDB32 *pdb = PROCESS_Current();
621 char titleformat[]="\033]2;%s\a"; /*this should work for xterms*/
625 TRACE(console,"SetConsoleTitle(%s)\n",title);
627 console = (CONSOLE *)pdb->console;
630 if(console->title) /* Free old title, if there is one */
631 HeapFree( SystemHeap, 0, console->title );
632 console->title = (LPSTR)HeapAlloc(SystemHeap, 0,strlen(title)+1);
633 if(console->title) strcpy(console->title,title);
635 titlestring = HeapAlloc(GetProcessHeap(), 0,strlen(title)+strlen(titleformat)+1);
639 sprintf(titlestring,titleformat,title);
641 CONSOLE_Write((K32OBJ *)console,titlestring,strlen(titlestring),&written,NULL);
642 if (written == strlen(titlestring))
644 HeapFree( GetProcessHeap(), 0, titlestring );
648 /***********************************************************************
649 * SetConsoleTitle32W (KERNEL32.477)
651 BOOL32 WINAPI SetConsoleTitle32W( LPCWSTR title )
655 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
656 ret = SetConsoleTitle32A(titleA);
657 HeapFree( GetProcessHeap(), 0, titleA );
661 /***********************************************************************
662 * FlushConsoleInputBuffer (KERNEL32.132)
664 BOOL32 WINAPI FlushConsoleInputBuffer(HANDLE32 hConsoleInput)
666 FIXME(console,"(%d): stub\n",hConsoleInput);
670 BOOL32 WINAPI SetConsoleCursorPosition(HANDLE32 hcons,COORD c)
672 /* x are columns, y rows */
674 fprintf(stderr,"\r");
676 fprintf(stderr,"%c[%dC", 0x1B, c.x); /* note: 0x1b == ESC */
679 /* handle rest of the cases */
683 /***********************************************************************
684 * GetNumberOfConsoleInputEvents (KERNEL32.246)
686 BOOL32 WINAPI GetNumberOfConsoleInputEvents(HANDLE32 hcon,LPDWORD nrofevents)
692 /***********************************************************************
693 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
695 BOOL32 WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
698 FIXME(console,"(%p): STUB returning 2\n", nrofbuttons);
702 /***********************************************************************
703 * PeekConsoleInputA (KERNEL32.550)
705 BOOL32 WINAPI PeekConsoleInput32A(HANDLE32 hConsoleInput,
706 LPINPUT_RECORD pirBuffer,
713 FIXME(console,"(...): STUB returning TRUE\n");
717 /***********************************************************************
718 * PeekConsoleInputW (KERNEL32.551)
720 BOOL32 WINAPI PeekConsoleInput32W(HANDLE32 hConsoleInput,
721 LPINPUT_RECORD pirBuffer,
728 FIXME(console,"(...): STUB returning TRUE\n");
732 /***********************************************************************
733 * GetConsoleCursorInfo32 (KERNEL32.296)
735 BOOL32 WINAPI GetConsoleCursorInfo32(HANDLE32 hcon, LPDWORD cinfo)
737 cinfo[0] = 10; /* 10% of character box is cursor. */
738 cinfo[1] = TRUE; /* Cursor is visible. */
739 FIXME(console, "(%x,%p): STUB!\n", hcon, cinfo);
743 /***********************************************************************
744 * SetConsoleCursorInfo32 (KERNEL32.626)
746 BOOL32 WINAPI SetConsoleCursorInfo32(HANDLE32 hcon, LPDWORD cinfo)
748 FIXME(console, "(%#x,%p): STUB!\n", hcon, cinfo);
752 /***********************************************************************
753 * SetConsoleWindowInfo32 (KERNEL32.634)
755 BOOL32 WINAPI SetConsoleWindowInfo32(HANDLE32 hcon, BOOL32 flag, LPSMALL_RECT window)
757 FIXME(console, "(%x,%d,%p): STUB!\n", hcon, flag, window);
761 /***********************************************************************
764 BOOL32 WINAPI SetConsoleTextAttribute32(HANDLE32 hcon, DWORD attributes)
766 FIXME(console, "(%#x,%#lx): STUB!\n", hcon, attributes);