TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / programs / wineconsole / wineconsole.c
blob7a89eebaed52d245efb9b7a80a5254d756e220e8
1 /*
2 * an application for displaying Win32 console
4 * Copyright 2001, 2002 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include "wine/server.h"
27 #include "winecon_private.h"
28 #include "winnls.h"
29 #include "winuser.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
35 void WINECON_Fatal(const char* msg)
37 WINE_ERR("%s\n", msg);
38 ExitProcess(0);
41 static void printf_res(UINT uResId, ...)
43 WCHAR buffer[1024];
44 CHAR ansi[1024];
45 va_list args;
47 va_start(args, uResId);
48 LoadStringW(GetModuleHandleW(NULL), uResId, buffer, sizeof(buffer)/sizeof(buffer[0]));
49 WideCharToMultiByte(CP_UNIXCP, 0, buffer, -1, ansi, sizeof(ansi), NULL, NULL);
50 vprintf(ansi, args);
51 va_end(args);
54 static void WINECON_Usage(void)
56 printf_res(IDS_USAGE_HEADER);
57 printf_res(IDS_USAGE_BACKEND);
58 printf_res(IDS_USAGE_COMMAND);
59 printf_res(IDS_USAGE_FOOTER);
62 /******************************************************************
63 * WINECON_FetchCells
65 * updates the local copy of cells (band to update)
67 static void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
69 SERVER_START_REQ( read_console_output )
71 req->handle = wine_server_obj_handle( data->hConOut );
72 req->x = 0;
73 req->y = upd_tp;
74 req->mode = CHAR_INFO_MODE_TEXTATTR;
75 req->wrap = TRUE;
76 wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
77 (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
78 wine_server_call( req );
80 SERVER_END_REQ;
81 data->fnRefresh(data, upd_tp, upd_bm);
84 /******************************************************************
85 * WINECON_ResizeWithContainer
87 * For console embedded in a container (e.g. user in a win32 window, or (n)curses
88 * in a TERM, perform resize of console (screen buffer and window) to fit in
89 * (new) container size.
91 void WINECON_ResizeWithContainer(struct inner_data* data, int width, int height)
93 struct config_data cfg;
95 if (data->in_set_config) return;
97 cfg = data->curcfg;
98 cfg.win_width = width;
99 cfg.win_height = height;
101 /* auto size screen-buffer if it's now smaller than window */
102 if (cfg.sb_width < cfg.win_width) cfg.sb_width = cfg.win_width;
103 if (cfg.sb_height < cfg.win_height) cfg.sb_height = cfg.win_height;
105 /* and reset window pos so that we don't display outside of the screen-buffer */
106 if (cfg.win_pos.X + cfg.win_width > cfg.sb_width) cfg.win_pos.X = cfg.sb_width - cfg.win_width;
107 if (cfg.win_pos.Y + cfg.win_height > cfg.sb_height) cfg.win_pos.Y = cfg.sb_height - cfg.win_height;
109 WINECON_SetConfig(data, &cfg);
112 /******************************************************************
113 * WINECON_SetHistorySize
117 static BOOL WINECON_SetHistorySize(HANDLE hConIn, int size)
119 BOOL ret;
121 SERVER_START_REQ(set_console_input_info)
123 req->handle = wine_server_obj_handle( hConIn );
124 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
125 req->history_size = size;
126 ret = !wine_server_call_err( req );
128 SERVER_END_REQ;
129 return ret;
132 /******************************************************************
133 * WINECON_SetHistoryMode
137 static BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
139 BOOL ret;
141 SERVER_START_REQ(set_console_input_info)
143 req->handle = wine_server_obj_handle( hConIn );
144 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
145 req->history_mode = mode;
146 ret = !wine_server_call_err( req );
148 SERVER_END_REQ;
149 return ret;
152 /******************************************************************
153 * WINECON_SetInsertMode
157 static void WINECON_SetInsertMode(HANDLE hConIn, unsigned int enable)
159 DWORD mode;
161 GetConsoleMode(hConIn, &mode);
162 if (enable)
163 mode |= ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS;
164 else
165 mode &= ~ENABLE_INSERT_MODE;
166 SetConsoleMode(hConIn, mode);
169 /******************************************************************
170 * WINECON_GetConsoleTitle
174 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
176 BOOL ret;
178 if (len < sizeof(WCHAR)) return FALSE;
180 SERVER_START_REQ( get_console_input_info )
182 req->handle = wine_server_obj_handle( hConIn );
183 wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
184 if ((ret = !wine_server_call_err( req )))
186 len = wine_server_reply_size( reply );
187 buffer[len / sizeof(WCHAR)] = 0;
190 SERVER_END_REQ;
191 return ret;
194 /******************************************************************
195 * WINECON_SetEditionMode
199 static BOOL WINECON_SetEditionMode(HANDLE hConIn, int edition_mode)
201 BOOL ret;
203 SERVER_START_REQ( set_console_input_info )
205 req->handle = wine_server_obj_handle( hConIn );
206 req->mask = SET_CONSOLE_INPUT_INFO_EDITION_MODE;
207 req->edition_mode = edition_mode;
208 ret = !wine_server_call_err( req );
210 SERVER_END_REQ;
211 return ret;
214 /******************************************************************
215 * WINECON_GrabChanges
217 * A change occurs, try to figure out which
219 void WINECON_GrabChanges(struct inner_data* data)
221 struct console_renderer_event evts[256];
222 int i, num, ev_found;
223 HANDLE h;
225 if (data->in_grab_changes) return;
227 SERVER_START_REQ( get_console_renderer_events )
229 wine_server_set_reply( req, evts, sizeof(evts) );
230 req->handle = wine_server_obj_handle( data->hSynchro );
231 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
232 else num = 0;
234 SERVER_END_REQ;
235 if (!num) {WINE_WARN("hmm renderer signaled but no events available\n"); return;}
236 WINE_TRACE( "got %u events\n", num );
238 /* FIXME: should do some event compression here (cursor pos, update) */
239 /* step 1: keep only last cursor pos event */
240 ev_found = -1;
241 for (i = num - 1; i >= 0; i--)
243 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
245 if (ev_found != -1)
247 WINE_TRACE("%u/%u: curs-pos(%d,%d) ignoring\n", i+1, num, evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
248 evts[i].event = CONSOLE_RENDERER_NONE_EVENT;
250 ev_found = i;
253 /* step 2: manage update events */
254 ev_found = -1;
255 for (i = 0; i < num; i++)
257 if (evts[i].event == CONSOLE_RENDERER_NONE_EVENT ||
258 evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT ||
259 evts[i].event == CONSOLE_RENDERER_CURSOR_GEOM_EVENT) continue;
260 if (evts[i].event != CONSOLE_RENDERER_UPDATE_EVENT)
262 ev_found = -1;
263 continue;
266 if (ev_found != -1 && /* Only 2 cases where they CANNOT merge */
267 !(evts[i ].u.update.bottom + 1 < evts[ev_found].u.update.top ||
268 evts[ev_found].u.update.bottom + 1 < evts[i ].u.update.top))
270 WINE_TRACE("%u/%u: update(%d,%d) merging with %u\n", ev_found+1, num, evts[i].u.update.top, evts[i].u.update.bottom, i+1);
271 evts[i].u.update.top = min(evts[i ].u.update.top,
272 evts[ev_found].u.update.top);
273 evts[i].u.update.bottom = max(evts[i ].u.update.bottom,
274 evts[ev_found].u.update.bottom);
275 evts[ev_found].event = CONSOLE_RENDERER_NONE_EVENT;
277 ev_found = i;
280 data->in_grab_changes = TRUE;
281 for (i = 0; i < num; i++)
283 switch (evts[i].event)
285 case CONSOLE_RENDERER_NONE_EVENT:
286 WINE_TRACE("%u/%u: NOP\n", i+1, num);
287 break;
288 case CONSOLE_RENDERER_TITLE_EVENT:
289 WINE_TRACE("%u/%u: title()\n", i+1, num);
290 data->fnSetTitle(data);
291 break;
292 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
293 SERVER_START_REQ( open_console )
295 req->from = wine_server_obj_handle( data->hConIn );
296 req->access = GENERIC_READ | GENERIC_WRITE;
297 req->attributes = 0;
298 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
299 h = wine_server_call_err( req ) ? 0 : wine_server_ptr_handle(reply->handle);
301 SERVER_END_REQ;
302 WINE_TRACE("%u/%u: active(%p)\n", i+1, num, h);
303 if (h)
305 CloseHandle(data->hConOut);
306 data->hConOut = h;
308 break;
309 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
310 if (data->curcfg.sb_width != evts[i].u.resize.width ||
311 data->curcfg.sb_height != evts[i].u.resize.height)
313 WINE_TRACE("%u/%u: resize(%d,%d)\n", i+1, num, evts[i].u.resize.width, evts[i].u.resize.height);
314 data->curcfg.sb_width = evts[i].u.resize.width;
315 data->curcfg.sb_height = evts[i].u.resize.height;
317 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
318 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
320 if (!data->cells) WINECON_Fatal("OOM\n");
321 data->fnResizeScreenBuffer(data);
322 data->fnComputePositions(data);
324 break;
325 case CONSOLE_RENDERER_UPDATE_EVENT:
326 WINE_TRACE("%u/%u: update(%d,%d)\n", i+1, num, evts[i].u.update.top, evts[i].u.update.bottom);
327 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
328 break;
329 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
330 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
332 WINE_TRACE("%u/%u: curs-pos(%d,%d)\n", i+1, num, evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
333 data->cursor.X = evts[i].u.cursor_pos.x;
334 data->cursor.Y = evts[i].u.cursor_pos.y;
335 data->fnPosCursor(data);
337 break;
338 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
339 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
340 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
342 WINE_TRACE("%u/%u: curs-geom(%d,%d)\n", i+1, num,
343 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
344 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
345 evts[i].u.cursor_geom.visible, FALSE);
347 break;
348 case CONSOLE_RENDERER_DISPLAY_EVENT:
349 if (evts[i].u.display.left != data->curcfg.win_pos.X)
351 WINE_TRACE("%u/%u: h-scroll(%d)\n", i+1, num, evts[i].u.display.left);
352 data->fnScroll(data, evts[i].u.display.left, TRUE);
353 data->fnPosCursor(data);
355 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
357 WINE_TRACE("%u/%u: v-scroll(%d)\n", i+1, num, evts[i].u.display.top);
358 data->fnScroll(data, evts[i].u.display.top, FALSE);
359 data->fnPosCursor(data);
361 if (evts[i].u.display.width != data->curcfg.win_width ||
362 evts[i].u.display.height != data->curcfg.win_height)
364 WINE_TRACE("%u/%u: win-size(%d,%d)\n", i+1, num, evts[i].u.display.width, evts[i].u.display.height);
365 data->curcfg.win_width = evts[i].u.display.width;
366 data->curcfg.win_height = evts[i].u.display.height;
367 data->fnComputePositions(data);
369 break;
370 case CONSOLE_RENDERER_EXIT_EVENT:
371 data->dying = TRUE;
372 WINE_TRACE("%u/%u: Exit!!\n", i+1, num);
373 return;
374 default:
375 WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
378 data->in_grab_changes = FALSE;
381 /******************************************************************
382 * WINECON_SetConfig
384 * Apply to data all the configuration elements from cfg. This includes modification
385 * of server side equivalent and visual parts.
386 * If force is FALSE, only the changed items are modified.
388 void WINECON_SetConfig(struct inner_data* data, const struct config_data* cfg)
390 if (data->in_set_config) return;
391 data->in_set_config = TRUE;
392 if (data->curcfg.cursor_size != cfg->cursor_size ||
393 data->curcfg.cursor_visible != cfg->cursor_visible)
395 CONSOLE_CURSOR_INFO cinfo;
396 cinfo.dwSize = cfg->cursor_size;
397 /* <FIXME>: this hack is needed to pass through the invariant test operation on the server side
398 * (no notification is sent when invariant operation is requested)
400 cinfo.bVisible = !cfg->cursor_visible;
401 SetConsoleCursorInfo(data->hConOut, &cinfo);
402 /* </FIXME> */
403 cinfo.bVisible = cfg->cursor_visible;
404 /* this shall update (through notif) curcfg */
405 SetConsoleCursorInfo(data->hConOut, &cinfo);
407 if (data->curcfg.history_size != cfg->history_size)
409 data->curcfg.history_size = cfg->history_size;
410 WINECON_SetHistorySize(data->hConIn, cfg->history_size);
412 if (data->curcfg.history_nodup != cfg->history_nodup)
414 data->curcfg.history_nodup = cfg->history_nodup;
415 WINECON_SetHistoryMode(data->hConIn, cfg->history_nodup);
417 if (data->curcfg.insert_mode != cfg->insert_mode)
419 data->curcfg.insert_mode = cfg->insert_mode;
420 WINECON_SetInsertMode(data->hConIn, cfg->insert_mode);
422 data->curcfg.menu_mask = cfg->menu_mask;
423 data->curcfg.quick_edit = cfg->quick_edit;
424 if (strcmpiW(data->curcfg.face_name, cfg->face_name) || data->curcfg.cell_width != cfg->cell_width ||
425 data->curcfg.cell_height != cfg->cell_height || data->curcfg.font_weight != cfg->font_weight)
427 data->fnSetFont(data, cfg->face_name, cfg->cell_height, cfg->font_weight);
428 SERVER_START_REQ(set_console_output_info)
430 req->handle = wine_server_obj_handle( data->hConOut );
431 req->mask = SET_CONSOLE_OUTPUT_INFO_FONT;
432 req->font_width = cfg->cell_width;
433 req->font_height = cfg->cell_height;
434 wine_server_call( req );
436 SERVER_END_REQ;
438 if (data->curcfg.def_attr != cfg->def_attr)
440 data->curcfg.def_attr = cfg->def_attr;
441 SetConsoleTextAttribute(data->hConOut, cfg->def_attr);
443 /* now let's look at the window / sb size changes...
444 * since the server checks that sb is always bigger than window,
445 * we have to take care of doing the operations in the right order
447 /* a set of macros to make things easier to read
448 * The Test<A><B> macros test if the <A> (width/height) needs to be changed
449 * for <B> (window / ScreenBuffer)
450 * The Change<A><B> actually modify the <B> dimension of <A>.
452 #define TstSBfWidth() (data->curcfg.sb_width != cfg->sb_width)
453 #define TstWinHPos() (data->curcfg.win_width != cfg->win_width || data->curcfg.win_pos.X != cfg->win_pos.X)
455 #define ChgSBfWidth() do {c.X = cfg->sb_width; \
456 c.Y = data->curcfg.sb_height;\
457 SetConsoleScreenBufferSize(data->hConOut, c);\
458 } while (0)
459 #define ChgWinHPos() do {pos.Left = cfg->win_pos.X - data->curcfg.win_pos.X; \
460 pos.Top = 0; \
461 pos.Right = pos.Left + cfg->win_width - data->curcfg.win_width; \
462 pos.Bottom = 0; \
463 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
464 } while (0)
465 #define TstSBfHeight() (data->curcfg.sb_height != cfg->sb_height)
466 #define TstWinVPos() (data->curcfg.win_height != cfg->win_height || data->curcfg.win_pos.Y != cfg->win_pos.Y)
468 /* since we're going to apply height after width is done, we use width as defined
469 * in cfg, and not in data->curcfg because if won't be updated yet */
470 #define ChgSBfHeight() do {c.X = cfg->sb_width; c.Y = cfg->sb_height; \
471 SetConsoleScreenBufferSize(data->hConOut, c); \
472 } while (0)
473 #define ChgWinVPos() do {pos.Left = 0; \
474 pos.Top = cfg->win_pos.Y - data->curcfg.win_pos.Y; \
475 pos.Right = 0; \
476 pos.Bottom = pos.Top + cfg->win_height - data->curcfg.win_height; \
477 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
478 } while (0)
482 COORD c;
483 SMALL_RECT pos;
485 if (TstSBfWidth())
487 if (TstWinHPos())
489 /* we're changing both at the same time, do it in the right order */
490 if (cfg->sb_width >= data->curcfg.win_width)
492 ChgSBfWidth(); ChgWinHPos();
494 else
496 ChgWinHPos(); ChgSBfWidth();
499 else ChgSBfWidth();
501 else if (TstWinHPos()) ChgWinHPos();
502 if (TstSBfHeight())
504 if (TstWinVPos())
506 if (cfg->sb_height >= data->curcfg.win_height)
508 ChgSBfHeight(); ChgWinVPos();
510 else
512 ChgWinVPos(); ChgSBfHeight();
515 else ChgSBfHeight();
517 else if (TstWinVPos()) ChgWinVPos();
518 } while (0);
519 #undef TstSBfWidth
520 #undef TstWinHPos
521 #undef ChgSBfWidth
522 #undef ChgWinHPos
523 #undef TstSBfHeight
524 #undef TstWinVPos
525 #undef ChgSBfHeight
526 #undef ChgWinVPos
528 data->curcfg.exit_on_die = cfg->exit_on_die;
529 if (data->curcfg.edition_mode != cfg->edition_mode)
531 data->curcfg.edition_mode = cfg->edition_mode;
532 WINECON_SetEditionMode(data->hConIn, cfg->edition_mode);
534 /* we now need to gather all events we got from the operations above,
535 * in order to get data correctly updated
537 WINECON_GrabChanges(data);
538 data->in_set_config = FALSE;
541 /******************************************************************
542 * WINECON_Delete
544 * Destroy wineconsole internal data
546 static void WINECON_Delete(struct inner_data* data)
548 if (!data) return;
550 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
551 if (data->hConIn) CloseHandle(data->hConIn);
552 if (data->hConOut) CloseHandle(data->hConOut);
553 if (data->hSynchro) CloseHandle(data->hSynchro);
554 if (data->hProcess) CloseHandle(data->hProcess);
555 HeapFree(GetProcessHeap(), 0, data->curcfg.registry);
556 HeapFree(GetProcessHeap(), 0, data->cells);
557 HeapFree(GetProcessHeap(), 0, data);
560 /******************************************************************
561 * WINECON_GetServerConfig
563 * Fills data->curcfg with the actual configuration running in the server
564 * (getting real information on the server, and not relying on cached
565 * information in data)
567 static BOOL WINECON_GetServerConfig(struct inner_data* data)
569 BOOL ret;
570 DWORD mode;
572 SERVER_START_REQ(get_console_input_info)
574 req->handle = wine_server_obj_handle( data->hConIn );
575 ret = !wine_server_call_err( req );
576 data->curcfg.history_size = reply->history_size;
577 data->curcfg.history_nodup = reply->history_mode;
578 data->curcfg.edition_mode = reply->edition_mode;
580 SERVER_END_REQ;
581 if (!ret) return FALSE;
583 GetConsoleMode(data->hConIn, &mode);
584 data->curcfg.insert_mode = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) ==
585 (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS);
587 SERVER_START_REQ(get_console_output_info)
589 req->handle = wine_server_obj_handle( data->hConOut );
590 ret = !wine_server_call_err( req );
591 data->curcfg.cursor_size = reply->cursor_size;
592 data->curcfg.cursor_visible = reply->cursor_visible;
593 data->curcfg.def_attr = reply->attr;
594 data->curcfg.sb_width = reply->width;
595 data->curcfg.sb_height = reply->height;
596 data->curcfg.win_width = reply->win_right - reply->win_left + 1;
597 data->curcfg.win_height = reply->win_bottom - reply->win_top + 1;
599 SERVER_END_REQ;
600 WINECON_DumpConfig("first cfg: ", &data->curcfg);
602 return ret;
605 /******************************************************************
606 * WINECON_Init
608 * Initialisation part I. Creation of server object (console input and
609 * active screen buffer)
611 static struct inner_data* WINECON_Init(HINSTANCE hInst, DWORD pid, LPCWSTR appname,
612 enum init_return (*backend)(struct inner_data*),
613 INT nCmdShow)
615 struct inner_data* data = NULL;
616 DWORD ret;
617 struct config_data cfg;
618 STARTUPINFOW si;
620 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
621 if (!data) return 0;
623 GetStartupInfoW(&si);
625 if (pid == 0)
627 if (!si.lpTitle) WINECON_Fatal("Should have a title set");
628 appname = si.lpTitle;
631 data->nCmdShow = nCmdShow;
632 /* load settings */
633 WINECON_RegLoad(appname, &cfg);
635 /* some overrides */
636 if (pid == 0)
638 if (si.dwFlags & STARTF_USECOUNTCHARS)
640 cfg.sb_width = si.dwXCountChars;
641 cfg.sb_height = si.dwYCountChars;
643 if (si.dwFlags & STARTF_USEFILLATTRIBUTE)
644 cfg.def_attr = si.dwFillAttribute;
645 /* should always be defined */
648 /* the handles here are created without the whistles and bells required by console
649 * (mainly because wineconsole doesn't need it)
650 * - they are not inheritable
651 * - hConIn is not synchronizable
653 SERVER_START_REQ(alloc_console)
655 req->access = GENERIC_READ | GENERIC_WRITE;
656 req->attributes = 0;
657 req->pid = pid;
658 req->input_fd = -1;
660 ret = !wine_server_call_err( req );
661 data->hConIn = wine_server_ptr_handle( reply->handle_in );
662 data->hSynchro = wine_server_ptr_handle( reply->event );
664 SERVER_END_REQ;
665 if (!ret) goto error;
666 WINE_TRACE("using hConIn %p, hSynchro event %p\n", data->hConIn, data->hSynchro);
668 SERVER_START_REQ(create_console_output)
670 req->handle_in = wine_server_obj_handle( data->hConIn );
671 req->access = GENERIC_WRITE|GENERIC_READ;
672 req->attributes = 0;
673 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
674 req->fd = -1;
675 ret = !wine_server_call_err( req );
676 data->hConOut = wine_server_ptr_handle( reply->handle_out );
678 SERVER_END_REQ;
679 if (!ret) goto error;
680 WINE_TRACE("using hConOut %p\n", data->hConOut);
682 /* filling data->curcfg from cfg */
683 switch ((*backend)(data))
685 case init_not_supported:
686 if (backend == WCCURSES_InitBackend)
688 if (WCUSER_InitBackend( data ) != init_success) break;
690 else if (backend == WCUSER_InitBackend)
692 if (WCCURSES_InitBackend( data ) != init_success) break;
694 /* fall through */
695 case init_success:
696 WINECON_GetServerConfig(data);
697 data->cells = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
698 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
699 if (!data->cells) WINECON_Fatal("OOM\n");
700 data->fnResizeScreenBuffer(data);
701 data->fnComputePositions(data);
702 WINECON_SetConfig(data, &cfg);
703 data->curcfg.registry = cfg.registry;
704 WINECON_DumpConfig("fint", &data->curcfg);
705 SERVER_START_REQ( set_console_input_info )
707 req->handle = wine_server_obj_handle( data->hConIn );
708 req->win = wine_server_user_handle( data->hWnd );
709 req->mask = SET_CONSOLE_INPUT_INFO_TITLE |
710 SET_CONSOLE_INPUT_INFO_WIN;
711 wine_server_add_data( req, appname, lstrlenW(appname) * sizeof(WCHAR) );
712 ret = !wine_server_call_err( req );
714 SERVER_END_REQ;
715 if (!ret) goto error;
717 return data;
718 case init_failed:
719 break;
722 error:
723 WINE_ERR("failed to init.\n");
725 WINECON_Delete(data);
726 return NULL;
729 /******************************************************************
730 * WINECON_Spawn
732 * Spawn the child process when invoked with wineconsole foo bar
734 static int WINECON_Spawn(struct inner_data* data, LPWSTR cmdLine)
736 PROCESS_INFORMATION info;
737 STARTUPINFOW startup;
738 BOOL done;
740 /* we're in the case wineconsole <exe> <options>... spawn the new process */
741 memset(&startup, 0, sizeof(startup));
742 startup.cb = sizeof(startup);
743 startup.dwFlags = STARTF_USESTDHANDLES;
745 /* the attributes of wineconsole's handles are not adequate for inheritance, so
746 * get them with the correct attributes before process creation
748 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
749 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
750 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
751 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
752 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
753 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
755 WINE_ERR("Can't dup handles\n");
756 /* no need to delete handles, we're exiting the program anyway */
757 return 1;
760 done = CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
761 if (done)
763 data->hProcess = info.hProcess;
764 CloseHandle(info.hThread);
767 /* we no longer need the handles passed to the child for the console */
768 CloseHandle(startup.hStdInput);
769 CloseHandle(startup.hStdOutput);
770 CloseHandle(startup.hStdError);
772 return !done;
775 struct wc_init {
776 LPCSTR ptr;
777 enum {from_event, from_process_name} mode;
778 enum init_return (*backend)(struct inner_data*);
779 HANDLE event;
782 #define WINECON_CMD_SHOW_USAGE 0x10000
784 /******************************************************************
785 * WINECON_ParseOptions
787 * RETURNS
788 * On success: 0
789 * On error: error string id optionally with the CMD_SHOW_USAGE flag
791 static UINT WINECON_ParseOptions(const char* lpCmdLine, struct wc_init* wci)
793 memset(wci, 0, sizeof(*wci));
794 wci->ptr = lpCmdLine;
795 wci->mode = from_process_name;
796 wci->backend = WCUSER_InitBackend;
798 for (;;)
800 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
801 if (wci->ptr[0] != '-') break;
802 if (strncmp(wci->ptr, "--use-event=", 12) == 0)
804 char* end;
805 wci->event = ULongToHandle( strtoul(wci->ptr + 12, &end, 10) );
806 if (end == wci->ptr + 12) return IDS_CMD_INVALID_EVENT_ID;
807 wci->mode = from_event;
808 wci->ptr = end;
810 else if (strncmp(wci->ptr, "--backend=", 10) == 0)
812 if (strncmp(wci->ptr + 10, "user", 4) == 0)
814 wci->backend = WCUSER_InitBackend;
815 wci->ptr += 14;
817 else if (strncmp(wci->ptr + 10, "curses", 6) == 0)
819 wci->backend = WCCURSES_InitBackend;
820 wci->ptr += 16;
822 else
823 return IDS_CMD_INVALID_BACKEND;
825 else if (!strncmp(wci->ptr, "--help", 6) &&
826 (!wci->ptr[6] || wci->ptr[6] == ' ' || wci->ptr[6] == '\t'))
827 return IDS_CMD_ABOUT|WINECON_CMD_SHOW_USAGE;
828 else
829 return IDS_CMD_INVALID_OPTION|WINECON_CMD_SHOW_USAGE;
832 if (wci->mode == from_event)
833 return 0;
835 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
836 if (*wci->ptr == 0) wci->ptr = "cmd";
838 return 0;
841 /******************************************************************
842 * WinMain
844 * wineconsole can either be started as:
845 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
846 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
847 * a freshly created console
848 * --backend=(curses|user) can also be used to select the desired backend
850 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
852 struct inner_data* data;
853 int ret = 1;
854 struct wc_init wci;
856 if ((ret = WINECON_ParseOptions(lpCmdLine, &wci)) != 0)
858 printf_res(ret & 0xffff);
859 if (ret & WINECON_CMD_SHOW_USAGE)
860 WINECON_Usage();
861 return 0;
864 switch (wci.mode)
866 case from_event:
867 /* case of wineconsole <evt>, signal process that created us that we're up and running */
868 if (!(data = WINECON_Init(hInst, 0, NULL, wci.backend, nCmdShow))) return 1;
869 ret = !SetEvent(wci.event);
870 if (ret != 0) WINE_ERR("SetEvent failed.\n");
871 break;
872 case from_process_name:
874 int len;
875 WCHAR *buffer;
877 len = MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, NULL, 0);
879 buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
880 if (!buffer)
881 return 1;
883 MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, buffer, len);
885 if (!(data = WINECON_Init(hInst, GetCurrentProcessId(), buffer, wci.backend, nCmdShow)))
887 HeapFree(GetProcessHeap(), 0, buffer);
888 return 1;
890 ret = WINECON_Spawn(data, buffer);
891 HeapFree(GetProcessHeap(), 0, buffer);
892 if (ret != 0)
894 WINECON_Delete(data);
895 printf_res(IDS_CMD_LAUNCH_FAILED, wine_dbgstr_a(wci.ptr));
896 return ret;
899 break;
900 default:
901 return 1;
904 if (!ret)
906 DWORD exitcode;
908 WINE_TRACE("calling MainLoop.\n");
909 ret = data->fnMainLoop(data);
911 if (!ret && data->hProcess &&
912 WaitForSingleObject(data->hProcess, INFINITE) == WAIT_OBJECT_0 &&
913 GetExitCodeProcess(data->hProcess, &exitcode))
915 WINE_TRACE("forwarding exitcode %u from child process\n", exitcode);
916 ret = exitcode;
920 WINECON_Delete(data);
922 return ret;