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