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