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