amstream: Increase parent IAMMediaStream refcount in IDirectDrawMediaStream::CreateSa...
[wine.git] / server / console.c
blob53910b3f46e4459c3a283decf6ae5d1b1470d420
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"
41 #include "wine/condrv.h"
43 struct screen_buffer;
44 struct console_input_events;
46 struct history_line
48 data_size_t len;
49 WCHAR text[1];
52 struct console_input
54 struct object obj; /* object header */
55 int num_proc; /* number of processes attached to this console */
56 struct thread *renderer; /* console renderer thread */
57 int mode; /* input mode */
58 struct screen_buffer *active; /* active screen buffer */
59 int recnum; /* number of input records */
60 INPUT_RECORD *records; /* input records */
61 struct console_input_events *evt; /* synchronization event with renderer */
62 WCHAR *title; /* console title */
63 data_size_t title_len; /* length of console title */
64 struct history_line **history; /* lines history */
65 int history_size; /* number of entries in history array */
66 int history_index; /* number of used entries in history array */
67 int history_mode; /* mode of history (non zero means remove doubled strings */
68 int edition_mode; /* index to edition mode flavors */
69 int input_cp; /* console input codepage */
70 int output_cp; /* console output codepage */
71 user_handle_t win; /* window handle if backend supports it */
72 struct event *event; /* event to wait on for input queue */
73 struct fd *fd; /* for bare console, attached input fd */
74 struct async_queue read_q; /* read queue */
77 static void console_input_dump( struct object *obj, int verbose );
78 static void console_input_destroy( struct object *obj );
79 static struct fd *console_input_get_fd( struct object *obj );
80 static struct object *console_input_open_file( struct object *obj, unsigned int access,
81 unsigned int sharing, unsigned int options );
83 static const struct object_ops console_input_ops =
85 sizeof(struct console_input), /* size */
86 console_input_dump, /* dump */
87 no_get_type, /* get_type */
88 no_add_queue, /* add_queue */
89 NULL, /* remove_queue */
90 NULL, /* signaled */
91 no_satisfied, /* satisfied */
92 no_signal, /* signal */
93 console_input_get_fd, /* get_fd */
94 default_fd_map_access, /* map_access */
95 default_get_sd, /* get_sd */
96 default_set_sd, /* set_sd */
97 no_lookup_name, /* lookup_name */
98 no_link_name, /* link_name */
99 NULL, /* unlink_name */
100 console_input_open_file, /* open_file */
101 no_kernel_obj_list, /* get_kernel_obj_list */
102 no_close_handle, /* close_handle */
103 console_input_destroy /* destroy */
106 static enum server_fd_type console_get_fd_type( struct fd *fd );
107 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
109 static const struct fd_ops console_input_fd_ops =
111 default_fd_get_poll_events, /* get_poll_events */
112 default_poll_event, /* poll_event */
113 console_get_fd_type, /* get_fd_type */
114 no_fd_read, /* read */
115 no_fd_write, /* write */
116 no_fd_flush, /* flush */
117 no_fd_get_file_info, /* get_file_info */
118 no_fd_get_volume_info, /* get_volume_info */
119 console_input_ioctl, /* ioctl */
120 default_fd_queue_async, /* queue_async */
121 default_fd_reselect_async /* reselect_async */
124 static void console_input_events_dump( struct object *obj, int verbose );
125 static void console_input_events_destroy( struct object *obj );
126 static struct fd *console_input_events_get_fd( struct object *obj );
127 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
128 unsigned int sharing, unsigned int options );
130 struct console_input_events
132 struct object obj; /* object header */
133 struct fd *fd; /* pseudo-fd for ioctls */
134 struct console_input *console; /* attached console */
135 int num_alloc; /* number of allocated events */
136 int num_used; /* number of actually used events */
137 struct condrv_renderer_event *events;
138 struct async_queue read_q; /* read queue */
141 static const struct object_ops console_input_events_ops =
143 sizeof(struct console_input_events), /* size */
144 console_input_events_dump, /* dump */
145 no_get_type, /* get_type */
146 add_queue, /* add_queue */
147 remove_queue, /* remove_queue */
148 NULL, /* signaled */
149 no_satisfied, /* satisfied */
150 no_signal, /* signal */
151 console_input_events_get_fd, /* get_fd */
152 default_fd_map_access, /* map_access */
153 default_get_sd, /* get_sd */
154 default_set_sd, /* set_sd */
155 no_lookup_name, /* lookup_name */
156 no_link_name, /* link_name */
157 NULL, /* unlink_name */
158 console_input_events_open_file, /* open_file */
159 no_kernel_obj_list, /* get_kernel_obj_list */
160 no_close_handle, /* close_handle */
161 console_input_events_destroy /* destroy */
164 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
166 static const struct fd_ops console_input_events_fd_ops =
168 default_fd_get_poll_events, /* get_poll_events */
169 default_poll_event, /* poll_event */
170 console_get_fd_type, /* get_fd_type */
171 no_fd_read, /* read */
172 no_fd_write, /* write */
173 no_fd_flush, /* flush */
174 no_fd_get_file_info, /* get_file_info */
175 no_fd_get_volume_info, /* get_volume_info */
176 console_input_events_ioctl, /* ioctl */
177 default_fd_queue_async, /* queue_async */
178 default_fd_reselect_async /* reselect_async */
181 struct font_info
183 short int width;
184 short int height;
185 short int weight;
186 short int pitch_family;
187 WCHAR *face_name;
188 data_size_t face_len;
191 struct screen_buffer
193 struct object obj; /* object header */
194 struct list entry; /* entry in list of all screen buffers */
195 struct console_input *input; /* associated console input */
196 unsigned int mode; /* output mode */
197 int cursor_size; /* size of cursor (percentage filled) */
198 int cursor_visible;/* cursor visibility flag */
199 int cursor_x; /* position of cursor */
200 int cursor_y; /* position of cursor */
201 int width; /* size (w-h) of the screen buffer */
202 int height;
203 int max_width; /* size (w-h) of the window given font size */
204 int max_height;
205 char_info_t *data; /* the data for each cell - a width x height matrix */
206 unsigned short attr; /* default fill attributes (screen colors) */
207 unsigned short popup_attr; /* pop-up color attributes */
208 unsigned int color_map[16]; /* color table */
209 rectangle_t win; /* current visible window on the screen buffer *
210 * as seen in wineconsole */
211 struct font_info font; /* console font information */
212 struct fd *fd; /* for bare console, attached output fd */
215 static void screen_buffer_dump( struct object *obj, int verbose );
216 static void screen_buffer_destroy( struct object *obj );
217 static struct fd *screen_buffer_get_fd( struct object *obj );
218 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
219 unsigned int sharing, unsigned int options );
221 static const struct object_ops screen_buffer_ops =
223 sizeof(struct screen_buffer), /* size */
224 screen_buffer_dump, /* dump */
225 no_get_type, /* get_type */
226 no_add_queue, /* add_queue */
227 NULL, /* remove_queue */
228 NULL, /* signaled */
229 NULL, /* satisfied */
230 no_signal, /* signal */
231 screen_buffer_get_fd, /* get_fd */
232 default_fd_map_access, /* map_access */
233 default_get_sd, /* get_sd */
234 default_set_sd, /* set_sd */
235 no_lookup_name, /* lookup_name */
236 no_link_name, /* link_name */
237 NULL, /* unlink_name */
238 screen_buffer_open_file, /* open_file */
239 no_kernel_obj_list, /* get_kernel_obj_list */
240 no_close_handle, /* close_handle */
241 screen_buffer_destroy /* destroy */
244 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
246 static const struct fd_ops screen_buffer_fd_ops =
248 default_fd_get_poll_events, /* get_poll_events */
249 default_poll_event, /* poll_event */
250 console_get_fd_type, /* get_fd_type */
251 no_fd_read, /* read */
252 no_fd_write, /* write */
253 no_fd_flush, /* flush */
254 no_fd_get_file_info, /* get_file_info */
255 no_fd_get_volume_info, /* get_volume_info */
256 screen_buffer_ioctl, /* ioctl */
257 default_fd_queue_async, /* queue_async */
258 default_fd_reselect_async /* reselect_async */
261 static struct object_type *console_device_get_type( struct object *obj );
262 static void console_device_dump( struct object *obj, int verbose );
263 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
264 static struct object *console_device_open_file( struct object *obj, unsigned int access,
265 unsigned int sharing, unsigned int options );
267 static const struct object_ops console_device_ops =
269 sizeof(struct object), /* size */
270 console_device_dump, /* dump */
271 console_device_get_type, /* get_type */
272 no_add_queue, /* add_queue */
273 NULL, /* remove_queue */
274 NULL, /* signaled */
275 no_satisfied, /* satisfied */
276 no_signal, /* signal */
277 no_get_fd, /* get_fd */
278 default_fd_map_access, /* map_access */
279 default_get_sd, /* get_sd */
280 default_set_sd, /* set_sd */
281 console_device_lookup_name, /* lookup_name */
282 directory_link_name, /* link_name */
283 default_unlink_name, /* unlink_name */
284 console_device_open_file, /* open_file */
285 no_kernel_obj_list, /* get_kernel_obj_list */
286 no_close_handle, /* close_handle */
287 no_destroy /* destroy */
290 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
292 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
294 static int console_input_is_bare( struct console_input* cin )
296 return cin->evt == NULL;
299 static struct fd *console_input_get_fd( struct object* obj )
301 struct console_input *console_input = (struct console_input*)obj;
302 assert( obj->ops == &console_input_ops );
303 return (struct fd *)grab_object( console_input->fd );
306 static enum server_fd_type console_get_fd_type( struct fd *fd )
308 return FD_TYPE_CHAR;
311 /* dumps the renderer events of a console */
312 static void console_input_events_dump( struct object *obj, int verbose )
314 struct console_input_events *evts = (struct console_input_events *)obj;
315 assert( obj->ops == &console_input_events_ops );
316 fprintf( stderr, "Console input events: %d/%d events\n",
317 evts->num_used, evts->num_alloc );
320 /* destroys the renderer events of a console */
321 static void console_input_events_destroy( struct object *obj )
323 struct console_input_events *evts = (struct console_input_events *)obj;
324 assert( obj->ops == &console_input_events_ops );
325 if (evts->console) evts->console->evt = NULL;
326 free_async_queue( &evts->read_q );
327 if (evts->fd) release_object( evts->fd );
328 free( evts->events );
331 static struct fd *console_input_events_get_fd( struct object* obj )
333 struct console_input_events *evts = (struct console_input_events*)obj;
334 assert( obj->ops == &console_input_events_ops );
335 return (struct fd*)grab_object( evts->fd );
338 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
339 unsigned int sharing, unsigned int options )
341 return grab_object( obj );
344 /* retrieves events from the console's renderer events list */
345 static int get_renderer_events( struct console_input_events* evts, struct async *async )
347 struct iosb *iosb = async_get_iosb( async );
348 data_size_t num;
350 num = min( iosb->out_size / sizeof(evts->events[0]), evts->num_used );
351 if (num && !(iosb->out_data = malloc( num * sizeof(evts->events[0] ))))
353 async_terminate( async, STATUS_NO_MEMORY );
354 release_object( iosb );
355 return 0;
358 iosb->status = STATUS_SUCCESS;
359 iosb->out_size = iosb->result = num * sizeof(evts->events[0]);
360 if (num) memcpy( iosb->out_data, evts->events, iosb->result );
361 release_object( iosb );
362 async_terminate( async, STATUS_ALERTED );
364 if (num && num < evts->num_used)
366 memmove( &evts->events[0], &evts->events[num],
367 (evts->num_used - num) * sizeof(evts->events[0]) );
369 evts->num_used -= num;
370 return 1;
373 /* add an event to the console's renderer events list */
374 static void console_input_events_append( struct console_input* console,
375 struct condrv_renderer_event* evt)
377 struct console_input_events* evts;
378 int collapsed = FALSE;
379 struct async *async;
381 if (!(evts = console->evt)) return;
382 /* to be done even when evt has been generated by the renderer ? */
384 /* try to collapse evt into current queue's events */
385 if (evts->num_used)
387 struct condrv_renderer_event* last = &evts->events[evts->num_used - 1];
389 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
390 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
392 /* if two update events overlap, collapse them into a single one */
393 if (last->u.update.bottom + 1 >= evt->u.update.top &&
394 evt->u.update.bottom + 1 >= last->u.update.top)
396 last->u.update.top = min(last->u.update.top, evt->u.update.top);
397 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
398 collapsed = TRUE;
402 if (!collapsed)
404 if (evts->num_used == evts->num_alloc)
406 evts->num_alloc += 16;
407 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
408 assert(evts->events);
410 evts->events[evts->num_used++] = *evt;
412 while (evts->num_used && (async = find_pending_async( &evts->read_q )))
414 get_renderer_events( evts, async );
415 release_object( async );
419 static struct object *create_console_input_events(void)
421 struct console_input_events* evt;
423 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
424 evt->console = NULL;
425 evt->num_alloc = evt->num_used = 0;
426 evt->events = NULL;
427 init_async_queue( &evt->read_q );
428 if (!(evt->fd = alloc_pseudo_fd( &console_input_events_fd_ops, &evt->obj, 0 )))
430 release_object( evt );
431 return NULL;
433 return &evt->obj;
436 static struct object *create_console_input( int fd )
438 struct console_input *console_input;
440 if (!(console_input = alloc_object( &console_input_ops )))
442 if (fd != -1) close( fd );
443 return NULL;
445 console_input->renderer = NULL;
446 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
447 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
448 ENABLE_EXTENDED_FLAGS;
449 console_input->num_proc = 0;
450 console_input->active = NULL;
451 console_input->recnum = 0;
452 console_input->records = NULL;
453 console_input->evt = NULL;
454 console_input->title = NULL;
455 console_input->title_len = 0;
456 console_input->history_size = 50;
457 console_input->history = calloc( console_input->history_size, sizeof(*console_input->history) );
458 console_input->history_index = 0;
459 console_input->history_mode = 0;
460 console_input->edition_mode = 0;
461 console_input->input_cp = 0;
462 console_input->output_cp = 0;
463 console_input->win = 0;
464 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
465 console_input->fd = NULL;
466 init_async_queue( &console_input->read_q );
468 if (!console_input->history || !console_input->event)
470 if (fd != -1) close( fd );
471 console_input->history_size = 0;
472 release_object( console_input );
473 return NULL;
475 if (fd != -1) /* bare console */
477 console_input->fd = create_anonymous_fd( &console_input_fd_ops, fd, &console_input->obj,
478 FILE_SYNCHRONOUS_IO_NONALERT );
480 else
482 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
483 FILE_SYNCHRONOUS_IO_NONALERT );
485 if (!console_input->fd)
487 release_object( console_input );
488 return NULL;
490 allow_fd_caching( console_input->fd );
491 return &console_input->obj;
494 static void generate_sb_initial_events( struct console_input *console_input )
496 struct screen_buffer *screen_buffer = console_input->active;
497 struct condrv_renderer_event evt;
499 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
500 evt.u.resize.width = screen_buffer->width;
501 evt.u.resize.height = screen_buffer->height;
502 console_input_events_append( console_input, &evt );
504 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
505 evt.u.display.left = screen_buffer->win.left;
506 evt.u.display.top = screen_buffer->win.top;
507 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
508 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
509 console_input_events_append( console_input, &evt );
511 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
512 evt.u.update.top = 0;
513 evt.u.update.bottom = screen_buffer->height - 1;
514 console_input_events_append( console_input, &evt );
516 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
517 evt.u.cursor_geom.size = screen_buffer->cursor_size;
518 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
519 console_input_events_append( console_input, &evt );
521 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
522 evt.u.cursor_pos.x = screen_buffer->cursor_x;
523 evt.u.cursor_pos.y = screen_buffer->cursor_y;
524 console_input_events_append( console_input, &evt );
527 static struct object *create_console_output( struct console_input *console_input, int fd )
529 struct screen_buffer *screen_buffer;
530 int i;
532 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
534 if (fd != -1) close( fd );
535 return NULL;
537 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
538 screen_buffer->input = console_input;
539 screen_buffer->cursor_size = 100;
540 screen_buffer->cursor_visible = 1;
541 screen_buffer->width = 80;
542 screen_buffer->height = 150;
543 screen_buffer->max_width = 80;
544 screen_buffer->max_height = 25;
545 screen_buffer->cursor_x = 0;
546 screen_buffer->cursor_y = 0;
547 screen_buffer->attr = 0x0F;
548 screen_buffer->popup_attr = 0xF5;
549 screen_buffer->win.left = 0;
550 screen_buffer->win.right = screen_buffer->max_width - 1;
551 screen_buffer->win.top = 0;
552 screen_buffer->win.bottom = screen_buffer->max_height - 1;
553 screen_buffer->data = NULL;
554 screen_buffer->font.width = 0;
555 screen_buffer->font.height = 0;
556 screen_buffer->font.weight = FW_NORMAL;
557 screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
558 screen_buffer->font.face_name = NULL;
559 screen_buffer->font.face_len = 0;
560 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
561 list_add_head( &screen_buffer_list, &screen_buffer->entry );
563 if (fd != -1)
564 screen_buffer->fd = create_anonymous_fd( &screen_buffer_fd_ops, fd, &screen_buffer->obj,
565 FILE_SYNCHRONOUS_IO_NONALERT );
566 else
567 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
568 FILE_SYNCHRONOUS_IO_NONALERT );
569 if (!screen_buffer->fd)
571 release_object( screen_buffer );
572 return NULL;
574 allow_fd_caching(screen_buffer->fd);
576 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
577 sizeof(*screen_buffer->data) )))
579 release_object( screen_buffer );
580 return NULL;
582 /* clear the first row */
583 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
584 /* and copy it to all other rows */
585 for (i = 1; i < screen_buffer->height; i++)
586 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
587 screen_buffer->width * sizeof(char_info_t) );
589 if (!console_input->active)
591 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
592 generate_sb_initial_events( console_input );
594 return &screen_buffer->obj;
597 /* free the console for this process */
598 int free_console( struct process *process )
600 struct console_input* console = process->console;
602 if (!console) return 0;
604 process->console = NULL;
605 if (--console->num_proc == 0 && console->renderer)
607 /* all processes have terminated... tell the renderer to terminate too */
608 struct condrv_renderer_event evt;
609 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
610 memset(&evt.u, 0, sizeof(evt.u));
611 console_input_events_append( console, &evt );
613 release_object( console );
615 return 1;
618 /* let process inherit the console from parent... this handle two cases :
619 * 1/ generic console inheritance
620 * 2/ parent is a renderer which launches process, and process should attach to the console
621 * rendered by parent
623 void inherit_console( struct thread *parent_thread, struct process *parent, struct process *process,
624 obj_handle_t hconin )
626 int done = 0;
628 /* if parent is a renderer, then attach current process to its console
629 * a bit hacky....
631 if (hconin && parent_thread)
633 struct console_input *console;
635 /* FIXME: should we check some access rights ? */
636 if ((console = (struct console_input *)get_handle_obj( parent, hconin,
637 0, &console_input_ops )))
639 if (console->renderer == parent_thread)
641 process->console = (struct console_input *)grab_object( console );
642 process->console->num_proc++;
643 done = 1;
645 release_object( console );
647 else clear_error(); /* ignore error */
649 /* otherwise, if parent has a console, attach child to this console */
650 if (!done && parent->console)
652 process->console = (struct console_input *)grab_object( parent->console );
653 process->console->num_proc++;
657 struct thread *console_get_renderer( struct console_input *console )
659 return console->renderer;
662 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
664 struct console_input* console = NULL;
666 if (handle)
667 console = (struct console_input *)get_handle_obj( current->process, handle,
668 access, &console_input_ops );
669 else if (current->process->console)
671 console = (struct console_input *)grab_object( current->process->console );
674 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
675 return console;
678 struct console_signal_info
680 struct console_input *console;
681 process_id_t group;
682 int signal;
685 static int propagate_console_signal_cb(struct process *process, void *user)
687 struct console_signal_info* csi = (struct console_signal_info*)user;
689 if (process->console == csi->console && process->running_threads &&
690 (!csi->group || process->group_id == csi->group))
692 /* find a suitable thread to signal */
693 struct thread *thread;
694 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
696 if (send_thread_signal( thread, csi->signal )) break;
699 return FALSE;
702 static void propagate_console_signal( struct console_input *console,
703 int sig, process_id_t group_id )
705 struct console_signal_info csi;
707 if (!console)
709 set_error( STATUS_INVALID_PARAMETER );
710 return;
712 /* FIXME: should support the other events (like CTRL_BREAK) */
713 if (sig != CTRL_C_EVENT)
715 set_error( STATUS_NOT_IMPLEMENTED );
716 return;
718 csi.console = console;
719 csi.signal = SIGINT;
720 csi.group = group_id;
722 enum_processes(propagate_console_signal_cb, &csi);
725 /* retrieve a pointer to the console input records */
726 static int read_console_input( struct console_input *console, struct async *async, int flush )
728 struct iosb *iosb = async_get_iosb( async );
729 data_size_t count;
731 count = min( iosb->out_size / sizeof(INPUT_RECORD), console->recnum );
732 if (count)
734 if (!(iosb->out_data = malloc( count * sizeof(INPUT_RECORD) )))
736 set_error( STATUS_NO_MEMORY );
737 release_object( iosb );
738 return 0;
740 iosb->out_size = iosb->result = count * sizeof(INPUT_RECORD);
741 memcpy( iosb->out_data, console->records, iosb->result );
742 iosb->status = STATUS_SUCCESS;
743 async_terminate( async, STATUS_ALERTED );
745 else
747 async_terminate( async, STATUS_SUCCESS );
750 release_object( iosb );
752 if (flush && count)
754 if (console->recnum > count)
756 INPUT_RECORD *new_rec;
757 memmove( console->records, console->records + count, (console->recnum - count) * sizeof(*console->records) );
758 console->recnum -= count;
759 new_rec = realloc( console->records, console->recnum * sizeof(*console->records) );
760 if (new_rec) console->records = new_rec;
762 else
764 console->recnum = 0;
765 free( console->records );
766 console->records = NULL;
767 reset_event( console->event );
771 return 1;
774 /* add input events to a console input queue */
775 static int write_console_input( struct console_input* console, int count,
776 const INPUT_RECORD *records )
778 INPUT_RECORD *new_rec;
779 struct async *async;
781 if (!count) return 1;
782 if (!(new_rec = realloc( console->records,
783 (console->recnum + count) * sizeof(INPUT_RECORD) )))
785 set_error( STATUS_NO_MEMORY );
786 return 0;
788 console->records = new_rec;
789 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
791 if (console->mode & ENABLE_PROCESSED_INPUT)
793 int i = 0;
794 while (i < count)
796 if (records[i].EventType == KEY_EVENT &&
797 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
798 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
800 if (i != count - 1)
801 memcpy( &console->records[console->recnum + i],
802 &console->records[console->recnum + i + 1],
803 (count - i - 1) * sizeof(INPUT_RECORD) );
804 count--;
805 if (records[i].Event.KeyEvent.bKeyDown)
807 /* send SIGINT to all processes attached to this console */
808 propagate_console_signal( console, CTRL_C_EVENT, 0 );
811 else i++;
814 console->recnum += count;
815 while (console->recnum && (async = find_pending_async( &console->read_q )))
817 read_console_input( console, async, 1 );
818 release_object( async );
820 if (console->recnum) set_event( console->event );
821 return 1;
824 /* set misc console input information */
825 static int set_console_input_info( const struct set_console_input_info_request *req,
826 const WCHAR *title, data_size_t len )
828 struct console_input *console;
829 struct condrv_renderer_event evt;
831 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
832 if (console_input_is_bare(console) && (req->mask & SET_CONSOLE_INPUT_INFO_WIN))
834 set_error( STATUS_UNSUCCESSFUL );
835 goto error;
838 memset(&evt.u, 0, sizeof(evt.u));
839 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
841 WCHAR *new_title = NULL;
843 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
844 if (len && !(new_title = memdup( title, len ))) goto error;
845 free( console->title );
846 console->title = new_title;
847 console->title_len = len;
848 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
849 console_input_events_append( console, &evt );
851 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
853 console->input_cp = req->input_cp;
855 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
857 console->output_cp = req->output_cp;
859 release_object( console );
860 return 1;
861 error:
862 if (console) release_object( console );
863 return 0;
866 /* resize a screen buffer */
867 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
868 int new_width, int new_height )
870 int i, old_width, old_height, copy_width, copy_height;
871 char_info_t *new_data;
873 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
875 set_error( STATUS_NO_MEMORY );
876 return 0;
878 old_width = screen_buffer->width;
879 old_height = screen_buffer->height;
880 copy_width = min( old_width, new_width );
881 copy_height = min( old_height, new_height );
883 /* copy all the rows */
884 for (i = 0; i < copy_height; i++)
886 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
887 copy_width * sizeof(char_info_t) );
890 /* clear the end of each row */
891 if (new_width > old_width)
893 /* fill first row */
894 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
895 /* and blast it to the other rows */
896 for (i = 1; i < copy_height; i++)
897 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
898 (new_width - old_width) * sizeof(char_info_t) );
901 /* clear remaining rows */
902 if (new_height > old_height)
904 /* fill first row */
905 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
906 /* and blast it to the other rows */
907 for (i = old_height+1; i < new_height; i++)
908 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
909 new_width * sizeof(char_info_t) );
911 free( screen_buffer->data );
912 screen_buffer->data = new_data;
913 screen_buffer->width = new_width;
914 screen_buffer->height = new_height;
915 return 1;
918 static int set_output_info( struct screen_buffer *screen_buffer,
919 const struct condrv_output_info_params *params, data_size_t extra_size )
921 const struct condrv_output_info *info = &params->info;
922 struct condrv_renderer_event evt;
923 WCHAR *font_name;
925 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
927 if (info->cursor_size < 1 || info->cursor_size > 100)
929 set_error( STATUS_INVALID_PARAMETER );
930 return 0;
932 if (screen_buffer->cursor_size != info->cursor_size ||
933 screen_buffer->cursor_visible != info->cursor_visible)
935 screen_buffer->cursor_size = info->cursor_size;
936 screen_buffer->cursor_visible = info->cursor_visible;
937 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
938 memset( &evt.u, 0, sizeof(evt.u) );
939 evt.u.cursor_geom.size = info->cursor_size;
940 evt.u.cursor_geom.visible = info->cursor_visible;
941 console_input_events_append( screen_buffer->input, &evt );
944 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
946 if (info->cursor_x < 0 || info->cursor_x >= screen_buffer->width ||
947 info->cursor_y < 0 || info->cursor_y >= screen_buffer->height)
949 set_error( STATUS_INVALID_PARAMETER );
950 return 0;
952 if (screen_buffer->cursor_x != info->cursor_x || screen_buffer->cursor_y != info->cursor_y)
954 screen_buffer->cursor_x = info->cursor_x;
955 screen_buffer->cursor_y = info->cursor_y;
956 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
957 memset( &evt.u, 0, sizeof(evt.u) );
958 evt.u.cursor_pos.x = info->cursor_x;
959 evt.u.cursor_pos.y = info->cursor_y;
960 console_input_events_append( screen_buffer->input, &evt );
963 if (params->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
965 unsigned cc;
967 /* new screen-buffer cannot be smaller than actual window */
968 if (info->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
969 info->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
971 set_error( STATUS_INVALID_PARAMETER );
972 return 0;
974 /* FIXME: there are also some basic minimum and max size to deal with */
975 if (!change_screen_buffer_size( screen_buffer, info->width, info->height )) return 0;
977 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
978 memset( &evt.u, 0, sizeof(evt.u) );
979 evt.u.resize.width = info->width;
980 evt.u.resize.height = info->height;
981 console_input_events_append( screen_buffer->input, &evt );
983 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
984 memset( &evt.u, 0, sizeof(evt.u) );
985 evt.u.update.top = 0;
986 evt.u.update.bottom = screen_buffer->height - 1;
987 console_input_events_append( screen_buffer->input, &evt );
989 /* scroll window to display sb */
990 if (screen_buffer->win.right >= info->width)
992 screen_buffer->win.right -= screen_buffer->win.left;
993 screen_buffer->win.left = 0;
995 if (screen_buffer->win.bottom >= info->height)
997 screen_buffer->win.bottom -= screen_buffer->win.top;
998 screen_buffer->win.top = 0;
1000 /* reset cursor if needed (normally, if cursor was outside of new sb, the
1001 * window has been shifted so that the new position of the cursor will be
1002 * visible */
1003 cc = 0;
1004 if (screen_buffer->cursor_x >= info->width)
1006 screen_buffer->cursor_x = info->width - 1;
1007 cc++;
1009 if (screen_buffer->cursor_y >= info->height)
1011 screen_buffer->cursor_y = info->height - 1;
1012 cc++;
1014 if (cc)
1016 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1017 memset( &evt.u, 0, sizeof(evt.u) );
1018 evt.u.cursor_pos.x = info->cursor_x;
1019 evt.u.cursor_pos.y = info->cursor_y;
1020 console_input_events_append( screen_buffer->input, &evt );
1023 if (screen_buffer == screen_buffer->input->active &&
1024 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
1026 INPUT_RECORD ir;
1027 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
1028 ir.Event.WindowBufferSizeEvent.dwSize.X = info->width;
1029 ir.Event.WindowBufferSizeEvent.dwSize.Y = info->height;
1030 write_console_input( screen_buffer->input, 1, &ir );
1033 if (params->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1035 screen_buffer->attr = info->attr;
1037 if (params->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1039 screen_buffer->popup_attr = info->popup_attr;
1041 if (params->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1043 if (info->win_left < 0 || info->win_left > info->win_right ||
1044 info->win_right >= screen_buffer->width ||
1045 info->win_top < 0 || info->win_top > info->win_bottom ||
1046 info->win_bottom >= screen_buffer->height)
1048 set_error( STATUS_INVALID_PARAMETER );
1049 return 0;
1051 if (screen_buffer->win.left != info->win_left || screen_buffer->win.top != info->win_top ||
1052 screen_buffer->win.right != info->win_right || screen_buffer->win.bottom != info->win_bottom)
1054 screen_buffer->win.left = info->win_left;
1055 screen_buffer->win.top = info->win_top;
1056 screen_buffer->win.right = info->win_right;
1057 screen_buffer->win.bottom = info->win_bottom;
1058 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1059 memset( &evt.u, 0, sizeof(evt.u) );
1060 evt.u.display.left = info->win_left;
1061 evt.u.display.top = info->win_top;
1062 evt.u.display.width = info->win_right - info->win_left + 1;
1063 evt.u.display.height = info->win_bottom - info->win_top + 1;
1064 console_input_events_append( screen_buffer->input, &evt );
1067 if (params->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1069 screen_buffer->max_width = info->max_width;
1070 screen_buffer->max_height = info->max_height;
1072 if (params->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1074 memcpy( screen_buffer->color_map, info->color_map, sizeof(info->color_map) );
1076 if (params->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1078 screen_buffer->font.width = info->font_width;
1079 screen_buffer->font.height = info->font_height;
1080 screen_buffer->font.weight = info->font_weight;
1081 screen_buffer->font.pitch_family = info->font_pitch_family;
1082 if (extra_size)
1084 extra_size = extra_size / sizeof(WCHAR) * sizeof(WCHAR);
1085 font_name = mem_alloc( extra_size );
1086 if (font_name)
1088 memcpy( font_name, info + 1, extra_size );
1089 free( screen_buffer->font.face_name );
1090 screen_buffer->font.face_name = font_name;
1091 screen_buffer->font.face_len = extra_size;
1096 return 1;
1099 /* appends a new line to history (history is a fixed size array) */
1100 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1102 struct history_line *ptr;
1104 if (!console || !console->history_size)
1106 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1107 return;
1110 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
1111 if (console->history_mode && console->history_index &&
1112 console->history[console->history_index - 1]->len == len &&
1113 !memcmp( console->history[console->history_index - 1]->text, buf, len ))
1115 /* don't duplicate entry */
1116 set_error( STATUS_ALIAS_EXISTS );
1117 return;
1119 if (!(ptr = mem_alloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
1120 ptr->len = len;
1121 memcpy( ptr->text, buf, len );
1123 if (console->history_index < console->history_size)
1125 console->history[console->history_index++] = ptr;
1127 else
1129 free( console->history[0]) ;
1130 memmove( &console->history[0], &console->history[1],
1131 (console->history_size - 1) * sizeof(*console->history) );
1132 console->history[console->history_size - 1] = ptr;
1136 /* returns a line from the cache */
1137 static data_size_t console_input_get_hist( struct console_input *console, int index )
1139 data_size_t ret = 0;
1141 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1142 else
1144 ret = console->history[index]->len;
1145 set_reply_data( console->history[index]->text, min( ret, get_reply_max_size() ));
1147 return ret;
1150 /* dumb dump */
1151 static void console_input_dump( struct object *obj, int verbose )
1153 struct console_input *console = (struct console_input *)obj;
1154 assert( obj->ops == &console_input_ops );
1155 fprintf( stderr, "Console input active=%p evt=%p\n",
1156 console->active, console->evt );
1159 static void console_input_destroy( struct object *obj )
1161 struct console_input* console_in = (struct console_input *)obj;
1162 struct screen_buffer* curr;
1163 int i;
1165 assert( obj->ops == &console_input_ops );
1166 free( console_in->title );
1167 free( console_in->records );
1169 if (console_in->active) release_object( console_in->active );
1170 console_in->active = NULL;
1172 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1174 if (curr->input == console_in) curr->input = NULL;
1177 free_async_queue( &console_in->read_q );
1178 if (console_in->evt)
1179 console_in->evt->console = NULL;
1180 if (console_in->event)
1181 release_object( console_in->event );
1182 if (console_in->fd)
1183 release_object( console_in->fd );
1185 for (i = 0; i < console_in->history_size; i++)
1186 free( console_in->history[i] );
1187 free( console_in->history );
1190 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1191 unsigned int sharing, unsigned int options )
1193 return grab_object( obj );
1196 static void screen_buffer_dump( struct object *obj, int verbose )
1198 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1199 assert( obj->ops == &screen_buffer_ops );
1201 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1204 static void screen_buffer_destroy( struct object *obj )
1206 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1208 assert( obj->ops == &screen_buffer_ops );
1210 list_remove( &screen_buffer->entry );
1212 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1214 struct screen_buffer *sb;
1216 screen_buffer->input->active = NULL;
1217 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1219 if (sb->input == screen_buffer->input)
1221 sb->input->active = sb;
1222 break;
1226 if (screen_buffer->fd) release_object( screen_buffer->fd );
1227 free( screen_buffer->data );
1228 free( screen_buffer->font.face_name );
1231 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
1232 unsigned int sharing, unsigned int options )
1234 return grab_object( obj );
1237 static struct fd *screen_buffer_get_fd( struct object *obj )
1239 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1240 assert( obj->ops == &screen_buffer_ops );
1241 if (screen_buffer->fd)
1242 return (struct fd*)grab_object( screen_buffer->fd );
1243 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1244 return NULL;
1247 /* read data from a screen buffer */
1248 static void read_console_output( struct screen_buffer *screen_buffer, unsigned int x, unsigned int y,
1249 enum char_info_mode mode, unsigned int width )
1251 unsigned int i, count;
1252 char_info_t *src;
1254 if (x >= screen_buffer->width || y >= screen_buffer->height)
1256 if (width) set_error( STATUS_INVALID_PARAMETER );
1257 return;
1259 src = screen_buffer->data + y * screen_buffer->width + x;
1261 switch(mode)
1263 case CHAR_INFO_MODE_TEXT:
1265 WCHAR *data;
1266 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1267 get_reply_max_size() / sizeof(*data) );
1268 if ((data = set_reply_data_size( count * sizeof(*data) )))
1270 for (i = 0; i < count; i++) data[i] = src[i].ch;
1273 break;
1274 case CHAR_INFO_MODE_ATTR:
1276 unsigned short *data;
1277 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1278 get_reply_max_size() / sizeof(*data) );
1279 if ((data = set_reply_data_size( count * sizeof(*data) )))
1281 for (i = 0; i < count; i++) data[i] = src[i].attr;
1284 break;
1285 case CHAR_INFO_MODE_TEXTATTR:
1287 char_info_t *data;
1288 SMALL_RECT *region;
1289 if (!width || get_reply_max_size() < sizeof(*region))
1291 set_error( STATUS_INVALID_PARAMETER );
1292 return;
1294 count = min( (get_reply_max_size() - sizeof(*region)) / (width * sizeof(*data)), screen_buffer->height - y );
1295 width = min( width, screen_buffer->width - x );
1296 if (!(region = set_reply_data_size( sizeof(*region) + width * count * sizeof(*data) ))) return;
1297 region->Left = x;
1298 region->Top = y;
1299 region->Right = x + width - 1;
1300 region->Bottom = y + count - 1;
1301 data = (char_info_t *)(region + 1);
1302 for (i = 0; i < count; i++)
1304 memcpy( &data[i * width], &src[i * screen_buffer->width], width * sizeof(*data) );
1307 break;
1308 default:
1309 set_error( STATUS_INVALID_PARAMETER );
1310 break;
1314 /* write data into a screen buffer */
1315 static void write_console_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
1316 data_size_t size )
1318 unsigned int i, entry_size, entry_cnt, x, y;
1319 char_info_t *dest;
1320 char *src;
1322 entry_size = params->mode == CHAR_INFO_MODE_TEXTATTR ? sizeof(char_info_t) : sizeof(WCHAR);
1323 if (size % entry_size)
1325 set_error( STATUS_INVALID_PARAMETER );
1326 return;
1328 if (params->x >= screen_buffer->width) return;
1329 entry_cnt = size / entry_size;
1331 for (i = 0, src = (char *)(params + 1); i < entry_cnt; i++, src += entry_size)
1333 if (params->width)
1335 x = params->x + i % params->width;
1336 y = params->y + i / params->width;
1337 if (x >= screen_buffer->width) continue;
1339 else
1341 x = (params->x + i) % screen_buffer->width;
1342 y = params->y + (params->x + i) / screen_buffer->width;
1344 if (y >= screen_buffer->height) break;
1346 dest = &screen_buffer->data[y * screen_buffer->width + x];
1347 switch(params->mode)
1349 case CHAR_INFO_MODE_TEXT:
1350 dest->ch = *(const WCHAR *)src;
1351 break;
1352 case CHAR_INFO_MODE_ATTR:
1353 dest->attr = *(const unsigned short *)src;
1354 break;
1355 case CHAR_INFO_MODE_TEXTATTR:
1356 *dest = *(const char_info_t *)src;
1357 break;
1358 case CHAR_INFO_MODE_TEXTSTDATTR:
1359 dest->ch = *(const WCHAR *)src;
1360 dest->attr = screen_buffer->attr;
1361 break;
1362 default:
1363 set_error( STATUS_INVALID_PARAMETER );
1364 return;
1368 if (i && screen_buffer == screen_buffer->input->active)
1370 struct condrv_renderer_event evt;
1371 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1372 memset(&evt.u, 0, sizeof(evt.u));
1373 evt.u.update.top = params->y;
1374 evt.u.update.bottom = params->width
1375 ? min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1
1376 : params->y + (params->x + i - 1) / screen_buffer->width;
1377 console_input_events_append( screen_buffer->input, &evt );
1380 if (get_reply_max_size() == sizeof(SMALL_RECT))
1382 SMALL_RECT region;
1383 region.Left = params->x;
1384 region.Top = params->y;
1385 region.Right = min( params->x + params->width, screen_buffer->width ) - 1;
1386 region.Bottom = min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1;
1387 set_reply_data( &region, sizeof(region) );
1389 else set_reply_data( &i, sizeof(i) );
1392 /* fill a screen buffer with uniform data */
1393 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1394 enum char_info_mode mode, int x, int y, int count, int wrap )
1396 int i;
1397 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1399 if (y >= screen_buffer->height) return 0;
1401 if (wrap)
1402 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1403 else
1404 end = screen_buffer->data + (y+1) * screen_buffer->width;
1406 if (count > end - dest) count = end - dest;
1408 switch(mode)
1410 case CHAR_INFO_MODE_TEXT:
1411 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1412 break;
1413 case CHAR_INFO_MODE_ATTR:
1414 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1415 break;
1416 case CHAR_INFO_MODE_TEXTATTR:
1417 for (i = 0; i < count; i++) dest[i] = data;
1418 break;
1419 case CHAR_INFO_MODE_TEXTSTDATTR:
1420 for (i = 0; i < count; i++)
1422 dest[i].ch = data.ch;
1423 dest[i].attr = screen_buffer->attr;
1425 break;
1426 default:
1427 set_error( STATUS_INVALID_PARAMETER );
1428 return 0;
1431 if (count && screen_buffer == screen_buffer->input->active)
1433 struct condrv_renderer_event evt;
1434 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1435 memset(&evt.u, 0, sizeof(evt.u));
1436 evt.u.update.top = y;
1437 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1438 console_input_events_append( screen_buffer->input, &evt );
1440 return i;
1443 /* scroll parts of a screen buffer */
1444 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1445 int w, int h, const rectangle_t *clip, char_info_t fill )
1447 struct condrv_renderer_event evt;
1448 rectangle_t src, dst;
1449 int x, y;
1451 src.left = max( xsrc, clip->left );
1452 src.top = max( ysrc, clip->top );
1453 src.right = min( xsrc + w - 1, clip->right );
1454 src.bottom = min( ysrc + h - 1, clip->bottom );
1456 dst.left = xdst;
1457 dst.top = ydst;
1458 dst.right = xdst + w - 1;
1459 dst.bottom = ydst + h - 1;
1461 if (dst.left < clip->left)
1463 xsrc += clip->left - dst.left;
1464 w -= clip->left - dst.left;
1465 dst.left = clip->left;
1467 if (dst.top < clip->top)
1469 ysrc += clip->top - dst.top;
1470 h -= clip->top - dst.top;
1471 dst.top = clip->top;
1473 if (dst.right > clip->right) w -= dst.right - clip->right;
1474 if (dst.bottom > clip->bottom) h -= dst.bottom - clip->bottom;
1476 if (w > 0 && h > 0)
1478 if (ysrc < ydst)
1480 for (y = h; y > 0; y--)
1482 memcpy( &screen_buffer->data[(dst.top + y - 1) * screen_buffer->width + dst.left],
1483 &screen_buffer->data[(ysrc + y - 1) * screen_buffer->width + xsrc],
1484 w * sizeof(screen_buffer->data[0]) );
1487 else
1489 for (y = 0; y < h; y++)
1491 /* we use memmove here because when psrc and pdst are the same,
1492 * copies are done on the same row, so the dst and src blocks
1493 * can overlap */
1494 memmove( &screen_buffer->data[(dst.top + y) * screen_buffer->width + dst.left],
1495 &screen_buffer->data[(ysrc + y) * screen_buffer->width + xsrc],
1496 w * sizeof(screen_buffer->data[0]) );
1501 for (y = src.top; y <= src.bottom; y++)
1503 int left = src.left;
1504 int right = src.right;
1505 if (dst.top <= y && y <= dst.bottom)
1507 if (dst.left <= src.left) left = max( left, dst.right + 1 );
1508 if (dst.left >= src.left) right = min( right, dst.left - 1 );
1510 for (x = left; x <= right; x++) screen_buffer->data[y * screen_buffer->width + x] = fill;
1513 /* FIXME: this could be enhanced, by signalling scroll */
1514 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1515 memset(&evt.u, 0, sizeof(evt.u));
1516 evt.u.update.top = min( src.top, dst.top );
1517 evt.u.update.bottom = max( src.bottom, dst.bottom );
1518 console_input_events_append( screen_buffer->input, &evt );
1521 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1523 struct console_input *console = get_fd_user( fd );
1525 switch (code)
1527 case IOCTL_CONDRV_GET_MODE:
1528 if (get_reply_max_size() != sizeof(console->mode))
1530 set_error( STATUS_INVALID_PARAMETER );
1531 return 0;
1533 return set_reply_data( &console->mode, sizeof(console->mode) ) != NULL;
1535 case IOCTL_CONDRV_SET_MODE:
1536 if (get_req_data_size() != sizeof(console->mode))
1538 set_error( STATUS_INVALID_PARAMETER );
1539 return 0;
1541 console->mode = *(unsigned int *)get_req_data();
1542 return 1;
1544 case IOCTL_CONDRV_READ_INPUT:
1546 int blocking = 0;
1547 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1549 set_error( STATUS_INVALID_PARAMETER );
1550 return 0;
1552 if (get_req_data_size())
1554 if (get_req_data_size() != sizeof(int))
1556 set_error( STATUS_INVALID_PARAMETER );
1557 return 0;
1559 blocking = *(int *)get_req_data();
1561 set_error( STATUS_PENDING );
1562 if (blocking && !console->recnum)
1564 queue_async( &console->read_q, async );
1565 return 1;
1567 return read_console_input( console, async, 1 );
1570 case IOCTL_CONDRV_WRITE_INPUT:
1571 return write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD), get_req_data() );
1573 case IOCTL_CONDRV_PEEK:
1574 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1576 set_error( STATUS_INVALID_PARAMETER );
1577 return 0;
1579 set_error( STATUS_PENDING );
1580 return read_console_input( console, async, 0 );
1582 case IOCTL_CONDRV_GET_INPUT_INFO:
1584 struct condrv_input_info info;
1585 if (get_reply_max_size() != sizeof(info))
1587 set_error( STATUS_INVALID_PARAMETER );
1588 return 0;
1590 info.input_cp = console->input_cp;
1591 info.output_cp = console->output_cp;
1592 info.history_mode = console->history_mode;
1593 info.history_size = console->history_size;
1594 info.edition_mode = console->edition_mode;
1595 info.input_count = console->recnum;
1596 info.win = console->win;
1597 return set_reply_data( &info, sizeof(info) ) != NULL;
1600 case IOCTL_CONDRV_SET_INPUT_INFO:
1602 const struct condrv_input_info_params *params = get_req_data();
1603 if (get_req_data_size() != sizeof(*params))
1605 set_error( STATUS_INVALID_PARAMETER );
1606 return 0;
1608 if (params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
1610 console->history_mode = params->info.history_mode;
1612 if ((params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
1613 console->history_size != params->info.history_size)
1615 struct history_line **mem = NULL;
1616 int i, delta;
1618 if (params->info.history_size)
1620 if (!(mem = mem_alloc( params->info.history_size * sizeof(*mem) ))) return 0;
1621 memset( mem, 0, params->info.history_size * sizeof(*mem) );
1624 delta = (console->history_index > params->info.history_size) ?
1625 (console->history_index - params->info.history_size) : 0;
1627 for (i = delta; i < console->history_index; i++)
1629 mem[i - delta] = console->history[i];
1630 console->history[i] = NULL;
1632 console->history_index -= delta;
1634 for (i = 0; i < console->history_size; i++)
1635 free( console->history[i] );
1636 free( console->history );
1637 console->history = mem;
1638 console->history_size = params->info.history_size;
1640 if (params->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
1642 console->edition_mode = params->info.edition_mode;
1644 if (params->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
1646 console->input_cp = params->info.input_cp;
1648 if (params->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
1650 console->output_cp = params->info.output_cp;
1652 if (params->mask & SET_CONSOLE_INPUT_INFO_WIN)
1654 console->win = params->info.win;
1656 return 1;
1659 case IOCTL_CONDRV_GET_TITLE:
1660 if (!console->title_len) return 1;
1661 return set_reply_data( console->title, min( console->title_len, get_reply_max_size() )) != NULL;
1663 case IOCTL_CONDRV_SET_TITLE:
1665 data_size_t len = get_req_data_size();
1666 struct condrv_renderer_event evt;
1667 WCHAR *title = NULL;
1669 if (len % sizeof(WCHAR))
1671 set_error( STATUS_INVALID_PARAMETER );
1672 return 0;
1675 if (len && !(title = memdup( get_req_data(), len ))) return 0;
1676 free( console->title );
1677 console->title = title;
1678 console->title_len = len;
1679 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
1680 console_input_events_append( console, &evt );
1681 return 1;
1684 default:
1685 set_error( STATUS_INVALID_HANDLE );
1686 return 0;
1690 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1692 struct screen_buffer *screen_buffer = get_fd_user( fd );
1694 switch (code)
1696 case IOCTL_CONDRV_GET_MODE:
1697 if (get_reply_max_size() != sizeof(screen_buffer->mode))
1699 set_error( STATUS_INVALID_PARAMETER );
1700 return 0;
1702 return set_reply_data( &screen_buffer->mode, sizeof(screen_buffer->mode) ) != NULL;
1704 case IOCTL_CONDRV_SET_MODE:
1705 if (get_req_data_size() != sizeof(screen_buffer->mode))
1707 set_error( STATUS_INVALID_PARAMETER );
1708 return 0;
1710 screen_buffer->mode = *(unsigned int *)get_req_data();
1711 return 1;
1713 case IOCTL_CONDRV_READ_OUTPUT:
1715 const struct condrv_output_params *params = get_req_data();
1716 if (get_req_data_size() != sizeof(*params))
1718 set_error( STATUS_INVALID_PARAMETER );
1719 return 0;
1721 if (console_input_is_bare( screen_buffer->input ))
1723 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1724 return 0;
1726 read_console_output( screen_buffer, params->x, params->y, params->mode, params->width );
1727 return !get_error();
1730 case IOCTL_CONDRV_WRITE_OUTPUT:
1731 if (get_req_data_size() < sizeof(struct condrv_output_params) ||
1732 (get_reply_max_size() != sizeof(SMALL_RECT) && get_reply_max_size() != sizeof(unsigned int)))
1734 set_error( STATUS_INVALID_PARAMETER );
1735 return 0;
1737 if (console_input_is_bare( screen_buffer->input ))
1739 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1740 return 0;
1742 write_console_output( screen_buffer, get_req_data(), get_req_data_size() - sizeof(struct condrv_output_params) );
1743 return !get_error();
1745 case IOCTL_CONDRV_GET_OUTPUT_INFO:
1747 struct condrv_output_info *info;
1748 data_size_t size;
1750 size = min( sizeof(*info) + screen_buffer->font.face_len, get_reply_max_size() );
1751 if (size < sizeof(*info))
1753 set_error( STATUS_INVALID_PARAMETER );
1754 return 0;
1756 if (!(info = set_reply_data_size( size ))) return 0;
1758 info->cursor_size = screen_buffer->cursor_size;
1759 info->cursor_visible = screen_buffer->cursor_visible;
1760 info->cursor_x = screen_buffer->cursor_x;
1761 info->cursor_y = screen_buffer->cursor_y;
1762 info->width = screen_buffer->width;
1763 info->height = screen_buffer->height;
1764 info->attr = screen_buffer->attr;
1765 info->popup_attr = screen_buffer->popup_attr;
1766 info->win_left = screen_buffer->win.left;
1767 info->win_top = screen_buffer->win.top;
1768 info->win_right = screen_buffer->win.right;
1769 info->win_bottom = screen_buffer->win.bottom;
1770 info->max_width = screen_buffer->max_width;
1771 info->max_height = screen_buffer->max_height;
1772 info->font_width = screen_buffer->font.width;
1773 info->font_height = screen_buffer->font.height;
1774 info->font_weight = screen_buffer->font.weight;
1775 info->font_pitch_family = screen_buffer->font.pitch_family;
1776 memcpy( info->color_map, screen_buffer->color_map, sizeof(info->color_map) );
1777 size -= sizeof(*info);
1778 if (size) memcpy( info + 1, screen_buffer->font.face_name, size );
1779 return 1;
1782 case IOCTL_CONDRV_SET_OUTPUT_INFO:
1784 const struct condrv_output_info_params *params = get_req_data();
1785 if (get_req_data_size() < sizeof(*params))
1787 set_error( STATUS_INVALID_PARAMETER );
1788 return 0;
1790 if (!screen_buffer->input)
1792 set_error( STATUS_INVALID_HANDLE );
1793 return 0;
1795 return set_output_info( screen_buffer, params, get_req_data_size() - sizeof(*params) );
1798 case IOCTL_CONDRV_ACTIVATE:
1799 if (!screen_buffer->input)
1801 set_error( STATUS_INVALID_HANDLE );
1802 return 0;
1805 if (screen_buffer != screen_buffer->input->active)
1807 if (screen_buffer->input->active) release_object( screen_buffer->input->active );
1808 screen_buffer->input->active = (struct screen_buffer *)grab_object( screen_buffer );
1809 generate_sb_initial_events( screen_buffer->input );
1811 return 1;
1813 case IOCTL_CONDRV_FILL_OUTPUT:
1815 const struct condrv_fill_output_params *params = get_req_data();
1816 char_info_t data;
1817 DWORD written;
1818 if (get_req_data_size() != sizeof(*params) ||
1819 (get_reply_max_size() && get_reply_max_size() != sizeof(written)))
1821 set_error( STATUS_INVALID_PARAMETER );
1822 return 0;
1824 data.ch = params->ch;
1825 data.attr = params->attr;
1826 written = fill_console_output( screen_buffer, data, params->mode,
1827 params->x, params->y, params->count, params->wrap );
1828 if (written && get_reply_max_size() == sizeof(written))
1829 set_reply_data( &written, sizeof(written) );
1830 return !get_error();
1833 case IOCTL_CONDRV_SCROLL:
1835 const struct condrv_scroll_params *params = get_req_data();
1836 rectangle_t clip;
1837 if (get_req_data_size() != sizeof(*params))
1839 set_error( STATUS_INVALID_PARAMETER );
1840 return 0;
1842 if (console_input_is_bare( screen_buffer->input ) || !screen_buffer->input)
1844 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1845 return 0;
1847 clip.left = max( params->clip.Left, 0 );
1848 clip.top = max( params->clip.Top, 0 );
1849 clip.right = min( params->clip.Right, screen_buffer->width - 1 );
1850 clip.bottom = min( params->clip.Bottom, screen_buffer->height - 1 );
1851 if (clip.left > clip.right || clip.top > clip.bottom || params->scroll.Left < 0 || params->scroll.Top < 0 ||
1852 params->scroll.Right >= screen_buffer->width || params->scroll.Bottom >= screen_buffer->height ||
1853 params->scroll.Right < params->scroll.Left || params->scroll.Top > params->scroll.Bottom ||
1854 params->origin.X < 0 || params->origin.X >= screen_buffer->width || params->origin.Y < 0 ||
1855 params->origin.Y >= screen_buffer->height)
1857 set_error( STATUS_INVALID_PARAMETER );
1858 return 0;
1861 scroll_console_output( screen_buffer, params->scroll.Left, params->scroll.Top, params->origin.X, params->origin.Y,
1862 params->scroll.Right - params->scroll.Left + 1, params->scroll.Bottom - params->scroll.Top + 1,
1863 &clip, params->fill );
1864 return !get_error();
1867 default:
1868 set_error( STATUS_INVALID_HANDLE );
1869 return 0;
1873 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1875 struct console_input_events *evts = get_fd_user( fd );
1877 switch (code)
1879 case IOCTL_CONDRV_GET_RENDERER_EVENTS:
1880 set_error( STATUS_PENDING );
1881 if (evts->num_used) return get_renderer_events( evts, async );
1882 queue_async( &evts->read_q, async );
1883 return 1;
1885 case IOCTL_CONDRV_ATTACH_RENDERER:
1887 struct console_input *console_input;
1888 if (get_req_data_size() != sizeof(condrv_handle_t))
1890 set_error( STATUS_INVALID_PARAMETER );
1891 return 0;
1893 console_input = (struct console_input *)get_handle_obj( current->process, *(condrv_handle_t *)get_req_data(),
1894 0, &console_input_ops );
1895 if (!console_input) return 0;
1897 if (!console_input->evt && !evts->console)
1899 console_input->evt = evts;
1900 console_input->renderer = current;
1901 evts->console = console_input;
1903 else set_error( STATUS_INVALID_HANDLE );
1905 release_object( console_input );
1906 return !get_error();
1909 case IOCTL_CONDRV_SCROLL:
1910 case IOCTL_CONDRV_SET_MODE:
1911 case IOCTL_CONDRV_WRITE_OUTPUT:
1912 case IOCTL_CONDRV_READ_OUTPUT:
1913 case IOCTL_CONDRV_FILL_OUTPUT:
1914 case IOCTL_CONDRV_GET_OUTPUT_INFO:
1915 case IOCTL_CONDRV_SET_OUTPUT_INFO:
1916 if (!evts->console || !evts->console->active)
1918 set_error( STATUS_INVALID_HANDLE );
1919 return 0;
1921 return screen_buffer_ioctl( evts->console->active->fd, code, async );
1923 default:
1924 if (!evts->console)
1926 set_error( STATUS_INVALID_HANDLE );
1927 return 0;
1929 return console_input_ioctl( evts->console->fd, code, async );
1933 static struct object_type *console_device_get_type( struct object *obj )
1935 static const WCHAR name[] = {'D','e','v','i','c','e'};
1936 static const struct unicode_str str = { name, sizeof(name) };
1937 return get_object_type( &str );
1940 static void console_device_dump( struct object *obj, int verbose )
1942 fputs( "Console device\n", stderr );
1945 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1947 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1948 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1949 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1950 static const WCHAR rendererW[] = {'R','e','n','d','e','r','e','r'};
1951 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1953 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1955 if (!current->process->console)
1957 set_error( STATUS_INVALID_HANDLE );
1958 return NULL;
1960 name->len = 0;
1961 return grab_object( current->process->console );
1964 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1966 if (!current->process->console || !current->process->console->active)
1968 set_error( STATUS_INVALID_HANDLE );
1969 return NULL;
1971 name->len = 0;
1972 return grab_object( current->process->console->active );
1975 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1977 name->len = 0;
1978 return grab_object( obj );
1981 if (name->len == sizeof(rendererW) && !memcmp( name->str, rendererW, name->len ))
1983 name->len = 0;
1984 return create_console_input_events();
1987 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1989 if (!current->process->console)
1991 set_error( STATUS_INVALID_HANDLE );
1992 return NULL;
1994 name->len = 0;
1995 return create_console_output( current->process->console, -1 );
1998 return NULL;
2001 static struct object *console_device_open_file( struct object *obj, unsigned int access,
2002 unsigned int sharing, unsigned int options )
2004 int is_output;
2005 access = default_fd_map_access( obj, access );
2006 is_output = access & FILE_WRITE_DATA;
2007 if (!current->process->console || (is_output && !current->process->console))
2009 set_error( STATUS_INVALID_HANDLE );
2010 return NULL;
2012 if (is_output && (access & FILE_READ_DATA))
2014 set_error( STATUS_INVALID_PARAMETER );
2015 return NULL;
2017 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
2020 struct object *create_console_device( struct object *root, const struct unicode_str *name )
2022 return create_named_object( root, &console_device_ops, name, 0, NULL );
2025 /* allocate a console for the renderer */
2026 DECL_HANDLER(alloc_console)
2028 struct process *process;
2029 struct console_input *console;
2030 int fd;
2031 int attach = 0;
2033 if (req->input_fd != -1)
2035 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
2037 set_error( STATUS_INVALID_PARAMETER );
2038 return;
2041 else fd = -1;
2043 switch (req->pid)
2045 case 0:
2046 /* console to be attached to parent process */
2047 if (!(process = get_process_from_id( current->process->parent_id )))
2049 if (fd != -1) close( fd );
2050 set_error( STATUS_ACCESS_DENIED );
2051 return;
2053 attach = 1;
2054 break;
2055 case 0xffffffff:
2056 /* console to be attached to current process */
2057 process = current->process;
2058 grab_object( process );
2059 attach = 1;
2060 break;
2061 default:
2062 /* console to be attached to req->pid */
2063 if (!(process = get_process_from_id( req->pid )))
2065 if (fd != -1) close( fd );
2066 return;
2070 if (attach && process->console)
2072 if (fd != -1) close( fd );
2073 set_error( STATUS_ACCESS_DENIED );
2075 else if ((console = (struct console_input*)create_console_input( fd )))
2077 if ((reply->handle_in = alloc_handle( current->process, console, req->access,
2078 req->attributes )) && attach)
2080 process->console = (struct console_input*)grab_object( console );
2081 console->num_proc++;
2083 release_object( console );
2085 release_object( process );
2088 /* free the console of the current process */
2089 DECL_HANDLER(free_console)
2091 free_console( current->process );
2094 /* attach to a other process's console */
2095 DECL_HANDLER(attach_console)
2097 struct process *process;
2099 if (current->process->console)
2101 set_error( STATUS_ACCESS_DENIED );
2102 return;
2105 process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
2106 ? current->process->parent_id : req->pid );
2107 if (!process) return;
2109 if (process->console && process->console->active)
2111 current->process->console = (struct console_input *)grab_object( process->console );
2112 current->process->console->num_proc++;
2114 else
2116 set_error( STATUS_INVALID_HANDLE );
2119 release_object( process );
2120 return;
2123 /* set info about a console input */
2124 DECL_HANDLER(set_console_input_info)
2126 set_console_input_info( req, get_req_data(), get_req_data_size() );
2129 /* get info about a console (output only) */
2130 DECL_HANDLER(get_console_input_info)
2132 struct console_input *console;
2134 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
2135 if (console->title) set_reply_data( console->title, min( console->title_len, get_reply_max_size() ));
2136 reply->history_mode = console->history_mode;
2137 reply->history_size = console->history_size;
2138 reply->history_index = console->history_index;
2139 reply->edition_mode = console->edition_mode;
2140 reply->input_cp = console->input_cp;
2141 reply->output_cp = console->output_cp;
2142 reply->win = console->win;
2144 release_object( console );
2147 /* appends a string to console's history */
2148 DECL_HANDLER(append_console_input_history)
2150 struct console_input *console;
2152 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
2153 console_input_append_hist( console, get_req_data(), get_req_data_size() );
2154 release_object( console );
2157 /* appends a string to console's history */
2158 DECL_HANDLER(get_console_input_history)
2160 struct console_input *console;
2162 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
2163 reply->total = console_input_get_hist( console, req->index );
2164 release_object( console );
2167 /* creates a screen buffer */
2168 DECL_HANDLER(create_console_output)
2170 struct console_input *console;
2171 struct object *screen_buffer;
2172 int fd;
2174 if (req->fd != -1)
2176 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
2178 set_error( STATUS_INVALID_HANDLE );
2179 return;
2182 else fd = -1;
2183 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
2185 if (fd != -1) close( fd );
2186 return;
2188 if (console_input_is_bare( console ) ^ (fd != -1))
2190 if (fd != -1) close( fd );
2191 release_object( console );
2192 set_error( STATUS_INVALID_HANDLE );
2193 return;
2196 screen_buffer = create_console_output( console, fd );
2197 if (screen_buffer)
2199 /* FIXME: should store sharing and test it when opening the CONOUT$ device
2200 * see file.c on how this could be done */
2201 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
2202 release_object( screen_buffer );
2204 release_object( console );
2207 /* sends a signal to a console (process, group...) */
2208 DECL_HANDLER(send_console_signal)
2210 process_id_t group;
2212 group = req->group_id ? req->group_id : current->process->group_id;
2214 if (!group)
2215 set_error( STATUS_INVALID_PARAMETER );
2216 else
2217 propagate_console_signal( current->process->console, req->signal, group );
2220 /* get console which renderer is 'current' */
2221 static int cgwe_enum( struct process* process, void* user)
2223 if (process->console && process->console->renderer == current)
2225 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
2226 return 1;
2228 return 0;
2231 DECL_HANDLER(get_console_wait_event)
2233 struct console_input* console = NULL;
2234 struct object *obj;
2236 if (req->handle)
2238 if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
2239 if (obj->ops == &console_input_ops)
2240 console = (struct console_input *)grab_object( obj );
2241 else if (obj->ops == &screen_buffer_ops)
2242 console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
2243 else
2244 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2245 release_object( obj );
2247 else if (current->process->console)
2248 console = (struct console_input *)grab_object( current->process->console );
2249 else enum_processes(cgwe_enum, &console);
2251 if (console)
2253 reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
2254 release_object( console );
2256 else set_error( STATUS_INVALID_PARAMETER );