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