winebus.sys: Fix reporting axis values for joysticks.
[wine.git] / programs / wineconsole / wineconsole.c
blob4b65e689390bb4624b8253909002bc3d6f761f0d
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, ARRAY_SIZE(buffer));
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_SetColors
217 * Sets ColorTable and Pop-up menu colors
219 static void WINECON_SetColors(struct inner_data *data, const struct config_data* cfg)
221 size_t color_map_size = sizeof(data->curcfg.color_map);
223 memcpy(data->curcfg.color_map, cfg->color_map, color_map_size);
224 data->curcfg.popup_attr = cfg->popup_attr;
226 SERVER_START_REQ( set_console_output_info )
228 req->handle = wine_server_obj_handle( data->hConOut );
229 req->mask = SET_CONSOLE_OUTPUT_INFO_COLORTABLE | SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR;
230 req->popup_attr = cfg->popup_attr;
231 wine_server_add_data( req, cfg->color_map, color_map_size );
232 wine_server_call( req );
234 SERVER_END_REQ;
237 /******************************************************************
238 * WINECON_GrabChanges
240 * A change occurs, try to figure out which
242 void WINECON_GrabChanges(struct inner_data* data)
244 struct console_renderer_event evts[256];
245 int i, num, ev_found;
246 HANDLE h;
248 if (data->in_grab_changes) return;
250 SERVER_START_REQ( get_console_renderer_events )
252 wine_server_set_reply( req, evts, sizeof(evts) );
253 req->handle = wine_server_obj_handle( data->hSynchro );
254 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
255 else num = 0;
257 SERVER_END_REQ;
258 if (!num) {WINE_WARN("hmm renderer signaled but no events available\n"); return;}
259 WINE_TRACE( "got %u events\n", num );
261 /* FIXME: should do some event compression here (cursor pos, update) */
262 /* step 1: keep only last cursor pos event */
263 ev_found = -1;
264 for (i = num - 1; i >= 0; i--)
266 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
268 if (ev_found != -1)
270 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);
271 evts[i].event = CONSOLE_RENDERER_NONE_EVENT;
273 ev_found = i;
276 /* step 2: manage update events */
277 ev_found = -1;
278 for (i = 0; i < num; i++)
280 if (evts[i].event == CONSOLE_RENDERER_NONE_EVENT ||
281 evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT ||
282 evts[i].event == CONSOLE_RENDERER_CURSOR_GEOM_EVENT) continue;
283 if (evts[i].event != CONSOLE_RENDERER_UPDATE_EVENT)
285 ev_found = -1;
286 continue;
289 if (ev_found != -1 && /* Only 2 cases where they CANNOT merge */
290 !(evts[i ].u.update.bottom + 1 < evts[ev_found].u.update.top ||
291 evts[ev_found].u.update.bottom + 1 < evts[i ].u.update.top))
293 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);
294 evts[i].u.update.top = min(evts[i ].u.update.top,
295 evts[ev_found].u.update.top);
296 evts[i].u.update.bottom = max(evts[i ].u.update.bottom,
297 evts[ev_found].u.update.bottom);
298 evts[ev_found].event = CONSOLE_RENDERER_NONE_EVENT;
300 ev_found = i;
303 data->in_grab_changes = TRUE;
304 for (i = 0; i < num; i++)
306 switch (evts[i].event)
308 case CONSOLE_RENDERER_NONE_EVENT:
309 WINE_TRACE("%u/%u: NOP\n", i+1, num);
310 break;
311 case CONSOLE_RENDERER_TITLE_EVENT:
312 WINE_TRACE("%u/%u: title()\n", i+1, num);
313 data->fnSetTitle(data);
314 break;
315 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
316 SERVER_START_REQ( open_console )
318 req->from = wine_server_obj_handle( data->hConIn );
319 req->access = GENERIC_READ | GENERIC_WRITE;
320 req->attributes = 0;
321 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
322 h = wine_server_call_err( req ) ? 0 : wine_server_ptr_handle(reply->handle);
324 SERVER_END_REQ;
325 WINE_TRACE("%u/%u: active(%p)\n", i+1, num, h);
326 if (h)
328 CloseHandle(data->hConOut);
329 data->hConOut = h;
331 break;
332 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
333 if (data->curcfg.sb_width != evts[i].u.resize.width ||
334 data->curcfg.sb_height != evts[i].u.resize.height)
336 WINE_TRACE("%u/%u: resize(%d,%d)\n", i+1, num, evts[i].u.resize.width, evts[i].u.resize.height);
337 data->curcfg.sb_width = evts[i].u.resize.width;
338 data->curcfg.sb_height = evts[i].u.resize.height;
340 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
341 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
343 if (!data->cells) WINECON_Fatal("OOM");
344 data->fnResizeScreenBuffer(data);
345 data->fnComputePositions(data);
347 break;
348 case CONSOLE_RENDERER_UPDATE_EVENT:
349 WINE_TRACE("%u/%u: update(%d,%d)\n", i+1, num, evts[i].u.update.top, evts[i].u.update.bottom);
350 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
351 break;
352 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
353 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
355 WINE_TRACE("%u/%u: curs-pos(%d,%d)\n", i+1, num, evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
356 data->cursor.X = evts[i].u.cursor_pos.x;
357 data->cursor.Y = evts[i].u.cursor_pos.y;
358 data->fnPosCursor(data);
360 break;
361 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
362 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
363 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
365 WINE_TRACE("%u/%u: curs-geom(%d,%d)\n", i+1, num,
366 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
367 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
368 evts[i].u.cursor_geom.visible, FALSE);
370 break;
371 case CONSOLE_RENDERER_DISPLAY_EVENT:
372 if (evts[i].u.display.left != data->curcfg.win_pos.X)
374 WINE_TRACE("%u/%u: h-scroll(%d)\n", i+1, num, evts[i].u.display.left);
375 data->fnScroll(data, evts[i].u.display.left, TRUE);
376 data->fnPosCursor(data);
378 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
380 WINE_TRACE("%u/%u: v-scroll(%d)\n", i+1, num, evts[i].u.display.top);
381 data->fnScroll(data, evts[i].u.display.top, FALSE);
382 data->fnPosCursor(data);
384 if (evts[i].u.display.width != data->curcfg.win_width ||
385 evts[i].u.display.height != data->curcfg.win_height)
387 WINE_TRACE("%u/%u: win-size(%d,%d)\n", i+1, num, evts[i].u.display.width, evts[i].u.display.height);
388 data->curcfg.win_width = evts[i].u.display.width;
389 data->curcfg.win_height = evts[i].u.display.height;
390 data->fnComputePositions(data);
392 break;
393 case CONSOLE_RENDERER_EXIT_EVENT:
394 data->dying = TRUE;
395 WINE_TRACE("%u/%u: Exit!!\n", i+1, num);
396 return;
397 default:
398 WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
401 data->in_grab_changes = FALSE;
404 /******************************************************************
405 * WINECON_SetConfig
407 * Apply to data all the configuration elements from cfg. This includes modification
408 * of server side equivalent and visual parts.
409 * If force is FALSE, only the changed items are modified.
411 void WINECON_SetConfig(struct inner_data* data, const struct config_data* cfg)
413 if (data->in_set_config) return;
414 data->in_set_config = TRUE;
415 if (data->curcfg.cursor_size != cfg->cursor_size ||
416 data->curcfg.cursor_visible != cfg->cursor_visible)
418 CONSOLE_CURSOR_INFO cinfo;
419 cinfo.dwSize = cfg->cursor_size;
420 /* <FIXME>: this hack is needed to pass through the invariant test operation on the server side
421 * (no notification is sent when invariant operation is requested)
423 cinfo.bVisible = !cfg->cursor_visible;
424 SetConsoleCursorInfo(data->hConOut, &cinfo);
425 /* </FIXME> */
426 cinfo.bVisible = cfg->cursor_visible;
427 /* this shall update (through notif) curcfg */
428 SetConsoleCursorInfo(data->hConOut, &cinfo);
430 if (data->curcfg.history_size != cfg->history_size)
432 data->curcfg.history_size = cfg->history_size;
433 WINECON_SetHistorySize(data->hConIn, cfg->history_size);
435 if (data->curcfg.history_nodup != cfg->history_nodup)
437 data->curcfg.history_nodup = cfg->history_nodup;
438 WINECON_SetHistoryMode(data->hConIn, cfg->history_nodup);
440 if (data->curcfg.insert_mode != cfg->insert_mode)
442 data->curcfg.insert_mode = cfg->insert_mode;
443 WINECON_SetInsertMode(data->hConIn, cfg->insert_mode);
445 data->curcfg.menu_mask = cfg->menu_mask;
446 data->curcfg.quick_edit = cfg->quick_edit;
447 if (strcmpiW(data->curcfg.face_name, cfg->face_name) || data->curcfg.cell_width != cfg->cell_width ||
448 data->curcfg.cell_height != cfg->cell_height || data->curcfg.font_pitch_family != cfg->font_pitch_family ||
449 data->curcfg.font_weight != cfg->font_weight)
451 RECT r;
452 data->fnSetFont(data, cfg->face_name, cfg->cell_height, cfg->font_weight);
453 SystemParametersInfoW(SPI_GETWORKAREA, 0, &r, 0);
454 SERVER_START_REQ(set_console_output_info)
456 req->handle = wine_server_obj_handle( data->hConOut );
457 req->mask = SET_CONSOLE_OUTPUT_INFO_MAX_SIZE | SET_CONSOLE_OUTPUT_INFO_FONT;
458 req->max_width = (r.right - r.left) / cfg->cell_width;
459 req->max_height = (r.bottom - r.top - GetSystemMetrics(SM_CYCAPTION)) / cfg->cell_height;
460 req->font_width = cfg->cell_width;
461 req->font_height = cfg->cell_height;
462 req->font_weight = cfg->font_weight;
463 req->font_pitch_family = cfg->font_pitch_family;
464 wine_server_add_data( req, cfg->face_name, lstrlenW(cfg->face_name) * sizeof(WCHAR) );
465 wine_server_call( req );
467 SERVER_END_REQ;
469 if (data->curcfg.def_attr != cfg->def_attr)
471 DWORD screen_size, written;
472 COORD top_left = {0,0};
474 data->curcfg.def_attr = cfg->def_attr;
475 screen_size = cfg->win_width * (cfg->win_height + 1);
476 FillConsoleOutputAttribute(data->hConOut, cfg->def_attr, screen_size, top_left, &written);
477 SetConsoleTextAttribute(data->hConOut, cfg->def_attr);
479 WINECON_SetColors(data, cfg);
480 /* now let's look at the window / sb size changes...
481 * since the server checks that sb is always bigger than window,
482 * we have to take care of doing the operations in the right order
484 /* a set of macros to make things easier to read
485 * The Test<A><B> macros test if the <A> (width/height) needs to be changed
486 * for <B> (window / ScreenBuffer)
487 * The Change<A><B> actually modify the <B> dimension of <A>.
489 #define TstSBfWidth() (data->curcfg.sb_width != cfg->sb_width)
490 #define TstWinHPos() (data->curcfg.win_width != cfg->win_width || data->curcfg.win_pos.X != cfg->win_pos.X)
492 #define ChgSBfWidth() do {c.X = cfg->sb_width; \
493 c.Y = data->curcfg.sb_height;\
494 SetConsoleScreenBufferSize(data->hConOut, c);\
495 } while (0)
496 #define ChgWinHPos() do {pos.Left = cfg->win_pos.X - data->curcfg.win_pos.X; \
497 pos.Top = 0; \
498 pos.Right = pos.Left + cfg->win_width - data->curcfg.win_width; \
499 pos.Bottom = 0; \
500 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
501 } while (0)
502 #define TstSBfHeight() (data->curcfg.sb_height != cfg->sb_height)
503 #define TstWinVPos() (data->curcfg.win_height != cfg->win_height || data->curcfg.win_pos.Y != cfg->win_pos.Y)
505 /* since we're going to apply height after width is done, we use width as defined
506 * in cfg, and not in data->curcfg because if won't be updated yet */
507 #define ChgSBfHeight() do {c.X = cfg->sb_width; c.Y = cfg->sb_height; \
508 SetConsoleScreenBufferSize(data->hConOut, c); \
509 } while (0)
510 #define ChgWinVPos() do {pos.Left = 0; \
511 pos.Top = cfg->win_pos.Y - data->curcfg.win_pos.Y; \
512 pos.Right = 0; \
513 pos.Bottom = pos.Top + cfg->win_height - data->curcfg.win_height; \
514 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
515 } while (0)
519 COORD c;
520 SMALL_RECT pos;
522 if (TstSBfWidth())
524 if (TstWinHPos())
526 /* we're changing both at the same time, do it in the right order */
527 if (cfg->sb_width >= data->curcfg.win_width)
529 ChgSBfWidth(); ChgWinHPos();
531 else
533 ChgWinHPos(); ChgSBfWidth();
536 else ChgSBfWidth();
538 else if (TstWinHPos()) ChgWinHPos();
539 if (TstSBfHeight())
541 if (TstWinVPos())
543 if (cfg->sb_height >= data->curcfg.win_height)
545 ChgSBfHeight(); ChgWinVPos();
547 else
549 ChgWinVPos(); ChgSBfHeight();
552 else ChgSBfHeight();
554 else if (TstWinVPos()) ChgWinVPos();
555 } while (0);
556 #undef TstSBfWidth
557 #undef TstWinHPos
558 #undef ChgSBfWidth
559 #undef ChgWinHPos
560 #undef TstSBfHeight
561 #undef TstWinVPos
562 #undef ChgSBfHeight
563 #undef ChgWinVPos
565 data->curcfg.exit_on_die = cfg->exit_on_die;
566 if (data->curcfg.edition_mode != cfg->edition_mode)
568 data->curcfg.edition_mode = cfg->edition_mode;
569 WINECON_SetEditionMode(data->hConIn, cfg->edition_mode);
571 /* we now need to gather all events we got from the operations above,
572 * in order to get data correctly updated
574 WINECON_GrabChanges(data);
575 data->in_set_config = FALSE;
578 /******************************************************************
579 * WINECON_Delete
581 * Destroy wineconsole internal data
583 static void WINECON_Delete(struct inner_data* data)
585 if (!data) return;
587 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
588 if (data->hConIn) CloseHandle(data->hConIn);
589 if (data->hConOut) CloseHandle(data->hConOut);
590 if (data->hSynchro) CloseHandle(data->hSynchro);
591 if (data->hProcess) CloseHandle(data->hProcess);
592 HeapFree(GetProcessHeap(), 0, data->curcfg.registry);
593 HeapFree(GetProcessHeap(), 0, data->cells);
594 HeapFree(GetProcessHeap(), 0, data);
597 /******************************************************************
598 * WINECON_GetServerConfig
600 * Fills data->curcfg with the actual configuration running in the server
601 * (getting real information on the server, and not relying on cached
602 * information in data)
604 static BOOL WINECON_GetServerConfig(struct inner_data* data)
606 BOOL ret;
607 DWORD mode;
609 SERVER_START_REQ(get_console_input_info)
611 req->handle = wine_server_obj_handle( data->hConIn );
612 ret = !wine_server_call_err( req );
613 data->curcfg.history_size = reply->history_size;
614 data->curcfg.history_nodup = reply->history_mode;
615 data->curcfg.edition_mode = reply->edition_mode;
617 SERVER_END_REQ;
618 if (!ret) return FALSE;
620 GetConsoleMode(data->hConIn, &mode);
621 data->curcfg.insert_mode = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) ==
622 (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS);
624 SERVER_START_REQ(get_console_output_info)
626 req->handle = wine_server_obj_handle( data->hConOut );
627 ret = !wine_server_call_err( req );
628 data->curcfg.cursor_size = reply->cursor_size;
629 data->curcfg.cursor_visible = reply->cursor_visible;
630 data->curcfg.def_attr = reply->attr;
631 data->curcfg.sb_width = reply->width;
632 data->curcfg.sb_height = reply->height;
633 data->curcfg.win_width = reply->win_right - reply->win_left + 1;
634 data->curcfg.win_height = reply->win_bottom - reply->win_top + 1;
636 SERVER_END_REQ;
637 WINECON_DumpConfig("first cfg: ", &data->curcfg);
639 return ret;
642 /******************************************************************
643 * WINECON_Init
645 * Initialisation part I. Creation of server object (console input and
646 * active screen buffer)
648 static struct inner_data* WINECON_Init(HINSTANCE hInst, DWORD pid, LPCWSTR appname,
649 enum init_return (*backend)(struct inner_data*),
650 INT nCmdShow)
652 struct inner_data* data = NULL;
653 DWORD ret;
654 struct config_data cfg;
655 STARTUPINFOW si;
657 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
658 if (!data) return 0;
660 GetStartupInfoW(&si);
662 if (pid == 0) appname = si.lpTitle;
664 data->nCmdShow = nCmdShow;
665 /* load settings */
666 WINECON_RegLoad(appname, &cfg);
668 /* some overrides */
669 if (pid == 0)
671 if (si.dwFlags & STARTF_USECOUNTCHARS)
673 cfg.sb_width = si.dwXCountChars;
674 cfg.sb_height = si.dwYCountChars;
676 if (si.dwFlags & STARTF_USEFILLATTRIBUTE)
677 cfg.def_attr = si.dwFillAttribute;
678 /* should always be defined */
681 /* the handles here are created without the whistles and bells required by console
682 * (mainly because wineconsole doesn't need it)
683 * - they are not inheritable
684 * - hConIn is not synchronizable
686 SERVER_START_REQ(alloc_console)
688 req->access = GENERIC_READ | GENERIC_WRITE;
689 req->attributes = 0;
690 req->pid = pid;
691 req->input_fd = -1;
693 ret = !wine_server_call_err( req );
694 data->hConIn = wine_server_ptr_handle( reply->handle_in );
695 data->hSynchro = wine_server_ptr_handle( reply->event );
697 SERVER_END_REQ;
698 if (!ret) goto error;
699 WINE_TRACE("using hConIn %p, hSynchro event %p\n", data->hConIn, data->hSynchro);
701 SERVER_START_REQ(create_console_output)
703 req->handle_in = wine_server_obj_handle( data->hConIn );
704 req->access = GENERIC_WRITE|GENERIC_READ;
705 req->attributes = 0;
706 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
707 req->fd = -1;
708 ret = !wine_server_call_err( req );
709 data->hConOut = wine_server_ptr_handle( reply->handle_out );
711 SERVER_END_REQ;
712 if (!ret) goto error;
713 WINE_TRACE("using hConOut %p\n", data->hConOut);
715 /* filling data->curcfg from cfg */
716 switch ((*backend)(data))
718 case init_not_supported:
719 if (backend == WCCURSES_InitBackend)
721 if (WCUSER_InitBackend( data ) != init_success) break;
723 else if (backend == WCUSER_InitBackend)
725 if (WCCURSES_InitBackend( data ) != init_success) break;
727 /* fall through */
728 case init_success:
729 WINECON_GetServerConfig(data);
730 data->cells = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
731 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
732 if (!data->cells) goto error;
733 data->fnResizeScreenBuffer(data);
734 data->fnComputePositions(data);
735 WINECON_SetConfig(data, &cfg);
736 data->curcfg.registry = cfg.registry;
737 WINECON_DumpConfig("fint", &data->curcfg);
738 SERVER_START_REQ( set_console_input_info )
740 req->handle = wine_server_obj_handle( data->hConIn );
741 req->win = wine_server_user_handle( data->hWnd );
742 req->mask = SET_CONSOLE_INPUT_INFO_TITLE |
743 SET_CONSOLE_INPUT_INFO_WIN;
744 wine_server_add_data( req, appname, lstrlenW(appname) * sizeof(WCHAR) );
745 ret = !wine_server_call_err( req );
747 SERVER_END_REQ;
748 if (!ret) goto error;
750 return data;
751 case init_failed:
752 break;
755 error:
756 WINE_ERR("failed to init.\n");
758 WINECON_Delete(data);
759 return NULL;
762 /******************************************************************
763 * WINECON_Spawn
765 * Spawn the child process when invoked with wineconsole foo bar
767 static int WINECON_Spawn(struct inner_data* data, LPWSTR cmdLine)
769 PROCESS_INFORMATION info;
770 STARTUPINFOW startup;
771 BOOL done;
773 /* we're in the case wineconsole <exe> <options>... spawn the new process */
774 memset(&startup, 0, sizeof(startup));
775 startup.cb = sizeof(startup);
776 startup.dwFlags = STARTF_USESTDHANDLES;
778 /* the attributes of wineconsole's handles are not adequate for inheritance, so
779 * get them with the correct attributes before process creation
781 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
782 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
783 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
784 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
785 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
786 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
788 WINE_ERR("Can't dup handles\n");
789 /* no need to delete handles, we're exiting the program anyway */
790 return 1;
793 done = CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
794 if (done)
796 data->hProcess = info.hProcess;
797 CloseHandle(info.hThread);
800 /* we no longer need the handles passed to the child for the console */
801 CloseHandle(startup.hStdInput);
802 CloseHandle(startup.hStdOutput);
803 CloseHandle(startup.hStdError);
805 return !done;
808 struct wc_init {
809 LPCSTR ptr;
810 enum {from_event, from_process_name} mode;
811 enum init_return (*backend)(struct inner_data*);
812 HANDLE event;
815 #define WINECON_CMD_SHOW_USAGE 0x10000
817 /******************************************************************
818 * WINECON_ParseOptions
820 * RETURNS
821 * On success: 0
822 * On error: error string id optionally with the CMD_SHOW_USAGE flag
824 static UINT WINECON_ParseOptions(const char* lpCmdLine, struct wc_init* wci)
826 memset(wci, 0, sizeof(*wci));
827 wci->ptr = lpCmdLine;
828 wci->mode = from_process_name;
829 wci->backend = WCUSER_InitBackend;
831 for (;;)
833 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
834 if (wci->ptr[0] != '-') break;
835 if (strncmp(wci->ptr, "--use-event=", 12) == 0)
837 char* end;
838 wci->event = ULongToHandle( strtoul(wci->ptr + 12, &end, 10) );
839 if (end == wci->ptr + 12) return IDS_CMD_INVALID_EVENT_ID;
840 wci->mode = from_event;
841 wci->ptr = end;
843 else if (strncmp(wci->ptr, "--backend=", 10) == 0)
845 if (strncmp(wci->ptr + 10, "user", 4) == 0)
847 wci->backend = WCUSER_InitBackend;
848 wci->ptr += 14;
850 else if (strncmp(wci->ptr + 10, "curses", 6) == 0)
852 wci->backend = WCCURSES_InitBackend;
853 wci->ptr += 16;
855 else
856 return IDS_CMD_INVALID_BACKEND;
858 else if (!strncmp(wci->ptr, "--help", 6) &&
859 (!wci->ptr[6] || wci->ptr[6] == ' ' || wci->ptr[6] == '\t'))
860 return IDS_CMD_ABOUT|WINECON_CMD_SHOW_USAGE;
861 else
862 return IDS_CMD_INVALID_OPTION|WINECON_CMD_SHOW_USAGE;
865 if (wci->mode == from_event)
866 return 0;
868 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
869 if (*wci->ptr == 0) wci->ptr = "cmd";
871 return 0;
874 /******************************************************************
875 * WinMain
877 * wineconsole can either be started as:
878 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
879 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
880 * a freshly created console
881 * --backend=(curses|user) can also be used to select the desired backend
883 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
885 struct inner_data* data;
886 int ret = 1;
887 struct wc_init wci;
889 if ((ret = WINECON_ParseOptions(lpCmdLine, &wci)) != 0)
891 printf_res(ret & 0xffff);
892 if (ret & WINECON_CMD_SHOW_USAGE)
893 WINECON_Usage();
894 return 0;
897 switch (wci.mode)
899 case from_event:
900 /* case of wineconsole <evt>, signal process that created us that we're up and running */
901 if (!(data = WINECON_Init(hInst, 0, NULL, wci.backend, nCmdShow))) return 1;
902 ret = !SetEvent(wci.event);
903 if (ret != 0) WINE_ERR("SetEvent failed.\n");
904 break;
905 case from_process_name:
907 int len;
908 WCHAR *buffer;
910 len = MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, NULL, 0);
912 buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
913 if (!buffer)
914 return 1;
916 MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, buffer, len);
918 if (!(data = WINECON_Init(hInst, GetCurrentProcessId(), buffer, wci.backend, nCmdShow)))
920 HeapFree(GetProcessHeap(), 0, buffer);
921 return 1;
923 ret = WINECON_Spawn(data, buffer);
924 HeapFree(GetProcessHeap(), 0, buffer);
925 if (ret != 0)
927 WINECON_Delete(data);
928 printf_res(IDS_CMD_LAUNCH_FAILED, wine_dbgstr_a(wci.ptr));
929 return ret;
932 break;
933 default:
934 return 1;
937 if (!ret)
939 DWORD exitcode;
941 WINE_TRACE("calling MainLoop.\n");
942 ret = data->fnMainLoop(data);
944 if (!ret && data->hProcess &&
945 WaitForSingleObject(data->hProcess, INFINITE) == WAIT_OBJECT_0 &&
946 GetExitCodeProcess(data->hProcess, &exitcode))
948 WINE_TRACE("forwarding exitcode %u from child process\n", exitcode);
949 ret = exitcode;
953 WINECON_Delete(data);
955 return ret;