server: Add get_sd and set_sd object operations to allow the security descriptor...
[wine] / server / console.c
1 /*
2  * Server-side console management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *               2001 Eric Pouech
6  *
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
31
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "handle.h"
35 #include "process.h"
36 #include "request.h"
37 #include "unicode.h"
38 #include "wincon.h"
39 #include "winternl.h"
40
41 /* specific access rights (FIXME: should use finer-grained access rights) */
42 #define CONSOLE_READ   0x01
43 #define CONSOLE_WRITE  0x02
44
45 struct screen_buffer;
46 struct console_input_events;
47
48 struct console_input
49 {
50     struct object                obj;           /* object header */
51     int                          num_proc;      /* number of processes attached to this console */
52     struct thread               *renderer;      /* console renderer thread */
53     int                          mode;          /* input mode */
54     struct screen_buffer        *active;        /* active screen buffer */
55     int                          recnum;        /* number of input records */
56     INPUT_RECORD                *records;       /* input records */
57     struct console_input_events *evt;           /* synchronization event with renderer */
58     WCHAR                       *title;         /* console title */
59     WCHAR                      **history;       /* lines history */
60     int                          history_size;  /* number of entries in history array */
61     int                          history_index; /* number of used entries in history array */
62     int                          history_mode;  /* mode of history (non zero means remove doubled strings */
63     int                          edition_mode;  /* index to edition mode flavors */
64     int                          input_cp;      /* console input codepage */
65     int                          output_cp;     /* console output codepage */
66     user_handle_t                win;           /* window handle if backend supports it */
67     struct event                *event;         /* event to wait on for input queue */
68 };
69
70 static unsigned int console_map_access( struct object *obj, unsigned int access );
71
72 static void console_input_dump( struct object *obj, int verbose );
73 static void console_input_destroy( struct object *obj );
74
75 static const struct object_ops console_input_ops =
76 {
77     sizeof(struct console_input),     /* size */
78     console_input_dump,               /* dump */
79     no_add_queue,                     /* add_queue */
80     NULL,                             /* remove_queue */
81     NULL,                             /* signaled */
82     no_satisfied,                     /* satisfied */
83     no_signal,                        /* signal */
84     no_get_fd,                        /* get_fd */
85     console_map_access,               /* map_access */
86     default_get_sd,                   /* get_sd */
87     default_set_sd,                   /* set_sd */
88     no_lookup_name,                   /* lookup_name */
89     no_open_file,                     /* open_file */
90     no_close_handle,                  /* close_handle */
91     console_input_destroy             /* destroy */
92 };
93
94 static void console_input_events_dump( struct object *obj, int verbose );
95 static void console_input_events_destroy( struct object *obj );
96 static int  console_input_events_signaled( struct object *obj, struct thread *thread );
97
98 struct console_input_events
99 {
100     struct object         obj;         /* object header */
101     int                   num_alloc;   /* number of allocated events */
102     int                   num_used;    /* number of actually used events */
103     struct console_renderer_event*      events;
104 };
105
106 static const struct object_ops console_input_events_ops =
107 {
108     sizeof(struct console_input_events), /* size */
109     console_input_events_dump,        /* dump */
110     add_queue,                        /* add_queue */
111     remove_queue,                     /* remove_queue */
112     console_input_events_signaled,    /* signaled */
113     no_satisfied,                     /* satisfied */
114     no_signal,                        /* signal */
115     no_get_fd,                        /* get_fd */
116     console_map_access,               /* map_access */
117     default_get_sd,                   /* get_sd */
118     default_set_sd,                   /* set_sd */
119     no_lookup_name,                   /* lookup_name */
120     no_open_file,                     /* open_file */
121     no_close_handle,                  /* close_handle */
122     console_input_events_destroy      /* destroy */
123 };
124
125 struct screen_buffer
126 {
127     struct object         obj;           /* object header */
128     struct list           entry;         /* entry in list of all screen buffers */
129     struct console_input *input;         /* associated console input */
130     int                   mode;          /* output mode */
131     int                   cursor_size;   /* size of cursor (percentage filled) */
132     int                   cursor_visible;/* cursor visibility flag */
133     int                   cursor_x;      /* position of cursor */
134     int                   cursor_y;      /* position of cursor */
135     int                   width;         /* size (w-h) of the screen buffer */
136     int                   height;
137     int                   max_width;     /* size (w-h) of the window given font size */
138     int                   max_height;
139     char_info_t          *data;          /* the data for each cell - a width x height matrix */
140     unsigned short        attr;          /* default attribute for screen buffer */
141     rectangle_t           win;           /* current visible window on the screen buffer *
142                                           * as seen in wineconsole */
143 };
144
145 static void screen_buffer_dump( struct object *obj, int verbose );
146 static void screen_buffer_destroy( struct object *obj );
147
148 static const struct object_ops screen_buffer_ops =
149 {
150     sizeof(struct screen_buffer),     /* size */
151     screen_buffer_dump,               /* dump */
152     no_add_queue,                     /* add_queue */
153     NULL,                             /* remove_queue */
154     NULL,                             /* signaled */
155     NULL,                             /* satisfied */
156     no_signal,                        /* signal */
157     no_get_fd,                        /* get_fd */
158     console_map_access,               /* map_access */
159     default_get_sd,                   /* get_sd */
160     default_set_sd,                   /* set_sd */
161     no_lookup_name,                   /* lookup_name */
162     no_open_file,                     /* open_file */
163     no_close_handle,                  /* close_handle */
164     screen_buffer_destroy             /* destroy */
165 };
166
167 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
168
169 static const char_info_t empty_char_info = { ' ', 0x000f };  /* white on black space */
170
171 /* access mapping for all console objects */
172 static unsigned int console_map_access( struct object *obj, unsigned int access )
173 {
174     if (access & GENERIC_READ)    access |= SYNCHRONIZE | STANDARD_RIGHTS_READ | CONSOLE_READ;
175     if (access & GENERIC_WRITE)   access |= SYNCHRONIZE | STANDARD_RIGHTS_WRITE | CONSOLE_WRITE;
176     if (access & GENERIC_EXECUTE) access |= SYNCHRONIZE | STANDARD_RIGHTS_EXECUTE;
177     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL | CONSOLE_READ | CONSOLE_WRITE;
178     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
179 }
180
181 /* dumps the renderer events of a console */
182 static void console_input_events_dump( struct object *obj, int verbose )
183 {
184     struct console_input_events *evts = (struct console_input_events *)obj;
185     assert( obj->ops == &console_input_events_ops );
186     fprintf( stderr, "Console input events: %d/%d events\n",
187              evts->num_used, evts->num_alloc );
188 }
189
190 /* destroys the renderer events of a console */
191 static void console_input_events_destroy( struct object *obj )
192 {
193     struct console_input_events *evts = (struct console_input_events *)obj;
194     assert( obj->ops == &console_input_events_ops );
195     free( evts->events );
196 }
197
198 /* the renderer events list is signaled when it's not empty */
199 static int console_input_events_signaled( struct object *obj, struct thread *thread )
200 {
201     struct console_input_events *evts = (struct console_input_events *)obj;
202     assert( obj->ops == &console_input_events_ops );
203     return (evts->num_used != 0);
204 }
205
206 /* add an event to the console's renderer events list */
207 static void console_input_events_append( struct console_input_events* evts,
208                                          struct console_renderer_event* evt)
209 {
210     int collapsed = FALSE;
211
212     /* to be done even when evt has been generated by the rendere ? */
213
214     /* try to collapse evt into current queue's events */
215     if (evts->num_used)
216     {
217         struct console_renderer_event* last = &evts->events[evts->num_used - 1];
218
219         if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
220             evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
221         {
222             /* if two update events overlap, collapse them into a single one */
223             if (last->u.update.bottom + 1 >= evt->u.update.top &&
224                 evt->u.update.bottom + 1 >= last->u.update.top)
225             {
226                 last->u.update.top = min(last->u.update.top, evt->u.update.top);
227                 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
228                 collapsed = TRUE;
229             }
230         }
231     }
232     if (!collapsed)
233     {
234         if (evts->num_used == evts->num_alloc)
235         {
236             evts->num_alloc += 16;
237             evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
238             assert(evts->events);
239         }
240         evts->events[evts->num_used++] = *evt;
241     }
242     wake_up( &evts->obj, 0 );
243 }
244
245 /* retrieves events from the console's renderer events list */
246 static void console_input_events_get( struct console_input_events* evts )
247 {
248     data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
249
250     if (num > evts->num_used) num = evts->num_used;
251     set_reply_data( evts->events, num * sizeof(evts->events[0]) );
252     if (num < evts->num_used)
253     {
254         memmove( &evts->events[0], &evts->events[num],
255                  (evts->num_used - num) * sizeof(evts->events[0]) );
256     }
257     evts->num_used -= num;
258 }
259
260 static struct console_input_events *create_console_input_events(void)
261 {
262     struct console_input_events*        evt;
263
264     if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
265     evt->num_alloc = evt->num_used = 0;
266     evt->events = NULL;
267     return evt;
268 }
269
270 static struct object *create_console_input( struct thread* renderer )
271 {
272     struct console_input *console_input;
273
274     if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
275     console_input->renderer      = renderer;
276     console_input->mode          = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
277                                    ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
278     console_input->num_proc      = 0;
279     console_input->active        = NULL;
280     console_input->recnum        = 0;
281     console_input->records       = NULL;
282     console_input->evt           = create_console_input_events();
283     console_input->title         = NULL;
284     console_input->history_size  = 50;
285     console_input->history       = calloc( console_input->history_size, sizeof(WCHAR*) );
286     console_input->history_index = 0;
287     console_input->history_mode  = 0;
288     console_input->edition_mode  = 0;
289     console_input->input_cp      = 0;
290     console_input->output_cp     = 0;
291     console_input->win           = 0;
292     console_input->event         = create_event( NULL, NULL, 0, 1, 0 );
293
294     if (!console_input->history || !console_input->evt)
295     {
296         release_object( console_input );
297         return NULL;
298     }
299     return &console_input->obj;
300 }
301
302 static struct screen_buffer *create_console_output( struct console_input *console_input )
303 {
304     struct screen_buffer *screen_buffer;
305     int i;
306
307     if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
308     screen_buffer->mode           = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
309     screen_buffer->input          = console_input;
310     screen_buffer->cursor_size    = 100;
311     screen_buffer->cursor_visible = 1;
312     screen_buffer->width          = 80;
313     screen_buffer->height         = 150;
314     screen_buffer->max_width      = 80;
315     screen_buffer->max_height     = 25;
316     screen_buffer->cursor_x       = 0;
317     screen_buffer->cursor_y       = 0;
318     screen_buffer->attr           = 0x0F;
319     screen_buffer->win.left       = 0;
320     screen_buffer->win.right      = screen_buffer->max_width - 1;
321     screen_buffer->win.top        = 0;
322     screen_buffer->win.bottom     = screen_buffer->max_height - 1;
323
324     list_add_head( &screen_buffer_list, &screen_buffer->entry );
325
326     if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
327                                         sizeof(*screen_buffer->data) )))
328     {
329         release_object( screen_buffer );
330         return NULL;
331     }
332     /* clear the first row */
333     for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
334     /* and copy it to all other rows */
335     for (i = 1; i < screen_buffer->height; i++)
336         memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
337                 screen_buffer->width * sizeof(char_info_t) );
338
339     if (!console_input->active)
340     {
341         struct console_renderer_event evt;
342         console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
343
344         /* generate the initial events */
345         evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
346         memset(&evt.u, 0, sizeof(evt.u));
347         console_input_events_append( console_input->evt, &evt );
348
349         evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
350         evt.u.resize.width  = screen_buffer->width;
351         evt.u.resize.height = screen_buffer->height;
352         console_input_events_append( console_input->evt, &evt );
353
354         evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
355         evt.u.display.left   = screen_buffer->win.left;
356         evt.u.display.top    = screen_buffer->win.top;
357         evt.u.display.width  = screen_buffer->win.right - screen_buffer->win.left + 1;
358         evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
359         console_input_events_append( console_input->evt, &evt );
360
361         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
362         evt.u.update.top    = 0;
363         evt.u.update.bottom = screen_buffer->height - 1;
364         console_input_events_append( console_input->evt, &evt );
365
366         evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
367         evt.u.cursor_geom.size    = screen_buffer->cursor_size;
368         evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
369         console_input_events_append( console_input->evt, &evt );
370
371         evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
372         evt.u.cursor_pos.x = screen_buffer->cursor_x;
373         evt.u.cursor_pos.y = screen_buffer->cursor_y;
374         console_input_events_append( console_input->evt, &evt );
375     }
376     return screen_buffer;
377 }
378
379 /* free the console for this process */
380 int free_console( struct process *process )
381 {
382     struct console_input* console = process->console;
383
384     if (!console || !console->renderer) return 0;
385
386     process->console = NULL;
387     if (--console->num_proc == 0)
388     {
389         /* all processes have terminated... tell the renderer to terminate too */
390         struct console_renderer_event evt;
391         evt.event = CONSOLE_RENDERER_EXIT_EVENT;
392         memset(&evt.u, 0, sizeof(evt.u));
393         console_input_events_append( console->evt, &evt );
394     }
395     release_object( console );
396
397     return 1;
398 }
399
400 /* let process inherit the console from parent... this handle two cases :
401  *      1/ generic console inheritance
402  *      2/ parent is a renderer which launches process, and process should attach to the console
403  *         renderered by parent
404  */
405 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
406 {
407     int done = 0;
408     struct process* parent = parent_thread->process;
409
410     /* if parent is a renderer, then attach current process to its console
411      * a bit hacky....
412      */
413     if (hconin)
414     {
415         struct console_input* console;
416
417         /* FIXME: should we check some access rights ? */
418         if ((console = (struct console_input*)get_handle_obj( parent, hconin,
419                                                               0, &console_input_ops )))
420         {
421             if (console->renderer == parent_thread)
422             {
423                 process->console = (struct console_input*)grab_object( console );
424                 process->console->num_proc++;
425                 done = 1;
426             }
427             release_object( console );
428         }
429         else clear_error();  /* ignore error */
430     }
431     /* otherwise, if parent has a console, attach child to this console */
432     if (!done && parent->console)
433     {
434         assert(parent->console->renderer);
435         process->console = (struct console_input*)grab_object( parent->console );
436         process->console->num_proc++;
437     }
438 }
439
440 struct thread *console_get_renderer( struct console_input *console )
441 {
442     return console->renderer;
443 }
444
445 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
446 {
447     struct console_input*       console = NULL;
448
449     if (handle)
450         console = (struct console_input *)get_handle_obj( current->process, handle,
451                                                           access, &console_input_ops );
452     else if (current->process->console)
453     {
454         assert( current->process->console->renderer );
455         console = (struct console_input *)grab_object( current->process->console );
456     }
457
458     if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
459     return console;
460 }
461
462 struct console_signal_info
463 {
464     struct console_input        *console;
465     process_id_t                 group;
466     int                          signal;
467 };
468
469 static int propagate_console_signal_cb(struct process *process, void *user)
470 {
471     struct console_signal_info* csi = (struct console_signal_info*)user;
472
473     if (process->console == csi->console && process->running_threads &&
474         (!csi->group || process->group_id == csi->group))
475     {
476         /* find a suitable thread to signal */
477         struct thread *thread;
478         LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
479         {
480             if (send_thread_signal( thread, csi->signal )) break;
481         }
482     }
483     return FALSE;
484 }
485
486 static void propagate_console_signal( struct console_input *console,
487                                       int sig, process_id_t group_id )
488 {
489     struct console_signal_info csi;
490
491     if (!console)
492     {
493         set_error( STATUS_INVALID_PARAMETER );
494         return;
495     }
496     /* FIXME: should support the other events (like CTRL_BREAK) */
497     if (sig != CTRL_C_EVENT)
498     {
499         set_error( STATUS_NOT_IMPLEMENTED );
500         return;
501     }
502     csi.console = console;
503     csi.signal  = SIGINT;
504     csi.group   = group_id;
505
506     enum_processes(propagate_console_signal_cb, &csi);
507 }
508
509 static int get_console_mode( obj_handle_t handle )
510 {
511     struct object *obj;
512     int ret = 0;
513
514     if ((obj = get_handle_obj( current->process, handle, CONSOLE_READ, NULL )))
515     {
516         if (obj->ops == &console_input_ops)
517             ret = ((struct console_input *)obj)->mode;
518         else if (obj->ops == &screen_buffer_ops)
519             ret = ((struct screen_buffer *)obj)->mode;
520         else
521             set_error( STATUS_OBJECT_TYPE_MISMATCH );
522         release_object( obj );
523     }
524     return ret;
525 }
526
527 /* changes the mode of either a console input or a screen buffer */
528 static int set_console_mode( obj_handle_t handle, int mode )
529 {
530     struct object *obj;
531     int ret = 0;
532
533     if (!(obj = get_handle_obj( current->process, handle, CONSOLE_WRITE, NULL )))
534         return 0;
535     if (obj->ops == &console_input_ops)
536     {
537         /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
538         ((struct console_input *)obj)->mode = mode;
539         ret = 1;
540     }
541     else if (obj->ops == &screen_buffer_ops)
542     {
543         ((struct screen_buffer *)obj)->mode = mode;
544         ret = 1;
545     }
546     else set_error( STATUS_OBJECT_TYPE_MISMATCH );
547     release_object( obj );
548     return ret;
549 }
550
551 /* add input events to a console input queue */
552 static int write_console_input( struct console_input* console, int count,
553                                 const INPUT_RECORD *records )
554 {
555     INPUT_RECORD *new_rec;
556
557     if (!count) return 0;
558     if (!(new_rec = realloc( console->records,
559                              (console->recnum + count) * sizeof(INPUT_RECORD) )))
560     {
561         set_error( STATUS_NO_MEMORY );
562         release_object( console );
563         return -1;
564     }
565     console->records = new_rec;
566     memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
567
568     if (console->mode & ENABLE_PROCESSED_INPUT)
569     {
570         int i = 0;
571         while (i < count)
572         {
573             if (records[i].EventType == KEY_EVENT &&
574                 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
575                 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
576             {
577                 if (i != count - 1)
578                     memcpy( &console->records[console->recnum + i],
579                             &console->records[console->recnum + i + 1],
580                             (count - i - 1) * sizeof(INPUT_RECORD) );
581                 count--;
582                 if (records[i].Event.KeyEvent.bKeyDown)
583                 {
584                     /* send SIGINT to all processes attached to this console */
585                     propagate_console_signal( console, CTRL_C_EVENT, 0 );
586                 }
587             }
588             else i++;
589         }
590     }
591     if (!console->recnum && count) set_event( console->event );
592     console->recnum += count;
593     return count;
594 }
595
596 /* retrieve a pointer to the console input records */
597 static int read_console_input( obj_handle_t handle, int count, int flush )
598 {
599     struct console_input *console;
600
601     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
602                                                             CONSOLE_READ, &console_input_ops )))
603         return -1;
604
605     if (!count)
606     {
607         /* special case: do not retrieve anything, but return
608          * the total number of records available */
609         count = console->recnum;
610     }
611     else
612     {
613         if (count > console->recnum) count = console->recnum;
614         set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
615     }
616     if (flush)
617     {
618         int i;
619         for (i = count; i < console->recnum; i++)
620             console->records[i-count] = console->records[i];
621         if ((console->recnum -= count) > 0)
622         {
623             INPUT_RECORD *new_rec = realloc( console->records,
624                                              console->recnum * sizeof(INPUT_RECORD) );
625             if (new_rec) console->records = new_rec;
626         }
627         else
628         {
629             free( console->records );
630             console->records = NULL;
631             reset_event( console->event );
632         }
633     }
634     release_object( console );
635     return count;
636 }
637
638 /* set misc console input information */
639 static int set_console_input_info( const struct set_console_input_info_request *req,
640                                    const WCHAR *title, data_size_t len )
641 {
642     struct console_input *console;
643     struct console_renderer_event evt;
644
645     if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) goto error;
646
647     memset(&evt.u, 0, sizeof(evt.u));
648     if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
649     {
650         struct screen_buffer *screen_buffer;
651
652         screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
653                                                                 CONSOLE_WRITE, &screen_buffer_ops );
654         if (!screen_buffer || screen_buffer->input != console)
655         {
656             set_error( STATUS_INVALID_HANDLE );
657             if (screen_buffer) release_object( screen_buffer );
658             goto error;
659         }
660
661         if (screen_buffer != console->active)
662         {
663             if (console->active) release_object( console->active );
664             console->active = screen_buffer;
665             evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
666             console_input_events_append( console->evt, &evt );
667         }
668         else
669             release_object( screen_buffer );
670     }
671     if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
672     {
673         WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
674         if (new_title)
675         {
676             memcpy( new_title, title, len );
677             new_title[len / sizeof(WCHAR)] = 0;
678             free( console->title );
679             console->title = new_title;
680             evt.event = CONSOLE_RENDERER_TITLE_EVENT;
681             console_input_events_append( console->evt, &evt );
682         }
683     }
684     if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
685     {
686         console->history_mode = req->history_mode;
687     }
688     if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
689         console->history_size != req->history_size)
690     {
691         WCHAR** mem = NULL;
692         int     i;
693         int     delta;
694
695         if (req->history_size)
696         {
697             mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
698             if (!mem) goto error;
699             memset( mem, 0, req->history_size * sizeof(WCHAR*) );
700         }
701
702         delta = (console->history_index > req->history_size) ?
703             (console->history_index - req->history_size) : 0;
704
705         for (i = delta; i < console->history_index; i++)
706         {
707             mem[i - delta] = console->history[i];
708             console->history[i] = NULL;
709         }
710         console->history_index -= delta;
711
712         for (i = 0; i < console->history_size; i++)
713             if (console->history[i]) free( console->history[i] );
714         free( console->history );
715         console->history = mem;
716         console->history_size = req->history_size;
717     }
718     if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
719     {
720         console->edition_mode = req->edition_mode;
721     }
722     if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
723     {
724         console->input_cp = req->input_cp;
725     }
726     if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
727     {
728         console->output_cp = req->output_cp;
729     }
730     if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
731     {
732         console->win = req->win;
733     }
734     release_object( console );
735     return 1;
736  error:
737     if (console) release_object( console );
738     return 0;
739 }
740
741 /* resize a screen buffer */
742 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
743                                       int new_width, int new_height )
744 {
745     int i, old_width, old_height, copy_width, copy_height;
746     char_info_t *new_data;
747
748     if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
749     {
750         set_error( STATUS_NO_MEMORY );
751         return 0;
752     }
753     old_width = screen_buffer->width;
754     old_height = screen_buffer->height;
755     copy_width = min( old_width, new_width );
756     copy_height = min( old_height, new_height );
757
758     /* copy all the rows */
759     for (i = 0; i < copy_height; i++)
760     {
761         memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
762                 copy_width * sizeof(char_info_t) );
763     }
764
765     /* clear the end of each row */
766     if (new_width > old_width)
767     {
768         /* fill first row */
769         for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
770         /* and blast it to the other rows */
771         for (i = 1; i < copy_height; i++)
772             memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
773                     (new_width - old_width) * sizeof(char_info_t) );
774     }
775
776     /* clear remaining rows */
777     if (new_height > old_height)
778     {
779         /* fill first row */
780         for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
781         /* and blast it to the other rows */
782         for (i = old_height+1; i < new_height; i++)
783             memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
784                     new_width * sizeof(char_info_t) );
785     }
786     free( screen_buffer->data );
787     screen_buffer->data = new_data;
788     screen_buffer->width = new_width;
789     screen_buffer->height = new_height;
790     return 1;
791 }
792
793 /* set misc screen buffer information */
794 static int set_console_output_info( struct screen_buffer *screen_buffer,
795                                     const struct set_console_output_info_request *req )
796 {
797     struct console_renderer_event evt;
798
799     memset(&evt.u, 0, sizeof(evt.u));
800     if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
801     {
802         if (req->cursor_size < 1 || req->cursor_size > 100)
803         {
804             set_error( STATUS_INVALID_PARAMETER );
805             return 0;
806         }
807         if (screen_buffer->cursor_size != req->cursor_size ||
808             screen_buffer->cursor_visible != req->cursor_visible)
809         {
810             screen_buffer->cursor_size    = req->cursor_size;
811             screen_buffer->cursor_visible = req->cursor_visible;
812             evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
813             evt.u.cursor_geom.size    = req->cursor_size;
814             evt.u.cursor_geom.visible = req->cursor_visible;
815             console_input_events_append( screen_buffer->input->evt, &evt );
816         }
817     }
818     if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
819     {
820         if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
821             req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
822         {
823             set_error( STATUS_INVALID_PARAMETER );
824             return 0;
825         }
826         if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
827         {
828             screen_buffer->cursor_x       = req->cursor_x;
829             screen_buffer->cursor_y       = req->cursor_y;
830             evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
831             evt.u.cursor_pos.x = req->cursor_x;
832             evt.u.cursor_pos.y = req->cursor_y;
833             console_input_events_append( screen_buffer->input->evt, &evt );
834         }
835     }
836     if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
837     {
838         unsigned cc;
839
840         /* new screen-buffer cannot be smaller than actual window */
841         if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
842             req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
843         {
844             set_error( STATUS_INVALID_PARAMETER );
845             return 0;
846         }
847         /* FIXME: there are also some basic minimum and max size to deal with */
848         if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
849
850         evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
851         evt.u.resize.width  = req->width;
852         evt.u.resize.height = req->height;
853         console_input_events_append( screen_buffer->input->evt, &evt );
854
855         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
856         evt.u.update.top    = 0;
857         evt.u.update.bottom = screen_buffer->height - 1;
858         console_input_events_append( screen_buffer->input->evt, &evt );
859
860         /* scroll window to display sb */
861         if (screen_buffer->win.right >= req->width)
862         {       
863             screen_buffer->win.right -= screen_buffer->win.left;
864             screen_buffer->win.left = 0;
865         }
866         if (screen_buffer->win.bottom >= req->height)
867         {       
868             screen_buffer->win.bottom -= screen_buffer->win.top;
869             screen_buffer->win.top = 0;
870         }
871         /* reset cursor if needed (normally, if cursor was outside of new sb, the
872          * window has been shifted so that the new position of the cursor will be 
873          * visible */
874         cc = 0;
875         if (screen_buffer->cursor_x >= req->width)
876         {
877             screen_buffer->cursor_x = req->width - 1;
878             cc++;
879         }
880         if (screen_buffer->cursor_y >= req->height)
881         {
882             screen_buffer->cursor_y = req->height - 1;
883             cc++;
884         }
885         if (cc)
886         {
887             evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
888             evt.u.cursor_pos.x = req->cursor_x;
889             evt.u.cursor_pos.y = req->cursor_y;
890             console_input_events_append( screen_buffer->input->evt, &evt );
891         }
892
893         if (screen_buffer == screen_buffer->input->active &&
894             screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
895         {
896             INPUT_RECORD        ir;
897             ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
898             ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
899             ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
900             write_console_input( screen_buffer->input, 1, &ir );
901         }
902     }
903     if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
904     {
905         screen_buffer->attr = req->attr;
906     }
907     if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
908     {
909         if (req->win_left < 0 || req->win_left > req->win_right ||
910             req->win_right >= screen_buffer->width ||
911             req->win_top < 0  || req->win_top > req->win_bottom ||
912             req->win_bottom >= screen_buffer->height)
913         {
914             set_error( STATUS_INVALID_PARAMETER );
915             return 0;
916         }
917         if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
918             screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
919         {
920             screen_buffer->win.left   = req->win_left;
921             screen_buffer->win.top    = req->win_top;
922             screen_buffer->win.right  = req->win_right;
923             screen_buffer->win.bottom = req->win_bottom;
924             evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
925             evt.u.display.left   = req->win_left;
926             evt.u.display.top    = req->win_top;
927             evt.u.display.width  = req->win_right - req->win_left + 1;
928             evt.u.display.height = req->win_bottom - req->win_top + 1;
929             console_input_events_append( screen_buffer->input->evt, &evt );
930         }
931     }
932     if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
933     {
934         /* can only be done by renderer */
935         if (current->process->console != screen_buffer->input)
936         {
937             set_error( STATUS_INVALID_PARAMETER );
938             return 0;
939         }
940
941         screen_buffer->max_width  = req->max_width;
942         screen_buffer->max_height = req->max_height;
943     }
944
945     return 1;
946 }
947
948 /* appends a new line to history (history is a fixed size array) */
949 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
950 {
951     WCHAR*      ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
952
953     if (!ptr)
954         return;
955
956     if (!console || !console->history_size)
957     {
958         set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
959         free( ptr );
960         return;
961     }
962
963     memcpy( ptr, buf, len * sizeof(WCHAR) );
964     ptr[len] = 0;
965
966     if (console->history_mode && console->history_index &&
967         strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
968     {
969         /* ok, mode ask us to not use twice the same string...
970          * so just free mem and returns
971          */
972         set_error( STATUS_ALIAS_EXISTS );
973         free(ptr);
974         return;
975     }
976
977     if (console->history_index < console->history_size)
978     {
979         console->history[console->history_index++] = ptr;
980     }
981     else
982     {
983         free( console->history[0]) ;
984         memmove( &console->history[0], &console->history[1],
985                  (console->history_size - 1) * sizeof(WCHAR*) );
986         console->history[console->history_size - 1] = ptr;
987     }
988 }
989
990 /* returns a line from the cache */
991 static data_size_t console_input_get_hist( struct console_input *console, int index )
992 {
993     data_size_t ret = 0;
994
995     if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
996     else
997     {
998         ret = strlenW( console->history[index] ) * sizeof(WCHAR);
999         set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1000     }
1001     return ret;
1002 }
1003
1004 /* dumb dump */
1005 static void console_input_dump( struct object *obj, int verbose )
1006 {
1007     struct console_input *console = (struct console_input *)obj;
1008     assert( obj->ops == &console_input_ops );
1009     fprintf( stderr, "Console input active=%p evt=%p\n",
1010              console->active, console->evt );
1011 }
1012
1013 static void console_input_destroy( struct object *obj )
1014 {
1015     struct console_input*       console_in = (struct console_input *)obj;
1016     struct screen_buffer*       curr;
1017     int                         i;
1018
1019     assert( obj->ops == &console_input_ops );
1020     free( console_in->title );
1021     free( console_in->records );
1022
1023     if (console_in->active) release_object( console_in->active );
1024     console_in->active = NULL;
1025
1026     LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1027     {
1028         if (curr->input == console_in) curr->input = NULL;
1029     }
1030
1031     release_object( console_in->evt );
1032     console_in->evt = NULL;
1033     release_object( console_in->event );
1034
1035     for (i = 0; i < console_in->history_size; i++)
1036         if (console_in->history[i]) free( console_in->history[i] );
1037     free( console_in->history );
1038 }
1039
1040 static void screen_buffer_dump( struct object *obj, int verbose )
1041 {
1042     struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1043     assert( obj->ops == &screen_buffer_ops );
1044
1045     fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1046 }
1047
1048 static void screen_buffer_destroy( struct object *obj )
1049 {
1050     struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1051
1052     assert( obj->ops == &screen_buffer_ops );
1053
1054     list_remove( &screen_buffer->entry );
1055
1056     if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1057     {
1058         struct screen_buffer *sb;
1059
1060         screen_buffer->input->active = NULL;
1061         LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1062         {
1063             if (sb->input == screen_buffer->input)
1064             {
1065                 sb->input->active = sb;
1066                 break;
1067             }
1068         }
1069     }
1070     free( screen_buffer->data );
1071 }
1072
1073 /* write data into a screen buffer */
1074 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1075                                  const void* data, enum char_info_mode mode,
1076                                  int x, int y, int wrap )
1077 {
1078     unsigned int i;
1079     char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1080
1081     if (y >= screen_buffer->height) return 0;
1082
1083     if (wrap)
1084         end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1085     else
1086         end = screen_buffer->data + (y+1) * screen_buffer->width;
1087
1088     switch(mode)
1089     {
1090     case CHAR_INFO_MODE_TEXT:
1091         {
1092             const WCHAR *ptr = data;
1093             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1094         }
1095         break;
1096     case CHAR_INFO_MODE_ATTR:
1097         {
1098             const unsigned short *ptr = data;
1099             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1100         }
1101         break;
1102     case CHAR_INFO_MODE_TEXTATTR:
1103         {
1104             const char_info_t *ptr = data;
1105             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1106         }
1107         break;
1108     case CHAR_INFO_MODE_TEXTSTDATTR:
1109         {
1110             const WCHAR *ptr = data;
1111             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1112             {
1113                 dest->ch   = ptr[i];
1114                 dest->attr = screen_buffer->attr;
1115             }
1116         }
1117         break;
1118     default:
1119         set_error( STATUS_INVALID_PARAMETER );
1120         return 0;
1121     }
1122
1123     if (i && screen_buffer == screen_buffer->input->active)
1124     {
1125         struct console_renderer_event evt;
1126         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1127         memset(&evt.u, 0, sizeof(evt.u));
1128         evt.u.update.top    = y + x / screen_buffer->width;
1129         evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1130         console_input_events_append( screen_buffer->input->evt, &evt );
1131     }
1132     return i;
1133 }
1134
1135 /* fill a screen buffer with uniform data */
1136 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1137                                 enum char_info_mode mode, int x, int y, int count, int wrap )
1138 {
1139     int i;
1140     char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1141
1142     if (y >= screen_buffer->height) return 0;
1143
1144     if (wrap)
1145         end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1146     else
1147         end = screen_buffer->data + (y+1) * screen_buffer->width;
1148
1149     if (count > end - dest) count = end - dest;
1150
1151     switch(mode)
1152     {
1153     case CHAR_INFO_MODE_TEXT:
1154         for (i = 0; i < count; i++) dest[i].ch = data.ch;
1155         break;
1156     case CHAR_INFO_MODE_ATTR:
1157         for (i = 0; i < count; i++) dest[i].attr = data.attr;
1158         break;
1159     case CHAR_INFO_MODE_TEXTATTR:
1160         for (i = 0; i < count; i++) dest[i] = data;
1161         break;
1162     case CHAR_INFO_MODE_TEXTSTDATTR:
1163         for (i = 0; i < count; i++)
1164         {
1165             dest[i].ch   = data.ch;
1166             dest[i].attr = screen_buffer->attr;
1167         }
1168         break;
1169     default:
1170         set_error( STATUS_INVALID_PARAMETER );
1171         return 0;
1172     }
1173
1174     if (count && screen_buffer == screen_buffer->input->active)
1175     {
1176         struct console_renderer_event evt;
1177         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1178         memset(&evt.u, 0, sizeof(evt.u));
1179         evt.u.update.top    = y;
1180         evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1181         console_input_events_append( screen_buffer->input->evt, &evt );
1182     }
1183     return i;
1184 }
1185
1186 /* read data from a screen buffer */
1187 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1188                                  enum char_info_mode mode, int wrap )
1189 {
1190     int i;
1191     char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1192
1193     if (y >= screen_buffer->height) return;
1194
1195     if (wrap)
1196         end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1197     else
1198         end = screen_buffer->data + (y+1) * screen_buffer->width;
1199
1200     switch(mode)
1201     {
1202     case CHAR_INFO_MODE_TEXT:
1203         {
1204             WCHAR *data;
1205             int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1206             if ((data = set_reply_data_size( count * sizeof(*data) )))
1207             {
1208                 for (i = 0; i < count; i++) data[i] = src[i].ch;
1209             }
1210         }
1211         break;
1212     case CHAR_INFO_MODE_ATTR:
1213         {
1214             unsigned short *data;
1215             int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1216             if ((data = set_reply_data_size( count * sizeof(*data) )))
1217             {
1218                 for (i = 0; i < count; i++) data[i] = src[i].attr;
1219             }
1220         }
1221         break;
1222     case CHAR_INFO_MODE_TEXTATTR:
1223         {
1224             char_info_t *data;
1225             int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1226             if ((data = set_reply_data_size( count * sizeof(*data) )))
1227             {
1228                 for (i = 0; i < count; i++) data[i] = src[i];
1229             }
1230         }
1231         break;
1232     default:
1233         set_error( STATUS_INVALID_PARAMETER );
1234         break;
1235     }
1236 }
1237
1238 /* scroll parts of a screen buffer */
1239 static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1240                                    int w, int h )
1241 {
1242     struct screen_buffer *screen_buffer;
1243     int                         j;
1244     char_info_t *psrc, *pdst;
1245     struct console_renderer_event evt;
1246
1247     if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1248                                                                   CONSOLE_READ, &screen_buffer_ops )))
1249         return;
1250     if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1251         xsrc + w > screen_buffer->width  ||
1252         xdst + w > screen_buffer->width  ||
1253         ysrc + h > screen_buffer->height ||
1254         ydst + h > screen_buffer->height ||
1255         w == 0 || h == 0)
1256     {
1257         set_error( STATUS_INVALID_PARAMETER );
1258         release_object( screen_buffer );
1259         return;
1260     }
1261
1262     if (ysrc < ydst)
1263     {
1264         psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1265         pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1266
1267         for (j = h; j > 0; j--)
1268         {
1269             memcpy(pdst, psrc, w * sizeof(*pdst) );
1270             pdst -= screen_buffer->width;
1271             psrc -= screen_buffer->width;
1272         }
1273     }
1274     else
1275     {
1276         psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1277         pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1278
1279         for (j = 0; j < h; j++)
1280         {
1281             /* we use memmove here because when psrc and pdst are the same,
1282              * copies are done on the same row, so the dst and src blocks
1283              * can overlap */
1284             memmove( pdst, psrc, w * sizeof(*pdst) );
1285             pdst += screen_buffer->width;
1286             psrc += screen_buffer->width;
1287         }
1288     }
1289
1290     /* FIXME: this could be enhanced, by signalling scroll */
1291     evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1292     memset(&evt.u, 0, sizeof(evt.u));
1293     evt.u.update.top    = min(ysrc, ydst);
1294     evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1295     console_input_events_append( screen_buffer->input->evt, &evt );
1296
1297     release_object( screen_buffer );
1298 }
1299
1300 /* allocate a console for the renderer */
1301 DECL_HANDLER(alloc_console)
1302 {
1303     obj_handle_t in = 0;
1304     obj_handle_t evt = 0;
1305     struct process *process;
1306     struct process *renderer = current->process;
1307     struct console_input *console;
1308
1309     if (req->pid)
1310     {
1311         if (!(process = get_process_from_id( req->pid ))) return;
1312     }
1313     else
1314     {
1315         if (!(process = renderer->parent))
1316         {
1317             set_error( STATUS_ACCESS_DENIED );
1318             return;
1319         }
1320         grab_object( process );
1321     }
1322
1323     if (process != renderer && process->console)
1324     {
1325         set_error( STATUS_ACCESS_DENIED );
1326         goto the_end;
1327     }
1328     if ((console = (struct console_input*)create_console_input( current )))
1329     {
1330         if ((in = alloc_handle( renderer, console, req->access, req->attributes )))
1331         {
1332             if ((evt = alloc_handle( renderer, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1333             {
1334                 if (process != renderer)
1335                 {
1336                     process->console = (struct console_input*)grab_object( console );
1337                     console->num_proc++;
1338                 }
1339                 reply->handle_in = in;
1340                 reply->event = evt;
1341                 release_object( console );
1342                 goto the_end;
1343             }
1344             close_handle( renderer, in );
1345         }
1346         free_console( process );
1347     }
1348  the_end:
1349     release_object( process );
1350 }
1351
1352 /* free the console of the current process */
1353 DECL_HANDLER(free_console)
1354 {
1355     free_console( current->process );
1356 }
1357
1358 /* let the renderer peek the events it's waiting on */
1359 DECL_HANDLER(get_console_renderer_events)
1360 {
1361     struct console_input_events *evt;
1362
1363     evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1364                                                          CONSOLE_READ, &console_input_events_ops );
1365     if (!evt) return;
1366     console_input_events_get( evt );
1367     release_object( evt );
1368 }
1369
1370 /* open a handle to the process console */
1371 DECL_HANDLER(open_console)
1372 {
1373     struct object      *obj = NULL;
1374
1375     reply->handle = 0;
1376     if (req->from == (obj_handle_t)0)
1377     {
1378         if (current->process->console && current->process->console->renderer)
1379             obj = grab_object( (struct object*)current->process->console );
1380     }
1381     else if (req->from == (obj_handle_t)1)
1382     {
1383          if (current->process->console && current->process->console->renderer &&
1384              current->process->console->active)
1385              obj = grab_object( (struct object*)current->process->console->active );
1386     }
1387     else if ((obj = get_handle_obj( current->process, req->from,
1388                                     CONSOLE_READ|CONSOLE_WRITE, &console_input_ops )))
1389     {
1390         struct console_input *console = (struct console_input *)obj;
1391         obj = (console->active) ? grab_object( console->active ) : NULL;
1392         release_object( console );
1393     }
1394
1395     /* FIXME: req->share is not used (as in screen buffer creation)  */
1396     if (obj)
1397     {
1398         reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1399         release_object( obj );
1400     }
1401     else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1402 }
1403
1404 /* set info about a console input */
1405 DECL_HANDLER(set_console_input_info)
1406 {
1407     set_console_input_info( req, get_req_data(), get_req_data_size() );
1408 }
1409
1410 /* get info about a console (output only) */
1411 DECL_HANDLER(get_console_input_info)
1412 {
1413     struct console_input *console;
1414
1415     if (!(console = console_input_get( req->handle, CONSOLE_READ ))) return;
1416     if (console->title)
1417     {
1418         data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1419         if (len > get_reply_max_size()) len = get_reply_max_size();
1420         set_reply_data( console->title, len );
1421     }
1422     reply->history_mode  = console->history_mode;
1423     reply->history_size  = console->history_size;
1424     reply->history_index = console->history_index;
1425     reply->edition_mode  = console->edition_mode;
1426     reply->input_cp      = console->input_cp;
1427     reply->output_cp     = console->output_cp;
1428     reply->win           = console->win;
1429
1430     release_object( console );
1431 }
1432
1433 /* get a console mode (input or output) */
1434 DECL_HANDLER(get_console_mode)
1435 {
1436     reply->mode = get_console_mode( req->handle );
1437 }
1438
1439 /* set a console mode (input or output) */
1440 DECL_HANDLER(set_console_mode)
1441 {
1442     set_console_mode( req->handle, req->mode );
1443 }
1444
1445 /* add input records to a console input queue */
1446 DECL_HANDLER(write_console_input)
1447 {
1448     struct console_input *console;
1449
1450     reply->written = 0;
1451     if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1452                                                             CONSOLE_WRITE, &console_input_ops )))
1453         return;
1454     reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1455                                           get_req_data() );
1456     release_object( console );
1457 }
1458
1459 /* fetch input records from a console input queue */
1460 DECL_HANDLER(read_console_input)
1461 {
1462     int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1463     reply->read = read_console_input( req->handle, count, req->flush );
1464 }
1465
1466 /* appends a string to console's history */
1467 DECL_HANDLER(append_console_input_history)
1468 {
1469     struct console_input *console;
1470
1471     if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1472     console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1473     release_object( console );
1474 }
1475
1476 /* appends a string to console's history */
1477 DECL_HANDLER(get_console_input_history)
1478 {
1479     struct console_input *console;
1480
1481     if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1482     reply->total = console_input_get_hist( console, req->index );
1483     release_object( console );
1484 }
1485
1486 /* creates a screen buffer */
1487 DECL_HANDLER(create_console_output)
1488 {
1489     struct console_input*       console;
1490     struct screen_buffer*       screen_buffer;
1491
1492     if (!(console = console_input_get( req->handle_in, CONSOLE_WRITE ))) return;
1493
1494     screen_buffer = create_console_output( console );
1495     if (screen_buffer)
1496     {
1497         /* FIXME: should store sharing and test it when opening the CONOUT$ device
1498          * see file.c on how this could be done */
1499         reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1500         release_object( screen_buffer );
1501     }
1502     release_object( console );
1503 }
1504
1505 /* set info about a console screen buffer */
1506 DECL_HANDLER(set_console_output_info)
1507 {
1508     struct screen_buffer *screen_buffer;
1509
1510     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1511                                                                 CONSOLE_WRITE, &screen_buffer_ops)))
1512     {
1513         set_console_output_info( screen_buffer, req );
1514         release_object( screen_buffer );
1515     }
1516 }
1517
1518 /* get info about a console screen buffer */
1519 DECL_HANDLER(get_console_output_info)
1520 {
1521     struct screen_buffer *screen_buffer;
1522
1523     if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1524                                                                  CONSOLE_READ, &screen_buffer_ops)))
1525     {
1526         reply->cursor_size    = screen_buffer->cursor_size;
1527         reply->cursor_visible = screen_buffer->cursor_visible;
1528         reply->cursor_x       = screen_buffer->cursor_x;
1529         reply->cursor_y       = screen_buffer->cursor_y;
1530         reply->width          = screen_buffer->width;
1531         reply->height         = screen_buffer->height;
1532         reply->attr           = screen_buffer->attr;
1533         reply->win_left       = screen_buffer->win.left;
1534         reply->win_top        = screen_buffer->win.top;
1535         reply->win_right      = screen_buffer->win.right;
1536         reply->win_bottom     = screen_buffer->win.bottom;
1537         reply->max_width      = screen_buffer->max_width;
1538         reply->max_height     = screen_buffer->max_height;
1539         release_object( screen_buffer );
1540     }
1541 }
1542
1543 /* read data (chars & attrs) from a screen buffer */
1544 DECL_HANDLER(read_console_output)
1545 {
1546     struct screen_buffer *screen_buffer;
1547
1548     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1549                                                                 CONSOLE_READ, &screen_buffer_ops )))
1550     {
1551         read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1552         reply->width  = screen_buffer->width;
1553         reply->height = screen_buffer->height;
1554         release_object( screen_buffer );
1555     }
1556 }
1557
1558 /* write data (char and/or attrs) to a screen buffer */
1559 DECL_HANDLER(write_console_output)
1560 {
1561     struct screen_buffer *screen_buffer;
1562
1563     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1564                                                                 CONSOLE_WRITE, &screen_buffer_ops)))
1565     {
1566         reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1567                                                req->mode, req->x, req->y, req->wrap );
1568         reply->width  = screen_buffer->width;
1569         reply->height = screen_buffer->height;
1570         release_object( screen_buffer );
1571     }
1572 }
1573
1574 /* fill a screen buffer with constant data (chars and/or attributes) */
1575 DECL_HANDLER(fill_console_output)
1576 {
1577     struct screen_buffer *screen_buffer;
1578
1579     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1580                                                                 CONSOLE_WRITE, &screen_buffer_ops)))
1581     {
1582         reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1583                                               req->x, req->y, req->count, req->wrap );
1584         release_object( screen_buffer );
1585     }
1586 }
1587
1588 /* move a rect of data in a screen buffer */
1589 DECL_HANDLER(move_console_output)
1590 {
1591     scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1592                            req->w, req->h );
1593 }
1594
1595 /* sends a signal to a console (process, group...) */
1596 DECL_HANDLER(send_console_signal)
1597 {
1598     process_id_t group;
1599
1600     group = req->group_id ? req->group_id : current->process->group_id;
1601
1602     if (!group)
1603         set_error( STATUS_INVALID_PARAMETER );
1604     else
1605         propagate_console_signal( current->process->console, req->signal, group );
1606 }
1607
1608 /* get console which renderer is 'current' */
1609 static int cgwe_enum( struct process* process, void* user)
1610 {
1611     if (process->console && process->console->renderer == current)
1612     {
1613         *(struct console_input**)user = process->console;
1614         return 1;
1615     }
1616     return 0;
1617 }
1618
1619 DECL_HANDLER(get_console_wait_event)
1620 {
1621     struct console_input* console = NULL;
1622
1623     if (current->process->console && current->process->console->renderer)
1624         console = (struct console_input*)grab_object( (struct object*)current->process->console );
1625     else enum_processes(cgwe_enum, &console);
1626
1627     if (console)
1628     {
1629         reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1630         release_object( console );
1631     }
1632     else set_error( STATUS_INVALID_PARAMETER );
1633 }