dmime/tests: Add IDirectMusicObject::ParseDescriptor() tests.
[wine.git] / server / console.c
blob058c8ced9f95608976cc17438b8ab61ca7d736e2
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_file_info, /* get_file_info */
195 no_fd_get_volume_info, /* get_volume_info */
196 default_fd_ioctl, /* ioctl */
197 default_fd_queue_async, /* queue_async */
198 default_fd_reselect_async /* reselect_async */
201 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
203 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
205 static int console_input_is_bare( struct console_input* cin )
207 return cin->evt == NULL;
210 static struct fd *console_input_get_fd( struct object* obj )
212 struct console_input *console_input = (struct console_input*)obj;
213 assert( obj->ops == &console_input_ops );
214 if (console_input->fd)
215 return (struct fd*)grab_object( console_input->fd );
216 set_error( STATUS_OBJECT_TYPE_MISMATCH );
217 return NULL;
220 static enum server_fd_type console_get_fd_type( struct fd *fd )
222 return FD_TYPE_CHAR;
225 /* dumps the renderer events of a console */
226 static void console_input_events_dump( struct object *obj, int verbose )
228 struct console_input_events *evts = (struct console_input_events *)obj;
229 assert( obj->ops == &console_input_events_ops );
230 fprintf( stderr, "Console input events: %d/%d events\n",
231 evts->num_used, evts->num_alloc );
234 /* destroys the renderer events of a console */
235 static void console_input_events_destroy( struct object *obj )
237 struct console_input_events *evts = (struct console_input_events *)obj;
238 assert( obj->ops == &console_input_events_ops );
239 free( evts->events );
242 /* the renderer events list is signaled when it's not empty */
243 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
245 struct console_input_events *evts = (struct console_input_events *)obj;
246 assert( obj->ops == &console_input_events_ops );
247 return (evts->num_used != 0);
250 /* add an event to the console's renderer events list */
251 static void console_input_events_append( struct console_input* console,
252 struct console_renderer_event* evt)
254 struct console_input_events* evts;
255 int collapsed = FALSE;
257 if (!(evts = console->evt)) return;
258 /* to be done even when evt has been generated by the renderer ? */
260 /* try to collapse evt into current queue's events */
261 if (evts->num_used)
263 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
265 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
266 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
268 /* if two update events overlap, collapse them into a single one */
269 if (last->u.update.bottom + 1 >= evt->u.update.top &&
270 evt->u.update.bottom + 1 >= last->u.update.top)
272 last->u.update.top = min(last->u.update.top, evt->u.update.top);
273 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
274 collapsed = TRUE;
278 if (!collapsed)
280 if (evts->num_used == evts->num_alloc)
282 evts->num_alloc += 16;
283 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
284 assert(evts->events);
286 evts->events[evts->num_used++] = *evt;
288 wake_up( &evts->obj, 0 );
291 /* retrieves events from the console's renderer events list */
292 static void console_input_events_get( struct console_input_events* evts )
294 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
296 if (num > evts->num_used) num = evts->num_used;
297 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
298 if (num < evts->num_used)
300 memmove( &evts->events[0], &evts->events[num],
301 (evts->num_used - num) * sizeof(evts->events[0]) );
303 evts->num_used -= num;
306 static struct console_input_events *create_console_input_events(void)
308 struct console_input_events* evt;
310 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
311 evt->num_alloc = evt->num_used = 0;
312 evt->events = NULL;
313 return evt;
316 static struct object *create_console_input( struct thread* renderer, int fd )
318 struct console_input *console_input;
320 if (!(console_input = alloc_object( &console_input_ops )))
322 if (fd != -1) close( fd );
323 return NULL;
325 console_input->renderer = renderer;
326 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
327 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
328 ENABLE_EXTENDED_FLAGS;
329 console_input->num_proc = 0;
330 console_input->active = NULL;
331 console_input->recnum = 0;
332 console_input->records = NULL;
333 console_input->evt = renderer ? create_console_input_events() : NULL;
334 console_input->title = NULL;
335 console_input->history_size = 50;
336 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
337 console_input->history_index = 0;
338 console_input->history_mode = 0;
339 console_input->edition_mode = 0;
340 console_input->input_cp = 0;
341 console_input->output_cp = 0;
342 console_input->win = 0;
343 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
344 console_input->fd = NULL;
346 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
348 if (fd != -1) close( fd );
349 console_input->history_size = 0;
350 release_object( console_input );
351 return NULL;
353 if (fd != -1) /* bare console */
355 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
356 FILE_SYNCHRONOUS_IO_NONALERT )))
358 release_object( console_input );
359 return NULL;
361 allow_fd_caching( console_input->fd );
364 return &console_input->obj;
367 static void generate_sb_initial_events( struct console_input *console_input )
369 struct screen_buffer *screen_buffer = console_input->active;
370 struct console_renderer_event evt;
372 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
373 memset(&evt.u, 0, sizeof(evt.u));
374 console_input_events_append( console_input, &evt );
376 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
377 evt.u.resize.width = screen_buffer->width;
378 evt.u.resize.height = screen_buffer->height;
379 console_input_events_append( console_input, &evt );
381 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
382 evt.u.display.left = screen_buffer->win.left;
383 evt.u.display.top = screen_buffer->win.top;
384 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
385 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
386 console_input_events_append( console_input, &evt );
388 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
389 evt.u.update.top = 0;
390 evt.u.update.bottom = screen_buffer->height - 1;
391 console_input_events_append( console_input, &evt );
393 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
394 evt.u.cursor_geom.size = screen_buffer->cursor_size;
395 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
396 console_input_events_append( console_input, &evt );
398 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
399 evt.u.cursor_pos.x = screen_buffer->cursor_x;
400 evt.u.cursor_pos.y = screen_buffer->cursor_y;
401 console_input_events_append( console_input, &evt );
404 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
406 struct screen_buffer *screen_buffer;
407 int i;
409 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
411 if (fd != -1) close( fd );
412 return NULL;
414 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
415 screen_buffer->input = console_input;
416 screen_buffer->cursor_size = 100;
417 screen_buffer->cursor_visible = 1;
418 screen_buffer->width = 80;
419 screen_buffer->height = 150;
420 screen_buffer->max_width = 80;
421 screen_buffer->max_height = 25;
422 screen_buffer->cursor_x = 0;
423 screen_buffer->cursor_y = 0;
424 screen_buffer->attr = 0x0F;
425 screen_buffer->popup_attr = 0xF5;
426 screen_buffer->win.left = 0;
427 screen_buffer->win.right = screen_buffer->max_width - 1;
428 screen_buffer->win.top = 0;
429 screen_buffer->win.bottom = screen_buffer->max_height - 1;
430 screen_buffer->data = NULL;
431 screen_buffer->font.width = 0;
432 screen_buffer->font.height = 0;
433 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
434 list_add_head( &screen_buffer_list, &screen_buffer->entry );
436 if (fd == -1)
437 screen_buffer->fd = NULL;
438 else
440 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
441 FILE_SYNCHRONOUS_IO_NONALERT )))
443 release_object( screen_buffer );
444 return NULL;
446 allow_fd_caching(screen_buffer->fd);
449 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
450 sizeof(*screen_buffer->data) )))
452 release_object( screen_buffer );
453 return NULL;
455 /* clear the first row */
456 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
457 /* and copy it to all other rows */
458 for (i = 1; i < screen_buffer->height; i++)
459 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
460 screen_buffer->width * sizeof(char_info_t) );
462 if (!console_input->active)
464 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
465 generate_sb_initial_events( console_input );
467 return screen_buffer;
470 /* free the console for this process */
471 int free_console( struct process *process )
473 struct console_input* console = process->console;
475 if (!console) return 0;
477 process->console = NULL;
478 if (--console->num_proc == 0 && console->renderer)
480 /* all processes have terminated... tell the renderer to terminate too */
481 struct console_renderer_event evt;
482 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
483 memset(&evt.u, 0, sizeof(evt.u));
484 console_input_events_append( console, &evt );
486 release_object( console );
488 return 1;
491 /* let process inherit the console from parent... this handle two cases :
492 * 1/ generic console inheritance
493 * 2/ parent is a renderer which launches process, and process should attach to the console
494 * rendered by parent
496 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
498 int done = 0;
499 struct process* parent = parent_thread->process;
501 /* if parent is a renderer, then attach current process to its console
502 * a bit hacky....
504 if (hconin)
506 struct console_input* console;
508 /* FIXME: should we check some access rights ? */
509 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
510 0, &console_input_ops )))
512 if (console->renderer == parent_thread)
514 process->console = (struct console_input*)grab_object( console );
515 process->console->num_proc++;
516 done = 1;
518 release_object( console );
520 else clear_error(); /* ignore error */
522 /* otherwise, if parent has a console, attach child to this console */
523 if (!done && parent->console)
525 process->console = (struct console_input*)grab_object( parent->console );
526 process->console->num_proc++;
530 struct thread *console_get_renderer( struct console_input *console )
532 return console->renderer;
535 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
537 struct console_input* console = NULL;
539 if (handle)
540 console = (struct console_input *)get_handle_obj( current->process, handle,
541 access, &console_input_ops );
542 else if (current->process->console)
544 console = (struct console_input *)grab_object( current->process->console );
547 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
548 return console;
551 struct console_signal_info
553 struct console_input *console;
554 process_id_t group;
555 int signal;
558 static int propagate_console_signal_cb(struct process *process, void *user)
560 struct console_signal_info* csi = (struct console_signal_info*)user;
562 if (process->console == csi->console && process->running_threads &&
563 (!csi->group || process->group_id == csi->group))
565 /* find a suitable thread to signal */
566 struct thread *thread;
567 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
569 if (send_thread_signal( thread, csi->signal )) break;
572 return FALSE;
575 static void propagate_console_signal( struct console_input *console,
576 int sig, process_id_t group_id )
578 struct console_signal_info csi;
580 if (!console)
582 set_error( STATUS_INVALID_PARAMETER );
583 return;
585 /* FIXME: should support the other events (like CTRL_BREAK) */
586 if (sig != CTRL_C_EVENT)
588 set_error( STATUS_NOT_IMPLEMENTED );
589 return;
591 csi.console = console;
592 csi.signal = SIGINT;
593 csi.group = group_id;
595 enum_processes(propagate_console_signal_cb, &csi);
598 static int get_console_mode( obj_handle_t handle )
600 struct object *obj;
601 int ret = 0;
603 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
605 if (obj->ops == &console_input_ops)
607 ret = ((struct console_input *)obj)->mode;
609 else if (obj->ops == &screen_buffer_ops)
611 ret = ((struct screen_buffer *)obj)->mode;
613 else
614 set_error( STATUS_OBJECT_TYPE_MISMATCH );
615 release_object( obj );
617 return ret;
620 /* changes the mode of either a console input or a screen buffer */
621 static int set_console_mode( obj_handle_t handle, int mode )
623 struct object *obj;
624 int ret = 0;
626 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
627 return 0;
628 if (obj->ops == &console_input_ops)
630 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
631 ((struct console_input *)obj)->mode = mode;
632 ret = 1;
634 else if (obj->ops == &screen_buffer_ops)
636 ((struct screen_buffer *)obj)->mode = mode;
637 ret = 1;
639 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
640 release_object( obj );
641 return ret;
644 /* add input events to a console input queue */
645 static int write_console_input( struct console_input* console, int count,
646 const INPUT_RECORD *records )
648 INPUT_RECORD *new_rec;
650 if (!count) return 0;
651 if (!(new_rec = realloc( console->records,
652 (console->recnum + count) * sizeof(INPUT_RECORD) )))
654 set_error( STATUS_NO_MEMORY );
655 return -1;
657 console->records = new_rec;
658 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
660 if (console->mode & ENABLE_PROCESSED_INPUT)
662 int i = 0;
663 while (i < count)
665 if (records[i].EventType == KEY_EVENT &&
666 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
667 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
669 if (i != count - 1)
670 memcpy( &console->records[console->recnum + i],
671 &console->records[console->recnum + i + 1],
672 (count - i - 1) * sizeof(INPUT_RECORD) );
673 count--;
674 if (records[i].Event.KeyEvent.bKeyDown)
676 /* send SIGINT to all processes attached to this console */
677 propagate_console_signal( console, CTRL_C_EVENT, 0 );
680 else i++;
683 if (!console->recnum && count) set_event( console->event );
684 console->recnum += count;
685 return count;
688 /* retrieve a pointer to the console input records */
689 static int read_console_input( obj_handle_t handle, int count, int flush )
691 struct console_input *console;
693 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
694 FILE_READ_DATA, &console_input_ops )))
695 return -1;
697 if (!count)
699 /* special case: do not retrieve anything, but return
700 * the total number of records available */
701 count = console->recnum;
703 else
705 if (count > console->recnum) count = console->recnum;
706 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
708 if (flush)
710 int i;
711 for (i = count; i < console->recnum; i++)
712 console->records[i-count] = console->records[i];
713 if ((console->recnum -= count) > 0)
715 INPUT_RECORD *new_rec = realloc( console->records,
716 console->recnum * sizeof(INPUT_RECORD) );
717 if (new_rec) console->records = new_rec;
719 else
721 free( console->records );
722 console->records = NULL;
723 reset_event( console->event );
726 release_object( console );
727 return count;
730 /* set misc console input information */
731 static int set_console_input_info( const struct set_console_input_info_request *req,
732 const WCHAR *title, data_size_t len )
734 struct console_input *console;
735 struct console_renderer_event evt;
737 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
738 if (console_input_is_bare(console) &&
739 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
740 SET_CONSOLE_INPUT_INFO_WIN)))
742 set_error( STATUS_UNSUCCESSFUL );
743 goto error;
746 memset(&evt.u, 0, sizeof(evt.u));
747 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
749 struct screen_buffer *screen_buffer;
751 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
752 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
753 if (!screen_buffer || screen_buffer->input != console)
755 set_error( STATUS_INVALID_HANDLE );
756 if (screen_buffer) release_object( screen_buffer );
757 goto error;
760 if (screen_buffer != console->active)
762 if (console->active) release_object( console->active );
763 console->active = screen_buffer;
764 generate_sb_initial_events( console );
766 else
767 release_object( screen_buffer );
769 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
771 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
772 if (new_title)
774 memcpy( new_title, title, len );
775 new_title[len / sizeof(WCHAR)] = 0;
776 free( console->title );
777 console->title = new_title;
778 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
779 console_input_events_append( console, &evt );
782 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
784 console->history_mode = req->history_mode;
786 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
787 console->history_size != req->history_size)
789 WCHAR** mem = NULL;
790 int i;
791 int delta;
793 if (req->history_size)
795 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
796 if (!mem) goto error;
797 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
800 delta = (console->history_index > req->history_size) ?
801 (console->history_index - req->history_size) : 0;
803 for (i = delta; i < console->history_index; i++)
805 mem[i - delta] = console->history[i];
806 console->history[i] = NULL;
808 console->history_index -= delta;
810 for (i = 0; i < console->history_size; i++)
811 free( console->history[i] );
812 free( console->history );
813 console->history = mem;
814 console->history_size = req->history_size;
816 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
818 console->edition_mode = req->edition_mode;
820 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
822 console->input_cp = req->input_cp;
824 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
826 console->output_cp = req->output_cp;
828 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
830 console->win = req->win;
832 release_object( console );
833 return 1;
834 error:
835 if (console) release_object( console );
836 return 0;
839 /* resize a screen buffer */
840 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
841 int new_width, int new_height )
843 int i, old_width, old_height, copy_width, copy_height;
844 char_info_t *new_data;
846 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
848 set_error( STATUS_NO_MEMORY );
849 return 0;
851 old_width = screen_buffer->width;
852 old_height = screen_buffer->height;
853 copy_width = min( old_width, new_width );
854 copy_height = min( old_height, new_height );
856 /* copy all the rows */
857 for (i = 0; i < copy_height; i++)
859 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
860 copy_width * sizeof(char_info_t) );
863 /* clear the end of each row */
864 if (new_width > old_width)
866 /* fill first row */
867 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
868 /* and blast it to the other rows */
869 for (i = 1; i < copy_height; i++)
870 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
871 (new_width - old_width) * sizeof(char_info_t) );
874 /* clear remaining rows */
875 if (new_height > old_height)
877 /* fill first row */
878 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
879 /* and blast it to the other rows */
880 for (i = old_height+1; i < new_height; i++)
881 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
882 new_width * sizeof(char_info_t) );
884 free( screen_buffer->data );
885 screen_buffer->data = new_data;
886 screen_buffer->width = new_width;
887 screen_buffer->height = new_height;
888 return 1;
891 /* set misc screen buffer information */
892 static int set_console_output_info( struct screen_buffer *screen_buffer,
893 const struct set_console_output_info_request *req )
895 struct console_renderer_event evt;
897 memset(&evt.u, 0, sizeof(evt.u));
898 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
900 if (req->cursor_size < 1 || req->cursor_size > 100)
902 set_error( STATUS_INVALID_PARAMETER );
903 return 0;
905 if (screen_buffer->cursor_size != req->cursor_size ||
906 screen_buffer->cursor_visible != req->cursor_visible)
908 screen_buffer->cursor_size = req->cursor_size;
909 screen_buffer->cursor_visible = req->cursor_visible;
910 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
911 evt.u.cursor_geom.size = req->cursor_size;
912 evt.u.cursor_geom.visible = req->cursor_visible;
913 console_input_events_append( screen_buffer->input, &evt );
916 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
918 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
919 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
921 set_error( STATUS_INVALID_PARAMETER );
922 return 0;
924 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
926 screen_buffer->cursor_x = req->cursor_x;
927 screen_buffer->cursor_y = req->cursor_y;
928 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
929 evt.u.cursor_pos.x = req->cursor_x;
930 evt.u.cursor_pos.y = req->cursor_y;
931 console_input_events_append( screen_buffer->input, &evt );
934 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
936 unsigned cc;
938 /* new screen-buffer cannot be smaller than actual window */
939 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
940 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
942 set_error( STATUS_INVALID_PARAMETER );
943 return 0;
945 /* FIXME: there are also some basic minimum and max size to deal with */
946 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
948 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
949 evt.u.resize.width = req->width;
950 evt.u.resize.height = req->height;
951 console_input_events_append( screen_buffer->input, &evt );
953 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
954 evt.u.update.top = 0;
955 evt.u.update.bottom = screen_buffer->height - 1;
956 console_input_events_append( screen_buffer->input, &evt );
958 /* scroll window to display sb */
959 if (screen_buffer->win.right >= req->width)
961 screen_buffer->win.right -= screen_buffer->win.left;
962 screen_buffer->win.left = 0;
964 if (screen_buffer->win.bottom >= req->height)
966 screen_buffer->win.bottom -= screen_buffer->win.top;
967 screen_buffer->win.top = 0;
969 /* reset cursor if needed (normally, if cursor was outside of new sb, the
970 * window has been shifted so that the new position of the cursor will be
971 * visible */
972 cc = 0;
973 if (screen_buffer->cursor_x >= req->width)
975 screen_buffer->cursor_x = req->width - 1;
976 cc++;
978 if (screen_buffer->cursor_y >= req->height)
980 screen_buffer->cursor_y = req->height - 1;
981 cc++;
983 if (cc)
985 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
986 evt.u.cursor_pos.x = req->cursor_x;
987 evt.u.cursor_pos.y = req->cursor_y;
988 console_input_events_append( screen_buffer->input, &evt );
991 if (screen_buffer == screen_buffer->input->active &&
992 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
994 INPUT_RECORD ir;
995 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
996 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
997 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
998 write_console_input( screen_buffer->input, 1, &ir );
1001 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1003 screen_buffer->attr = req->attr;
1005 if (req->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1007 screen_buffer->popup_attr = req->popup_attr;
1009 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1011 if (req->win_left < 0 || req->win_left > req->win_right ||
1012 req->win_right >= screen_buffer->width ||
1013 req->win_top < 0 || req->win_top > req->win_bottom ||
1014 req->win_bottom >= screen_buffer->height)
1016 set_error( STATUS_INVALID_PARAMETER );
1017 return 0;
1019 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1020 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1022 screen_buffer->win.left = req->win_left;
1023 screen_buffer->win.top = req->win_top;
1024 screen_buffer->win.right = req->win_right;
1025 screen_buffer->win.bottom = req->win_bottom;
1026 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1027 evt.u.display.left = req->win_left;
1028 evt.u.display.top = req->win_top;
1029 evt.u.display.width = req->win_right - req->win_left + 1;
1030 evt.u.display.height = req->win_bottom - req->win_top + 1;
1031 console_input_events_append( screen_buffer->input, &evt );
1034 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1036 screen_buffer->max_width = req->max_width;
1037 screen_buffer->max_height = req->max_height;
1039 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1041 screen_buffer->font.width = req->font_width;
1042 screen_buffer->font.height = req->font_height;
1044 if (req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1046 memcpy( screen_buffer->color_map, get_req_data(),
1047 min( sizeof(screen_buffer->color_map), get_req_data_size() ));
1050 return 1;
1053 /* appends a new line to history (history is a fixed size array) */
1054 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1056 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1058 if (!ptr)
1059 return;
1061 if (!console || !console->history_size)
1063 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1064 free( ptr );
1065 return;
1068 memcpy( ptr, buf, len * sizeof(WCHAR) );
1069 ptr[len] = 0;
1071 if (console->history_mode && console->history_index &&
1072 !strcmpW( console->history[console->history_index - 1], ptr ))
1074 /* ok, mode ask us to not use twice the same string...
1075 * so just free mem and returns
1077 set_error( STATUS_ALIAS_EXISTS );
1078 free(ptr);
1079 return;
1082 if (console->history_index < console->history_size)
1084 console->history[console->history_index++] = ptr;
1086 else
1088 free( console->history[0]) ;
1089 memmove( &console->history[0], &console->history[1],
1090 (console->history_size - 1) * sizeof(WCHAR*) );
1091 console->history[console->history_size - 1] = ptr;
1095 /* returns a line from the cache */
1096 static data_size_t console_input_get_hist( struct console_input *console, int index )
1098 data_size_t ret = 0;
1100 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1101 else
1103 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1104 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1106 return ret;
1109 /* dumb dump */
1110 static void console_input_dump( struct object *obj, int verbose )
1112 struct console_input *console = (struct console_input *)obj;
1113 assert( obj->ops == &console_input_ops );
1114 fprintf( stderr, "Console input active=%p evt=%p\n",
1115 console->active, console->evt );
1118 static void console_input_destroy( struct object *obj )
1120 struct console_input* console_in = (struct console_input *)obj;
1121 struct screen_buffer* curr;
1122 int i;
1124 assert( obj->ops == &console_input_ops );
1125 free( console_in->title );
1126 free( console_in->records );
1128 if (console_in->active) release_object( console_in->active );
1129 console_in->active = NULL;
1131 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1133 if (curr->input == console_in) curr->input = NULL;
1136 if (console_in->evt)
1138 release_object( console_in->evt );
1139 console_in->evt = NULL;
1141 if (console_in->event)
1142 release_object( console_in->event );
1143 if (console_in->fd)
1144 release_object( console_in->fd );
1146 for (i = 0; i < console_in->history_size; i++)
1147 free( console_in->history[i] );
1148 free( console_in->history );
1151 static void screen_buffer_dump( struct object *obj, int verbose )
1153 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1154 assert( obj->ops == &screen_buffer_ops );
1156 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1159 static void screen_buffer_destroy( struct object *obj )
1161 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1163 assert( obj->ops == &screen_buffer_ops );
1165 list_remove( &screen_buffer->entry );
1167 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1169 struct screen_buffer *sb;
1171 screen_buffer->input->active = NULL;
1172 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1174 if (sb->input == screen_buffer->input)
1176 sb->input->active = sb;
1177 break;
1181 if (screen_buffer->fd) release_object( screen_buffer->fd );
1182 free( screen_buffer->data );
1185 static struct fd *screen_buffer_get_fd( struct object *obj )
1187 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1188 assert( obj->ops == &screen_buffer_ops );
1189 if (screen_buffer->fd)
1190 return (struct fd*)grab_object( screen_buffer->fd );
1191 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1192 return NULL;
1195 /* write data into a screen buffer */
1196 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1197 const void* data, enum char_info_mode mode,
1198 int x, int y, int wrap )
1200 unsigned int i;
1201 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1203 if (y >= screen_buffer->height) return 0;
1205 if (wrap)
1206 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1207 else
1208 end = screen_buffer->data + (y+1) * screen_buffer->width;
1210 switch(mode)
1212 case CHAR_INFO_MODE_TEXT:
1214 const WCHAR *ptr = data;
1215 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1217 break;
1218 case CHAR_INFO_MODE_ATTR:
1220 const unsigned short *ptr = data;
1221 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1223 break;
1224 case CHAR_INFO_MODE_TEXTATTR:
1226 const char_info_t *ptr = data;
1227 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1229 break;
1230 case CHAR_INFO_MODE_TEXTSTDATTR:
1232 const WCHAR *ptr = data;
1233 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1235 dest->ch = ptr[i];
1236 dest->attr = screen_buffer->attr;
1239 break;
1240 default:
1241 set_error( STATUS_INVALID_PARAMETER );
1242 return 0;
1245 if (i && screen_buffer == screen_buffer->input->active)
1247 struct console_renderer_event evt;
1248 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1249 memset(&evt.u, 0, sizeof(evt.u));
1250 evt.u.update.top = y + x / screen_buffer->width;
1251 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1252 console_input_events_append( screen_buffer->input, &evt );
1254 return i;
1257 /* fill a screen buffer with uniform data */
1258 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1259 enum char_info_mode mode, int x, int y, int count, int wrap )
1261 int i;
1262 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1264 if (y >= screen_buffer->height) return 0;
1266 if (wrap)
1267 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1268 else
1269 end = screen_buffer->data + (y+1) * screen_buffer->width;
1271 if (count > end - dest) count = end - dest;
1273 switch(mode)
1275 case CHAR_INFO_MODE_TEXT:
1276 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1277 break;
1278 case CHAR_INFO_MODE_ATTR:
1279 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1280 break;
1281 case CHAR_INFO_MODE_TEXTATTR:
1282 for (i = 0; i < count; i++) dest[i] = data;
1283 break;
1284 case CHAR_INFO_MODE_TEXTSTDATTR:
1285 for (i = 0; i < count; i++)
1287 dest[i].ch = data.ch;
1288 dest[i].attr = screen_buffer->attr;
1290 break;
1291 default:
1292 set_error( STATUS_INVALID_PARAMETER );
1293 return 0;
1296 if (count && screen_buffer == screen_buffer->input->active)
1298 struct console_renderer_event evt;
1299 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1300 memset(&evt.u, 0, sizeof(evt.u));
1301 evt.u.update.top = y;
1302 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1303 console_input_events_append( screen_buffer->input, &evt );
1305 return i;
1308 /* read data from a screen buffer */
1309 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1310 enum char_info_mode mode, int wrap )
1312 int i;
1313 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1315 if (y >= screen_buffer->height) return;
1317 if (wrap)
1318 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1319 else
1320 end = screen_buffer->data + (y+1) * screen_buffer->width;
1322 switch(mode)
1324 case CHAR_INFO_MODE_TEXT:
1326 WCHAR *data;
1327 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1328 if ((data = set_reply_data_size( count * sizeof(*data) )))
1330 for (i = 0; i < count; i++) data[i] = src[i].ch;
1333 break;
1334 case CHAR_INFO_MODE_ATTR:
1336 unsigned short *data;
1337 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1338 if ((data = set_reply_data_size( count * sizeof(*data) )))
1340 for (i = 0; i < count; i++) data[i] = src[i].attr;
1343 break;
1344 case CHAR_INFO_MODE_TEXTATTR:
1346 char_info_t *data;
1347 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1348 if ((data = set_reply_data_size( count * sizeof(*data) )))
1350 for (i = 0; i < count; i++) data[i] = src[i];
1353 break;
1354 default:
1355 set_error( STATUS_INVALID_PARAMETER );
1356 break;
1360 /* scroll parts of a screen buffer */
1361 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1362 int w, int h )
1364 int j;
1365 char_info_t *psrc, *pdst;
1366 struct console_renderer_event evt;
1368 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1369 xsrc + w > screen_buffer->width ||
1370 xdst + w > screen_buffer->width ||
1371 ysrc + h > screen_buffer->height ||
1372 ydst + h > screen_buffer->height ||
1373 w == 0 || h == 0)
1375 set_error( STATUS_INVALID_PARAMETER );
1376 return;
1379 if (ysrc < ydst)
1381 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1382 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1384 for (j = h; j > 0; j--)
1386 memcpy(pdst, psrc, w * sizeof(*pdst) );
1387 pdst -= screen_buffer->width;
1388 psrc -= screen_buffer->width;
1391 else
1393 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1394 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1396 for (j = 0; j < h; j++)
1398 /* we use memmove here because when psrc and pdst are the same,
1399 * copies are done on the same row, so the dst and src blocks
1400 * can overlap */
1401 memmove( pdst, psrc, w * sizeof(*pdst) );
1402 pdst += screen_buffer->width;
1403 psrc += screen_buffer->width;
1407 /* FIXME: this could be enhanced, by signalling scroll */
1408 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1409 memset(&evt.u, 0, sizeof(evt.u));
1410 evt.u.update.top = min(ysrc, ydst);
1411 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1412 console_input_events_append( screen_buffer->input, &evt );
1415 /* allocate a console for the renderer */
1416 DECL_HANDLER(alloc_console)
1418 obj_handle_t in = 0;
1419 obj_handle_t evt = 0;
1420 struct process *process;
1421 struct thread *renderer;
1422 struct console_input *console;
1423 int fd;
1424 int attach = 0;
1426 if (req->input_fd != -1)
1428 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1430 set_error( STATUS_INVALID_PARAMETER );
1431 return;
1434 else fd = -1;
1436 switch (req->pid)
1438 case 0:
1439 /* renderer is current, console to be attached to parent process */
1440 renderer = current;
1441 if (!(process = get_process_from_id( current->process->parent_id )))
1443 if (fd != -1) close( fd );
1444 set_error( STATUS_ACCESS_DENIED );
1445 return;
1447 attach = 1;
1448 break;
1449 case 0xffffffff:
1450 /* no renderer, console to be attached to current process */
1451 renderer = NULL;
1452 process = current->process;
1453 grab_object( process );
1454 attach = 1;
1455 break;
1456 default:
1457 /* renderer is current, console to be attached to req->pid */
1458 renderer = current;
1459 if (!(process = get_process_from_id( req->pid )))
1461 if (fd != -1) close( fd );
1462 return;
1466 if (attach && process->console)
1468 if (fd != -1) close( fd );
1469 set_error( STATUS_ACCESS_DENIED );
1470 goto the_end;
1473 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1475 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1477 if (!console->evt ||
1478 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1480 if (attach)
1482 process->console = (struct console_input*)grab_object( console );
1483 console->num_proc++;
1485 reply->handle_in = in;
1486 reply->event = evt;
1487 release_object( console );
1488 goto the_end;
1490 close_handle( current->process, in );
1492 release_object( console );
1494 the_end:
1495 release_object( process );
1498 /* free the console of the current process */
1499 DECL_HANDLER(free_console)
1501 free_console( current->process );
1504 /* let the renderer peek the events it's waiting on */
1505 DECL_HANDLER(get_console_renderer_events)
1507 struct console_input_events *evt;
1509 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1510 FILE_READ_PROPERTIES, &console_input_events_ops );
1511 if (!evt) return;
1512 console_input_events_get( evt );
1513 release_object( evt );
1516 /* open a handle to the process console */
1517 DECL_HANDLER(open_console)
1519 struct object *obj = NULL;
1521 reply->handle = 0;
1522 if (!req->from)
1524 if (current->process->console)
1525 obj = grab_object( (struct object*)current->process->console );
1527 else if (req->from == (obj_handle_t)1)
1529 if (current->process->console && current->process->console->active)
1530 obj = grab_object( (struct object*)current->process->console->active );
1532 else if ((obj = get_handle_obj( current->process, req->from,
1533 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1535 struct console_input *console = (struct console_input *)obj;
1536 obj = (console->active) ? grab_object( console->active ) : NULL;
1537 release_object( console );
1540 /* FIXME: req->share is not used (as in screen buffer creation) */
1541 if (obj)
1543 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1544 release_object( obj );
1546 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1549 /* set info about a console input */
1550 DECL_HANDLER(set_console_input_info)
1552 set_console_input_info( req, get_req_data(), get_req_data_size() );
1555 /* get info about a console (output only) */
1556 DECL_HANDLER(get_console_input_info)
1558 struct console_input *console;
1560 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1561 if (console->title)
1563 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1564 if (len > get_reply_max_size()) len = get_reply_max_size();
1565 set_reply_data( console->title, len );
1567 reply->history_mode = console->history_mode;
1568 reply->history_size = console->history_size;
1569 reply->history_index = console->history_index;
1570 reply->edition_mode = console->edition_mode;
1571 reply->input_cp = console->input_cp;
1572 reply->output_cp = console->output_cp;
1573 reply->win = console->win;
1575 release_object( console );
1578 /* get a console mode (input or output) */
1579 DECL_HANDLER(get_console_mode)
1581 reply->mode = get_console_mode( req->handle );
1584 /* set a console mode (input or output) */
1585 DECL_HANDLER(set_console_mode)
1587 set_console_mode( req->handle, req->mode );
1590 /* add input records to a console input queue */
1591 DECL_HANDLER(write_console_input)
1593 struct console_input *console;
1595 reply->written = 0;
1596 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1597 FILE_WRITE_PROPERTIES, &console_input_ops )))
1598 return;
1599 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1600 get_req_data() );
1601 release_object( console );
1604 /* fetch input records from a console input queue */
1605 DECL_HANDLER(read_console_input)
1607 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1608 reply->read = read_console_input( req->handle, count, req->flush );
1611 /* appends a string to console's history */
1612 DECL_HANDLER(append_console_input_history)
1614 struct console_input *console;
1616 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1617 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1618 release_object( console );
1621 /* appends a string to console's history */
1622 DECL_HANDLER(get_console_input_history)
1624 struct console_input *console;
1626 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1627 reply->total = console_input_get_hist( console, req->index );
1628 release_object( console );
1631 /* creates a screen buffer */
1632 DECL_HANDLER(create_console_output)
1634 struct console_input* console;
1635 struct screen_buffer* screen_buffer;
1636 int fd;
1638 if (req->fd != -1)
1640 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1642 set_error( STATUS_INVALID_HANDLE );
1643 return;
1646 else fd = -1;
1647 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1649 if (fd != -1) close( fd );
1650 return;
1652 if (console_input_is_bare( console ) ^ (fd != -1))
1654 if (fd != -1) close( fd );
1655 release_object( console );
1656 set_error( STATUS_INVALID_HANDLE );
1657 return;
1660 screen_buffer = create_console_output( console, fd );
1661 if (screen_buffer)
1663 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1664 * see file.c on how this could be done */
1665 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1666 release_object( screen_buffer );
1668 release_object( console );
1671 /* set info about a console screen buffer */
1672 DECL_HANDLER(set_console_output_info)
1674 struct screen_buffer *screen_buffer;
1676 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1677 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1679 set_console_output_info( screen_buffer, req );
1680 release_object( screen_buffer );
1684 /* get info about a console screen buffer */
1685 DECL_HANDLER(get_console_output_info)
1687 struct screen_buffer *screen_buffer;
1689 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1690 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1692 reply->cursor_size = screen_buffer->cursor_size;
1693 reply->cursor_visible = screen_buffer->cursor_visible;
1694 reply->cursor_x = screen_buffer->cursor_x;
1695 reply->cursor_y = screen_buffer->cursor_y;
1696 reply->width = screen_buffer->width;
1697 reply->height = screen_buffer->height;
1698 reply->attr = screen_buffer->attr;
1699 reply->popup_attr = screen_buffer->popup_attr;
1700 reply->win_left = screen_buffer->win.left;
1701 reply->win_top = screen_buffer->win.top;
1702 reply->win_right = screen_buffer->win.right;
1703 reply->win_bottom = screen_buffer->win.bottom;
1704 reply->max_width = screen_buffer->max_width;
1705 reply->max_height = screen_buffer->max_height;
1706 reply->font_width = screen_buffer->font.width;
1707 reply->font_height = screen_buffer->font.height;
1708 set_reply_data( screen_buffer->color_map,
1709 min( sizeof(screen_buffer->color_map), get_reply_max_size() ));
1710 release_object( screen_buffer );
1714 /* read data (chars & attrs) from a screen buffer */
1715 DECL_HANDLER(read_console_output)
1717 struct screen_buffer *screen_buffer;
1719 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1720 FILE_READ_DATA, &screen_buffer_ops )))
1722 if (console_input_is_bare( screen_buffer->input ))
1724 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1725 release_object( screen_buffer );
1726 return;
1728 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1729 reply->width = screen_buffer->width;
1730 reply->height = screen_buffer->height;
1731 release_object( screen_buffer );
1735 /* write data (char and/or attrs) to a screen buffer */
1736 DECL_HANDLER(write_console_output)
1738 struct screen_buffer *screen_buffer;
1740 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1741 FILE_WRITE_DATA, &screen_buffer_ops)))
1743 if (console_input_is_bare( screen_buffer->input ))
1745 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1746 release_object( screen_buffer );
1747 return;
1749 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1750 req->mode, req->x, req->y, req->wrap );
1751 reply->width = screen_buffer->width;
1752 reply->height = screen_buffer->height;
1753 release_object( screen_buffer );
1757 /* fill a screen buffer with constant data (chars and/or attributes) */
1758 DECL_HANDLER(fill_console_output)
1760 struct screen_buffer *screen_buffer;
1762 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1763 FILE_WRITE_DATA, &screen_buffer_ops)))
1765 if (console_input_is_bare( screen_buffer->input ))
1767 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1768 release_object( screen_buffer );
1769 return;
1771 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1772 req->x, req->y, req->count, req->wrap );
1773 release_object( screen_buffer );
1777 /* move a rect of data in a screen buffer */
1778 DECL_HANDLER(move_console_output)
1780 struct screen_buffer *screen_buffer;
1782 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1783 FILE_WRITE_DATA, &screen_buffer_ops)))
1785 if (console_input_is_bare( screen_buffer->input ))
1787 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1788 release_object( screen_buffer );
1789 return;
1791 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1792 req->w, req->h );
1793 release_object( screen_buffer );
1797 /* sends a signal to a console (process, group...) */
1798 DECL_HANDLER(send_console_signal)
1800 process_id_t group;
1802 group = req->group_id ? req->group_id : current->process->group_id;
1804 if (!group)
1805 set_error( STATUS_INVALID_PARAMETER );
1806 else
1807 propagate_console_signal( current->process->console, req->signal, group );
1810 /* get console which renderer is 'current' */
1811 static int cgwe_enum( struct process* process, void* user)
1813 if (process->console && process->console->renderer == current)
1815 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1816 return 1;
1818 return 0;
1821 DECL_HANDLER(get_console_wait_event)
1823 struct console_input* console = NULL;
1825 if (current->process->console)
1826 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1827 else enum_processes(cgwe_enum, &console);
1829 if (console)
1831 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1832 release_object( console );
1834 else set_error( STATUS_INVALID_PARAMETER );