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