- allow to save/restore some properties into the registry (like font,
[wine] / programs / wineconsole / wineconsole.c
1 /*
2  * an application for displaying Win32 console
3  *
4  * Copyright 2001 Eric Pouech
5  */
6
7 #include <stdio.h>
8 #include "wine/server.h"
9 #include "wine/unicode.h"
10 #include "winecon_private.h"
11
12 static int trace_level = 1;
13 void      XTracer(int level, const char* format, ...)
14 {
15     char        buf[1024];
16     va_list     valist;
17     int         len;
18
19     if (level > trace_level) return;
20
21     va_start(valist, format);
22     len = vsnprintf(buf, sizeof(buf), format, valist);
23     va_end(valist);
24  
25     if (len <= -1) 
26     {
27         len = sizeof(buf) - 1;
28         buf[len] = 0;
29         buf[len - 1] = buf[len - 2] = buf[len - 3] = '.';
30     }
31     fprintf(stderr, buf);
32 }
33
34 /******************************************************************
35  *              WINECON_FetchCells
36  *
37  * updates the local copy of cells (band to update)
38  */
39 void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
40 {
41     SERVER_START_REQ( read_console_output )
42     {
43         req->handle = (handle_t)data->hConOut;
44         req->x      = 0;
45         req->y      = upd_tp;
46         req->mode   = CHAR_INFO_MODE_TEXTATTR;
47         req->wrap   = TRUE;
48         wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
49                                (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
50         wine_server_call( req );
51     }
52     SERVER_END_REQ;
53     data->fnRefresh(data, upd_tp, upd_bm);
54 }
55
56 /******************************************************************
57  *              WINECON_NotifyWindowChange
58  *
59  * Inform server that visible window on sb has changed
60  */
61 void WINECON_NotifyWindowChange(struct inner_data* data)
62 {
63     SERVER_START_REQ( set_console_output_info )
64     {
65         req->handle       = (handle_t)data->hConOut;
66         req->win_left     = data->curcfg.win_pos.X;
67         req->win_top      = data->curcfg.win_pos.Y;
68         req->win_right    = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
69         req->win_bottom   = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
70         req->mask         = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
71         wine_server_call( req );
72     }
73     SERVER_END_REQ;
74 }
75
76 /******************************************************************
77  *              WINECON_GetHistorySize
78  *
79  *
80  */
81 int     WINECON_GetHistorySize(HANDLE hConIn)
82 {
83     int ret = 0;
84
85     SERVER_START_REQ(get_console_input_info)
86     {
87         req->handle = (handle_t)hConIn;
88         if (!wine_server_call_err( req )) ret = reply->history_size;
89     }
90     SERVER_END_REQ;
91     return ret;
92 }
93
94 /******************************************************************
95  *              WINECON_SetHistorySize
96  *
97  *
98  */
99 BOOL    WINECON_SetHistorySize(HANDLE hConIn, int size)
100 {
101     BOOL        ret;
102
103     SERVER_START_REQ(set_console_input_info)
104     {
105         req->handle = (handle_t)hConIn;
106         req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
107         req->history_size = size;
108         ret = !wine_server_call_err( req );
109     }
110     SERVER_END_REQ;
111     return ret;
112 }
113
114
115 /******************************************************************
116  *              WINECON_GetHistoryMode
117  *
118  *
119  */
120 int     WINECON_GetHistoryMode(HANDLE hConIn)
121 {
122     int ret = 0;
123
124     SERVER_START_REQ(get_console_input_info)
125     {
126         req->handle = (handle_t)hConIn;
127         if (!wine_server_call_err( req )) ret = reply->history_mode;
128     }
129     SERVER_END_REQ;
130     return ret;
131 }
132
133 /******************************************************************
134  *              WINECON_SetHistoryMode
135  *
136  *
137  */
138 BOOL    WINECON_SetHistoryMode(HANDLE hConIn, int mode)
139 {
140     BOOL        ret;
141
142     SERVER_START_REQ(set_console_input_info)
143     {
144         req->handle = (handle_t)hConIn;
145         req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
146         req->history_mode = mode;
147         ret = !wine_server_call_err( req );
148     }
149     SERVER_END_REQ;
150     return ret;
151 }
152
153 /******************************************************************
154  *              WINECON_GetConsoleTitle
155  *
156  *
157  */
158 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
159 {
160     BOOL ret;
161
162     if (len < sizeof(WCHAR)) return FALSE;
163
164     SERVER_START_REQ( get_console_input_info )
165     {
166         req->handle = (handle_t)hConIn;
167         wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
168         if ((ret = !wine_server_call_err( req )))
169         {
170             len = wine_server_reply_size( reply );
171             buffer[len / sizeof(WCHAR)] = 0;
172         }
173     }
174     SERVER_END_REQ;
175     return ret;
176 }
177
178 /******************************************************************
179  *              WINECON_GrabChanges
180  *
181  * A change occurs, try to figure out which
182  */
183 int     WINECON_GrabChanges(struct inner_data* data)
184 {
185     struct console_renderer_event       evts[16];
186     int i, num;
187     HANDLE h;
188
189     SERVER_START_REQ( get_console_renderer_events )
190     {
191         wine_server_set_reply( req, evts, sizeof(evts) );
192         req->handle = (handle_t)data->hSynchro;
193         if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
194         else num = 0;
195     }
196     SERVER_END_REQ;
197     if (!num) {Trace(0, "hmm renderer signaled but no events available\n"); return 1;}
198     
199     /* FIXME: should do some event compression here (cursor pos, update) */
200     Trace(1, "Change notification:");
201     for (i = 0; i < num; i++)
202     {
203         switch (evts[i].event)
204         {
205         case CONSOLE_RENDERER_TITLE_EVENT:
206             data->fnSetTitle(data);
207             break;
208         case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
209             SERVER_START_REQ( open_console )
210             {
211                 req->from    = (int)data->hConIn;
212                 req->access  = GENERIC_READ | GENERIC_WRITE;
213                 req->share   = FILE_SHARE_READ | FILE_SHARE_WRITE;
214                 req->inherit = FALSE;
215                 h = wine_server_call_err( req ) ? 0 : (HANDLE)reply->handle;
216             }
217             SERVER_END_REQ;
218             Trace(1, " active(%d)", (int)h);
219             if (h)
220             {
221                 CloseHandle(data->hConOut);
222                 data->hConOut = h;
223             }
224             break;
225         case CONSOLE_RENDERER_SB_RESIZE_EVENT:
226             if (data->curcfg.sb_width != evts[i].u.resize.width || 
227                 data->curcfg.sb_height != evts[i].u.resize.height)
228             {
229                 Trace(1, " resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
230                 data->curcfg.sb_width  = evts[i].u.resize.width;
231                 data->curcfg.sb_height = evts[i].u.resize.height;
232                 
233                 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
234                                           data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
235                 if (!data->cells) {Trace(0, "OOM\n"); exit(0);}
236                 data->fnResizeScreenBuffer(data);
237                 data->fnComputePositions(data);
238             }
239             break;
240         case CONSOLE_RENDERER_UPDATE_EVENT:
241             Trace(1, " update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
242             WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
243             break;
244         case CONSOLE_RENDERER_CURSOR_POS_EVENT:
245             if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
246             {   
247                 data->cursor.X = evts[i].u.cursor_pos.x;
248                 data->cursor.Y = evts[i].u.cursor_pos.y;
249                 data->fnPosCursor(data);
250                 Trace(1, " curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
251             }
252             break;
253         case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
254             if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size || 
255                 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
256             {
257                 data->fnShapeCursor(data, evts[i].u.cursor_geom.size, 
258                                     evts[i].u.cursor_geom.visible, FALSE);
259                 Trace(1, " curs-geom(%d,%d)", 
260                       evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
261             }
262             break;
263         case CONSOLE_RENDERER_DISPLAY_EVENT:
264             if (evts[i].u.display.left != data->curcfg.win_pos.X)
265             {
266                 data->fnScroll(data, evts[i].u.display.left, TRUE);
267                 data->fnPosCursor(data);
268                 Trace(1, " h-scroll(%d)", evts[i].u.display.left);
269             }
270             if (evts[i].u.display.top != data->curcfg.win_pos.Y)
271             {
272                 data->fnScroll(data, evts[i].u.display.top, FALSE);
273                 data->fnPosCursor(data);
274                 Trace(1, " v-scroll(%d)", evts[i].u.display.top);
275             }
276             if (evts[i].u.display.width != data->curcfg.win_width || 
277                 evts[i].u.display.height != data->curcfg.win_height)
278             {
279                 Trace(1, " win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
280                 data->curcfg.win_width = evts[i].u.display.width;
281                 data->curcfg.win_height = evts[i].u.display.height;
282                 data->fnComputePositions(data);
283             }
284             break;
285         case CONSOLE_RENDERER_EXIT_EVENT:
286             Trace(1, ". Exit!!\n");
287             return 0;
288         default:
289             Trace(0, "Unknown event type (%d)\n", evts[i].event);
290         }
291     }
292
293     Trace(1, ". Done\n");
294     return 1;
295 }
296
297 /******************************************************************
298  *              WINECON_Delete
299  *
300  * Destroy wineconsole internal data
301  */
302 static void WINECON_Delete(struct inner_data* data)
303 {
304     if (!data) return;
305
306     if (data->hConIn)           CloseHandle(data->hConIn);
307     if (data->hConOut)          CloseHandle(data->hConOut);
308     if (data->hSynchro)         CloseHandle(data->hSynchro);
309     if (data->cells)            HeapFree(GetProcessHeap(), 0, data->cells);
310     data->fnDeleteBackend(data);
311     HeapFree(GetProcessHeap(), 0, data);
312 }
313
314 /******************************************************************
315  *              WINECON_Init
316  *
317  * Initialisation part I. Creation of server object (console input and
318  * active screen buffer)
319  */
320 static struct inner_data* WINECON_Init(HINSTANCE hInst, void* pid)
321 {
322     struct inner_data*  data = NULL;
323     DWORD               ret;
324     WCHAR               szTitle[] = {'W','i','n','e',' ','c','o','n','s','o','l','e',0};
325
326     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
327     if (!data) return 0;
328
329     /* load default registry settings, and copy them into our current configuration */
330     WINECON_RegLoad(&data->defcfg);
331     data->curcfg = data->defcfg;
332
333     /* the handles here are created without the whistles and bells required by console
334      * (mainly because wineconsole doesn't need it)
335      * - there are not inheritable
336      * - hConIn is not synchronizable
337      */
338     SERVER_START_REQ(alloc_console)
339     {
340         req->access  = GENERIC_READ | GENERIC_WRITE;
341         req->inherit = FALSE;
342         req->pid     = pid;
343         ret = !wine_server_call_err( req );
344         data->hConIn = (HANDLE)reply->handle_in;
345         data->hSynchro = (HANDLE)reply->event;
346     }
347     SERVER_END_REQ;
348     if (!ret) goto error;
349
350     SERVER_START_REQ( set_console_input_info )
351     {
352         req->handle = (handle_t)data->hConIn;
353         req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
354         wine_server_add_data( req, szTitle, strlenW(szTitle) * sizeof(WCHAR) );
355         ret = !wine_server_call_err( req );
356     }
357     SERVER_END_REQ;
358     if (!ret) goto error;
359
360     SERVER_START_REQ(create_console_output)
361     {
362         req->handle_in = (handle_t)data->hConIn;
363         req->access    = GENERIC_WRITE|GENERIC_READ;
364         req->share     = FILE_SHARE_READ|FILE_SHARE_WRITE;
365         req->inherit   = FALSE;
366         data->hConOut  = (HANDLE)(wine_server_call_err( req ) ? 0 : reply->handle_out);
367     }
368     SERVER_END_REQ;
369     if (data->hConOut) return data;
370
371  error:
372     WINECON_Delete(data);
373     return NULL;
374 }
375
376 /******************************************************************
377  *              WINECON_Spawn
378  *
379  * Spawn the child processus when invoked with wineconsole foo bar
380  */
381 static BOOL WINECON_Spawn(struct inner_data* data, LPCSTR lpCmdLine)
382 {
383     PROCESS_INFORMATION info;
384     STARTUPINFO         startup;
385     LPWSTR              ptr = GetCommandLine(); /* we're unicode... */
386     BOOL                done;
387
388     /* we're in the case wineconsole <exe> <options>... spawn the new process */
389     memset(&startup, 0, sizeof(startup));
390     startup.cb          = sizeof(startup);
391     startup.dwFlags     = STARTF_USESTDHANDLES;
392
393     /* the attributes of wineconsole's handles are not adequate for inheritance, so
394      * get them with the correct attributes before process creation
395      */
396     if (!DuplicateHandle(GetCurrentProcess(), data->hConIn,  GetCurrentProcess(),
397                          &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
398         !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(), 
399                          &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
400         !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(), 
401                              &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
402     {
403         Trace(0, "can't dup handles\n");
404         /* no need to delete handles, we're exiting the programm anyway */
405         return FALSE;
406     }
407
408     /* we could have several ' ' in process command line... so try first space...
409      * FIXME:
410      * the correct way would be to check the existence of the left part of ptr
411      * (to be a file)
412      */
413     while (*ptr && *ptr++ != ' ');
414
415     done = *ptr && CreateProcess(NULL, ptr, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
416     
417     /* we no longer need the handles passed to the child for the console */
418     CloseHandle(startup.hStdInput);
419     CloseHandle(startup.hStdOutput);
420     CloseHandle(startup.hStdError);
421
422     return done;
423 }
424
425 /******************************************************************
426  *               WINECON_HasEvent
427  *
428  *
429  */
430 static BOOL WINECON_HasEvent(LPCSTR ptr, unsigned *evt)
431 {
432     while (*ptr == ' ' || *ptr == '\t') ptr++;
433     if (strncmp(ptr, "--use-event=", 12)) return FALSE;
434     return sscanf(ptr + 12, "%d", evt) == 1;
435 }
436
437 /******************************************************************
438  *              WINECON_WinMain
439  *
440  * wineconsole can either be started as:
441  *      wineconsole --use-event=<int>   used when a new console is created (AllocConsole)
442  *      wineconsole <pgm> <arguments>   used to start the program <pgm> from the command line in
443  *                                      a freshly created console
444  */
445 int PASCAL WINECON_WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPCSTR lpCmdLine, UINT nCmdShow)
446 {
447     struct inner_data*  data;
448     int                 ret = 1;
449     unsigned            evt;
450
451     /* case of wineconsole <evt>, signal process that created us that we're up and running */
452     if (WINECON_HasEvent(lpCmdLine, &evt))
453     {
454         if (!(data = WINECON_Init(hInst, 0))) return 0;
455         ret = SetEvent((HANDLE)evt);
456     }
457     else
458     {
459         if (!(data = WINECON_Init(hInst, (void*)GetCurrentProcessId()))) return 0;
460         ret = WINECON_Spawn(data, lpCmdLine);
461     }
462
463     if (ret && WCUSER_InitBackend(data))
464     {
465         ret = data->fnMainLoop(data);
466     }
467     WINECON_Delete(data);
468
469     return ret;
470 }