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 static int trace_level = 1;
30 void XTracer(int level, const char* format, ...)
36 if (level > trace_level) return;
38 va_start(valist, format);
39 len = vsnprintf(buf, sizeof(buf), format, valist);
42 if ((len <= -1) || (len >= sizeof(buf)))
44 len = sizeof(buf) - 1;
46 buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
51 /******************************************************************
54 * updates the local copy of cells (band to update)
56 void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
58 SERVER_START_REQ( read_console_output )
60 req->handle = (handle_t)data->hConOut;
63 req->mode = CHAR_INFO_MODE_TEXTATTR;
65 wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
66 (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
67 wine_server_call( req );
70 data->fnRefresh(data, upd_tp, upd_bm);
73 /******************************************************************
74 * WINECON_NotifyWindowChange
76 * Inform server that visible window on sb has changed
78 void WINECON_NotifyWindowChange(struct inner_data* data)
80 SERVER_START_REQ( set_console_output_info )
82 req->handle = (handle_t)data->hConOut;
83 req->win_left = data->curcfg.win_pos.X;
84 req->win_top = data->curcfg.win_pos.Y;
85 req->win_right = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
86 req->win_bottom = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
87 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
88 wine_server_call( req );
93 /******************************************************************
94 * WINECON_GetHistorySize
98 int WINECON_GetHistorySize(HANDLE hConIn)
102 SERVER_START_REQ(get_console_input_info)
104 req->handle = (handle_t)hConIn;
105 if (!wine_server_call_err( req )) ret = reply->history_size;
111 /******************************************************************
112 * WINECON_SetHistorySize
116 BOOL WINECON_SetHistorySize(HANDLE hConIn, int size)
120 SERVER_START_REQ(set_console_input_info)
122 req->handle = (handle_t)hConIn;
123 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
124 req->history_size = size;
125 ret = !wine_server_call_err( req );
132 /******************************************************************
133 * WINECON_GetHistoryMode
137 int WINECON_GetHistoryMode(HANDLE hConIn)
141 SERVER_START_REQ(get_console_input_info)
143 req->handle = (handle_t)hConIn;
144 if (!wine_server_call_err( req )) ret = reply->history_mode;
150 /******************************************************************
151 * WINECON_SetHistoryMode
155 BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
159 SERVER_START_REQ(set_console_input_info)
161 req->handle = (handle_t)hConIn;
162 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
163 req->history_mode = mode;
164 ret = !wine_server_call_err( req );
170 /******************************************************************
171 * WINECON_GetConsoleTitle
175 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
179 if (len < sizeof(WCHAR)) return FALSE;
181 SERVER_START_REQ( get_console_input_info )
183 req->handle = (handle_t)hConIn;
184 wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
185 if ((ret = !wine_server_call_err( req )))
187 len = wine_server_reply_size( reply );
188 buffer[len / sizeof(WCHAR)] = 0;
195 /******************************************************************
196 * WINECON_GrabChanges
198 * A change occurs, try to figure out which
200 int WINECON_GrabChanges(struct inner_data* data)
202 struct console_renderer_event evts[256];
203 int i, num, curs = -1;
206 SERVER_START_REQ( get_console_renderer_events )
208 wine_server_set_reply( req, evts, sizeof(evts) );
209 req->handle = (handle_t)data->hSynchro;
210 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
214 if (!num) {Trace(0, "hmm renderer signaled but no events available\n"); return 1;}
216 /* FIXME: should do some event compression here (cursor pos, update) */
217 /* step 1: keep only last cursor pos event */
218 for (i = num - 1; i >= 0; i--)
220 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
226 memmove(&evts[i], &evts[i+1], (num - i - 1) * sizeof(evts[0]));
231 /* step 2: manage update events */
232 for (i = 0; i < num - 1; i++)
234 if (evts[i].event == CONSOLE_RENDERER_UPDATE_EVENT &&
235 evts[i+1].event == CONSOLE_RENDERER_UPDATE_EVENT)
238 if (evts[i].u.update.bottom + 1 == evts[i+1].u.update.top)
240 evts[i].u.update.bottom = evts[i+1].u.update.bottom;
241 memmove(&evts[i+1], &evts[i+2], (num - i - 2) * sizeof(evts[0]));
244 /* already handled cases */
245 else if (evts[i].u.update.top <= evts[i+1].u.update.top &&
246 evts[i].u.update.bottom >= evts[i+1].u.update.bottom)
248 memmove(&evts[i+1], &evts[i+2], (num - i - 2) * sizeof(evts[0]));
254 Trace(1, "Change notification:");
255 for (i = 0; i < num; i++)
257 switch (evts[i].event)
259 case CONSOLE_RENDERER_TITLE_EVENT:
260 data->fnSetTitle(data);
262 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
263 SERVER_START_REQ( open_console )
265 req->from = (int)data->hConIn;
266 req->access = GENERIC_READ | GENERIC_WRITE;
267 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
268 req->inherit = FALSE;
269 h = wine_server_call_err( req ) ? 0 : (HANDLE)reply->handle;
272 Trace(1, " active(%d)", (int)h);
275 CloseHandle(data->hConOut);
279 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
280 if (data->curcfg.sb_width != evts[i].u.resize.width ||
281 data->curcfg.sb_height != evts[i].u.resize.height)
283 Trace(1, " resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
284 data->curcfg.sb_width = evts[i].u.resize.width;
285 data->curcfg.sb_height = evts[i].u.resize.height;
287 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
288 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
289 if (!data->cells) {Trace(0, "OOM\n"); exit(0);}
290 data->fnResizeScreenBuffer(data);
291 data->fnComputePositions(data);
294 case CONSOLE_RENDERER_UPDATE_EVENT:
295 Trace(1, " update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
296 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
298 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
299 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
301 data->cursor.X = evts[i].u.cursor_pos.x;
302 data->cursor.Y = evts[i].u.cursor_pos.y;
303 data->fnPosCursor(data);
304 Trace(1, " curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
307 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
308 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
309 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
311 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
312 evts[i].u.cursor_geom.visible, FALSE);
313 Trace(1, " curs-geom(%d,%d)",
314 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
317 case CONSOLE_RENDERER_DISPLAY_EVENT:
318 if (evts[i].u.display.left != data->curcfg.win_pos.X)
320 data->fnScroll(data, evts[i].u.display.left, TRUE);
321 data->fnPosCursor(data);
322 Trace(1, " h-scroll(%d)", evts[i].u.display.left);
324 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
326 data->fnScroll(data, evts[i].u.display.top, FALSE);
327 data->fnPosCursor(data);
328 Trace(1, " v-scroll(%d)", evts[i].u.display.top);
330 if (evts[i].u.display.width != data->curcfg.win_width ||
331 evts[i].u.display.height != data->curcfg.win_height)
333 Trace(1, " win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
334 data->curcfg.win_width = evts[i].u.display.width;
335 data->curcfg.win_height = evts[i].u.display.height;
336 data->fnComputePositions(data);
339 case CONSOLE_RENDERER_EXIT_EVENT:
340 Trace(1, ". Exit!!\n");
343 Trace(0, "Unknown event type (%d)\n", evts[i].event);
347 Trace(1, ". Done\n");
351 /******************************************************************
354 * Destroy wineconsole internal data
356 static void WINECON_Delete(struct inner_data* data)
360 if (data->hConIn) CloseHandle(data->hConIn);
361 if (data->hConOut) CloseHandle(data->hConOut);
362 if (data->hSynchro) CloseHandle(data->hSynchro);
363 if (data->cells) HeapFree(GetProcessHeap(), 0, data->cells);
364 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
365 HeapFree(GetProcessHeap(), 0, data);
368 /******************************************************************
371 * Initialisation part I. Creation of server object (console input and
372 * active screen buffer)
374 static struct inner_data* WINECON_Init(HINSTANCE hInst, void* pid)
376 struct inner_data* data = NULL;
378 WCHAR szTitle[] = {'W','i','n','e',' ','c','o','n','s','o','l','e',0};
380 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
383 /* load default registry settings, and copy them into our current configuration */
384 WINECON_RegLoad(&data->defcfg);
385 data->curcfg = data->defcfg;
387 /* the handles here are created without the whistles and bells required by console
388 * (mainly because wineconsole doesn't need it)
389 * - there are not inheritable
390 * - hConIn is not synchronizable
392 SERVER_START_REQ(alloc_console)
394 req->access = GENERIC_READ | GENERIC_WRITE;
395 req->inherit = FALSE;
397 ret = !wine_server_call_err( req );
398 data->hConIn = (HANDLE)reply->handle_in;
399 data->hSynchro = (HANDLE)reply->event;
402 if (!ret) goto error;
404 SERVER_START_REQ( set_console_input_info )
406 req->handle = (handle_t)data->hConIn;
407 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
408 wine_server_add_data( req, szTitle, strlenW(szTitle) * sizeof(WCHAR) );
409 ret = !wine_server_call_err( req );
412 if (!ret) goto error;
414 SERVER_START_REQ(create_console_output)
416 req->handle_in = (handle_t)data->hConIn;
417 req->access = GENERIC_WRITE|GENERIC_READ;
418 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
419 req->inherit = FALSE;
420 data->hConOut = (HANDLE)(wine_server_call_err( req ) ? 0 : reply->handle_out);
423 if (data->hConOut) return data;
426 WINECON_Delete(data);
430 /******************************************************************
433 * Spawn the child processus when invoked with wineconsole foo bar
435 static BOOL WINECON_Spawn(struct inner_data* data, LPCSTR lpCmdLine)
437 PROCESS_INFORMATION info;
439 LPWSTR ptr = GetCommandLine(); /* we're unicode... */
442 /* we're in the case wineconsole <exe> <options>... spawn the new process */
443 memset(&startup, 0, sizeof(startup));
444 startup.cb = sizeof(startup);
445 startup.dwFlags = STARTF_USESTDHANDLES;
447 /* the attributes of wineconsole's handles are not adequate for inheritance, so
448 * get them with the correct attributes before process creation
450 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
451 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
452 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
453 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
454 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
455 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
457 Trace(0, "can't dup handles\n");
458 /* no need to delete handles, we're exiting the programm anyway */
462 /* we could have several ' ' in process command line... so try first space...
464 * the correct way would be to check the existence of the left part of ptr
467 while (*ptr && *ptr++ != ' ');
469 done = *ptr && CreateProcess(NULL, ptr, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
471 /* we no longer need the handles passed to the child for the console */
472 CloseHandle(startup.hStdInput);
473 CloseHandle(startup.hStdOutput);
474 CloseHandle(startup.hStdError);
479 /******************************************************************
484 static BOOL WINECON_HasEvent(LPCSTR ptr, unsigned *evt)
486 while (*ptr == ' ' || *ptr == '\t') ptr++;
487 if (strncmp(ptr, "--use-event=", 12)) return FALSE;
488 return sscanf(ptr + 12, "%d", evt) == 1;
491 /******************************************************************
494 * wineconsole can either be started as:
495 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
496 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
497 * a freshly created console
499 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
501 struct inner_data* data;
505 /* case of wineconsole <evt>, signal process that created us that we're up and running */
506 if (WINECON_HasEvent(lpCmdLine, &evt))
508 if (!(data = WINECON_Init(hInst, 0))) return 0;
509 ret = SetEvent((HANDLE)evt);
513 if (!(data = WINECON_Init(hInst, (void*)GetCurrentProcessId()))) return 0;
514 ret = WINECON_Spawn(data, lpCmdLine);
517 if (ret && WCUSER_InitBackend(data))
519 ret = data->fnMainLoop(data);
521 WINECON_Delete(data);