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