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
22 #include "wine/port.h"
26 #include "wine/server.h"
27 #include "winecon_private.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
);
41 static void printf_res(UINT uResId
, ...)
47 va_start(args
, uResId
);
48 LoadStringW(GetModuleHandleW(NULL
), uResId
, buffer
, sizeof(buffer
)/sizeof(buffer
[0]));
49 WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, ansi
, sizeof(ansi
), NULL
, NULL
);
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 /******************************************************************
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
);
74 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
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
);
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;
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
)
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
);
132 /******************************************************************
133 * WINECON_SetHistoryMode
137 static BOOL
WINECON_SetHistoryMode(HANDLE hConIn
, int mode
)
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
);
152 /******************************************************************
153 * WINECON_SetInsertMode
157 static void WINECON_SetInsertMode(HANDLE hConIn
, unsigned int enable
)
161 GetConsoleMode(hConIn
, &mode
);
163 mode
|= ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
;
165 mode
&= ~ENABLE_INSERT_MODE
;
166 SetConsoleMode(hConIn
, mode
);
169 /******************************************************************
170 * WINECON_GetConsoleTitle
174 BOOL
WINECON_GetConsoleTitle(HANDLE hConIn
, WCHAR
* buffer
, size_t len
)
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;
194 /******************************************************************
195 * WINECON_SetEditionMode
199 static BOOL
WINECON_SetEditionMode(HANDLE hConIn
, int edition_mode
)
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
);
214 /******************************************************************
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
);
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
;
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]);
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 */
264 for (i
= num
- 1; i
>= 0; i
--)
266 if (evts
[i
].event
== CONSOLE_RENDERER_CURSOR_POS_EVENT
)
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
;
276 /* step 2: manage update events */
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
)
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
;
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
);
311 case CONSOLE_RENDERER_TITLE_EVENT
:
312 WINE_TRACE("%u/%u: title()\n", i
+1, num
);
313 data
->fnSetTitle(data
);
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
;
321 req
->share
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
322 h
= wine_server_call_err( req
) ? 0 : wine_server_ptr_handle(reply
->handle
);
325 WINE_TRACE("%u/%u: active(%p)\n", i
+1, num
, h
);
328 CloseHandle(data
->hConOut
);
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
);
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
);
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
);
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
);
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
);
393 case CONSOLE_RENDERER_EXIT_EVENT
:
395 WINE_TRACE("%u/%u: Exit!!\n", i
+1, num
);
398 WINE_FIXME("Unknown event type (%d)\n", evts
[i
].event
);
401 data
->in_grab_changes
= FALSE
;
404 /******************************************************************
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
);
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_weight
!= cfg
->font_weight
)
451 data
->fnSetFont(data
, cfg
->face_name
, cfg
->cell_height
, cfg
->font_weight
);
452 SystemParametersInfoW(SPI_GETWORKAREA
, 0, &r
, 0);
453 SERVER_START_REQ(set_console_output_info
)
455 req
->handle
= wine_server_obj_handle( data
->hConOut
);
456 req
->mask
= SET_CONSOLE_OUTPUT_INFO_MAX_SIZE
| SET_CONSOLE_OUTPUT_INFO_FONT
;
457 req
->max_width
= (r
.right
- r
.left
) / cfg
->cell_width
;
458 req
->max_height
= (r
.bottom
- r
.top
- GetSystemMetrics(SM_CYCAPTION
)) / cfg
->cell_height
;
459 req
->font_width
= cfg
->cell_width
;
460 req
->font_height
= cfg
->cell_height
;
461 wine_server_call( req
);
465 if (data
->curcfg
.def_attr
!= cfg
->def_attr
)
467 DWORD screen_size
, written
;
468 COORD top_left
= {0,0};
470 data
->curcfg
.def_attr
= cfg
->def_attr
;
471 screen_size
= cfg
->win_width
* (cfg
->win_height
+ 1);
472 FillConsoleOutputAttribute(data
->hConOut
, cfg
->def_attr
, screen_size
, top_left
, &written
);
473 SetConsoleTextAttribute(data
->hConOut
, cfg
->def_attr
);
475 WINECON_SetColors(data
, cfg
);
476 /* now let's look at the window / sb size changes...
477 * since the server checks that sb is always bigger than window,
478 * we have to take care of doing the operations in the right order
480 /* a set of macros to make things easier to read
481 * The Test<A><B> macros test if the <A> (width/height) needs to be changed
482 * for <B> (window / ScreenBuffer)
483 * The Change<A><B> actually modify the <B> dimension of <A>.
485 #define TstSBfWidth() (data->curcfg.sb_width != cfg->sb_width)
486 #define TstWinHPos() (data->curcfg.win_width != cfg->win_width || data->curcfg.win_pos.X != cfg->win_pos.X)
488 #define ChgSBfWidth() do {c.X = cfg->sb_width; \
489 c.Y = data->curcfg.sb_height;\
490 SetConsoleScreenBufferSize(data->hConOut, c);\
492 #define ChgWinHPos() do {pos.Left = cfg->win_pos.X - data->curcfg.win_pos.X; \
494 pos.Right = pos.Left + cfg->win_width - data->curcfg.win_width; \
496 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
498 #define TstSBfHeight() (data->curcfg.sb_height != cfg->sb_height)
499 #define TstWinVPos() (data->curcfg.win_height != cfg->win_height || data->curcfg.win_pos.Y != cfg->win_pos.Y)
501 /* since we're going to apply height after width is done, we use width as defined
502 * in cfg, and not in data->curcfg because if won't be updated yet */
503 #define ChgSBfHeight() do {c.X = cfg->sb_width; c.Y = cfg->sb_height; \
504 SetConsoleScreenBufferSize(data->hConOut, c); \
506 #define ChgWinVPos() do {pos.Left = 0; \
507 pos.Top = cfg->win_pos.Y - data->curcfg.win_pos.Y; \
509 pos.Bottom = pos.Top + cfg->win_height - data->curcfg.win_height; \
510 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
522 /* we're changing both at the same time, do it in the right order */
523 if (cfg
->sb_width
>= data
->curcfg
.win_width
)
525 ChgSBfWidth(); ChgWinHPos();
529 ChgWinHPos(); ChgSBfWidth();
534 else if (TstWinHPos()) ChgWinHPos();
539 if (cfg
->sb_height
>= data
->curcfg
.win_height
)
541 ChgSBfHeight(); ChgWinVPos();
545 ChgWinVPos(); ChgSBfHeight();
550 else if (TstWinVPos()) ChgWinVPos();
561 data
->curcfg
.exit_on_die
= cfg
->exit_on_die
;
562 if (data
->curcfg
.edition_mode
!= cfg
->edition_mode
)
564 data
->curcfg
.edition_mode
= cfg
->edition_mode
;
565 WINECON_SetEditionMode(data
->hConIn
, cfg
->edition_mode
);
567 /* we now need to gather all events we got from the operations above,
568 * in order to get data correctly updated
570 WINECON_GrabChanges(data
);
571 data
->in_set_config
= FALSE
;
574 /******************************************************************
577 * Destroy wineconsole internal data
579 static void WINECON_Delete(struct inner_data
* data
)
583 if (data
->fnDeleteBackend
) data
->fnDeleteBackend(data
);
584 if (data
->hConIn
) CloseHandle(data
->hConIn
);
585 if (data
->hConOut
) CloseHandle(data
->hConOut
);
586 if (data
->hSynchro
) CloseHandle(data
->hSynchro
);
587 if (data
->hProcess
) CloseHandle(data
->hProcess
);
588 HeapFree(GetProcessHeap(), 0, data
->curcfg
.registry
);
589 HeapFree(GetProcessHeap(), 0, data
->cells
);
590 HeapFree(GetProcessHeap(), 0, data
);
593 /******************************************************************
594 * WINECON_GetServerConfig
596 * Fills data->curcfg with the actual configuration running in the server
597 * (getting real information on the server, and not relying on cached
598 * information in data)
600 static BOOL
WINECON_GetServerConfig(struct inner_data
* data
)
605 SERVER_START_REQ(get_console_input_info
)
607 req
->handle
= wine_server_obj_handle( data
->hConIn
);
608 ret
= !wine_server_call_err( req
);
609 data
->curcfg
.history_size
= reply
->history_size
;
610 data
->curcfg
.history_nodup
= reply
->history_mode
;
611 data
->curcfg
.edition_mode
= reply
->edition_mode
;
614 if (!ret
) return FALSE
;
616 GetConsoleMode(data
->hConIn
, &mode
);
617 data
->curcfg
.insert_mode
= (mode
& (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
)) ==
618 (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
);
620 SERVER_START_REQ(get_console_output_info
)
622 req
->handle
= wine_server_obj_handle( data
->hConOut
);
623 ret
= !wine_server_call_err( req
);
624 data
->curcfg
.cursor_size
= reply
->cursor_size
;
625 data
->curcfg
.cursor_visible
= reply
->cursor_visible
;
626 data
->curcfg
.def_attr
= reply
->attr
;
627 data
->curcfg
.sb_width
= reply
->width
;
628 data
->curcfg
.sb_height
= reply
->height
;
629 data
->curcfg
.win_width
= reply
->win_right
- reply
->win_left
+ 1;
630 data
->curcfg
.win_height
= reply
->win_bottom
- reply
->win_top
+ 1;
633 WINECON_DumpConfig("first cfg: ", &data
->curcfg
);
638 /******************************************************************
641 * Initialisation part I. Creation of server object (console input and
642 * active screen buffer)
644 static struct inner_data
* WINECON_Init(HINSTANCE hInst
, DWORD pid
, LPCWSTR appname
,
645 enum init_return (*backend
)(struct inner_data
*),
648 struct inner_data
* data
= NULL
;
650 struct config_data cfg
;
653 data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
));
656 GetStartupInfoW(&si
);
658 if (pid
== 0) appname
= si
.lpTitle
;
660 data
->nCmdShow
= nCmdShow
;
662 WINECON_RegLoad(appname
, &cfg
);
667 if (si
.dwFlags
& STARTF_USECOUNTCHARS
)
669 cfg
.sb_width
= si
.dwXCountChars
;
670 cfg
.sb_height
= si
.dwYCountChars
;
672 if (si
.dwFlags
& STARTF_USEFILLATTRIBUTE
)
673 cfg
.def_attr
= si
.dwFillAttribute
;
674 /* should always be defined */
677 /* the handles here are created without the whistles and bells required by console
678 * (mainly because wineconsole doesn't need it)
679 * - they are not inheritable
680 * - hConIn is not synchronizable
682 SERVER_START_REQ(alloc_console
)
684 req
->access
= GENERIC_READ
| GENERIC_WRITE
;
689 ret
= !wine_server_call_err( req
);
690 data
->hConIn
= wine_server_ptr_handle( reply
->handle_in
);
691 data
->hSynchro
= wine_server_ptr_handle( reply
->event
);
694 if (!ret
) goto error
;
695 WINE_TRACE("using hConIn %p, hSynchro event %p\n", data
->hConIn
, data
->hSynchro
);
697 SERVER_START_REQ(create_console_output
)
699 req
->handle_in
= wine_server_obj_handle( data
->hConIn
);
700 req
->access
= GENERIC_WRITE
|GENERIC_READ
;
702 req
->share
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
704 ret
= !wine_server_call_err( req
);
705 data
->hConOut
= wine_server_ptr_handle( reply
->handle_out
);
708 if (!ret
) goto error
;
709 WINE_TRACE("using hConOut %p\n", data
->hConOut
);
711 /* filling data->curcfg from cfg */
712 switch ((*backend
)(data
))
714 case init_not_supported
:
715 if (backend
== WCCURSES_InitBackend
)
717 if (WCUSER_InitBackend( data
) != init_success
) break;
719 else if (backend
== WCUSER_InitBackend
)
721 if (WCCURSES_InitBackend( data
) != init_success
) break;
725 WINECON_GetServerConfig(data
);
726 data
->cells
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
727 data
->curcfg
.sb_width
* data
->curcfg
.sb_height
* sizeof(CHAR_INFO
));
728 if (!data
->cells
) goto error
;
729 data
->fnResizeScreenBuffer(data
);
730 data
->fnComputePositions(data
);
731 WINECON_SetConfig(data
, &cfg
);
732 data
->curcfg
.registry
= cfg
.registry
;
733 WINECON_DumpConfig("fint", &data
->curcfg
);
734 SERVER_START_REQ( set_console_input_info
)
736 req
->handle
= wine_server_obj_handle( data
->hConIn
);
737 req
->win
= wine_server_user_handle( data
->hWnd
);
738 req
->mask
= SET_CONSOLE_INPUT_INFO_TITLE
|
739 SET_CONSOLE_INPUT_INFO_WIN
;
740 wine_server_add_data( req
, appname
, lstrlenW(appname
) * sizeof(WCHAR
) );
741 ret
= !wine_server_call_err( req
);
744 if (!ret
) goto error
;
752 WINE_ERR("failed to init.\n");
754 WINECON_Delete(data
);
758 /******************************************************************
761 * Spawn the child process when invoked with wineconsole foo bar
763 static int WINECON_Spawn(struct inner_data
* data
, LPWSTR cmdLine
)
765 PROCESS_INFORMATION info
;
766 STARTUPINFOW startup
;
769 /* we're in the case wineconsole <exe> <options>... spawn the new process */
770 memset(&startup
, 0, sizeof(startup
));
771 startup
.cb
= sizeof(startup
);
772 startup
.dwFlags
= STARTF_USESTDHANDLES
;
774 /* the attributes of wineconsole's handles are not adequate for inheritance, so
775 * get them with the correct attributes before process creation
777 if (!DuplicateHandle(GetCurrentProcess(), data
->hConIn
, GetCurrentProcess(),
778 &startup
.hStdInput
, GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, 0) ||
779 !DuplicateHandle(GetCurrentProcess(), data
->hConOut
, GetCurrentProcess(),
780 &startup
.hStdOutput
, GENERIC_READ
|GENERIC_WRITE
, TRUE
, 0) ||
781 !DuplicateHandle(GetCurrentProcess(), data
->hConOut
, GetCurrentProcess(),
782 &startup
.hStdError
, GENERIC_READ
|GENERIC_WRITE
, TRUE
, 0))
784 WINE_ERR("Can't dup handles\n");
785 /* no need to delete handles, we're exiting the program anyway */
789 done
= CreateProcessW(NULL
, cmdLine
, NULL
, NULL
, TRUE
, 0L, NULL
, NULL
, &startup
, &info
);
792 data
->hProcess
= info
.hProcess
;
793 CloseHandle(info
.hThread
);
796 /* we no longer need the handles passed to the child for the console */
797 CloseHandle(startup
.hStdInput
);
798 CloseHandle(startup
.hStdOutput
);
799 CloseHandle(startup
.hStdError
);
806 enum {from_event
, from_process_name
} mode
;
807 enum init_return (*backend
)(struct inner_data
*);
811 #define WINECON_CMD_SHOW_USAGE 0x10000
813 /******************************************************************
814 * WINECON_ParseOptions
818 * On error: error string id optionally with the CMD_SHOW_USAGE flag
820 static UINT
WINECON_ParseOptions(const char* lpCmdLine
, struct wc_init
* wci
)
822 memset(wci
, 0, sizeof(*wci
));
823 wci
->ptr
= lpCmdLine
;
824 wci
->mode
= from_process_name
;
825 wci
->backend
= WCUSER_InitBackend
;
829 while (*wci
->ptr
== ' ' || *wci
->ptr
== '\t') wci
->ptr
++;
830 if (wci
->ptr
[0] != '-') break;
831 if (strncmp(wci
->ptr
, "--use-event=", 12) == 0)
834 wci
->event
= ULongToHandle( strtoul(wci
->ptr
+ 12, &end
, 10) );
835 if (end
== wci
->ptr
+ 12) return IDS_CMD_INVALID_EVENT_ID
;
836 wci
->mode
= from_event
;
839 else if (strncmp(wci
->ptr
, "--backend=", 10) == 0)
841 if (strncmp(wci
->ptr
+ 10, "user", 4) == 0)
843 wci
->backend
= WCUSER_InitBackend
;
846 else if (strncmp(wci
->ptr
+ 10, "curses", 6) == 0)
848 wci
->backend
= WCCURSES_InitBackend
;
852 return IDS_CMD_INVALID_BACKEND
;
854 else if (!strncmp(wci
->ptr
, "--help", 6) &&
855 (!wci
->ptr
[6] || wci
->ptr
[6] == ' ' || wci
->ptr
[6] == '\t'))
856 return IDS_CMD_ABOUT
|WINECON_CMD_SHOW_USAGE
;
858 return IDS_CMD_INVALID_OPTION
|WINECON_CMD_SHOW_USAGE
;
861 if (wci
->mode
== from_event
)
864 while (*wci
->ptr
== ' ' || *wci
->ptr
== '\t') wci
->ptr
++;
865 if (*wci
->ptr
== 0) wci
->ptr
= "cmd";
870 /******************************************************************
873 * wineconsole can either be started as:
874 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
875 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
876 * a freshly created console
877 * --backend=(curses|user) can also be used to select the desired backend
879 int PASCAL
WinMain(HINSTANCE hInst
, HINSTANCE hPrev
, LPSTR lpCmdLine
, INT nCmdShow
)
881 struct inner_data
* data
;
885 if ((ret
= WINECON_ParseOptions(lpCmdLine
, &wci
)) != 0)
887 printf_res(ret
& 0xffff);
888 if (ret
& WINECON_CMD_SHOW_USAGE
)
896 /* case of wineconsole <evt>, signal process that created us that we're up and running */
897 if (!(data
= WINECON_Init(hInst
, 0, NULL
, wci
.backend
, nCmdShow
))) return 1;
898 ret
= !SetEvent(wci
.event
);
899 if (ret
!= 0) WINE_ERR("SetEvent failed.\n");
901 case from_process_name
:
906 len
= MultiByteToWideChar(CP_ACP
, 0, wci
.ptr
, -1, NULL
, 0);
908 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
912 MultiByteToWideChar(CP_ACP
, 0, wci
.ptr
, -1, buffer
, len
);
914 if (!(data
= WINECON_Init(hInst
, GetCurrentProcessId(), buffer
, wci
.backend
, nCmdShow
)))
916 HeapFree(GetProcessHeap(), 0, buffer
);
919 ret
= WINECON_Spawn(data
, buffer
);
920 HeapFree(GetProcessHeap(), 0, buffer
);
923 WINECON_Delete(data
);
924 printf_res(IDS_CMD_LAUNCH_FAILED
, wine_dbgstr_a(wci
.ptr
));
937 WINE_TRACE("calling MainLoop.\n");
938 ret
= data
->fnMainLoop(data
);
940 if (!ret
&& data
->hProcess
&&
941 WaitForSingleObject(data
->hProcess
, INFINITE
) == WAIT_OBJECT_0
&&
942 GetExitCodeProcess(data
->hProcess
, &exitcode
))
944 WINE_TRACE("forwarding exitcode %u from child process\n", exitcode
);
949 WINECON_Delete(data
);