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