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