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