1 /*
2 * an application for displaying Win32 console
3 *
4 * Copyright 2001, 2002 Eric Pouech
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
34
35 void WINECON_Fatal(const char* msg)
36 {
37 WINE_ERR("%s\n", msg);
38 ExitProcess(0);
39 }
40
41 static void printf_res(UINT uResId, ...)
42 {
43 WCHAR buffer[1024];
44 CHAR ansi[1024];
45 va_list args;
46
47 va_start(args, uResId);
48 LoadStringW(GetModuleHandle(NULL), uResId, buffer, sizeof(buffer)/sizeof(WCHAR));
49 WideCharToMultiByte(CP_UNIXCP, 0, buffer, -1, ansi, sizeof(ansi), NULL, NULL);
50 vprintf(ansi, args);
51 va_end(args);
52 }
53
54 static void WINECON_Usage(void)
55 {
56 printf_res(IDS_USAGE_HEADER);
57 printf_res(IDS_USAGE_BACKEND);
58 printf_res(IDS_USAGE_COMMAND);
59 printf_res(IDS_USAGE_FOOTER);
60 }
61
62 /******************************************************************
63 * WINECON_FetchCells
64 *
65 * updates the local copy of cells (band to update)
66 */
67 static void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
68 {
69 SERVER_START_REQ( read_console_output )
70 {
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 );
79 }
80 SERVER_END_REQ;
81 data->fnRefresh(data, upd_tp, upd_bm);
82 }
83
84 /******************************************************************
85 * WINECON_NotifyWindowChange
86 *
87 * Inform server that visible window on sb has changed
88 */
89 void WINECON_NotifyWindowChange(struct inner_data* data)
90 {
91 SERVER_START_REQ( set_console_output_info )
92 {
93 req->handle = wine_server_obj_handle( data->hConOut );
94 req->win_left = data->curcfg.win_pos.X;
95 req->win_top = data->curcfg.win_pos.Y;
96 req->win_right = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
97 req->win_bottom = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
98 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
99 wine_server_call( req );
100 }
101 SERVER_END_REQ;
102 }
103
104 /******************************************************************
105 * WINECON_SetHistorySize
106 *
107 *
108 */
109 static BOOL WINECON_SetHistorySize(HANDLE hConIn, int size)
110 {
111 BOOL ret;
112
113 SERVER_START_REQ(set_console_input_info)
114 {
115 req->handle = wine_server_obj_handle( hConIn );
116 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
117 req->history_size = size;
118 ret = !wine_server_call_err( req );
119 }
120 SERVER_END_REQ;
121 return ret;
122 }
123
124 /******************************************************************
125 * WINECON_SetHistoryMode
126 *
127 *
128 */
129 static BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
130 {
131 BOOL ret;
132
133 SERVER_START_REQ(set_console_input_info)
134 {
135 req->handle = wine_server_obj_handle( hConIn );
136 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
137 req->history_mode = mode;
138 ret = !wine_server_call_err( req );
139 }
140 SERVER_END_REQ;
141 return ret;
142 }
143
144 /******************************************************************
145 * WINECON_GetConsoleTitle
146 *
147 *
148 */
149 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
150 {
151 BOOL ret;
152
153 if (len < sizeof(WCHAR)) return FALSE;
154
155 SERVER_START_REQ( get_console_input_info )
156 {
157 req->handle = wine_server_obj_handle( hConIn );
158 wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
159 if ((ret = !wine_server_call_err( req )))
160 {
161 len = wine_server_reply_size( reply );
162 buffer[len / sizeof(WCHAR)] = 0;
163 }
164 }
165 SERVER_END_REQ;
166 return ret;
167 }
168
169 /******************************************************************
170 * WINECON_SetEditionMode
171 *
172 *
173 */
174 static BOOL WINECON_SetEditionMode(HANDLE hConIn, int edition_mode)
175 {
176 BOOL ret;
177
178 SERVER_START_REQ( set_console_input_info )
179 {
180 req->handle = wine_server_obj_handle( hConIn );
181 req->mask = SET_CONSOLE_INPUT_INFO_EDITION_MODE;
182 req->edition_mode = edition_mode;
183 ret = !wine_server_call_err( req );
184 }
185 SERVER_END_REQ;
186 return ret;
187 }
188
189 /******************************************************************
190 * WINECON_GrabChanges
191 *
192 * A change occurs, try to figure out which
193 */
194 int WINECON_GrabChanges(struct inner_data* data)
195 {
196 struct console_renderer_event evts[256];
197 int i, num, ev_found;
198 HANDLE h;
199
200 SERVER_START_REQ( get_console_renderer_events )
201 {
202 wine_server_set_reply( req, evts, sizeof(evts) );
203 req->handle = wine_server_obj_handle( data->hSynchro );
204 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
205 else num = 0;
206 }
207 SERVER_END_REQ;
208 if (!num) {WINE_WARN("hmm renderer signaled but no events available\n"); return 1;}
209
210 /* FIXME: should do some event compression here (cursor pos, update) */
211 /* step 1: keep only last cursor pos event */
212 ev_found = -1;
213 for (i = num - 1; i >= 0; i--)
214 {
215 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
216 {
217 if (ev_found != -1)
218 evts[i].event = CONSOLE_RENDERER_NONE_EVENT;
219 ev_found = i;
220 }
221 }
222 /* step 2: manage update events */
223 ev_found = -1;
224 for (i = 0; i < num; i++)
225 {
226 if (evts[i].event == CONSOLE_RENDERER_NONE_EVENT ||
227 evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT ||
228 evts[i].event == CONSOLE_RENDERER_CURSOR_GEOM_EVENT) continue;
229 if (evts[i].event != CONSOLE_RENDERER_UPDATE_EVENT)
230 {
231 ev_found = -1;
232 continue;
233 }
234
235 if (ev_found != -1 && /* Only 2 cases where they CANNOT merge */
236 !(evts[i ].u.update.bottom + 1 < evts[ev_found].u.update.top ||
237 evts[ev_found].u.update.bottom + 1 < evts[i ].u.update.top))
238 {
239 evts[i].u.update.top = min(evts[i ].u.update.top,
240 evts[ev_found].u.update.top);
241 evts[i].u.update.bottom = max(evts[i ].u.update.bottom,
242 evts[ev_found].u.update.bottom);
243 evts[ev_found].event = CONSOLE_RENDERER_NONE_EVENT;
244 }
245 ev_found = i;
246 }
247
248 WINE_TRACE("Events:");
249 for (i = 0; i < num; i++)
250 {
251 switch (evts[i].event)
252 {
253 case CONSOLE_RENDERER_NONE_EVENT:
254 WINE_TRACE(" NOP");
255 break;
256 case CONSOLE_RENDERER_TITLE_EVENT:
257 WINE_TRACE(" title()");
258 data->fnSetTitle(data);
259 break;
260 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
261 SERVER_START_REQ( open_console )
262 {
263 req->from = wine_server_obj_handle( data->hConIn );
264 req->access = GENERIC_READ | GENERIC_WRITE;
265 req->attributes = 0;
266 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
267 h = wine_server_call_err( req ) ? 0 : wine_server_ptr_handle(reply->handle);
268 }
269 SERVER_END_REQ;
270 WINE_TRACE(" active(%p)", h);
271 if (h)
272 {
273 CloseHandle(data->hConOut);
274 data->hConOut = h;
275 }
276 break;
277 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
278 if (data->curcfg.sb_width != evts[i].u.resize.width ||
279 data->curcfg.sb_height != evts[i].u.resize.height)
280 {
281 WINE_TRACE(" resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
282 data->curcfg.sb_width = evts[i].u.resize.width;
283 data->curcfg.sb_height = evts[i].u.resize.height;
284
285 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
286 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
287
288 if (!data->cells) WINECON_Fatal("OOM\n");
289 data->fnResizeScreenBuffer(data);
290 data->fnComputePositions(data);
291 }
292 break;
293 case CONSOLE_RENDERER_UPDATE_EVENT:
294 WINE_TRACE(" update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
295 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
296 break;
297 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
298 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
299 {
300 WINE_TRACE(" curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.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 }
305 break;
306 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
307 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
308 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
309 {
310 WINE_TRACE(" curs-geom(%d,%d)",
311 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
312 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
313 evts[i].u.cursor_geom.visible, FALSE);
314 }
315 break;
316 case CONSOLE_RENDERER_DISPLAY_EVENT:
317 if (evts[i].u.display.left != data->curcfg.win_pos.X)
318 {
319 WINE_TRACE(" h-scroll(%d)", evts[i].u.display.left);
320 data->fnScroll(data, evts[i].u.display.left, TRUE);
321 data->fnPosCursor(data);
322 }
323 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
324 {
325 WINE_TRACE(" v-scroll(%d)", evts[i].u.display.top);
326 data->fnScroll(data, evts[i].u.display.top, FALSE);
327 data->fnPosCursor(data);
328 }
329 if (evts[i].u.display.width != data->curcfg.win_width ||
330 evts[i].u.display.height != data->curcfg.win_height)
331 {
332 WINE_TRACE(" win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
333 data->curcfg.win_width = evts[i].u.display.width;
334 data->curcfg.win_height = evts[i].u.display.height;
335 data->fnComputePositions(data);
336 }
337 break;
338 case CONSOLE_RENDERER_EXIT_EVENT:
339 WINE_TRACE(". Exit!!\n");
340 return 0;
341 default:
342 WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
343 }
344 }
345
346 WINE_TRACE(".\n");
347 return 1;
348 }
349
350 /******************************************************************
351 * WINECON_SetConfig
352 *
353 * Apply to data all the configuration elements from cfg. This includes modification
354 * of server side equivalent and visual parts.
355 * If force is FALSE, only the changed items are modified.
356 */
357 void WINECON_SetConfig(struct inner_data* data, const struct config_data* cfg)
358 {
359 if (data->curcfg.cursor_size != cfg->cursor_size ||
360 data->curcfg.cursor_visible != cfg->cursor_visible)
361 {
362 CONSOLE_CURSOR_INFO cinfo;
363 cinfo.dwSize = cfg->cursor_size;
364 /* <FIXME>: this hack is needed to pass thru the invariant test operation on server side
365 * (no notification is sent when invariant operation is requested
366 */
367 cinfo.bVisible = !cfg->cursor_visible;
368 SetConsoleCursorInfo(data->hConOut, &cinfo);
369 /* </FIXME> */
370 cinfo.bVisible = cfg->cursor_visible;
371 /* this shall update (through notif) curcfg */
372 SetConsoleCursorInfo(data->hConOut, &cinfo);
373 }
374 if (data->curcfg.history_size != cfg->history_size)
375 {
376 data->curcfg.history_size = cfg->history_size;
377 WINECON_SetHistorySize(data->hConIn, cfg->history_size);
378 }
379 if (data->curcfg.history_nodup != cfg->history_nodup)
380 {
381 data->curcfg.history_nodup = cfg->history_nodup;
382 WINECON_SetHistoryMode(data->hConIn, cfg->history_nodup);
383 }
384 data->curcfg.menu_mask = cfg->menu_mask;
385 data->curcfg.quick_edit = cfg->quick_edit;
386 if (1 /* FIXME: font info has changed */)
387 {
388 data->fnSetFont(data, cfg->face_name, cfg->cell_height, cfg->font_weight);
389 }
390 if (data->curcfg.def_attr != cfg->def_attr)
391 {
392 data->curcfg.def_attr = cfg->def_attr;
393 SetConsoleTextAttribute(data->hConOut, cfg->def_attr);
394 }
395 /* now let's look at the window / sb size changes...
396 * since the server checks that sb is always bigger than window,
397 * we have to take care of doing the operations in the right order
398 */
399 /* a set of macros to make things easier to read
400 * The Test<A><B> macros test if the <A> (width/height) needs to be changed
401 * for <B> (window / ScreenBuffer)
402 * The Change<A><B> actually modify the <B> dimension of <A>.
403 */
404 #define TstSBfWidth() (data->curcfg.sb_width != cfg->sb_width)
405 #define TstWinWidth() (data->curcfg.win_width != cfg->win_width)
406
407 #define ChgSBfWidth() do {c.X = cfg->sb_width; \
408 c.Y = data->curcfg.sb_height;\
409 SetConsoleScreenBufferSize(data->hConOut, c);\
410 } while (0)
411 #define ChgWinWidth() do {pos.Left = pos.Top = 0; \
412 pos.Right = cfg->win_width - data->curcfg.win_width; \
413 pos.Bottom = 0; \
414 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
415 } while (0)
416 #define TstSBfHeight() (data->curcfg.sb_height != cfg->sb_height)
417 #define TstWinHeight() (data->curcfg.win_height != cfg->win_height)
418
419 /* since we're going to apply height after width is done, we use width as defined
420 * in cfg, and not in data->curcfg because if won't be updated yet */
421 #define ChgSBfHeight() do {c.X = cfg->sb_width; c.Y = cfg->sb_height; \
422 SetConsoleScreenBufferSize(data->hConOut, c); \
423 } while (0)
424 #define ChgWinHeight() do {pos.Left = pos.Top = 0; \
425 pos.Right = 0; \
426 pos.Bottom = cfg->win_height - data->curcfg.win_height; \
427 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
428 } while (0)
429
430 do
431 {
432 COORD c;
433 SMALL_RECT pos;
434
435 if (TstSBfWidth())
436 {
437 if (TstWinWidth())
438 {
439 /* we're changing both at the same time, do it in the right order */
440 if (cfg->sb_width >= data->curcfg.win_width)
441 {
442 ChgSBfWidth(); ChgWinWidth();
443 }
444 else
445 {
446 ChgWinWidth(); ChgSBfWidth();
447 }
448 }
449 else ChgSBfWidth();
450 }
451 else if (TstWinWidth()) ChgWinWidth();
452 if (TstSBfHeight())
453 {
454 if (TstWinHeight())
455 {
456 if (cfg->sb_height >= data->curcfg.win_height)
457 {
458 ChgSBfHeight(); ChgWinHeight();
459 }
460 else
461 {
462 ChgWinHeight(); ChgSBfHeight();
463 }
464 }
465 else ChgSBfHeight();
466 }
467 else if (TstWinHeight()) ChgWinHeight();
468 } while (0);
469 #undef TstSBfWidth
470 #undef TstWinWidth
471 #undef ChgSBfWidth
472 #undef ChgWinWidth
473 #undef TstSBfHeight
474 #undef TstWinHeight
475 #undef ChgSBfHeight
476 #undef ChgWinHeight
477
478 data->curcfg.exit_on_die = cfg->exit_on_die;
479 if (data->curcfg.edition_mode != cfg->edition_mode)
480 {
481 data->curcfg.edition_mode = cfg->edition_mode;
482 WINECON_SetEditionMode(data->hConIn, cfg->edition_mode);
483 }
484 /* we now need to gather all events we got from the operations above,
485 * in order to get data correctly updated
486 */
487 WINECON_GrabChanges(data);
488 }
489
490 /******************************************************************
491 * WINECON_Delete
492 *
493 * Destroy wineconsole internal data
494 */
495 static void WINECON_Delete(struct inner_data* data)
496 {
497 if (!data) return;
498
499 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
500 if (data->hConIn) CloseHandle(data->hConIn);
501 if (data->hConOut) CloseHandle(data->hConOut);
502 if (data->hSynchro) CloseHandle(data->hSynchro);
503 HeapFree(GetProcessHeap(), 0, data->cells);
504 HeapFree(GetProcessHeap(), 0, data);
505 }
506
507 /******************************************************************
508 * WINECON_GetServerConfig
509 *
510 * Fills data->curcfg with the actual configuration running in the server
511 * (getting real information on the server, and not relying on cached
512 * information in data)
513 */
514 static BOOL WINECON_GetServerConfig(struct inner_data* data)
515 {
516 BOOL ret;
517
518 SERVER_START_REQ(get_console_input_info)
519 {
520 req->handle = wine_server_obj_handle( data->hConIn );
521 ret = !wine_server_call_err( req );
522 data->curcfg.history_size = reply->history_size;
523 data->curcfg.history_nodup = reply->history_mode;
524 data->curcfg.edition_mode = reply->edition_mode;
525 }
526 SERVER_END_REQ;
527 if (!ret) return FALSE;
528 SERVER_START_REQ(get_console_output_info)
529 {
530 req->handle = wine_server_obj_handle( data->hConOut );
531 ret = !wine_server_call_err( req );
532 data->curcfg.cursor_size = reply->cursor_size;
533 data->curcfg.cursor_visible = reply->cursor_visible;
534 data->curcfg.def_attr = reply->attr;
535 data->curcfg.sb_width = reply->width;
536 data->curcfg.sb_height = reply->height;
537 data->curcfg.win_width = reply->win_right - reply->win_left + 1;
538 data->curcfg.win_height = reply->win_bottom - reply->win_top + 1;
539 }
540 SERVER_END_REQ;
541 WINECON_DumpConfig("first cfg: ", &data->curcfg);
542
543 return ret;
544 }
545
546 /******************************************************************
547 * WINECON_Init
548 *
549 * Initialisation part I. Creation of server object (console input and
550 * active screen buffer)
551 */
552 static struct inner_data* WINECON_Init(HINSTANCE hInst, DWORD pid, LPCWSTR appname,
553 enum init_return (*backend)(struct inner_data*),
554 INT nCmdShow)
555 {
556 struct inner_data* data = NULL;
557 DWORD ret;
558 struct config_data cfg;
559 STARTUPINFOW si;
560
561 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
562 if (!data) return 0;
563
564 GetStartupInfo(&si);
565
566 if (pid == 0)
567 {
568 if (!si.lpTitle) WINECON_Fatal("Should have a title set");
569 appname = si.lpTitle;
570 }
571
572 data->nCmdShow = nCmdShow;
573 /* load settings */
574 WINECON_RegLoad(appname, &cfg);
575
576 /* some overrides */
577 if (pid == 0)
578 {
579 if (si.dwFlags & STARTF_USECOUNTCHARS)
580 {
581 cfg.sb_width = si.dwXCountChars;
582 cfg.sb_height = si.dwYCountChars;
583 }
584 if (si.dwFlags & STARTF_USEFILLATTRIBUTE)
585 cfg.def_attr = si.dwFillAttribute;
586 /* should always be defined */
587 }
588
589 /* the handles here are created without the whistles and bells required by console
590 * (mainly because wineconsole doesn't need it)
591 * - they are not inheritable
592 * - hConIn is not synchronizable
593 */
594 SERVER_START_REQ(alloc_console)
595 {
596 req->access = GENERIC_READ | GENERIC_WRITE;
597 req->attributes = 0;
598 req->pid = pid;
599
600 ret = !wine_server_call_err( req );
601 data->hConIn = wine_server_ptr_handle( reply->handle_in );
602 data->hSynchro = wine_server_ptr_handle( reply->event );
603 }
604 SERVER_END_REQ;
605 if (!ret) goto error;
606 WINE_TRACE("using hConIn %p, hSynchro event %p\n", data->hConIn, data->hSynchro);
607
608 SERVER_START_REQ(create_console_output)
609 {
610 req->handle_in = wine_server_obj_handle( data->hConIn );
611 req->access = GENERIC_WRITE|GENERIC_READ;
612 req->attributes = 0;
613 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
614 ret = !wine_server_call_err( req );
615 data->hConOut = wine_server_ptr_handle( reply->handle_out );
616 }
617 SERVER_END_REQ;
618 if (!ret) goto error;
619 WINE_TRACE("using hConOut %p\n", data->hConOut);
620
621 /* filling data->curcfg from cfg */
622 switch ((*backend)(data))
623 {
624 case init_not_supported:
625 if (backend == WCCURSES_InitBackend)
626 {
627 if (WCUSER_InitBackend( data ) != init_success) break;
628 }
629 else if (backend == WCUSER_InitBackend)
630 {
631 if (WCCURSES_InitBackend( data ) != init_success) break;
632 }
633 /* fall through */
634 case init_success:
635 WINECON_GetServerConfig(data);
636 data->cells = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
637 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
638 if (!data->cells) WINECON_Fatal("OOM\n");
639 data->fnResizeScreenBuffer(data);
640 data->fnComputePositions(data);
641 WINECON_SetConfig(data, &cfg);
642 data->curcfg.registry = cfg.registry;
643 WINECON_DumpConfig("fint", &data->curcfg);
644 SERVER_START_REQ( set_console_input_info )
645 {
646 req->handle = wine_server_obj_handle( data->hConIn );
647 req->win = wine_server_user_handle( data->hWnd );
648 req->mask = SET_CONSOLE_INPUT_INFO_TITLE |
649 SET_CONSOLE_INPUT_INFO_WIN;
650 wine_server_add_data( req, appname, lstrlenW(appname) * sizeof(WCHAR) );
651 ret = !wine_server_call_err( req );
652 }
653 SERVER_END_REQ;
654 if (!ret) goto error;
655
656 return data;
657 case init_failed:
658 break;
659 }
660
661 error:
662 WINE_ERR("failed to init.\n");
663
664 WINECON_Delete(data);
665 return NULL;
666 }
667
668 /******************************************************************
669 * WINECON_Spawn
670 *
671 * Spawn the child process when invoked with wineconsole foo bar
672 */
673 static BOOL WINECON_Spawn(struct inner_data* data, LPWSTR cmdLine)
674 {
675 PROCESS_INFORMATION info;
676 STARTUPINFO startup;
677 BOOL done;
678
679 /* we're in the case wineconsole <exe> <options>... spawn the new process */
680 memset(&startup, 0, sizeof(startup));
681 startup.cb = sizeof(startup);
682 startup.dwFlags = STARTF_USESTDHANDLES;
683
684 /* the attributes of wineconsole's handles are not adequate for inheritance, so
685 * get them with the correct attributes before process creation
686 */
687 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
688 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
689 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
690 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
691 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
692 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
693 {
694 WINE_ERR("Can't dup handles\n");
695 /* no need to delete handles, we're exiting the program anyway */
696 return FALSE;
697 }
698
699 done = CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
700
701 /* we no longer need the handles passed to the child for the console */
702 CloseHandle(startup.hStdInput);
703 CloseHandle(startup.hStdOutput);
704 CloseHandle(startup.hStdError);
705
706 CloseHandle(info.hProcess);
707 CloseHandle(info.hThread);
708
709 return done;
710 }
711
712 struct wc_init {
713 LPCSTR ptr;
714 enum {from_event, from_process_name} mode;
715 enum init_return (*backend)(struct inner_data*);
716 HANDLE event;
717 };
718
719 #define WINECON_CMD_SHOW_USAGE 0x10000
720
721 /******************************************************************
722 * WINECON_ParseOptions
723 *
724 * RETURNS
725 * On success: 0
726 * On error: error string id optionally with the CMD_SHOW_USAGE flag
727 */
728 static UINT WINECON_ParseOptions(const char* lpCmdLine, struct wc_init* wci)
729 {
730 memset(wci, 0, sizeof(*wci));
731 wci->ptr = lpCmdLine;
732 wci->mode = from_process_name;
733 wci->backend = WCUSER_InitBackend;
734
735 for (;;)
736 {
737 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
738 if (wci->ptr[0] != '-') break;
739 if (strncmp(wci->ptr, "--use-event=", 12) == 0)
740 {
741 char* end;
742 wci->event = (HANDLE)strtol(wci->ptr + 12, &end, 10);
743 if (end == wci->ptr + 12) return IDS_CMD_INVALID_EVENT_ID;
744 wci->mode = from_event;
745 wci->ptr = end;
746 wci->backend = WCUSER_InitBackend;
747 }
748 else if (strncmp(wci->ptr, "--backend=", 10) == 0)
749 {
750 if (strncmp(wci->ptr + 10, "user", 4) == 0)
751 {
752 wci->backend = WCUSER_InitBackend;
753 wci->ptr += 14;
754 }
755 else if (strncmp(wci->ptr + 10, "curses", 6) == 0)
756 {
757 wci->backend = WCCURSES_InitBackend;
758 wci->ptr += 16;
759 }
760 else
761 return IDS_CMD_INVALID_BACKEND;
762 }
763 else
764 return IDS_CMD_INVALID_OPTION|WINECON_CMD_SHOW_USAGE;
765 }
766
767 if (wci->mode == from_event)
768 return 0;
769
770 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
771 if (*wci->ptr == 0)
772 return IDS_CMD_ABOUT|WINECON_CMD_SHOW_USAGE;
773
774 return 0;
775 }
776
777 /******************************************************************
778 * WinMain
779 *
780 * wineconsole can either be started as:
781 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
782 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
783 * a freshly created console
784 * --backend=(curses|user) can also be used to select the desired backend
785 */
786 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
787 {
788 struct inner_data* data;
789 int ret = 1;
790 struct wc_init wci;
791
792 if ((ret = WINECON_ParseOptions(lpCmdLine, &wci)) != 0)
793 {
794 printf_res(ret & 0xffff);
795 if (ret & WINECON_CMD_SHOW_USAGE)
796 WINECON_Usage();
797 return 0;
798 }
799
800 switch (wci.mode)
801 {
802 case from_event:
803 /* case of wineconsole <evt>, signal process that created us that we're up and running */
804 if (!(data = WINECON_Init(hInst, 0, NULL, wci.backend, nCmdShow))) return 0;
805 ret = SetEvent(wci.event);
806 if (!ret) WINE_ERR("SetEvent failed.\n");
807 break;
808 case from_process_name:
809 {
810 WCHAR buffer[256];
811
812 MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, buffer, sizeof(buffer) / sizeof(buffer[0]));
813
814 if (!(data = WINECON_Init(hInst, GetCurrentProcessId(), buffer, wci.backend, nCmdShow)))
815 return 0;
816 ret = WINECON_Spawn(data, buffer);
817 if (!ret)
818 {
819 WINECON_Delete(data);
820 printf_res(IDS_CMD_LAUNCH_FAILED, wine_dbgstr_a(wci.ptr));
821 return 0;
822 }
823 }
824 break;
825 default:
826 return 0;
827 }
828
829 if (ret)
830 {
831 WINE_TRACE("calling MainLoop.\n");
832 ret = data->fnMainLoop(data);
833 }
834
835 WINECON_Delete(data);
836
837 return ret;
838 }
839
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.