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