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