mshtml: Added IDOMEvent::get_timeStamp implementation.
[wine.git] / server / console.c
blobcb6410b774105b338f083e3a35bb44242f9c938a
1 /*
2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
5 * 2001 Eric Pouech
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "handle.h"
35 #include "process.h"
36 #include "request.h"
37 #include "file.h"
38 #include "unicode.h"
39 #include "wincon.h"
40 #include "winternl.h"
42 struct screen_buffer;
43 struct console_input_events;
45 struct console_input
47 struct object obj; /* object header */
48 int num_proc; /* number of processes attached to this console */
49 struct thread *renderer; /* console renderer thread */
50 int mode; /* input mode */
51 struct screen_buffer *active; /* active screen buffer */
52 int recnum; /* number of input records */
53 INPUT_RECORD *records; /* input records */
54 struct console_input_events *evt; /* synchronization event with renderer */
55 WCHAR *title; /* console title */
56 WCHAR **history; /* lines history */
57 int history_size; /* number of entries in history array */
58 int history_index; /* number of used entries in history array */
59 int history_mode; /* mode of history (non zero means remove doubled strings */
60 int edition_mode; /* index to edition mode flavors */
61 int input_cp; /* console input codepage */
62 int output_cp; /* console output codepage */
63 user_handle_t win; /* window handle if backend supports it */
64 struct event *event; /* event to wait on for input queue */
65 struct fd *fd; /* for bare console, attached input fd */
68 static void console_input_dump( struct object *obj, int verbose );
69 static void console_input_destroy( struct object *obj );
70 static struct fd *console_input_get_fd( struct object *obj );
72 static const struct object_ops console_input_ops =
74 sizeof(struct console_input), /* size */
75 console_input_dump, /* dump */
76 no_get_type, /* get_type */
77 no_add_queue, /* add_queue */
78 NULL, /* remove_queue */
79 NULL, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 console_input_get_fd, /* get_fd */
83 default_fd_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_link_name, /* link_name */
88 NULL, /* unlink_name */
89 no_open_file, /* open_file */
90 no_close_handle, /* close_handle */
91 console_input_destroy /* destroy */
94 static void console_input_events_dump( struct object *obj, int verbose );
95 static void console_input_events_destroy( struct object *obj );
96 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
98 struct console_input_events
100 struct object obj; /* object header */
101 int num_alloc; /* number of allocated events */
102 int num_used; /* number of actually used events */
103 struct console_renderer_event* events;
106 static const struct object_ops console_input_events_ops =
108 sizeof(struct console_input_events), /* size */
109 console_input_events_dump, /* dump */
110 no_get_type, /* get_type */
111 add_queue, /* add_queue */
112 remove_queue, /* remove_queue */
113 console_input_events_signaled, /* signaled */
114 no_satisfied, /* satisfied */
115 no_signal, /* signal */
116 no_get_fd, /* get_fd */
117 default_fd_map_access, /* map_access */
118 default_get_sd, /* get_sd */
119 default_set_sd, /* set_sd */
120 no_lookup_name, /* lookup_name */
121 no_link_name, /* link_name */
122 NULL, /* unlink_name */
123 no_open_file, /* open_file */
124 no_close_handle, /* close_handle */
125 console_input_events_destroy /* destroy */
128 struct font_info
130 short int width;
131 short int height;
134 struct screen_buffer
136 struct object obj; /* object header */
137 struct list entry; /* entry in list of all screen buffers */
138 struct console_input *input; /* associated console input */
139 int mode; /* output mode */
140 int cursor_size; /* size of cursor (percentage filled) */
141 int cursor_visible;/* cursor visibility flag */
142 int cursor_x; /* position of cursor */
143 int cursor_y; /* position of cursor */
144 int width; /* size (w-h) of the screen buffer */
145 int height;
146 int max_width; /* size (w-h) of the window given font size */
147 int max_height;
148 char_info_t *data; /* the data for each cell - a width x height matrix */
149 unsigned short attr; /* default fill attributes (screen colors) */
150 unsigned short popup_attr; /* pop-up color attributes */
151 unsigned int color_map[16]; /* color table */
152 rectangle_t win; /* current visible window on the screen buffer *
153 * as seen in wineconsole */
154 struct font_info font; /* console font information */
155 struct fd *fd; /* for bare console, attached output fd */
158 static void screen_buffer_dump( struct object *obj, int verbose );
159 static void screen_buffer_destroy( struct object *obj );
160 static struct fd *screen_buffer_get_fd( struct object *obj );
162 static const struct object_ops screen_buffer_ops =
164 sizeof(struct screen_buffer), /* size */
165 screen_buffer_dump, /* dump */
166 no_get_type, /* get_type */
167 no_add_queue, /* add_queue */
168 NULL, /* remove_queue */
169 NULL, /* signaled */
170 NULL, /* satisfied */
171 no_signal, /* signal */
172 screen_buffer_get_fd, /* get_fd */
173 default_fd_map_access, /* map_access */
174 default_get_sd, /* get_sd */
175 default_set_sd, /* set_sd */
176 no_lookup_name, /* lookup_name */
177 no_link_name, /* link_name */
178 NULL, /* unlink_name */
179 no_open_file, /* open_file */
180 no_close_handle, /* close_handle */
181 screen_buffer_destroy /* destroy */
184 static enum server_fd_type console_get_fd_type( struct fd *fd );
186 static const struct fd_ops console_fd_ops =
188 default_fd_get_poll_events, /* get_poll_events */
189 default_poll_event, /* poll_event */
190 console_get_fd_type, /* get_fd_type */
191 no_fd_read, /* read */
192 no_fd_write, /* write */
193 no_fd_flush, /* flush */
194 no_fd_get_volume_info, /* get_volume_info */
195 default_fd_ioctl, /* ioctl */
196 default_fd_queue_async, /* queue_async */
197 default_fd_reselect_async /* reselect_async */
200 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
202 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
204 static int console_input_is_bare( struct console_input* cin )
206 return cin->evt == NULL;
209 static struct fd *console_input_get_fd( struct object* obj )
211 struct console_input *console_input = (struct console_input*)obj;
212 assert( obj->ops == &console_input_ops );
213 if (console_input->fd)
214 return (struct fd*)grab_object( console_input->fd );
215 set_error( STATUS_OBJECT_TYPE_MISMATCH );
216 return NULL;
219 static enum server_fd_type console_get_fd_type( struct fd *fd )
221 return FD_TYPE_CHAR;
224 /* dumps the renderer events of a console */
225 static void console_input_events_dump( struct object *obj, int verbose )
227 struct console_input_events *evts = (struct console_input_events *)obj;
228 assert( obj->ops == &console_input_events_ops );
229 fprintf( stderr, "Console input events: %d/%d events\n",
230 evts->num_used, evts->num_alloc );
233 /* destroys the renderer events of a console */
234 static void console_input_events_destroy( struct object *obj )
236 struct console_input_events *evts = (struct console_input_events *)obj;
237 assert( obj->ops == &console_input_events_ops );
238 free( evts->events );
241 /* the renderer events list is signaled when it's not empty */
242 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
244 struct console_input_events *evts = (struct console_input_events *)obj;
245 assert( obj->ops == &console_input_events_ops );
246 return (evts->num_used != 0);
249 /* add an event to the console's renderer events list */
250 static void console_input_events_append( struct console_input* console,
251 struct console_renderer_event* evt)
253 struct console_input_events* evts;
254 int collapsed = FALSE;
256 if (!(evts = console->evt)) return;
257 /* to be done even when evt has been generated by the renderer ? */
259 /* try to collapse evt into current queue's events */
260 if (evts->num_used)
262 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
264 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
265 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
267 /* if two update events overlap, collapse them into a single one */
268 if (last->u.update.bottom + 1 >= evt->u.update.top &&
269 evt->u.update.bottom + 1 >= last->u.update.top)
271 last->u.update.top = min(last->u.update.top, evt->u.update.top);
272 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
273 collapsed = TRUE;
277 if (!collapsed)
279 if (evts->num_used == evts->num_alloc)
281 evts->num_alloc += 16;
282 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
283 assert(evts->events);
285 evts->events[evts->num_used++] = *evt;
287 wake_up( &evts->obj, 0 );
290 /* retrieves events from the console's renderer events list */
291 static void console_input_events_get( struct console_input_events* evts )
293 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
295 if (num > evts->num_used) num = evts->num_used;
296 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
297 if (num < evts->num_used)
299 memmove( &evts->events[0], &evts->events[num],
300 (evts->num_used - num) * sizeof(evts->events[0]) );
302 evts->num_used -= num;
305 static struct console_input_events *create_console_input_events(void)
307 struct console_input_events* evt;
309 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
310 evt->num_alloc = evt->num_used = 0;
311 evt->events = NULL;
312 return evt;
315 static struct object *create_console_input( struct thread* renderer, int fd )
317 struct console_input *console_input;
319 if (!(console_input = alloc_object( &console_input_ops )))
321 if (fd != -1) close( fd );
322 return NULL;
324 console_input->renderer = renderer;
325 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
326 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
327 ENABLE_EXTENDED_FLAGS;
328 console_input->num_proc = 0;
329 console_input->active = NULL;
330 console_input->recnum = 0;
331 console_input->records = NULL;
332 console_input->evt = renderer ? create_console_input_events() : NULL;
333 console_input->title = NULL;
334 console_input->history_size = 50;
335 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
336 console_input->history_index = 0;
337 console_input->history_mode = 0;
338 console_input->edition_mode = 0;
339 console_input->input_cp = 0;
340 console_input->output_cp = 0;
341 console_input->win = 0;
342 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
343 console_input->fd = NULL;
345 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
347 if (fd != -1) close( fd );
348 console_input->history_size = 0;
349 release_object( console_input );
350 return NULL;
352 if (fd != -1) /* bare console */
354 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
355 FILE_SYNCHRONOUS_IO_NONALERT )))
357 release_object( console_input );
358 return NULL;
360 allow_fd_caching( console_input->fd );
363 return &console_input->obj;
366 static void generate_sb_initial_events( struct console_input *console_input )
368 struct screen_buffer *screen_buffer = console_input->active;
369 struct console_renderer_event evt;
371 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
372 memset(&evt.u, 0, sizeof(evt.u));
373 console_input_events_append( console_input, &evt );
375 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
376 evt.u.resize.width = screen_buffer->width;
377 evt.u.resize.height = screen_buffer->height;
378 console_input_events_append( console_input, &evt );
380 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
381 evt.u.display.left = screen_buffer->win.left;
382 evt.u.display.top = screen_buffer->win.top;
383 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
384 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
385 console_input_events_append( console_input, &evt );
387 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
388 evt.u.update.top = 0;
389 evt.u.update.bottom = screen_buffer->height - 1;
390 console_input_events_append( console_input, &evt );
392 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
393 evt.u.cursor_geom.size = screen_buffer->cursor_size;
394 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
395 console_input_events_append( console_input, &evt );
397 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
398 evt.u.cursor_pos.x = screen_buffer->cursor_x;
399 evt.u.cursor_pos.y = screen_buffer->cursor_y;
400 console_input_events_append( console_input, &evt );
403 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
405 struct screen_buffer *screen_buffer;
406 int i;
408 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
410 if (fd != -1) close( fd );
411 return NULL;
413 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
414 screen_buffer->input = console_input;
415 screen_buffer->cursor_size = 100;
416 screen_buffer->cursor_visible = 1;
417 screen_buffer->width = 80;
418 screen_buffer->height = 150;
419 screen_buffer->max_width = 80;
420 screen_buffer->max_height = 25;
421 screen_buffer->cursor_x = 0;
422 screen_buffer->cursor_y = 0;
423 screen_buffer->attr = 0x0F;
424 screen_buffer->popup_attr = 0xF5;
425 screen_buffer->win.left = 0;
426 screen_buffer->win.right = screen_buffer->max_width - 1;
427 screen_buffer->win.top = 0;
428 screen_buffer->win.bottom = screen_buffer->max_height - 1;
429 screen_buffer->data = NULL;
430 screen_buffer->font.width = 0;
431 screen_buffer->font.height = 0;
432 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
433 list_add_head( &screen_buffer_list, &screen_buffer->entry );
435 if (fd == -1)
436 screen_buffer->fd = NULL;
437 else
439 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
440 FILE_SYNCHRONOUS_IO_NONALERT )))
442 release_object( screen_buffer );
443 return NULL;
445 allow_fd_caching(screen_buffer->fd);
448 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
449 sizeof(*screen_buffer->data) )))
451 release_object( screen_buffer );
452 return NULL;
454 /* clear the first row */
455 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
456 /* and copy it to all other rows */
457 for (i = 1; i < screen_buffer->height; i++)
458 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
459 screen_buffer->width * sizeof(char_info_t) );
461 if (!console_input->active)
463 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
464 generate_sb_initial_events( console_input );
466 return screen_buffer;
469 /* free the console for this process */
470 int free_console( struct process *process )
472 struct console_input* console = process->console;
474 if (!console) return 0;
476 process->console = NULL;
477 if (--console->num_proc == 0 && console->renderer)
479 /* all processes have terminated... tell the renderer to terminate too */
480 struct console_renderer_event evt;
481 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
482 memset(&evt.u, 0, sizeof(evt.u));
483 console_input_events_append( console, &evt );
485 release_object( console );
487 return 1;
490 /* let process inherit the console from parent... this handle two cases :
491 * 1/ generic console inheritance
492 * 2/ parent is a renderer which launches process, and process should attach to the console
493 * rendered by parent
495 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
497 int done = 0;
498 struct process* parent = parent_thread->process;
500 /* if parent is a renderer, then attach current process to its console
501 * a bit hacky....
503 if (hconin)
505 struct console_input* console;
507 /* FIXME: should we check some access rights ? */
508 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
509 0, &console_input_ops )))
511 if (console->renderer == parent_thread)
513 process->console = (struct console_input*)grab_object( console );
514 process->console->num_proc++;
515 done = 1;
517 release_object( console );
519 else clear_error(); /* ignore error */
521 /* otherwise, if parent has a console, attach child to this console */
522 if (!done && parent->console)
524 process->console = (struct console_input*)grab_object( parent->console );
525 process->console->num_proc++;
529 struct thread *console_get_renderer( struct console_input *console )
531 return console->renderer;
534 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
536 struct console_input* console = NULL;
538 if (handle)
539 console = (struct console_input *)get_handle_obj( current->process, handle,
540 access, &console_input_ops );
541 else if (current->process->console)
543 console = (struct console_input *)grab_object( current->process->console );
546 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
547 return console;
550 struct console_signal_info
552 struct console_input *console;
553 process_id_t group;
554 int signal;
557 static int propagate_console_signal_cb(struct process *process, void *user)
559 struct console_signal_info* csi = (struct console_signal_info*)user;
561 if (process->console == csi->console && process->running_threads &&
562 (!csi->group || process->group_id == csi->group))
564 /* find a suitable thread to signal */
565 struct thread *thread;
566 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
568 if (send_thread_signal( thread, csi->signal )) break;
571 return FALSE;
574 static void propagate_console_signal( struct console_input *console,
575 int sig, process_id_t group_id )
577 struct console_signal_info csi;
579 if (!console)
581 set_error( STATUS_INVALID_PARAMETER );
582 return;
584 /* FIXME: should support the other events (like CTRL_BREAK) */
585 if (sig != CTRL_C_EVENT)
587 set_error( STATUS_NOT_IMPLEMENTED );
588 return;
590 csi.console = console;
591 csi.signal = SIGINT;
592 csi.group = group_id;
594 enum_processes(propagate_console_signal_cb, &csi);
597 static int get_console_mode( obj_handle_t handle )
599 struct object *obj;
600 int ret = 0;
602 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
604 if (obj->ops == &console_input_ops)
606 ret = ((struct console_input *)obj)->mode;
608 else if (obj->ops == &screen_buffer_ops)
610 ret = ((struct screen_buffer *)obj)->mode;
612 else
613 set_error( STATUS_OBJECT_TYPE_MISMATCH );
614 release_object( obj );
616 return ret;
619 /* changes the mode of either a console input or a screen buffer */
620 static int set_console_mode( obj_handle_t handle, int mode )
622 struct object *obj;
623 int ret = 0;
625 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
626 return 0;
627 if (obj->ops == &console_input_ops)
629 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
630 ((struct console_input *)obj)->mode = mode;
631 ret = 1;
633 else if (obj->ops == &screen_buffer_ops)
635 ((struct screen_buffer *)obj)->mode = mode;
636 ret = 1;
638 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
639 release_object( obj );
640 return ret;
643 /* add input events to a console input queue */
644 static int write_console_input( struct console_input* console, int count,
645 const INPUT_RECORD *records )
647 INPUT_RECORD *new_rec;
649 if (!count) return 0;
650 if (!(new_rec = realloc( console->records,
651 (console->recnum + count) * sizeof(INPUT_RECORD) )))
653 set_error( STATUS_NO_MEMORY );
654 return -1;
656 console->records = new_rec;
657 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
659 if (console->mode & ENABLE_PROCESSED_INPUT)
661 int i = 0;
662 while (i < count)
664 if (records[i].EventType == KEY_EVENT &&
665 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
666 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
668 if (i != count - 1)
669 memcpy( &console->records[console->recnum + i],
670 &console->records[console->recnum + i + 1],
671 (count - i - 1) * sizeof(INPUT_RECORD) );
672 count--;
673 if (records[i].Event.KeyEvent.bKeyDown)
675 /* send SIGINT to all processes attached to this console */
676 propagate_console_signal( console, CTRL_C_EVENT, 0 );
679 else i++;
682 if (!console->recnum && count) set_event( console->event );
683 console->recnum += count;
684 return count;
687 /* retrieve a pointer to the console input records */
688 static int read_console_input( obj_handle_t handle, int count, int flush )
690 struct console_input *console;
692 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
693 FILE_READ_DATA, &console_input_ops )))
694 return -1;
696 if (!count)
698 /* special case: do not retrieve anything, but return
699 * the total number of records available */
700 count = console->recnum;
702 else
704 if (count > console->recnum) count = console->recnum;
705 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
707 if (flush)
709 int i;
710 for (i = count; i < console->recnum; i++)
711 console->records[i-count] = console->records[i];
712 if ((console->recnum -= count) > 0)
714 INPUT_RECORD *new_rec = realloc( console->records,
715 console->recnum * sizeof(INPUT_RECORD) );
716 if (new_rec) console->records = new_rec;
718 else
720 free( console->records );
721 console->records = NULL;
722 reset_event( console->event );
725 release_object( console );
726 return count;
729 /* set misc console input information */
730 static int set_console_input_info( const struct set_console_input_info_request *req,
731 const WCHAR *title, data_size_t len )
733 struct console_input *console;
734 struct console_renderer_event evt;
736 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
737 if (console_input_is_bare(console) &&
738 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
739 SET_CONSOLE_INPUT_INFO_WIN)))
741 set_error( STATUS_UNSUCCESSFUL );
742 goto error;
745 memset(&evt.u, 0, sizeof(evt.u));
746 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
748 struct screen_buffer *screen_buffer;
750 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
751 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
752 if (!screen_buffer || screen_buffer->input != console)
754 set_error( STATUS_INVALID_HANDLE );
755 if (screen_buffer) release_object( screen_buffer );
756 goto error;
759 if (screen_buffer != console->active)
761 if (console->active) release_object( console->active );
762 console->active = screen_buffer;
763 generate_sb_initial_events( console );
765 else
766 release_object( screen_buffer );
768 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
770 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
771 if (new_title)
773 memcpy( new_title, title, len );
774 new_title[len / sizeof(WCHAR)] = 0;
775 free( console->title );
776 console->title = new_title;
777 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
778 console_input_events_append( console, &evt );
781 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
783 console->history_mode = req->history_mode;
785 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
786 console->history_size != req->history_size)
788 WCHAR** mem = NULL;
789 int i;
790 int delta;
792 if (req->history_size)
794 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
795 if (!mem) goto error;
796 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
799 delta = (console->history_index > req->history_size) ?
800 (console->history_index - req->history_size) : 0;
802 for (i = delta; i < console->history_index; i++)
804 mem[i - delta] = console->history[i];
805 console->history[i] = NULL;
807 console->history_index -= delta;
809 for (i = 0; i < console->history_size; i++)
810 free( console->history[i] );
811 free( console->history );
812 console->history = mem;
813 console->history_size = req->history_size;
815 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
817 console->edition_mode = req->edition_mode;
819 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
821 console->input_cp = req->input_cp;
823 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
825 console->output_cp = req->output_cp;
827 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
829 console->win = req->win;
831 release_object( console );
832 return 1;
833 error:
834 if (console) release_object( console );
835 return 0;
838 /* resize a screen buffer */
839 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
840 int new_width, int new_height )
842 int i, old_width, old_height, copy_width, copy_height;
843 char_info_t *new_data;
845 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
847 set_error( STATUS_NO_MEMORY );
848 return 0;
850 old_width = screen_buffer->width;
851 old_height = screen_buffer->height;
852 copy_width = min( old_width, new_width );
853 copy_height = min( old_height, new_height );
855 /* copy all the rows */
856 for (i = 0; i < copy_height; i++)
858 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
859 copy_width * sizeof(char_info_t) );
862 /* clear the end of each row */
863 if (new_width > old_width)
865 /* fill first row */
866 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
867 /* and blast it to the other rows */
868 for (i = 1; i < copy_height; i++)
869 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
870 (new_width - old_width) * sizeof(char_info_t) );
873 /* clear remaining rows */
874 if (new_height > old_height)
876 /* fill first row */
877 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
878 /* and blast it to the other rows */
879 for (i = old_height+1; i < new_height; i++)
880 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
881 new_width * sizeof(char_info_t) );
883 free( screen_buffer->data );
884 screen_buffer->data = new_data;
885 screen_buffer->width = new_width;
886 screen_buffer->height = new_height;
887 return 1;
890 /* set misc screen buffer information */
891 static int set_console_output_info( struct screen_buffer *screen_buffer,
892 const struct set_console_output_info_request *req )
894 struct console_renderer_event evt;
896 memset(&evt.u, 0, sizeof(evt.u));
897 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
899 if (req->cursor_size < 1 || req->cursor_size > 100)
901 set_error( STATUS_INVALID_PARAMETER );
902 return 0;
904 if (screen_buffer->cursor_size != req->cursor_size ||
905 screen_buffer->cursor_visible != req->cursor_visible)
907 screen_buffer->cursor_size = req->cursor_size;
908 screen_buffer->cursor_visible = req->cursor_visible;
909 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
910 evt.u.cursor_geom.size = req->cursor_size;
911 evt.u.cursor_geom.visible = req->cursor_visible;
912 console_input_events_append( screen_buffer->input, &evt );
915 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
917 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
918 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
920 set_error( STATUS_INVALID_PARAMETER );
921 return 0;
923 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
925 screen_buffer->cursor_x = req->cursor_x;
926 screen_buffer->cursor_y = req->cursor_y;
927 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
928 evt.u.cursor_pos.x = req->cursor_x;
929 evt.u.cursor_pos.y = req->cursor_y;
930 console_input_events_append( screen_buffer->input, &evt );
933 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
935 unsigned cc;
937 /* new screen-buffer cannot be smaller than actual window */
938 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
939 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
941 set_error( STATUS_INVALID_PARAMETER );
942 return 0;
944 /* FIXME: there are also some basic minimum and max size to deal with */
945 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
947 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
948 evt.u.resize.width = req->width;
949 evt.u.resize.height = req->height;
950 console_input_events_append( screen_buffer->input, &evt );
952 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
953 evt.u.update.top = 0;
954 evt.u.update.bottom = screen_buffer->height - 1;
955 console_input_events_append( screen_buffer->input, &evt );
957 /* scroll window to display sb */
958 if (screen_buffer->win.right >= req->width)
960 screen_buffer->win.right -= screen_buffer->win.left;
961 screen_buffer->win.left = 0;
963 if (screen_buffer->win.bottom >= req->height)
965 screen_buffer->win.bottom -= screen_buffer->win.top;
966 screen_buffer->win.top = 0;
968 /* reset cursor if needed (normally, if cursor was outside of new sb, the
969 * window has been shifted so that the new position of the cursor will be
970 * visible */
971 cc = 0;
972 if (screen_buffer->cursor_x >= req->width)
974 screen_buffer->cursor_x = req->width - 1;
975 cc++;
977 if (screen_buffer->cursor_y >= req->height)
979 screen_buffer->cursor_y = req->height - 1;
980 cc++;
982 if (cc)
984 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
985 evt.u.cursor_pos.x = req->cursor_x;
986 evt.u.cursor_pos.y = req->cursor_y;
987 console_input_events_append( screen_buffer->input, &evt );
990 if (screen_buffer == screen_buffer->input->active &&
991 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
993 INPUT_RECORD ir;
994 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
995 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
996 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
997 write_console_input( screen_buffer->input, 1, &ir );
1000 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1002 screen_buffer->attr = req->attr;
1004 if (req->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1006 screen_buffer->popup_attr = req->popup_attr;
1008 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1010 if (req->win_left < 0 || req->win_left > req->win_right ||
1011 req->win_right >= screen_buffer->width ||
1012 req->win_top < 0 || req->win_top > req->win_bottom ||
1013 req->win_bottom >= screen_buffer->height)
1015 set_error( STATUS_INVALID_PARAMETER );
1016 return 0;
1018 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1019 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1021 screen_buffer->win.left = req->win_left;
1022 screen_buffer->win.top = req->win_top;
1023 screen_buffer->win.right = req->win_right;
1024 screen_buffer->win.bottom = req->win_bottom;
1025 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1026 evt.u.display.left = req->win_left;
1027 evt.u.display.top = req->win_top;
1028 evt.u.display.width = req->win_right - req->win_left + 1;
1029 evt.u.display.height = req->win_bottom - req->win_top + 1;
1030 console_input_events_append( screen_buffer->input, &evt );
1033 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1035 screen_buffer->max_width = req->max_width;
1036 screen_buffer->max_height = req->max_height;
1038 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1040 screen_buffer->font.width = req->font_width;
1041 screen_buffer->font.height = req->font_height;
1043 if (req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1045 memcpy( screen_buffer->color_map, get_req_data(),
1046 min( sizeof(screen_buffer->color_map), get_req_data_size() ));
1049 return 1;
1052 /* appends a new line to history (history is a fixed size array) */
1053 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1055 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1057 if (!ptr)
1058 return;
1060 if (!console || !console->history_size)
1062 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1063 free( ptr );
1064 return;
1067 memcpy( ptr, buf, len * sizeof(WCHAR) );
1068 ptr[len] = 0;
1070 if (console->history_mode && console->history_index &&
1071 !strcmpW( console->history[console->history_index - 1], ptr ))
1073 /* ok, mode ask us to not use twice the same string...
1074 * so just free mem and returns
1076 set_error( STATUS_ALIAS_EXISTS );
1077 free(ptr);
1078 return;
1081 if (console->history_index < console->history_size)
1083 console->history[console->history_index++] = ptr;
1085 else
1087 free( console->history[0]) ;
1088 memmove( &console->history[0], &console->history[1],
1089 (console->history_size - 1) * sizeof(WCHAR*) );
1090 console->history[console->history_size - 1] = ptr;
1094 /* returns a line from the cache */
1095 static data_size_t console_input_get_hist( struct console_input *console, int index )
1097 data_size_t ret = 0;
1099 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1100 else
1102 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1103 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1105 return ret;
1108 /* dumb dump */
1109 static void console_input_dump( struct object *obj, int verbose )
1111 struct console_input *console = (struct console_input *)obj;
1112 assert( obj->ops == &console_input_ops );
1113 fprintf( stderr, "Console input active=%p evt=%p\n",
1114 console->active, console->evt );
1117 static void console_input_destroy( struct object *obj )
1119 struct console_input* console_in = (struct console_input *)obj;
1120 struct screen_buffer* curr;
1121 int i;
1123 assert( obj->ops == &console_input_ops );
1124 free( console_in->title );
1125 free( console_in->records );
1127 if (console_in->active) release_object( console_in->active );
1128 console_in->active = NULL;
1130 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1132 if (curr->input == console_in) curr->input = NULL;
1135 if (console_in->evt)
1137 release_object( console_in->evt );
1138 console_in->evt = NULL;
1140 if (console_in->event)
1141 release_object( console_in->event );
1142 if (console_in->fd)
1143 release_object( console_in->fd );
1145 for (i = 0; i < console_in->history_size; i++)
1146 free( console_in->history[i] );
1147 free( console_in->history );
1150 static void screen_buffer_dump( struct object *obj, int verbose )
1152 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1153 assert( obj->ops == &screen_buffer_ops );
1155 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1158 static void screen_buffer_destroy( struct object *obj )
1160 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1162 assert( obj->ops == &screen_buffer_ops );
1164 list_remove( &screen_buffer->entry );
1166 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1168 struct screen_buffer *sb;
1170 screen_buffer->input->active = NULL;
1171 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1173 if (sb->input == screen_buffer->input)
1175 sb->input->active = sb;
1176 break;
1180 if (screen_buffer->fd) release_object( screen_buffer->fd );
1181 free( screen_buffer->data );
1184 static struct fd *screen_buffer_get_fd( struct object *obj )
1186 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1187 assert( obj->ops == &screen_buffer_ops );
1188 if (screen_buffer->fd)
1189 return (struct fd*)grab_object( screen_buffer->fd );
1190 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1191 return NULL;
1194 /* write data into a screen buffer */
1195 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1196 const void* data, enum char_info_mode mode,
1197 int x, int y, int wrap )
1199 unsigned int i;
1200 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1202 if (y >= screen_buffer->height) return 0;
1204 if (wrap)
1205 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1206 else
1207 end = screen_buffer->data + (y+1) * screen_buffer->width;
1209 switch(mode)
1211 case CHAR_INFO_MODE_TEXT:
1213 const WCHAR *ptr = data;
1214 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1216 break;
1217 case CHAR_INFO_MODE_ATTR:
1219 const unsigned short *ptr = data;
1220 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1222 break;
1223 case CHAR_INFO_MODE_TEXTATTR:
1225 const char_info_t *ptr = data;
1226 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1228 break;
1229 case CHAR_INFO_MODE_TEXTSTDATTR:
1231 const WCHAR *ptr = data;
1232 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1234 dest->ch = ptr[i];
1235 dest->attr = screen_buffer->attr;
1238 break;
1239 default:
1240 set_error( STATUS_INVALID_PARAMETER );
1241 return 0;
1244 if (i && screen_buffer == screen_buffer->input->active)
1246 struct console_renderer_event evt;
1247 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1248 memset(&evt.u, 0, sizeof(evt.u));
1249 evt.u.update.top = y + x / screen_buffer->width;
1250 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1251 console_input_events_append( screen_buffer->input, &evt );
1253 return i;
1256 /* fill a screen buffer with uniform data */
1257 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1258 enum char_info_mode mode, int x, int y, int count, int wrap )
1260 int i;
1261 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1263 if (y >= screen_buffer->height) return 0;
1265 if (wrap)
1266 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1267 else
1268 end = screen_buffer->data + (y+1) * screen_buffer->width;
1270 if (count > end - dest) count = end - dest;
1272 switch(mode)
1274 case CHAR_INFO_MODE_TEXT:
1275 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1276 break;
1277 case CHAR_INFO_MODE_ATTR:
1278 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1279 break;
1280 case CHAR_INFO_MODE_TEXTATTR:
1281 for (i = 0; i < count; i++) dest[i] = data;
1282 break;
1283 case CHAR_INFO_MODE_TEXTSTDATTR:
1284 for (i = 0; i < count; i++)
1286 dest[i].ch = data.ch;
1287 dest[i].attr = screen_buffer->attr;
1289 break;
1290 default:
1291 set_error( STATUS_INVALID_PARAMETER );
1292 return 0;
1295 if (count && screen_buffer == screen_buffer->input->active)
1297 struct console_renderer_event evt;
1298 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1299 memset(&evt.u, 0, sizeof(evt.u));
1300 evt.u.update.top = y;
1301 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1302 console_input_events_append( screen_buffer->input, &evt );
1304 return i;
1307 /* read data from a screen buffer */
1308 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1309 enum char_info_mode mode, int wrap )
1311 int i;
1312 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1314 if (y >= screen_buffer->height) return;
1316 if (wrap)
1317 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1318 else
1319 end = screen_buffer->data + (y+1) * screen_buffer->width;
1321 switch(mode)
1323 case CHAR_INFO_MODE_TEXT:
1325 WCHAR *data;
1326 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1327 if ((data = set_reply_data_size( count * sizeof(*data) )))
1329 for (i = 0; i < count; i++) data[i] = src[i].ch;
1332 break;
1333 case CHAR_INFO_MODE_ATTR:
1335 unsigned short *data;
1336 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1337 if ((data = set_reply_data_size( count * sizeof(*data) )))
1339 for (i = 0; i < count; i++) data[i] = src[i].attr;
1342 break;
1343 case CHAR_INFO_MODE_TEXTATTR:
1345 char_info_t *data;
1346 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1347 if ((data = set_reply_data_size( count * sizeof(*data) )))
1349 for (i = 0; i < count; i++) data[i] = src[i];
1352 break;
1353 default:
1354 set_error( STATUS_INVALID_PARAMETER );
1355 break;
1359 /* scroll parts of a screen buffer */
1360 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1361 int w, int h )
1363 int j;
1364 char_info_t *psrc, *pdst;
1365 struct console_renderer_event evt;
1367 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1368 xsrc + w > screen_buffer->width ||
1369 xdst + w > screen_buffer->width ||
1370 ysrc + h > screen_buffer->height ||
1371 ydst + h > screen_buffer->height ||
1372 w == 0 || h == 0)
1374 set_error( STATUS_INVALID_PARAMETER );
1375 return;
1378 if (ysrc < ydst)
1380 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1381 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1383 for (j = h; j > 0; j--)
1385 memcpy(pdst, psrc, w * sizeof(*pdst) );
1386 pdst -= screen_buffer->width;
1387 psrc -= screen_buffer->width;
1390 else
1392 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1393 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1395 for (j = 0; j < h; j++)
1397 /* we use memmove here because when psrc and pdst are the same,
1398 * copies are done on the same row, so the dst and src blocks
1399 * can overlap */
1400 memmove( pdst, psrc, w * sizeof(*pdst) );
1401 pdst += screen_buffer->width;
1402 psrc += screen_buffer->width;
1406 /* FIXME: this could be enhanced, by signalling scroll */
1407 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1408 memset(&evt.u, 0, sizeof(evt.u));
1409 evt.u.update.top = min(ysrc, ydst);
1410 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1411 console_input_events_append( screen_buffer->input, &evt );
1414 /* allocate a console for the renderer */
1415 DECL_HANDLER(alloc_console)
1417 obj_handle_t in = 0;
1418 obj_handle_t evt = 0;
1419 struct process *process;
1420 struct thread *renderer;
1421 struct console_input *console;
1422 int fd;
1423 int attach = 0;
1425 if (req->input_fd != -1)
1427 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1429 set_error( STATUS_INVALID_PARAMETER );
1430 return;
1433 else fd = -1;
1435 switch (req->pid)
1437 case 0:
1438 /* renderer is current, console to be attached to parent process */
1439 renderer = current;
1440 if (!(process = get_process_from_id( current->process->parent_id )))
1442 if (fd != -1) close( fd );
1443 set_error( STATUS_ACCESS_DENIED );
1444 return;
1446 attach = 1;
1447 break;
1448 case 0xffffffff:
1449 /* no renderer, console to be attached to current process */
1450 renderer = NULL;
1451 process = current->process;
1452 grab_object( process );
1453 attach = 1;
1454 break;
1455 default:
1456 /* renderer is current, console to be attached to req->pid */
1457 renderer = current;
1458 if (!(process = get_process_from_id( req->pid )))
1460 if (fd != -1) close( fd );
1461 return;
1465 if (attach && process->console)
1467 if (fd != -1) close( fd );
1468 set_error( STATUS_ACCESS_DENIED );
1469 goto the_end;
1472 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1474 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1476 if (!console->evt ||
1477 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1479 if (attach)
1481 process->console = (struct console_input*)grab_object( console );
1482 console->num_proc++;
1484 reply->handle_in = in;
1485 reply->event = evt;
1486 release_object( console );
1487 goto the_end;
1489 close_handle( current->process, in );
1491 release_object( console );
1493 the_end:
1494 release_object( process );
1497 /* free the console of the current process */
1498 DECL_HANDLER(free_console)
1500 free_console( current->process );
1503 /* let the renderer peek the events it's waiting on */
1504 DECL_HANDLER(get_console_renderer_events)
1506 struct console_input_events *evt;
1508 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1509 FILE_READ_PROPERTIES, &console_input_events_ops );
1510 if (!evt) return;
1511 console_input_events_get( evt );
1512 release_object( evt );
1515 /* open a handle to the process console */
1516 DECL_HANDLER(open_console)
1518 struct object *obj = NULL;
1520 reply->handle = 0;
1521 if (!req->from)
1523 if (current->process->console)
1524 obj = grab_object( (struct object*)current->process->console );
1526 else if (req->from == (obj_handle_t)1)
1528 if (current->process->console && current->process->console->active)
1529 obj = grab_object( (struct object*)current->process->console->active );
1531 else if ((obj = get_handle_obj( current->process, req->from,
1532 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1534 struct console_input *console = (struct console_input *)obj;
1535 obj = (console->active) ? grab_object( console->active ) : NULL;
1536 release_object( console );
1539 /* FIXME: req->share is not used (as in screen buffer creation) */
1540 if (obj)
1542 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1543 release_object( obj );
1545 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1548 /* set info about a console input */
1549 DECL_HANDLER(set_console_input_info)
1551 set_console_input_info( req, get_req_data(), get_req_data_size() );
1554 /* get info about a console (output only) */
1555 DECL_HANDLER(get_console_input_info)
1557 struct console_input *console;
1559 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1560 if (console->title)
1562 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1563 if (len > get_reply_max_size()) len = get_reply_max_size();
1564 set_reply_data( console->title, len );
1566 reply->history_mode = console->history_mode;
1567 reply->history_size = console->history_size;
1568 reply->history_index = console->history_index;
1569 reply->edition_mode = console->edition_mode;
1570 reply->input_cp = console->input_cp;
1571 reply->output_cp = console->output_cp;
1572 reply->win = console->win;
1574 release_object( console );
1577 /* get a console mode (input or output) */
1578 DECL_HANDLER(get_console_mode)
1580 reply->mode = get_console_mode( req->handle );
1583 /* set a console mode (input or output) */
1584 DECL_HANDLER(set_console_mode)
1586 set_console_mode( req->handle, req->mode );
1589 /* add input records to a console input queue */
1590 DECL_HANDLER(write_console_input)
1592 struct console_input *console;
1594 reply->written = 0;
1595 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1596 FILE_WRITE_PROPERTIES, &console_input_ops )))
1597 return;
1598 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1599 get_req_data() );
1600 release_object( console );
1603 /* fetch input records from a console input queue */
1604 DECL_HANDLER(read_console_input)
1606 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1607 reply->read = read_console_input( req->handle, count, req->flush );
1610 /* appends a string to console's history */
1611 DECL_HANDLER(append_console_input_history)
1613 struct console_input *console;
1615 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1616 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1617 release_object( console );
1620 /* appends a string to console's history */
1621 DECL_HANDLER(get_console_input_history)
1623 struct console_input *console;
1625 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1626 reply->total = console_input_get_hist( console, req->index );
1627 release_object( console );
1630 /* creates a screen buffer */
1631 DECL_HANDLER(create_console_output)
1633 struct console_input* console;
1634 struct screen_buffer* screen_buffer;
1635 int fd;
1637 if (req->fd != -1)
1639 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1641 set_error( STATUS_INVALID_HANDLE );
1642 return;
1645 else fd = -1;
1646 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1648 if (fd != -1) close( fd );
1649 return;
1651 if (console_input_is_bare( console ) ^ (fd != -1))
1653 if (fd != -1) close( fd );
1654 release_object( console );
1655 set_error( STATUS_INVALID_HANDLE );
1656 return;
1659 screen_buffer = create_console_output( console, fd );
1660 if (screen_buffer)
1662 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1663 * see file.c on how this could be done */
1664 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1665 release_object( screen_buffer );
1667 release_object( console );
1670 /* set info about a console screen buffer */
1671 DECL_HANDLER(set_console_output_info)
1673 struct screen_buffer *screen_buffer;
1675 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1676 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1678 set_console_output_info( screen_buffer, req );
1679 release_object( screen_buffer );
1683 /* get info about a console screen buffer */
1684 DECL_HANDLER(get_console_output_info)
1686 struct screen_buffer *screen_buffer;
1688 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1689 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1691 reply->cursor_size = screen_buffer->cursor_size;
1692 reply->cursor_visible = screen_buffer->cursor_visible;
1693 reply->cursor_x = screen_buffer->cursor_x;
1694 reply->cursor_y = screen_buffer->cursor_y;
1695 reply->width = screen_buffer->width;
1696 reply->height = screen_buffer->height;
1697 reply->attr = screen_buffer->attr;
1698 reply->popup_attr = screen_buffer->popup_attr;
1699 reply->win_left = screen_buffer->win.left;
1700 reply->win_top = screen_buffer->win.top;
1701 reply->win_right = screen_buffer->win.right;
1702 reply->win_bottom = screen_buffer->win.bottom;
1703 reply->max_width = screen_buffer->max_width;
1704 reply->max_height = screen_buffer->max_height;
1705 reply->font_width = screen_buffer->font.width;
1706 reply->font_height = screen_buffer->font.height;
1707 set_reply_data( screen_buffer->color_map,
1708 min( sizeof(screen_buffer->color_map), get_reply_max_size() ));
1709 release_object( screen_buffer );
1713 /* read data (chars & attrs) from a screen buffer */
1714 DECL_HANDLER(read_console_output)
1716 struct screen_buffer *screen_buffer;
1718 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1719 FILE_READ_DATA, &screen_buffer_ops )))
1721 if (console_input_is_bare( screen_buffer->input ))
1723 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1724 release_object( screen_buffer );
1725 return;
1727 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1728 reply->width = screen_buffer->width;
1729 reply->height = screen_buffer->height;
1730 release_object( screen_buffer );
1734 /* write data (char and/or attrs) to a screen buffer */
1735 DECL_HANDLER(write_console_output)
1737 struct screen_buffer *screen_buffer;
1739 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1740 FILE_WRITE_DATA, &screen_buffer_ops)))
1742 if (console_input_is_bare( screen_buffer->input ))
1744 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1745 release_object( screen_buffer );
1746 return;
1748 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1749 req->mode, req->x, req->y, req->wrap );
1750 reply->width = screen_buffer->width;
1751 reply->height = screen_buffer->height;
1752 release_object( screen_buffer );
1756 /* fill a screen buffer with constant data (chars and/or attributes) */
1757 DECL_HANDLER(fill_console_output)
1759 struct screen_buffer *screen_buffer;
1761 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1762 FILE_WRITE_DATA, &screen_buffer_ops)))
1764 if (console_input_is_bare( screen_buffer->input ))
1766 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1767 release_object( screen_buffer );
1768 return;
1770 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1771 req->x, req->y, req->count, req->wrap );
1772 release_object( screen_buffer );
1776 /* move a rect of data in a screen buffer */
1777 DECL_HANDLER(move_console_output)
1779 struct screen_buffer *screen_buffer;
1781 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1782 FILE_WRITE_DATA, &screen_buffer_ops)))
1784 if (console_input_is_bare( screen_buffer->input ))
1786 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1787 release_object( screen_buffer );
1788 return;
1790 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1791 req->w, req->h );
1792 release_object( screen_buffer );
1796 /* sends a signal to a console (process, group...) */
1797 DECL_HANDLER(send_console_signal)
1799 process_id_t group;
1801 group = req->group_id ? req->group_id : current->process->group_id;
1803 if (!group)
1804 set_error( STATUS_INVALID_PARAMETER );
1805 else
1806 propagate_console_signal( current->process->console, req->signal, group );
1809 /* get console which renderer is 'current' */
1810 static int cgwe_enum( struct process* process, void* user)
1812 if (process->console && process->console->renderer == current)
1814 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1815 return 1;
1817 return 0;
1820 DECL_HANDLER(get_console_wait_event)
1822 struct console_input* console = NULL;
1824 if (current->process->console)
1825 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1826 else enum_processes(cgwe_enum, &console);
1828 if (console)
1830 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1831 release_object( console );
1833 else set_error( STATUS_INVALID_PARAMETER );