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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
33 #define WIN32_NO_STATUS
41 /* specific access rights (FIXME: should use finer-grained access rights) */
42 #define CONSOLE_READ 0x01
43 #define CONSOLE_WRITE 0x02
45 static unsigned int console_map_access( struct object *obj, unsigned int access );
47 static void console_input_dump( struct object *obj, int verbose );
48 static void console_input_destroy( struct object *obj );
50 static const struct object_ops console_input_ops =
52 sizeof(struct console_input), /* size */
53 console_input_dump, /* dump */
54 no_add_queue, /* add_queue */
55 NULL, /* remove_queue */
57 no_satisfied, /* satisfied */
58 no_signal, /* signal */
59 no_get_fd, /* get_fd */
60 console_map_access, /* map_access */
61 no_lookup_name, /* lookup_name */
62 no_open_file, /* open_file */
63 no_close_handle, /* close_handle */
64 console_input_destroy /* destroy */
67 static void console_input_events_dump( struct object *obj, int verbose );
68 static void console_input_events_destroy( struct object *obj );
69 static int console_input_events_signaled( struct object *obj, struct thread *thread );
71 struct console_input_events
73 struct object obj; /* object header */
74 int num_alloc; /* number of allocated events */
75 int num_used; /* number of actually used events */
76 struct console_renderer_event* events;
79 static const struct object_ops console_input_events_ops =
81 sizeof(struct console_input_events), /* size */
82 console_input_events_dump, /* dump */
83 add_queue, /* add_queue */
84 remove_queue, /* remove_queue */
85 console_input_events_signaled, /* signaled */
86 no_satisfied, /* satisfied */
87 no_signal, /* signal */
88 no_get_fd, /* get_fd */
89 console_map_access, /* map_access */
90 no_lookup_name, /* lookup_name */
91 no_open_file, /* open_file */
92 no_close_handle, /* close_handle */
93 console_input_events_destroy /* destroy */
98 struct object obj; /* object header */
99 struct list entry; /* entry in list of all screen buffers */
100 struct console_input *input; /* associated console input */
101 int mode; /* output mode */
102 int cursor_size; /* size of cursor (percentage filled) */
103 int cursor_visible;/* cursor visibility flag */
104 int cursor_x; /* position of cursor */
105 int cursor_y; /* position of cursor */
106 int width; /* size (w-h) of the screen buffer */
108 int max_width; /* size (w-h) of the window given font size */
110 char_info_t *data; /* the data for each cell - a width x height matrix */
111 unsigned short attr; /* default attribute for screen buffer */
112 rectangle_t win; /* current visible window on the screen buffer *
113 * as seen in wineconsole */
116 static void screen_buffer_dump( struct object *obj, int verbose );
117 static void screen_buffer_destroy( struct object *obj );
119 static const struct object_ops screen_buffer_ops =
121 sizeof(struct screen_buffer), /* size */
122 screen_buffer_dump, /* dump */
123 no_add_queue, /* add_queue */
124 NULL, /* remove_queue */
126 NULL, /* satisfied */
127 no_signal, /* signal */
128 no_get_fd, /* get_fd */
129 console_map_access, /* map_access */
130 no_lookup_name, /* lookup_name */
131 no_open_file, /* open_file */
132 no_close_handle, /* close_handle */
133 screen_buffer_destroy /* destroy */
136 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
138 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
140 /* access mapping for all console objects */
141 static unsigned int console_map_access( struct object *obj, unsigned int access )
143 if (access & GENERIC_READ) access |= SYNCHRONIZE | STANDARD_RIGHTS_READ | CONSOLE_READ;
144 if (access & GENERIC_WRITE) access |= SYNCHRONIZE | STANDARD_RIGHTS_WRITE | CONSOLE_WRITE;
145 if (access & GENERIC_EXECUTE) access |= SYNCHRONIZE | STANDARD_RIGHTS_EXECUTE;
146 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | CONSOLE_READ | CONSOLE_WRITE;
147 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
150 /* dumps the renderer events of a console */
151 static void console_input_events_dump( struct object *obj, int verbose )
153 struct console_input_events *evts = (struct console_input_events *)obj;
154 assert( obj->ops == &console_input_events_ops );
155 fprintf( stderr, "Console input events: %d/%d events\n",
156 evts->num_used, evts->num_alloc );
159 /* destroys the renderer events of a console */
160 static void console_input_events_destroy( struct object *obj )
162 struct console_input_events *evts = (struct console_input_events *)obj;
163 assert( obj->ops == &console_input_events_ops );
164 free( evts->events );
167 /* the renderer events list is signaled when it's not empty */
168 static int console_input_events_signaled( struct object *obj, struct thread *thread )
170 struct console_input_events *evts = (struct console_input_events *)obj;
171 assert( obj->ops == &console_input_events_ops );
172 return (evts->num_used != 0);
175 /* add an event to the console's renderer events list */
176 static void console_input_events_append( struct console_input_events* evts,
177 struct console_renderer_event* evt)
179 int collapsed = FALSE;
181 /* to be done even when evt has been generated by the rendere ? */
183 /* try to collapse evt into current queue's events */
186 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
188 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
189 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
191 /* if two update events overlap, collapse them into a single one */
192 if (last->u.update.bottom + 1 >= evt->u.update.top &&
193 evt->u.update.bottom + 1 >= last->u.update.top)
195 last->u.update.top = min(last->u.update.top, evt->u.update.top);
196 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
203 if (evts->num_used == evts->num_alloc)
205 evts->num_alloc += 16;
206 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
207 assert(evts->events);
209 evts->events[evts->num_used++] = *evt;
211 wake_up( &evts->obj, 0 );
214 /* retrieves events from the console's renderer events list */
215 static void console_input_events_get( struct console_input_events* evts )
217 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
219 if (num > evts->num_used) num = evts->num_used;
220 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
221 if (num < evts->num_used)
223 memmove( &evts->events[0], &evts->events[num],
224 (evts->num_used - num) * sizeof(evts->events[0]) );
226 evts->num_used -= num;
229 static struct console_input_events *create_console_input_events(void)
231 struct console_input_events* evt;
233 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
234 evt->num_alloc = evt->num_used = 0;
239 static struct object *create_console_input( struct thread* renderer )
241 struct console_input *console_input;
243 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
244 console_input->renderer = renderer;
245 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
246 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
247 console_input->num_proc = 0;
248 console_input->active = NULL;
249 console_input->recnum = 0;
250 console_input->records = NULL;
251 console_input->evt = create_console_input_events();
252 console_input->title = NULL;
253 console_input->history_size = 50;
254 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
255 console_input->history_index = 0;
256 console_input->history_mode = 0;
257 console_input->edition_mode = 0;
258 console_input->event = create_event( NULL, NULL, 0, 1, 0 );
260 if (!console_input->history || !console_input->evt)
262 release_object( console_input );
265 return &console_input->obj;
268 static struct screen_buffer *create_console_output( struct console_input *console_input )
270 struct screen_buffer *screen_buffer;
273 if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
274 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
275 screen_buffer->input = console_input;
276 screen_buffer->cursor_size = 100;
277 screen_buffer->cursor_visible = 1;
278 screen_buffer->width = 80;
279 screen_buffer->height = 150;
280 screen_buffer->max_width = 80;
281 screen_buffer->max_height = 25;
282 screen_buffer->cursor_x = 0;
283 screen_buffer->cursor_y = 0;
284 screen_buffer->attr = 0x0F;
285 screen_buffer->win.left = 0;
286 screen_buffer->win.right = screen_buffer->max_width - 1;
287 screen_buffer->win.top = 0;
288 screen_buffer->win.bottom = screen_buffer->max_height - 1;
290 list_add_head( &screen_buffer_list, &screen_buffer->entry );
292 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
293 sizeof(*screen_buffer->data) )))
295 release_object( screen_buffer );
298 /* clear the first row */
299 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
300 /* and copy it to all other rows */
301 for (i = 1; i < screen_buffer->height; i++)
302 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
303 screen_buffer->width * sizeof(char_info_t) );
305 if (!console_input->active)
307 struct console_renderer_event evt;
308 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
310 /* generate the initial events */
311 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
312 memset(&evt.u, 0, sizeof(evt.u));
313 console_input_events_append( console_input->evt, &evt );
315 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
316 evt.u.resize.width = screen_buffer->width;
317 evt.u.resize.height = screen_buffer->height;
318 console_input_events_append( console_input->evt, &evt );
320 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
321 evt.u.display.left = screen_buffer->win.left;
322 evt.u.display.top = screen_buffer->win.top;
323 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
324 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
325 console_input_events_append( console_input->evt, &evt );
327 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
328 evt.u.update.top = 0;
329 evt.u.update.bottom = screen_buffer->height - 1;
330 console_input_events_append( console_input->evt, &evt );
332 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
333 evt.u.cursor_geom.size = screen_buffer->cursor_size;
334 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
335 console_input_events_append( console_input->evt, &evt );
337 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
338 evt.u.cursor_pos.x = screen_buffer->cursor_x;
339 evt.u.cursor_pos.y = screen_buffer->cursor_y;
340 console_input_events_append( console_input->evt, &evt );
342 return screen_buffer;
345 /* free the console for this process */
346 int free_console( struct process *process )
348 struct console_input* console = process->console;
350 if (!console || !console->renderer) return 0;
352 process->console = NULL;
353 if (--console->num_proc == 0)
355 /* all processes have terminated... tell the renderer to terminate too */
356 struct console_renderer_event evt;
357 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
358 memset(&evt.u, 0, sizeof(evt.u));
359 console_input_events_append( console->evt, &evt );
361 release_object( console );
366 /* let process inherit the console from parent... this handle two cases :
367 * 1/ generic console inheritance
368 * 2/ parent is a renderer which launches process, and process should attach to the console
369 * renderered by parent
371 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
374 struct process* parent = parent_thread->process;
376 /* if parent is a renderer, then attach current process to its console
381 struct console_input* console;
383 /* FIXME: should we check some access rights ? */
384 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
385 0, &console_input_ops )))
387 if (console->renderer == parent_thread)
389 process->console = (struct console_input*)grab_object( console );
390 process->console->num_proc++;
393 release_object( console );
395 else clear_error(); /* ignore error */
397 /* otherwise, if parent has a console, attach child to this console */
398 if (!done && parent->console)
400 assert(parent->console->renderer);
401 process->console = (struct console_input*)grab_object( parent->console );
402 process->console->num_proc++;
406 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
408 struct console_input* console = NULL;
411 console = (struct console_input *)get_handle_obj( current->process, handle,
412 access, &console_input_ops );
413 else if (current->process->console)
415 assert( current->process->console->renderer );
416 console = (struct console_input *)grab_object( current->process->console );
419 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
423 struct console_signal_info
425 struct console_input *console;
430 static int propagate_console_signal_cb(struct process *process, void *user)
432 struct console_signal_info* csi = (struct console_signal_info*)user;
434 if (process->console == csi->console && process->running_threads &&
435 (!csi->group || process->group_id == csi->group))
437 /* find a suitable thread to signal */
438 struct thread *thread;
439 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
441 if (send_thread_signal( thread, csi->signal )) break;
447 static void propagate_console_signal( struct console_input *console,
448 int sig, process_id_t group_id )
450 struct console_signal_info csi;
454 set_error( STATUS_INVALID_PARAMETER );
457 /* FIXME: should support the other events (like CTRL_BREAK) */
458 if (sig != CTRL_C_EVENT)
460 set_error( STATUS_NOT_IMPLEMENTED );
463 csi.console = console;
465 csi.group = group_id;
467 enum_processes(propagate_console_signal_cb, &csi);
470 static int get_console_mode( obj_handle_t handle )
475 if ((obj = get_handle_obj( current->process, handle, CONSOLE_READ, NULL )))
477 if (obj->ops == &console_input_ops)
478 ret = ((struct console_input *)obj)->mode;
479 else if (obj->ops == &screen_buffer_ops)
480 ret = ((struct screen_buffer *)obj)->mode;
482 set_error( STATUS_OBJECT_TYPE_MISMATCH );
483 release_object( obj );
488 /* changes the mode of either a console input or a screen buffer */
489 static int set_console_mode( obj_handle_t handle, int mode )
494 if (!(obj = get_handle_obj( current->process, handle, CONSOLE_WRITE, NULL )))
496 if (obj->ops == &console_input_ops)
498 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
499 ((struct console_input *)obj)->mode = mode;
502 else if (obj->ops == &screen_buffer_ops)
504 ((struct screen_buffer *)obj)->mode = mode;
507 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
508 release_object( obj );
512 /* add input events to a console input queue */
513 static int write_console_input( struct console_input* console, int count,
514 const INPUT_RECORD *records )
516 INPUT_RECORD *new_rec;
518 if (!count) return 0;
519 if (!(new_rec = realloc( console->records,
520 (console->recnum + count) * sizeof(INPUT_RECORD) )))
522 set_error( STATUS_NO_MEMORY );
523 release_object( console );
526 console->records = new_rec;
527 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
529 if (console->mode & ENABLE_PROCESSED_INPUT)
534 if (records[i].EventType == KEY_EVENT &&
535 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
536 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
539 memcpy( &console->records[console->recnum + i],
540 &console->records[console->recnum + i + 1],
541 (count - i - 1) * sizeof(INPUT_RECORD) );
543 if (records[i].Event.KeyEvent.bKeyDown)
545 /* send SIGINT to all processes attached to this console */
546 propagate_console_signal( console, CTRL_C_EVENT, 0 );
552 if (!console->recnum && count) set_event( console->event );
553 console->recnum += count;
557 /* retrieve a pointer to the console input records */
558 static int read_console_input( obj_handle_t handle, int count, int flush )
560 struct console_input *console;
562 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
563 CONSOLE_READ, &console_input_ops )))
568 /* special case: do not retrieve anything, but return
569 * the total number of records available */
570 count = console->recnum;
574 if (count > console->recnum) count = console->recnum;
575 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
580 for (i = count; i < console->recnum; i++)
581 console->records[i-count] = console->records[i];
582 if ((console->recnum -= count) > 0)
584 INPUT_RECORD *new_rec = realloc( console->records,
585 console->recnum * sizeof(INPUT_RECORD) );
586 if (new_rec) console->records = new_rec;
590 free( console->records );
591 console->records = NULL;
592 reset_event( console->event );
595 release_object( console );
599 /* set misc console input information */
600 static int set_console_input_info( const struct set_console_input_info_request *req,
601 const WCHAR *title, data_size_t len )
603 struct console_input *console;
604 struct console_renderer_event evt;
606 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) goto error;
608 memset(&evt.u, 0, sizeof(evt.u));
609 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
611 struct screen_buffer *screen_buffer;
613 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
614 CONSOLE_READ, &screen_buffer_ops );
615 if (!screen_buffer || screen_buffer->input != console)
617 set_error( STATUS_INVALID_PARAMETER );
618 if (screen_buffer) release_object( screen_buffer );
622 if (screen_buffer != console->active)
624 if (console->active) release_object( console->active );
625 console->active = screen_buffer;
626 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
627 console_input_events_append( console->evt, &evt );
630 release_object( screen_buffer );
632 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
634 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
637 memcpy( new_title, title, len );
638 new_title[len / sizeof(WCHAR)] = 0;
639 free( console->title );
640 console->title = new_title;
641 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
642 console_input_events_append( console->evt, &evt );
645 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
647 console->history_mode = req->history_mode;
649 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
650 console->history_size != req->history_size)
656 if (req->history_size)
658 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
659 if (!mem) goto error;
660 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
663 delta = (console->history_index > req->history_size) ?
664 (console->history_index - req->history_size) : 0;
666 for (i = delta; i < console->history_index; i++)
668 mem[i - delta] = console->history[i];
669 console->history[i] = NULL;
671 console->history_index -= delta;
673 for (i = 0; i < console->history_size; i++)
674 if (console->history[i]) free( console->history[i] );
675 free( console->history );
676 console->history = mem;
677 console->history_size = req->history_size;
679 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
681 console->edition_mode = req->edition_mode;
683 release_object( console );
686 if (console) release_object( console );
690 /* resize a screen buffer */
691 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
692 int new_width, int new_height )
694 int i, old_width, old_height, copy_width, copy_height;
695 char_info_t *new_data;
697 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
699 set_error( STATUS_NO_MEMORY );
702 old_width = screen_buffer->width;
703 old_height = screen_buffer->height;
704 copy_width = min( old_width, new_width );
705 copy_height = min( old_height, new_height );
707 /* copy all the rows */
708 for (i = 0; i < copy_height; i++)
710 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
711 copy_width * sizeof(char_info_t) );
714 /* clear the end of each row */
715 if (new_width > old_width)
718 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
719 /* and blast it to the other rows */
720 for (i = 1; i < copy_height; i++)
721 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
722 (new_width - old_width) * sizeof(char_info_t) );
725 /* clear remaining rows */
726 if (new_height > old_height)
729 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
730 /* and blast it to the other rows */
731 for (i = old_height+1; i < new_height; i++)
732 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
733 new_width * sizeof(char_info_t) );
735 free( screen_buffer->data );
736 screen_buffer->data = new_data;
737 screen_buffer->width = new_width;
738 screen_buffer->height = new_height;
742 /* set misc screen buffer information */
743 static int set_console_output_info( struct screen_buffer *screen_buffer,
744 const struct set_console_output_info_request *req )
746 struct console_renderer_event evt;
748 memset(&evt.u, 0, sizeof(evt.u));
749 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
751 if (req->cursor_size < 1 || req->cursor_size > 100)
753 set_error( STATUS_INVALID_PARAMETER );
756 if (screen_buffer->cursor_size != req->cursor_size ||
757 screen_buffer->cursor_visible != req->cursor_visible)
759 screen_buffer->cursor_size = req->cursor_size;
760 screen_buffer->cursor_visible = req->cursor_visible;
761 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
762 evt.u.cursor_geom.size = req->cursor_size;
763 evt.u.cursor_geom.visible = req->cursor_visible;
764 console_input_events_append( screen_buffer->input->evt, &evt );
767 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
769 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
770 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
772 set_error( STATUS_INVALID_PARAMETER );
775 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
777 screen_buffer->cursor_x = req->cursor_x;
778 screen_buffer->cursor_y = req->cursor_y;
779 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
780 evt.u.cursor_pos.x = req->cursor_x;
781 evt.u.cursor_pos.y = req->cursor_y;
782 console_input_events_append( screen_buffer->input->evt, &evt );
785 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
789 /* new screen-buffer cannot be smaller than actual window */
790 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
791 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
793 set_error( STATUS_INVALID_PARAMETER );
796 /* FIXME: there are also some basic minimum and max size to deal with */
797 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
799 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
800 evt.u.resize.width = req->width;
801 evt.u.resize.height = req->height;
802 console_input_events_append( screen_buffer->input->evt, &evt );
804 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
805 evt.u.update.top = 0;
806 evt.u.update.bottom = screen_buffer->height - 1;
807 console_input_events_append( screen_buffer->input->evt, &evt );
809 /* scroll window to display sb */
810 if (screen_buffer->win.right >= req->width)
812 screen_buffer->win.right -= screen_buffer->win.left;
813 screen_buffer->win.left = 0;
815 if (screen_buffer->win.bottom >= req->height)
817 screen_buffer->win.bottom -= screen_buffer->win.top;
818 screen_buffer->win.top = 0;
820 /* reset cursor if needed (normally, if cursor was outside of new sb, the
821 * window has been shifted so that the new position of the cursor will be
824 if (screen_buffer->cursor_x >= req->width)
826 screen_buffer->cursor_x = req->width - 1;
829 if (screen_buffer->cursor_y >= req->height)
831 screen_buffer->cursor_y = req->height - 1;
836 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
837 evt.u.cursor_pos.x = req->cursor_x;
838 evt.u.cursor_pos.y = req->cursor_y;
839 console_input_events_append( screen_buffer->input->evt, &evt );
842 if (screen_buffer == screen_buffer->input->active &&
843 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
846 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
847 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
848 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
849 write_console_input( screen_buffer->input, 1, &ir );
852 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
854 screen_buffer->attr = req->attr;
856 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
858 if (req->win_left < 0 || req->win_left > req->win_right ||
859 req->win_right >= screen_buffer->width ||
860 req->win_top < 0 || req->win_top > req->win_bottom ||
861 req->win_bottom >= screen_buffer->height)
863 set_error( STATUS_INVALID_PARAMETER );
866 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
867 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
869 screen_buffer->win.left = req->win_left;
870 screen_buffer->win.top = req->win_top;
871 screen_buffer->win.right = req->win_right;
872 screen_buffer->win.bottom = req->win_bottom;
873 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
874 evt.u.display.left = req->win_left;
875 evt.u.display.top = req->win_top;
876 evt.u.display.width = req->win_right - req->win_left + 1;
877 evt.u.display.height = req->win_bottom - req->win_top + 1;
878 console_input_events_append( screen_buffer->input->evt, &evt );
881 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
883 /* can only be done by renderer */
884 if (current->process->console != screen_buffer->input)
886 set_error( STATUS_INVALID_PARAMETER );
890 screen_buffer->max_width = req->max_width;
891 screen_buffer->max_height = req->max_height;
897 /* appends a new line to history (history is a fixed size array) */
898 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
900 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
905 if (!console || !console->history_size)
907 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
912 memcpy( ptr, buf, len * sizeof(WCHAR) );
915 if (console->history_mode && console->history_index &&
916 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
918 /* ok, mode ask us to not use twice the same string...
919 * so just free mem and returns
921 set_error( STATUS_ALIAS_EXISTS );
926 if (console->history_index < console->history_size)
928 console->history[console->history_index++] = ptr;
932 free( console->history[0]) ;
933 memmove( &console->history[0], &console->history[1],
934 (console->history_size - 1) * sizeof(WCHAR*) );
935 console->history[console->history_size - 1] = ptr;
939 /* returns a line from the cache */
940 static data_size_t console_input_get_hist( struct console_input *console, int index )
944 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
947 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
948 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
954 static void console_input_dump( struct object *obj, int verbose )
956 struct console_input *console = (struct console_input *)obj;
957 assert( obj->ops == &console_input_ops );
958 fprintf( stderr, "Console input active=%p evt=%p\n",
959 console->active, console->evt );
962 static void console_input_destroy( struct object *obj )
964 struct console_input* console_in = (struct console_input *)obj;
965 struct screen_buffer* curr;
968 assert( obj->ops == &console_input_ops );
969 free( console_in->title );
970 free( console_in->records );
972 if (console_in->active) release_object( console_in->active );
973 console_in->active = NULL;
975 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
977 if (curr->input == console_in) curr->input = NULL;
980 release_object( console_in->evt );
981 console_in->evt = NULL;
982 release_object( console_in->event );
984 for (i = 0; i < console_in->history_size; i++)
985 if (console_in->history[i]) free( console_in->history[i] );
986 free( console_in->history );
989 static void screen_buffer_dump( struct object *obj, int verbose )
991 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
992 assert( obj->ops == &screen_buffer_ops );
994 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
997 static void screen_buffer_destroy( struct object *obj )
999 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1001 assert( obj->ops == &screen_buffer_ops );
1003 list_remove( &screen_buffer->entry );
1005 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1007 struct screen_buffer *sb;
1009 screen_buffer->input->active = NULL;
1010 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1012 if (sb->input == screen_buffer->input)
1014 sb->input->active = sb;
1019 free( screen_buffer->data );
1022 /* write data into a screen buffer */
1023 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1024 const void* data, enum char_info_mode mode,
1025 int x, int y, int wrap )
1028 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1030 if (y >= screen_buffer->height) return 0;
1033 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1035 end = screen_buffer->data + (y+1) * screen_buffer->width;
1039 case CHAR_INFO_MODE_TEXT:
1041 const WCHAR *ptr = data;
1042 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1045 case CHAR_INFO_MODE_ATTR:
1047 const unsigned short *ptr = data;
1048 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1051 case CHAR_INFO_MODE_TEXTATTR:
1053 const char_info_t *ptr = data;
1054 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1057 case CHAR_INFO_MODE_TEXTSTDATTR:
1059 const WCHAR *ptr = data;
1060 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1063 dest->attr = screen_buffer->attr;
1068 set_error( STATUS_INVALID_PARAMETER );
1072 if (i && screen_buffer == screen_buffer->input->active)
1074 struct console_renderer_event evt;
1075 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1076 memset(&evt.u, 0, sizeof(evt.u));
1077 evt.u.update.top = y + x / screen_buffer->width;
1078 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1079 console_input_events_append( screen_buffer->input->evt, &evt );
1084 /* fill a screen buffer with uniform data */
1085 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1086 enum char_info_mode mode, int x, int y, int count, int wrap )
1089 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1091 if (y >= screen_buffer->height) return 0;
1094 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1096 end = screen_buffer->data + (y+1) * screen_buffer->width;
1098 if (count > end - dest) count = end - dest;
1102 case CHAR_INFO_MODE_TEXT:
1103 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1105 case CHAR_INFO_MODE_ATTR:
1106 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1108 case CHAR_INFO_MODE_TEXTATTR:
1109 for (i = 0; i < count; i++) dest[i] = data;
1111 case CHAR_INFO_MODE_TEXTSTDATTR:
1112 for (i = 0; i < count; i++)
1114 dest[i].ch = data.ch;
1115 dest[i].attr = screen_buffer->attr;
1119 set_error( STATUS_INVALID_PARAMETER );
1123 if (count && screen_buffer == screen_buffer->input->active)
1125 struct console_renderer_event evt;
1126 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1127 memset(&evt.u, 0, sizeof(evt.u));
1128 evt.u.update.top = y;
1129 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1130 console_input_events_append( screen_buffer->input->evt, &evt );
1135 /* read data from a screen buffer */
1136 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1137 enum char_info_mode mode, int wrap )
1140 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1142 if (y >= screen_buffer->height) return;
1145 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1147 end = screen_buffer->data + (y+1) * screen_buffer->width;
1151 case CHAR_INFO_MODE_TEXT:
1154 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1155 if ((data = set_reply_data_size( count * sizeof(*data) )))
1157 for (i = 0; i < count; i++) data[i] = src[i].ch;
1161 case CHAR_INFO_MODE_ATTR:
1163 unsigned short *data;
1164 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1165 if ((data = set_reply_data_size( count * sizeof(*data) )))
1167 for (i = 0; i < count; i++) data[i] = src[i].attr;
1171 case CHAR_INFO_MODE_TEXTATTR:
1174 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1175 if ((data = set_reply_data_size( count * sizeof(*data) )))
1177 for (i = 0; i < count; i++) data[i] = src[i];
1182 set_error( STATUS_INVALID_PARAMETER );
1187 /* scroll parts of a screen buffer */
1188 static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1191 struct screen_buffer *screen_buffer;
1193 char_info_t *psrc, *pdst;
1194 struct console_renderer_event evt;
1196 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1197 CONSOLE_READ, &screen_buffer_ops )))
1199 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1200 xsrc + w > screen_buffer->width ||
1201 xdst + w > screen_buffer->width ||
1202 ysrc + h > screen_buffer->height ||
1203 ydst + h > screen_buffer->height ||
1206 set_error( STATUS_INVALID_PARAMETER );
1207 release_object( screen_buffer );
1213 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1214 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1216 for (j = h; j > 0; j--)
1218 memcpy(pdst, psrc, w * sizeof(*pdst) );
1219 pdst -= screen_buffer->width;
1220 psrc -= screen_buffer->width;
1225 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1226 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1228 for (j = 0; j < h; j++)
1230 /* we use memmove here because when psrc and pdst are the same,
1231 * copies are done on the same row, so the dst and src blocks
1233 memmove( pdst, psrc, w * sizeof(*pdst) );
1234 pdst += screen_buffer->width;
1235 psrc += screen_buffer->width;
1239 /* FIXME: this could be enhanced, by signalling scroll */
1240 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1241 memset(&evt.u, 0, sizeof(evt.u));
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;
1260 if (!(process = get_process_from_id( req->pid ))) return;
1264 if (!(process = renderer->parent))
1266 set_error( STATUS_ACCESS_DENIED );
1269 grab_object( process );
1272 if (process != renderer && process->console)
1274 set_error( STATUS_ACCESS_DENIED );
1277 if ((console = (struct console_input*)create_console_input( current )))
1279 if ((in = alloc_handle( renderer, console, req->access, req->attributes )))
1281 if ((evt = alloc_handle( renderer, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1283 if (process != renderer)
1285 process->console = (struct console_input*)grab_object( console );
1286 console->num_proc++;
1288 reply->handle_in = in;
1290 release_object( console );
1293 close_handle( renderer, in );
1295 free_console( process );
1298 release_object( process );
1301 /* free the console of the current process */
1302 DECL_HANDLER(free_console)
1304 free_console( current->process );
1307 /* let the renderer peek the events it's waiting on */
1308 DECL_HANDLER(get_console_renderer_events)
1310 struct console_input_events *evt;
1312 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1313 CONSOLE_READ, &console_input_events_ops );
1315 console_input_events_get( evt );
1316 release_object( evt );
1319 /* open a handle to the process console */
1320 DECL_HANDLER(open_console)
1322 struct object *obj = NULL;
1325 if (req->from == (obj_handle_t)0)
1327 if (current->process->console && current->process->console->renderer)
1328 obj = grab_object( (struct object*)current->process->console );
1330 else if (req->from == (obj_handle_t)1)
1332 if (current->process->console && current->process->console->renderer &&
1333 current->process->console->active)
1334 obj = grab_object( (struct object*)current->process->console->active );
1336 else if ((obj = get_handle_obj( current->process, req->from,
1337 CONSOLE_READ|CONSOLE_WRITE, &console_input_ops )))
1339 struct console_input *console = (struct console_input *)obj;
1340 obj = (console->active) ? grab_object( console->active ) : NULL;
1341 release_object( console );
1344 /* FIXME: req->share is not used (as in screen buffer creation) */
1347 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1348 release_object( obj );
1350 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1353 /* set info about a console input */
1354 DECL_HANDLER(set_console_input_info)
1356 set_console_input_info( req, get_req_data(), get_req_data_size() );
1359 /* get info about a console (output only) */
1360 DECL_HANDLER(get_console_input_info)
1362 struct console_input *console;
1364 if (!(console = console_input_get( req->handle, CONSOLE_READ ))) return;
1367 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1368 if (len > get_reply_max_size()) len = get_reply_max_size();
1369 set_reply_data( console->title, len );
1371 reply->history_mode = console->history_mode;
1372 reply->history_size = console->history_size;
1373 reply->history_index = console->history_index;
1374 reply->edition_mode = console->edition_mode;
1376 release_object( console );
1379 /* get a console mode (input or output) */
1380 DECL_HANDLER(get_console_mode)
1382 reply->mode = get_console_mode( req->handle );
1385 /* set a console mode (input or output) */
1386 DECL_HANDLER(set_console_mode)
1388 set_console_mode( req->handle, req->mode );
1391 /* add input records to a console input queue */
1392 DECL_HANDLER(write_console_input)
1394 struct console_input *console;
1397 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1398 CONSOLE_WRITE, &console_input_ops )))
1400 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1402 release_object( console );
1405 /* fetch input records from a console input queue */
1406 DECL_HANDLER(read_console_input)
1408 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1409 reply->read = read_console_input( req->handle, count, req->flush );
1412 /* appends a string to console's history */
1413 DECL_HANDLER(append_console_input_history)
1415 struct console_input *console;
1417 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1418 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1419 release_object( console );
1422 /* appends a string to console's history */
1423 DECL_HANDLER(get_console_input_history)
1425 struct console_input *console;
1427 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1428 reply->total = console_input_get_hist( console, req->index );
1429 release_object( console );
1432 /* creates a screen buffer */
1433 DECL_HANDLER(create_console_output)
1435 struct console_input* console;
1436 struct screen_buffer* screen_buffer;
1438 if (!(console = console_input_get( req->handle_in, CONSOLE_WRITE ))) return;
1440 screen_buffer = create_console_output( console );
1443 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1444 * see file.c on how this could be done */
1445 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1446 release_object( screen_buffer );
1448 release_object( console );
1451 /* set info about a console screen buffer */
1452 DECL_HANDLER(set_console_output_info)
1454 struct screen_buffer *screen_buffer;
1456 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1457 CONSOLE_WRITE, &screen_buffer_ops)))
1459 set_console_output_info( screen_buffer, req );
1460 release_object( screen_buffer );
1464 /* get info about a console screen buffer */
1465 DECL_HANDLER(get_console_output_info)
1467 struct screen_buffer *screen_buffer;
1469 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1470 CONSOLE_READ, &screen_buffer_ops)))
1472 reply->cursor_size = screen_buffer->cursor_size;
1473 reply->cursor_visible = screen_buffer->cursor_visible;
1474 reply->cursor_x = screen_buffer->cursor_x;
1475 reply->cursor_y = screen_buffer->cursor_y;
1476 reply->width = screen_buffer->width;
1477 reply->height = screen_buffer->height;
1478 reply->attr = screen_buffer->attr;
1479 reply->win_left = screen_buffer->win.left;
1480 reply->win_top = screen_buffer->win.top;
1481 reply->win_right = screen_buffer->win.right;
1482 reply->win_bottom = screen_buffer->win.bottom;
1483 reply->max_width = screen_buffer->max_width;
1484 reply->max_height = screen_buffer->max_height;
1485 release_object( screen_buffer );
1489 /* read data (chars & attrs) from a screen buffer */
1490 DECL_HANDLER(read_console_output)
1492 struct screen_buffer *screen_buffer;
1494 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1495 CONSOLE_READ, &screen_buffer_ops )))
1497 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1498 reply->width = screen_buffer->width;
1499 reply->height = screen_buffer->height;
1500 release_object( screen_buffer );
1504 /* write data (char and/or attrs) to a screen buffer */
1505 DECL_HANDLER(write_console_output)
1507 struct screen_buffer *screen_buffer;
1509 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1510 CONSOLE_WRITE, &screen_buffer_ops)))
1512 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1513 req->mode, req->x, req->y, req->wrap );
1514 reply->width = screen_buffer->width;
1515 reply->height = screen_buffer->height;
1516 release_object( screen_buffer );
1520 /* fill a screen buffer with constant data (chars and/or attributes) */
1521 DECL_HANDLER(fill_console_output)
1523 struct screen_buffer *screen_buffer;
1525 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1526 CONSOLE_WRITE, &screen_buffer_ops)))
1528 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1529 req->x, req->y, req->count, req->wrap );
1530 release_object( screen_buffer );
1534 /* move a rect of data in a screen buffer */
1535 DECL_HANDLER(move_console_output)
1537 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1541 /* sends a signal to a console (process, group...) */
1542 DECL_HANDLER(send_console_signal)
1546 group = req->group_id ? req->group_id : current->process->group_id;
1549 set_error( STATUS_INVALID_PARAMETER );
1551 propagate_console_signal( current->process->console, req->signal, group );
1554 /* get console which renderer is 'current' */
1555 static int cgwe_enum( struct process* process, void* user)
1557 if (process->console && process->console->renderer == current)
1559 *(struct console_input**)user = process->console;
1565 DECL_HANDLER(get_console_wait_event)
1567 struct console_input* console = NULL;
1569 if (current->process->console && current->process->console->renderer)
1570 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1571 else enum_processes(cgwe_enum, &console);
1575 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1576 release_object( console );
1578 else set_error( STATUS_INVALID_PARAMETER );