x3daudio1_0: Use shared source.
[wine.git] / server / console.c
blob9b016144efecb1c7fec1d959d518b1e4311d2831
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_open_file, /* open_file */
88 no_close_handle, /* close_handle */
89 console_input_destroy /* destroy */
92 static void console_input_events_dump( struct object *obj, int verbose );
93 static void console_input_events_destroy( struct object *obj );
94 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
96 struct console_input_events
98 struct object obj; /* object header */
99 int num_alloc; /* number of allocated events */
100 int num_used; /* number of actually used events */
101 struct console_renderer_event* events;
104 static const struct object_ops console_input_events_ops =
106 sizeof(struct console_input_events), /* size */
107 console_input_events_dump, /* dump */
108 no_get_type, /* get_type */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 console_input_events_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 default_fd_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 no_close_handle, /* close_handle */
121 console_input_events_destroy /* destroy */
124 struct font_info
126 short int width;
127 short int height;
130 struct screen_buffer
132 struct object obj; /* object header */
133 struct list entry; /* entry in list of all screen buffers */
134 struct console_input *input; /* associated console input */
135 int mode; /* output mode */
136 int cursor_size; /* size of cursor (percentage filled) */
137 int cursor_visible;/* cursor visibility flag */
138 int cursor_x; /* position of cursor */
139 int cursor_y; /* position of cursor */
140 int width; /* size (w-h) of the screen buffer */
141 int height;
142 int max_width; /* size (w-h) of the window given font size */
143 int max_height;
144 char_info_t *data; /* the data for each cell - a width x height matrix */
145 unsigned short attr; /* default attribute for screen buffer */
146 rectangle_t win; /* current visible window on the screen buffer *
147 * as seen in wineconsole */
148 struct font_info font; /* console font information */
149 struct fd *fd; /* for bare console, attached output fd */
152 static void screen_buffer_dump( struct object *obj, int verbose );
153 static void screen_buffer_destroy( struct object *obj );
154 static struct fd *screen_buffer_get_fd( struct object *obj );
156 static const struct object_ops screen_buffer_ops =
158 sizeof(struct screen_buffer), /* size */
159 screen_buffer_dump, /* dump */
160 no_get_type, /* get_type */
161 no_add_queue, /* add_queue */
162 NULL, /* remove_queue */
163 NULL, /* signaled */
164 NULL, /* satisfied */
165 no_signal, /* signal */
166 screen_buffer_get_fd, /* get_fd */
167 default_fd_map_access, /* map_access */
168 default_get_sd, /* get_sd */
169 default_set_sd, /* set_sd */
170 no_lookup_name, /* lookup_name */
171 no_open_file, /* open_file */
172 no_close_handle, /* close_handle */
173 screen_buffer_destroy /* destroy */
176 static enum server_fd_type console_get_fd_type( struct fd *fd );
178 static const struct fd_ops console_fd_ops =
180 default_fd_get_poll_events, /* get_poll_events */
181 default_poll_event, /* poll_event */
182 console_get_fd_type, /* get_fd_type */
183 no_fd_read, /* read */
184 no_fd_write, /* write */
185 no_fd_flush, /* flush */
186 default_fd_ioctl, /* ioctl */
187 default_fd_queue_async, /* queue_async */
188 default_fd_reselect_async, /* reselect_async */
189 default_fd_cancel_async /* cancel_async */
192 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
194 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
196 static int console_input_is_bare( struct console_input* cin )
198 return cin->evt == NULL;
201 static struct fd *console_input_get_fd( struct object* obj )
203 struct console_input *console_input = (struct console_input*)obj;
204 assert( obj->ops == &console_input_ops );
205 if (console_input->fd)
206 return (struct fd*)grab_object( console_input->fd );
207 set_error( STATUS_OBJECT_TYPE_MISMATCH );
208 return NULL;
211 static enum server_fd_type console_get_fd_type( struct fd *fd )
213 return FD_TYPE_CHAR;
216 /* dumps the renderer events of a console */
217 static void console_input_events_dump( struct object *obj, int verbose )
219 struct console_input_events *evts = (struct console_input_events *)obj;
220 assert( obj->ops == &console_input_events_ops );
221 fprintf( stderr, "Console input events: %d/%d events\n",
222 evts->num_used, evts->num_alloc );
225 /* destroys the renderer events of a console */
226 static void console_input_events_destroy( struct object *obj )
228 struct console_input_events *evts = (struct console_input_events *)obj;
229 assert( obj->ops == &console_input_events_ops );
230 free( evts->events );
233 /* the renderer events list is signaled when it's not empty */
234 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
236 struct console_input_events *evts = (struct console_input_events *)obj;
237 assert( obj->ops == &console_input_events_ops );
238 return (evts->num_used != 0);
241 /* add an event to the console's renderer events list */
242 static void console_input_events_append( struct console_input* console,
243 struct console_renderer_event* evt)
245 struct console_input_events* evts;
246 int collapsed = FALSE;
248 if (!(evts = console->evt)) return;
249 /* to be done even when evt has been generated by the renderer ? */
251 /* try to collapse evt into current queue's events */
252 if (evts->num_used)
254 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
256 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
257 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
259 /* if two update events overlap, collapse them into a single one */
260 if (last->u.update.bottom + 1 >= evt->u.update.top &&
261 evt->u.update.bottom + 1 >= last->u.update.top)
263 last->u.update.top = min(last->u.update.top, evt->u.update.top);
264 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
265 collapsed = TRUE;
269 if (!collapsed)
271 if (evts->num_used == evts->num_alloc)
273 evts->num_alloc += 16;
274 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
275 assert(evts->events);
277 evts->events[evts->num_used++] = *evt;
279 wake_up( &evts->obj, 0 );
282 /* retrieves events from the console's renderer events list */
283 static void console_input_events_get( struct console_input_events* evts )
285 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
287 if (num > evts->num_used) num = evts->num_used;
288 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
289 if (num < evts->num_used)
291 memmove( &evts->events[0], &evts->events[num],
292 (evts->num_used - num) * sizeof(evts->events[0]) );
294 evts->num_used -= num;
297 static struct console_input_events *create_console_input_events(void)
299 struct console_input_events* evt;
301 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
302 evt->num_alloc = evt->num_used = 0;
303 evt->events = NULL;
304 return evt;
307 static struct object *create_console_input( struct thread* renderer, int fd )
309 struct console_input *console_input;
311 if (!(console_input = alloc_object( &console_input_ops )))
313 if (fd != -1) close( fd );
314 return NULL;
316 console_input->renderer = renderer;
317 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
318 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
319 ENABLE_EXTENDED_FLAGS;
320 console_input->num_proc = 0;
321 console_input->active = NULL;
322 console_input->recnum = 0;
323 console_input->records = NULL;
324 console_input->evt = renderer ? create_console_input_events() : NULL;
325 console_input->title = NULL;
326 console_input->history_size = 50;
327 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
328 console_input->history_index = 0;
329 console_input->history_mode = 0;
330 console_input->edition_mode = 0;
331 console_input->input_cp = 0;
332 console_input->output_cp = 0;
333 console_input->win = 0;
334 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
335 console_input->fd = NULL;
337 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
339 if (fd != -1) close( fd );
340 console_input->history_size = 0;
341 release_object( console_input );
342 return NULL;
344 if (fd != -1) /* bare console */
346 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
347 FILE_SYNCHRONOUS_IO_NONALERT )))
349 release_object( console_input );
350 return NULL;
352 allow_fd_caching( console_input->fd );
355 return &console_input->obj;
358 static void generate_sb_initial_events( struct console_input *console_input )
360 struct screen_buffer *screen_buffer = console_input->active;
361 struct console_renderer_event evt;
363 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
364 memset(&evt.u, 0, sizeof(evt.u));
365 console_input_events_append( console_input, &evt );
367 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
368 evt.u.resize.width = screen_buffer->width;
369 evt.u.resize.height = screen_buffer->height;
370 console_input_events_append( console_input, &evt );
372 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
373 evt.u.display.left = screen_buffer->win.left;
374 evt.u.display.top = screen_buffer->win.top;
375 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
376 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
377 console_input_events_append( console_input, &evt );
379 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
380 evt.u.update.top = 0;
381 evt.u.update.bottom = screen_buffer->height - 1;
382 console_input_events_append( console_input, &evt );
384 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
385 evt.u.cursor_geom.size = screen_buffer->cursor_size;
386 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
387 console_input_events_append( console_input, &evt );
389 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
390 evt.u.cursor_pos.x = screen_buffer->cursor_x;
391 evt.u.cursor_pos.y = screen_buffer->cursor_y;
392 console_input_events_append( console_input, &evt );
395 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
397 struct screen_buffer *screen_buffer;
398 int i;
400 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
402 if (fd != -1) close( fd );
403 return NULL;
405 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
406 screen_buffer->input = console_input;
407 screen_buffer->cursor_size = 100;
408 screen_buffer->cursor_visible = 1;
409 screen_buffer->width = 80;
410 screen_buffer->height = 150;
411 screen_buffer->max_width = 80;
412 screen_buffer->max_height = 25;
413 screen_buffer->cursor_x = 0;
414 screen_buffer->cursor_y = 0;
415 screen_buffer->attr = 0x0F;
416 screen_buffer->win.left = 0;
417 screen_buffer->win.right = screen_buffer->max_width - 1;
418 screen_buffer->win.top = 0;
419 screen_buffer->win.bottom = screen_buffer->max_height - 1;
420 screen_buffer->data = NULL;
421 screen_buffer->font.width = 0;
422 screen_buffer->font.height = 0;
423 list_add_head( &screen_buffer_list, &screen_buffer->entry );
425 if (fd == -1)
426 screen_buffer->fd = NULL;
427 else
429 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
430 FILE_SYNCHRONOUS_IO_NONALERT )))
432 release_object( screen_buffer );
433 return NULL;
435 allow_fd_caching(screen_buffer->fd);
438 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
439 sizeof(*screen_buffer->data) )))
441 release_object( screen_buffer );
442 return NULL;
444 /* clear the first row */
445 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
446 /* and copy it to all other rows */
447 for (i = 1; i < screen_buffer->height; i++)
448 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
449 screen_buffer->width * sizeof(char_info_t) );
451 if (!console_input->active)
453 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
454 generate_sb_initial_events( console_input );
456 return screen_buffer;
459 /* free the console for this process */
460 int free_console( struct process *process )
462 struct console_input* console = process->console;
464 if (!console) return 0;
466 process->console = NULL;
467 if (--console->num_proc == 0 && console->renderer)
469 /* all processes have terminated... tell the renderer to terminate too */
470 struct console_renderer_event evt;
471 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
472 memset(&evt.u, 0, sizeof(evt.u));
473 console_input_events_append( console, &evt );
475 release_object( console );
477 return 1;
480 /* let process inherit the console from parent... this handle two cases :
481 * 1/ generic console inheritance
482 * 2/ parent is a renderer which launches process, and process should attach to the console
483 * renderered by parent
485 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
487 int done = 0;
488 struct process* parent = parent_thread->process;
490 /* if parent is a renderer, then attach current process to its console
491 * a bit hacky....
493 if (hconin)
495 struct console_input* console;
497 /* FIXME: should we check some access rights ? */
498 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
499 0, &console_input_ops )))
501 if (console->renderer == parent_thread)
503 process->console = (struct console_input*)grab_object( console );
504 process->console->num_proc++;
505 done = 1;
507 release_object( console );
509 else clear_error(); /* ignore error */
511 /* otherwise, if parent has a console, attach child to this console */
512 if (!done && parent->console)
514 process->console = (struct console_input*)grab_object( parent->console );
515 process->console->num_proc++;
519 struct thread *console_get_renderer( struct console_input *console )
521 return console->renderer;
524 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
526 struct console_input* console = NULL;
528 if (handle)
529 console = (struct console_input *)get_handle_obj( current->process, handle,
530 access, &console_input_ops );
531 else if (current->process->console)
533 console = (struct console_input *)grab_object( current->process->console );
536 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
537 return console;
540 struct console_signal_info
542 struct console_input *console;
543 process_id_t group;
544 int signal;
547 static int propagate_console_signal_cb(struct process *process, void *user)
549 struct console_signal_info* csi = (struct console_signal_info*)user;
551 if (process->console == csi->console && process->running_threads &&
552 (!csi->group || process->group_id == csi->group))
554 /* find a suitable thread to signal */
555 struct thread *thread;
556 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
558 if (send_thread_signal( thread, csi->signal )) break;
561 return FALSE;
564 static void propagate_console_signal( struct console_input *console,
565 int sig, process_id_t group_id )
567 struct console_signal_info csi;
569 if (!console)
571 set_error( STATUS_INVALID_PARAMETER );
572 return;
574 /* FIXME: should support the other events (like CTRL_BREAK) */
575 if (sig != CTRL_C_EVENT)
577 set_error( STATUS_NOT_IMPLEMENTED );
578 return;
580 csi.console = console;
581 csi.signal = SIGINT;
582 csi.group = group_id;
584 enum_processes(propagate_console_signal_cb, &csi);
587 static int get_console_mode( obj_handle_t handle )
589 struct object *obj;
590 int ret = 0;
592 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
594 if (obj->ops == &console_input_ops)
596 ret = ((struct console_input *)obj)->mode;
598 else if (obj->ops == &screen_buffer_ops)
600 ret = ((struct screen_buffer *)obj)->mode;
602 else
603 set_error( STATUS_OBJECT_TYPE_MISMATCH );
604 release_object( obj );
606 return ret;
609 /* changes the mode of either a console input or a screen buffer */
610 static int set_console_mode( obj_handle_t handle, int mode )
612 struct object *obj;
613 int ret = 0;
615 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
616 return 0;
617 if (obj->ops == &console_input_ops)
619 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
620 ((struct console_input *)obj)->mode = mode;
621 ret = 1;
623 else if (obj->ops == &screen_buffer_ops)
625 ((struct screen_buffer *)obj)->mode = mode;
626 ret = 1;
628 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
629 release_object( obj );
630 return ret;
633 /* add input events to a console input queue */
634 static int write_console_input( struct console_input* console, int count,
635 const INPUT_RECORD *records )
637 INPUT_RECORD *new_rec;
639 if (!count) return 0;
640 if (!(new_rec = realloc( console->records,
641 (console->recnum + count) * sizeof(INPUT_RECORD) )))
643 set_error( STATUS_NO_MEMORY );
644 return -1;
646 console->records = new_rec;
647 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
649 if (console->mode & ENABLE_PROCESSED_INPUT)
651 int i = 0;
652 while (i < count)
654 if (records[i].EventType == KEY_EVENT &&
655 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
656 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
658 if (i != count - 1)
659 memcpy( &console->records[console->recnum + i],
660 &console->records[console->recnum + i + 1],
661 (count - i - 1) * sizeof(INPUT_RECORD) );
662 count--;
663 if (records[i].Event.KeyEvent.bKeyDown)
665 /* send SIGINT to all processes attached to this console */
666 propagate_console_signal( console, CTRL_C_EVENT, 0 );
669 else i++;
672 if (!console->recnum && count) set_event( console->event );
673 console->recnum += count;
674 return count;
677 /* retrieve a pointer to the console input records */
678 static int read_console_input( obj_handle_t handle, int count, int flush )
680 struct console_input *console;
682 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
683 FILE_READ_DATA, &console_input_ops )))
684 return -1;
686 if (!count)
688 /* special case: do not retrieve anything, but return
689 * the total number of records available */
690 count = console->recnum;
692 else
694 if (count > console->recnum) count = console->recnum;
695 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
697 if (flush)
699 int i;
700 for (i = count; i < console->recnum; i++)
701 console->records[i-count] = console->records[i];
702 if ((console->recnum -= count) > 0)
704 INPUT_RECORD *new_rec = realloc( console->records,
705 console->recnum * sizeof(INPUT_RECORD) );
706 if (new_rec) console->records = new_rec;
708 else
710 free( console->records );
711 console->records = NULL;
712 reset_event( console->event );
715 release_object( console );
716 return count;
719 /* set misc console input information */
720 static int set_console_input_info( const struct set_console_input_info_request *req,
721 const WCHAR *title, data_size_t len )
723 struct console_input *console;
724 struct console_renderer_event evt;
726 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
727 if (console_input_is_bare(console) &&
728 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
729 SET_CONSOLE_INPUT_INFO_WIN)))
731 set_error( STATUS_UNSUCCESSFUL );
732 goto error;
735 memset(&evt.u, 0, sizeof(evt.u));
736 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
738 struct screen_buffer *screen_buffer;
740 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
741 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
742 if (!screen_buffer || screen_buffer->input != console)
744 set_error( STATUS_INVALID_HANDLE );
745 if (screen_buffer) release_object( screen_buffer );
746 goto error;
749 if (screen_buffer != console->active)
751 if (console->active) release_object( console->active );
752 console->active = screen_buffer;
753 generate_sb_initial_events( console );
755 else
756 release_object( screen_buffer );
758 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
760 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
761 if (new_title)
763 memcpy( new_title, title, len );
764 new_title[len / sizeof(WCHAR)] = 0;
765 free( console->title );
766 console->title = new_title;
767 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
768 console_input_events_append( console, &evt );
771 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
773 console->history_mode = req->history_mode;
775 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
776 console->history_size != req->history_size)
778 WCHAR** mem = NULL;
779 int i;
780 int delta;
782 if (req->history_size)
784 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
785 if (!mem) goto error;
786 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
789 delta = (console->history_index > req->history_size) ?
790 (console->history_index - req->history_size) : 0;
792 for (i = delta; i < console->history_index; i++)
794 mem[i - delta] = console->history[i];
795 console->history[i] = NULL;
797 console->history_index -= delta;
799 for (i = 0; i < console->history_size; i++)
800 free( console->history[i] );
801 free( console->history );
802 console->history = mem;
803 console->history_size = req->history_size;
805 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
807 console->edition_mode = req->edition_mode;
809 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
811 console->input_cp = req->input_cp;
813 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
815 console->output_cp = req->output_cp;
817 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
819 console->win = req->win;
821 release_object( console );
822 return 1;
823 error:
824 if (console) release_object( console );
825 return 0;
828 /* resize a screen buffer */
829 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
830 int new_width, int new_height )
832 int i, old_width, old_height, copy_width, copy_height;
833 char_info_t *new_data;
835 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
837 set_error( STATUS_NO_MEMORY );
838 return 0;
840 old_width = screen_buffer->width;
841 old_height = screen_buffer->height;
842 copy_width = min( old_width, new_width );
843 copy_height = min( old_height, new_height );
845 /* copy all the rows */
846 for (i = 0; i < copy_height; i++)
848 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
849 copy_width * sizeof(char_info_t) );
852 /* clear the end of each row */
853 if (new_width > old_width)
855 /* fill first row */
856 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
857 /* and blast it to the other rows */
858 for (i = 1; i < copy_height; i++)
859 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
860 (new_width - old_width) * sizeof(char_info_t) );
863 /* clear remaining rows */
864 if (new_height > old_height)
866 /* fill first row */
867 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
868 /* and blast it to the other rows */
869 for (i = old_height+1; i < new_height; i++)
870 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
871 new_width * sizeof(char_info_t) );
873 free( screen_buffer->data );
874 screen_buffer->data = new_data;
875 screen_buffer->width = new_width;
876 screen_buffer->height = new_height;
877 return 1;
880 /* set misc screen buffer information */
881 static int set_console_output_info( struct screen_buffer *screen_buffer,
882 const struct set_console_output_info_request *req )
884 struct console_renderer_event evt;
886 memset(&evt.u, 0, sizeof(evt.u));
887 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
889 if (req->cursor_size < 1 || req->cursor_size > 100)
891 set_error( STATUS_INVALID_PARAMETER );
892 return 0;
894 if (screen_buffer->cursor_size != req->cursor_size ||
895 screen_buffer->cursor_visible != req->cursor_visible)
897 screen_buffer->cursor_size = req->cursor_size;
898 screen_buffer->cursor_visible = req->cursor_visible;
899 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
900 evt.u.cursor_geom.size = req->cursor_size;
901 evt.u.cursor_geom.visible = req->cursor_visible;
902 console_input_events_append( screen_buffer->input, &evt );
905 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
907 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
908 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
910 set_error( STATUS_INVALID_PARAMETER );
911 return 0;
913 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
915 screen_buffer->cursor_x = req->cursor_x;
916 screen_buffer->cursor_y = req->cursor_y;
917 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
918 evt.u.cursor_pos.x = req->cursor_x;
919 evt.u.cursor_pos.y = req->cursor_y;
920 console_input_events_append( screen_buffer->input, &evt );
923 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
925 unsigned cc;
927 /* new screen-buffer cannot be smaller than actual window */
928 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
929 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
931 set_error( STATUS_INVALID_PARAMETER );
932 return 0;
934 /* FIXME: there are also some basic minimum and max size to deal with */
935 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
937 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
938 evt.u.resize.width = req->width;
939 evt.u.resize.height = req->height;
940 console_input_events_append( screen_buffer->input, &evt );
942 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
943 evt.u.update.top = 0;
944 evt.u.update.bottom = screen_buffer->height - 1;
945 console_input_events_append( screen_buffer->input, &evt );
947 /* scroll window to display sb */
948 if (screen_buffer->win.right >= req->width)
950 screen_buffer->win.right -= screen_buffer->win.left;
951 screen_buffer->win.left = 0;
953 if (screen_buffer->win.bottom >= req->height)
955 screen_buffer->win.bottom -= screen_buffer->win.top;
956 screen_buffer->win.top = 0;
958 /* reset cursor if needed (normally, if cursor was outside of new sb, the
959 * window has been shifted so that the new position of the cursor will be
960 * visible */
961 cc = 0;
962 if (screen_buffer->cursor_x >= req->width)
964 screen_buffer->cursor_x = req->width - 1;
965 cc++;
967 if (screen_buffer->cursor_y >= req->height)
969 screen_buffer->cursor_y = req->height - 1;
970 cc++;
972 if (cc)
974 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
975 evt.u.cursor_pos.x = req->cursor_x;
976 evt.u.cursor_pos.y = req->cursor_y;
977 console_input_events_append( screen_buffer->input, &evt );
980 if (screen_buffer == screen_buffer->input->active &&
981 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
983 INPUT_RECORD ir;
984 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
985 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
986 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
987 write_console_input( screen_buffer->input, 1, &ir );
990 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
992 screen_buffer->attr = req->attr;
994 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
996 if (req->win_left < 0 || req->win_left > req->win_right ||
997 req->win_right >= screen_buffer->width ||
998 req->win_top < 0 || req->win_top > req->win_bottom ||
999 req->win_bottom >= screen_buffer->height)
1001 set_error( STATUS_INVALID_PARAMETER );
1002 return 0;
1004 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1005 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1007 screen_buffer->win.left = req->win_left;
1008 screen_buffer->win.top = req->win_top;
1009 screen_buffer->win.right = req->win_right;
1010 screen_buffer->win.bottom = req->win_bottom;
1011 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1012 evt.u.display.left = req->win_left;
1013 evt.u.display.top = req->win_top;
1014 evt.u.display.width = req->win_right - req->win_left + 1;
1015 evt.u.display.height = req->win_bottom - req->win_top + 1;
1016 console_input_events_append( screen_buffer->input, &evt );
1019 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1021 screen_buffer->max_width = req->max_width;
1022 screen_buffer->max_height = req->max_height;
1024 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1026 screen_buffer->font.width = req->font_width;
1027 screen_buffer->font.height = req->font_height;
1030 return 1;
1033 /* appends a new line to history (history is a fixed size array) */
1034 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1036 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1038 if (!ptr)
1039 return;
1041 if (!console || !console->history_size)
1043 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1044 free( ptr );
1045 return;
1048 memcpy( ptr, buf, len * sizeof(WCHAR) );
1049 ptr[len] = 0;
1051 if (console->history_mode && console->history_index &&
1052 strncmpW( console->history[console->history_index - 1], ptr, len ) == 0)
1054 /* ok, mode ask us to not use twice the same string...
1055 * so just free mem and returns
1057 set_error( STATUS_ALIAS_EXISTS );
1058 free(ptr);
1059 return;
1062 if (console->history_index < console->history_size)
1064 console->history[console->history_index++] = ptr;
1066 else
1068 free( console->history[0]) ;
1069 memmove( &console->history[0], &console->history[1],
1070 (console->history_size - 1) * sizeof(WCHAR*) );
1071 console->history[console->history_size - 1] = ptr;
1075 /* returns a line from the cache */
1076 static data_size_t console_input_get_hist( struct console_input *console, int index )
1078 data_size_t ret = 0;
1080 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1081 else
1083 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1084 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1086 return ret;
1089 /* dumb dump */
1090 static void console_input_dump( struct object *obj, int verbose )
1092 struct console_input *console = (struct console_input *)obj;
1093 assert( obj->ops == &console_input_ops );
1094 fprintf( stderr, "Console input active=%p evt=%p\n",
1095 console->active, console->evt );
1098 static void console_input_destroy( struct object *obj )
1100 struct console_input* console_in = (struct console_input *)obj;
1101 struct screen_buffer* curr;
1102 int i;
1104 assert( obj->ops == &console_input_ops );
1105 free( console_in->title );
1106 free( console_in->records );
1108 if (console_in->active) release_object( console_in->active );
1109 console_in->active = NULL;
1111 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1113 if (curr->input == console_in) curr->input = NULL;
1116 if (console_in->evt)
1118 release_object( console_in->evt );
1119 console_in->evt = NULL;
1121 if (console_in->event)
1122 release_object( console_in->event );
1123 if (console_in->fd)
1124 release_object( console_in->fd );
1126 for (i = 0; i < console_in->history_size; i++)
1127 free( console_in->history[i] );
1128 free( console_in->history );
1131 static void screen_buffer_dump( struct object *obj, int verbose )
1133 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1134 assert( obj->ops == &screen_buffer_ops );
1136 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1139 static void screen_buffer_destroy( struct object *obj )
1141 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1143 assert( obj->ops == &screen_buffer_ops );
1145 list_remove( &screen_buffer->entry );
1147 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1149 struct screen_buffer *sb;
1151 screen_buffer->input->active = NULL;
1152 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1154 if (sb->input == screen_buffer->input)
1156 sb->input->active = sb;
1157 break;
1161 if (screen_buffer->fd) release_object( screen_buffer->fd );
1162 free( screen_buffer->data );
1165 static struct fd *screen_buffer_get_fd( struct object *obj )
1167 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1168 assert( obj->ops == &screen_buffer_ops );
1169 if (screen_buffer->fd)
1170 return (struct fd*)grab_object( screen_buffer->fd );
1171 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1172 return NULL;
1175 /* write data into a screen buffer */
1176 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1177 const void* data, enum char_info_mode mode,
1178 int x, int y, int wrap )
1180 unsigned int i;
1181 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1183 if (y >= screen_buffer->height) return 0;
1185 if (wrap)
1186 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1187 else
1188 end = screen_buffer->data + (y+1) * screen_buffer->width;
1190 switch(mode)
1192 case CHAR_INFO_MODE_TEXT:
1194 const WCHAR *ptr = data;
1195 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1197 break;
1198 case CHAR_INFO_MODE_ATTR:
1200 const unsigned short *ptr = data;
1201 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1203 break;
1204 case CHAR_INFO_MODE_TEXTATTR:
1206 const char_info_t *ptr = data;
1207 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1209 break;
1210 case CHAR_INFO_MODE_TEXTSTDATTR:
1212 const WCHAR *ptr = data;
1213 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1215 dest->ch = ptr[i];
1216 dest->attr = screen_buffer->attr;
1219 break;
1220 default:
1221 set_error( STATUS_INVALID_PARAMETER );
1222 return 0;
1225 if (i && screen_buffer == screen_buffer->input->active)
1227 struct console_renderer_event evt;
1228 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1229 memset(&evt.u, 0, sizeof(evt.u));
1230 evt.u.update.top = y + x / screen_buffer->width;
1231 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1232 console_input_events_append( screen_buffer->input, &evt );
1234 return i;
1237 /* fill a screen buffer with uniform data */
1238 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1239 enum char_info_mode mode, int x, int y, int count, int wrap )
1241 int i;
1242 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1244 if (y >= screen_buffer->height) return 0;
1246 if (wrap)
1247 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1248 else
1249 end = screen_buffer->data + (y+1) * screen_buffer->width;
1251 if (count > end - dest) count = end - dest;
1253 switch(mode)
1255 case CHAR_INFO_MODE_TEXT:
1256 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1257 break;
1258 case CHAR_INFO_MODE_ATTR:
1259 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1260 break;
1261 case CHAR_INFO_MODE_TEXTATTR:
1262 for (i = 0; i < count; i++) dest[i] = data;
1263 break;
1264 case CHAR_INFO_MODE_TEXTSTDATTR:
1265 for (i = 0; i < count; i++)
1267 dest[i].ch = data.ch;
1268 dest[i].attr = screen_buffer->attr;
1270 break;
1271 default:
1272 set_error( STATUS_INVALID_PARAMETER );
1273 return 0;
1276 if (count && screen_buffer == screen_buffer->input->active)
1278 struct console_renderer_event evt;
1279 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1280 memset(&evt.u, 0, sizeof(evt.u));
1281 evt.u.update.top = y;
1282 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1283 console_input_events_append( screen_buffer->input, &evt );
1285 return i;
1288 /* read data from a screen buffer */
1289 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1290 enum char_info_mode mode, int wrap )
1292 int i;
1293 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1295 if (y >= screen_buffer->height) return;
1297 if (wrap)
1298 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1299 else
1300 end = screen_buffer->data + (y+1) * screen_buffer->width;
1302 switch(mode)
1304 case CHAR_INFO_MODE_TEXT:
1306 WCHAR *data;
1307 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1308 if ((data = set_reply_data_size( count * sizeof(*data) )))
1310 for (i = 0; i < count; i++) data[i] = src[i].ch;
1313 break;
1314 case CHAR_INFO_MODE_ATTR:
1316 unsigned short *data;
1317 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1318 if ((data = set_reply_data_size( count * sizeof(*data) )))
1320 for (i = 0; i < count; i++) data[i] = src[i].attr;
1323 break;
1324 case CHAR_INFO_MODE_TEXTATTR:
1326 char_info_t *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];
1333 break;
1334 default:
1335 set_error( STATUS_INVALID_PARAMETER );
1336 break;
1340 /* scroll parts of a screen buffer */
1341 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1342 int w, int h )
1344 int j;
1345 char_info_t *psrc, *pdst;
1346 struct console_renderer_event evt;
1348 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1349 xsrc + w > screen_buffer->width ||
1350 xdst + w > screen_buffer->width ||
1351 ysrc + h > screen_buffer->height ||
1352 ydst + h > screen_buffer->height ||
1353 w == 0 || h == 0)
1355 set_error( STATUS_INVALID_PARAMETER );
1356 return;
1359 if (ysrc < ydst)
1361 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1362 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1364 for (j = h; j > 0; j--)
1366 memcpy(pdst, psrc, w * sizeof(*pdst) );
1367 pdst -= screen_buffer->width;
1368 psrc -= screen_buffer->width;
1371 else
1373 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1374 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1376 for (j = 0; j < h; j++)
1378 /* we use memmove here because when psrc and pdst are the same,
1379 * copies are done on the same row, so the dst and src blocks
1380 * can overlap */
1381 memmove( pdst, psrc, w * sizeof(*pdst) );
1382 pdst += screen_buffer->width;
1383 psrc += screen_buffer->width;
1387 /* FIXME: this could be enhanced, by signalling scroll */
1388 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1389 memset(&evt.u, 0, sizeof(evt.u));
1390 evt.u.update.top = min(ysrc, ydst);
1391 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1392 console_input_events_append( screen_buffer->input, &evt );
1395 /* allocate a console for the renderer */
1396 DECL_HANDLER(alloc_console)
1398 obj_handle_t in = 0;
1399 obj_handle_t evt = 0;
1400 struct process *process;
1401 struct thread *renderer;
1402 struct console_input *console;
1403 int fd;
1404 int attach = 0;
1406 if (req->input_fd != -1)
1408 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1410 set_error( STATUS_INVALID_PARAMETER );
1411 return;
1414 else fd = -1;
1416 switch (req->pid)
1418 case 0:
1419 /* renderer is current, console to be attached to parent process */
1420 renderer = current;
1421 if (!(process = current->process->parent))
1423 if (fd != -1) close( fd );
1424 set_error( STATUS_ACCESS_DENIED );
1425 return;
1427 grab_object( process );
1428 attach = 1;
1429 break;
1430 case 0xffffffff:
1431 /* no renderer, console to be attached to current process */
1432 renderer = NULL;
1433 process = current->process;
1434 grab_object( process );
1435 attach = 1;
1436 break;
1437 default:
1438 /* renderer is current, console to be attached to req->pid */
1439 renderer = current;
1440 if (!(process = get_process_from_id( req->pid )))
1442 if (fd != -1) close( fd );
1443 return;
1447 if (attach && process->console)
1449 if (fd != -1) close( fd );
1450 set_error( STATUS_ACCESS_DENIED );
1451 goto the_end;
1454 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1456 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1458 if (!console->evt ||
1459 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1461 if (attach)
1463 process->console = (struct console_input*)grab_object( console );
1464 console->num_proc++;
1466 reply->handle_in = in;
1467 reply->event = evt;
1468 release_object( console );
1469 goto the_end;
1471 close_handle( current->process, in );
1473 release_object( console );
1475 the_end:
1476 release_object( process );
1479 /* free the console of the current process */
1480 DECL_HANDLER(free_console)
1482 free_console( current->process );
1485 /* let the renderer peek the events it's waiting on */
1486 DECL_HANDLER(get_console_renderer_events)
1488 struct console_input_events *evt;
1490 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1491 FILE_READ_PROPERTIES, &console_input_events_ops );
1492 if (!evt) return;
1493 console_input_events_get( evt );
1494 release_object( evt );
1497 /* open a handle to the process console */
1498 DECL_HANDLER(open_console)
1500 struct object *obj = NULL;
1502 reply->handle = 0;
1503 if (!req->from)
1505 if (current->process->console)
1506 obj = grab_object( (struct object*)current->process->console );
1508 else if (req->from == (obj_handle_t)1)
1510 if (current->process->console && current->process->console->active)
1511 obj = grab_object( (struct object*)current->process->console->active );
1513 else if ((obj = get_handle_obj( current->process, req->from,
1514 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1516 struct console_input *console = (struct console_input *)obj;
1517 obj = (console->active) ? grab_object( console->active ) : NULL;
1518 release_object( console );
1521 /* FIXME: req->share is not used (as in screen buffer creation) */
1522 if (obj)
1524 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1525 release_object( obj );
1527 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1530 /* set info about a console input */
1531 DECL_HANDLER(set_console_input_info)
1533 set_console_input_info( req, get_req_data(), get_req_data_size() );
1536 /* get info about a console (output only) */
1537 DECL_HANDLER(get_console_input_info)
1539 struct console_input *console;
1541 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1542 if (console->title)
1544 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1545 if (len > get_reply_max_size()) len = get_reply_max_size();
1546 set_reply_data( console->title, len );
1548 reply->history_mode = console->history_mode;
1549 reply->history_size = console->history_size;
1550 reply->history_index = console->history_index;
1551 reply->edition_mode = console->edition_mode;
1552 reply->input_cp = console->input_cp;
1553 reply->output_cp = console->output_cp;
1554 reply->win = console->win;
1556 release_object( console );
1559 /* get a console mode (input or output) */
1560 DECL_HANDLER(get_console_mode)
1562 reply->mode = get_console_mode( req->handle );
1565 /* set a console mode (input or output) */
1566 DECL_HANDLER(set_console_mode)
1568 set_console_mode( req->handle, req->mode );
1571 /* add input records to a console input queue */
1572 DECL_HANDLER(write_console_input)
1574 struct console_input *console;
1576 reply->written = 0;
1577 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1578 FILE_WRITE_PROPERTIES, &console_input_ops )))
1579 return;
1580 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1581 get_req_data() );
1582 release_object( console );
1585 /* fetch input records from a console input queue */
1586 DECL_HANDLER(read_console_input)
1588 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1589 reply->read = read_console_input( req->handle, count, req->flush );
1592 /* appends a string to console's history */
1593 DECL_HANDLER(append_console_input_history)
1595 struct console_input *console;
1597 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1598 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1599 release_object( console );
1602 /* appends a string to console's history */
1603 DECL_HANDLER(get_console_input_history)
1605 struct console_input *console;
1607 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1608 reply->total = console_input_get_hist( console, req->index );
1609 release_object( console );
1612 /* creates a screen buffer */
1613 DECL_HANDLER(create_console_output)
1615 struct console_input* console;
1616 struct screen_buffer* screen_buffer;
1617 int fd;
1619 if (req->fd != -1)
1621 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1623 set_error( STATUS_INVALID_HANDLE );
1624 return;
1627 else fd = -1;
1628 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1630 if (fd != -1) close( fd );
1631 return;
1633 if (console_input_is_bare( console ) ^ (fd != -1))
1635 if (fd != -1) close( fd );
1636 release_object( console );
1637 set_error( STATUS_INVALID_HANDLE );
1638 return;
1641 screen_buffer = create_console_output( console, fd );
1642 if (screen_buffer)
1644 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1645 * see file.c on how this could be done */
1646 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1647 release_object( screen_buffer );
1649 release_object( console );
1652 /* set info about a console screen buffer */
1653 DECL_HANDLER(set_console_output_info)
1655 struct screen_buffer *screen_buffer;
1657 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1658 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1660 set_console_output_info( screen_buffer, req );
1661 release_object( screen_buffer );
1665 /* get info about a console screen buffer */
1666 DECL_HANDLER(get_console_output_info)
1668 struct screen_buffer *screen_buffer;
1670 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1671 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1673 reply->cursor_size = screen_buffer->cursor_size;
1674 reply->cursor_visible = screen_buffer->cursor_visible;
1675 reply->cursor_x = screen_buffer->cursor_x;
1676 reply->cursor_y = screen_buffer->cursor_y;
1677 reply->width = screen_buffer->width;
1678 reply->height = screen_buffer->height;
1679 reply->attr = screen_buffer->attr;
1680 reply->win_left = screen_buffer->win.left;
1681 reply->win_top = screen_buffer->win.top;
1682 reply->win_right = screen_buffer->win.right;
1683 reply->win_bottom = screen_buffer->win.bottom;
1684 reply->max_width = screen_buffer->max_width;
1685 reply->max_height = screen_buffer->max_height;
1686 reply->font_width = screen_buffer->font.width;
1687 reply->font_height = screen_buffer->font.height;
1688 release_object( screen_buffer );
1692 /* read data (chars & attrs) from a screen buffer */
1693 DECL_HANDLER(read_console_output)
1695 struct screen_buffer *screen_buffer;
1697 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1698 FILE_READ_DATA, &screen_buffer_ops )))
1700 if (console_input_is_bare( screen_buffer->input ))
1702 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1703 release_object( screen_buffer );
1704 return;
1706 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1707 reply->width = screen_buffer->width;
1708 reply->height = screen_buffer->height;
1709 release_object( screen_buffer );
1713 /* write data (char and/or attrs) to a screen buffer */
1714 DECL_HANDLER(write_console_output)
1716 struct screen_buffer *screen_buffer;
1718 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1719 FILE_WRITE_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 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1728 req->mode, req->x, req->y, req->wrap );
1729 reply->width = screen_buffer->width;
1730 reply->height = screen_buffer->height;
1731 release_object( screen_buffer );
1735 /* fill a screen buffer with constant data (chars and/or attributes) */
1736 DECL_HANDLER(fill_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 = fill_console_output( screen_buffer, req->data, req->mode,
1750 req->x, req->y, req->count, req->wrap );
1751 release_object( screen_buffer );
1755 /* move a rect of data in a screen buffer */
1756 DECL_HANDLER(move_console_output)
1758 struct screen_buffer *screen_buffer;
1760 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1761 FILE_WRITE_DATA, &screen_buffer_ops)))
1763 if (console_input_is_bare( screen_buffer->input ))
1765 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1766 release_object( screen_buffer );
1767 return;
1769 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1770 req->w, req->h );
1771 release_object( screen_buffer );
1775 /* sends a signal to a console (process, group...) */
1776 DECL_HANDLER(send_console_signal)
1778 process_id_t group;
1780 group = req->group_id ? req->group_id : current->process->group_id;
1782 if (!group)
1783 set_error( STATUS_INVALID_PARAMETER );
1784 else
1785 propagate_console_signal( current->process->console, req->signal, group );
1788 /* get console which renderer is 'current' */
1789 static int cgwe_enum( struct process* process, void* user)
1791 if (process->console && process->console->renderer == current)
1793 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1794 return 1;
1796 return 0;
1799 DECL_HANDLER(get_console_wait_event)
1801 struct console_input* console = NULL;
1803 if (current->process->console)
1804 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1805 else enum_processes(cgwe_enum, &console);
1807 if (console)
1809 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1810 release_object( console );
1812 else set_error( STATUS_INVALID_PARAMETER );