Skip queue cleanups if queue has been destroyed already.
[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 "winnt.h"
17 #include "winbase.h"
18 #include "wincon.h"
19
20 #include "handle.h"
21 #include "process.h"
22 #include "request.h"
23 #include "unicode.h"
24 #include "console.h"
25
26
27 static void console_input_dump( struct object *obj, int verbose );
28 static void console_input_destroy( struct object *obj );
29 static int console_input_signaled( struct object *obj, struct thread *thread );
30
31 /* common routine */
32 static int console_get_file_info( struct object *obj, struct get_file_info_request *req );
33
34 static const struct object_ops console_input_ops =
35 {
36     sizeof(struct console_input),     /* size */
37     console_input_dump,               /* dump */
38     add_queue,                        /* add_queue */
39     remove_queue,                     /* remove_queue */
40     console_input_signaled,           /* signaled */
41     no_satisfied,                     /* satisfied */
42     NULL,                             /* get_poll_events */
43     NULL,                             /* poll_event */
44     no_get_fd,                        /* get_fd */
45     no_flush,                         /* flush */
46     console_get_file_info,            /* get_file_info */
47     console_input_destroy             /* destroy */
48 };
49
50 static void console_input_events_dump( struct object *obj, int verbose );
51 static void console_input_events_destroy( struct object *obj );
52 static int  console_input_events_signaled( struct object *obj, struct thread *thread );
53
54 struct console_input_events 
55 {
56     struct object         obj;         /* object header */
57     int                   num_alloc;   /* number of allocated events */
58     int                   num_used;    /* number of actually used events */
59     struct console_renderer_event*      events;
60 };
61
62 static const struct object_ops console_input_events_ops =
63 {
64     sizeof(struct console_input_events), /* size */
65     console_input_events_dump,        /* dump */
66     add_queue,                        /* add_queue */
67     remove_queue,                     /* remove_queue */
68     console_input_events_signaled,    /* signaled */
69     no_satisfied,                     /* satisfied */
70     NULL,                             /* get_poll_events */
71     NULL,                             /* poll_event */
72     no_get_fd,                        /* get_fd */
73     no_flush,                         /* flush */
74     no_get_file_info,                 /* get_file_info */
75     console_input_events_destroy      /* destroy */
76 };
77
78 struct screen_buffer
79 {
80     struct object         obj;           /* object header */
81     int                   mode;          /* output mode */
82     struct console_input *input;         /* associated console input */
83     struct screen_buffer *next;          /* linked list of all screen buffers */
84     short int             cursor_size;   /* size of cursor (percentage filled) */
85     short int             cursor_visible;/* cursor visibility flag */
86     COORD                 cursor;        /* position of cursor */
87     short int             width;         /* size (w-h) of the screen buffer */
88     short int             height;
89     short int             max_width;     /* size (w-h) of the window given font size */
90     short int             max_height;
91     unsigned             *data;          /* the data for each cell - a width x height matrix */
92     unsigned short        attr;          /* default attribute for screen buffer */
93     SMALL_RECT            win;           /* current visible window on the screen buffer *
94                                           * as seen in wineconsole */
95 };
96
97 static void screen_buffer_dump( struct object *obj, int verbose );
98 static void screen_buffer_destroy( struct object *obj );
99
100 static const struct object_ops screen_buffer_ops =
101 {
102     sizeof(struct screen_buffer),     /* size */
103     screen_buffer_dump,               /* dump */
104     no_add_queue,                     /* add_queue */
105     NULL,                             /* remove_queue */
106     NULL,                             /* signaled */
107     NULL,                             /* satisfied */
108     NULL,                             /* get_poll_events */
109     NULL,                             /* poll_event */
110     no_get_fd,                        /* get_fd */
111     no_flush,                         /* flush */
112     console_get_file_info,            /* get_file_info */
113     screen_buffer_destroy             /* destroy */
114 };
115
116 static struct screen_buffer *screen_buffer_list;
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 rendere 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 ? 1 : 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     if (!evt) return;
148
149     /* to be done even when the renderer generates the events ? */
150     if (evts->num_used == evts->num_alloc)
151     {
152         evts->num_alloc += 16;
153         evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
154         assert(evts->events);
155     }
156     evts->events[evts->num_used++] = *evt;
157     wake_up( &evts->obj, 0 );
158 }
159
160 /* retrieves events from the console's renderer events list */
161 static size_t  console_input_events_get( struct console_input_events* evts, 
162                                          struct console_renderer_event* evt, size_t num )
163 {
164     if (num % sizeof(*evt) != 0)
165     {
166         set_error( STATUS_INVALID_PARAMETER );
167         return 0;
168     }
169     num /= sizeof(*evt);
170     if (num > evts->num_used)
171         num = evts->num_used;
172     memcpy( evt, evts->events, num * sizeof(*evt) );
173     if (num < evts->num_used)
174     {
175         memmove( &evts->events[0], &evts->events[num], 
176                  (evts->num_used - num) * sizeof(*evt) );
177     }
178     evts->num_used -= num;
179     return num * sizeof(struct console_renderer_event);
180 }
181
182 static struct console_input_events *create_console_input_events(void)
183 {
184     struct console_input_events*        evt;
185
186     if (!(evt = alloc_object( &console_input_events_ops, -1 ))) return NULL;
187     evt->num_alloc = evt->num_used = 0;
188     evt->events = NULL;
189     return evt;
190 }
191
192 static struct object *create_console_input( struct process* renderer )
193 {
194     struct console_input *console_input;
195
196     if (!(console_input = alloc_object( &console_input_ops, -1 ))) return NULL;
197     console_input->renderer      = renderer;
198     console_input->mode          = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
199                                    ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
200     console_input->num_proc      = 0;
201     console_input->active        = NULL;
202     console_input->recnum        = 0;
203     console_input->records       = NULL;
204     console_input->evt           = create_console_input_events();
205     console_input->title         = NULL;
206     console_input->history_size  = 50;
207     console_input->history       = calloc( console_input->history_size, sizeof(WCHAR*) );
208     console_input->history_index = 0;
209     console_input->history_mode  = 0;
210
211     if (!console_input->history || !console_input->evt)
212     {
213         release_object( console_input );
214         return NULL;
215     }
216     return &console_input->obj;
217 }
218
219 static struct object *create_console_output( struct console_input *console_input )
220 {
221     struct screen_buffer *screen_buffer;
222     struct console_renderer_event evt;
223     int i;
224
225     if (!(screen_buffer = alloc_object( &screen_buffer_ops, -1 ))) return NULL;
226     screen_buffer->mode           = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
227     screen_buffer->input          = console_input;
228     screen_buffer->cursor_size    = 100;
229     screen_buffer->cursor_visible = 1;
230     screen_buffer->width          = 80;
231     screen_buffer->height         = 150;
232     screen_buffer->max_width      = 80;
233     screen_buffer->max_height     = 25;
234     screen_buffer->data           = malloc( 4 * screen_buffer->width * screen_buffer->height );
235     /* fill the buffer with white on black spaces */
236     for (i = 0; i < screen_buffer->width * screen_buffer->height; i++)
237     {
238         screen_buffer->data[i] = 0x00F00020;
239     }
240     screen_buffer->cursor.X       = 0;
241     screen_buffer->cursor.Y       = 0;
242     screen_buffer->attr           = 0xF0;
243     screen_buffer->win.Left       = 0;
244     screen_buffer->win.Right      = screen_buffer->max_width - 1;
245     screen_buffer->win.Top        = 0;
246     screen_buffer->win.Bottom     = screen_buffer->max_height - 1;
247
248     screen_buffer->next = screen_buffer_list;
249     screen_buffer_list = screen_buffer;
250
251     if (!console_input->active)
252     {
253         console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
254
255         /* generate the fist events */
256         evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
257         console_input_events_append( console_input->evt, &evt );
258
259         evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
260         evt.u.resize.width  = screen_buffer->width;
261         evt.u.resize.height = screen_buffer->height;
262         console_input_events_append( console_input->evt, &evt );
263
264         evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
265         evt.u.display.left   = screen_buffer->win.Left;
266         evt.u.display.top    = screen_buffer->win.Top;
267         evt.u.display.width  = screen_buffer->win.Right - screen_buffer->win.Left + 1;
268         evt.u.display.height = screen_buffer->win.Bottom - screen_buffer->win.Top + 1;
269         console_input_events_append( console_input->evt, &evt );
270
271         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
272         evt.u.update.top    = 0;
273         evt.u.update.bottom = screen_buffer->height - 1;
274         console_input_events_append( console_input->evt, &evt );
275
276         evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
277         evt.u.cursor_geom.size    = screen_buffer->cursor_size;
278         evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
279         console_input_events_append( console_input->evt, &evt );
280
281         evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
282         evt.u.cursor_pos.x = screen_buffer->cursor.X;
283         evt.u.cursor_pos.y = screen_buffer->cursor.Y;
284         console_input_events_append( console_input->evt, &evt );
285     }
286
287     return &screen_buffer->obj;
288 }
289
290 /* free the console for this process */
291 int free_console( struct process *process )
292 {
293     struct console_input* console = process->console;
294
295     if (!console || !console->renderer) return 0;
296
297     process->console = NULL;
298     if (--console->num_proc == 0)
299     {
300         /* all processes have terminated... tell the renderer to terminate too */
301         struct console_renderer_event evt;
302         evt.event = CONSOLE_RENDERER_EXIT_EVENT;
303         console_input_events_append( console->evt, &evt );
304     }
305     release_object( console );
306
307     return 1;
308 }
309
310 /* let process inherit the console from parent... this handle two cases :
311  *      1/ generic console inheritance
312  *      2/ parent is a renderer which launches process, and process should attach to the console
313  *         renderered by parent
314  */
315 void inherit_console(struct process *parent, struct process *process, handle_t hconin)
316 {
317     int done = 0;
318
319     /* if parent is a renderer, then attach current process to its console
320      * a bit hacky....
321      */
322     if (hconin)
323     {
324         struct console_input* console;
325
326         if ((console = (struct console_input*)get_handle_obj( parent, hconin, 0, NULL )))
327         {
328             if (console->renderer == parent)
329             {
330                 process->console = (struct console_input*)grab_object( console );
331                 process->console->num_proc++;
332                 done = 1;
333             }
334             release_object( console );
335         }
336     }
337     /* otherwise, if parent has a console, attach child to this console */
338     if (!done && parent->console)
339     {
340         assert(parent->console->renderer);
341         process->console = (struct console_input*)grab_object( parent->console );
342         process->console->num_proc++;
343     }
344 }
345
346 static struct console_input* console_input_get( handle_t handle, unsigned access )
347 {
348     struct console_input*       console = 0;
349
350     if (handle)
351         console = (struct console_input *)get_handle_obj( current->process, handle,
352                                                           access, &console_input_ops );
353     else if (current->process->console)
354     {
355         assert( current->process->console->renderer );
356         console = (struct console_input *)grab_object( current->process->console );
357     }
358
359     if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
360     return console;
361 }
362
363 /* check if a console input is signaled: yes if non read input records */
364 static int console_input_signaled( struct object *obj, struct thread *thread )
365 {
366     struct console_input *console = (struct console_input *)obj;
367     assert( obj->ops == &console_input_ops );
368     return console->recnum ? 1 : 0;
369 }
370
371 static int get_console_mode( handle_t handle )
372 {
373     struct object *obj;
374     int ret = 0;
375
376     if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
377     {
378         if (obj->ops == &console_input_ops)
379             ret = ((struct console_input *)obj)->mode;
380         else if (obj->ops == &screen_buffer_ops)
381             ret = ((struct screen_buffer *)obj)->mode;
382         else
383             set_error( STATUS_OBJECT_TYPE_MISMATCH );
384         release_object( obj );
385     }
386     return ret;
387 }
388
389 /* changes the mode of either a console input or a screen buffer */
390 static int set_console_mode( handle_t handle, int mode )
391 {
392     struct object *obj;
393     int ret = 0;
394
395     if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
396         return 0;
397     if (obj->ops == &console_input_ops)
398     {
399         /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
400         ((struct console_input *)obj)->mode = mode;
401         ret = 1;
402     }
403     else if (obj->ops == &screen_buffer_ops)
404     {
405         ((struct screen_buffer *)obj)->mode = mode;
406         ret = 1;
407     }
408     else set_error( STATUS_OBJECT_TYPE_MISMATCH );
409     release_object( obj );
410     return ret;
411 }
412
413 /* add input events to a console input queue */
414 static int write_console_input( struct console_input* console, int count, INPUT_RECORD *records )
415 {
416     INPUT_RECORD *new_rec;
417
418     assert(count);
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, INPUT_RECORD *rec, 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         memcpy( rec, console->records, count * sizeof(INPUT_RECORD) );
454     }
455     if (flush)
456     {
457         int i;
458         for (i = count; i < console->recnum; i++)
459             ((INPUT_RECORD*)console->records)[i-count] = ((INPUT_RECORD*)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( 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 /* set misc screen buffer information */
561 static int set_console_output_info( struct screen_buffer *screen_buffer, 
562                                     struct set_console_output_info_request *req )
563 {
564     struct console_renderer_event evt;
565
566     if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
567     {
568         if (req->cursor_size < 1 || req->cursor_size > 100)
569         {
570             set_error( STATUS_INVALID_PARAMETER );
571             return 0;
572         }
573         if (screen_buffer->cursor_size != req->cursor_size || 
574             screen_buffer->cursor_visible != req->cursor_visible)
575         {
576             screen_buffer->cursor_size    = req->cursor_size;
577             screen_buffer->cursor_visible = req->cursor_visible;
578             evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
579             evt.u.cursor_geom.size    = req->cursor_size;
580             evt.u.cursor_geom.visible = req->cursor_visible;
581             console_input_events_append( screen_buffer->input->evt, &evt );
582         }
583     }
584     if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
585     {
586         if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width || 
587             req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
588         {
589             set_error( STATUS_INVALID_PARAMETER );
590             return 0;
591         }
592         if (screen_buffer->cursor.X != req->cursor_x || screen_buffer->cursor.Y != req->cursor_y)
593         {
594             screen_buffer->cursor.X       = req->cursor_x;
595             screen_buffer->cursor.Y       = req->cursor_y;
596             evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
597             evt.u.cursor_pos.x = req->cursor_x;
598             evt.u.cursor_pos.y = req->cursor_y;
599             console_input_events_append( screen_buffer->input->evt, &evt );
600         }
601     }
602     if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
603     {
604         int             i, j;
605         /* FIXME: there are also some basic minimum and max size to deal with */
606         unsigned*       new_data = mem_alloc( 4 * req->width * req->height );
607
608         if (!new_data) return 0;
609
610         /* fill the buffer with either the old buffer content or white on black spaces */
611         for (j = 0; j < req->height; j++)
612         {
613             for (i = 0; i < req->width; i++)
614             {
615                 new_data[j * req->width + i] = 
616                     (i < screen_buffer->width && j < screen_buffer->height) ?
617                     screen_buffer->data[j * screen_buffer->width + i] : 0x00F00020;
618             }   
619         }
620         free( screen_buffer->data );
621         screen_buffer->data = new_data;
622         screen_buffer->width = req->width;
623         screen_buffer->height = req->height;
624         evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
625         evt.u.resize.width  = req->width;
626         evt.u.resize.height = req->height;
627         console_input_events_append( screen_buffer->input->evt, &evt );
628
629         if (screen_buffer == screen_buffer->input->active && 
630             screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
631         {
632             INPUT_RECORD        ir;
633             ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
634             ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
635             ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
636             write_console_input( screen_buffer->input, 1, &ir );
637         }
638     }
639     if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
640     {
641         screen_buffer->attr = req->attr;
642     }
643     if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
644     {
645         if (req->win_left < 0 || req->win_left > req->win_right || 
646             req->win_right >= screen_buffer->width ||
647             req->win_top < 0  || req->win_top > req->win_bottom || 
648             req->win_bottom >= screen_buffer->height)
649         {
650             set_error( STATUS_INVALID_PARAMETER );
651             return 0;
652         }
653         if (screen_buffer->win.Left != req->win_left || screen_buffer->win.Top != req->win_top ||
654             screen_buffer->win.Right != req->win_right || screen_buffer->win.Bottom != req->win_bottom)
655         {
656             screen_buffer->win.Left   = req->win_left;
657             screen_buffer->win.Top    = req->win_top;
658             screen_buffer->win.Right  = req->win_right;
659             screen_buffer->win.Bottom = req->win_bottom;
660             evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
661             evt.u.display.left   = req->win_left;
662             evt.u.display.top    = req->win_top;
663             evt.u.display.width  = req->win_right - req->win_left + 1;
664             evt.u.display.height = req->win_bottom - req->win_top + 1;
665             console_input_events_append( screen_buffer->input->evt, &evt );
666         }
667     }
668     if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
669     {
670         /* can only be done by renderer */
671         if (current->process->console != screen_buffer->input)
672         {
673             set_error( STATUS_INVALID_PARAMETER );
674             return 0;
675         }
676
677         screen_buffer->max_width  = req->max_width;
678         screen_buffer->max_height = req->max_height;
679     }
680
681     return 1;
682 }
683
684 /* appends a new line to history (history is a fixed size array) */
685 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
686 {
687     WCHAR*      ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
688
689     if (!ptr)
690     {
691         set_error( STATUS_NO_MEMORY );
692         return;
693     }
694     if (!console || !console->history_size)
695     {
696         set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
697         return;
698     }
699
700     memcpy( ptr, buf, len * sizeof(WCHAR) );
701     ptr[len] = 0;
702
703     if (console->history_mode && console->history_index &&
704         strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
705     {
706         /* ok, mode ask us to not use twice the same string...
707          * so just free mem and returns
708          */
709         set_error( STATUS_ALIAS_EXISTS );
710         free(ptr);
711         return;
712     }
713
714     if (console->history_index < console->history_size)
715     {
716         console->history[console->history_index++] = ptr;
717     }
718     else
719     {
720         free( console->history[0]) ;
721         memmove( &console->history[0], &console->history[1], 
722                  (console->history_size - 1) * sizeof(WCHAR*) );
723         console->history[console->history_size - 1] = ptr;
724     }
725 }
726
727 /* returns a line from the cachde */
728 static int console_input_get_hist( struct console_input* console, WCHAR* buf, size_t len, int index )
729 {
730     int ret;
731
732     /* FIXME: don't use len yet */
733     if (!console || index >= console->history_index)
734     {
735         set_error( STATUS_INVALID_PARAMETER );
736         return 0;
737     }
738     ret = strlenW(console->history[index]);
739     memcpy( buf, console->history[index], ret * sizeof(WCHAR) ); /* FIXME should use len */
740     return ret;
741 }
742
743 /* dumb dump */
744 static void console_input_dump( struct object *obj, int verbose )
745 {
746     struct console_input *console = (struct console_input *)obj;
747     assert( obj->ops == &console_input_ops );
748     fprintf( stderr, "Console input active=%p evt=%p\n", 
749              console->active, console->evt );
750 }
751
752 static int console_get_file_info( struct object *obj, struct get_file_info_request *req )
753 {
754     if (req)
755     {
756         req->type        = FILE_TYPE_CHAR;
757         req->attr        = 0;
758         req->access_time = 0;
759         req->write_time  = 0;
760         req->size_high   = 0;
761         req->size_low    = 0;
762         req->links       = 0;
763         req->index_high  = 0;
764         req->index_low   = 0;
765         req->serial      = 0;
766     }
767     return FD_TYPE_CONSOLE;
768 }
769
770 static void console_input_destroy( struct object *obj )
771 {
772     struct console_input*       console_in = (struct console_input *)obj;
773     struct screen_buffer*       curr;
774     int                         i;
775
776     assert( obj->ops == &console_input_ops );
777     if (console_in->title) free( console_in->title );
778     if (console_in->records) free( console_in->records );
779
780     if (console_in->active)     release_object( console_in->active );
781     console_in->active = NULL;
782
783     for (curr = screen_buffer_list; curr; curr = curr->next)
784     {
785         if (curr->input == console_in) curr->input = NULL;
786     }
787
788     release_object( console_in->evt );
789     console_in->evt = NULL;
790
791     for (i = 0; i < console_in->history_size; i++)
792         if (console_in->history[i]) free( console_in->history[i] );
793     if (console_in->history) free( console_in->history );
794 }
795
796 static void screen_buffer_dump( struct object *obj, int verbose )
797 {
798     struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
799     assert( obj->ops == &screen_buffer_ops );
800
801     fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
802 }
803
804 static void screen_buffer_destroy( struct object *obj )
805 {
806     struct screen_buffer*       screen_buffer = (struct screen_buffer *)obj;
807     struct screen_buffer**      psb;
808
809     assert( obj->ops == &screen_buffer_ops );
810
811     for (psb = &screen_buffer_list; *psb; *psb = (*psb)->next)
812     {
813         if (*psb == screen_buffer)
814         {
815             *psb = screen_buffer->next;
816             break;
817         }
818     }   
819     if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
820     {
821         struct screen_buffer*   sb;
822         for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
823         screen_buffer->input->active = sb;
824     }
825 }
826
827 /* write data into a screen buffer */
828 static int write_console_output( struct screen_buffer *screen_buffer, size_t size, 
829                                  const unsigned char* data, int mode, short int x, short int y )
830 {
831     int                         uniform = mode & WRITE_CONSOLE_MODE_UNIFORM;
832     unsigned                   *ptr;
833     unsigned                    i, inc;
834     int                         len;
835
836     mode &= ~WRITE_CONSOLE_MODE_UNIFORM;
837
838     if (mode < 0 || mode > 3)
839     {
840         set_error(STATUS_INVALID_PARAMETER);
841         return 0;
842     }
843
844     /* set destination pointer and increment */
845     ptr = screen_buffer->data + (y * screen_buffer->width + x);
846     if (mode == WRITE_CONSOLE_MODE_ATTR) ptr = (unsigned*)((char*)ptr + 2);
847     inc = (mode == WRITE_CONSOLE_MODE_TEXTATTR) ? 4 : 2;
848     len = size / inc;
849
850     /* crop if needed */
851     if (x + len > screen_buffer->width) len = screen_buffer->width - x;
852
853     for (i = 0; i < len; i++)
854     {
855         if (mode == WRITE_CONSOLE_MODE_TEXTSTDATTR)
856         {
857             memcpy( (char*)ptr + 2, &screen_buffer->attr, 2 );
858         }       
859         memcpy( ptr++, data, inc );
860         if (!uniform) data += inc;
861     }
862
863     if (len && screen_buffer == screen_buffer->input->active)
864     {
865         int     y2;
866         struct console_renderer_event evt;
867
868         y2 = (y * screen_buffer->width + x + len - 1) / screen_buffer->width;
869
870         evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
871         evt.u.update.top    = y;
872         evt.u.update.bottom = y2;
873         console_input_events_append( screen_buffer->input->evt, &evt );
874     }
875     return len;
876 }
877
878 /* read data from a screen buffer */
879 static int read_console_output( struct screen_buffer *screen_buffer, size_t size, void* data, 
880                                 short int x, short int y, short int w, short int h, 
881                                 short int* eff_w, short int* eff_h )
882 {
883     int j;
884
885     if (size < w * h * 4 || x >= screen_buffer->width || y >= screen_buffer->height)
886     {   
887         set_error(STATUS_INVALID_PARAMETER);
888         return 0;
889     }
890
891     *eff_w = w;
892     *eff_h = h;
893     if (x + w > screen_buffer->width)  *eff_w = screen_buffer->width  - x;
894     if (y + h > screen_buffer->height) *eff_h = screen_buffer->height - y;
895
896     for (j = 0; j < *eff_h; j++)
897     {
898         memcpy( (char*)data + 4 * j * w, &screen_buffer->data[(y + j) * screen_buffer->width + x],
899                 *eff_w * 4 );
900     }
901
902     return *eff_w * *eff_h;
903 }
904
905 /* scroll parts of a screen buffer */
906 static void scroll_console_output( handle_t handle, short int xsrc, short int ysrc, 
907                                    short int xdst, short int ydst, short int w, short int h )
908 {
909     struct screen_buffer *screen_buffer;
910     int                         j;
911     unsigned*                   psrc;
912     unsigned*                   pdst;
913     struct console_renderer_event evt;
914
915     if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
916                                                                   GENERIC_READ, &screen_buffer_ops )))
917         return;
918     if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
919         xsrc + w > screen_buffer->width  ||
920         xdst + w > screen_buffer->width  ||
921         ysrc + h > screen_buffer->height ||
922         ydst + h > screen_buffer->height ||
923         w == 0 || h == 0)
924     {
925         set_error( STATUS_INVALID_PARAMETER );
926         release_object( screen_buffer );
927         return;
928     }
929
930     if (ysrc < ydst)
931     {
932         psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
933         pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
934
935         for (j = h; j > 0; j--)
936         {
937             memcpy(pdst, psrc, w * 4);
938             pdst -= screen_buffer->width;
939             psrc -= screen_buffer->width;
940         }
941     }
942     else
943     {
944         psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
945         pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
946
947         for (j = 0; j < h; j++)
948         {
949             /* we use memmove here because when psrc and pdst are the same, 
950              * copies are done on the same row, so the dst and src blocks
951              * can overlap */
952             memmove( pdst, psrc, w * 4 );
953             pdst += screen_buffer->width;
954             psrc += screen_buffer->width;
955         }
956     }
957
958     /* FIXME: this could be enhanced, by signalling scroll */
959     evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
960     evt.u.update.top    = min(ysrc, ydst);
961     evt.u.update.bottom = max(ysrc, ydst) + h - 1;
962     console_input_events_append( screen_buffer->input->evt, &evt );
963
964     release_object( screen_buffer );
965 }
966
967 /* allocate a console for the renderer */
968 DECL_HANDLER(alloc_console)
969 {
970     handle_t in = 0;
971     handle_t evt = 0;
972     struct process *process;
973     struct process *renderer = current->process;
974     struct console_input *console;
975
976     process = (req->pid) ? get_process_from_id( req->pid ) :
977               (struct process *)grab_object( renderer->parent );
978
979     req->handle_in = 0;
980     req->event = 0;
981     if (!process) return;
982     if (process != renderer && process->console)
983     {   
984         set_error( STATUS_ACCESS_DENIED );
985         goto the_end;
986     }
987
988     if ((console = (struct console_input*)create_console_input( renderer )))
989     {
990         if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
991         {
992             if ((evt = alloc_handle( renderer, console->evt, 
993                                      SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
994             {
995                 if (process != renderer)
996                 {
997                     process->console = (struct console_input*)grab_object( console );
998                     console->num_proc++;
999                 }
1000                 req->handle_in = in;
1001                 req->event = evt;
1002                 release_object( console );
1003                 goto the_end;
1004             }
1005             close_handle( renderer, in, NULL );
1006         }
1007         free_console( process );
1008     }
1009  the_end:
1010     release_object( process );
1011 }
1012
1013 /* free the console of the current process */
1014 DECL_HANDLER(free_console)
1015 {
1016     free_console( current->process );
1017 }
1018
1019 /* let the renderer peek the events it's waiting on */
1020 DECL_HANDLER(get_console_renderer_events)
1021 {
1022     struct console_input_events*        evt;
1023     size_t len = 0;
1024
1025     evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1026                                                          GENERIC_WRITE, &console_input_events_ops );
1027     if (!evt) return;
1028     len = console_input_events_get( evt, get_req_data(req), get_req_data_size(req) );
1029     set_req_data_size( req, len );
1030     release_object( evt );
1031 }
1032
1033 /* open a handle to the process console */
1034 DECL_HANDLER(open_console)
1035 {
1036     struct object      *obj = NULL;
1037
1038     req->handle = 0;
1039     switch (req->from)
1040     {
1041     case 0: 
1042         if (current->process->console && current->process->console->renderer)
1043             obj = grab_object( (struct object*)current->process->console );
1044         break;
1045     case 1: 
1046          if (current->process->console && current->process->console->renderer && 
1047              current->process->console->active)
1048              obj = grab_object( (struct object*)current->process->console->active );
1049         break;
1050     default:
1051         if ((obj = get_handle_obj( current->process, (handle_t)req->from,
1052                                    GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1053         {
1054             struct console_input* console = (struct console_input*)obj;
1055             obj = (console->active) ? grab_object( console->active ) : NULL;
1056             release_object( console );
1057         }
1058         break;
1059     }
1060
1061     /* FIXME: req->share is not used (as in screen buffer creation)  */
1062     if (obj)
1063     {
1064         req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1065         release_object( obj );
1066     }
1067     if (!req->handle && !get_error()) set_error( STATUS_ACCESS_DENIED );
1068 }
1069
1070 /* set info about a console input */
1071 DECL_HANDLER(set_console_input_info)
1072 {
1073     set_console_input_info( req, get_req_data(req), get_req_data_size(req) );
1074 }
1075
1076 /* get info about a console (output only) */
1077 DECL_HANDLER(get_console_input_info)
1078 {
1079     struct console_input *console = 0;
1080
1081     set_req_data_size( req, 0 );
1082     if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1083
1084     if (console->title)
1085     {
1086         size_t len = strlenW( console->title ) * sizeof(WCHAR);
1087         if (len > get_req_data_size(req)) len = get_req_data_size(req);
1088         memcpy( get_req_data(req), console->title, len );
1089         set_req_data_size( req, len );
1090     }
1091     req->history_mode  = console->history_mode;
1092     req->history_size  = console->history_size;
1093     req->history_index = console->history_index;
1094
1095     release_object( console );
1096 }
1097
1098 /* get a console mode (input or output) */
1099 DECL_HANDLER(get_console_mode)
1100 {
1101     req->mode = get_console_mode( req->handle );
1102 }
1103
1104 /* set a console mode (input or output) */
1105 DECL_HANDLER(set_console_mode)
1106 {
1107     set_console_mode( req->handle, req->mode );
1108 }
1109
1110 /* add input records to a console input queue */
1111 DECL_HANDLER(write_console_input)
1112 {
1113     struct console_input *console;
1114
1115     req->written = 0;
1116     if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1117                                                             GENERIC_WRITE, &console_input_ops )))
1118         return;
1119
1120     req->written = write_console_input( console, get_req_data_size(req) / sizeof(INPUT_RECORD),
1121                                         get_req_data(req) );
1122     release_object( console );
1123 }
1124
1125 /* fetch input records from a console input queue */
1126 DECL_HANDLER(read_console_input)
1127 {
1128     size_t size = get_req_data_size(req) / sizeof(INPUT_RECORD);
1129     int res = read_console_input( req->handle, size, get_req_data(req), req->flush );
1130     /* if size was 0 we didn't fetch anything */
1131     if (size) set_req_data_size( req, res * sizeof(INPUT_RECORD) );
1132     req->read = res;
1133 }
1134
1135 /* appends a string to console's history */
1136 DECL_HANDLER(append_console_input_history)
1137 {
1138     struct console_input*       console;
1139
1140     if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1141     console_input_append_hist( console, get_req_data(req), 
1142                                get_req_data_size(req) / sizeof(WCHAR) );
1143     release_object( console );
1144 }
1145
1146 /* appends a string to console's history */
1147 DECL_HANDLER(get_console_input_history)
1148 {
1149     struct console_input*       console;
1150     int len;
1151
1152     if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1153
1154     len = console_input_get_hist( console, get_req_data(req), 0 /* FIXME */, req->index );
1155     set_req_data_size( req, len * sizeof(WCHAR));
1156     release_object( console );
1157 }
1158
1159 /* creates a screen buffer */
1160 DECL_HANDLER(create_console_output)
1161 {
1162     struct console_input*       console;
1163     struct screen_buffer*       screen_buffer;
1164
1165     if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1166
1167     screen_buffer = (struct screen_buffer*)create_console_output( console );
1168     if (screen_buffer)
1169     {
1170         /* FIXME: should store sharing and test it when opening the CONOUT$ device 
1171          * see file.c on how this could be done
1172          */
1173         req->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->inherit );
1174         release_object( screen_buffer );
1175     }
1176     release_object( console );
1177 }
1178
1179 /* set info about a console screen buffer */
1180 DECL_HANDLER(set_console_output_info)
1181 {
1182     struct screen_buffer       *screen_buffer;
1183
1184     if (!(screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1185                                                                  GENERIC_WRITE, &screen_buffer_ops )))
1186         return;
1187
1188     set_console_output_info( screen_buffer, req );
1189     release_object( screen_buffer );
1190 }
1191
1192 /* get info about a console screen buffer */
1193 DECL_HANDLER(get_console_output_info)
1194 {
1195     struct screen_buffer *screen_buffer;
1196     size_t len = 0;
1197
1198     if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1199                                                                  GENERIC_READ, &screen_buffer_ops )))
1200     {
1201         req->cursor_size    = screen_buffer->cursor_size;
1202         req->cursor_visible = screen_buffer->cursor_visible;
1203         req->cursor_x       = screen_buffer->cursor.X;
1204         req->cursor_y       = screen_buffer->cursor.Y;
1205         req->width          = screen_buffer->width;
1206         req->height         = screen_buffer->height;
1207         req->attr           = screen_buffer->attr;
1208         req->win_left       = screen_buffer->win.Left;
1209         req->win_top        = screen_buffer->win.Top;
1210         req->win_right      = screen_buffer->win.Right;
1211         req->win_bottom     = screen_buffer->win.Bottom;
1212         req->max_width      = screen_buffer->max_width;
1213         req->max_height     = screen_buffer->max_height;
1214
1215         release_object( screen_buffer );
1216     }
1217     set_req_data_size( req, len );
1218 }
1219
1220 /* read data (chars & attrs) from a screen buffer */
1221 DECL_HANDLER(read_console_output)
1222 {
1223     struct screen_buffer       *screen_buffer;
1224     size_t                      size = get_req_data_size(req);
1225     int                         res;
1226
1227     if (!(screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1228                                                                  GENERIC_READ, &screen_buffer_ops )))
1229         return;
1230
1231     res = read_console_output( screen_buffer, size, get_req_data(req), 
1232                                req->x, req->y, req->w, req->h, &req->eff_w, &req->eff_h);
1233     /* if size was 0 we didn't fetch anything */
1234     if (size) set_req_data_size( req, res * 4 );
1235     release_object( screen_buffer );
1236 }
1237
1238 /* write data (char and/or attrs) to a screen buffer */
1239 DECL_HANDLER(write_console_output)
1240 {
1241     struct screen_buffer       *screen_buffer;
1242     size_t                      size = get_req_data_size(req);
1243     int                         res;
1244
1245     if (!(screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1246                                                                  GENERIC_WRITE, &screen_buffer_ops )))
1247         return;
1248
1249     res = write_console_output( screen_buffer, size, get_req_data(req), req->mode, req->x, req->y );
1250
1251     /* if size was 0 we didn't fetch anything */
1252     if (size) set_req_data_size( req, res );
1253     req->written = res;
1254     release_object( screen_buffer );
1255 }
1256
1257 /* move a rect of data in a screen buffer */
1258 DECL_HANDLER(move_console_output)
1259 {
1260     scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst, 
1261                            req->w, req->h );
1262 }