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