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