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