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