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"
38 static void console_input_dump( struct object *obj, int verbose );
39 static void console_input_destroy( struct object *obj );
41 static const struct object_ops console_input_ops =
43 sizeof(struct console_input), /* size */
44 console_input_dump, /* dump */
45 no_add_queue, /* add_queue */
46 NULL, /* remove_queue */
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 */
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 );
59 struct console_input_events
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;
67 static const struct object_ops console_input_events_ops =
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 */
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 */
93 int max_width; /* size (w-h) of the window given font size */
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 */
101 static void screen_buffer_dump( struct object *obj, int verbose );
102 static void screen_buffer_destroy( struct object *obj );
104 static const struct object_ops screen_buffer_ops =
106 sizeof(struct screen_buffer), /* size */
107 screen_buffer_dump, /* dump */
108 no_add_queue, /* add_queue */
109 NULL, /* remove_queue */
111 NULL, /* satisfied */
112 no_signal, /* signal */
113 no_get_fd, /* get_fd */
114 no_close_handle, /* close_handle */
115 screen_buffer_destroy /* destroy */
118 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
120 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
122 /* dumps the renderer events of a console */
123 static void console_input_events_dump( struct object *obj, int verbose )
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 );
131 /* destroys the renderer events of a console */
132 static void console_input_events_destroy( struct object *obj )
134 struct console_input_events *evts = (struct console_input_events *)obj;
135 assert( obj->ops == &console_input_events_ops );
136 free( evts->events );
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 )
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);
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)
151 int collapsed = FALSE;
153 /* to be done even when evt has been generated by the rendere ? */
155 /* try to collapse evt into current queue's events */
158 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
160 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
161 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
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)
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);
175 if (evts->num_used == evts->num_alloc)
177 evts->num_alloc += 16;
178 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
179 assert(evts->events);
181 evts->events[evts->num_used++] = *evt;
183 wake_up( &evts->obj, 0 );
186 /* retrieves events from the console's renderer events list */
187 static void console_input_events_get( struct console_input_events* evts )
189 size_t num = get_reply_max_size() / sizeof(evts->events[0]);
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)
195 memmove( &evts->events[0], &evts->events[num],
196 (evts->num_used - num) * sizeof(evts->events[0]) );
198 evts->num_used -= num;
201 static struct console_input_events *create_console_input_events(void)
203 struct console_input_events* evt;
205 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
206 evt->num_alloc = evt->num_used = 0;
211 static struct object *create_console_input( struct thread* renderer )
213 struct console_input *console_input;
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 );
232 if (!console_input->history || !console_input->evt)
234 release_object( console_input );
237 return &console_input->obj;
240 static struct screen_buffer *create_console_output( struct console_input *console_input )
242 struct screen_buffer *screen_buffer;
243 struct console_renderer_event evt;
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;
263 list_add_head( &screen_buffer_list, &screen_buffer->entry );
265 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
266 sizeof(*screen_buffer->data) )))
268 release_object( screen_buffer );
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) );
278 if (!console_input->active)
280 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
282 /* generate the initial events */
283 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
284 console_input_events_append( console_input->evt, &evt );
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 );
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 );
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 );
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 );
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 );
313 return screen_buffer;
316 /* free the console for this process */
317 int free_console( struct process *process )
319 struct console_input* console = process->console;
321 if (!console || !console->renderer) return 0;
323 process->console = NULL;
324 if (--console->num_proc == 0)
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 );
331 release_object( console );
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
341 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
344 struct process* parent = parent_thread->process;
346 /* if parent is a renderer, then attach current process to its console
351 struct console_input* console;
353 /* FIXME: should we check some access rights ? */
354 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
355 0, &console_input_ops )))
357 if (console->renderer == parent_thread)
359 process->console = (struct console_input*)grab_object( console );
360 process->console->num_proc++;
363 release_object( console );
365 else clear_error(); /* ignore error */
367 /* otherwise, if parent has a console, attach child to this console */
368 if (!done && parent->console)
370 assert(parent->console->renderer);
371 process->console = (struct console_input*)grab_object( parent->console );
372 process->console->num_proc++;
376 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
378 struct console_input* console = NULL;
381 console = (struct console_input *)get_handle_obj( current->process, handle,
382 access, &console_input_ops );
383 else if (current->process->console)
385 assert( current->process->console->renderer );
386 console = (struct console_input *)grab_object( current->process->console );
389 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
393 struct console_signal_info
395 struct console_input *console;
400 static int propagate_console_signal_cb(struct process *process, void *user)
402 struct console_signal_info* csi = (struct console_signal_info*)user;
404 if (process->console == csi->console && process->running_threads &&
405 (!csi->group || process->group_id == csi->group))
407 /* find a suitable thread to signal */
408 struct thread *thread;
409 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
411 if (send_thread_signal( thread, csi->signal )) break;
417 static void propagate_console_signal( struct console_input *console,
418 int sig, process_id_t group_id )
420 struct console_signal_info csi;
424 set_error( STATUS_INVALID_PARAMETER );
427 /* FIXME: should support the other events (like CTRL_BREAK) */
428 if (sig != CTRL_C_EVENT)
430 set_error( STATUS_NOT_IMPLEMENTED );
433 csi.console = console;
435 csi.group = group_id;
437 enum_processes(propagate_console_signal_cb, &csi);
440 static int get_console_mode( obj_handle_t handle )
445 if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
447 if (obj->ops == &console_input_ops)
448 ret = ((struct console_input *)obj)->mode;
449 else if (obj->ops == &screen_buffer_ops)
450 ret = ((struct screen_buffer *)obj)->mode;
452 set_error( STATUS_OBJECT_TYPE_MISMATCH );
453 release_object( obj );
458 /* changes the mode of either a console input or a screen buffer */
459 static int set_console_mode( obj_handle_t handle, int mode )
464 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
466 if (obj->ops == &console_input_ops)
468 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
469 ((struct console_input *)obj)->mode = mode;
472 else if (obj->ops == &screen_buffer_ops)
474 ((struct screen_buffer *)obj)->mode = mode;
477 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
478 release_object( obj );
482 /* add input events to a console input queue */
483 static int write_console_input( struct console_input* console, int count,
484 const INPUT_RECORD *records )
486 INPUT_RECORD *new_rec;
488 if (!count) return 0;
489 if (!(new_rec = realloc( console->records,
490 (console->recnum + count) * sizeof(INPUT_RECORD) )))
492 set_error( STATUS_NO_MEMORY );
493 release_object( console );
496 console->records = new_rec;
497 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
499 if (console->mode & ENABLE_PROCESSED_INPUT)
504 if (records[i].EventType == KEY_EVENT &&
505 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
506 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
509 memcpy( &console->records[console->recnum + i],
510 &console->records[console->recnum + i + 1],
511 (count - i - 1) * sizeof(INPUT_RECORD) );
513 if (records[i].Event.KeyEvent.bKeyDown)
515 /* send SIGINT to all processes attached to this console */
516 propagate_console_signal( console, CTRL_C_EVENT, 0 );
522 if (!console->recnum && count) set_event( console->event );
523 console->recnum += count;
527 /* retrieve a pointer to the console input records */
528 static int read_console_input( obj_handle_t handle, int count, int flush )
530 struct console_input *console;
532 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
533 GENERIC_READ, &console_input_ops )))
538 /* special case: do not retrieve anything, but return
539 * the total number of records available */
540 count = console->recnum;
544 if (count > console->recnum) count = console->recnum;
545 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
550 for (i = count; i < console->recnum; i++)
551 console->records[i-count] = console->records[i];
552 if ((console->recnum -= count) > 0)
554 INPUT_RECORD *new_rec = realloc( console->records,
555 console->recnum * sizeof(INPUT_RECORD) );
556 if (new_rec) console->records = new_rec;
560 free( console->records );
561 console->records = NULL;
562 reset_event( console->event );
565 release_object( console );
569 /* set misc console input information */
570 static int set_console_input_info( const struct set_console_input_info_request *req,
571 const WCHAR *title, size_t len )
573 struct console_input *console;
574 struct console_renderer_event evt;
576 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) goto error;
578 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
580 struct screen_buffer *screen_buffer;
582 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
583 GENERIC_READ, &screen_buffer_ops );
584 if (!screen_buffer || screen_buffer->input != console)
586 set_error( STATUS_INVALID_PARAMETER );
587 if (screen_buffer) release_object( screen_buffer );
591 if (screen_buffer != console->active)
593 if (console->active) release_object( console->active );
594 console->active = screen_buffer;
595 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
596 console_input_events_append( console->evt, &evt );
599 release_object( screen_buffer );
601 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
603 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
606 memcpy( new_title, title, len );
607 new_title[len / sizeof(WCHAR)] = 0;
608 if (console->title) free( console->title );
609 console->title = new_title;
610 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
611 console_input_events_append( console->evt, &evt );
614 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
616 console->history_mode = req->history_mode;
618 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
619 console->history_size != req->history_size)
625 if (req->history_size)
627 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
628 if (!mem) goto error;
629 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
632 delta = (console->history_index > req->history_size) ?
633 (console->history_index - req->history_size) : 0;
635 for (i = delta; i < console->history_index; i++)
637 mem[i - delta] = console->history[i];
638 console->history[i] = NULL;
640 console->history_index -= delta;
642 for (i = 0; i < console->history_size; i++)
643 if (console->history[i]) free( console->history[i] );
644 free( console->history );
645 console->history = mem;
646 console->history_size = req->history_size;
648 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
650 console->edition_mode = req->edition_mode;
652 release_object( console );
655 if (console) release_object( console );
659 /* resize a screen buffer */
660 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
661 int new_width, int new_height )
663 int i, old_width, old_height, copy_width, copy_height;
664 char_info_t *new_data;
666 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
668 set_error( STATUS_NO_MEMORY );
671 old_width = screen_buffer->width;
672 old_height = screen_buffer->height;
673 copy_width = min( old_width, new_width );
674 copy_height = min( old_height, new_height );
676 /* copy all the rows */
677 for (i = 0; i < copy_height; i++)
679 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
680 copy_width * sizeof(char_info_t) );
683 /* clear the end of each row */
684 if (new_width > old_width)
687 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
688 /* and blast it to the other rows */
689 for (i = 1; i < copy_height; i++)
690 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
691 (new_width - old_width) * sizeof(char_info_t) );
694 /* clear remaining rows */
695 if (new_height > old_height)
698 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
699 /* and blast it to the other rows */
700 for (i = old_height+1; i < new_height; i++)
701 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
702 new_width * sizeof(char_info_t) );
704 free( screen_buffer->data );
705 screen_buffer->data = new_data;
706 screen_buffer->width = new_width;
707 screen_buffer->height = new_height;
711 /* set misc screen buffer information */
712 static int set_console_output_info( struct screen_buffer *screen_buffer,
713 const struct set_console_output_info_request *req )
715 struct console_renderer_event evt;
717 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
719 if (req->cursor_size < 1 || req->cursor_size > 100)
721 set_error( STATUS_INVALID_PARAMETER );
724 if (screen_buffer->cursor_size != req->cursor_size ||
725 screen_buffer->cursor_visible != req->cursor_visible)
727 screen_buffer->cursor_size = req->cursor_size;
728 screen_buffer->cursor_visible = req->cursor_visible;
729 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
730 evt.u.cursor_geom.size = req->cursor_size;
731 evt.u.cursor_geom.visible = req->cursor_visible;
732 console_input_events_append( screen_buffer->input->evt, &evt );
735 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
737 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
738 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
740 set_error( STATUS_INVALID_PARAMETER );
743 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
745 screen_buffer->cursor_x = req->cursor_x;
746 screen_buffer->cursor_y = req->cursor_y;
747 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
748 evt.u.cursor_pos.x = req->cursor_x;
749 evt.u.cursor_pos.y = req->cursor_y;
750 console_input_events_append( screen_buffer->input->evt, &evt );
753 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
757 /* new screen-buffer cannot be smaller than actual window */
758 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
759 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
761 set_error( STATUS_INVALID_PARAMETER );
764 /* FIXME: there are also some basic minimum and max size to deal with */
765 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
767 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
768 evt.u.resize.width = req->width;
769 evt.u.resize.height = req->height;
770 console_input_events_append( screen_buffer->input->evt, &evt );
772 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
773 evt.u.update.top = 0;
774 evt.u.update.bottom = screen_buffer->height - 1;
775 console_input_events_append( screen_buffer->input->evt, &evt );
777 /* scroll window to display sb */
778 if (screen_buffer->win.right >= req->width)
780 screen_buffer->win.right -= screen_buffer->win.left;
781 screen_buffer->win.left = 0;
783 if (screen_buffer->win.bottom >= req->height)
785 screen_buffer->win.bottom -= screen_buffer->win.top;
786 screen_buffer->win.top = 0;
788 /* reset cursor if needed (normally, if cursor was outside of new sb, the
789 * window has been shifted so that the new position of the cursor will be
792 if (screen_buffer->cursor_x >= req->width)
794 screen_buffer->cursor_x = req->width - 1;
797 if (screen_buffer->cursor_y >= req->height)
799 screen_buffer->cursor_y = req->height - 1;
804 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
805 evt.u.cursor_pos.x = req->cursor_x;
806 evt.u.cursor_pos.y = req->cursor_y;
807 console_input_events_append( screen_buffer->input->evt, &evt );
810 if (screen_buffer == screen_buffer->input->active &&
811 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
814 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
815 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
816 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
817 write_console_input( screen_buffer->input, 1, &ir );
820 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
822 screen_buffer->attr = req->attr;
824 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
826 if (req->win_left < 0 || req->win_left > req->win_right ||
827 req->win_right >= screen_buffer->width ||
828 req->win_top < 0 || req->win_top > req->win_bottom ||
829 req->win_bottom >= screen_buffer->height)
831 set_error( STATUS_INVALID_PARAMETER );
834 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
835 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
837 screen_buffer->win.left = req->win_left;
838 screen_buffer->win.top = req->win_top;
839 screen_buffer->win.right = req->win_right;
840 screen_buffer->win.bottom = req->win_bottom;
841 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
842 evt.u.display.left = req->win_left;
843 evt.u.display.top = req->win_top;
844 evt.u.display.width = req->win_right - req->win_left + 1;
845 evt.u.display.height = req->win_bottom - req->win_top + 1;
846 console_input_events_append( screen_buffer->input->evt, &evt );
849 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
851 /* can only be done by renderer */
852 if (current->process->console != screen_buffer->input)
854 set_error( STATUS_INVALID_PARAMETER );
858 screen_buffer->max_width = req->max_width;
859 screen_buffer->max_height = req->max_height;
865 /* appends a new line to history (history is a fixed size array) */
866 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
868 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
872 set_error( STATUS_NO_MEMORY );
875 if (!console || !console->history_size)
877 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
881 memcpy( ptr, buf, len * sizeof(WCHAR) );
884 if (console->history_mode && console->history_index &&
885 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
887 /* ok, mode ask us to not use twice the same string...
888 * so just free mem and returns
890 set_error( STATUS_ALIAS_EXISTS );
895 if (console->history_index < console->history_size)
897 console->history[console->history_index++] = ptr;
901 free( console->history[0]) ;
902 memmove( &console->history[0], &console->history[1],
903 (console->history_size - 1) * sizeof(WCHAR*) );
904 console->history[console->history_size - 1] = ptr;
908 /* returns a line from the cache */
909 static size_t console_input_get_hist( struct console_input *console, int index )
913 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
916 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
917 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
923 static void console_input_dump( struct object *obj, int verbose )
925 struct console_input *console = (struct console_input *)obj;
926 assert( obj->ops == &console_input_ops );
927 fprintf( stderr, "Console input active=%p evt=%p\n",
928 console->active, console->evt );
931 static void console_input_destroy( struct object *obj )
933 struct console_input* console_in = (struct console_input *)obj;
934 struct screen_buffer* curr;
937 assert( obj->ops == &console_input_ops );
938 if (console_in->title) free( console_in->title );
939 if (console_in->records) free( console_in->records );
941 if (console_in->active) release_object( console_in->active );
942 console_in->active = NULL;
944 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
946 if (curr->input == console_in) curr->input = NULL;
949 release_object( console_in->evt );
950 console_in->evt = NULL;
951 release_object( console_in->event );
953 for (i = 0; i < console_in->history_size; i++)
954 if (console_in->history[i]) free( console_in->history[i] );
955 if (console_in->history) free( console_in->history );
958 static void screen_buffer_dump( struct object *obj, int verbose )
960 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
961 assert( obj->ops == &screen_buffer_ops );
963 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
966 static void screen_buffer_destroy( struct object *obj )
968 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
970 assert( obj->ops == &screen_buffer_ops );
972 list_remove( &screen_buffer->entry );
974 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
976 struct screen_buffer *sb;
978 screen_buffer->input->active = NULL;
979 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
981 if (sb->input == screen_buffer->input)
983 sb->input->active = sb;
988 if (screen_buffer->data) free( screen_buffer->data );
991 /* write data into a screen buffer */
992 static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
993 const void* data, enum char_info_mode mode,
994 int x, int y, int wrap )
997 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
999 if (y >= screen_buffer->height) return 0;
1002 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1004 end = screen_buffer->data + (y+1) * screen_buffer->width;
1008 case CHAR_INFO_MODE_TEXT:
1010 const WCHAR *ptr = data;
1011 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1014 case CHAR_INFO_MODE_ATTR:
1016 const unsigned short *ptr = data;
1017 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1020 case CHAR_INFO_MODE_TEXTATTR:
1022 const char_info_t *ptr = data;
1023 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1026 case CHAR_INFO_MODE_TEXTSTDATTR:
1028 const WCHAR *ptr = data;
1029 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1032 dest->attr = screen_buffer->attr;
1037 set_error( STATUS_INVALID_PARAMETER );
1041 if (i && screen_buffer == screen_buffer->input->active)
1043 struct console_renderer_event evt;
1044 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1045 evt.u.update.top = y + x / screen_buffer->width;
1046 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1047 console_input_events_append( screen_buffer->input->evt, &evt );
1052 /* fill a screen buffer with uniform data */
1053 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1054 enum char_info_mode mode, int x, int y, int count, int wrap )
1057 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1059 if (y >= screen_buffer->height) return 0;
1062 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1064 end = screen_buffer->data + (y+1) * screen_buffer->width;
1066 if (count > end - dest) count = end - dest;
1070 case CHAR_INFO_MODE_TEXT:
1071 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1073 case CHAR_INFO_MODE_ATTR:
1074 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1076 case CHAR_INFO_MODE_TEXTATTR:
1077 for (i = 0; i < count; i++) dest[i] = data;
1079 case CHAR_INFO_MODE_TEXTSTDATTR:
1080 for (i = 0; i < count; i++)
1082 dest[i].ch = data.ch;
1083 dest[i].attr = screen_buffer->attr;
1087 set_error( STATUS_INVALID_PARAMETER );
1091 if (count && screen_buffer == screen_buffer->input->active)
1093 struct console_renderer_event evt;
1094 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1095 evt.u.update.top = y;
1096 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1097 console_input_events_append( screen_buffer->input->evt, &evt );
1102 /* read data from a screen buffer */
1103 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1104 enum char_info_mode mode, int wrap )
1107 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1109 if (y >= screen_buffer->height) return;
1112 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1114 end = screen_buffer->data + (y+1) * screen_buffer->width;
1118 case CHAR_INFO_MODE_TEXT:
1121 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1122 if ((data = set_reply_data_size( count * sizeof(*data) )))
1124 for (i = 0; i < count; i++) data[i] = src[i].ch;
1128 case CHAR_INFO_MODE_ATTR:
1130 unsigned short *data;
1131 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1132 if ((data = set_reply_data_size( count * sizeof(*data) )))
1134 for (i = 0; i < count; i++) data[i] = src[i].attr;
1138 case CHAR_INFO_MODE_TEXTATTR:
1141 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1142 if ((data = set_reply_data_size( count * sizeof(*data) )))
1144 for (i = 0; i < count; i++) data[i] = src[i];
1149 set_error( STATUS_INVALID_PARAMETER );
1154 /* scroll parts of a screen buffer */
1155 static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1158 struct screen_buffer *screen_buffer;
1160 char_info_t *psrc, *pdst;
1161 struct console_renderer_event evt;
1163 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1164 GENERIC_READ, &screen_buffer_ops )))
1166 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1167 xsrc + w > screen_buffer->width ||
1168 xdst + w > screen_buffer->width ||
1169 ysrc + h > screen_buffer->height ||
1170 ydst + h > screen_buffer->height ||
1173 set_error( STATUS_INVALID_PARAMETER );
1174 release_object( screen_buffer );
1180 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1181 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1183 for (j = h; j > 0; j--)
1185 memcpy(pdst, psrc, w * sizeof(*pdst) );
1186 pdst -= screen_buffer->width;
1187 psrc -= screen_buffer->width;
1192 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1193 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1195 for (j = 0; j < h; j++)
1197 /* we use memmove here because when psrc and pdst are the same,
1198 * copies are done on the same row, so the dst and src blocks
1200 memmove( pdst, psrc, w * sizeof(*pdst) );
1201 pdst += screen_buffer->width;
1202 psrc += screen_buffer->width;
1206 /* FIXME: this could be enhanced, by signalling scroll */
1207 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1208 evt.u.update.top = min(ysrc, ydst);
1209 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1210 console_input_events_append( screen_buffer->input->evt, &evt );
1212 release_object( screen_buffer );
1215 /* allocate a console for the renderer */
1216 DECL_HANDLER(alloc_console)
1218 obj_handle_t in = 0;
1219 obj_handle_t evt = 0;
1220 struct process *process;
1221 struct process *renderer = current->process;
1222 struct console_input *console;
1224 process = (req->pid) ? get_process_from_id( req->pid ) :
1225 (struct process *)grab_object( renderer->parent );
1227 reply->handle_in = 0;
1229 if (!process) return;
1230 if (process != renderer && process->console)
1232 set_error( STATUS_ACCESS_DENIED );
1235 if ((console = (struct console_input*)create_console_input( current )))
1237 if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
1239 if ((evt = alloc_handle( renderer, console->evt,
1240 SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
1242 if (process != renderer)
1244 process->console = (struct console_input*)grab_object( console );
1245 console->num_proc++;
1247 reply->handle_in = in;
1249 release_object( console );
1252 close_handle( renderer, in, NULL );
1254 free_console( process );
1257 release_object( process );
1260 /* free the console of the current process */
1261 DECL_HANDLER(free_console)
1263 free_console( current->process );
1266 /* let the renderer peek the events it's waiting on */
1267 DECL_HANDLER(get_console_renderer_events)
1269 struct console_input_events *evt;
1271 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1272 GENERIC_WRITE, &console_input_events_ops );
1274 console_input_events_get( evt );
1275 release_object( evt );
1278 /* open a handle to the process console */
1279 DECL_HANDLER(open_console)
1281 struct object *obj = NULL;
1287 if (current->process->console && current->process->console->renderer)
1288 obj = grab_object( (struct object*)current->process->console );
1291 if (current->process->console && current->process->console->renderer &&
1292 current->process->console->active)
1293 obj = grab_object( (struct object*)current->process->console->active );
1296 if ((obj = get_handle_obj( current->process, (obj_handle_t)req->from,
1297 GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1299 struct console_input* console = (struct console_input*)obj;
1300 obj = (console->active) ? grab_object( console->active ) : NULL;
1301 release_object( console );
1306 /* FIXME: req->share is not used (as in screen buffer creation) */
1309 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1310 release_object( obj );
1312 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1315 /* set info about a console input */
1316 DECL_HANDLER(set_console_input_info)
1318 set_console_input_info( req, get_req_data(), get_req_data_size() );
1321 /* get info about a console (output only) */
1322 DECL_HANDLER(get_console_input_info)
1324 struct console_input *console;
1326 if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1329 size_t len = strlenW( console->title ) * sizeof(WCHAR);
1330 if (len > get_reply_max_size()) len = get_reply_max_size();
1331 set_reply_data( console->title, len );
1333 reply->history_mode = console->history_mode;
1334 reply->history_size = console->history_size;
1335 reply->history_index = console->history_index;
1336 reply->edition_mode = console->edition_mode;
1338 release_object( console );
1341 /* get a console mode (input or output) */
1342 DECL_HANDLER(get_console_mode)
1344 reply->mode = get_console_mode( req->handle );
1347 /* set a console mode (input or output) */
1348 DECL_HANDLER(set_console_mode)
1350 set_console_mode( req->handle, req->mode );
1353 /* add input records to a console input queue */
1354 DECL_HANDLER(write_console_input)
1356 struct console_input *console;
1359 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1360 GENERIC_WRITE, &console_input_ops )))
1362 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1364 release_object( console );
1367 /* fetch input records from a console input queue */
1368 DECL_HANDLER(read_console_input)
1370 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1371 reply->read = read_console_input( req->handle, count, req->flush );
1374 /* appends a string to console's history */
1375 DECL_HANDLER(append_console_input_history)
1377 struct console_input *console;
1379 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1380 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1381 release_object( console );
1384 /* appends a string to console's history */
1385 DECL_HANDLER(get_console_input_history)
1387 struct console_input *console;
1389 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1390 reply->total = console_input_get_hist( console, req->index );
1391 release_object( console );
1394 /* creates a screen buffer */
1395 DECL_HANDLER(create_console_output)
1397 struct console_input* console;
1398 struct screen_buffer* screen_buffer;
1400 if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1402 screen_buffer = create_console_output( console );
1405 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1406 * see file.c on how this could be done */
1407 reply->handle_out = alloc_handle( current->process, screen_buffer,
1408 req->access, req->inherit );
1409 release_object( screen_buffer );
1411 release_object( console );
1414 /* set info about a console screen buffer */
1415 DECL_HANDLER(set_console_output_info)
1417 struct screen_buffer *screen_buffer;
1419 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1420 GENERIC_WRITE, &screen_buffer_ops)))
1422 set_console_output_info( screen_buffer, req );
1423 release_object( screen_buffer );
1427 /* get info about a console screen buffer */
1428 DECL_HANDLER(get_console_output_info)
1430 struct screen_buffer *screen_buffer;
1432 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1433 GENERIC_READ, &screen_buffer_ops)))
1435 reply->cursor_size = screen_buffer->cursor_size;
1436 reply->cursor_visible = screen_buffer->cursor_visible;
1437 reply->cursor_x = screen_buffer->cursor_x;
1438 reply->cursor_y = screen_buffer->cursor_y;
1439 reply->width = screen_buffer->width;
1440 reply->height = screen_buffer->height;
1441 reply->attr = screen_buffer->attr;
1442 reply->win_left = screen_buffer->win.left;
1443 reply->win_top = screen_buffer->win.top;
1444 reply->win_right = screen_buffer->win.right;
1445 reply->win_bottom = screen_buffer->win.bottom;
1446 reply->max_width = screen_buffer->max_width;
1447 reply->max_height = screen_buffer->max_height;
1448 release_object( screen_buffer );
1452 /* read data (chars & attrs) from a screen buffer */
1453 DECL_HANDLER(read_console_output)
1455 struct screen_buffer *screen_buffer;
1457 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1458 GENERIC_READ, &screen_buffer_ops )))
1460 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1461 reply->width = screen_buffer->width;
1462 reply->height = screen_buffer->height;
1463 release_object( screen_buffer );
1467 /* write data (char and/or attrs) to a screen buffer */
1468 DECL_HANDLER(write_console_output)
1470 struct screen_buffer *screen_buffer;
1472 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1473 GENERIC_WRITE, &screen_buffer_ops)))
1475 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1476 req->mode, req->x, req->y, req->wrap );
1477 reply->width = screen_buffer->width;
1478 reply->height = screen_buffer->height;
1479 release_object( screen_buffer );
1483 /* fill a screen buffer with constant data (chars and/or attributes) */
1484 DECL_HANDLER(fill_console_output)
1486 struct screen_buffer *screen_buffer;
1488 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1489 GENERIC_WRITE, &screen_buffer_ops)))
1491 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1492 req->x, req->y, req->count, req->wrap );
1493 release_object( screen_buffer );
1497 /* move a rect of data in a screen buffer */
1498 DECL_HANDLER(move_console_output)
1500 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1504 /* sends a signal to a console (process, group...) */
1505 DECL_HANDLER(send_console_signal)
1509 group = req->group_id ? req->group_id : current->process->group_id;
1512 set_error( STATUS_INVALID_PARAMETER );
1514 propagate_console_signal( current->process->console, req->signal, group );
1517 /* get console which renderer is 'current' */
1518 static int cgwe_enum( struct process* process, void* user)
1520 if (process->console && process->console->renderer == current)
1522 *(struct console_input**)user = process->console;
1528 DECL_HANDLER(get_console_wait_event)
1530 struct console_input* console = NULL;
1532 if (current->process->console && current->process->console->renderer)
1533 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1534 else enum_processes(cgwe_enum, &console);
1538 reply->handle = alloc_handle( current->process, console->event,
1539 EVENT_ALL_ACCESS, FALSE);
1540 release_object( console );
1542 else set_error( STATUS_INVALID_PARAMETER );