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