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