Moved a number of 16-bit functions to file16.c.
[wine.git] / programs / wineconsole / wineconsole.c
blobbbe6d40383d4f7fc9f9b664ed9f056c653f553c3
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include "wine/server.h"
26 #include "winecon_private.h"
27 #include "winnls.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
33 void WINECON_Fatal(const char* msg)
35 WINE_ERR("%s\n", msg);
36 ExitProcess(0);
39 /******************************************************************
40 * WINECON_FetchCells
42 * updates the local copy of cells (band to update)
44 void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
46 SERVER_START_REQ( read_console_output )
48 req->handle = (obj_handle_t)data->hConOut;
49 req->x = 0;
50 req->y = upd_tp;
51 req->mode = CHAR_INFO_MODE_TEXTATTR;
52 req->wrap = TRUE;
53 wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
54 (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
55 wine_server_call( req );
57 SERVER_END_REQ;
58 data->fnRefresh(data, upd_tp, upd_bm);
61 /******************************************************************
62 * WINECON_NotifyWindowChange
64 * Inform server that visible window on sb has changed
66 void WINECON_NotifyWindowChange(struct inner_data* data)
68 SERVER_START_REQ( set_console_output_info )
70 req->handle = (obj_handle_t)data->hConOut;
71 req->win_left = data->curcfg.win_pos.X;
72 req->win_top = data->curcfg.win_pos.Y;
73 req->win_right = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
74 req->win_bottom = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
75 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
76 wine_server_call( req );
78 SERVER_END_REQ;
81 /******************************************************************
82 * WINECON_GetHistorySize
86 int WINECON_GetHistorySize(HANDLE hConIn)
88 int ret = 0;
90 SERVER_START_REQ(get_console_input_info)
92 req->handle = (obj_handle_t)hConIn;
93 if (!wine_server_call_err( req )) ret = reply->history_size;
95 SERVER_END_REQ;
96 return ret;
99 /******************************************************************
100 * WINECON_SetHistorySize
104 BOOL WINECON_SetHistorySize(HANDLE hConIn, int size)
106 BOOL ret;
108 SERVER_START_REQ(set_console_input_info)
110 req->handle = (obj_handle_t)hConIn;
111 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
112 req->history_size = size;
113 ret = !wine_server_call_err( req );
115 SERVER_END_REQ;
116 return ret;
120 /******************************************************************
121 * WINECON_GetHistoryMode
125 int WINECON_GetHistoryMode(HANDLE hConIn)
127 int ret = 0;
129 SERVER_START_REQ(get_console_input_info)
131 req->handle = (obj_handle_t)hConIn;
132 if (!wine_server_call_err( req )) ret = reply->history_mode;
134 SERVER_END_REQ;
135 return ret;
138 /******************************************************************
139 * WINECON_SetHistoryMode
143 BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
145 BOOL ret;
147 SERVER_START_REQ(set_console_input_info)
149 req->handle = (obj_handle_t)hConIn;
150 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
151 req->history_mode = mode;
152 ret = !wine_server_call_err( req );
154 SERVER_END_REQ;
155 return ret;
158 /******************************************************************
159 * WINECON_GetConsoleTitle
163 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
165 BOOL ret;
167 if (len < sizeof(WCHAR)) return FALSE;
169 SERVER_START_REQ( get_console_input_info )
171 req->handle = (obj_handle_t)hConIn;
172 wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
173 if ((ret = !wine_server_call_err( req )))
175 len = wine_server_reply_size( reply );
176 buffer[len / sizeof(WCHAR)] = 0;
179 SERVER_END_REQ;
180 return ret;
183 /******************************************************************
184 * WINECON_SetEditionMode
188 static BOOL WINECON_SetEditionMode(HANDLE hConIn, int edition_mode)
190 BOOL ret;
192 SERVER_START_REQ( set_console_input_info )
194 req->handle = (obj_handle_t)hConIn;
195 req->mask = SET_CONSOLE_INPUT_INFO_EDITION_MODE;
196 req->edition_mode = edition_mode;
197 ret = !wine_server_call_err( req );
199 SERVER_END_REQ;
200 return ret;
203 /******************************************************************
204 * WINECON_GrabChanges
206 * A change occurs, try to figure out which
208 int WINECON_GrabChanges(struct inner_data* data)
210 struct console_renderer_event evts[256];
211 int i, num, ev_found;
212 HANDLE h;
214 SERVER_START_REQ( get_console_renderer_events )
216 wine_server_set_reply( req, evts, sizeof(evts) );
217 req->handle = (obj_handle_t)data->hSynchro;
218 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
219 else num = 0;
221 SERVER_END_REQ;
222 if (!num) {WINE_WARN("hmm renderer signaled but no events available\n"); return 1;}
224 /* FIXME: should do some event compression here (cursor pos, update) */
225 /* step 1: keep only last cursor pos event */
226 ev_found = -1;
227 for (i = num - 1; i >= 0; i--)
229 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
231 if (ev_found != -1) evts[ev_found].event = CONSOLE_RENDERER_NONE_EVENT;
232 ev_found = i;
235 /* step 2: manage update events */
236 ev_found = -1;
237 for (i = 0; i < num; i++)
239 if (evts[i].event == CONSOLE_RENDERER_NONE_EVENT ||
240 evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT ||
241 evts[i].event == CONSOLE_RENDERER_CURSOR_GEOM_EVENT) continue;
242 if (evts[i].event != CONSOLE_RENDERER_UPDATE_EVENT)
244 ev_found = -1;
245 continue;
248 if (ev_found != -1 && /* Only 2 cases where they can NOT merge */
249 !(evts[i ].u.update.bottom + 1 < evts[ev_found].u.update.top ||
250 evts[ev_found].u.update.bottom + 1 < evts[i ].u.update.top))
252 evts[i].u.update.top = min(evts[i ].u.update.top,
253 evts[ev_found].u.update.top);
254 evts[i].u.update.bottom = max(evts[i ].u.update.bottom,
255 evts[ev_found].u.update.bottom);
256 evts[ev_found].event = CONSOLE_RENDERER_NONE_EVENT;
258 ev_found = i;
261 WINE_TRACE("Events:");
262 for (i = 0; i < num; i++)
264 switch (evts[i].event)
266 case CONSOLE_RENDERER_NONE_EVENT:
267 WINE_TRACE(" NOP");
268 break;
269 case CONSOLE_RENDERER_TITLE_EVENT:
270 WINE_TRACE(" title()");
271 data->fnSetTitle(data);
272 break;
273 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
274 SERVER_START_REQ( open_console )
276 req->from = (int)data->hConIn;
277 req->access = GENERIC_READ | GENERIC_WRITE;
278 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
279 req->inherit = FALSE;
280 h = wine_server_call_err( req ) ? 0 : (HANDLE)reply->handle;
282 SERVER_END_REQ;
283 WINE_TRACE(" active(%d)", (int)h);
284 if (h)
286 CloseHandle(data->hConOut);
287 data->hConOut = h;
289 break;
290 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
291 if (data->curcfg.sb_width != evts[i].u.resize.width ||
292 data->curcfg.sb_height != evts[i].u.resize.height || !data->cells)
294 WINE_TRACE(" resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
295 data->curcfg.sb_width = evts[i].u.resize.width;
296 data->curcfg.sb_height = evts[i].u.resize.height;
298 if (data->cells)
299 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
300 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
301 else
302 data->cells = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
303 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
305 if (!data->cells) WINECON_Fatal("OOM\n");
306 data->fnResizeScreenBuffer(data);
307 data->fnComputePositions(data);
309 break;
310 case CONSOLE_RENDERER_UPDATE_EVENT:
311 WINE_TRACE(" update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
312 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
313 break;
314 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
315 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
317 WINE_TRACE(" curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
318 data->cursor.X = evts[i].u.cursor_pos.x;
319 data->cursor.Y = evts[i].u.cursor_pos.y;
320 data->fnPosCursor(data);
322 break;
323 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
324 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
325 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
327 WINE_TRACE(" curs-geom(%d,%d)",
328 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
329 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
330 evts[i].u.cursor_geom.visible, FALSE);
332 break;
333 case CONSOLE_RENDERER_DISPLAY_EVENT:
334 if (evts[i].u.display.left != data->curcfg.win_pos.X)
336 WINE_TRACE(" h-scroll(%d)", evts[i].u.display.left);
337 data->fnScroll(data, evts[i].u.display.left, TRUE);
338 data->fnPosCursor(data);
340 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
342 WINE_TRACE(" v-scroll(%d)", evts[i].u.display.top);
343 data->fnScroll(data, evts[i].u.display.top, FALSE);
344 data->fnPosCursor(data);
346 if (evts[i].u.display.width != data->curcfg.win_width ||
347 evts[i].u.display.height != data->curcfg.win_height)
349 WINE_TRACE(" win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
350 data->curcfg.win_width = evts[i].u.display.width;
351 data->curcfg.win_height = evts[i].u.display.height;
352 data->fnComputePositions(data);
354 break;
355 case CONSOLE_RENDERER_EXIT_EVENT:
356 WINE_TRACE(". Exit!!\n");
357 return 0;
358 default:
359 WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
363 WINE_TRACE(".\n");
364 return 1;
367 /******************************************************************
368 * WINECON_SetConfig
370 * Apply to data all the configuration elements from cfg. This includes modification
371 * of server side equivalent and visual parts.
372 * If force is FALSE, only the changed items are modified.
374 void WINECON_SetConfig(struct inner_data* data,
375 const struct config_data* cfg, BOOL force)
377 if (force || data->curcfg.cursor_size != cfg->cursor_size ||
378 data->curcfg.cursor_visible != cfg->cursor_visible)
380 CONSOLE_CURSOR_INFO cinfo;
381 cinfo.dwSize = cfg->cursor_size;
382 /* <FIXME>: this hack is needed to pass thru the invariant test operation on server side
383 * (no notification is sent when invariant operation is requested
385 cinfo.bVisible = !cfg->cursor_visible;
386 SetConsoleCursorInfo(data->hConOut, &cinfo);
387 /* </FIXME> */
388 cinfo.bVisible = cfg->cursor_visible;
389 /* this shall update (through notif) curcfg */
390 SetConsoleCursorInfo(data->hConOut, &cinfo);
392 if (force || data->curcfg.history_size != cfg->history_size)
394 data->curcfg.history_size = cfg->history_size;
395 WINECON_SetHistorySize(data->hConIn, cfg->history_size);
397 if (force || data->curcfg.history_nodup != cfg->history_nodup)
399 data->curcfg.history_nodup = cfg->history_nodup;
400 WINECON_SetHistoryMode(data->hConIn, cfg->history_nodup);
402 data->curcfg.menu_mask = cfg->menu_mask;
403 data->curcfg.quick_edit = cfg->quick_edit;
404 if (force || 1 /* FIXME: font info has changed */)
406 data->fnSetFont(data, cfg->face_name, cfg->cell_height, cfg->font_weight);
408 if (force || data->curcfg.def_attr != cfg->def_attr)
410 data->curcfg.def_attr = cfg->def_attr;
411 SetConsoleTextAttribute(data->hConOut, cfg->def_attr);
413 /* now let's look at the window / sb size changes...
414 * since the server checks that sb is always bigger than window,
415 * we have to take care of doing the operations in the right order
417 /* a set of macros to make things easier to read
418 * The Test<A><B> macros test if the <A> (width/height) needs to be changed
419 * for <B> (window / ScreenBuffer)
420 * The Change<A><B> actually modify the <B> dimension of <A>.
422 #define TstSBfWidth() (force || data->curcfg.sb_width != cfg->sb_width)
423 #define TstWinWidth() (force || data->curcfg.win_width != cfg->win_width)
425 #define ChgSBfWidth() do {c.X = cfg->sb_width; \
426 c.Y = data->curcfg.sb_height;\
427 SetConsoleScreenBufferSize(data->hConOut, c);\
428 } while (0)
429 #define ChgWinWidth() do {pos.Left = pos.Top = 0; \
430 pos.Right = cfg->win_width - 1; \
431 pos.Bottom = data->curcfg.win_height - 1; \
432 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
433 } while (0)
434 #define TstSBfHeight() (force || data->curcfg.sb_height != cfg->sb_height)
435 #define TstWinHeight() (force || data->curcfg.win_height != cfg->win_height)
437 /* since we're going to apply height after width is done, we use width as defined
438 * in cfg, and not in data->curcfg because if won't be updated yet */
439 #define ChgSBfHeight() do {c.X = cfg->sb_width; c.Y = cfg->sb_height; \
440 SetConsoleScreenBufferSize(data->hConOut, c); \
441 } while (0)
442 #define ChgWinHeight() do {pos.Left = pos.Top = 0; \
443 pos.Right = cfg->win_width - 1; \
444 pos.Bottom = cfg->win_height - 1; \
445 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
446 } while (0)
450 COORD c;
451 SMALL_RECT pos;
453 if (TstSBfWidth())
455 if (TstWinWidth())
457 /* we're changing both at the same time, do it in the right order */
458 if (cfg->sb_width >= data->curcfg.win_width)
460 ChgSBfWidth(); ChgWinWidth();
462 else
464 ChgWinWidth(); ChgSBfWidth();
467 else ChgSBfWidth();
469 else if (TstWinWidth()) ChgWinWidth();
470 if (TstSBfHeight())
472 if (TstWinHeight())
474 if (cfg->sb_height >= data->curcfg.win_height)
476 ChgSBfHeight(); ChgWinHeight();
478 else
480 ChgWinHeight(); ChgSBfHeight();
483 else ChgSBfHeight();
485 else if (TstWinHeight()) ChgWinHeight();
486 } while (0);
487 #undef TstSBfWidth
488 #undef TstWinWidth
489 #undef ChgSBfWidth
490 #undef ChgWinWidth
491 #undef TstSBfHeight
492 #undef TstWinHeight
493 #undef ChgSBfHeight
494 #undef ChgWinHeight
496 data->curcfg.exit_on_die = cfg->exit_on_die;
497 if (force || data->curcfg.edition_mode != cfg->edition_mode)
499 data->curcfg.edition_mode = cfg->edition_mode;
500 WINECON_SetEditionMode(data->hConIn, cfg->edition_mode);
502 /* we now need to gather all events we got from the operations above,
503 * in order to get data correctly updated
505 WINECON_GrabChanges(data);
508 /******************************************************************
509 * WINECON_Delete
511 * Destroy wineconsole internal data
513 static void WINECON_Delete(struct inner_data* data)
515 if (!data) return;
517 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
518 if (data->hConIn) CloseHandle(data->hConIn);
519 if (data->hConOut) CloseHandle(data->hConOut);
520 if (data->hSynchro) CloseHandle(data->hSynchro);
521 if (data->cells) HeapFree(GetProcessHeap(), 0, data->cells);
522 HeapFree(GetProcessHeap(), 0, data);
525 /******************************************************************
526 * WINECON_Init
528 * Initialisation part I. Creation of server object (console input and
529 * active screen buffer)
531 static struct inner_data* WINECON_Init(HINSTANCE hInst, DWORD pid, LPCWSTR appname,
532 enum init_return (*backend)(struct inner_data*))
534 struct inner_data* data = NULL;
535 DWORD ret;
536 struct config_data cfg;
537 STARTUPINFOW si;
539 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
540 if (!data) return 0;
542 GetStartupInfo(&si);
544 if (pid == 0)
546 if (!si.lpTitle) WINECON_Fatal("Should have a title set");
547 appname = si.lpTitle;
550 /* load settings */
551 WINECON_RegLoad(appname, &cfg);
553 /* some overrides */
554 if (pid == 0)
556 if (si.dwFlags & STARTF_USECOUNTCHARS)
558 cfg.sb_width = si.dwXCountChars;
559 cfg.sb_height = si.dwYCountChars;
561 if (si.dwFlags & STARTF_USEFILLATTRIBUTE)
562 cfg.def_attr = si.dwFillAttribute;
563 /* should always be defined */
566 /* the handles here are created without the whistles and bells required by console
567 * (mainly because wineconsole doesn't need it)
568 * - they are not inheritable
569 * - hConIn is not synchronizable
571 SERVER_START_REQ(alloc_console)
573 req->access = GENERIC_READ | GENERIC_WRITE;
574 req->inherit = FALSE;
575 req->pid = pid;
577 ret = !wine_server_call_err( req );
578 data->hConIn = (HANDLE)reply->handle_in;
579 data->hSynchro = (HANDLE)reply->event;
581 SERVER_END_REQ;
582 if (!ret) goto error;
583 WINE_TRACE("using hConIn %p, hSynchro event %p\n", data->hConIn, data->hSynchro);
585 SERVER_START_REQ( set_console_input_info )
587 req->handle = (obj_handle_t)data->hConIn;
588 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
589 wine_server_add_data( req, appname, lstrlenW(appname) * sizeof(WCHAR) );
590 ret = !wine_server_call_err( req );
592 SERVER_END_REQ;
593 if (!ret) goto error;
595 SERVER_START_REQ(create_console_output)
597 req->handle_in = (obj_handle_t)data->hConIn;
598 req->access = GENERIC_WRITE|GENERIC_READ;
599 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
600 req->inherit = FALSE;
601 ret = !wine_server_call_err( req );
602 data->hConOut = (HANDLE)reply->handle_out;
604 SERVER_END_REQ;
605 if (!ret) goto error;
606 WINE_TRACE("using hConOut %p\n", data->hConOut);
608 /* filling data->curcfg from cfg */
609 retry:
610 switch ((*backend)(data))
612 case init_success:
613 WINECON_SetConfig(data, &cfg, TRUE);
614 data->curcfg.registry = cfg.registry;
615 WINECON_DumpConfig("fint", &data->curcfg);
616 return data;
617 case init_failed:
618 break;
619 case init_not_supported:
620 if (backend == WCCURSES_InitBackend)
622 WINE_ERR("(n)curses was not found at configuration time.\n"
623 "If you want (n)curses support, please install relevant packages.\n"
624 "Now forcing user backend instead of (n)curses.\n");
625 backend = WCUSER_InitBackend;
626 goto retry;
628 break;
631 error:
632 WINE_ERR("failed to init.\n");
634 WINECON_Delete(data);
635 return NULL;
638 /******************************************************************
639 * WINECON_Spawn
641 * Spawn the child process when invoked with wineconsole foo bar
643 static BOOL WINECON_Spawn(struct inner_data* data, LPWSTR cmdLine)
645 PROCESS_INFORMATION info;
646 STARTUPINFO startup;
647 BOOL done;
649 /* we're in the case wineconsole <exe> <options>... spawn the new process */
650 memset(&startup, 0, sizeof(startup));
651 startup.cb = sizeof(startup);
652 startup.dwFlags = STARTF_USESTDHANDLES;
654 /* the attributes of wineconsole's handles are not adequate for inheritance, so
655 * get them with the correct attributes before process creation
657 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
658 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
659 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
660 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
661 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
662 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
664 WINE_ERR("Can't dup handles\n");
665 /* no need to delete handles, we're exiting the programm anyway */
666 return FALSE;
669 done = CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
671 /* we no longer need the handles passed to the child for the console */
672 CloseHandle(startup.hStdInput);
673 CloseHandle(startup.hStdOutput);
674 CloseHandle(startup.hStdError);
676 CloseHandle(info.hProcess);
677 CloseHandle(info.hThread);
679 return done;
682 struct wc_init {
683 LPCSTR ptr;
684 enum {from_event, from_process_name} mode;
685 enum init_return (*backend)(struct inner_data*);
686 HANDLE event;
689 /******************************************************************
690 * WINECON_ParseOptions
694 static BOOL WINECON_ParseOptions(const char* lpCmdLine, struct wc_init* wci)
696 memset(wci, 0, sizeof(*wci));
697 wci->ptr = lpCmdLine;
698 wci->mode = from_process_name;
699 wci->backend = WCCURSES_InitBackend;
701 for (;;)
703 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
704 if (wci->ptr[0] != '-') break;
705 if (strncmp(wci->ptr, "--use-event=", 12) == 0)
707 char* end;
708 wci->event = (HANDLE)strtol(wci->ptr + 12, &end, 10);
709 if (end == wci->ptr + 12) return FALSE;
710 wci->mode = from_event;
711 wci->ptr = end;
712 wci->backend = WCUSER_InitBackend;
714 else if (strncmp(wci->ptr, "--backend=", 10) == 0)
716 if (strncmp(wci->ptr + 10, "user", 4) == 0)
718 wci->backend = WCUSER_InitBackend;
719 wci->ptr += 14;
721 else if (strncmp(wci->ptr + 10, "curses", 6) == 0)
723 wci->ptr += 16;
725 else
726 return FALSE;
728 else
729 return FALSE;
732 return TRUE;
735 /******************************************************************
736 * WinMain
738 * wineconsole can either be started as:
739 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
740 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
741 * a freshly created console
742 * --backend=(curses|user) can also be used to select the desired backend
744 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
746 struct inner_data* data;
747 int ret = 1;
748 struct wc_init wci;
750 if (!WINECON_ParseOptions(lpCmdLine, &wci))
752 WINE_ERR("Wrong command line options\n");
753 return 0;
756 switch (wci.mode)
758 case from_event:
759 /* case of wineconsole <evt>, signal process that created us that we're up and running */
760 if (!(data = WINECON_Init(hInst, 0, NULL, wci.backend))) return 0;
761 ret = SetEvent(wci.event);
762 if (!ret) WINE_ERR("SetEvent failed.\n");
763 break;
764 case from_process_name:
766 WCHAR buffer[256];
768 MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, buffer, sizeof(buffer) / sizeof(buffer[0]));
770 if (!(data = WINECON_Init(hInst, GetCurrentProcessId(), buffer, wci.backend)))
771 return 0;
772 ret = WINECON_Spawn(data, buffer);
773 if (!ret)
774 WINE_MESSAGE("wineconsole: spawning client program failed (%s), invalid/missing command line arguments ?\n", wine_dbgstr_w(buffer));
776 break;
777 default:
778 return 0;
781 if (ret)
783 WINE_TRACE("calling MainLoop.\n");
784 ret = data->fnMainLoop(data);
787 WINECON_Delete(data);
789 return ret;