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
, ARRAY_SIZE(buffer
));
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_pitch_family
!= cfg
->font_pitch_family
||
449 data
->curcfg
.font_weight
!= cfg
->font_weight
)
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
);
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);\
496 #define ChgWinHPos() do {pos.Left = cfg->win_pos.X - data->curcfg.win_pos.X; \
498 pos.Right = pos.Left + cfg->win_width - data->curcfg.win_width; \
500 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
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); \
510 #define ChgWinVPos() do {pos.Left = 0; \
511 pos.Top = cfg->win_pos.Y - data->curcfg.win_pos.Y; \
513 pos.Bottom = pos.Top + cfg->win_height - data->curcfg.win_height; \
514 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
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();
533 ChgWinHPos(); ChgSBfWidth();
538 else if (TstWinHPos()) ChgWinHPos();
543 if (cfg
->sb_height
>= data
->curcfg
.win_height
)
545 ChgSBfHeight(); ChgWinVPos();
549 ChgWinVPos(); ChgSBfHeight();
554 else if (TstWinVPos()) 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 /******************************************************************
581 * Destroy wineconsole internal data
583 static void WINECON_Delete(struct inner_data
* data
)
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
)
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
;
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;
637 WINECON_DumpConfig("first cfg: ", &data
->curcfg
);
642 /******************************************************************
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
*),
652 struct inner_data
* data
= NULL
;
654 struct config_data cfg
;
657 data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
));
660 GetStartupInfoW(&si
);
662 if (pid
== 0) appname
= si
.lpTitle
;
664 data
->nCmdShow
= nCmdShow
;
666 WINECON_RegLoad(appname
, &cfg
);
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
;
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
);
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
;
706 req
->share
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
708 ret
= !wine_server_call_err( req
);
709 data
->hConOut
= wine_server_ptr_handle( reply
->handle_out
);
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;
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
);
748 if (!ret
) goto error
;
756 WINE_ERR("failed to init.\n");
758 WINECON_Delete(data
);
762 /******************************************************************
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
;
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 */
793 done
= CreateProcessW(NULL
, cmdLine
, NULL
, NULL
, TRUE
, 0L, NULL
, NULL
, &startup
, &info
);
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
);
810 enum {from_event
, from_process_name
} mode
;
811 enum init_return (*backend
)(struct inner_data
*);
815 #define WINECON_CMD_SHOW_USAGE 0x10000
817 /******************************************************************
818 * WINECON_ParseOptions
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
;
833 while (*wci
->ptr
== ' ' || *wci
->ptr
== '\t') wci
->ptr
++;
834 if (wci
->ptr
[0] != '-') break;
835 if (strncmp(wci
->ptr
, "--use-event=", 12) == 0)
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
;
843 else if (strncmp(wci
->ptr
, "--backend=", 10) == 0)
845 if (strncmp(wci
->ptr
+ 10, "user", 4) == 0)
847 wci
->backend
= WCUSER_InitBackend
;
850 else if (strncmp(wci
->ptr
+ 10, "curses", 6) == 0)
852 wci
->backend
= WCCURSES_InitBackend
;
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
;
862 return IDS_CMD_INVALID_OPTION
|WINECON_CMD_SHOW_USAGE
;
865 if (wci
->mode
== from_event
)
868 while (*wci
->ptr
== ' ' || *wci
->ptr
== '\t') wci
->ptr
++;
869 if (*wci
->ptr
== 0) wci
->ptr
= "cmd";
874 /******************************************************************
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
;
889 if ((ret
= WINECON_ParseOptions(lpCmdLine
, &wci
)) != 0)
891 printf_res(ret
& 0xffff);
892 if (ret
& WINECON_CMD_SHOW_USAGE
)
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");
905 case from_process_name
:
910 len
= MultiByteToWideChar(CP_ACP
, 0, wci
.ptr
, -1, NULL
, 0);
912 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
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
);
923 ret
= WINECON_Spawn(data
, buffer
);
924 HeapFree(GetProcessHeap(), 0, buffer
);
927 WINECON_Delete(data
);
928 printf_res(IDS_CMD_LAUNCH_FAILED
, wine_dbgstr_a(wci
.ptr
));
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
);
953 WINECON_Delete(data
);