2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
23 static void console_input_dump( struct object *obj, int verbose );
24 static void console_input_destroy( struct object *obj );
25 static int console_input_signaled( struct object *obj, struct thread *thread );
28 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply );
30 static const struct object_ops console_input_ops =
32 sizeof(struct console_input), /* size */
33 console_input_dump, /* dump */
34 add_queue, /* add_queue */
35 remove_queue, /* remove_queue */
36 console_input_signaled, /* signaled */
37 no_satisfied, /* satisfied */
38 NULL, /* get_poll_events */
39 NULL, /* poll_event */
40 no_get_fd, /* get_fd */
42 console_get_file_info, /* get_file_info */
43 console_input_destroy /* destroy */
46 static void console_input_events_dump( struct object *obj, int verbose );
47 static void console_input_events_destroy( struct object *obj );
48 static int console_input_events_signaled( struct object *obj, struct thread *thread );
50 struct console_input_events
52 struct object obj; /* object header */
53 int num_alloc; /* number of allocated events */
54 int num_used; /* number of actually used events */
55 struct console_renderer_event* events;
58 static const struct object_ops console_input_events_ops =
60 sizeof(struct console_input_events), /* size */
61 console_input_events_dump, /* dump */
62 add_queue, /* add_queue */
63 remove_queue, /* remove_queue */
64 console_input_events_signaled, /* signaled */
65 no_satisfied, /* satisfied */
66 NULL, /* get_poll_events */
67 NULL, /* poll_event */
68 no_get_fd, /* get_fd */
70 no_get_file_info, /* get_file_info */
71 console_input_events_destroy /* destroy */
76 struct object obj; /* object header */
77 struct screen_buffer *next; /* linked list of all screen buffers */
78 struct screen_buffer *prev;
79 struct console_input *input; /* associated console input */
80 int mode; /* output mode */
81 int cursor_size; /* size of cursor (percentage filled) */
82 int cursor_visible;/* cursor visibility flag */
83 int cursor_x; /* position of cursor */
84 int cursor_y; /* position of cursor */
85 int width; /* size (w-h) of the screen buffer */
87 int max_width; /* size (w-h) of the window given font size */
89 char_info_t *data; /* the data for each cell - a width x height matrix */
90 unsigned short attr; /* default attribute for screen buffer */
91 rectangle_t win; /* current visible window on the screen buffer *
92 * as seen in wineconsole */
95 static void screen_buffer_dump( struct object *obj, int verbose );
96 static void screen_buffer_destroy( struct object *obj );
98 static const struct object_ops screen_buffer_ops =
100 sizeof(struct screen_buffer), /* size */
101 screen_buffer_dump, /* dump */
102 no_add_queue, /* add_queue */
103 NULL, /* remove_queue */
105 NULL, /* satisfied */
106 NULL, /* get_poll_events */
107 NULL, /* poll_event */
108 no_get_fd, /* get_fd */
109 no_flush, /* flush */
110 console_get_file_info, /* get_file_info */
111 screen_buffer_destroy /* destroy */
114 static struct screen_buffer *screen_buffer_list;
116 static const char_info_t empty_char_info = { ' ', 0x00f0 }; /* white on black space */
118 /* dumps the renderer events of a console */
119 static void console_input_events_dump( struct object *obj, int verbose )
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 );
127 /* destroys the renderer events of a console */
128 static void console_input_events_destroy( struct object *obj )
130 struct console_input_events *evts = (struct console_input_events *)obj;
131 assert( obj->ops == &console_input_events_ops );
132 free( evts->events );
135 /* the renderer events list is signaled when it's not empty */
136 static int console_input_events_signaled( struct object *obj, struct thread *thread )
138 struct console_input_events *evts = (struct console_input_events *)obj;
139 assert( obj->ops == &console_input_events_ops );
140 return (evts->num_used != 0);
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)
147 /* to be done even when the renderer generates the events ? */
148 if (evts->num_used == evts->num_alloc)
150 evts->num_alloc += 16;
151 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
152 assert(evts->events);
154 evts->events[evts->num_used++] = *evt;
155 wake_up( &evts->obj, 0 );
158 /* retrieves events from the console's renderer events list */
159 static void console_input_events_get( struct console_input_events* evts )
161 size_t num = get_reply_max_size() / sizeof(evts->events[0]);
163 if (num > evts->num_used) num = evts->num_used;
164 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
165 if (num < evts->num_used)
167 memmove( &evts->events[0], &evts->events[num],
168 (evts->num_used - num) * sizeof(evts->events[0]) );
170 evts->num_used -= num;
173 static struct console_input_events *create_console_input_events(void)
175 struct console_input_events* evt;
177 if (!(evt = alloc_object( &console_input_events_ops, -1 ))) return NULL;
178 evt->num_alloc = evt->num_used = 0;
183 static struct object *create_console_input( struct thread* renderer )
185 struct console_input *console_input;
187 if (!(console_input = alloc_object( &console_input_ops, -1 ))) return NULL;
188 console_input->renderer = renderer;
189 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
190 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
191 console_input->num_proc = 0;
192 console_input->active = NULL;
193 console_input->recnum = 0;
194 console_input->records = NULL;
195 console_input->evt = create_console_input_events();
196 console_input->title = NULL;
197 console_input->history_size = 50;
198 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
199 console_input->history_index = 0;
200 console_input->history_mode = 0;
202 if (!console_input->history || !console_input->evt)
204 release_object( console_input );
207 return &console_input->obj;
210 static struct screen_buffer *create_console_output( struct console_input *console_input )
212 struct screen_buffer *screen_buffer;
213 struct console_renderer_event evt;
216 if (!(screen_buffer = alloc_object( &screen_buffer_ops, -1 ))) return NULL;
217 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
218 screen_buffer->input = console_input;
219 screen_buffer->cursor_size = 100;
220 screen_buffer->cursor_visible = 1;
221 screen_buffer->width = 80;
222 screen_buffer->height = 150;
223 screen_buffer->max_width = 80;
224 screen_buffer->max_height = 25;
225 screen_buffer->cursor_x = 0;
226 screen_buffer->cursor_y = 0;
227 screen_buffer->attr = 0xF0;
228 screen_buffer->win.left = 0;
229 screen_buffer->win.right = screen_buffer->max_width - 1;
230 screen_buffer->win.top = 0;
231 screen_buffer->win.bottom = screen_buffer->max_height - 1;
233 if ((screen_buffer->next = screen_buffer_list)) screen_buffer->next->prev = screen_buffer;
234 screen_buffer->prev = NULL;
235 screen_buffer_list = screen_buffer;
237 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
238 sizeof(*screen_buffer->data) )))
240 release_object( screen_buffer );
243 /* clear the first row */
244 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
245 /* and copy it to all other rows */
246 for (i = 1; i < screen_buffer->height; i++)
247 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
248 screen_buffer->width * sizeof(char_info_t) );
250 if (!console_input->active)
252 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
254 /* generate the fist events */
255 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
256 console_input_events_append( console_input->evt, &evt );
258 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
259 evt.u.resize.width = screen_buffer->width;
260 evt.u.resize.height = screen_buffer->height;
261 console_input_events_append( console_input->evt, &evt );
263 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
264 evt.u.display.left = screen_buffer->win.left;
265 evt.u.display.top = screen_buffer->win.top;
266 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
267 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
268 console_input_events_append( console_input->evt, &evt );
270 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
271 evt.u.update.top = 0;
272 evt.u.update.bottom = screen_buffer->height - 1;
273 console_input_events_append( console_input->evt, &evt );
275 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
276 evt.u.cursor_geom.size = screen_buffer->cursor_size;
277 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
278 console_input_events_append( console_input->evt, &evt );
280 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
281 evt.u.cursor_pos.x = screen_buffer->cursor_x;
282 evt.u.cursor_pos.y = screen_buffer->cursor_y;
283 console_input_events_append( console_input->evt, &evt );
285 return screen_buffer;
288 /* free the console for this process */
289 int free_console( struct process *process )
291 struct console_input* console = process->console;
293 if (!console || !console->renderer) return 0;
295 process->console = NULL;
296 if (--console->num_proc == 0)
298 /* all processes have terminated... tell the renderer to terminate too */
299 struct console_renderer_event evt;
300 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
301 console_input_events_append( console->evt, &evt );
303 release_object( console );
308 /* let process inherit the console from parent... this handle two cases :
309 * 1/ generic console inheritance
310 * 2/ parent is a renderer which launches process, and process should attach to the console
311 * renderered by parent
313 void inherit_console(struct thread *parent_thread, struct process *process, handle_t hconin)
316 struct process* parent = parent_thread->process;
318 /* if parent is a renderer, then attach current process to its console
323 struct console_input* console;
325 if ((console = (struct console_input*)get_handle_obj( parent, hconin, 0, NULL )))
327 if (console->renderer == parent_thread)
329 process->console = (struct console_input*)grab_object( console );
330 process->console->num_proc++;
333 release_object( console );
336 /* otherwise, if parent has a console, attach child to this console */
337 if (!done && parent->console)
339 assert(parent->console->renderer);
340 process->console = (struct console_input*)grab_object( parent->console );
341 process->console->num_proc++;
345 static struct console_input* console_input_get( handle_t handle, unsigned access )
347 struct console_input* console = 0;
350 console = (struct console_input *)get_handle_obj( current->process, handle,
351 access, &console_input_ops );
352 else if (current->process->console)
354 assert( current->process->console->renderer );
355 console = (struct console_input *)grab_object( current->process->console );
358 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
362 /* check if a console input is signaled: yes if non read input records */
363 static int console_input_signaled( struct object *obj, struct thread *thread )
365 struct console_input *console = (struct console_input *)obj;
366 assert( obj->ops == &console_input_ops );
367 return console->recnum ? 1 : 0;
370 static int get_console_mode( handle_t handle )
375 if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
377 if (obj->ops == &console_input_ops)
378 ret = ((struct console_input *)obj)->mode;
379 else if (obj->ops == &screen_buffer_ops)
380 ret = ((struct screen_buffer *)obj)->mode;
382 set_error( STATUS_OBJECT_TYPE_MISMATCH );
383 release_object( obj );
388 /* changes the mode of either a console input or a screen buffer */
389 static int set_console_mode( handle_t handle, int mode )
394 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
396 if (obj->ops == &console_input_ops)
398 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
399 ((struct console_input *)obj)->mode = mode;
402 else if (obj->ops == &screen_buffer_ops)
404 ((struct screen_buffer *)obj)->mode = mode;
407 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
408 release_object( obj );
412 /* add input events to a console input queue */
413 static int write_console_input( struct console_input* console, int count,
414 const INPUT_RECORD *records )
416 INPUT_RECORD *new_rec;
418 if (!count) return 0;
419 if (!(new_rec = realloc( console->records,
420 (console->recnum + count) * sizeof(INPUT_RECORD) )))
422 set_error( STATUS_NO_MEMORY );
423 release_object( console );
426 console->records = new_rec;
427 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
428 console->recnum += count;
430 /* wake up all waiters */
431 wake_up( &console->obj, 0 );
435 /* retrieve a pointer to the console input records */
436 static int read_console_input( handle_t handle, int count, int flush )
438 struct console_input *console;
440 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
441 GENERIC_READ, &console_input_ops )))
446 /* special case: do not retrieve anything, but return
447 * the total number of records available */
448 count = console->recnum;
452 if (count > console->recnum) count = console->recnum;
453 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
458 for (i = count; i < console->recnum; i++)
459 console->records[i-count] = console->records[i];
460 if ((console->recnum -= count) > 0)
462 INPUT_RECORD *new_rec = realloc( console->records,
463 console->recnum * sizeof(INPUT_RECORD) );
464 if (new_rec) console->records = new_rec;
468 free( console->records );
469 console->records = NULL;
472 release_object( console );
476 /* set misc console input information */
477 static int set_console_input_info( const struct set_console_input_info_request *req,
478 const WCHAR *title, size_t len )
480 struct console_input *console;
481 struct console_renderer_event evt;
483 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) goto error;
485 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
487 struct screen_buffer *screen_buffer;
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)
493 set_error( STATUS_INVALID_PARAMETER );
494 if (screen_buffer) release_object( screen_buffer );
498 if (screen_buffer != console->active)
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 );
506 release_object( screen_buffer );
508 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
510 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
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;
519 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
521 console->history_mode = req->history_mode;
523 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
524 console->history_size != req->history_size)
530 if (req->history_size)
532 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
533 if (!mem) goto error;
534 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
537 delta = (console->history_index > req->history_size) ?
538 (console->history_index - req->history_size) : 0;
540 for (i = delta; i < console->history_index; i++)
542 mem[i - delta] = console->history[i];
543 console->history[i] = NULL;
545 console->history_index -= delta;
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;
553 release_object( console );
556 if (console) release_object( console );
560 /* resize a screen buffer */
561 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
562 int new_width, int new_height )
564 int i, old_width, old_height, copy_width, copy_height;
565 char_info_t *new_data;
567 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
569 set_error( STATUS_NO_MEMORY );
572 old_width = screen_buffer->width;
573 old_height = screen_buffer->height;
574 copy_width = min( old_width, new_width );
575 copy_height = min( old_height, new_height );
577 /* copy all the rows */
578 for (i = 0; i < copy_height; i++)
580 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
581 copy_width * sizeof(char_info_t) );
584 /* clear the end of each row */
585 if (new_width > old_width)
588 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
589 /* and blast it to the other rows */
590 for (i = 1; i < copy_height; i++)
591 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
592 (new_width - old_width) * sizeof(char_info_t) );
595 /* clear remaining rows */
596 if (new_height > old_height)
599 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
600 /* and blast it to the other rows */
601 for (i = old_height+1; i < new_height; i++)
602 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
603 new_width * sizeof(char_info_t) );
605 free( screen_buffer->data );
606 screen_buffer->data = new_data;
607 screen_buffer->width = new_width;
608 screen_buffer->height = new_height;
612 /* set misc screen buffer information */
613 static int set_console_output_info( struct screen_buffer *screen_buffer,
614 const struct set_console_output_info_request *req )
616 struct console_renderer_event evt;
618 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
620 if (req->cursor_size < 1 || req->cursor_size > 100)
622 set_error( STATUS_INVALID_PARAMETER );
625 if (screen_buffer->cursor_size != req->cursor_size ||
626 screen_buffer->cursor_visible != req->cursor_visible)
628 screen_buffer->cursor_size = req->cursor_size;
629 screen_buffer->cursor_visible = req->cursor_visible;
630 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
631 evt.u.cursor_geom.size = req->cursor_size;
632 evt.u.cursor_geom.visible = req->cursor_visible;
633 console_input_events_append( screen_buffer->input->evt, &evt );
636 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
638 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
639 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
641 set_error( STATUS_INVALID_PARAMETER );
644 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
646 screen_buffer->cursor_x = req->cursor_x;
647 screen_buffer->cursor_y = req->cursor_y;
648 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
649 evt.u.cursor_pos.x = req->cursor_x;
650 evt.u.cursor_pos.y = req->cursor_y;
651 console_input_events_append( screen_buffer->input->evt, &evt );
654 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
656 /* FIXME: there are also some basic minimum and max size to deal with */
657 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
659 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
660 evt.u.resize.width = req->width;
661 evt.u.resize.height = req->height;
662 console_input_events_append( screen_buffer->input->evt, &evt );
664 if (screen_buffer == screen_buffer->input->active &&
665 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
668 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
669 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
670 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
671 write_console_input( screen_buffer->input, 1, &ir );
674 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
676 screen_buffer->attr = req->attr;
678 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
680 if (req->win_left < 0 || req->win_left > req->win_right ||
681 req->win_right >= screen_buffer->width ||
682 req->win_top < 0 || req->win_top > req->win_bottom ||
683 req->win_bottom >= screen_buffer->height)
685 set_error( STATUS_INVALID_PARAMETER );
688 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
689 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
691 screen_buffer->win.left = req->win_left;
692 screen_buffer->win.top = req->win_top;
693 screen_buffer->win.right = req->win_right;
694 screen_buffer->win.bottom = req->win_bottom;
695 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
696 evt.u.display.left = req->win_left;
697 evt.u.display.top = req->win_top;
698 evt.u.display.width = req->win_right - req->win_left + 1;
699 evt.u.display.height = req->win_bottom - req->win_top + 1;
700 console_input_events_append( screen_buffer->input->evt, &evt );
703 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
705 /* can only be done by renderer */
706 if (current->process->console != screen_buffer->input)
708 set_error( STATUS_INVALID_PARAMETER );
712 screen_buffer->max_width = req->max_width;
713 screen_buffer->max_height = req->max_height;
719 /* appends a new line to history (history is a fixed size array) */
720 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
722 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
726 set_error( STATUS_NO_MEMORY );
729 if (!console || !console->history_size)
731 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
735 memcpy( ptr, buf, len * sizeof(WCHAR) );
738 if (console->history_mode && console->history_index &&
739 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
741 /* ok, mode ask us to not use twice the same string...
742 * so just free mem and returns
744 set_error( STATUS_ALIAS_EXISTS );
749 if (console->history_index < console->history_size)
751 console->history[console->history_index++] = ptr;
755 free( console->history[0]) ;
756 memmove( &console->history[0], &console->history[1],
757 (console->history_size - 1) * sizeof(WCHAR*) );
758 console->history[console->history_size - 1] = ptr;
762 /* returns a line from the cachde */
763 static size_t console_input_get_hist( struct console_input *console, int index )
767 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
770 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
771 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
777 static void console_input_dump( struct object *obj, int verbose )
779 struct console_input *console = (struct console_input *)obj;
780 assert( obj->ops == &console_input_ops );
781 fprintf( stderr, "Console input active=%p evt=%p\n",
782 console->active, console->evt );
785 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply )
789 reply->type = FILE_TYPE_CHAR;
791 reply->access_time = 0;
792 reply->write_time = 0;
793 reply->size_high = 0;
796 reply->index_high = 0;
797 reply->index_low = 0;
800 return FD_TYPE_CONSOLE;
803 static void console_input_destroy( struct object *obj )
805 struct console_input* console_in = (struct console_input *)obj;
806 struct screen_buffer* curr;
809 assert( obj->ops == &console_input_ops );
810 if (console_in->title) free( console_in->title );
811 if (console_in->records) free( console_in->records );
813 if (console_in->active) release_object( console_in->active );
814 console_in->active = NULL;
816 for (curr = screen_buffer_list; curr; curr = curr->next)
818 if (curr->input == console_in) curr->input = NULL;
821 release_object( console_in->evt );
822 console_in->evt = NULL;
824 for (i = 0; i < console_in->history_size; i++)
825 if (console_in->history[i]) free( console_in->history[i] );
826 if (console_in->history) free( console_in->history );
829 static void screen_buffer_dump( struct object *obj, int verbose )
831 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
832 assert( obj->ops == &screen_buffer_ops );
834 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
837 static void screen_buffer_destroy( struct object *obj )
839 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
841 assert( obj->ops == &screen_buffer_ops );
843 if (screen_buffer->next) screen_buffer->next->prev = screen_buffer->prev;
844 if (screen_buffer->prev) screen_buffer->prev->next = screen_buffer->next;
845 else screen_buffer_list = screen_buffer->next;
847 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
849 struct screen_buffer* sb;
850 for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
851 screen_buffer->input->active = sb;
853 if (screen_buffer->data) free( screen_buffer->data );
856 /* write data into a screen buffer */
857 static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
858 const void* data, enum char_info_mode mode,
859 int x, int y, int wrap )
862 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
864 if (y >= screen_buffer->height) return 0;
867 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
869 end = screen_buffer->data + (y+1) * screen_buffer->width;
873 case CHAR_INFO_MODE_TEXT:
875 const WCHAR *ptr = data;
876 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
879 case CHAR_INFO_MODE_ATTR:
881 const unsigned short *ptr = data;
882 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
885 case CHAR_INFO_MODE_TEXTATTR:
887 const char_info_t *ptr = data;
888 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
891 case CHAR_INFO_MODE_TEXTSTDATTR:
893 const WCHAR *ptr = data;
894 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
897 dest->attr = screen_buffer->attr;
902 set_error( STATUS_INVALID_PARAMETER );
906 if (i && screen_buffer == screen_buffer->input->active)
908 struct console_renderer_event evt;
909 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
910 evt.u.update.top = y;
911 evt.u.update.bottom = (y * screen_buffer->width + x + i - 1) / screen_buffer->width;
912 console_input_events_append( screen_buffer->input->evt, &evt );
917 /* fill a screen buffer with uniform data */
918 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
919 enum char_info_mode mode, int x, int y, int count, int wrap )
922 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
924 if (y >= screen_buffer->height) return 0;
927 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
929 end = screen_buffer->data + (y+1) * screen_buffer->width;
931 if (count > end - dest) count = end - dest;
935 case CHAR_INFO_MODE_TEXT:
936 for (i = 0; i < count; i++) dest[i].ch = data.ch;
938 case CHAR_INFO_MODE_ATTR:
939 for (i = 0; i < count; i++) dest[i].attr = data.attr;
941 case CHAR_INFO_MODE_TEXTATTR:
942 for (i = 0; i < count; i++) dest[i] = data;
944 case CHAR_INFO_MODE_TEXTSTDATTR:
945 for (i = 0; i < count; i++)
947 dest[i].ch = data.ch;
948 dest[i].attr = screen_buffer->attr;
952 set_error( STATUS_INVALID_PARAMETER );
956 if (count && screen_buffer == screen_buffer->input->active)
958 struct console_renderer_event evt;
959 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
960 evt.u.update.top = y;
961 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
962 console_input_events_append( screen_buffer->input->evt, &evt );
967 /* read data from a screen buffer */
968 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
969 enum char_info_mode mode, int wrap )
972 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
974 if (y >= screen_buffer->height) return;
977 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
979 end = screen_buffer->data + (y+1) * screen_buffer->width;
983 case CHAR_INFO_MODE_TEXT:
986 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
987 if ((data = set_reply_data_size( count * sizeof(*data) )))
989 for (i = 0; i < count; i++) data[i] = src[i].ch;
993 case CHAR_INFO_MODE_ATTR:
995 unsigned short *data;
996 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
997 if ((data = set_reply_data_size( count * sizeof(*data) )))
999 for (i = 0; i < count; i++) data[i] = src[i].attr;
1003 case CHAR_INFO_MODE_TEXTATTR:
1006 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1007 if ((data = set_reply_data_size( count * sizeof(*data) )))
1009 for (i = 0; i < count; i++) data[i] = src[i];
1014 set_error( STATUS_INVALID_PARAMETER );
1019 /* scroll parts of a screen buffer */
1020 static void scroll_console_output( handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1023 struct screen_buffer *screen_buffer;
1025 char_info_t *psrc, *pdst;
1026 struct console_renderer_event evt;
1028 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1029 GENERIC_READ, &screen_buffer_ops )))
1031 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1032 xsrc + w > screen_buffer->width ||
1033 xdst + w > screen_buffer->width ||
1034 ysrc + h > screen_buffer->height ||
1035 ydst + h > screen_buffer->height ||
1038 set_error( STATUS_INVALID_PARAMETER );
1039 release_object( screen_buffer );
1045 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1046 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1048 for (j = h; j > 0; j--)
1050 memcpy(pdst, psrc, w * sizeof(*pdst) );
1051 pdst -= screen_buffer->width;
1052 psrc -= screen_buffer->width;
1057 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1058 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1060 for (j = 0; j < h; j++)
1062 /* we use memmove here because when psrc and pdst are the same,
1063 * copies are done on the same row, so the dst and src blocks
1065 memmove( pdst, psrc, w * sizeof(*pdst) );
1066 pdst += screen_buffer->width;
1067 psrc += screen_buffer->width;
1071 /* FIXME: this could be enhanced, by signalling scroll */
1072 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1073 evt.u.update.top = min(ysrc, ydst);
1074 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1075 console_input_events_append( screen_buffer->input->evt, &evt );
1077 release_object( screen_buffer );
1080 /* allocate a console for the renderer */
1081 DECL_HANDLER(alloc_console)
1085 struct process *process;
1086 struct process *renderer = current->process;
1087 struct console_input *console;
1089 process = (req->pid) ? get_process_from_id( req->pid ) :
1090 (struct process *)grab_object( renderer->parent );
1092 reply->handle_in = 0;
1094 if (!process) return;
1095 if (process != renderer && process->console)
1097 set_error( STATUS_ACCESS_DENIED );
1101 if ((console = (struct console_input*)create_console_input( current )))
1103 if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
1105 if ((evt = alloc_handle( renderer, console->evt,
1106 SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
1108 if (process != renderer)
1110 process->console = (struct console_input*)grab_object( console );
1111 console->num_proc++;
1113 reply->handle_in = in;
1115 release_object( console );
1118 close_handle( renderer, in, NULL );
1120 free_console( process );
1123 release_object( process );
1126 /* free the console of the current process */
1127 DECL_HANDLER(free_console)
1129 free_console( current->process );
1132 /* let the renderer peek the events it's waiting on */
1133 DECL_HANDLER(get_console_renderer_events)
1135 struct console_input_events *evt;
1137 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1138 GENERIC_WRITE, &console_input_events_ops );
1140 console_input_events_get( evt );
1141 release_object( evt );
1144 /* open a handle to the process console */
1145 DECL_HANDLER(open_console)
1147 struct object *obj = NULL;
1153 if (current->process->console && current->process->console->renderer)
1154 obj = grab_object( (struct object*)current->process->console );
1157 if (current->process->console && current->process->console->renderer &&
1158 current->process->console->active)
1159 obj = grab_object( (struct object*)current->process->console->active );
1162 if ((obj = get_handle_obj( current->process, (handle_t)req->from,
1163 GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1165 struct console_input* console = (struct console_input*)obj;
1166 obj = (console->active) ? grab_object( console->active ) : NULL;
1167 release_object( console );
1172 /* FIXME: req->share is not used (as in screen buffer creation) */
1175 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1176 release_object( obj );
1178 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1181 /* set info about a console input */
1182 DECL_HANDLER(set_console_input_info)
1184 set_console_input_info( req, get_req_data(), get_req_data_size() );
1187 /* get info about a console (output only) */
1188 DECL_HANDLER(get_console_input_info)
1190 struct console_input *console;
1192 if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1195 size_t len = strlenW( console->title ) * sizeof(WCHAR);
1196 if (len > get_reply_max_size()) len = get_reply_max_size();
1197 set_reply_data( console->title, len );
1199 reply->history_mode = console->history_mode;
1200 reply->history_size = console->history_size;
1201 reply->history_index = console->history_index;
1202 release_object( console );
1205 /* get a console mode (input or output) */
1206 DECL_HANDLER(get_console_mode)
1208 reply->mode = get_console_mode( req->handle );
1211 /* set a console mode (input or output) */
1212 DECL_HANDLER(set_console_mode)
1214 set_console_mode( req->handle, req->mode );
1217 /* add input records to a console input queue */
1218 DECL_HANDLER(write_console_input)
1220 struct console_input *console;
1223 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1224 GENERIC_WRITE, &console_input_ops )))
1226 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1228 release_object( console );
1231 /* fetch input records from a console input queue */
1232 DECL_HANDLER(read_console_input)
1234 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1235 reply->read = read_console_input( req->handle, count, req->flush );
1238 /* appends a string to console's history */
1239 DECL_HANDLER(append_console_input_history)
1241 struct console_input *console;
1243 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1244 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1245 release_object( console );
1248 /* appends a string to console's history */
1249 DECL_HANDLER(get_console_input_history)
1251 struct console_input *console;
1253 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1254 reply->total = console_input_get_hist( console, req->index );
1255 release_object( console );
1258 /* creates a screen buffer */
1259 DECL_HANDLER(create_console_output)
1261 struct console_input* console;
1262 struct screen_buffer* screen_buffer;
1264 if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1266 screen_buffer = create_console_output( console );
1269 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1270 * see file.c on how this could be done */
1271 reply->handle_out = alloc_handle( current->process, screen_buffer,
1272 req->access, req->inherit );
1273 release_object( screen_buffer );
1275 release_object( console );
1278 /* set info about a console screen buffer */
1279 DECL_HANDLER(set_console_output_info)
1281 struct screen_buffer *screen_buffer;
1283 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1284 GENERIC_WRITE, &screen_buffer_ops)))
1286 set_console_output_info( screen_buffer, req );
1287 release_object( screen_buffer );
1291 /* get info about a console screen buffer */
1292 DECL_HANDLER(get_console_output_info)
1294 struct screen_buffer *screen_buffer;
1296 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1297 GENERIC_READ, &screen_buffer_ops)))
1299 reply->cursor_size = screen_buffer->cursor_size;
1300 reply->cursor_visible = screen_buffer->cursor_visible;
1301 reply->cursor_x = screen_buffer->cursor_x;
1302 reply->cursor_y = screen_buffer->cursor_y;
1303 reply->width = screen_buffer->width;
1304 reply->height = screen_buffer->height;
1305 reply->attr = screen_buffer->attr;
1306 reply->win_left = screen_buffer->win.left;
1307 reply->win_top = screen_buffer->win.top;
1308 reply->win_right = screen_buffer->win.right;
1309 reply->win_bottom = screen_buffer->win.bottom;
1310 reply->max_width = screen_buffer->max_width;
1311 reply->max_height = screen_buffer->max_height;
1312 release_object( screen_buffer );
1316 /* read data (chars & attrs) from a screen buffer */
1317 DECL_HANDLER(read_console_output)
1319 struct screen_buffer *screen_buffer;
1321 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1322 GENERIC_READ, &screen_buffer_ops )))
1324 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1325 reply->width = screen_buffer->width;
1326 reply->height = screen_buffer->height;
1327 release_object( screen_buffer );
1331 /* write data (char and/or attrs) to a screen buffer */
1332 DECL_HANDLER(write_console_output)
1334 struct screen_buffer *screen_buffer;
1336 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1337 GENERIC_WRITE, &screen_buffer_ops)))
1339 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1340 req->mode, req->x, req->y, req->wrap );
1341 reply->width = screen_buffer->width;
1342 reply->height = screen_buffer->height;
1343 release_object( screen_buffer );
1347 /* fill a screen buffer with constant data (chars and/or attributes) */
1348 DECL_HANDLER(fill_console_output)
1350 struct screen_buffer *screen_buffer;
1352 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1353 GENERIC_WRITE, &screen_buffer_ops)))
1355 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1356 req->x, req->y, req->count, req->wrap );
1357 release_object( screen_buffer );
1361 /* move a rect of data in a screen buffer */
1362 DECL_HANDLER(move_console_output)
1364 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,