Implemented processing for actions: SPI_GET/SETGRIDGRANULARITY,
[wine] / server / console.c
1 /*
2  * Server-side console management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *               2001 Eric Pouech
6  *
7  */
8
9 #include "config.h"
10
11 #include <assert.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <unistd.h>
15
16 #include "handle.h"
17 #include "process.h"
18 #include "request.h"
19 #include "unicode.h"
20 #include "console.h"
21
22
23 static void console_input_dump( struct object *obj, int verbose );
24 static void console_input_destroy( struct object *obj );
25 static int console_input_signaled( struct object *obj, struct thread *thread );
26
27 /* common routine */
28 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply );
29
30 static const struct object_ops console_input_ops =
31 {
32     sizeof(struct console_input),     /* size */
33     console_input_dump,               /* dump */
34     add_queue,                        /* add_queue */
35     remove_queue,                     /* remove_queue */
36     console_input_signaled,           /* signaled */
37     no_satisfied,                     /* satisfied */
38     NULL,                             /* get_poll_events */
39     NULL,                             /* poll_event */
40     no_get_fd,                        /* get_fd */
41     no_flush,                         /* flush */
42     console_get_file_info,            /* get_file_info */
43     console_input_destroy             /* destroy */
44 };
45
46 static void console_input_events_dump( struct object *obj, int verbose );
47 static void console_input_events_destroy( struct object *obj );
48 static int  console_input_events_signaled( struct object *obj, struct thread *thread );
49
50 struct console_input_events 
51 {
52     struct object         obj;         /* object header */
53     int                   num_alloc;   /* number of allocated events */
54     int                   num_used;    /* number of actually used events */
55     struct console_renderer_event*      events;
56 };
57
58 static const struct object_ops console_input_events_ops =
59 {
60     sizeof(struct console_input_events), /* size */
61     console_input_events_dump,        /* dump */
62     add_queue,                        /* add_queue */
63     remove_queue,                     /* remove_queue */
64     console_input_events_signaled,    /* signaled */
65     no_satisfied,                     /* satisfied */
66     NULL,                             /* get_poll_events */
67     NULL,                             /* poll_event */
68     no_get_fd,                        /* get_fd */
69     no_flush,                         /* flush */
70     no_get_file_info,                 /* get_file_info */
71     console_input_events_destroy      /* destroy */
72 };
73
74 struct screen_buffer
75 {
76     struct object         obj;           /* object header */
77     struct screen_buffer *next;          /* linked list of all screen buffers */
78     struct screen_buffer *prev;
79     struct console_input *input;         /* associated console input */
80     int                   mode;          /* output mode */
81     int                   cursor_size;   /* size of cursor (percentage filled) */
82     int                   cursor_visible;/* cursor visibility flag */
83     int                   cursor_x;      /* position of cursor */
84     int                   cursor_y;      /* position of cursor */
85     int                   width;         /* size (w-h) of the screen buffer */
86     int                   height;
87     int                   max_width;     /* size (w-h) of the window given font size */
88     int                   max_height;
89     char_info_t          *data;          /* the data for each cell - a width x height matrix */
90     unsigned short        attr;          /* default attribute for screen buffer */
91     rectangle_t           win;           /* current visible window on the screen buffer *
92                                           * as seen in wineconsole */
93 };
94
95 static void screen_buffer_dump( struct object *obj, int verbose );
96 static void screen_buffer_destroy( struct object *obj );
97
98 static const struct object_ops screen_buffer_ops =
99 {
100     sizeof(struct screen_buffer),     /* size */
101     screen_buffer_dump,               /* dump */
102     no_add_queue,                     /* add_queue */
103     NULL,                             /* remove_queue */
104     NULL,                             /* signaled */
105     NULL,                             /* satisfied */
106     NULL,                             /* get_poll_events */
107     NULL,                             /* poll_event */
108     no_get_fd,                        /* get_fd */
109     no_flush,                         /* flush */
110     console_get_file_info,            /* get_file_info */
111     screen_buffer_destroy             /* destroy */
112 };
113
114 static struct screen_buffer *screen_buffer_list;
115
116 static const char_info_t empty_char_info = { ' ', 0x00f0 };  /* white on black space */
117
118 /* dumps the renderer events of a console */
119 static void console_input_events_dump( struct object *obj, int verbose )
120 {
121     struct console_input_events *evts = (struct console_input_events *)obj;
122     assert( obj->ops == &console_input_events_ops );
123     fprintf( stderr, "Console input events: %d/%d events\n", 
124              evts->num_used, evts->num_alloc );
125 }
126
127 /* destroys the renderer events of a console */
128 static void console_input_events_destroy( struct object *obj )
129 {
130     struct console_input_events *evts = (struct console_input_events *)obj;
131     assert( obj->ops == &console_input_events_ops );
132     free( evts->events );
133 }
134
135 /* the renderer events list is signaled when it's not empty */
136 static int console_input_events_signaled( struct object *obj, struct thread *thread )
137 {
138     struct console_input_events *evts = (struct console_input_events *)obj;
139     assert( obj->ops == &console_input_events_ops );
140     return (evts->num_used != 0);
141 }
142
143 /* add an event to the console's renderer events list */
144 static void console_input_events_append( struct console_input_events* evts, 
145                                          struct console_renderer_event* evt)
146 {
147     /* to be done even when the renderer generates the events ? */
148     if (evts->num_used == evts->num_alloc)
149     {
150         evts->num_alloc += 16;
151         evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
152         assert(evts->events);
153     }
154     evts->events[evts->num_used++] = *evt;
155     wake_up( &evts->obj, 0 );
156 }
157
158 /* retrieves events from the console's renderer events list */
159 static void console_input_events_get( struct console_input_events* evts )
160 {
161     size_t num = get_reply_max_size() / sizeof(evts->events[0]);
162
163     if (num > evts->num_used) num = evts->num_used;
164     set_reply_data( evts->events, num * sizeof(evts->events[0]) );
165     if (num < evts->num_used)
166     {
167         memmove( &evts->events[0], &evts->events[num],
168                  (evts->num_used - num) * sizeof(evts->events[0]) );
169     }
170     evts->num_used -= num;
171 }
172
173 static struct console_input_events *create_console_input_events(void)
174 {
175     struct console_input_events*        evt;
176
177     if (!(evt = alloc_object( &console_input_events_ops, -1 ))) return NULL;
178     evt->num_alloc = evt->num_used = 0;
179     evt->events = NULL;
180     return evt;
181 }
182
183 static struct object *create_console_input( struct thread* renderer )
184 {
185     struct console_input *console_input;
186
187     if (!(console_input = alloc_object( &console_input_ops, -1 ))) return NULL;
188     console_input->renderer      = renderer;
189     console_input->mode          = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
190                                    ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
191     console_input->num_proc      = 0;
192     console_input->active        = NULL;
193     console_input->recnum        = 0;
194     console_input->records       = NULL;
195     console_input->evt           = create_console_input_events();
196     console_input->title         = NULL;
197     console_input->history_size  = 50;
198     console_input->history       = calloc( console_input->history_size, sizeof(WCHAR*) );
199     console_input->history_index = 0;
200     console_input->history_mode  = 0;
201
202     if (!console_input->history || !console_input->evt)
203     {
204         release_object( console_input );
205         return NULL;
206     }
207     return &console_input->obj;
208 }
209
210 static struct screen_buffer *create_console_output( struct console_input *console_input )
211 {
212     struct screen_buffer *screen_buffer;
213     struct console_renderer_event evt;
214     int i;
215
216     if (!(screen_buffer = alloc_object( &screen_buffer_ops, -1 ))) return NULL;
217     screen_buffer->mode           = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
218     screen_buffer->input          = console_input;
219     screen_buffer->cursor_size    = 100;
220     screen_buffer->cursor_visible = 1;
221     screen_buffer->width          = 80;
222     screen_buffer->height         = 150;
223     screen_buffer->max_width      = 80;
224     screen_buffer->max_height     = 25;
225     screen_buffer->cursor_x       = 0;
226     screen_buffer->cursor_y       = 0;
227     screen_buffer->attr           = 0xF0;
228     screen_buffer->win.left       = 0;
229     screen_buffer->win.right      = screen_buffer->max_width - 1;
230     screen_buffer->win.top        = 0;
231     screen_buffer->win.bottom     = screen_buffer->max_height - 1;
232
233     if ((screen_buffer->next = screen_buffer_list)) screen_buffer->next->prev = screen_buffer;
234     screen_buffer->prev = NULL;
235     screen_buffer_list = screen_buffer;
236
237     if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
238                                         sizeof(*screen_buffer->data) )))
239     {
240         release_object( screen_buffer );
241         return NULL;
242     }
243     /* clear the first row */
244     for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
245     /* and copy it to all other rows */
246     for (i = 1; i < screen_buffer->height; i++)
247         memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
248                 screen_buffer->width * sizeof(char_info_t) );
249
250     if (!console_input->active)
251     {
252         console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
253
254         /* generate the fist events */
255         evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
256         console_input_events_append( console_input->evt, &evt );
257
258         evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
259         evt.u.resize.width  = screen_buffer->width;
260         evt.u.resize.height = screen_buffer->height;
261         console_input_events_append( console_input->evt, &evt );
262
263         evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
264         evt.u.display.left   = screen_buffer->win.left;
265         evt.u.display.top    = screen_buffer->win.top;
266         evt.u.display.width  = screen_buffer->win.right - screen_buffer->win.left + 1;
267         evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
268         console_input_events_append( console_input->evt, &evt );
269
270         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
271         evt.u.update.top    = 0;
272         evt.u.update.bottom = screen_buffer->height - 1;
273         console_input_events_append( console_input->evt, &evt );
274
275         evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
276         evt.u.cursor_geom.size    = screen_buffer->cursor_size;
277         evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
278         console_input_events_append( console_input->evt, &evt );
279
280         evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
281         evt.u.cursor_pos.x = screen_buffer->cursor_x;
282         evt.u.cursor_pos.y = screen_buffer->cursor_y;
283         console_input_events_append( console_input->evt, &evt );
284     }
285     return screen_buffer;
286 }
287
288 /* free the console for this process */
289 int free_console( struct process *process )
290 {
291     struct console_input* console = process->console;
292
293     if (!console || !console->renderer) return 0;
294
295     process->console = NULL;
296     if (--console->num_proc == 0)
297     {
298         /* all processes have terminated... tell the renderer to terminate too */
299         struct console_renderer_event evt;
300         evt.event = CONSOLE_RENDERER_EXIT_EVENT;
301         console_input_events_append( console->evt, &evt );
302     }
303     release_object( console );
304
305     return 1;
306 }
307
308 /* let process inherit the console from parent... this handle two cases :
309  *      1/ generic console inheritance
310  *      2/ parent is a renderer which launches process, and process should attach to the console
311  *         renderered by parent
312  */
313 void inherit_console(struct thread *parent_thread, struct process *process, handle_t hconin)
314 {
315     int done = 0;
316     struct process* parent = parent_thread->process;
317
318     /* if parent is a renderer, then attach current process to its console
319      * a bit hacky....
320      */
321     if (hconin)
322     {
323         struct console_input* console;
324
325         if ((console = (struct console_input*)get_handle_obj( parent, hconin, 0, NULL )))
326         {
327             if (console->renderer == parent_thread)
328             {
329                 process->console = (struct console_input*)grab_object( console );
330                 process->console->num_proc++;
331                 done = 1;
332             }
333             release_object( console );
334         }
335     }
336     /* otherwise, if parent has a console, attach child to this console */
337     if (!done && parent->console)
338     {
339         assert(parent->console->renderer);
340         process->console = (struct console_input*)grab_object( parent->console );
341         process->console->num_proc++;
342     }
343 }
344
345 static struct console_input* console_input_get( handle_t handle, unsigned access )
346 {
347     struct console_input*       console = 0;
348
349     if (handle)
350         console = (struct console_input *)get_handle_obj( current->process, handle,
351                                                           access, &console_input_ops );
352     else if (current->process->console)
353     {
354         assert( current->process->console->renderer );
355         console = (struct console_input *)grab_object( current->process->console );
356     }
357
358     if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
359     return console;
360 }
361
362 /* check if a console input is signaled: yes if non read input records */
363 static int console_input_signaled( struct object *obj, struct thread *thread )
364 {
365     struct console_input *console = (struct console_input *)obj;
366     assert( obj->ops == &console_input_ops );
367     return console->recnum ? 1 : 0;
368 }
369
370 static int get_console_mode( handle_t handle )
371 {
372     struct object *obj;
373     int ret = 0;
374
375     if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
376     {
377         if (obj->ops == &console_input_ops)
378             ret = ((struct console_input *)obj)->mode;
379         else if (obj->ops == &screen_buffer_ops)
380             ret = ((struct screen_buffer *)obj)->mode;
381         else
382             set_error( STATUS_OBJECT_TYPE_MISMATCH );
383         release_object( obj );
384     }
385     return ret;
386 }
387
388 /* changes the mode of either a console input or a screen buffer */
389 static int set_console_mode( handle_t handle, int mode )
390 {
391     struct object *obj;
392     int ret = 0;
393
394     if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
395         return 0;
396     if (obj->ops == &console_input_ops)
397     {
398         /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
399         ((struct console_input *)obj)->mode = mode;
400         ret = 1;
401     }
402     else if (obj->ops == &screen_buffer_ops)
403     {
404         ((struct screen_buffer *)obj)->mode = mode;
405         ret = 1;
406     }
407     else set_error( STATUS_OBJECT_TYPE_MISMATCH );
408     release_object( obj );
409     return ret;
410 }
411
412 /* add input events to a console input queue */
413 static int write_console_input( struct console_input* console, int count,
414                                 const INPUT_RECORD *records )
415 {
416     INPUT_RECORD *new_rec;
417
418     if (!count) return 0;
419     if (!(new_rec = realloc( console->records,
420                              (console->recnum + count) * sizeof(INPUT_RECORD) )))
421     {
422         set_error( STATUS_NO_MEMORY );
423         release_object( console );
424         return -1;
425     }
426     console->records = new_rec;
427     memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
428     console->recnum += count;
429
430     /* wake up all waiters */
431     wake_up( &console->obj, 0 );
432     return count;
433 }
434
435 /* retrieve a pointer to the console input records */
436 static int read_console_input( handle_t handle, int count, int flush )
437 {
438     struct console_input *console;
439
440     if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
441                                                             GENERIC_READ, &console_input_ops )))
442         return -1;
443
444     if (!count)
445     {
446         /* special case: do not retrieve anything, but return
447          * the total number of records available */
448         count = console->recnum;
449     }
450     else
451     {
452         if (count > console->recnum) count = console->recnum;
453         set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
454     }
455     if (flush)
456     {
457         int i;
458         for (i = count; i < console->recnum; i++)
459             console->records[i-count] = console->records[i];
460         if ((console->recnum -= count) > 0)
461         {
462             INPUT_RECORD *new_rec = realloc( console->records,
463                                              console->recnum * sizeof(INPUT_RECORD) );
464             if (new_rec) console->records = new_rec;
465         }
466         else
467         {
468             free( console->records );
469             console->records = NULL;
470         }
471     }
472     release_object( console );
473     return count;
474 }
475
476 /* set misc console input information */
477 static int set_console_input_info( const struct set_console_input_info_request *req,
478                                    const WCHAR *title, size_t len )
479 {
480     struct console_input *console;
481     struct console_renderer_event evt;
482
483     if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) goto error;
484
485     if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
486     {
487         struct screen_buffer *screen_buffer;
488
489         screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb, 
490                                                                 GENERIC_READ, &screen_buffer_ops );
491         if (!screen_buffer || screen_buffer->input != console)
492         {
493             set_error( STATUS_INVALID_PARAMETER );
494             if (screen_buffer) release_object( screen_buffer );
495             goto error;
496         }
497
498         if (screen_buffer != console->active)
499         {
500             if (console->active) release_object( console->active );
501             console->active = screen_buffer;
502             evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
503             console_input_events_append( console->evt, &evt );
504         }
505         else
506             release_object( screen_buffer );
507     }
508     if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
509     {
510         WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
511         if (new_title)
512         {
513             memcpy( new_title, title, len + sizeof(WCHAR) );
514             new_title[len / sizeof(WCHAR)] = 0;
515             if (console->title) free( console->title );
516             console->title = new_title;
517         }
518     }
519     if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
520     {
521         console->history_mode = req->history_mode;
522     }
523     if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) && 
524         console->history_size != req->history_size)
525     {
526         WCHAR** mem = NULL;
527         int     i;
528         int     delta;
529
530         if (req->history_size)
531         {
532             mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
533             if (!mem) goto error;
534             memset( mem, 0, req->history_size * sizeof(WCHAR*) );
535         }
536
537         delta = (console->history_index > req->history_size) ? 
538             (console->history_index - req->history_size) : 0;
539
540         for (i = delta; i < console->history_index; i++)
541         {
542             mem[i - delta] = console->history[i];
543             console->history[i] = NULL;
544         }
545         console->history_index -= delta;
546
547         for (i = 0; i < console->history_size; i++)
548             if (console->history[i]) free( console->history[i] );
549         free( console->history );
550         console->history = mem;
551         console->history_size = req->history_size;
552     }
553     release_object( console );
554     return 1;
555  error:
556     if (console) release_object( console );
557     return 0;
558 }
559
560 /* resize a screen buffer */
561 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
562                                       int new_width, int new_height )
563 {
564     int i, old_width, old_height, copy_width, copy_height;
565     char_info_t *new_data;
566
567     if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
568     {
569         set_error( STATUS_NO_MEMORY );
570         return 0;
571     }
572     old_width = screen_buffer->width;
573     old_height = screen_buffer->height;
574     copy_width = min( old_width, new_width );
575     copy_height = min( old_height, new_height );
576
577     /* copy all the rows */
578     for (i = 0; i < copy_height; i++)
579     {
580         memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
581                 copy_width * sizeof(char_info_t) );
582     }
583
584     /* clear the end of each row */
585     if (new_width > old_width)
586     {
587         /* fill first row */
588         for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
589         /* and blast it to the other rows */
590         for (i = 1; i < copy_height; i++)
591             memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
592                     (new_width - old_width) * sizeof(char_info_t) );
593     }
594
595     /* clear remaining rows */
596     if (new_height > old_height)
597     {
598         /* fill first row */
599         for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
600         /* and blast it to the other rows */
601         for (i = old_height+1; i < new_height; i++)
602             memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
603                     new_width * sizeof(char_info_t) );
604     }
605     free( screen_buffer->data );
606     screen_buffer->data = new_data;
607     screen_buffer->width = new_width;
608     screen_buffer->height = new_height;
609     return 1;
610 }
611
612 /* set misc screen buffer information */
613 static int set_console_output_info( struct screen_buffer *screen_buffer,
614                                     const struct set_console_output_info_request *req )
615 {
616     struct console_renderer_event evt;
617
618     if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
619     {
620         if (req->cursor_size < 1 || req->cursor_size > 100)
621         {
622             set_error( STATUS_INVALID_PARAMETER );
623             return 0;
624         }
625         if (screen_buffer->cursor_size != req->cursor_size || 
626             screen_buffer->cursor_visible != req->cursor_visible)
627         {
628             screen_buffer->cursor_size    = req->cursor_size;
629             screen_buffer->cursor_visible = req->cursor_visible;
630             evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
631             evt.u.cursor_geom.size    = req->cursor_size;
632             evt.u.cursor_geom.visible = req->cursor_visible;
633             console_input_events_append( screen_buffer->input->evt, &evt );
634         }
635     }
636     if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
637     {
638         if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width || 
639             req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
640         {
641             set_error( STATUS_INVALID_PARAMETER );
642             return 0;
643         }
644         if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
645         {
646             screen_buffer->cursor_x       = req->cursor_x;
647             screen_buffer->cursor_y       = req->cursor_y;
648             evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
649             evt.u.cursor_pos.x = req->cursor_x;
650             evt.u.cursor_pos.y = req->cursor_y;
651             console_input_events_append( screen_buffer->input->evt, &evt );
652         }
653     }
654     if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
655     {
656         /* FIXME: there are also some basic minimum and max size to deal with */
657         if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
658
659         evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
660         evt.u.resize.width  = req->width;
661         evt.u.resize.height = req->height;
662         console_input_events_append( screen_buffer->input->evt, &evt );
663
664         if (screen_buffer == screen_buffer->input->active && 
665             screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
666         {
667             INPUT_RECORD        ir;
668             ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
669             ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
670             ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
671             write_console_input( screen_buffer->input, 1, &ir );
672         }
673     }
674     if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
675     {
676         screen_buffer->attr = req->attr;
677     }
678     if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
679     {
680         if (req->win_left < 0 || req->win_left > req->win_right || 
681             req->win_right >= screen_buffer->width ||
682             req->win_top < 0  || req->win_top > req->win_bottom || 
683             req->win_bottom >= screen_buffer->height)
684         {
685             set_error( STATUS_INVALID_PARAMETER );
686             return 0;
687         }
688         if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
689             screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
690         {
691             screen_buffer->win.left   = req->win_left;
692             screen_buffer->win.top    = req->win_top;
693             screen_buffer->win.right  = req->win_right;
694             screen_buffer->win.bottom = req->win_bottom;
695             evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
696             evt.u.display.left   = req->win_left;
697             evt.u.display.top    = req->win_top;
698             evt.u.display.width  = req->win_right - req->win_left + 1;
699             evt.u.display.height = req->win_bottom - req->win_top + 1;
700             console_input_events_append( screen_buffer->input->evt, &evt );
701         }
702     }
703     if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
704     {
705         /* can only be done by renderer */
706         if (current->process->console != screen_buffer->input)
707         {
708             set_error( STATUS_INVALID_PARAMETER );
709             return 0;
710         }
711
712         screen_buffer->max_width  = req->max_width;
713         screen_buffer->max_height = req->max_height;
714     }
715
716     return 1;
717 }
718
719 /* appends a new line to history (history is a fixed size array) */
720 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
721 {
722     WCHAR*      ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
723
724     if (!ptr)
725     {
726         set_error( STATUS_NO_MEMORY );
727         return;
728     }
729     if (!console || !console->history_size)
730     {
731         set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
732         return;
733     }
734
735     memcpy( ptr, buf, len * sizeof(WCHAR) );
736     ptr[len] = 0;
737
738     if (console->history_mode && console->history_index &&
739         strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
740     {
741         /* ok, mode ask us to not use twice the same string...
742          * so just free mem and returns
743          */
744         set_error( STATUS_ALIAS_EXISTS );
745         free(ptr);
746         return;
747     }
748
749     if (console->history_index < console->history_size)
750     {
751         console->history[console->history_index++] = ptr;
752     }
753     else
754     {
755         free( console->history[0]) ;
756         memmove( &console->history[0], &console->history[1], 
757                  (console->history_size - 1) * sizeof(WCHAR*) );
758         console->history[console->history_size - 1] = ptr;
759     }
760 }
761
762 /* returns a line from the cachde */
763 static size_t console_input_get_hist( struct console_input *console, int index )
764 {
765     size_t ret = 0;
766
767     if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
768     else
769     {
770         ret = strlenW( console->history[index] ) * sizeof(WCHAR);
771         set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
772     }
773     return ret;
774 }
775
776 /* dumb dump */
777 static void console_input_dump( struct object *obj, int verbose )
778 {
779     struct console_input *console = (struct console_input *)obj;
780     assert( obj->ops == &console_input_ops );
781     fprintf( stderr, "Console input active=%p evt=%p\n", 
782              console->active, console->evt );
783 }
784
785 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply )
786 {
787     if (reply)
788     {
789         reply->type        = FILE_TYPE_CHAR;
790         reply->attr        = 0;
791         reply->access_time = 0;
792         reply->write_time  = 0;
793         reply->size_high   = 0;
794         reply->size_low    = 0;
795         reply->links       = 0;
796         reply->index_high  = 0;
797         reply->index_low   = 0;
798         reply->serial      = 0;
799     }
800     return FD_TYPE_CONSOLE;
801 }
802
803 static void console_input_destroy( struct object *obj )
804 {
805     struct console_input*       console_in = (struct console_input *)obj;
806     struct screen_buffer*       curr;
807     int                         i;
808
809     assert( obj->ops == &console_input_ops );
810     if (console_in->title) free( console_in->title );
811     if (console_in->records) free( console_in->records );
812
813     if (console_in->active)     release_object( console_in->active );
814     console_in->active = NULL;
815
816     for (curr = screen_buffer_list; curr; curr = curr->next)
817     {
818         if (curr->input == console_in) curr->input = NULL;
819     }
820
821     release_object( console_in->evt );
822     console_in->evt = NULL;
823
824     for (i = 0; i < console_in->history_size; i++)
825         if (console_in->history[i]) free( console_in->history[i] );
826     if (console_in->history) free( console_in->history );
827 }
828
829 static void screen_buffer_dump( struct object *obj, int verbose )
830 {
831     struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
832     assert( obj->ops == &screen_buffer_ops );
833
834     fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
835 }
836
837 static void screen_buffer_destroy( struct object *obj )
838 {
839     struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
840
841     assert( obj->ops == &screen_buffer_ops );
842
843     if (screen_buffer->next) screen_buffer->next->prev = screen_buffer->prev;
844     if (screen_buffer->prev) screen_buffer->prev->next = screen_buffer->next;
845     else screen_buffer_list = screen_buffer->next;
846
847     if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
848     {
849         struct screen_buffer*   sb;
850         for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
851         screen_buffer->input->active = sb;
852     }
853     if (screen_buffer->data) free( screen_buffer->data );
854 }
855
856 /* write data into a screen buffer */
857 static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
858                                  const void* data, enum char_info_mode mode,
859                                  int x, int y, int wrap )
860 {
861     int i;
862     char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
863
864     if (y >= screen_buffer->height) return 0;
865
866     if (wrap)
867         end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
868     else
869         end = screen_buffer->data + (y+1) * screen_buffer->width;
870
871     switch(mode)
872     {
873     case CHAR_INFO_MODE_TEXT:
874         {
875             const WCHAR *ptr = data;
876             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
877         }
878         break;
879     case CHAR_INFO_MODE_ATTR:
880         {
881             const unsigned short *ptr = data;
882             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
883         }
884         break;
885     case CHAR_INFO_MODE_TEXTATTR:
886         {
887             const char_info_t *ptr = data;
888             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
889         }
890         break;
891     case CHAR_INFO_MODE_TEXTSTDATTR:
892         {
893             const WCHAR *ptr = data;
894             for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
895             {
896                 dest->ch   = ptr[i];
897                 dest->attr = screen_buffer->attr;
898             }
899         }
900         break;
901     default:
902         set_error( STATUS_INVALID_PARAMETER );
903         return 0;
904     }
905
906     if (i && screen_buffer == screen_buffer->input->active)
907     {
908         struct console_renderer_event evt;
909         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
910         evt.u.update.top    = y;
911         evt.u.update.bottom = (y * screen_buffer->width + x + i - 1) / screen_buffer->width;
912         console_input_events_append( screen_buffer->input->evt, &evt );
913     }
914     return i;
915 }
916
917 /* fill a screen buffer with uniform data */
918 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
919                                 enum char_info_mode mode, int x, int y, int count, int wrap )
920 {
921     int i;
922     char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
923
924     if (y >= screen_buffer->height) return 0;
925
926     if (wrap)
927         end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
928     else
929         end = screen_buffer->data + (y+1) * screen_buffer->width;
930
931     if (count > end - dest) count = end - dest;
932
933     switch(mode)
934     {
935     case CHAR_INFO_MODE_TEXT:
936         for (i = 0; i < count; i++) dest[i].ch = data.ch;
937         break;
938     case CHAR_INFO_MODE_ATTR:
939         for (i = 0; i < count; i++) dest[i].attr = data.attr;
940         break;
941     case CHAR_INFO_MODE_TEXTATTR:
942         for (i = 0; i < count; i++) dest[i] = data;
943         break;
944     case CHAR_INFO_MODE_TEXTSTDATTR:
945         for (i = 0; i < count; i++)
946         {
947             dest[i].ch   = data.ch;
948             dest[i].attr = screen_buffer->attr;
949         }
950         break;
951     default:
952         set_error( STATUS_INVALID_PARAMETER );
953         return 0;
954     }
955
956     if (count && screen_buffer == screen_buffer->input->active)
957     {
958         struct console_renderer_event evt;
959         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
960         evt.u.update.top    = y;
961         evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
962         console_input_events_append( screen_buffer->input->evt, &evt );
963     }
964     return i;
965 }
966
967 /* read data from a screen buffer */
968 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
969                                  enum char_info_mode mode, int wrap )
970 {
971     int i;
972     char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
973
974     if (y >= screen_buffer->height) return;
975
976     if (wrap)
977         end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
978     else
979         end = screen_buffer->data + (y+1) * screen_buffer->width;
980
981     switch(mode)
982     {
983     case CHAR_INFO_MODE_TEXT:
984         {
985             WCHAR *data;
986             int count = min( end - src, get_reply_max_size() / sizeof(*data) );
987             if ((data = set_reply_data_size( count * sizeof(*data) )))
988             {
989                 for (i = 0; i < count; i++) data[i] = src[i].ch;
990             }
991         }
992         break;
993     case CHAR_INFO_MODE_ATTR:
994         {
995             unsigned short *data;
996             int count = min( end - src, get_reply_max_size() / sizeof(*data) );
997             if ((data = set_reply_data_size( count * sizeof(*data) )))
998             {
999                 for (i = 0; i < count; i++) data[i] = src[i].attr;
1000             }
1001         }
1002         break;
1003     case CHAR_INFO_MODE_TEXTATTR:
1004         {
1005             char_info_t *data;
1006             int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1007             if ((data = set_reply_data_size( count * sizeof(*data) )))
1008             {
1009                 for (i = 0; i < count; i++) data[i] = src[i];
1010             }
1011         }
1012         break;
1013     default:
1014         set_error( STATUS_INVALID_PARAMETER );
1015         break;
1016     }
1017 }
1018
1019 /* scroll parts of a screen buffer */
1020 static void scroll_console_output( handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1021                                    int w, int h )
1022 {
1023     struct screen_buffer *screen_buffer;
1024     int                         j;
1025     char_info_t *psrc, *pdst;
1026     struct console_renderer_event evt;
1027
1028     if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1029                                                                   GENERIC_READ, &screen_buffer_ops )))
1030         return;
1031     if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1032         xsrc + w > screen_buffer->width  ||
1033         xdst + w > screen_buffer->width  ||
1034         ysrc + h > screen_buffer->height ||
1035         ydst + h > screen_buffer->height ||
1036         w == 0 || h == 0)
1037     {
1038         set_error( STATUS_INVALID_PARAMETER );
1039         release_object( screen_buffer );
1040         return;
1041     }
1042
1043     if (ysrc < ydst)
1044     {
1045         psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1046         pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1047
1048         for (j = h; j > 0; j--)
1049         {
1050             memcpy(pdst, psrc, w * sizeof(*pdst) );
1051             pdst -= screen_buffer->width;
1052             psrc -= screen_buffer->width;
1053         }
1054     }
1055     else
1056     {
1057         psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1058         pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1059
1060         for (j = 0; j < h; j++)
1061         {
1062             /* we use memmove here because when psrc and pdst are the same, 
1063              * copies are done on the same row, so the dst and src blocks
1064              * can overlap */
1065             memmove( pdst, psrc, w * sizeof(*pdst) );
1066             pdst += screen_buffer->width;
1067             psrc += screen_buffer->width;
1068         }
1069     }
1070
1071     /* FIXME: this could be enhanced, by signalling scroll */
1072     evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1073     evt.u.update.top    = min(ysrc, ydst);
1074     evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1075     console_input_events_append( screen_buffer->input->evt, &evt );
1076
1077     release_object( screen_buffer );
1078 }
1079
1080 /* allocate a console for the renderer */
1081 DECL_HANDLER(alloc_console)
1082 {
1083     handle_t in = 0;
1084     handle_t evt = 0;
1085     struct process *process;
1086     struct process *renderer = current->process;
1087     struct console_input *console;
1088
1089     process = (req->pid) ? get_process_from_id( req->pid ) :
1090               (struct process *)grab_object( renderer->parent );
1091
1092     reply->handle_in = 0;
1093     reply->event = 0;
1094     if (!process) return;
1095     if (process != renderer && process->console)
1096     {
1097         set_error( STATUS_ACCESS_DENIED );
1098         goto the_end;
1099     }
1100
1101     if ((console = (struct console_input*)create_console_input( current )))
1102     {
1103         if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
1104         {
1105             if ((evt = alloc_handle( renderer, console->evt,
1106                                      SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
1107             {
1108                 if (process != renderer)
1109                 {
1110                     process->console = (struct console_input*)grab_object( console );
1111                     console->num_proc++;
1112                 }
1113                 reply->handle_in = in;
1114                 reply->event = evt;
1115                 release_object( console );
1116                 goto the_end;
1117             }
1118             close_handle( renderer, in, NULL );
1119         }
1120         free_console( process );
1121     }
1122  the_end:
1123     release_object( process );
1124 }
1125
1126 /* free the console of the current process */
1127 DECL_HANDLER(free_console)
1128 {
1129     free_console( current->process );
1130 }
1131
1132 /* let the renderer peek the events it's waiting on */
1133 DECL_HANDLER(get_console_renderer_events)
1134 {
1135     struct console_input_events *evt;
1136
1137     evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1138                                                          GENERIC_WRITE, &console_input_events_ops );
1139     if (!evt) return;
1140     console_input_events_get( evt );
1141     release_object( evt );
1142 }
1143
1144 /* open a handle to the process console */
1145 DECL_HANDLER(open_console)
1146 {
1147     struct object      *obj = NULL;
1148
1149     reply->handle = 0;
1150     switch (req->from)
1151     {
1152     case 0:
1153         if (current->process->console && current->process->console->renderer)
1154             obj = grab_object( (struct object*)current->process->console );
1155         break;
1156     case 1:
1157          if (current->process->console && current->process->console->renderer &&
1158              current->process->console->active)
1159              obj = grab_object( (struct object*)current->process->console->active );
1160         break;
1161     default:
1162         if ((obj = get_handle_obj( current->process, (handle_t)req->from,
1163                                    GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1164         {
1165             struct console_input* console = (struct console_input*)obj;
1166             obj = (console->active) ? grab_object( console->active ) : NULL;
1167             release_object( console );
1168         }
1169         break;
1170     }
1171
1172     /* FIXME: req->share is not used (as in screen buffer creation)  */
1173     if (obj)
1174     {
1175         reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1176         release_object( obj );
1177     }
1178     else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1179 }
1180
1181 /* set info about a console input */
1182 DECL_HANDLER(set_console_input_info)
1183 {
1184     set_console_input_info( req, get_req_data(), get_req_data_size() );
1185 }
1186
1187 /* get info about a console (output only) */
1188 DECL_HANDLER(get_console_input_info)
1189 {
1190     struct console_input *console;
1191
1192     if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1193     if (console->title)
1194     {
1195         size_t len = strlenW( console->title ) * sizeof(WCHAR);
1196         if (len > get_reply_max_size()) len = get_reply_max_size();
1197         set_reply_data( console->title, len );
1198     }
1199     reply->history_mode  = console->history_mode;
1200     reply->history_size  = console->history_size;
1201     reply->history_index = console->history_index;
1202     release_object( console );
1203 }
1204
1205 /* get a console mode (input or output) */
1206 DECL_HANDLER(get_console_mode)
1207 {
1208     reply->mode = get_console_mode( req->handle );
1209 }
1210
1211 /* set a console mode (input or output) */
1212 DECL_HANDLER(set_console_mode)
1213 {
1214     set_console_mode( req->handle, req->mode );
1215 }
1216
1217 /* add input records to a console input queue */
1218 DECL_HANDLER(write_console_input)
1219 {
1220     struct console_input *console;
1221
1222     reply->written = 0;
1223     if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1224                                                             GENERIC_WRITE, &console_input_ops )))
1225         return;
1226     reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1227                                           get_req_data() );
1228     release_object( console );
1229 }
1230
1231 /* fetch input records from a console input queue */
1232 DECL_HANDLER(read_console_input)
1233 {
1234     int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1235     reply->read = read_console_input( req->handle, count, req->flush );
1236 }
1237
1238 /* appends a string to console's history */
1239 DECL_HANDLER(append_console_input_history)
1240 {
1241     struct console_input *console;
1242
1243     if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1244     console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1245     release_object( console );
1246 }
1247
1248 /* appends a string to console's history */
1249 DECL_HANDLER(get_console_input_history)
1250 {
1251     struct console_input *console;
1252
1253     if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1254     reply->total = console_input_get_hist( console, req->index );
1255     release_object( console );
1256 }
1257
1258 /* creates a screen buffer */
1259 DECL_HANDLER(create_console_output)
1260 {
1261     struct console_input*       console;
1262     struct screen_buffer*       screen_buffer;
1263
1264     if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1265
1266     screen_buffer = create_console_output( console );
1267     if (screen_buffer)
1268     {
1269         /* FIXME: should store sharing and test it when opening the CONOUT$ device 
1270          * see file.c on how this could be done */
1271         reply->handle_out = alloc_handle( current->process, screen_buffer,
1272                                           req->access, req->inherit );
1273         release_object( screen_buffer );
1274     }
1275     release_object( console );
1276 }
1277
1278 /* set info about a console screen buffer */
1279 DECL_HANDLER(set_console_output_info)
1280 {
1281     struct screen_buffer *screen_buffer;
1282
1283     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1284                                                                 GENERIC_WRITE, &screen_buffer_ops)))
1285     {
1286         set_console_output_info( screen_buffer, req );
1287         release_object( screen_buffer );
1288     }
1289 }
1290
1291 /* get info about a console screen buffer */
1292 DECL_HANDLER(get_console_output_info)
1293 {
1294     struct screen_buffer *screen_buffer;
1295
1296     if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1297                                                                  GENERIC_READ, &screen_buffer_ops)))
1298     {
1299         reply->cursor_size    = screen_buffer->cursor_size;
1300         reply->cursor_visible = screen_buffer->cursor_visible;
1301         reply->cursor_x       = screen_buffer->cursor_x;
1302         reply->cursor_y       = screen_buffer->cursor_y;
1303         reply->width          = screen_buffer->width;
1304         reply->height         = screen_buffer->height;
1305         reply->attr           = screen_buffer->attr;
1306         reply->win_left       = screen_buffer->win.left;
1307         reply->win_top        = screen_buffer->win.top;
1308         reply->win_right      = screen_buffer->win.right;
1309         reply->win_bottom     = screen_buffer->win.bottom;
1310         reply->max_width      = screen_buffer->max_width;
1311         reply->max_height     = screen_buffer->max_height;
1312         release_object( screen_buffer );
1313     }
1314 }
1315
1316 /* read data (chars & attrs) from a screen buffer */
1317 DECL_HANDLER(read_console_output)
1318 {
1319     struct screen_buffer *screen_buffer;
1320
1321     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1322                                                                 GENERIC_READ, &screen_buffer_ops )))
1323     {
1324         read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1325         reply->width  = screen_buffer->width;
1326         reply->height = screen_buffer->height;
1327         release_object( screen_buffer );
1328     }
1329 }
1330
1331 /* write data (char and/or attrs) to a screen buffer */
1332 DECL_HANDLER(write_console_output)
1333 {
1334     struct screen_buffer *screen_buffer;
1335
1336     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1337                                                                 GENERIC_WRITE, &screen_buffer_ops)))
1338     {
1339         reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1340                                                req->mode, req->x, req->y, req->wrap );
1341         reply->width  = screen_buffer->width;
1342         reply->height = screen_buffer->height;
1343         release_object( screen_buffer );
1344     }
1345 }
1346
1347 /* fill a screen buffer with constant data (chars and/or attributes) */
1348 DECL_HANDLER(fill_console_output)
1349 {
1350     struct screen_buffer *screen_buffer;
1351
1352     if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1353                                                                 GENERIC_WRITE, &screen_buffer_ops)))
1354     {
1355         reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1356                                               req->x, req->y, req->count, req->wrap );
1357         release_object( screen_buffer );
1358     }
1359 }
1360
1361 /* move a rect of data in a screen buffer */
1362 DECL_HANDLER(move_console_output)
1363 {
1364     scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst, 
1365                            req->w, req->h );
1366 }