Fixed typo in HKEY_CURRENT_CONFIG name.
[wine] / server / console.c
1 /*
2  * Server-side console management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
6  * FIXME: all this stuff is a hack to avoid breaking
7  *        the client-side console support.
8  */
9
10 #include "config.h"
11
12 #include <assert.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #ifdef HAVE_SYS_ERRNO_H
19 #include <sys/errno.h>
20 #endif
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #include "winnt.h"
28 #include "winbase.h"
29 #include "wincon.h"
30
31 #include "handle.h"
32 #include "process.h"
33 #include "thread.h"
34 #include "request.h"
35
36 struct screen_buffer;
37
38 struct console_input
39 {
40     struct object         obj;           /* object header */
41     int                   mode;          /* input mode */
42     struct screen_buffer *output;        /* associated screen buffer */
43     int                   recnum;        /* number of input records */
44     INPUT_RECORD         *records;       /* input records */
45 };
46
47 struct screen_buffer
48 {
49     struct object         obj;           /* object header */
50     int                   mode;          /* output mode */
51     struct console_input *input;         /* associated console input */
52     int                   cursor_size;   /* size of cursor (percentage filled) */
53     int                   cursor_visible;/* cursor visibility flag */
54     int                   pid;           /* xterm pid (hack) */
55     char                 *title;         /* console title */
56 };
57
58
59 static void console_input_dump( struct object *obj, int verbose );
60 static int console_input_get_poll_events( struct object *obj );
61 static int console_input_get_read_fd( struct object *obj );
62 static void console_input_destroy( struct object *obj );
63
64 static void screen_buffer_dump( struct object *obj, int verbose );
65 static int screen_buffer_get_poll_events( struct object *obj );
66 static int screen_buffer_get_write_fd( struct object *obj );
67 static void screen_buffer_destroy( struct object *obj );
68
69 /* common routine */
70 static int console_get_info( struct object *obj, struct get_file_info_request *req );
71
72 static const struct object_ops console_input_ops =
73 {
74     sizeof(struct console_input),     /* size */
75     console_input_dump,               /* dump */
76     default_poll_add_queue,           /* add_queue */
77     default_poll_remove_queue,        /* remove_queue */
78     default_poll_signaled,            /* signaled */
79     no_satisfied,                     /* satisfied */
80     console_input_get_poll_events,    /* get_poll_events */
81     default_poll_event,               /* poll_event */
82     console_input_get_read_fd,        /* get_read_fd */
83     no_write_fd,                      /* get_write_fd */
84     no_flush,                         /* flush */
85     console_get_info,                 /* get_file_info */
86     console_input_destroy             /* destroy */
87 };
88
89 static const struct object_ops screen_buffer_ops =
90 {
91     sizeof(struct screen_buffer),     /* size */
92     screen_buffer_dump,               /* dump */
93     default_poll_add_queue,           /* add_queue */
94     default_poll_remove_queue,        /* remove_queue */
95     default_poll_signaled,            /* signaled */
96     no_satisfied,                     /* satisfied */
97     screen_buffer_get_poll_events,    /* get_poll_events */
98     default_poll_event,               /* poll_event */
99     no_read_fd,                       /* get_read_fd */
100     screen_buffer_get_write_fd,       /* get_write_fd */
101     no_flush,                         /* flush */
102     console_get_info,                 /* get_file_info */
103     screen_buffer_destroy             /* destroy */
104 };
105
106
107 static struct object *create_console_input( int fd )
108 {
109     struct console_input *console_input;
110
111     if ((fd = (fd != -1) ? dup(fd) : dup(0)) == -1)
112     {
113         file_set_error();
114         return NULL;
115     }
116     if (!(console_input = alloc_object( &console_input_ops, fd ))) return NULL;
117     console_input->mode    = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
118                              ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
119     console_input->output  = NULL;
120     console_input->recnum  = 0;
121     console_input->records = NULL;
122     return &console_input->obj;
123 }
124
125 static struct object *create_console_output( int fd, struct object *input )
126 {
127     struct console_input *console_input = (struct console_input *)input;
128     struct screen_buffer *screen_buffer;
129
130     if ((fd = (fd != -1) ? dup(fd) : dup(1)) == -1)
131     {
132         file_set_error();
133         return NULL;
134     }
135     if (!(screen_buffer = alloc_object( &screen_buffer_ops, fd ))) return NULL;
136     screen_buffer->mode           = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
137     screen_buffer->input          = console_input;
138     screen_buffer->cursor_size    = 100;
139     screen_buffer->cursor_visible = 1;
140     screen_buffer->pid            = 0;
141     screen_buffer->title          = strdup( "Wine console" );
142     console_input->output = screen_buffer;
143     return &screen_buffer->obj;
144 }
145
146 /* allocate a console for this process */
147 int alloc_console( struct process *process )
148 {
149     if (process->console_in || process->console_out)
150     {
151         set_error( STATUS_ACCESS_DENIED );
152         return 0;
153     }
154     if ((process->console_in = create_console_input( -1 )))
155     {
156         if ((process->console_out = create_console_output( -1, process->console_in )))
157             return 1;
158         release_object( process->console_in );
159     }
160     return 0;
161 }
162
163 /* free the console for this process */
164 int free_console( struct process *process )
165 {
166     if (process->console_in) release_object( process->console_in );
167     if (process->console_out) release_object( process->console_out );
168     process->console_in = process->console_out = NULL;
169     return 1;
170 }
171
172 static int set_console_fd( int handle, int fd_in, int fd_out, int pid )
173 {
174     struct console_input *input;
175     struct screen_buffer *output;
176     struct object *obj;
177
178     if (!(obj = get_handle_obj( current->process, handle, 0, NULL )))
179         return 0;
180     if (obj->ops == &console_input_ops)
181     {
182         input = (struct console_input *)obj;
183         output = input->output;
184         grab_object( output );
185     }
186     else if (obj->ops == &screen_buffer_ops)
187     {
188         output = (struct screen_buffer *)obj;
189         input = output->input;
190         grab_object( input );
191     }
192     else
193     {
194         set_error( STATUS_OBJECT_TYPE_MISMATCH );
195         release_object( obj );
196         return 0;
197     }
198
199     /* can't change the fd if someone is waiting on it */
200     assert( !input->obj.head );
201     assert( !output->obj.head );
202
203     change_select_fd( &input->obj, fd_in );
204     change_select_fd( &output->obj, fd_out );
205     output->pid = pid;
206     release_object( input );
207     release_object( output );
208     return 1;
209 }
210
211 static int get_console_mode( int handle )
212 {
213     struct object *obj;
214     int ret = 0;
215
216     if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
217     {
218         if (obj->ops == &console_input_ops)
219             ret = ((struct console_input *)obj)->mode;
220         else if (obj->ops == &screen_buffer_ops)
221             ret = ((struct screen_buffer *)obj)->mode;
222         else
223             set_error( STATUS_OBJECT_TYPE_MISMATCH );
224         release_object( obj );
225     }
226     return ret;
227 }
228
229 static int set_console_mode( int handle, int mode )
230 {
231     struct object *obj;
232     int ret = 0;
233
234     if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
235         return 0;
236     if (obj->ops == &console_input_ops)
237     {
238         ((struct console_input *)obj)->mode = mode;
239         ret = 1;
240     }
241     else if (obj->ops == &screen_buffer_ops)
242     {
243         ((struct screen_buffer *)obj)->mode = mode;
244         ret = 1;
245     }
246     else set_error( STATUS_OBJECT_TYPE_MISMATCH );
247     release_object( obj );
248     return ret;
249 }
250
251 /* set misc console information (output handle only) */
252 static int set_console_info( int handle, struct set_console_info_request *req,
253                              const char *title, size_t len )
254 {
255     struct screen_buffer *console;
256     if (!(console = (struct screen_buffer *)get_handle_obj( current->process, handle,
257                                                             GENERIC_WRITE, &screen_buffer_ops )))
258         return 0;
259     if (req->mask & SET_CONSOLE_INFO_CURSOR)
260     {
261         console->cursor_size    = req->cursor_size;
262         console->cursor_visible = req->cursor_visible;
263     }
264     if (req->mask & SET_CONSOLE_INFO_TITLE)
265     {
266         char *new_title = mem_alloc( len + 1 );
267         if (new_title)
268         {
269             memcpy( new_title, title, len );
270             new_title[len] = 0;
271             if (console->title) free( console->title );
272             console->title = new_title;
273         }
274     }
275     release_object( console );
276     return 1;
277 }
278
279 /* add input events to a console input queue */
280 static int write_console_input( int handle, int count, INPUT_RECORD *records )
281 {
282     INPUT_RECORD *new_rec;
283     struct console_input *console;
284
285     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
286                                                             GENERIC_WRITE, &console_input_ops )))
287         return -1;
288     if (!(new_rec = realloc( console->records,
289                              (console->recnum + count) * sizeof(INPUT_RECORD) )))
290     {
291         set_error( STATUS_NO_MEMORY );
292         release_object( console );
293         return -1;
294     }
295     console->records = new_rec;
296     memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
297     console->recnum += count;
298     release_object( console );
299     return count;
300 }
301
302 /* retrieve a pointer to the console input records */
303 static int read_console_input( int handle, int count, INPUT_RECORD *rec, int max, int flush )
304 {
305     struct console_input *console;
306
307     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
308                                                             GENERIC_READ, &console_input_ops )))
309         return -1;
310     if ((count < 0) || (count > console->recnum)) count = console->recnum;
311     if (count > max) count = max;
312     memcpy( rec, console->records, count * sizeof(INPUT_RECORD) );
313     if (flush)
314     {
315         int i;
316         for (i = count; i < console->recnum; i++)
317             console->records[i-count] = console->records[i];
318         if ((console->recnum -= count) > 0)
319         {
320             INPUT_RECORD *new_rec = realloc( console->records,
321                                              console->recnum * sizeof(INPUT_RECORD) );
322             if (new_rec) console->records = new_rec;
323         }
324         else
325         {
326             free( console->records );
327             console->records = NULL;
328         }
329     }
330     release_object( console );
331     return count;
332 }
333
334 static void console_input_dump( struct object *obj, int verbose )
335 {
336     struct console_input *console = (struct console_input *)obj;
337     assert( obj->ops == &console_input_ops );
338     fprintf( stderr, "Console input fd=%d\n", console->obj.fd );
339 }
340
341 static int console_input_get_poll_events( struct object *obj )
342 {
343     return POLLIN;
344 }
345
346 static int console_input_get_read_fd( struct object *obj )
347 {
348     struct console_input *console = (struct console_input *)obj;
349     assert( obj->ops == &console_input_ops );
350     return dup( console->obj.fd );
351 }
352
353 static int console_get_info( struct object *obj, struct get_file_info_request *req )
354 {
355     req->type        = FILE_TYPE_CHAR;
356     req->attr        = 0;
357     req->access_time = 0;
358     req->write_time  = 0;
359     req->size_high   = 0;
360     req->size_low    = 0;
361     req->links       = 0;
362     req->index_high  = 0;
363     req->index_low   = 0;
364     req->serial      = 0;
365     return 1;
366 }
367
368 static void console_input_destroy( struct object *obj )
369 {
370     struct console_input *console = (struct console_input *)obj;
371     assert( obj->ops == &console_input_ops );
372     if (console->output) console->output->input = NULL;
373 }
374
375 static void screen_buffer_dump( struct object *obj, int verbose )
376 {
377     struct screen_buffer *console = (struct screen_buffer *)obj;
378     assert( obj->ops == &screen_buffer_ops );
379     fprintf( stderr, "Console screen buffer fd=%d\n", console->obj.fd );
380 }
381
382 static int screen_buffer_get_poll_events( struct object *obj )
383 {
384     return POLLOUT;
385 }
386
387 static int screen_buffer_get_write_fd( struct object *obj )
388 {
389     struct screen_buffer *console = (struct screen_buffer *)obj;
390     assert( obj->ops == &screen_buffer_ops );
391     return dup( console->obj.fd );
392 }
393
394 static void screen_buffer_destroy( struct object *obj )
395 {
396     struct screen_buffer *console = (struct screen_buffer *)obj;
397     assert( obj->ops == &screen_buffer_ops );
398     if (console->input) console->input->output = NULL;
399     if (console->title) free( console->title );
400 }
401
402 /* allocate a console for the current process */
403 DECL_HANDLER(alloc_console)
404 {
405     int in = -1, out = -1;
406
407     if (!alloc_console( current->process )) goto done;
408
409     if ((in = alloc_handle( current->process, current->process->console_in,
410                             req->access, req->inherit )) != -1)
411     {
412         if ((out = alloc_handle( current->process, current->process->console_out,
413                                  req->access, req->inherit )) != -1)
414             goto done;  /* everything is fine */
415         close_handle( current->process, in );
416         in = -1;
417     }
418     free_console( current->process );
419
420  done:
421     req->handle_in  = in;
422     req->handle_out = out;
423 }
424
425 /* free the console of the current process */
426 DECL_HANDLER(free_console)
427 {
428     free_console( current->process );
429 }
430
431 /* open a handle to the process console */
432 DECL_HANDLER(open_console)
433 {
434     struct object *obj= req->output ? current->process->console_out : current->process->console_in;
435
436     if (obj) req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
437     else set_error( STATUS_ACCESS_DENIED );
438 }
439
440 /* set info about a console (output only) */
441 DECL_HANDLER(set_console_info)
442 {
443     size_t len = get_req_strlen( req, req->title );
444     set_console_info( req->handle, req, req->title, len );
445 }
446
447 /* get info about a console (output only) */
448 DECL_HANDLER(get_console_info)
449 {
450     struct screen_buffer *console;
451     req->title[0] = 0;
452     if ((console = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
453                                                            GENERIC_READ, &screen_buffer_ops )))
454     {
455         req->cursor_size    = console->cursor_size;
456         req->cursor_visible = console->cursor_visible;
457         req->pid            = console->pid;
458         if (console->title) strcpy( req->title, console->title );
459         release_object( console );
460     }
461 }
462
463 /* set a console fd */
464 DECL_HANDLER(set_console_fd)
465 {
466     struct object *obj;
467     int fd_in, fd_out;
468
469     if (!(obj = get_handle_obj( current->process, req->file_handle,
470                                 GENERIC_READ | GENERIC_WRITE, NULL ))) return;
471     if ((fd_in = obj->ops->get_read_fd( obj )) == -1)
472     {
473         release_object( obj );
474         return;
475     }
476     fd_out = obj->ops->get_write_fd( obj );
477     release_object( obj );
478     if (fd_out != -1)
479     {
480         if (set_console_fd( req->handle, fd_in, fd_out, req->pid )) return;
481         close( fd_out );
482     }
483     close( fd_in );
484 }
485
486 /* get a console mode (input or output) */
487 DECL_HANDLER(get_console_mode)
488 {
489     req->mode = get_console_mode( req->handle );
490 }
491
492 /* set a console mode (input or output) */
493 DECL_HANDLER(set_console_mode)
494 {
495     set_console_mode( req->handle, req->mode );
496 }
497
498 /* add input records to a console input queue */
499 DECL_HANDLER(write_console_input)
500 {
501     int max = get_req_size( req, req + 1, sizeof(INPUT_RECORD) );
502     int count = req->count;
503
504     if (count > max) count = max;
505     req->written = write_console_input( req->handle, count, (INPUT_RECORD *)(req + 1) );
506 }
507
508 /* fetch input records from a console input queue */
509 DECL_HANDLER(read_console_input)
510 {
511     int max = get_req_size( req, req + 1, sizeof(INPUT_RECORD) );
512     req->read = read_console_input( req->handle, req->count, (INPUT_RECORD *)(req + 1),
513                                     max, req->flush );
514 }