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