2 * an application for displaying Win32 console
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
25 #include "wine/server.h"
26 #include "wine/unicode.h"
27 #include "winecon_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
33 /******************************************************************
36 * updates the local copy of cells (band to update)
38 void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
40 SERVER_START_REQ( read_console_output )
42 req->handle = (obj_handle_t)data->hConOut;
45 req->mode = CHAR_INFO_MODE_TEXTATTR;
47 wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
48 (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
49 wine_server_call( req );
52 data->fnRefresh(data, upd_tp, upd_bm);
55 /******************************************************************
56 * WINECON_NotifyWindowChange
58 * Inform server that visible window on sb has changed
60 void WINECON_NotifyWindowChange(struct inner_data* data)
62 SERVER_START_REQ( set_console_output_info )
64 req->handle = (obj_handle_t)data->hConOut;
65 req->win_left = data->curcfg.win_pos.X;
66 req->win_top = data->curcfg.win_pos.Y;
67 req->win_right = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
68 req->win_bottom = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
69 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
70 wine_server_call( req );
75 /******************************************************************
76 * WINECON_GetHistorySize
80 int WINECON_GetHistorySize(HANDLE hConIn)
84 SERVER_START_REQ(get_console_input_info)
86 req->handle = (obj_handle_t)hConIn;
87 if (!wine_server_call_err( req )) ret = reply->history_size;
93 /******************************************************************
94 * WINECON_SetHistorySize
98 BOOL WINECON_SetHistorySize(HANDLE hConIn, int size)
102 SERVER_START_REQ(set_console_input_info)
104 req->handle = (obj_handle_t)hConIn;
105 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
106 req->history_size = size;
107 ret = !wine_server_call_err( req );
114 /******************************************************************
115 * WINECON_GetHistoryMode
119 int WINECON_GetHistoryMode(HANDLE hConIn)
123 SERVER_START_REQ(get_console_input_info)
125 req->handle = (obj_handle_t)hConIn;
126 if (!wine_server_call_err( req )) ret = reply->history_mode;
132 /******************************************************************
133 * WINECON_SetHistoryMode
137 BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
141 SERVER_START_REQ(set_console_input_info)
143 req->handle = (obj_handle_t)hConIn;
144 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
145 req->history_mode = mode;
146 ret = !wine_server_call_err( req );
152 /******************************************************************
153 * WINECON_GetConsoleTitle
157 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
161 if (len < sizeof(WCHAR)) return FALSE;
163 SERVER_START_REQ( get_console_input_info )
165 req->handle = (obj_handle_t)hConIn;
166 wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
167 if ((ret = !wine_server_call_err( req )))
169 len = wine_server_reply_size( reply );
170 buffer[len / sizeof(WCHAR)] = 0;
177 /******************************************************************
178 * WINECON_GrabChanges
180 * A change occurs, try to figure out which
182 int WINECON_GrabChanges(struct inner_data* data)
184 struct console_renderer_event evts[256];
185 int i, num, curs = -1;
188 SERVER_START_REQ( get_console_renderer_events )
190 wine_server_set_reply( req, evts, sizeof(evts) );
191 req->handle = (obj_handle_t)data->hSynchro;
192 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
196 if (!num) {WINE_WARN("hmm renderer signaled but no events available\n"); return 1;}
198 /* FIXME: should do some event compression here (cursor pos, update) */
199 /* step 1: keep only last cursor pos event */
200 for (i = num - 1; i >= 0; i--)
202 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
208 memmove(&evts[i], &evts[i+1], (num - i - 1) * sizeof(evts[0]));
213 /* step 2: manage update events */
214 for (i = 0; i < num - 1; i++)
216 if (evts[i].event == CONSOLE_RENDERER_UPDATE_EVENT &&
217 evts[i+1].event == CONSOLE_RENDERER_UPDATE_EVENT)
220 if (evts[i].u.update.bottom + 1 == evts[i+1].u.update.top)
222 evts[i].u.update.bottom = evts[i+1].u.update.bottom;
223 memmove(&evts[i+1], &evts[i+2], (num - i - 2) * sizeof(evts[0]));
226 /* already handled cases */
227 else if (evts[i].u.update.top <= evts[i+1].u.update.top &&
228 evts[i].u.update.bottom >= evts[i+1].u.update.bottom)
230 memmove(&evts[i+1], &evts[i+2], (num - i - 2) * sizeof(evts[0]));
236 WINE_TRACE("Events:");
237 for (i = 0; i < num; i++)
239 switch (evts[i].event)
241 case CONSOLE_RENDERER_TITLE_EVENT:
242 data->fnSetTitle(data);
244 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
245 SERVER_START_REQ( open_console )
247 req->from = (int)data->hConIn;
248 req->access = GENERIC_READ | GENERIC_WRITE;
249 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
250 req->inherit = FALSE;
251 h = wine_server_call_err( req ) ? 0 : (HANDLE)reply->handle;
254 if (WINE_TRACE_ON(wineconsole))
255 WINE_DPRINTF(" active(%d)", (int)h);
258 CloseHandle(data->hConOut);
262 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
263 if (data->curcfg.sb_width != evts[i].u.resize.width ||
264 data->curcfg.sb_height != evts[i].u.resize.height)
266 if (WINE_TRACE_ON(wineconsole))
267 WINE_DPRINTF(" resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
268 data->curcfg.sb_width = evts[i].u.resize.width;
269 data->curcfg.sb_height = evts[i].u.resize.height;
271 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
272 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
273 if (!data->cells) {WINE_ERR("OOM\n"); exit(0);}
274 data->fnResizeScreenBuffer(data);
275 data->fnComputePositions(data);
278 case CONSOLE_RENDERER_UPDATE_EVENT:
279 if (WINE_TRACE_ON(wineconsole))
280 WINE_DPRINTF(" update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
281 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
283 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
284 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
286 data->cursor.X = evts[i].u.cursor_pos.x;
287 data->cursor.Y = evts[i].u.cursor_pos.y;
288 data->fnPosCursor(data);
289 if (WINE_TRACE_ON(wineconsole))
290 WINE_DPRINTF(" curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
293 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
294 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
295 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
297 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
298 evts[i].u.cursor_geom.visible, FALSE);
299 if (WINE_TRACE_ON(wineconsole))
300 WINE_DPRINTF(" curs-geom(%d,%d)",
301 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
304 case CONSOLE_RENDERER_DISPLAY_EVENT:
305 if (evts[i].u.display.left != data->curcfg.win_pos.X)
307 data->fnScroll(data, evts[i].u.display.left, TRUE);
308 data->fnPosCursor(data);
309 if (WINE_TRACE_ON(wineconsole))
310 WINE_DPRINTF(" h-scroll(%d)", evts[i].u.display.left);
312 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
314 data->fnScroll(data, evts[i].u.display.top, FALSE);
315 data->fnPosCursor(data);
316 if (WINE_TRACE_ON(wineconsole))
317 WINE_DPRINTF(" v-scroll(%d)", evts[i].u.display.top);
319 if (evts[i].u.display.width != data->curcfg.win_width ||
320 evts[i].u.display.height != data->curcfg.win_height)
322 if (WINE_TRACE_ON(wineconsole))
323 WINE_DPRINTF(" win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
324 data->curcfg.win_width = evts[i].u.display.width;
325 data->curcfg.win_height = evts[i].u.display.height;
326 data->fnComputePositions(data);
329 case CONSOLE_RENDERER_EXIT_EVENT:
330 if (WINE_TRACE_ON(wineconsole)) WINE_DPRINTF(". Exit!!\n");
333 WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
337 if (WINE_TRACE_ON(wineconsole)) WINE_DPRINTF(".\n");
341 /******************************************************************
344 * Destroy wineconsole internal data
346 static void WINECON_Delete(struct inner_data* data)
350 if (data->hConIn) CloseHandle(data->hConIn);
351 if (data->hConOut) CloseHandle(data->hConOut);
352 if (data->hSynchro) CloseHandle(data->hSynchro);
353 if (data->cells) HeapFree(GetProcessHeap(), 0, data->cells);
354 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
355 HeapFree(GetProcessHeap(), 0, data);
358 /******************************************************************
361 * Initialisation part I. Creation of server object (console input and
362 * active screen buffer)
364 static struct inner_data* WINECON_Init(HINSTANCE hInst, void* pid)
366 struct inner_data* data = NULL;
368 WCHAR szTitle[] = {'W','i','n','e',' ','c','o','n','s','o','l','e',0};
370 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
373 /* load default registry settings, and copy them into our current configuration */
374 WINECON_RegLoad(&data->defcfg);
375 data->curcfg = data->defcfg;
377 /* the handles here are created without the whistles and bells required by console
378 * (mainly because wineconsole doesn't need it)
379 * - there are not inheritable
380 * - hConIn is not synchronizable
382 SERVER_START_REQ(alloc_console)
384 req->access = GENERIC_READ | GENERIC_WRITE;
385 req->inherit = FALSE;
387 ret = !wine_server_call_err( req );
388 data->hConIn = (HANDLE)reply->handle_in;
389 data->hSynchro = (HANDLE)reply->event;
392 if (!ret) goto error;
394 SERVER_START_REQ( set_console_input_info )
396 req->handle = (obj_handle_t)data->hConIn;
397 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
398 wine_server_add_data( req, szTitle, strlenW(szTitle) * sizeof(WCHAR) );
399 ret = !wine_server_call_err( req );
402 if (!ret) goto error;
404 SERVER_START_REQ(create_console_output)
406 req->handle_in = (obj_handle_t)data->hConIn;
407 req->access = GENERIC_WRITE|GENERIC_READ;
408 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
409 req->inherit = FALSE;
410 data->hConOut = (HANDLE)(wine_server_call_err( req ) ? 0 : reply->handle_out);
413 if (data->hConOut) return data;
416 WINECON_Delete(data);
420 /******************************************************************
423 * Spawn the child process when invoked with wineconsole foo bar
425 static BOOL WINECON_Spawn(struct inner_data* data, LPCSTR lpCmdLine)
427 PROCESS_INFORMATION info;
429 LPWSTR ptr = GetCommandLine(); /* we're unicode... */
432 /* we're in the case wineconsole <exe> <options>... spawn the new process */
433 memset(&startup, 0, sizeof(startup));
434 startup.cb = sizeof(startup);
435 startup.dwFlags = STARTF_USESTDHANDLES;
437 /* the attributes of wineconsole's handles are not adequate for inheritance, so
438 * get them with the correct attributes before process creation
440 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
441 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
442 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
443 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
444 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
445 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
447 WINE_ERR("Can't dup handles\n");
448 /* no need to delete handles, we're exiting the programm anyway */
452 /* we could have several ' ' in process command line... so try first space...
454 * the correct way would be to check the existence of the left part of ptr
457 while (*ptr && *ptr++ != ' ');
459 done = *ptr && CreateProcess(NULL, ptr, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
461 /* we no longer need the handles passed to the child for the console */
462 CloseHandle(startup.hStdInput);
463 CloseHandle(startup.hStdOutput);
464 CloseHandle(startup.hStdError);
469 /******************************************************************
474 static BOOL WINECON_HasEvent(LPCSTR ptr, unsigned *evt)
476 while (*ptr == ' ' || *ptr == '\t') ptr++;
477 if (strncmp(ptr, "--use-event=", 12)) return FALSE;
478 return sscanf(ptr + 12, "%d", evt) == 1;
481 /******************************************************************
484 * wineconsole can either be started as:
485 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
486 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
487 * a freshly created console
489 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
491 struct inner_data* data;
495 /* case of wineconsole <evt>, signal process that created us that we're up and running */
496 if (WINECON_HasEvent(lpCmdLine, &evt))
498 if (!(data = WINECON_Init(hInst, 0)))
500 WINE_ERR("failed to init1 wineconsole.\n");
503 ret = SetEvent((HANDLE)evt);
506 WINE_ERR("SetEvent failed.\n");
512 if (!(data = WINECON_Init(hInst, (void*)GetCurrentProcessId())))
514 WINE_ERR("failed to init2 wineconsole.\n");
517 ret = WINECON_Spawn(data, lpCmdLine);
520 WINE_MESSAGE("wineconsole: spawning client program failed. Invalid/missing command line arguments ?\n");
525 if (WCUSER_InitBackend(data))
527 WINE_TRACE("calling MainLoop.\n");
528 ret = data->fnMainLoop(data);
531 WINE_ERR("WCUSER_InitBackend failed.\n");
534 WINECON_Delete(data);