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/server.h"
23 #include "wine/unicode.h"
24 #include "winecon_private.h"
26 static int trace_level = 1;
27 void XTracer(int level, const char* format, ...)
33 if (level > trace_level) return;
35 va_start(valist, format);
36 len = vsnprintf(buf, sizeof(buf), format, valist);
39 if ((len <= -1) || (len >= sizeof(buf)))
41 len = sizeof(buf) - 1;
43 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
48 /******************************************************************
51 * updates the local copy of cells (band to update)
53 void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
55 SERVER_START_REQ( read_console_output )
57 req->handle = (handle_t)data->hConOut;
60 req->mode = CHAR_INFO_MODE_TEXTATTR;
62 wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
63 (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
64 wine_server_call( req );
67 data->fnRefresh(data, upd_tp, upd_bm);
70 /******************************************************************
71 * WINECON_NotifyWindowChange
73 * Inform server that visible window on sb has changed
75 void WINECON_NotifyWindowChange(struct inner_data* data)
77 SERVER_START_REQ( set_console_output_info )
79 req->handle = (handle_t)data->hConOut;
80 req->win_left = data->curcfg.win_pos.X;
81 req->win_top = data->curcfg.win_pos.Y;
82 req->win_right = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
83 req->win_bottom = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
84 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
85 wine_server_call( req );
90 /******************************************************************
91 * WINECON_GetHistorySize
95 int WINECON_GetHistorySize(HANDLE hConIn)
99 SERVER_START_REQ(get_console_input_info)
101 req->handle = (handle_t)hConIn;
102 if (!wine_server_call_err( req )) ret = reply->history_size;
108 /******************************************************************
109 * WINECON_SetHistorySize
113 BOOL WINECON_SetHistorySize(HANDLE hConIn, int size)
117 SERVER_START_REQ(set_console_input_info)
119 req->handle = (handle_t)hConIn;
120 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
121 req->history_size = size;
122 ret = !wine_server_call_err( req );
129 /******************************************************************
130 * WINECON_GetHistoryMode
134 int WINECON_GetHistoryMode(HANDLE hConIn)
138 SERVER_START_REQ(get_console_input_info)
140 req->handle = (handle_t)hConIn;
141 if (!wine_server_call_err( req )) ret = reply->history_mode;
147 /******************************************************************
148 * WINECON_SetHistoryMode
152 BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
156 SERVER_START_REQ(set_console_input_info)
158 req->handle = (handle_t)hConIn;
159 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
160 req->history_mode = mode;
161 ret = !wine_server_call_err( req );
167 /******************************************************************
168 * WINECON_GetConsoleTitle
172 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
176 if (len < sizeof(WCHAR)) return FALSE;
178 SERVER_START_REQ( get_console_input_info )
180 req->handle = (handle_t)hConIn;
181 wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
182 if ((ret = !wine_server_call_err( req )))
184 len = wine_server_reply_size( reply );
185 buffer[len / sizeof(WCHAR)] = 0;
192 /******************************************************************
193 * WINECON_GrabChanges
195 * A change occurs, try to figure out which
197 int WINECON_GrabChanges(struct inner_data* data)
199 struct console_renderer_event evts[16];
203 SERVER_START_REQ( get_console_renderer_events )
205 wine_server_set_reply( req, evts, sizeof(evts) );
206 req->handle = (handle_t)data->hSynchro;
207 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
211 if (!num) {Trace(0, "hmm renderer signaled but no events available\n"); return 1;}
213 /* FIXME: should do some event compression here (cursor pos, update) */
214 Trace(1, "Change notification:");
215 for (i = 0; i < num; i++)
217 switch (evts[i].event)
219 case CONSOLE_RENDERER_TITLE_EVENT:
220 data->fnSetTitle(data);
222 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
223 SERVER_START_REQ( open_console )
225 req->from = (int)data->hConIn;
226 req->access = GENERIC_READ | GENERIC_WRITE;
227 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
228 req->inherit = FALSE;
229 h = wine_server_call_err( req ) ? 0 : (HANDLE)reply->handle;
232 Trace(1, " active(%d)", (int)h);
235 CloseHandle(data->hConOut);
239 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
240 if (data->curcfg.sb_width != evts[i].u.resize.width ||
241 data->curcfg.sb_height != evts[i].u.resize.height)
243 Trace(1, " resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
244 data->curcfg.sb_width = evts[i].u.resize.width;
245 data->curcfg.sb_height = evts[i].u.resize.height;
247 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
248 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
249 if (!data->cells) {Trace(0, "OOM\n"); exit(0);}
250 data->fnResizeScreenBuffer(data);
251 data->fnComputePositions(data);
254 case CONSOLE_RENDERER_UPDATE_EVENT:
255 Trace(1, " update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
256 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
258 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
259 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
261 data->cursor.X = evts[i].u.cursor_pos.x;
262 data->cursor.Y = evts[i].u.cursor_pos.y;
263 data->fnPosCursor(data);
264 Trace(1, " curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
267 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
268 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
269 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
271 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
272 evts[i].u.cursor_geom.visible, FALSE);
273 Trace(1, " curs-geom(%d,%d)",
274 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
277 case CONSOLE_RENDERER_DISPLAY_EVENT:
278 if (evts[i].u.display.left != data->curcfg.win_pos.X)
280 data->fnScroll(data, evts[i].u.display.left, TRUE);
281 data->fnPosCursor(data);
282 Trace(1, " h-scroll(%d)", evts[i].u.display.left);
284 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
286 data->fnScroll(data, evts[i].u.display.top, FALSE);
287 data->fnPosCursor(data);
288 Trace(1, " v-scroll(%d)", evts[i].u.display.top);
290 if (evts[i].u.display.width != data->curcfg.win_width ||
291 evts[i].u.display.height != data->curcfg.win_height)
293 Trace(1, " win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
294 data->curcfg.win_width = evts[i].u.display.width;
295 data->curcfg.win_height = evts[i].u.display.height;
296 data->fnComputePositions(data);
299 case CONSOLE_RENDERER_EXIT_EVENT:
300 Trace(1, ". Exit!!\n");
303 Trace(0, "Unknown event type (%d)\n", evts[i].event);
307 Trace(1, ". Done\n");
311 /******************************************************************
314 * Destroy wineconsole internal data
316 static void WINECON_Delete(struct inner_data* data)
320 if (data->hConIn) CloseHandle(data->hConIn);
321 if (data->hConOut) CloseHandle(data->hConOut);
322 if (data->hSynchro) CloseHandle(data->hSynchro);
323 if (data->cells) HeapFree(GetProcessHeap(), 0, data->cells);
324 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
325 HeapFree(GetProcessHeap(), 0, data);
328 /******************************************************************
331 * Initialisation part I. Creation of server object (console input and
332 * active screen buffer)
334 static struct inner_data* WINECON_Init(HINSTANCE hInst, void* pid)
336 struct inner_data* data = NULL;
338 WCHAR szTitle[] = {'W','i','n','e',' ','c','o','n','s','o','l','e',0};
340 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
343 /* load default registry settings, and copy them into our current configuration */
344 WINECON_RegLoad(&data->defcfg);
345 data->curcfg = data->defcfg;
347 /* the handles here are created without the whistles and bells required by console
348 * (mainly because wineconsole doesn't need it)
349 * - there are not inheritable
350 * - hConIn is not synchronizable
352 SERVER_START_REQ(alloc_console)
354 req->access = GENERIC_READ | GENERIC_WRITE;
355 req->inherit = FALSE;
357 ret = !wine_server_call_err( req );
358 data->hConIn = (HANDLE)reply->handle_in;
359 data->hSynchro = (HANDLE)reply->event;
362 if (!ret) goto error;
364 SERVER_START_REQ( set_console_input_info )
366 req->handle = (handle_t)data->hConIn;
367 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
368 wine_server_add_data( req, szTitle, strlenW(szTitle) * sizeof(WCHAR) );
369 ret = !wine_server_call_err( req );
372 if (!ret) goto error;
374 SERVER_START_REQ(create_console_output)
376 req->handle_in = (handle_t)data->hConIn;
377 req->access = GENERIC_WRITE|GENERIC_READ;
378 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
379 req->inherit = FALSE;
380 data->hConOut = (HANDLE)(wine_server_call_err( req ) ? 0 : reply->handle_out);
383 if (data->hConOut) return data;
386 WINECON_Delete(data);
390 /******************************************************************
393 * Spawn the child processus when invoked with wineconsole foo bar
395 static BOOL WINECON_Spawn(struct inner_data* data, LPCSTR lpCmdLine)
397 PROCESS_INFORMATION info;
399 LPWSTR ptr = GetCommandLine(); /* we're unicode... */
402 /* we're in the case wineconsole <exe> <options>... spawn the new process */
403 memset(&startup, 0, sizeof(startup));
404 startup.cb = sizeof(startup);
405 startup.dwFlags = STARTF_USESTDHANDLES;
407 /* the attributes of wineconsole's handles are not adequate for inheritance, so
408 * get them with the correct attributes before process creation
410 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
411 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
412 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
413 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
414 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
415 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
417 Trace(0, "can't dup handles\n");
418 /* no need to delete handles, we're exiting the programm anyway */
422 /* we could have several ' ' in process command line... so try first space...
424 * the correct way would be to check the existence of the left part of ptr
427 while (*ptr && *ptr++ != ' ');
429 done = *ptr && CreateProcess(NULL, ptr, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
431 /* we no longer need the handles passed to the child for the console */
432 CloseHandle(startup.hStdInput);
433 CloseHandle(startup.hStdOutput);
434 CloseHandle(startup.hStdError);
439 /******************************************************************
444 static BOOL WINECON_HasEvent(LPCSTR ptr, unsigned *evt)
446 while (*ptr == ' ' || *ptr == '\t') ptr++;
447 if (strncmp(ptr, "--use-event=", 12)) return FALSE;
448 return sscanf(ptr + 12, "%d", evt) == 1;
451 /******************************************************************
454 * wineconsole can either be started as:
455 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
456 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
457 * a freshly created console
459 int PASCAL WINECON_WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPCSTR lpCmdLine, UINT nCmdShow)
461 struct inner_data* data;
465 /* case of wineconsole <evt>, signal process that created us that we're up and running */
466 if (WINECON_HasEvent(lpCmdLine, &evt))
468 if (!(data = WINECON_Init(hInst, 0))) return 0;
469 ret = SetEvent((HANDLE)evt);
473 if (!(data = WINECON_Init(hInst, (void*)GetCurrentProcessId()))) return 0;
474 ret = WINECON_Spawn(data, lpCmdLine);
477 if (ret && WCUSER_InitBackend(data))
479 ret = data->fnMainLoop(data);
481 WINECON_Delete(data);