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