wineconsole: Use IOCTL_CONDRV_SET_INPUT_INFO in WINECON_SetHistoryMode.
[wine.git] / programs / wineconsole / wineconsole.c
blob2ad9cc84ee0324d0268d7f47cc68972e354434e0
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 || data->in_grab_changes) 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 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_HISTORY_SIZE };
120 params.info.history_size = size;
121 return DeviceIoControl(hConIn, IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL, NULL);
124 /******************************************************************
125 * WINECON_SetHistoryMode
129 static BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
131 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_HISTORY_MODE };
132 params.info.history_mode = mode;
133 return DeviceIoControl(hConIn, IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL, NULL);
136 /******************************************************************
137 * WINECON_SetInsertMode
141 static void WINECON_SetInsertMode(HANDLE hConIn, unsigned int enable)
143 DWORD mode;
145 GetConsoleMode(hConIn, &mode);
146 if (enable)
147 mode |= ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS;
148 else
149 mode &= ~ENABLE_INSERT_MODE;
150 SetConsoleMode(hConIn, mode);
153 /******************************************************************
154 * WINECON_GetConsoleTitle
158 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
160 DWORD size;
162 if (!DeviceIoControl(hConIn, IOCTL_CONDRV_GET_TITLE, NULL, 0, buffer, len - sizeof(WCHAR), &size, NULL))
163 return FALSE;
165 buffer[size / sizeof(WCHAR)] = 0;
166 return TRUE;
169 /******************************************************************
170 * WINECON_SetEditionMode
174 static BOOL WINECON_SetEditionMode(HANDLE hConIn, int edition_mode)
176 BOOL ret;
178 SERVER_START_REQ( set_console_input_info )
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 );
185 SERVER_END_REQ;
186 return ret;
189 /******************************************************************
190 * WINECON_SetColors
192 * Sets ColorTable and Pop-up menu colors
194 static void WINECON_SetColors(struct inner_data *data, const struct config_data* cfg)
196 struct condrv_output_info_params params =
197 { SET_CONSOLE_OUTPUT_INFO_COLORTABLE | SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR };
199 memcpy(data->curcfg.color_map, cfg->color_map, sizeof(data->curcfg.color_map));
200 data->curcfg.popup_attr = cfg->popup_attr;
202 params.info.popup_attr = cfg->popup_attr;
203 memcpy(params.info.color_map, cfg->color_map, sizeof(cfg->color_map));
204 DeviceIoControl(data->hConOut, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL, NULL);
207 /******************************************************************
208 * WINECON_GrabChanges
210 * A change occurs, try to figure out which
212 void WINECON_GrabChanges(struct inner_data* data)
214 struct condrv_renderer_event *evts = data->events;
215 int i, ev_found;
216 DWORD num;
217 HANDLE h;
219 if (data->in_grab_changes) return;
221 if (!GetOverlappedResult(data->hSynchro, &data->overlapped, &num, FALSE))
223 ERR( "failed to get renderer events: %u\n", GetLastError() );
224 data->dying = TRUE;
225 return;
227 num /= sizeof(data->events[0]);
228 WINE_TRACE( "got %u events\n", num );
230 /* FIXME: should do some event compression here (cursor pos, update) */
231 /* step 1: keep only last cursor pos event */
232 ev_found = -1;
233 for (i = num - 1; i >= 0; i--)
235 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
237 if (ev_found != -1)
239 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);
240 evts[i].event = CONSOLE_RENDERER_NONE_EVENT;
242 ev_found = i;
245 /* step 2: manage update events */
246 ev_found = -1;
247 for (i = 0; i < num; i++)
249 if (evts[i].event == CONSOLE_RENDERER_NONE_EVENT ||
250 evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT ||
251 evts[i].event == CONSOLE_RENDERER_CURSOR_GEOM_EVENT) continue;
252 if (evts[i].event != CONSOLE_RENDERER_UPDATE_EVENT)
254 ev_found = -1;
255 continue;
258 if (ev_found != -1 && /* Only 2 cases where they CANNOT merge */
259 !(evts[i ].u.update.bottom + 1 < evts[ev_found].u.update.top ||
260 evts[ev_found].u.update.bottom + 1 < evts[i ].u.update.top))
262 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);
263 evts[i].u.update.top = min(evts[i ].u.update.top,
264 evts[ev_found].u.update.top);
265 evts[i].u.update.bottom = max(evts[i ].u.update.bottom,
266 evts[ev_found].u.update.bottom);
267 evts[ev_found].event = CONSOLE_RENDERER_NONE_EVENT;
269 ev_found = i;
272 data->in_grab_changes = TRUE;
273 for (i = 0; i < num; i++)
275 switch (evts[i].event)
277 case CONSOLE_RENDERER_NONE_EVENT:
278 WINE_TRACE("%u/%u: NOP\n", i+1, num);
279 break;
280 case CONSOLE_RENDERER_TITLE_EVENT:
281 WINE_TRACE("%u/%u: title()\n", i+1, num);
282 data->fnSetTitle(data);
283 break;
284 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
285 SERVER_START_REQ( open_console )
287 req->from = wine_server_obj_handle( data->hConIn );
288 req->access = GENERIC_READ | GENERIC_WRITE;
289 req->attributes = 0;
290 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
291 h = wine_server_call_err( req ) ? 0 : wine_server_ptr_handle(reply->handle);
293 SERVER_END_REQ;
294 WINE_TRACE("%u/%u: active(%p)\n", i+1, num, h);
295 if (h)
297 CloseHandle(data->hConOut);
298 data->hConOut = h;
300 break;
301 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
302 if (data->curcfg.sb_width != evts[i].u.resize.width ||
303 data->curcfg.sb_height != evts[i].u.resize.height)
305 WINE_TRACE("%u/%u: resize(%d,%d)\n", i+1, num, evts[i].u.resize.width, evts[i].u.resize.height);
306 data->curcfg.sb_width = evts[i].u.resize.width;
307 data->curcfg.sb_height = evts[i].u.resize.height;
309 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
310 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
312 if (!data->cells) WINECON_Fatal("OOM");
313 data->fnResizeScreenBuffer(data);
314 data->fnComputePositions(data);
316 break;
317 case CONSOLE_RENDERER_UPDATE_EVENT:
318 WINE_TRACE("%u/%u: update(%d,%d)\n", i+1, num, evts[i].u.update.top, evts[i].u.update.bottom);
319 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
320 break;
321 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
322 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
324 WINE_TRACE("%u/%u: curs-pos(%d,%d)\n", i+1, num, evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
325 data->cursor.X = evts[i].u.cursor_pos.x;
326 data->cursor.Y = evts[i].u.cursor_pos.y;
327 data->fnPosCursor(data);
329 break;
330 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
331 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
332 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
334 WINE_TRACE("%u/%u: curs-geom(%d,%d)\n", i+1, num,
335 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
336 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
337 evts[i].u.cursor_geom.visible, FALSE);
339 break;
340 case CONSOLE_RENDERER_DISPLAY_EVENT:
341 if (evts[i].u.display.left != data->curcfg.win_pos.X)
343 WINE_TRACE("%u/%u: h-scroll(%d)\n", i+1, num, evts[i].u.display.left);
344 data->fnScroll(data, evts[i].u.display.left, TRUE);
345 data->fnPosCursor(data);
347 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
349 WINE_TRACE("%u/%u: v-scroll(%d)\n", i+1, num, evts[i].u.display.top);
350 data->fnScroll(data, evts[i].u.display.top, FALSE);
351 data->fnPosCursor(data);
353 if (evts[i].u.display.width != data->curcfg.win_width ||
354 evts[i].u.display.height != data->curcfg.win_height)
356 WINE_TRACE("%u/%u: win-size(%d,%d)\n", i+1, num, evts[i].u.display.width, evts[i].u.display.height);
357 data->curcfg.win_width = evts[i].u.display.width;
358 data->curcfg.win_height = evts[i].u.display.height;
359 data->fnComputePositions(data);
361 break;
362 case CONSOLE_RENDERER_EXIT_EVENT:
363 data->dying = TRUE;
364 WINE_TRACE("%u/%u: Exit!!\n", i+1, num);
365 return;
366 default:
367 WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
370 data->in_grab_changes = FALSE;
372 if (!DeviceIoControl(data->hSynchro, IOCTL_CONDRV_GET_RENDERER_EVENTS, NULL, 0, data->events,
373 sizeof(data->events), NULL, &data->overlapped) && GetLastError() != ERROR_IO_PENDING)
375 ERR("failed to get renderer events: %u\n", GetLastError());
376 data->dying = TRUE;
380 /******************************************************************
381 * WINECON_SetConfig
383 * Apply to data all the configuration elements from cfg. This includes modification
384 * of server side equivalent and visual parts.
385 * If force is FALSE, only the changed items are modified.
387 void WINECON_SetConfig(struct inner_data* data, const struct config_data* cfg)
389 if (data->in_set_config) return;
390 data->in_set_config = TRUE;
391 if (data->curcfg.cursor_size != cfg->cursor_size ||
392 data->curcfg.cursor_visible != cfg->cursor_visible)
394 CONSOLE_CURSOR_INFO cinfo;
395 cinfo.dwSize = cfg->cursor_size;
396 /* <FIXME>: this hack is needed to pass through the invariant test operation on the server side
397 * (no notification is sent when invariant operation is requested)
399 cinfo.bVisible = !cfg->cursor_visible;
400 SetConsoleCursorInfo(data->hConOut, &cinfo);
401 /* </FIXME> */
402 cinfo.bVisible = cfg->cursor_visible;
403 /* this shall update (through notif) curcfg */
404 SetConsoleCursorInfo(data->hConOut, &cinfo);
406 if (data->curcfg.history_size != cfg->history_size)
408 data->curcfg.history_size = cfg->history_size;
409 WINECON_SetHistorySize(data->hConIn, cfg->history_size);
411 if (data->curcfg.history_nodup != cfg->history_nodup)
413 data->curcfg.history_nodup = cfg->history_nodup;
414 WINECON_SetHistoryMode(data->hConIn, cfg->history_nodup);
416 if (data->curcfg.insert_mode != cfg->insert_mode)
418 data->curcfg.insert_mode = cfg->insert_mode;
419 WINECON_SetInsertMode(data->hConIn, cfg->insert_mode);
421 data->curcfg.menu_mask = cfg->menu_mask;
422 data->curcfg.quick_edit = cfg->quick_edit;
423 if (strcmpiW(data->curcfg.face_name, cfg->face_name) || data->curcfg.cell_width != cfg->cell_width ||
424 data->curcfg.cell_height != cfg->cell_height || data->curcfg.font_pitch_family != cfg->font_pitch_family ||
425 data->curcfg.font_weight != cfg->font_weight)
427 struct condrv_output_info_params *params;
428 size_t len = lstrlenW(cfg->face_name);
429 RECT r;
430 data->fnSetFont(data, cfg->face_name, cfg->cell_height, cfg->font_weight);
431 SystemParametersInfoW(SPI_GETWORKAREA, 0, &r, 0);
432 if ((params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*params) + len * sizeof(WCHAR))))
434 params->mask = SET_CONSOLE_OUTPUT_INFO_MAX_SIZE | SET_CONSOLE_OUTPUT_INFO_FONT;
435 params->info.max_width = (r.right - r.left) / cfg->cell_width;
436 params->info.max_height = (r.bottom - r.top - GetSystemMetrics(SM_CYCAPTION)) / cfg->cell_height;
437 params->info.font_width = cfg->cell_width;
438 params->info.font_height = cfg->cell_height;
439 params->info.font_weight = cfg->font_weight;
440 params->info.font_pitch_family = cfg->font_pitch_family;
441 memcpy(params + 1, cfg->face_name, len * sizeof(WCHAR));
442 DeviceIoControl(data->hConOut, IOCTL_CONDRV_SET_OUTPUT_INFO, params, sizeof(*params) + len * sizeof(WCHAR),
443 NULL, 0, NULL, NULL);
446 if (data->curcfg.def_attr != cfg->def_attr)
448 DWORD screen_size, written;
449 COORD top_left = {0,0};
451 data->curcfg.def_attr = cfg->def_attr;
452 screen_size = cfg->win_width * (cfg->win_height + 1);
453 FillConsoleOutputAttribute(data->hConOut, cfg->def_attr, screen_size, top_left, &written);
454 SetConsoleTextAttribute(data->hConOut, cfg->def_attr);
456 WINECON_SetColors(data, cfg);
457 /* now let's look at the window / sb size changes...
458 * since the server checks that sb is always bigger than window,
459 * we have to take care of doing the operations in the right order
461 /* a set of macros to make things easier to read
462 * The Test<A><B> macros test if the <A> (width/height) needs to be changed
463 * for <B> (window / ScreenBuffer)
464 * The Change<A><B> actually modify the <B> dimension of <A>.
466 #define TstSBfWidth() (data->curcfg.sb_width != cfg->sb_width)
467 #define TstWinHPos() (data->curcfg.win_width != cfg->win_width || data->curcfg.win_pos.X != cfg->win_pos.X)
469 #define ChgSBfWidth() do {c.X = cfg->sb_width; \
470 c.Y = data->curcfg.sb_height;\
471 SetConsoleScreenBufferSize(data->hConOut, c);\
472 } while (0)
473 #define ChgWinHPos() do {pos.Left = cfg->win_pos.X - data->curcfg.win_pos.X; \
474 pos.Top = 0; \
475 pos.Right = pos.Left + cfg->win_width - data->curcfg.win_width; \
476 pos.Bottom = 0; \
477 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
478 } while (0)
479 #define TstSBfHeight() (data->curcfg.sb_height != cfg->sb_height)
480 #define TstWinVPos() (data->curcfg.win_height != cfg->win_height || data->curcfg.win_pos.Y != cfg->win_pos.Y)
482 /* since we're going to apply height after width is done, we use width as defined
483 * in cfg, and not in data->curcfg because if won't be updated yet */
484 #define ChgSBfHeight() do {c.X = cfg->sb_width; c.Y = cfg->sb_height; \
485 SetConsoleScreenBufferSize(data->hConOut, c); \
486 } while (0)
487 #define ChgWinVPos() do {pos.Left = 0; \
488 pos.Top = cfg->win_pos.Y - data->curcfg.win_pos.Y; \
489 pos.Right = 0; \
490 pos.Bottom = pos.Top + cfg->win_height - data->curcfg.win_height; \
491 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
492 } while (0)
496 COORD c;
497 SMALL_RECT pos;
499 if (TstSBfWidth())
501 if (TstWinHPos())
503 /* we're changing both at the same time, do it in the right order */
504 if (cfg->sb_width >= data->curcfg.win_width)
506 ChgSBfWidth(); ChgWinHPos();
508 else
510 ChgWinHPos(); ChgSBfWidth();
513 else ChgSBfWidth();
515 else if (TstWinHPos()) ChgWinHPos();
516 if (TstSBfHeight())
518 if (TstWinVPos())
520 if (cfg->sb_height >= data->curcfg.win_height)
522 ChgSBfHeight(); ChgWinVPos();
524 else
526 ChgWinVPos(); ChgSBfHeight();
529 else ChgSBfHeight();
531 else if (TstWinVPos()) ChgWinVPos();
532 } while (0);
533 #undef TstSBfWidth
534 #undef TstWinHPos
535 #undef ChgSBfWidth
536 #undef ChgWinHPos
537 #undef TstSBfHeight
538 #undef TstWinVPos
539 #undef ChgSBfHeight
540 #undef ChgWinVPos
542 data->curcfg.exit_on_die = cfg->exit_on_die;
543 if (data->curcfg.edition_mode != cfg->edition_mode)
545 data->curcfg.edition_mode = cfg->edition_mode;
546 WINECON_SetEditionMode(data->hConIn, cfg->edition_mode);
548 /* we now need to gather all events we got from the operations above,
549 * in order to get data correctly updated
551 WINECON_GrabChanges(data);
552 data->in_set_config = FALSE;
555 /******************************************************************
556 * WINECON_Delete
558 * Destroy wineconsole internal data
560 static void WINECON_Delete(struct inner_data* data)
562 if (!data) return;
564 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
565 if (data->hConIn) CloseHandle(data->hConIn);
566 if (data->hConOut) CloseHandle(data->hConOut);
567 if (data->hSynchro) CloseHandle(data->hSynchro);
568 if (data->hProcess) CloseHandle(data->hProcess);
569 if (data->overlapped.hEvent) CloseHandle(data->overlapped.hEvent);
570 HeapFree(GetProcessHeap(), 0, data->curcfg.registry);
571 HeapFree(GetProcessHeap(), 0, data->cells);
572 HeapFree(GetProcessHeap(), 0, data);
575 /******************************************************************
576 * WINECON_GetServerConfig
578 * Fills data->curcfg with the actual configuration running in the server
579 * (getting real information on the server, and not relying on cached
580 * information in data)
582 static BOOL WINECON_GetServerConfig(struct inner_data* data)
584 struct condrv_input_info input_info;
585 struct condrv_output_info output_info;
586 DWORD mode;
588 if (!DeviceIoControl(data->hConIn, IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0,
589 &input_info, sizeof(input_info), NULL, NULL))
590 return FALSE;
591 data->curcfg.history_size = input_info.history_size;
592 data->curcfg.history_nodup = input_info.history_mode;
593 data->curcfg.edition_mode = input_info.edition_mode;
595 GetConsoleMode(data->hConIn, &mode);
596 data->curcfg.insert_mode = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) ==
597 (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS);
599 if (!DeviceIoControl(data->hConOut, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
600 &output_info, sizeof(output_info), NULL, NULL))
601 return FALSE;
602 data->curcfg.cursor_size = output_info.cursor_size;
603 data->curcfg.cursor_visible = output_info.cursor_visible;
604 data->curcfg.def_attr = output_info.attr;
605 data->curcfg.sb_width = output_info.width;
606 data->curcfg.sb_height = output_info.height;
607 data->curcfg.win_width = output_info.win_right - output_info.win_left + 1;
608 data->curcfg.win_height = output_info.win_bottom - output_info.win_top + 1;
610 WINECON_DumpConfig("first cfg: ", &data->curcfg);
611 return TRUE;
614 /******************************************************************
615 * WINECON_Init
617 * Initialisation part I. Creation of server object (console input and
618 * active screen buffer)
620 static struct inner_data* WINECON_Init(HINSTANCE hInst, DWORD pid, LPCWSTR appname,
621 enum init_return (*backend)(struct inner_data*),
622 INT nCmdShow)
624 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
625 struct inner_data* data = NULL;
626 DWORD ret;
627 struct config_data cfg;
628 STARTUPINFOW si;
629 UNICODE_STRING string;
630 IO_STATUS_BLOCK io;
631 condrv_handle_t h;
632 NTSTATUS status;
634 static const WCHAR renderer_pathW[] = {'\\','D','e','v','i','c','e','\\','C','o','n','D','r','v',
635 '\\','R','e','n','d','e','r','e','r',0};
637 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
638 if (!data) return 0;
640 GetStartupInfoW(&si);
642 if (pid == 0) appname = si.lpTitle;
644 data->nCmdShow = nCmdShow;
645 /* load settings */
646 WINECON_RegLoad(appname, &cfg);
648 /* some overrides */
649 if (pid == 0)
651 if (si.dwFlags & STARTF_USECOUNTCHARS)
653 cfg.sb_width = si.dwXCountChars;
654 cfg.sb_height = si.dwYCountChars;
656 if (si.dwFlags & STARTF_USEFILLATTRIBUTE)
657 cfg.def_attr = si.dwFillAttribute;
658 /* should always be defined */
661 if (!(data->overlapped.hEvent = CreateEventW(NULL, TRUE, TRUE, NULL))) goto error;
663 /* the handles here are created without the whistles and bells required by console
664 * (mainly because wineconsole doesn't need it)
665 * - they are not inheritable
666 * - hConIn is not synchronizable
668 SERVER_START_REQ(alloc_console)
670 req->access = GENERIC_READ | GENERIC_WRITE;
671 req->attributes = 0;
672 req->pid = pid;
673 req->input_fd = -1;
675 ret = !wine_server_call_err( req );
676 data->hConIn = wine_server_ptr_handle( reply->handle_in );
678 SERVER_END_REQ;
679 if (!ret) goto error;
680 WINE_TRACE("using hConIn %p, hSynchro event %p\n", data->hConIn, data->hSynchro);
682 RtlInitUnicodeString(&string, renderer_pathW);
683 attr.ObjectName = &string;
684 status = NtCreateFile(&data->hSynchro, FILE_READ_DATA | FILE_WRITE_DATA | FILE_WRITE_PROPERTIES
685 | FILE_READ_PROPERTIES | SYNCHRONIZE, &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL,
686 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE, NULL, 0);
687 if (status) goto error;
689 h = condrv_handle(data->hConIn);
690 if (!DeviceIoControl(data->hSynchro, IOCTL_CONDRV_ATTACH_RENDERER, &h, sizeof(h), NULL, 0, NULL, NULL))
691 goto error;
693 SERVER_START_REQ(create_console_output)
695 req->handle_in = wine_server_obj_handle( data->hConIn );
696 req->access = GENERIC_WRITE|GENERIC_READ;
697 req->attributes = 0;
698 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
699 req->fd = -1;
700 ret = !wine_server_call_err( req );
701 data->hConOut = wine_server_ptr_handle( reply->handle_out );
703 SERVER_END_REQ;
704 if (!ret) goto error;
705 WINE_TRACE("using hConOut %p\n", data->hConOut);
707 /* filling data->curcfg from cfg */
708 switch ((*backend)(data))
710 case init_not_supported:
711 if (backend == WCCURSES_InitBackend)
713 if (WCUSER_InitBackend( data ) != init_success) break;
715 else if (backend == WCUSER_InitBackend)
717 if (WCCURSES_InitBackend( data ) != init_success) break;
719 /* fall through */
720 case init_success:
721 WINECON_GetServerConfig(data);
722 data->cells = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
723 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
724 if (!data->cells) goto error;
725 data->fnResizeScreenBuffer(data);
726 data->fnComputePositions(data);
727 WINECON_SetConfig(data, &cfg);
728 data->curcfg.registry = cfg.registry;
729 WINECON_DumpConfig("fint", &data->curcfg);
730 SERVER_START_REQ( set_console_input_info )
732 req->handle = wine_server_obj_handle( data->hConIn );
733 req->win = wine_server_user_handle( data->hWnd );
734 req->mask = SET_CONSOLE_INPUT_INFO_TITLE |
735 SET_CONSOLE_INPUT_INFO_WIN;
736 wine_server_add_data( req, appname, lstrlenW(appname) * sizeof(WCHAR) );
737 ret = !wine_server_call_err( req );
739 SERVER_END_REQ;
740 if (!ret) goto error;
742 return data;
743 case init_failed:
744 break;
747 error:
748 WINE_ERR("failed to init.\n");
750 WINECON_Delete(data);
751 return NULL;
754 /******************************************************************
755 * WINECON_Spawn
757 * Spawn the child process when invoked with wineconsole foo bar
759 static int WINECON_Spawn(struct inner_data* data, LPWSTR cmdLine)
761 PROCESS_INFORMATION info;
762 STARTUPINFOW startup;
763 BOOL done;
765 /* we're in the case wineconsole <exe> <options>... spawn the new process */
766 memset(&startup, 0, sizeof(startup));
767 startup.cb = sizeof(startup);
768 startup.dwFlags = STARTF_USESTDHANDLES;
770 /* the attributes of wineconsole's handles are not adequate for inheritance, so
771 * get them with the correct attributes before process creation
773 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
774 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
775 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
776 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
777 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
778 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
780 WINE_ERR("Can't dup handles\n");
781 /* no need to delete handles, we're exiting the program anyway */
782 return 1;
785 done = CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
786 if (done)
788 data->hProcess = info.hProcess;
789 CloseHandle(info.hThread);
792 /* we no longer need the handles passed to the child for the console */
793 CloseHandle(startup.hStdInput);
794 CloseHandle(startup.hStdOutput);
795 CloseHandle(startup.hStdError);
797 return !done;
800 struct wc_init {
801 LPCSTR ptr;
802 enum {from_event, from_process_name} mode;
803 enum init_return (*backend)(struct inner_data*);
804 HANDLE event;
807 #define WINECON_CMD_SHOW_USAGE 0x10000
809 /******************************************************************
810 * WINECON_ParseOptions
812 * RETURNS
813 * On success: 0
814 * On error: error string id optionally with the CMD_SHOW_USAGE flag
816 static UINT WINECON_ParseOptions(const char* lpCmdLine, struct wc_init* wci)
818 memset(wci, 0, sizeof(*wci));
819 wci->ptr = lpCmdLine;
820 wci->mode = from_process_name;
821 wci->backend = WCUSER_InitBackend;
823 for (;;)
825 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
826 if (wci->ptr[0] != '-') break;
827 if (strncmp(wci->ptr, "--use-event=", 12) == 0)
829 char* end;
830 wci->event = ULongToHandle( strtoul(wci->ptr + 12, &end, 10) );
831 if (end == wci->ptr + 12) return IDS_CMD_INVALID_EVENT_ID;
832 wci->mode = from_event;
833 wci->ptr = end;
835 else if (strncmp(wci->ptr, "--backend=", 10) == 0)
837 if (strncmp(wci->ptr + 10, "user", 4) == 0)
839 wci->backend = WCUSER_InitBackend;
840 wci->ptr += 14;
842 else if (strncmp(wci->ptr + 10, "curses", 6) == 0)
844 wci->backend = WCCURSES_InitBackend;
845 wci->ptr += 16;
847 else
848 return IDS_CMD_INVALID_BACKEND;
850 else if (!strncmp(wci->ptr, "--help", 6) &&
851 (!wci->ptr[6] || wci->ptr[6] == ' ' || wci->ptr[6] == '\t'))
852 return IDS_CMD_ABOUT|WINECON_CMD_SHOW_USAGE;
853 else
854 return IDS_CMD_INVALID_OPTION|WINECON_CMD_SHOW_USAGE;
857 if (wci->mode == from_event)
858 return 0;
860 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
861 if (*wci->ptr == 0) wci->ptr = "cmd";
863 return 0;
866 /******************************************************************
867 * WinMain
869 * wineconsole can either be started as:
870 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
871 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
872 * a freshly created console
873 * --backend=(curses|user) can also be used to select the desired backend
875 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
877 struct inner_data* data;
878 int ret = 1;
879 struct wc_init wci;
881 if ((ret = WINECON_ParseOptions(lpCmdLine, &wci)) != 0)
883 printf_res(ret & 0xffff);
884 if (ret & WINECON_CMD_SHOW_USAGE)
885 WINECON_Usage();
886 return 0;
889 switch (wci.mode)
891 case from_event:
892 /* case of wineconsole <evt>, signal process that created us that we're up and running */
893 if (!(data = WINECON_Init(hInst, 0, NULL, wci.backend, nCmdShow))) return 1;
894 ret = !SetEvent(wci.event);
895 if (ret != 0) WINE_ERR("SetEvent failed.\n");
896 break;
897 case from_process_name:
899 int len;
900 WCHAR *buffer;
902 len = MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, NULL, 0);
904 buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
905 if (!buffer)
906 return 1;
908 MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, buffer, len);
910 if (!(data = WINECON_Init(hInst, GetCurrentProcessId(), buffer, wci.backend, nCmdShow)))
912 HeapFree(GetProcessHeap(), 0, buffer);
913 return 1;
915 ret = WINECON_Spawn(data, buffer);
916 HeapFree(GetProcessHeap(), 0, buffer);
917 if (ret != 0)
919 WINECON_Delete(data);
920 printf_res(IDS_CMD_LAUNCH_FAILED, wine_dbgstr_a(wci.ptr));
921 return ret;
924 break;
925 default:
926 return 1;
929 if (!ret)
931 DWORD exitcode;
933 WINE_TRACE("calling MainLoop.\n");
934 ret = data->fnMainLoop(data);
936 if (!ret && data->hProcess &&
937 WaitForSingleObject(data->hProcess, INFINITE) == WAIT_OBJECT_0 &&
938 GetExitCodeProcess(data->hProcess, &exitcode))
940 WINE_TRACE("forwarding exitcode %u from child process\n", exitcode);
941 ret = exitcode;
945 WINECON_Delete(data);
947 return ret;