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