reg/tests: Fix the add empty type test.
[wine.git] / server / console.c
bloba57b2fea56cdd2a6f78f3b2678d29e2f61f28913
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 /* can only be done by renderer */
1022 if (current->process->console != screen_buffer->input)
1024 set_error( STATUS_INVALID_PARAMETER );
1025 return 0;
1028 screen_buffer->max_width = req->max_width;
1029 screen_buffer->max_height = req->max_height;
1031 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1033 screen_buffer->font.width = req->font_width;
1034 screen_buffer->font.height = req->font_height;
1037 return 1;
1040 /* appends a new line to history (history is a fixed size array) */
1041 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1043 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1045 if (!ptr)
1046 return;
1048 if (!console || !console->history_size)
1050 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1051 free( ptr );
1052 return;
1055 memcpy( ptr, buf, len * sizeof(WCHAR) );
1056 ptr[len] = 0;
1058 if (console->history_mode && console->history_index &&
1059 strncmpW( console->history[console->history_index - 1], ptr, len ) == 0)
1061 /* ok, mode ask us to not use twice the same string...
1062 * so just free mem and returns
1064 set_error( STATUS_ALIAS_EXISTS );
1065 free(ptr);
1066 return;
1069 if (console->history_index < console->history_size)
1071 console->history[console->history_index++] = ptr;
1073 else
1075 free( console->history[0]) ;
1076 memmove( &console->history[0], &console->history[1],
1077 (console->history_size - 1) * sizeof(WCHAR*) );
1078 console->history[console->history_size - 1] = ptr;
1082 /* returns a line from the cache */
1083 static data_size_t console_input_get_hist( struct console_input *console, int index )
1085 data_size_t ret = 0;
1087 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1088 else
1090 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1091 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1093 return ret;
1096 /* dumb dump */
1097 static void console_input_dump( struct object *obj, int verbose )
1099 struct console_input *console = (struct console_input *)obj;
1100 assert( obj->ops == &console_input_ops );
1101 fprintf( stderr, "Console input active=%p evt=%p\n",
1102 console->active, console->evt );
1105 static void console_input_destroy( struct object *obj )
1107 struct console_input* console_in = (struct console_input *)obj;
1108 struct screen_buffer* curr;
1109 int i;
1111 assert( obj->ops == &console_input_ops );
1112 free( console_in->title );
1113 free( console_in->records );
1115 if (console_in->active) release_object( console_in->active );
1116 console_in->active = NULL;
1118 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1120 if (curr->input == console_in) curr->input = NULL;
1123 if (console_in->evt)
1125 release_object( console_in->evt );
1126 console_in->evt = NULL;
1128 if (console_in->event)
1129 release_object( console_in->event );
1130 if (console_in->fd)
1131 release_object( console_in->fd );
1133 for (i = 0; i < console_in->history_size; i++)
1134 free( console_in->history[i] );
1135 free( console_in->history );
1138 static void screen_buffer_dump( struct object *obj, int verbose )
1140 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1141 assert( obj->ops == &screen_buffer_ops );
1143 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1146 static void screen_buffer_destroy( struct object *obj )
1148 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1150 assert( obj->ops == &screen_buffer_ops );
1152 list_remove( &screen_buffer->entry );
1154 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1156 struct screen_buffer *sb;
1158 screen_buffer->input->active = NULL;
1159 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1161 if (sb->input == screen_buffer->input)
1163 sb->input->active = sb;
1164 break;
1168 if (screen_buffer->fd) release_object( screen_buffer->fd );
1169 free( screen_buffer->data );
1172 static struct fd *screen_buffer_get_fd( struct object *obj )
1174 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1175 assert( obj->ops == &screen_buffer_ops );
1176 if (screen_buffer->fd)
1177 return (struct fd*)grab_object( screen_buffer->fd );
1178 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1179 return NULL;
1182 /* write data into a screen buffer */
1183 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1184 const void* data, enum char_info_mode mode,
1185 int x, int y, int wrap )
1187 unsigned int i;
1188 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1190 if (y >= screen_buffer->height) return 0;
1192 if (wrap)
1193 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1194 else
1195 end = screen_buffer->data + (y+1) * screen_buffer->width;
1197 switch(mode)
1199 case CHAR_INFO_MODE_TEXT:
1201 const WCHAR *ptr = data;
1202 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1204 break;
1205 case CHAR_INFO_MODE_ATTR:
1207 const unsigned short *ptr = data;
1208 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1210 break;
1211 case CHAR_INFO_MODE_TEXTATTR:
1213 const char_info_t *ptr = data;
1214 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1216 break;
1217 case CHAR_INFO_MODE_TEXTSTDATTR:
1219 const WCHAR *ptr = data;
1220 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1222 dest->ch = ptr[i];
1223 dest->attr = screen_buffer->attr;
1226 break;
1227 default:
1228 set_error( STATUS_INVALID_PARAMETER );
1229 return 0;
1232 if (i && screen_buffer == screen_buffer->input->active)
1234 struct console_renderer_event evt;
1235 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1236 memset(&evt.u, 0, sizeof(evt.u));
1237 evt.u.update.top = y + x / screen_buffer->width;
1238 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1239 console_input_events_append( screen_buffer->input, &evt );
1241 return i;
1244 /* fill a screen buffer with uniform data */
1245 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1246 enum char_info_mode mode, int x, int y, int count, int wrap )
1248 int i;
1249 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1251 if (y >= screen_buffer->height) return 0;
1253 if (wrap)
1254 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1255 else
1256 end = screen_buffer->data + (y+1) * screen_buffer->width;
1258 if (count > end - dest) count = end - dest;
1260 switch(mode)
1262 case CHAR_INFO_MODE_TEXT:
1263 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1264 break;
1265 case CHAR_INFO_MODE_ATTR:
1266 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1267 break;
1268 case CHAR_INFO_MODE_TEXTATTR:
1269 for (i = 0; i < count; i++) dest[i] = data;
1270 break;
1271 case CHAR_INFO_MODE_TEXTSTDATTR:
1272 for (i = 0; i < count; i++)
1274 dest[i].ch = data.ch;
1275 dest[i].attr = screen_buffer->attr;
1277 break;
1278 default:
1279 set_error( STATUS_INVALID_PARAMETER );
1280 return 0;
1283 if (count && screen_buffer == screen_buffer->input->active)
1285 struct console_renderer_event evt;
1286 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1287 memset(&evt.u, 0, sizeof(evt.u));
1288 evt.u.update.top = y;
1289 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1290 console_input_events_append( screen_buffer->input, &evt );
1292 return i;
1295 /* read data from a screen buffer */
1296 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1297 enum char_info_mode mode, int wrap )
1299 int i;
1300 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1302 if (y >= screen_buffer->height) return;
1304 if (wrap)
1305 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1306 else
1307 end = screen_buffer->data + (y+1) * screen_buffer->width;
1309 switch(mode)
1311 case CHAR_INFO_MODE_TEXT:
1313 WCHAR *data;
1314 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1315 if ((data = set_reply_data_size( count * sizeof(*data) )))
1317 for (i = 0; i < count; i++) data[i] = src[i].ch;
1320 break;
1321 case CHAR_INFO_MODE_ATTR:
1323 unsigned short *data;
1324 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1325 if ((data = set_reply_data_size( count * sizeof(*data) )))
1327 for (i = 0; i < count; i++) data[i] = src[i].attr;
1330 break;
1331 case CHAR_INFO_MODE_TEXTATTR:
1333 char_info_t *data;
1334 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1335 if ((data = set_reply_data_size( count * sizeof(*data) )))
1337 for (i = 0; i < count; i++) data[i] = src[i];
1340 break;
1341 default:
1342 set_error( STATUS_INVALID_PARAMETER );
1343 break;
1347 /* scroll parts of a screen buffer */
1348 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1349 int w, int h )
1351 int j;
1352 char_info_t *psrc, *pdst;
1353 struct console_renderer_event evt;
1355 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1356 xsrc + w > screen_buffer->width ||
1357 xdst + w > screen_buffer->width ||
1358 ysrc + h > screen_buffer->height ||
1359 ydst + h > screen_buffer->height ||
1360 w == 0 || h == 0)
1362 set_error( STATUS_INVALID_PARAMETER );
1363 return;
1366 if (ysrc < ydst)
1368 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1369 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1371 for (j = h; j > 0; j--)
1373 memcpy(pdst, psrc, w * sizeof(*pdst) );
1374 pdst -= screen_buffer->width;
1375 psrc -= screen_buffer->width;
1378 else
1380 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1381 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1383 for (j = 0; j < h; j++)
1385 /* we use memmove here because when psrc and pdst are the same,
1386 * copies are done on the same row, so the dst and src blocks
1387 * can overlap */
1388 memmove( pdst, psrc, w * sizeof(*pdst) );
1389 pdst += screen_buffer->width;
1390 psrc += screen_buffer->width;
1394 /* FIXME: this could be enhanced, by signalling scroll */
1395 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1396 memset(&evt.u, 0, sizeof(evt.u));
1397 evt.u.update.top = min(ysrc, ydst);
1398 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1399 console_input_events_append( screen_buffer->input, &evt );
1402 /* allocate a console for the renderer */
1403 DECL_HANDLER(alloc_console)
1405 obj_handle_t in = 0;
1406 obj_handle_t evt = 0;
1407 struct process *process;
1408 struct thread *renderer;
1409 struct console_input *console;
1410 int fd;
1411 int attach = 0;
1413 if (req->input_fd != -1)
1415 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1417 set_error( STATUS_INVALID_PARAMETER );
1418 return;
1421 else fd = -1;
1423 switch (req->pid)
1425 case 0:
1426 /* renderer is current, console to be attached to parent process */
1427 renderer = current;
1428 if (!(process = current->process->parent))
1430 if (fd != -1) close( fd );
1431 set_error( STATUS_ACCESS_DENIED );
1432 return;
1434 grab_object( process );
1435 attach = 1;
1436 break;
1437 case 0xffffffff:
1438 /* no renderer, console to be attached to current process */
1439 renderer = NULL;
1440 process = current->process;
1441 grab_object( process );
1442 attach = 1;
1443 break;
1444 default:
1445 /* renderer is current, console to be attached to req->pid */
1446 renderer = current;
1447 if (!(process = get_process_from_id( req->pid )))
1449 if (fd != -1) close( fd );
1450 return;
1454 if (attach && process->console)
1456 if (fd != -1) close( fd );
1457 set_error( STATUS_ACCESS_DENIED );
1458 goto the_end;
1461 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1463 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1465 if (!console->evt ||
1466 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1468 if (attach)
1470 process->console = (struct console_input*)grab_object( console );
1471 console->num_proc++;
1473 reply->handle_in = in;
1474 reply->event = evt;
1475 release_object( console );
1476 goto the_end;
1478 close_handle( current->process, in );
1480 release_object( console );
1482 the_end:
1483 release_object( process );
1486 /* free the console of the current process */
1487 DECL_HANDLER(free_console)
1489 free_console( current->process );
1492 /* let the renderer peek the events it's waiting on */
1493 DECL_HANDLER(get_console_renderer_events)
1495 struct console_input_events *evt;
1497 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1498 FILE_READ_PROPERTIES, &console_input_events_ops );
1499 if (!evt) return;
1500 console_input_events_get( evt );
1501 release_object( evt );
1504 /* open a handle to the process console */
1505 DECL_HANDLER(open_console)
1507 struct object *obj = NULL;
1509 reply->handle = 0;
1510 if (!req->from)
1512 if (current->process->console)
1513 obj = grab_object( (struct object*)current->process->console );
1515 else if (req->from == (obj_handle_t)1)
1517 if (current->process->console && current->process->console->active)
1518 obj = grab_object( (struct object*)current->process->console->active );
1520 else if ((obj = get_handle_obj( current->process, req->from,
1521 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1523 struct console_input *console = (struct console_input *)obj;
1524 obj = (console->active) ? grab_object( console->active ) : NULL;
1525 release_object( console );
1528 /* FIXME: req->share is not used (as in screen buffer creation) */
1529 if (obj)
1531 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1532 release_object( obj );
1534 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1537 /* set info about a console input */
1538 DECL_HANDLER(set_console_input_info)
1540 set_console_input_info( req, get_req_data(), get_req_data_size() );
1543 /* get info about a console (output only) */
1544 DECL_HANDLER(get_console_input_info)
1546 struct console_input *console;
1548 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1549 if (console->title)
1551 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1552 if (len > get_reply_max_size()) len = get_reply_max_size();
1553 set_reply_data( console->title, len );
1555 reply->history_mode = console->history_mode;
1556 reply->history_size = console->history_size;
1557 reply->history_index = console->history_index;
1558 reply->edition_mode = console->edition_mode;
1559 reply->input_cp = console->input_cp;
1560 reply->output_cp = console->output_cp;
1561 reply->win = console->win;
1563 release_object( console );
1566 /* get a console mode (input or output) */
1567 DECL_HANDLER(get_console_mode)
1569 reply->mode = get_console_mode( req->handle );
1572 /* set a console mode (input or output) */
1573 DECL_HANDLER(set_console_mode)
1575 set_console_mode( req->handle, req->mode );
1578 /* add input records to a console input queue */
1579 DECL_HANDLER(write_console_input)
1581 struct console_input *console;
1583 reply->written = 0;
1584 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1585 FILE_WRITE_PROPERTIES, &console_input_ops )))
1586 return;
1587 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1588 get_req_data() );
1589 release_object( console );
1592 /* fetch input records from a console input queue */
1593 DECL_HANDLER(read_console_input)
1595 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1596 reply->read = read_console_input( req->handle, count, req->flush );
1599 /* appends a string to console's history */
1600 DECL_HANDLER(append_console_input_history)
1602 struct console_input *console;
1604 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1605 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1606 release_object( console );
1609 /* appends a string to console's history */
1610 DECL_HANDLER(get_console_input_history)
1612 struct console_input *console;
1614 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1615 reply->total = console_input_get_hist( console, req->index );
1616 release_object( console );
1619 /* creates a screen buffer */
1620 DECL_HANDLER(create_console_output)
1622 struct console_input* console;
1623 struct screen_buffer* screen_buffer;
1624 int fd;
1626 if (req->fd != -1)
1628 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1630 set_error( STATUS_INVALID_HANDLE );
1631 return;
1634 else fd = -1;
1635 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1637 if (fd != -1) close( fd );
1638 return;
1640 if (console_input_is_bare( console ) ^ (fd != -1))
1642 if (fd != -1) close( fd );
1643 release_object( console );
1644 set_error( STATUS_INVALID_HANDLE );
1645 return;
1648 screen_buffer = create_console_output( console, fd );
1649 if (screen_buffer)
1651 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1652 * see file.c on how this could be done */
1653 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1654 release_object( screen_buffer );
1656 release_object( console );
1659 /* set info about a console screen buffer */
1660 DECL_HANDLER(set_console_output_info)
1662 struct screen_buffer *screen_buffer;
1664 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1665 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1667 set_console_output_info( screen_buffer, req );
1668 release_object( screen_buffer );
1672 /* get info about a console screen buffer */
1673 DECL_HANDLER(get_console_output_info)
1675 struct screen_buffer *screen_buffer;
1677 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1678 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1680 reply->cursor_size = screen_buffer->cursor_size;
1681 reply->cursor_visible = screen_buffer->cursor_visible;
1682 reply->cursor_x = screen_buffer->cursor_x;
1683 reply->cursor_y = screen_buffer->cursor_y;
1684 reply->width = screen_buffer->width;
1685 reply->height = screen_buffer->height;
1686 reply->attr = screen_buffer->attr;
1687 reply->win_left = screen_buffer->win.left;
1688 reply->win_top = screen_buffer->win.top;
1689 reply->win_right = screen_buffer->win.right;
1690 reply->win_bottom = screen_buffer->win.bottom;
1691 reply->max_width = screen_buffer->max_width;
1692 reply->max_height = screen_buffer->max_height;
1693 reply->font_width = screen_buffer->font.width;
1694 reply->font_height = screen_buffer->font.height;
1695 release_object( screen_buffer );
1699 /* read data (chars & attrs) from a screen buffer */
1700 DECL_HANDLER(read_console_output)
1702 struct screen_buffer *screen_buffer;
1704 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1705 FILE_READ_DATA, &screen_buffer_ops )))
1707 if (console_input_is_bare( screen_buffer->input ))
1709 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1710 release_object( screen_buffer );
1711 return;
1713 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1714 reply->width = screen_buffer->width;
1715 reply->height = screen_buffer->height;
1716 release_object( screen_buffer );
1720 /* write data (char and/or attrs) to a screen buffer */
1721 DECL_HANDLER(write_console_output)
1723 struct screen_buffer *screen_buffer;
1725 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1726 FILE_WRITE_DATA, &screen_buffer_ops)))
1728 if (console_input_is_bare( screen_buffer->input ))
1730 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1731 release_object( screen_buffer );
1732 return;
1734 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1735 req->mode, req->x, req->y, req->wrap );
1736 reply->width = screen_buffer->width;
1737 reply->height = screen_buffer->height;
1738 release_object( screen_buffer );
1742 /* fill a screen buffer with constant data (chars and/or attributes) */
1743 DECL_HANDLER(fill_console_output)
1745 struct screen_buffer *screen_buffer;
1747 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1748 FILE_WRITE_DATA, &screen_buffer_ops)))
1750 if (console_input_is_bare( screen_buffer->input ))
1752 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1753 release_object( screen_buffer );
1754 return;
1756 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1757 req->x, req->y, req->count, req->wrap );
1758 release_object( screen_buffer );
1762 /* move a rect of data in a screen buffer */
1763 DECL_HANDLER(move_console_output)
1765 struct screen_buffer *screen_buffer;
1767 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1768 FILE_WRITE_DATA, &screen_buffer_ops)))
1770 if (console_input_is_bare( screen_buffer->input ))
1772 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1773 release_object( screen_buffer );
1774 return;
1776 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1777 req->w, req->h );
1778 release_object( screen_buffer );
1782 /* sends a signal to a console (process, group...) */
1783 DECL_HANDLER(send_console_signal)
1785 process_id_t group;
1787 group = req->group_id ? req->group_id : current->process->group_id;
1789 if (!group)
1790 set_error( STATUS_INVALID_PARAMETER );
1791 else
1792 propagate_console_signal( current->process->console, req->signal, group );
1795 /* get console which renderer is 'current' */
1796 static int cgwe_enum( struct process* process, void* user)
1798 if (process->console && process->console->renderer == current)
1800 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1801 return 1;
1803 return 0;
1806 DECL_HANDLER(get_console_wait_event)
1808 struct console_input* console = NULL;
1810 if (current->process->console)
1811 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1812 else enum_processes(cgwe_enum, &console);
1814 if (console)
1816 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1817 release_object( console );
1819 else set_error( STATUS_INVALID_PARAMETER );