2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
24 #include "wine/port.h"
39 static void console_input_dump( struct object *obj, int verbose );
40 static void console_input_destroy( struct object *obj );
41 static int console_input_signaled( struct object *obj, struct thread *thread );
44 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
46 static const struct object_ops console_input_ops =
48 sizeof(struct console_input), /* size */
49 console_input_dump, /* dump */
50 add_queue, /* add_queue */
51 remove_queue, /* remove_queue */
52 console_input_signaled, /* signaled */
53 no_satisfied, /* satisfied */
54 NULL, /* get_poll_events */
55 NULL, /* poll_event */
56 no_get_fd, /* get_fd */
58 console_get_file_info, /* get_file_info */
59 NULL, /* queue_async */
60 console_input_destroy /* destroy */
63 static void console_input_events_dump( struct object *obj, int verbose );
64 static void console_input_events_destroy( struct object *obj );
65 static int console_input_events_signaled( struct object *obj, struct thread *thread );
67 struct console_input_events
69 struct object obj; /* object header */
70 int num_alloc; /* number of allocated events */
71 int num_used; /* number of actually used events */
72 struct console_renderer_event* events;
75 static const struct object_ops console_input_events_ops =
77 sizeof(struct console_input_events), /* size */
78 console_input_events_dump, /* dump */
79 add_queue, /* add_queue */
80 remove_queue, /* remove_queue */
81 console_input_events_signaled, /* signaled */
82 no_satisfied, /* satisfied */
83 NULL, /* get_poll_events */
84 NULL, /* poll_event */
85 no_get_fd, /* get_fd */
87 no_get_file_info, /* get_file_info */
88 NULL, /* queue_async */
89 console_input_events_destroy /* destroy */
94 struct object obj; /* object header */
95 struct screen_buffer *next; /* linked list of all screen buffers */
96 struct screen_buffer *prev;
97 struct console_input *input; /* associated console input */
98 int mode; /* output mode */
99 int cursor_size; /* size of cursor (percentage filled) */
100 int cursor_visible;/* cursor visibility flag */
101 int cursor_x; /* position of cursor */
102 int cursor_y; /* position of cursor */
103 int width; /* size (w-h) of the screen buffer */
105 int max_width; /* size (w-h) of the window given font size */
107 char_info_t *data; /* the data for each cell - a width x height matrix */
108 unsigned short attr; /* default attribute for screen buffer */
109 rectangle_t win; /* current visible window on the screen buffer *
110 * as seen in wineconsole */
113 static void screen_buffer_dump( struct object *obj, int verbose );
114 static void screen_buffer_destroy( struct object *obj );
116 static const struct object_ops screen_buffer_ops =
118 sizeof(struct screen_buffer), /* size */
119 screen_buffer_dump, /* dump */
120 no_add_queue, /* add_queue */
121 NULL, /* remove_queue */
123 NULL, /* satisfied */
124 NULL, /* get_poll_events */
125 NULL, /* poll_event */
126 no_get_fd, /* get_fd */
127 no_flush, /* flush */
128 console_get_file_info, /* get_file_info */
129 NULL, /* queue_async */
130 screen_buffer_destroy /* destroy */
133 static struct screen_buffer *screen_buffer_list;
135 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
137 /* dumps the renderer events of a console */
138 static void console_input_events_dump( struct object *obj, int verbose )
140 struct console_input_events *evts = (struct console_input_events *)obj;
141 assert( obj->ops == &console_input_events_ops );
142 fprintf( stderr, "Console input events: %d/%d events\n",
143 evts->num_used, evts->num_alloc );
146 /* destroys the renderer events of a console */
147 static void console_input_events_destroy( struct object *obj )
149 struct console_input_events *evts = (struct console_input_events *)obj;
150 assert( obj->ops == &console_input_events_ops );
151 free( evts->events );
154 /* the renderer events list is signaled when it's not empty */
155 static int console_input_events_signaled( struct object *obj, struct thread *thread )
157 struct console_input_events *evts = (struct console_input_events *)obj;
158 assert( obj->ops == &console_input_events_ops );
159 return (evts->num_used != 0);
162 /* add an event to the console's renderer events list */
163 static void console_input_events_append( struct console_input_events* evts,
164 struct console_renderer_event* evt)
166 int collapsed = FALSE;
168 /* to be done even when evt has been generated by the rendere ? */
170 /* try to collapse evt into current queue's events */
173 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
175 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
176 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
178 /* if two update events overlap, collapse them into a single one */
179 if (last->u.update.bottom + 1 >= evt->u.update.top &&
180 evt->u.update.bottom + 1 >= last->u.update.top)
182 last->u.update.top = min(last->u.update.top, evt->u.update.top);
183 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
190 if (evts->num_used == evts->num_alloc)
192 evts->num_alloc += 16;
193 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
194 assert(evts->events);
196 evts->events[evts->num_used++] = *evt;
198 wake_up( &evts->obj, 0 );
201 /* retrieves events from the console's renderer events list */
202 static void console_input_events_get( struct console_input_events* evts )
204 size_t num = get_reply_max_size() / sizeof(evts->events[0]);
206 if (num > evts->num_used) num = evts->num_used;
207 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
208 if (num < evts->num_used)
210 memmove( &evts->events[0], &evts->events[num],
211 (evts->num_used - num) * sizeof(evts->events[0]) );
213 evts->num_used -= num;
216 static struct console_input_events *create_console_input_events(void)
218 struct console_input_events* evt;
220 if (!(evt = alloc_object( &console_input_events_ops, -1 ))) return NULL;
221 evt->num_alloc = evt->num_used = 0;
226 static struct object *create_console_input( struct thread* renderer )
228 struct console_input *console_input;
230 if (!(console_input = alloc_object( &console_input_ops, -1 ))) return NULL;
231 console_input->renderer = renderer;
232 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
233 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
234 console_input->num_proc = 0;
235 console_input->active = NULL;
236 console_input->recnum = 0;
237 console_input->records = NULL;
238 console_input->evt = create_console_input_events();
239 console_input->title = NULL;
240 console_input->history_size = 50;
241 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
242 console_input->history_index = 0;
243 console_input->history_mode = 0;
244 console_input->edition_mode = 0;
246 if (!console_input->history || !console_input->evt)
248 release_object( console_input );
251 return &console_input->obj;
254 static struct screen_buffer *create_console_output( struct console_input *console_input )
256 struct screen_buffer *screen_buffer;
257 struct console_renderer_event evt;
260 if (!(screen_buffer = alloc_object( &screen_buffer_ops, -1 ))) return NULL;
261 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
262 screen_buffer->input = console_input;
263 screen_buffer->cursor_size = 100;
264 screen_buffer->cursor_visible = 1;
265 screen_buffer->width = 80;
266 screen_buffer->height = 150;
267 screen_buffer->max_width = 80;
268 screen_buffer->max_height = 25;
269 screen_buffer->cursor_x = 0;
270 screen_buffer->cursor_y = 0;
271 screen_buffer->attr = 0x0F;
272 screen_buffer->win.left = 0;
273 screen_buffer->win.right = screen_buffer->max_width - 1;
274 screen_buffer->win.top = 0;
275 screen_buffer->win.bottom = screen_buffer->max_height - 1;
277 if ((screen_buffer->next = screen_buffer_list)) screen_buffer->next->prev = screen_buffer;
278 screen_buffer->prev = NULL;
279 screen_buffer_list = screen_buffer;
281 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
282 sizeof(*screen_buffer->data) )))
284 release_object( screen_buffer );
287 /* clear the first row */
288 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
289 /* and copy it to all other rows */
290 for (i = 1; i < screen_buffer->height; i++)
291 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
292 screen_buffer->width * sizeof(char_info_t) );
294 if (!console_input->active)
296 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
298 /* generate the initial events */
299 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
300 console_input_events_append( console_input->evt, &evt );
302 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
303 evt.u.resize.width = screen_buffer->width;
304 evt.u.resize.height = screen_buffer->height;
305 console_input_events_append( console_input->evt, &evt );
307 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
308 evt.u.display.left = screen_buffer->win.left;
309 evt.u.display.top = screen_buffer->win.top;
310 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
311 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
312 console_input_events_append( console_input->evt, &evt );
314 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
315 evt.u.update.top = 0;
316 evt.u.update.bottom = screen_buffer->height - 1;
317 console_input_events_append( console_input->evt, &evt );
319 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
320 evt.u.cursor_geom.size = screen_buffer->cursor_size;
321 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
322 console_input_events_append( console_input->evt, &evt );
324 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
325 evt.u.cursor_pos.x = screen_buffer->cursor_x;
326 evt.u.cursor_pos.y = screen_buffer->cursor_y;
327 console_input_events_append( console_input->evt, &evt );
329 return screen_buffer;
332 /* free the console for this process */
333 int free_console( struct process *process )
335 struct console_input* console = process->console;
337 if (!console || !console->renderer) return 0;
339 process->console = NULL;
340 if (--console->num_proc == 0)
342 /* all processes have terminated... tell the renderer to terminate too */
343 struct console_renderer_event evt;
344 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
345 console_input_events_append( console->evt, &evt );
347 release_object( console );
352 /* let process inherit the console from parent... this handle two cases :
353 * 1/ generic console inheritance
354 * 2/ parent is a renderer which launches process, and process should attach to the console
355 * renderered by parent
357 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
360 struct process* parent = parent_thread->process;
362 /* if parent is a renderer, then attach current process to its console
367 struct console_input* console;
369 if ((console = (struct console_input*)get_handle_obj( parent, hconin, 0, NULL )))
371 if (console->renderer == parent_thread)
373 process->console = (struct console_input*)grab_object( console );
374 process->console->num_proc++;
377 release_object( console );
380 /* otherwise, if parent has a console, attach child to this console */
381 if (!done && parent->console)
383 assert(parent->console->renderer);
384 process->console = (struct console_input*)grab_object( parent->console );
385 process->console->num_proc++;
389 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
391 struct console_input* console = 0;
394 console = (struct console_input *)get_handle_obj( current->process, handle,
395 access, &console_input_ops );
396 else if (current->process->console)
398 assert( current->process->console->renderer );
399 console = (struct console_input *)grab_object( current->process->console );
402 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
406 /* check if a console input is signaled: yes if non read input records */
407 static int console_input_signaled( struct object *obj, struct thread *thread )
409 struct console_input *console = (struct console_input *)obj;
410 assert( obj->ops == &console_input_ops );
411 return console->recnum ? 1 : 0;
414 struct console_signal_info {
415 struct console_input *console;
420 static int propagate_console_signal_cb(struct process *process, void *user)
422 struct console_signal_info* csi = (struct console_signal_info*)user;
424 if (process->console == csi->console && process->running_threads &&
425 (!csi->group || process->group_id == csi->group))
427 struct thread *thread = process->thread_list;
431 struct thread *next = thread->proc_next;
432 kill( thread->unix_pid, csi->signal );
439 static void propagate_console_signal( struct console_input *console,
440 int sig, process_id_t group_id )
442 struct console_signal_info csi;
446 set_error( STATUS_INVALID_PARAMETER );
449 /* FIXME: should support the other events (like CTRL_BREAK) */
450 if (sig != CTRL_C_EVENT)
452 set_error( STATUS_NOT_IMPLEMENTED );
455 csi.console = console;
457 csi.group = group_id;
459 enum_processes(propagate_console_signal_cb, &csi);
462 static int get_console_mode( obj_handle_t handle )
467 if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
469 if (obj->ops == &console_input_ops)
470 ret = ((struct console_input *)obj)->mode;
471 else if (obj->ops == &screen_buffer_ops)
472 ret = ((struct screen_buffer *)obj)->mode;
474 set_error( STATUS_OBJECT_TYPE_MISMATCH );
475 release_object( obj );
480 /* changes the mode of either a console input or a screen buffer */
481 static int set_console_mode( obj_handle_t handle, int mode )
486 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
488 if (obj->ops == &console_input_ops)
490 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
491 ((struct console_input *)obj)->mode = mode;
494 else if (obj->ops == &screen_buffer_ops)
496 ((struct screen_buffer *)obj)->mode = mode;
499 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
500 release_object( obj );
504 /* add input events to a console input queue */
505 static int write_console_input( struct console_input* console, int count,
506 const INPUT_RECORD *records )
508 INPUT_RECORD *new_rec;
510 if (!count) return 0;
511 if (!(new_rec = realloc( console->records,
512 (console->recnum + count) * sizeof(INPUT_RECORD) )))
514 set_error( STATUS_NO_MEMORY );
515 release_object( console );
518 console->records = new_rec;
519 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
521 if (console->mode & ENABLE_PROCESSED_INPUT)
526 if (records[i].EventType == KEY_EVENT &&
527 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
528 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
531 memcpy( &console->records[console->recnum + i],
532 &console->records[console->recnum + i + 1],
533 (count - i - 1) * sizeof(INPUT_RECORD) );
535 if (records[i].Event.KeyEvent.bKeyDown)
537 /* send SIGINT to all processes attached to this console */
538 propagate_console_signal( console, CTRL_C_EVENT, 0 );
544 console->recnum += count;
545 /* wake up all waiters */
546 wake_up( &console->obj, 0 );
550 /* retrieve a pointer to the console input records */
551 static int read_console_input( obj_handle_t handle, int count, int flush )
553 struct console_input *console;
555 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
556 GENERIC_READ, &console_input_ops )))
561 /* special case: do not retrieve anything, but return
562 * the total number of records available */
563 count = console->recnum;
567 if (count > console->recnum) count = console->recnum;
568 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
573 for (i = count; i < console->recnum; i++)
574 console->records[i-count] = console->records[i];
575 if ((console->recnum -= count) > 0)
577 INPUT_RECORD *new_rec = realloc( console->records,
578 console->recnum * sizeof(INPUT_RECORD) );
579 if (new_rec) console->records = new_rec;
583 free( console->records );
584 console->records = NULL;
587 release_object( console );
591 /* set misc console input information */
592 static int set_console_input_info( const struct set_console_input_info_request *req,
593 const WCHAR *title, size_t len )
595 struct console_input *console;
596 struct console_renderer_event evt;
598 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) goto error;
600 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
602 struct screen_buffer *screen_buffer;
604 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
605 GENERIC_READ, &screen_buffer_ops );
606 if (!screen_buffer || screen_buffer->input != console)
608 set_error( STATUS_INVALID_PARAMETER );
609 if (screen_buffer) release_object( screen_buffer );
613 if (screen_buffer != console->active)
615 if (console->active) release_object( console->active );
616 console->active = screen_buffer;
617 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
618 console_input_events_append( console->evt, &evt );
621 release_object( screen_buffer );
623 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
625 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
628 memcpy( new_title, title, len + sizeof(WCHAR) );
629 new_title[len / sizeof(WCHAR)] = 0;
630 if (console->title) free( console->title );
631 console->title = new_title;
632 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
633 console_input_events_append( console->evt, &evt );
636 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
638 console->history_mode = req->history_mode;
640 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
641 console->history_size != req->history_size)
647 if (req->history_size)
649 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
650 if (!mem) goto error;
651 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
654 delta = (console->history_index > req->history_size) ?
655 (console->history_index - req->history_size) : 0;
657 for (i = delta; i < console->history_index; i++)
659 mem[i - delta] = console->history[i];
660 console->history[i] = NULL;
662 console->history_index -= delta;
664 for (i = 0; i < console->history_size; i++)
665 if (console->history[i]) free( console->history[i] );
666 free( console->history );
667 console->history = mem;
668 console->history_size = req->history_size;
670 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
672 console->edition_mode = req->edition_mode;
674 release_object( console );
677 if (console) release_object( console );
681 /* resize a screen buffer */
682 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
683 int new_width, int new_height )
685 int i, old_width, old_height, copy_width, copy_height;
686 char_info_t *new_data;
688 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
690 set_error( STATUS_NO_MEMORY );
693 old_width = screen_buffer->width;
694 old_height = screen_buffer->height;
695 copy_width = min( old_width, new_width );
696 copy_height = min( old_height, new_height );
698 /* copy all the rows */
699 for (i = 0; i < copy_height; i++)
701 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
702 copy_width * sizeof(char_info_t) );
705 /* clear the end of each row */
706 if (new_width > old_width)
709 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
710 /* and blast it to the other rows */
711 for (i = 1; i < copy_height; i++)
712 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
713 (new_width - old_width) * sizeof(char_info_t) );
716 /* clear remaining rows */
717 if (new_height > old_height)
720 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
721 /* and blast it to the other rows */
722 for (i = old_height+1; i < new_height; i++)
723 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
724 new_width * sizeof(char_info_t) );
726 free( screen_buffer->data );
727 screen_buffer->data = new_data;
728 screen_buffer->width = new_width;
729 screen_buffer->height = new_height;
733 /* set misc screen buffer information */
734 static int set_console_output_info( struct screen_buffer *screen_buffer,
735 const struct set_console_output_info_request *req )
737 struct console_renderer_event evt;
739 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
741 if (req->cursor_size < 1 || req->cursor_size > 100)
743 set_error( STATUS_INVALID_PARAMETER );
746 if (screen_buffer->cursor_size != req->cursor_size ||
747 screen_buffer->cursor_visible != req->cursor_visible)
749 screen_buffer->cursor_size = req->cursor_size;
750 screen_buffer->cursor_visible = req->cursor_visible;
751 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
752 evt.u.cursor_geom.size = req->cursor_size;
753 evt.u.cursor_geom.visible = req->cursor_visible;
754 console_input_events_append( screen_buffer->input->evt, &evt );
757 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
759 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
760 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
762 set_error( STATUS_INVALID_PARAMETER );
765 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
767 screen_buffer->cursor_x = req->cursor_x;
768 screen_buffer->cursor_y = req->cursor_y;
769 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
770 evt.u.cursor_pos.x = req->cursor_x;
771 evt.u.cursor_pos.y = req->cursor_y;
772 console_input_events_append( screen_buffer->input->evt, &evt );
775 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
779 /* new screen-buffer cannot be smaller than actual window */
780 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
781 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
783 set_error( STATUS_INVALID_PARAMETER );
786 /* FIXME: there are also some basic minimum and max size to deal with */
787 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
789 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
790 evt.u.resize.width = req->width;
791 evt.u.resize.height = req->height;
792 console_input_events_append( screen_buffer->input->evt, &evt );
794 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
795 evt.u.update.top = 0;
796 evt.u.update.bottom = screen_buffer->height - 1;
797 console_input_events_append( screen_buffer->input->evt, &evt );
799 /* scroll window to display sb */
800 if (screen_buffer->win.right >= req->width)
802 screen_buffer->win.right -= screen_buffer->win.left;
803 screen_buffer->win.left = 0;
805 if (screen_buffer->win.bottom >= req->height)
807 screen_buffer->win.bottom -= screen_buffer->win.top;
808 screen_buffer->win.top = 0;
810 /* reset cursor if needed (normally, if cursor was outside of new sb, the
811 * window has been shifted so that the new position of the cursor will be
814 if (screen_buffer->cursor_x >= req->width)
816 screen_buffer->cursor_x = req->width - 1;
819 if (screen_buffer->cursor_y >= req->height)
821 screen_buffer->cursor_y = req->height - 1;
826 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
827 evt.u.cursor_pos.x = req->cursor_x;
828 evt.u.cursor_pos.y = req->cursor_y;
829 console_input_events_append( screen_buffer->input->evt, &evt );
832 if (screen_buffer == screen_buffer->input->active &&
833 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
836 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
837 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
838 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
839 write_console_input( screen_buffer->input, 1, &ir );
842 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
844 screen_buffer->attr = req->attr;
846 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
848 if (req->win_left < 0 || req->win_left > req->win_right ||
849 req->win_right >= screen_buffer->width ||
850 req->win_top < 0 || req->win_top > req->win_bottom ||
851 req->win_bottom >= screen_buffer->height)
853 set_error( STATUS_INVALID_PARAMETER );
856 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
857 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
859 screen_buffer->win.left = req->win_left;
860 screen_buffer->win.top = req->win_top;
861 screen_buffer->win.right = req->win_right;
862 screen_buffer->win.bottom = req->win_bottom;
863 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
864 evt.u.display.left = req->win_left;
865 evt.u.display.top = req->win_top;
866 evt.u.display.width = req->win_right - req->win_left + 1;
867 evt.u.display.height = req->win_bottom - req->win_top + 1;
868 console_input_events_append( screen_buffer->input->evt, &evt );
871 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
873 /* can only be done by renderer */
874 if (current->process->console != screen_buffer->input)
876 set_error( STATUS_INVALID_PARAMETER );
880 screen_buffer->max_width = req->max_width;
881 screen_buffer->max_height = req->max_height;
887 /* appends a new line to history (history is a fixed size array) */
888 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
890 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
894 set_error( STATUS_NO_MEMORY );
897 if (!console || !console->history_size)
899 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
903 memcpy( ptr, buf, len * sizeof(WCHAR) );
906 if (console->history_mode && console->history_index &&
907 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
909 /* ok, mode ask us to not use twice the same string...
910 * so just free mem and returns
912 set_error( STATUS_ALIAS_EXISTS );
917 if (console->history_index < console->history_size)
919 console->history[console->history_index++] = ptr;
923 free( console->history[0]) ;
924 memmove( &console->history[0], &console->history[1],
925 (console->history_size - 1) * sizeof(WCHAR*) );
926 console->history[console->history_size - 1] = ptr;
930 /* returns a line from the cache */
931 static size_t console_input_get_hist( struct console_input *console, int index )
935 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
938 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
939 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
945 static void console_input_dump( struct object *obj, int verbose )
947 struct console_input *console = (struct console_input *)obj;
948 assert( obj->ops == &console_input_ops );
949 fprintf( stderr, "Console input active=%p evt=%p\n",
950 console->active, console->evt );
953 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
957 reply->type = FILE_TYPE_CHAR;
959 reply->access_time = 0;
960 reply->write_time = 0;
961 reply->size_high = 0;
964 reply->index_high = 0;
965 reply->index_low = 0;
969 return FD_TYPE_CONSOLE;
972 static void console_input_destroy( struct object *obj )
974 struct console_input* console_in = (struct console_input *)obj;
975 struct screen_buffer* curr;
978 assert( obj->ops == &console_input_ops );
979 if (console_in->title) free( console_in->title );
980 if (console_in->records) free( console_in->records );
982 if (console_in->active) release_object( console_in->active );
983 console_in->active = NULL;
985 for (curr = screen_buffer_list; curr; curr = curr->next)
987 if (curr->input == console_in) curr->input = NULL;
990 release_object( console_in->evt );
991 console_in->evt = NULL;
993 for (i = 0; i < console_in->history_size; i++)
994 if (console_in->history[i]) free( console_in->history[i] );
995 if (console_in->history) free( console_in->history );
998 static void screen_buffer_dump( struct object *obj, int verbose )
1000 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1001 assert( obj->ops == &screen_buffer_ops );
1003 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1006 static void screen_buffer_destroy( struct object *obj )
1008 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1010 assert( obj->ops == &screen_buffer_ops );
1012 if (screen_buffer->next) screen_buffer->next->prev = screen_buffer->prev;
1013 if (screen_buffer->prev) screen_buffer->prev->next = screen_buffer->next;
1014 else screen_buffer_list = screen_buffer->next;
1016 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1018 struct screen_buffer* sb;
1019 for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
1020 screen_buffer->input->active = sb;
1022 if (screen_buffer->data) free( screen_buffer->data );
1025 /* write data into a screen buffer */
1026 static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
1027 const void* data, enum char_info_mode mode,
1028 int x, int y, int wrap )
1031 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1033 if (y >= screen_buffer->height) return 0;
1036 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1038 end = screen_buffer->data + (y+1) * screen_buffer->width;
1042 case CHAR_INFO_MODE_TEXT:
1044 const WCHAR *ptr = data;
1045 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1048 case CHAR_INFO_MODE_ATTR:
1050 const unsigned short *ptr = data;
1051 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1054 case CHAR_INFO_MODE_TEXTATTR:
1056 const char_info_t *ptr = data;
1057 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1060 case CHAR_INFO_MODE_TEXTSTDATTR:
1062 const WCHAR *ptr = data;
1063 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1066 dest->attr = screen_buffer->attr;
1071 set_error( STATUS_INVALID_PARAMETER );
1075 if (i && screen_buffer == screen_buffer->input->active)
1077 struct console_renderer_event evt;
1078 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1079 evt.u.update.top = y + x / screen_buffer->width;
1080 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1081 console_input_events_append( screen_buffer->input->evt, &evt );
1086 /* fill a screen buffer with uniform data */
1087 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1088 enum char_info_mode mode, int x, int y, int count, int wrap )
1091 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1093 if (y >= screen_buffer->height) return 0;
1096 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1098 end = screen_buffer->data + (y+1) * screen_buffer->width;
1100 if (count > end - dest) count = end - dest;
1104 case CHAR_INFO_MODE_TEXT:
1105 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1107 case CHAR_INFO_MODE_ATTR:
1108 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1110 case CHAR_INFO_MODE_TEXTATTR:
1111 for (i = 0; i < count; i++) dest[i] = data;
1113 case CHAR_INFO_MODE_TEXTSTDATTR:
1114 for (i = 0; i < count; i++)
1116 dest[i].ch = data.ch;
1117 dest[i].attr = screen_buffer->attr;
1121 set_error( STATUS_INVALID_PARAMETER );
1125 if (count && screen_buffer == screen_buffer->input->active)
1127 struct console_renderer_event evt;
1128 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1129 evt.u.update.top = y;
1130 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1131 console_input_events_append( screen_buffer->input->evt, &evt );
1136 /* read data from a screen buffer */
1137 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1138 enum char_info_mode mode, int wrap )
1141 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1143 if (y >= screen_buffer->height) return;
1146 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1148 end = screen_buffer->data + (y+1) * screen_buffer->width;
1152 case CHAR_INFO_MODE_TEXT:
1155 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1156 if ((data = set_reply_data_size( count * sizeof(*data) )))
1158 for (i = 0; i < count; i++) data[i] = src[i].ch;
1162 case CHAR_INFO_MODE_ATTR:
1164 unsigned short *data;
1165 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1166 if ((data = set_reply_data_size( count * sizeof(*data) )))
1168 for (i = 0; i < count; i++) data[i] = src[i].attr;
1172 case CHAR_INFO_MODE_TEXTATTR:
1175 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1176 if ((data = set_reply_data_size( count * sizeof(*data) )))
1178 for (i = 0; i < count; i++) data[i] = src[i];
1183 set_error( STATUS_INVALID_PARAMETER );
1188 /* scroll parts of a screen buffer */
1189 static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1192 struct screen_buffer *screen_buffer;
1194 char_info_t *psrc, *pdst;
1195 struct console_renderer_event evt;
1197 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1198 GENERIC_READ, &screen_buffer_ops )))
1200 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1201 xsrc + w > screen_buffer->width ||
1202 xdst + w > screen_buffer->width ||
1203 ysrc + h > screen_buffer->height ||
1204 ydst + h > screen_buffer->height ||
1207 set_error( STATUS_INVALID_PARAMETER );
1208 release_object( screen_buffer );
1214 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1215 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1217 for (j = h; j > 0; j--)
1219 memcpy(pdst, psrc, w * sizeof(*pdst) );
1220 pdst -= screen_buffer->width;
1221 psrc -= screen_buffer->width;
1226 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1227 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1229 for (j = 0; j < h; j++)
1231 /* we use memmove here because when psrc and pdst are the same,
1232 * copies are done on the same row, so the dst and src blocks
1234 memmove( pdst, psrc, w * sizeof(*pdst) );
1235 pdst += screen_buffer->width;
1236 psrc += screen_buffer->width;
1240 /* FIXME: this could be enhanced, by signalling scroll */
1241 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1242 evt.u.update.top = min(ysrc, ydst);
1243 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1244 console_input_events_append( screen_buffer->input->evt, &evt );
1246 release_object( screen_buffer );
1249 /* allocate a console for the renderer */
1250 DECL_HANDLER(alloc_console)
1252 obj_handle_t in = 0;
1253 obj_handle_t evt = 0;
1254 struct process *process;
1255 struct process *renderer = current->process;
1256 struct console_input *console;
1258 process = (req->pid) ? get_process_from_id( req->pid ) :
1259 (struct process *)grab_object( renderer->parent );
1261 reply->handle_in = 0;
1263 if (!process) return;
1264 if (process != renderer && process->console)
1266 set_error( STATUS_ACCESS_DENIED );
1270 if ((console = (struct console_input*)create_console_input( current )))
1272 if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
1274 if ((evt = alloc_handle( renderer, console->evt,
1275 SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
1277 if (process != renderer)
1279 process->console = (struct console_input*)grab_object( console );
1280 console->num_proc++;
1282 reply->handle_in = in;
1284 release_object( console );
1287 close_handle( renderer, in, NULL );
1289 free_console( process );
1292 release_object( process );
1295 /* free the console of the current process */
1296 DECL_HANDLER(free_console)
1298 free_console( current->process );
1301 /* let the renderer peek the events it's waiting on */
1302 DECL_HANDLER(get_console_renderer_events)
1304 struct console_input_events *evt;
1306 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1307 GENERIC_WRITE, &console_input_events_ops );
1309 console_input_events_get( evt );
1310 release_object( evt );
1313 /* open a handle to the process console */
1314 DECL_HANDLER(open_console)
1316 struct object *obj = NULL;
1322 if (current->process->console && current->process->console->renderer)
1323 obj = grab_object( (struct object*)current->process->console );
1326 if (current->process->console && current->process->console->renderer &&
1327 current->process->console->active)
1328 obj = grab_object( (struct object*)current->process->console->active );
1331 if ((obj = get_handle_obj( current->process, (obj_handle_t)req->from,
1332 GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1334 struct console_input* console = (struct console_input*)obj;
1335 obj = (console->active) ? grab_object( console->active ) : NULL;
1336 release_object( console );
1341 /* FIXME: req->share is not used (as in screen buffer creation) */
1344 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1345 release_object( obj );
1347 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1350 /* set info about a console input */
1351 DECL_HANDLER(set_console_input_info)
1353 set_console_input_info( req, get_req_data(), get_req_data_size() );
1356 /* get info about a console (output only) */
1357 DECL_HANDLER(get_console_input_info)
1359 struct console_input *console;
1361 if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1364 size_t len = strlenW( console->title ) * sizeof(WCHAR);
1365 if (len > get_reply_max_size()) len = get_reply_max_size();
1366 set_reply_data( console->title, len );
1368 reply->history_mode = console->history_mode;
1369 reply->history_size = console->history_size;
1370 reply->history_index = console->history_index;
1371 reply->edition_mode = console->edition_mode;
1373 release_object( console );
1376 /* get a console mode (input or output) */
1377 DECL_HANDLER(get_console_mode)
1379 reply->mode = get_console_mode( req->handle );
1382 /* set a console mode (input or output) */
1383 DECL_HANDLER(set_console_mode)
1385 set_console_mode( req->handle, req->mode );
1388 /* add input records to a console input queue */
1389 DECL_HANDLER(write_console_input)
1391 struct console_input *console;
1394 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1395 GENERIC_WRITE, &console_input_ops )))
1397 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1399 release_object( console );
1402 /* fetch input records from a console input queue */
1403 DECL_HANDLER(read_console_input)
1405 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1406 reply->read = read_console_input( req->handle, count, req->flush );
1409 /* appends a string to console's history */
1410 DECL_HANDLER(append_console_input_history)
1412 struct console_input *console;
1414 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1415 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1416 release_object( console );
1419 /* appends a string to console's history */
1420 DECL_HANDLER(get_console_input_history)
1422 struct console_input *console;
1424 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1425 reply->total = console_input_get_hist( console, req->index );
1426 release_object( console );
1429 /* creates a screen buffer */
1430 DECL_HANDLER(create_console_output)
1432 struct console_input* console;
1433 struct screen_buffer* screen_buffer;
1435 if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1437 screen_buffer = create_console_output( console );
1440 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1441 * see file.c on how this could be done */
1442 reply->handle_out = alloc_handle( current->process, screen_buffer,
1443 req->access, req->inherit );
1444 release_object( screen_buffer );
1446 release_object( console );
1449 /* set info about a console screen buffer */
1450 DECL_HANDLER(set_console_output_info)
1452 struct screen_buffer *screen_buffer;
1454 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1455 GENERIC_WRITE, &screen_buffer_ops)))
1457 set_console_output_info( screen_buffer, req );
1458 release_object( screen_buffer );
1462 /* get info about a console screen buffer */
1463 DECL_HANDLER(get_console_output_info)
1465 struct screen_buffer *screen_buffer;
1467 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1468 GENERIC_READ, &screen_buffer_ops)))
1470 reply->cursor_size = screen_buffer->cursor_size;
1471 reply->cursor_visible = screen_buffer->cursor_visible;
1472 reply->cursor_x = screen_buffer->cursor_x;
1473 reply->cursor_y = screen_buffer->cursor_y;
1474 reply->width = screen_buffer->width;
1475 reply->height = screen_buffer->height;
1476 reply->attr = screen_buffer->attr;
1477 reply->win_left = screen_buffer->win.left;
1478 reply->win_top = screen_buffer->win.top;
1479 reply->win_right = screen_buffer->win.right;
1480 reply->win_bottom = screen_buffer->win.bottom;
1481 reply->max_width = screen_buffer->max_width;
1482 reply->max_height = screen_buffer->max_height;
1483 release_object( screen_buffer );
1487 /* read data (chars & attrs) from a screen buffer */
1488 DECL_HANDLER(read_console_output)
1490 struct screen_buffer *screen_buffer;
1492 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1493 GENERIC_READ, &screen_buffer_ops )))
1495 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1496 reply->width = screen_buffer->width;
1497 reply->height = screen_buffer->height;
1498 release_object( screen_buffer );
1502 /* write data (char and/or attrs) to a screen buffer */
1503 DECL_HANDLER(write_console_output)
1505 struct screen_buffer *screen_buffer;
1507 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1508 GENERIC_WRITE, &screen_buffer_ops)))
1510 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1511 req->mode, req->x, req->y, req->wrap );
1512 reply->width = screen_buffer->width;
1513 reply->height = screen_buffer->height;
1514 release_object( screen_buffer );
1518 /* fill a screen buffer with constant data (chars and/or attributes) */
1519 DECL_HANDLER(fill_console_output)
1521 struct screen_buffer *screen_buffer;
1523 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1524 GENERIC_WRITE, &screen_buffer_ops)))
1526 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1527 req->x, req->y, req->count, req->wrap );
1528 release_object( screen_buffer );
1532 /* move a rect of data in a screen buffer */
1533 DECL_HANDLER(move_console_output)
1535 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1539 /* sends a signal to a console (process, group...) */
1540 DECL_HANDLER(send_console_signal)
1544 group = req->group_id ? req->group_id : current->process->group_id;
1547 set_error( STATUS_INVALID_PARAMETER);
1549 propagate_console_signal( current->process->console, req->signal, group );