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