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
|| data
->in_grab_changes
) 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
)
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
, ¶ms
, 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
, ¶ms
, sizeof(params
), NULL
, 0, NULL
, NULL
);
136 /******************************************************************
137 * WINECON_SetInsertMode
141 static void WINECON_SetInsertMode(HANDLE hConIn
, unsigned int enable
)
145 GetConsoleMode(hConIn
, &mode
);
147 mode
|= ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
;
149 mode
&= ~ENABLE_INSERT_MODE
;
150 SetConsoleMode(hConIn
, mode
);
153 /******************************************************************
154 * WINECON_GetConsoleTitle
158 BOOL
WINECON_GetConsoleTitle(HANDLE hConIn
, WCHAR
* buffer
, size_t len
)
162 if (!DeviceIoControl(hConIn
, IOCTL_CONDRV_GET_TITLE
, NULL
, 0, buffer
, len
- sizeof(WCHAR
), &size
, NULL
))
165 buffer
[size
/ sizeof(WCHAR
)] = 0;
169 /******************************************************************
170 * WINECON_SetEditionMode
174 static BOOL
WINECON_SetEditionMode(HANDLE hConIn
, int edition_mode
)
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
);
189 /******************************************************************
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
, ¶ms
, 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
;
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() );
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 */
233 for (i
= num
- 1; i
>= 0; i
--)
235 if (evts
[i
].event
== CONSOLE_RENDERER_CURSOR_POS_EVENT
)
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
;
245 /* step 2: manage update events */
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
)
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
;
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
);
280 case CONSOLE_RENDERER_TITLE_EVENT
:
281 WINE_TRACE("%u/%u: title()\n", i
+1, num
);
282 data
->fnSetTitle(data
);
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
;
290 req
->share
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
291 h
= wine_server_call_err( req
) ? 0 : wine_server_ptr_handle(reply
->handle
);
294 WINE_TRACE("%u/%u: active(%p)\n", i
+1, num
, h
);
297 CloseHandle(data
->hConOut
);
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
);
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
);
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
);
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
);
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
);
362 case CONSOLE_RENDERER_EXIT_EVENT
:
364 WINE_TRACE("%u/%u: Exit!!\n", i
+1, num
);
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());
380 /******************************************************************
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
);
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
);
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);\
473 #define ChgWinHPos() do {pos.Left = cfg->win_pos.X - data->curcfg.win_pos.X; \
475 pos.Right = pos.Left + cfg->win_width - data->curcfg.win_width; \
477 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
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); \
487 #define ChgWinVPos() do {pos.Left = 0; \
488 pos.Top = cfg->win_pos.Y - data->curcfg.win_pos.Y; \
490 pos.Bottom = pos.Top + cfg->win_height - data->curcfg.win_height; \
491 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
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();
510 ChgWinHPos(); ChgSBfWidth();
515 else if (TstWinHPos()) ChgWinHPos();
520 if (cfg
->sb_height
>= data
->curcfg
.win_height
)
522 ChgSBfHeight(); ChgWinVPos();
526 ChgWinVPos(); ChgSBfHeight();
531 else if (TstWinVPos()) 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 /******************************************************************
558 * Destroy wineconsole internal data
560 static void WINECON_Delete(struct inner_data
* data
)
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
;
588 if (!DeviceIoControl(data
->hConIn
, IOCTL_CONDRV_GET_INPUT_INFO
, NULL
, 0,
589 &input_info
, sizeof(input_info
), NULL
, NULL
))
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
))
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
);
614 /******************************************************************
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
*),
624 OBJECT_ATTRIBUTES attr
= {sizeof(attr
)};
625 struct inner_data
* data
= NULL
;
627 struct config_data cfg
;
629 UNICODE_STRING string
;
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
));
640 GetStartupInfoW(&si
);
642 if (pid
== 0) appname
= si
.lpTitle
;
644 data
->nCmdShow
= nCmdShow
;
646 WINECON_RegLoad(appname
, &cfg
);
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
;
675 ret
= !wine_server_call_err( req
);
676 data
->hConIn
= wine_server_ptr_handle( reply
->handle_in
);
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
))
693 SERVER_START_REQ(create_console_output
)
695 req
->handle_in
= wine_server_obj_handle( data
->hConIn
);
696 req
->access
= GENERIC_WRITE
|GENERIC_READ
;
698 req
->share
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
700 ret
= !wine_server_call_err( req
);
701 data
->hConOut
= wine_server_ptr_handle( reply
->handle_out
);
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;
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
);
740 if (!ret
) goto error
;
748 WINE_ERR("failed to init.\n");
750 WINECON_Delete(data
);
754 /******************************************************************
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
;
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 */
785 done
= CreateProcessW(NULL
, cmdLine
, NULL
, NULL
, TRUE
, 0L, NULL
, NULL
, &startup
, &info
);
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
);
802 enum {from_event
, from_process_name
} mode
;
803 enum init_return (*backend
)(struct inner_data
*);
807 #define WINECON_CMD_SHOW_USAGE 0x10000
809 /******************************************************************
810 * WINECON_ParseOptions
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
;
825 while (*wci
->ptr
== ' ' || *wci
->ptr
== '\t') wci
->ptr
++;
826 if (wci
->ptr
[0] != '-') break;
827 if (strncmp(wci
->ptr
, "--use-event=", 12) == 0)
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
;
835 else if (strncmp(wci
->ptr
, "--backend=", 10) == 0)
837 if (strncmp(wci
->ptr
+ 10, "user", 4) == 0)
839 wci
->backend
= WCUSER_InitBackend
;
842 else if (strncmp(wci
->ptr
+ 10, "curses", 6) == 0)
844 wci
->backend
= WCCURSES_InitBackend
;
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
;
854 return IDS_CMD_INVALID_OPTION
|WINECON_CMD_SHOW_USAGE
;
857 if (wci
->mode
== from_event
)
860 while (*wci
->ptr
== ' ' || *wci
->ptr
== '\t') wci
->ptr
++;
861 if (*wci
->ptr
== 0) wci
->ptr
= "cmd";
866 /******************************************************************
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
;
881 if ((ret
= WINECON_ParseOptions(lpCmdLine
, &wci
)) != 0)
883 printf_res(ret
& 0xffff);
884 if (ret
& WINECON_CMD_SHOW_USAGE
)
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");
897 case from_process_name
:
902 len
= MultiByteToWideChar(CP_ACP
, 0, wci
.ptr
, -1, NULL
, 0);
904 buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
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
);
915 ret
= WINECON_Spawn(data
, buffer
);
916 HeapFree(GetProcessHeap(), 0, buffer
);
919 WINECON_Delete(data
);
920 printf_res(IDS_CMD_LAUNCH_FAILED
, wine_dbgstr_a(wci
.ptr
));
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
);
945 WINECON_Delete(data
);