Release 5.15.
[wine.git] / server / console.c
blob59a2794e01999fbd18c83f4659a25897c8d8117b
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 obj_handle_t 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++;
656 if (!process->console) return 0;
657 return alloc_handle( process, process->console,
658 SYNCHRONIZE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, 0 );
661 struct thread *console_get_renderer( struct console_input *console )
663 return console->renderer;
666 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
668 struct console_input* console = NULL;
670 if (handle)
671 console = (struct console_input *)get_handle_obj( current->process, handle,
672 access, &console_input_ops );
673 else if (current->process->console)
675 console = (struct console_input *)grab_object( current->process->console );
678 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
679 return console;
682 struct console_signal_info
684 struct console_input *console;
685 process_id_t group;
686 int signal;
689 static int propagate_console_signal_cb(struct process *process, void *user)
691 struct console_signal_info* csi = (struct console_signal_info*)user;
693 if (process->console == csi->console && process->running_threads &&
694 (!csi->group || process->group_id == csi->group))
696 /* find a suitable thread to signal */
697 struct thread *thread;
698 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
700 if (send_thread_signal( thread, csi->signal )) break;
703 return FALSE;
706 static void propagate_console_signal( struct console_input *console,
707 int sig, process_id_t group_id )
709 struct console_signal_info csi;
711 if (!console)
713 set_error( STATUS_INVALID_PARAMETER );
714 return;
716 /* FIXME: should support the other events (like CTRL_BREAK) */
717 if (sig != CTRL_C_EVENT)
719 set_error( STATUS_NOT_IMPLEMENTED );
720 return;
722 csi.console = console;
723 csi.signal = SIGINT;
724 csi.group = group_id;
726 enum_processes(propagate_console_signal_cb, &csi);
729 /* retrieve a pointer to the console input records */
730 static int read_console_input( struct console_input *console, struct async *async, int flush )
732 struct iosb *iosb = async_get_iosb( async );
733 data_size_t count;
735 count = min( iosb->out_size / sizeof(INPUT_RECORD), console->recnum );
736 if (count)
738 if (!(iosb->out_data = malloc( count * sizeof(INPUT_RECORD) )))
740 set_error( STATUS_NO_MEMORY );
741 release_object( iosb );
742 return 0;
744 iosb->out_size = iosb->result = count * sizeof(INPUT_RECORD);
745 memcpy( iosb->out_data, console->records, iosb->result );
746 iosb->status = STATUS_SUCCESS;
747 async_terminate( async, STATUS_ALERTED );
749 else
751 async_terminate( async, STATUS_SUCCESS );
754 release_object( iosb );
756 if (flush && count)
758 if (console->recnum > count)
760 INPUT_RECORD *new_rec;
761 memmove( console->records, console->records + count, (console->recnum - count) * sizeof(*console->records) );
762 console->recnum -= count;
763 new_rec = realloc( console->records, console->recnum * sizeof(*console->records) );
764 if (new_rec) console->records = new_rec;
766 else
768 console->recnum = 0;
769 free( console->records );
770 console->records = NULL;
771 reset_event( console->event );
775 return 1;
778 /* add input events to a console input queue */
779 static int write_console_input( struct console_input* console, int count,
780 const INPUT_RECORD *records )
782 INPUT_RECORD *new_rec;
783 struct async *async;
785 if (!count) return 1;
786 if (!(new_rec = realloc( console->records,
787 (console->recnum + count) * sizeof(INPUT_RECORD) )))
789 set_error( STATUS_NO_MEMORY );
790 return 0;
792 console->records = new_rec;
793 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
795 if (console->mode & ENABLE_PROCESSED_INPUT)
797 int i = 0;
798 while (i < count)
800 if (records[i].EventType == KEY_EVENT &&
801 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
802 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
804 if (i != count - 1)
805 memcpy( &console->records[console->recnum + i],
806 &console->records[console->recnum + i + 1],
807 (count - i - 1) * sizeof(INPUT_RECORD) );
808 count--;
809 if (records[i].Event.KeyEvent.bKeyDown)
811 /* send SIGINT to all processes attached to this console */
812 propagate_console_signal( console, CTRL_C_EVENT, 0 );
815 else i++;
818 console->recnum += count;
819 while (console->recnum && (async = find_pending_async( &console->read_q )))
821 read_console_input( console, async, 1 );
822 release_object( async );
824 if (console->recnum) set_event( console->event );
825 return 1;
828 /* set misc console input information */
829 static int set_console_input_info( const struct set_console_input_info_request *req,
830 const WCHAR *title, data_size_t len )
832 struct console_input *console;
833 struct condrv_renderer_event evt;
835 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
836 if (console_input_is_bare(console) && (req->mask & SET_CONSOLE_INPUT_INFO_WIN))
838 set_error( STATUS_UNSUCCESSFUL );
839 goto error;
842 memset(&evt.u, 0, sizeof(evt.u));
843 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
845 WCHAR *new_title = NULL;
847 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
848 if (len && !(new_title = memdup( title, len ))) goto error;
849 free( console->title );
850 console->title = new_title;
851 console->title_len = len;
852 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
853 console_input_events_append( console, &evt );
855 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
857 console->input_cp = req->input_cp;
859 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
861 console->output_cp = req->output_cp;
863 release_object( console );
864 return 1;
865 error:
866 if (console) release_object( console );
867 return 0;
870 /* resize a screen buffer */
871 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
872 int new_width, int new_height )
874 int i, old_width, old_height, copy_width, copy_height;
875 char_info_t *new_data;
877 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
879 set_error( STATUS_NO_MEMORY );
880 return 0;
882 old_width = screen_buffer->width;
883 old_height = screen_buffer->height;
884 copy_width = min( old_width, new_width );
885 copy_height = min( old_height, new_height );
887 /* copy all the rows */
888 for (i = 0; i < copy_height; i++)
890 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
891 copy_width * sizeof(char_info_t) );
894 /* clear the end of each row */
895 if (new_width > old_width)
897 /* fill first row */
898 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
899 /* and blast it to the other rows */
900 for (i = 1; i < copy_height; i++)
901 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
902 (new_width - old_width) * sizeof(char_info_t) );
905 /* clear remaining rows */
906 if (new_height > old_height)
908 /* fill first row */
909 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
910 /* and blast it to the other rows */
911 for (i = old_height+1; i < new_height; i++)
912 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
913 new_width * sizeof(char_info_t) );
915 free( screen_buffer->data );
916 screen_buffer->data = new_data;
917 screen_buffer->width = new_width;
918 screen_buffer->height = new_height;
919 return 1;
922 static int set_output_info( struct screen_buffer *screen_buffer,
923 const struct condrv_output_info_params *params, data_size_t extra_size )
925 const struct condrv_output_info *info = &params->info;
926 struct condrv_renderer_event evt;
927 WCHAR *font_name;
929 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
931 if (info->cursor_size < 1 || info->cursor_size > 100)
933 set_error( STATUS_INVALID_PARAMETER );
934 return 0;
936 if (screen_buffer->cursor_size != info->cursor_size ||
937 screen_buffer->cursor_visible != info->cursor_visible)
939 screen_buffer->cursor_size = info->cursor_size;
940 screen_buffer->cursor_visible = info->cursor_visible;
941 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
942 memset( &evt.u, 0, sizeof(evt.u) );
943 evt.u.cursor_geom.size = info->cursor_size;
944 evt.u.cursor_geom.visible = info->cursor_visible;
945 console_input_events_append( screen_buffer->input, &evt );
948 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
950 if (info->cursor_x < 0 || info->cursor_x >= screen_buffer->width ||
951 info->cursor_y < 0 || info->cursor_y >= screen_buffer->height)
953 set_error( STATUS_INVALID_PARAMETER );
954 return 0;
956 if (screen_buffer->cursor_x != info->cursor_x || screen_buffer->cursor_y != info->cursor_y)
958 screen_buffer->cursor_x = info->cursor_x;
959 screen_buffer->cursor_y = info->cursor_y;
960 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
961 memset( &evt.u, 0, sizeof(evt.u) );
962 evt.u.cursor_pos.x = info->cursor_x;
963 evt.u.cursor_pos.y = info->cursor_y;
964 console_input_events_append( screen_buffer->input, &evt );
967 if (params->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
969 unsigned cc;
971 /* new screen-buffer cannot be smaller than actual window */
972 if (info->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
973 info->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
975 set_error( STATUS_INVALID_PARAMETER );
976 return 0;
978 /* FIXME: there are also some basic minimum and max size to deal with */
979 if (!change_screen_buffer_size( screen_buffer, info->width, info->height )) return 0;
981 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
982 memset( &evt.u, 0, sizeof(evt.u) );
983 evt.u.resize.width = info->width;
984 evt.u.resize.height = info->height;
985 console_input_events_append( screen_buffer->input, &evt );
987 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
988 memset( &evt.u, 0, sizeof(evt.u) );
989 evt.u.update.top = 0;
990 evt.u.update.bottom = screen_buffer->height - 1;
991 console_input_events_append( screen_buffer->input, &evt );
993 /* scroll window to display sb */
994 if (screen_buffer->win.right >= info->width)
996 screen_buffer->win.right -= screen_buffer->win.left;
997 screen_buffer->win.left = 0;
999 if (screen_buffer->win.bottom >= info->height)
1001 screen_buffer->win.bottom -= screen_buffer->win.top;
1002 screen_buffer->win.top = 0;
1004 /* reset cursor if needed (normally, if cursor was outside of new sb, the
1005 * window has been shifted so that the new position of the cursor will be
1006 * visible */
1007 cc = 0;
1008 if (screen_buffer->cursor_x >= info->width)
1010 screen_buffer->cursor_x = info->width - 1;
1011 cc++;
1013 if (screen_buffer->cursor_y >= info->height)
1015 screen_buffer->cursor_y = info->height - 1;
1016 cc++;
1018 if (cc)
1020 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1021 memset( &evt.u, 0, sizeof(evt.u) );
1022 evt.u.cursor_pos.x = info->cursor_x;
1023 evt.u.cursor_pos.y = info->cursor_y;
1024 console_input_events_append( screen_buffer->input, &evt );
1027 if (screen_buffer == screen_buffer->input->active &&
1028 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
1030 INPUT_RECORD ir;
1031 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
1032 ir.Event.WindowBufferSizeEvent.dwSize.X = info->width;
1033 ir.Event.WindowBufferSizeEvent.dwSize.Y = info->height;
1034 write_console_input( screen_buffer->input, 1, &ir );
1037 if (params->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1039 screen_buffer->attr = info->attr;
1041 if (params->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1043 screen_buffer->popup_attr = info->popup_attr;
1045 if (params->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1047 if (info->win_left < 0 || info->win_left > info->win_right ||
1048 info->win_right >= screen_buffer->width ||
1049 info->win_top < 0 || info->win_top > info->win_bottom ||
1050 info->win_bottom >= screen_buffer->height)
1052 set_error( STATUS_INVALID_PARAMETER );
1053 return 0;
1055 if (screen_buffer->win.left != info->win_left || screen_buffer->win.top != info->win_top ||
1056 screen_buffer->win.right != info->win_right || screen_buffer->win.bottom != info->win_bottom)
1058 screen_buffer->win.left = info->win_left;
1059 screen_buffer->win.top = info->win_top;
1060 screen_buffer->win.right = info->win_right;
1061 screen_buffer->win.bottom = info->win_bottom;
1062 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1063 memset( &evt.u, 0, sizeof(evt.u) );
1064 evt.u.display.left = info->win_left;
1065 evt.u.display.top = info->win_top;
1066 evt.u.display.width = info->win_right - info->win_left + 1;
1067 evt.u.display.height = info->win_bottom - info->win_top + 1;
1068 console_input_events_append( screen_buffer->input, &evt );
1071 if (params->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1073 screen_buffer->max_width = info->max_width;
1074 screen_buffer->max_height = info->max_height;
1076 if (params->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1078 memcpy( screen_buffer->color_map, info->color_map, sizeof(info->color_map) );
1080 if (params->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1082 screen_buffer->font.width = info->font_width;
1083 screen_buffer->font.height = info->font_height;
1084 screen_buffer->font.weight = info->font_weight;
1085 screen_buffer->font.pitch_family = info->font_pitch_family;
1086 if (extra_size)
1088 extra_size = extra_size / sizeof(WCHAR) * sizeof(WCHAR);
1089 font_name = mem_alloc( extra_size );
1090 if (font_name)
1092 memcpy( font_name, info + 1, extra_size );
1093 free( screen_buffer->font.face_name );
1094 screen_buffer->font.face_name = font_name;
1095 screen_buffer->font.face_len = extra_size;
1100 return 1;
1103 /* appends a new line to history (history is a fixed size array) */
1104 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1106 struct history_line *ptr;
1108 if (!console || !console->history_size)
1110 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1111 return;
1114 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
1115 if (console->history_mode && console->history_index &&
1116 console->history[console->history_index - 1]->len == len &&
1117 !memcmp( console->history[console->history_index - 1]->text, buf, len ))
1119 /* don't duplicate entry */
1120 set_error( STATUS_ALIAS_EXISTS );
1121 return;
1123 if (!(ptr = mem_alloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
1124 ptr->len = len;
1125 memcpy( ptr->text, buf, len );
1127 if (console->history_index < console->history_size)
1129 console->history[console->history_index++] = ptr;
1131 else
1133 free( console->history[0]) ;
1134 memmove( &console->history[0], &console->history[1],
1135 (console->history_size - 1) * sizeof(*console->history) );
1136 console->history[console->history_size - 1] = ptr;
1140 /* returns a line from the cache */
1141 static data_size_t console_input_get_hist( struct console_input *console, int index )
1143 data_size_t ret = 0;
1145 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1146 else
1148 ret = console->history[index]->len;
1149 set_reply_data( console->history[index]->text, min( ret, get_reply_max_size() ));
1151 return ret;
1154 /* dumb dump */
1155 static void console_input_dump( struct object *obj, int verbose )
1157 struct console_input *console = (struct console_input *)obj;
1158 assert( obj->ops == &console_input_ops );
1159 fprintf( stderr, "Console input active=%p evt=%p\n",
1160 console->active, console->evt );
1163 static void console_input_destroy( struct object *obj )
1165 struct console_input* console_in = (struct console_input *)obj;
1166 struct screen_buffer* curr;
1167 int i;
1169 assert( obj->ops == &console_input_ops );
1170 free( console_in->title );
1171 free( console_in->records );
1173 if (console_in->active) release_object( console_in->active );
1174 console_in->active = NULL;
1176 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1178 if (curr->input == console_in) curr->input = NULL;
1181 free_async_queue( &console_in->read_q );
1182 if (console_in->evt)
1183 console_in->evt->console = NULL;
1184 if (console_in->event)
1185 release_object( console_in->event );
1186 if (console_in->fd)
1187 release_object( console_in->fd );
1189 for (i = 0; i < console_in->history_size; i++)
1190 free( console_in->history[i] );
1191 free( console_in->history );
1194 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1195 unsigned int sharing, unsigned int options )
1197 return grab_object( obj );
1200 static void screen_buffer_dump( struct object *obj, int verbose )
1202 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1203 assert( obj->ops == &screen_buffer_ops );
1205 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1208 static void screen_buffer_destroy( struct object *obj )
1210 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1212 assert( obj->ops == &screen_buffer_ops );
1214 list_remove( &screen_buffer->entry );
1216 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1218 struct screen_buffer *sb;
1220 screen_buffer->input->active = NULL;
1221 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1223 if (sb->input == screen_buffer->input)
1225 sb->input->active = sb;
1226 break;
1230 if (screen_buffer->fd) release_object( screen_buffer->fd );
1231 free( screen_buffer->data );
1232 free( screen_buffer->font.face_name );
1235 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
1236 unsigned int sharing, unsigned int options )
1238 return grab_object( obj );
1241 static struct fd *screen_buffer_get_fd( struct object *obj )
1243 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1244 assert( obj->ops == &screen_buffer_ops );
1245 if (screen_buffer->fd)
1246 return (struct fd*)grab_object( screen_buffer->fd );
1247 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1248 return NULL;
1251 /* read data from a screen buffer */
1252 static void read_console_output( struct screen_buffer *screen_buffer, unsigned int x, unsigned int y,
1253 enum char_info_mode mode, unsigned int width )
1255 unsigned int i, count;
1256 char_info_t *src;
1258 if (x >= screen_buffer->width || y >= screen_buffer->height)
1260 if (width) set_error( STATUS_INVALID_PARAMETER );
1261 return;
1263 src = screen_buffer->data + y * screen_buffer->width + x;
1265 switch(mode)
1267 case CHAR_INFO_MODE_TEXT:
1269 WCHAR *data;
1270 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1271 get_reply_max_size() / sizeof(*data) );
1272 if ((data = set_reply_data_size( count * sizeof(*data) )))
1274 for (i = 0; i < count; i++) data[i] = src[i].ch;
1277 break;
1278 case CHAR_INFO_MODE_ATTR:
1280 unsigned short *data;
1281 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1282 get_reply_max_size() / sizeof(*data) );
1283 if ((data = set_reply_data_size( count * sizeof(*data) )))
1285 for (i = 0; i < count; i++) data[i] = src[i].attr;
1288 break;
1289 case CHAR_INFO_MODE_TEXTATTR:
1291 char_info_t *data;
1292 SMALL_RECT *region;
1293 if (!width || get_reply_max_size() < sizeof(*region))
1295 set_error( STATUS_INVALID_PARAMETER );
1296 return;
1298 count = min( (get_reply_max_size() - sizeof(*region)) / (width * sizeof(*data)), screen_buffer->height - y );
1299 width = min( width, screen_buffer->width - x );
1300 if (!(region = set_reply_data_size( sizeof(*region) + width * count * sizeof(*data) ))) return;
1301 region->Left = x;
1302 region->Top = y;
1303 region->Right = x + width - 1;
1304 region->Bottom = y + count - 1;
1305 data = (char_info_t *)(region + 1);
1306 for (i = 0; i < count; i++)
1308 memcpy( &data[i * width], &src[i * screen_buffer->width], width * sizeof(*data) );
1311 break;
1312 default:
1313 set_error( STATUS_INVALID_PARAMETER );
1314 break;
1318 /* write data into a screen buffer */
1319 static void write_console_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
1320 data_size_t size )
1322 unsigned int i, entry_size, entry_cnt, x, y;
1323 char_info_t *dest;
1324 char *src;
1326 entry_size = params->mode == CHAR_INFO_MODE_TEXTATTR ? sizeof(char_info_t) : sizeof(WCHAR);
1327 if (size % entry_size)
1329 set_error( STATUS_INVALID_PARAMETER );
1330 return;
1332 if (params->x >= screen_buffer->width) return;
1333 entry_cnt = size / entry_size;
1335 for (i = 0, src = (char *)(params + 1); i < entry_cnt; i++, src += entry_size)
1337 if (params->width)
1339 x = params->x + i % params->width;
1340 y = params->y + i / params->width;
1341 if (x >= screen_buffer->width) continue;
1343 else
1345 x = (params->x + i) % screen_buffer->width;
1346 y = params->y + (params->x + i) / screen_buffer->width;
1348 if (y >= screen_buffer->height) break;
1350 dest = &screen_buffer->data[y * screen_buffer->width + x];
1351 switch(params->mode)
1353 case CHAR_INFO_MODE_TEXT:
1354 dest->ch = *(const WCHAR *)src;
1355 break;
1356 case CHAR_INFO_MODE_ATTR:
1357 dest->attr = *(const unsigned short *)src;
1358 break;
1359 case CHAR_INFO_MODE_TEXTATTR:
1360 *dest = *(const char_info_t *)src;
1361 break;
1362 case CHAR_INFO_MODE_TEXTSTDATTR:
1363 dest->ch = *(const WCHAR *)src;
1364 dest->attr = screen_buffer->attr;
1365 break;
1366 default:
1367 set_error( STATUS_INVALID_PARAMETER );
1368 return;
1372 if (i && screen_buffer == screen_buffer->input->active)
1374 struct condrv_renderer_event evt;
1375 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1376 memset(&evt.u, 0, sizeof(evt.u));
1377 evt.u.update.top = params->y;
1378 evt.u.update.bottom = params->width
1379 ? min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1
1380 : params->y + (params->x + i - 1) / screen_buffer->width;
1381 console_input_events_append( screen_buffer->input, &evt );
1384 if (get_reply_max_size() == sizeof(SMALL_RECT))
1386 SMALL_RECT region;
1387 region.Left = params->x;
1388 region.Top = params->y;
1389 region.Right = min( params->x + params->width, screen_buffer->width ) - 1;
1390 region.Bottom = min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1;
1391 set_reply_data( &region, sizeof(region) );
1393 else set_reply_data( &i, sizeof(i) );
1396 /* fill a screen buffer with uniform data */
1397 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1398 enum char_info_mode mode, int x, int y, int count, int wrap )
1400 int i;
1401 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1403 if (y >= screen_buffer->height) return 0;
1405 if (wrap)
1406 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1407 else
1408 end = screen_buffer->data + (y+1) * screen_buffer->width;
1410 if (count > end - dest) count = end - dest;
1412 switch(mode)
1414 case CHAR_INFO_MODE_TEXT:
1415 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1416 break;
1417 case CHAR_INFO_MODE_ATTR:
1418 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1419 break;
1420 case CHAR_INFO_MODE_TEXTATTR:
1421 for (i = 0; i < count; i++) dest[i] = data;
1422 break;
1423 case CHAR_INFO_MODE_TEXTSTDATTR:
1424 for (i = 0; i < count; i++)
1426 dest[i].ch = data.ch;
1427 dest[i].attr = screen_buffer->attr;
1429 break;
1430 default:
1431 set_error( STATUS_INVALID_PARAMETER );
1432 return 0;
1435 if (count && screen_buffer == screen_buffer->input->active)
1437 struct condrv_renderer_event evt;
1438 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1439 memset(&evt.u, 0, sizeof(evt.u));
1440 evt.u.update.top = y;
1441 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1442 console_input_events_append( screen_buffer->input, &evt );
1444 return i;
1447 /* scroll parts of a screen buffer */
1448 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1449 int w, int h, const rectangle_t *clip, char_info_t fill )
1451 struct condrv_renderer_event evt;
1452 rectangle_t src, dst;
1453 int x, y;
1455 src.left = max( xsrc, clip->left );
1456 src.top = max( ysrc, clip->top );
1457 src.right = min( xsrc + w - 1, clip->right );
1458 src.bottom = min( ysrc + h - 1, clip->bottom );
1460 dst.left = xdst;
1461 dst.top = ydst;
1462 dst.right = xdst + w - 1;
1463 dst.bottom = ydst + h - 1;
1465 if (dst.left < clip->left)
1467 xsrc += clip->left - dst.left;
1468 w -= clip->left - dst.left;
1469 dst.left = clip->left;
1471 if (dst.top < clip->top)
1473 ysrc += clip->top - dst.top;
1474 h -= clip->top - dst.top;
1475 dst.top = clip->top;
1477 if (dst.right > clip->right) w -= dst.right - clip->right;
1478 if (dst.bottom > clip->bottom) h -= dst.bottom - clip->bottom;
1480 if (w > 0 && h > 0)
1482 if (ysrc < ydst)
1484 for (y = h; y > 0; y--)
1486 memcpy( &screen_buffer->data[(dst.top + y - 1) * screen_buffer->width + dst.left],
1487 &screen_buffer->data[(ysrc + y - 1) * screen_buffer->width + xsrc],
1488 w * sizeof(screen_buffer->data[0]) );
1491 else
1493 for (y = 0; y < h; y++)
1495 /* we use memmove here because when psrc and pdst are the same,
1496 * copies are done on the same row, so the dst and src blocks
1497 * can overlap */
1498 memmove( &screen_buffer->data[(dst.top + y) * screen_buffer->width + dst.left],
1499 &screen_buffer->data[(ysrc + y) * screen_buffer->width + xsrc],
1500 w * sizeof(screen_buffer->data[0]) );
1505 for (y = src.top; y <= src.bottom; y++)
1507 int left = src.left;
1508 int right = src.right;
1509 if (dst.top <= y && y <= dst.bottom)
1511 if (dst.left <= src.left) left = max( left, dst.right + 1 );
1512 if (dst.left >= src.left) right = min( right, dst.left - 1 );
1514 for (x = left; x <= right; x++) screen_buffer->data[y * screen_buffer->width + x] = fill;
1517 /* FIXME: this could be enhanced, by signalling scroll */
1518 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1519 memset(&evt.u, 0, sizeof(evt.u));
1520 evt.u.update.top = min( src.top, dst.top );
1521 evt.u.update.bottom = max( src.bottom, dst.bottom );
1522 console_input_events_append( screen_buffer->input, &evt );
1525 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1527 struct console_input *console = get_fd_user( fd );
1529 switch (code)
1531 case IOCTL_CONDRV_GET_MODE:
1532 if (get_reply_max_size() != sizeof(console->mode))
1534 set_error( STATUS_INVALID_PARAMETER );
1535 return 0;
1537 return set_reply_data( &console->mode, sizeof(console->mode) ) != NULL;
1539 case IOCTL_CONDRV_SET_MODE:
1540 if (get_req_data_size() != sizeof(console->mode))
1542 set_error( STATUS_INVALID_PARAMETER );
1543 return 0;
1545 console->mode = *(unsigned int *)get_req_data();
1546 return 1;
1548 case IOCTL_CONDRV_READ_INPUT:
1550 int blocking = 0;
1551 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1553 set_error( STATUS_INVALID_PARAMETER );
1554 return 0;
1556 if (get_req_data_size())
1558 if (get_req_data_size() != sizeof(int))
1560 set_error( STATUS_INVALID_PARAMETER );
1561 return 0;
1563 blocking = *(int *)get_req_data();
1565 set_error( STATUS_PENDING );
1566 if (blocking && !console->recnum)
1568 queue_async( &console->read_q, async );
1569 return 1;
1571 return read_console_input( console, async, 1 );
1574 case IOCTL_CONDRV_WRITE_INPUT:
1575 return write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD), get_req_data() );
1577 case IOCTL_CONDRV_PEEK:
1578 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1580 set_error( STATUS_INVALID_PARAMETER );
1581 return 0;
1583 set_error( STATUS_PENDING );
1584 return read_console_input( console, async, 0 );
1586 case IOCTL_CONDRV_GET_INPUT_INFO:
1588 struct condrv_input_info info;
1589 if (get_reply_max_size() != sizeof(info))
1591 set_error( STATUS_INVALID_PARAMETER );
1592 return 0;
1594 info.input_cp = console->input_cp;
1595 info.output_cp = console->output_cp;
1596 info.history_mode = console->history_mode;
1597 info.history_size = console->history_size;
1598 info.history_index = console->history_index;
1599 info.edition_mode = console->edition_mode;
1600 info.input_count = console->recnum;
1601 info.win = console->win;
1602 return set_reply_data( &info, sizeof(info) ) != NULL;
1605 case IOCTL_CONDRV_SET_INPUT_INFO:
1607 const struct condrv_input_info_params *params = get_req_data();
1608 if (get_req_data_size() != sizeof(*params))
1610 set_error( STATUS_INVALID_PARAMETER );
1611 return 0;
1613 if (params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
1615 console->history_mode = params->info.history_mode;
1617 if ((params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
1618 console->history_size != params->info.history_size)
1620 struct history_line **mem = NULL;
1621 int i, delta;
1623 if (params->info.history_size)
1625 if (!(mem = mem_alloc( params->info.history_size * sizeof(*mem) ))) return 0;
1626 memset( mem, 0, params->info.history_size * sizeof(*mem) );
1629 delta = (console->history_index > params->info.history_size) ?
1630 (console->history_index - params->info.history_size) : 0;
1632 for (i = delta; i < console->history_index; i++)
1634 mem[i - delta] = console->history[i];
1635 console->history[i] = NULL;
1637 console->history_index -= delta;
1639 for (i = 0; i < console->history_size; i++)
1640 free( console->history[i] );
1641 free( console->history );
1642 console->history = mem;
1643 console->history_size = params->info.history_size;
1645 if (params->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
1647 console->edition_mode = params->info.edition_mode;
1649 if (params->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
1651 console->input_cp = params->info.input_cp;
1653 if (params->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
1655 console->output_cp = params->info.output_cp;
1657 if (params->mask & SET_CONSOLE_INPUT_INFO_WIN)
1659 console->win = params->info.win;
1661 return 1;
1664 case IOCTL_CONDRV_GET_TITLE:
1665 if (!console->title_len) return 1;
1666 return set_reply_data( console->title, min( console->title_len, get_reply_max_size() )) != NULL;
1668 case IOCTL_CONDRV_SET_TITLE:
1670 data_size_t len = get_req_data_size();
1671 struct condrv_renderer_event evt;
1672 WCHAR *title = NULL;
1674 if (len % sizeof(WCHAR))
1676 set_error( STATUS_INVALID_PARAMETER );
1677 return 0;
1680 if (len && !(title = memdup( get_req_data(), len ))) return 0;
1681 free( console->title );
1682 console->title = title;
1683 console->title_len = len;
1684 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
1685 console_input_events_append( console, &evt );
1686 return 1;
1689 default:
1690 set_error( STATUS_INVALID_HANDLE );
1691 return 0;
1695 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1697 struct screen_buffer *screen_buffer = get_fd_user( fd );
1699 switch (code)
1701 case IOCTL_CONDRV_GET_MODE:
1702 if (get_reply_max_size() != sizeof(screen_buffer->mode))
1704 set_error( STATUS_INVALID_PARAMETER );
1705 return 0;
1707 return set_reply_data( &screen_buffer->mode, sizeof(screen_buffer->mode) ) != NULL;
1709 case IOCTL_CONDRV_SET_MODE:
1710 if (get_req_data_size() != sizeof(screen_buffer->mode))
1712 set_error( STATUS_INVALID_PARAMETER );
1713 return 0;
1715 screen_buffer->mode = *(unsigned int *)get_req_data();
1716 return 1;
1718 case IOCTL_CONDRV_READ_OUTPUT:
1720 const struct condrv_output_params *params = get_req_data();
1721 if (get_req_data_size() != sizeof(*params))
1723 set_error( STATUS_INVALID_PARAMETER );
1724 return 0;
1726 if (console_input_is_bare( screen_buffer->input ))
1728 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1729 return 0;
1731 read_console_output( screen_buffer, params->x, params->y, params->mode, params->width );
1732 return !get_error();
1735 case IOCTL_CONDRV_WRITE_OUTPUT:
1736 if (get_req_data_size() < sizeof(struct condrv_output_params) ||
1737 (get_reply_max_size() != sizeof(SMALL_RECT) && get_reply_max_size() != sizeof(unsigned int)))
1739 set_error( STATUS_INVALID_PARAMETER );
1740 return 0;
1742 if (console_input_is_bare( screen_buffer->input ))
1744 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1745 return 0;
1747 write_console_output( screen_buffer, get_req_data(), get_req_data_size() - sizeof(struct condrv_output_params) );
1748 return !get_error();
1750 case IOCTL_CONDRV_GET_OUTPUT_INFO:
1752 struct condrv_output_info *info;
1753 data_size_t size;
1755 size = min( sizeof(*info) + screen_buffer->font.face_len, get_reply_max_size() );
1756 if (size < sizeof(*info))
1758 set_error( STATUS_INVALID_PARAMETER );
1759 return 0;
1761 if (!(info = set_reply_data_size( size ))) return 0;
1763 info->cursor_size = screen_buffer->cursor_size;
1764 info->cursor_visible = screen_buffer->cursor_visible;
1765 info->cursor_x = screen_buffer->cursor_x;
1766 info->cursor_y = screen_buffer->cursor_y;
1767 info->width = screen_buffer->width;
1768 info->height = screen_buffer->height;
1769 info->attr = screen_buffer->attr;
1770 info->popup_attr = screen_buffer->popup_attr;
1771 info->win_left = screen_buffer->win.left;
1772 info->win_top = screen_buffer->win.top;
1773 info->win_right = screen_buffer->win.right;
1774 info->win_bottom = screen_buffer->win.bottom;
1775 info->max_width = screen_buffer->max_width;
1776 info->max_height = screen_buffer->max_height;
1777 info->font_width = screen_buffer->font.width;
1778 info->font_height = screen_buffer->font.height;
1779 info->font_weight = screen_buffer->font.weight;
1780 info->font_pitch_family = screen_buffer->font.pitch_family;
1781 memcpy( info->color_map, screen_buffer->color_map, sizeof(info->color_map) );
1782 size -= sizeof(*info);
1783 if (size) memcpy( info + 1, screen_buffer->font.face_name, size );
1784 return 1;
1787 case IOCTL_CONDRV_SET_OUTPUT_INFO:
1789 const struct condrv_output_info_params *params = get_req_data();
1790 if (get_req_data_size() < sizeof(*params))
1792 set_error( STATUS_INVALID_PARAMETER );
1793 return 0;
1795 if (!screen_buffer->input)
1797 set_error( STATUS_INVALID_HANDLE );
1798 return 0;
1800 return set_output_info( screen_buffer, params, get_req_data_size() - sizeof(*params) );
1803 case IOCTL_CONDRV_ACTIVATE:
1804 if (!screen_buffer->input)
1806 set_error( STATUS_INVALID_HANDLE );
1807 return 0;
1810 if (screen_buffer != screen_buffer->input->active)
1812 if (screen_buffer->input->active) release_object( screen_buffer->input->active );
1813 screen_buffer->input->active = (struct screen_buffer *)grab_object( screen_buffer );
1814 generate_sb_initial_events( screen_buffer->input );
1816 return 1;
1818 case IOCTL_CONDRV_FILL_OUTPUT:
1820 const struct condrv_fill_output_params *params = get_req_data();
1821 char_info_t data;
1822 DWORD written;
1823 if (get_req_data_size() != sizeof(*params) ||
1824 (get_reply_max_size() && get_reply_max_size() != sizeof(written)))
1826 set_error( STATUS_INVALID_PARAMETER );
1827 return 0;
1829 data.ch = params->ch;
1830 data.attr = params->attr;
1831 written = fill_console_output( screen_buffer, data, params->mode,
1832 params->x, params->y, params->count, params->wrap );
1833 if (written && get_reply_max_size() == sizeof(written))
1834 set_reply_data( &written, sizeof(written) );
1835 return !get_error();
1838 case IOCTL_CONDRV_SCROLL:
1840 const struct condrv_scroll_params *params = get_req_data();
1841 rectangle_t clip;
1842 if (get_req_data_size() != sizeof(*params))
1844 set_error( STATUS_INVALID_PARAMETER );
1845 return 0;
1847 if (console_input_is_bare( screen_buffer->input ) || !screen_buffer->input)
1849 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1850 return 0;
1852 clip.left = max( params->clip.Left, 0 );
1853 clip.top = max( params->clip.Top, 0 );
1854 clip.right = min( params->clip.Right, screen_buffer->width - 1 );
1855 clip.bottom = min( params->clip.Bottom, screen_buffer->height - 1 );
1856 if (clip.left > clip.right || clip.top > clip.bottom || params->scroll.Left < 0 || params->scroll.Top < 0 ||
1857 params->scroll.Right >= screen_buffer->width || params->scroll.Bottom >= screen_buffer->height ||
1858 params->scroll.Right < params->scroll.Left || params->scroll.Top > params->scroll.Bottom ||
1859 params->origin.X < 0 || params->origin.X >= screen_buffer->width || params->origin.Y < 0 ||
1860 params->origin.Y >= screen_buffer->height)
1862 set_error( STATUS_INVALID_PARAMETER );
1863 return 0;
1866 scroll_console_output( screen_buffer, params->scroll.Left, params->scroll.Top, params->origin.X, params->origin.Y,
1867 params->scroll.Right - params->scroll.Left + 1, params->scroll.Bottom - params->scroll.Top + 1,
1868 &clip, params->fill );
1869 return !get_error();
1872 default:
1873 set_error( STATUS_INVALID_HANDLE );
1874 return 0;
1878 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1880 struct console_input_events *evts = get_fd_user( fd );
1882 switch (code)
1884 case IOCTL_CONDRV_GET_RENDERER_EVENTS:
1885 set_error( STATUS_PENDING );
1886 if (evts->num_used) return get_renderer_events( evts, async );
1887 queue_async( &evts->read_q, async );
1888 return 1;
1890 case IOCTL_CONDRV_ATTACH_RENDERER:
1892 struct console_input *console_input;
1893 if (get_req_data_size() != sizeof(condrv_handle_t))
1895 set_error( STATUS_INVALID_PARAMETER );
1896 return 0;
1898 console_input = (struct console_input *)get_handle_obj( current->process, *(condrv_handle_t *)get_req_data(),
1899 0, &console_input_ops );
1900 if (!console_input) return 0;
1902 if (!console_input->evt && !evts->console)
1904 console_input->evt = evts;
1905 console_input->renderer = current;
1906 evts->console = console_input;
1908 else set_error( STATUS_INVALID_HANDLE );
1910 release_object( console_input );
1911 return !get_error();
1914 case IOCTL_CONDRV_SCROLL:
1915 case IOCTL_CONDRV_SET_MODE:
1916 case IOCTL_CONDRV_WRITE_OUTPUT:
1917 case IOCTL_CONDRV_READ_OUTPUT:
1918 case IOCTL_CONDRV_FILL_OUTPUT:
1919 case IOCTL_CONDRV_GET_OUTPUT_INFO:
1920 case IOCTL_CONDRV_SET_OUTPUT_INFO:
1921 if (!evts->console || !evts->console->active)
1923 set_error( STATUS_INVALID_HANDLE );
1924 return 0;
1926 return screen_buffer_ioctl( evts->console->active->fd, code, async );
1928 default:
1929 if (!evts->console)
1931 set_error( STATUS_INVALID_HANDLE );
1932 return 0;
1934 return console_input_ioctl( evts->console->fd, code, async );
1938 static struct object_type *console_device_get_type( struct object *obj )
1940 static const WCHAR name[] = {'D','e','v','i','c','e'};
1941 static const struct unicode_str str = { name, sizeof(name) };
1942 return get_object_type( &str );
1945 static void console_device_dump( struct object *obj, int verbose )
1947 fputs( "Console device\n", stderr );
1950 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1952 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1953 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1954 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1955 static const WCHAR rendererW[] = {'R','e','n','d','e','r','e','r'};
1956 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1958 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1960 if (!current->process->console)
1962 set_error( STATUS_INVALID_HANDLE );
1963 return NULL;
1965 name->len = 0;
1966 return grab_object( current->process->console );
1969 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1971 if (!current->process->console || !current->process->console->active)
1973 set_error( STATUS_INVALID_HANDLE );
1974 return NULL;
1976 name->len = 0;
1977 return grab_object( current->process->console->active );
1980 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1982 name->len = 0;
1983 return grab_object( obj );
1986 if (name->len == sizeof(rendererW) && !memcmp( name->str, rendererW, name->len ))
1988 name->len = 0;
1989 return create_console_input_events();
1992 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1994 if (!current->process->console)
1996 set_error( STATUS_INVALID_HANDLE );
1997 return NULL;
1999 name->len = 0;
2000 return create_console_output( current->process->console, -1 );
2003 return NULL;
2006 static struct object *console_device_open_file( struct object *obj, unsigned int access,
2007 unsigned int sharing, unsigned int options )
2009 int is_output;
2010 access = default_fd_map_access( obj, access );
2011 is_output = access & FILE_WRITE_DATA;
2012 if (!current->process->console || (is_output && !current->process->console))
2014 set_error( STATUS_INVALID_HANDLE );
2015 return NULL;
2017 if (is_output && (access & FILE_READ_DATA))
2019 set_error( STATUS_INVALID_PARAMETER );
2020 return NULL;
2022 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
2025 struct object *create_console_device( struct object *root, const struct unicode_str *name )
2027 return create_named_object( root, &console_device_ops, name, 0, NULL );
2030 /* allocate a console for the renderer */
2031 DECL_HANDLER(alloc_console)
2033 struct process *process;
2034 struct console_input *console;
2035 int fd;
2036 int attach = 0;
2038 if (req->input_fd != -1)
2040 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
2042 set_error( STATUS_INVALID_PARAMETER );
2043 return;
2046 else fd = -1;
2048 switch (req->pid)
2050 case 0:
2051 /* console to be attached to parent process */
2052 if (!(process = get_process_from_id( current->process->parent_id )))
2054 if (fd != -1) close( fd );
2055 set_error( STATUS_ACCESS_DENIED );
2056 return;
2058 attach = 1;
2059 break;
2060 case 0xffffffff:
2061 /* console to be attached to current process */
2062 process = current->process;
2063 grab_object( process );
2064 attach = 1;
2065 break;
2066 default:
2067 /* console to be attached to req->pid */
2068 if (!(process = get_process_from_id( req->pid )))
2070 if (fd != -1) close( fd );
2071 return;
2075 if (attach && process->console)
2077 if (fd != -1) close( fd );
2078 set_error( STATUS_ACCESS_DENIED );
2080 else if ((console = (struct console_input*)create_console_input( fd )))
2082 if ((reply->handle_in = alloc_handle( current->process, console, req->access,
2083 req->attributes )) && attach)
2085 process->console = (struct console_input*)grab_object( console );
2086 console->num_proc++;
2088 release_object( console );
2090 release_object( process );
2093 /* free the console of the current process */
2094 DECL_HANDLER(free_console)
2096 free_console( current->process );
2099 /* attach to a other process's console */
2100 DECL_HANDLER(attach_console)
2102 struct process *process;
2104 if (current->process->console)
2106 set_error( STATUS_ACCESS_DENIED );
2107 return;
2110 process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
2111 ? current->process->parent_id : req->pid );
2112 if (!process) return;
2114 if (process->console && process->console->active)
2116 current->process->console = (struct console_input *)grab_object( process->console );
2117 current->process->console->num_proc++;
2119 else
2121 set_error( STATUS_INVALID_HANDLE );
2124 release_object( process );
2125 return;
2128 /* set info about a console input */
2129 DECL_HANDLER(set_console_input_info)
2131 set_console_input_info( req, get_req_data(), get_req_data_size() );
2134 /* appends a string to console's history */
2135 DECL_HANDLER(append_console_input_history)
2137 struct console_input *console;
2139 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
2140 console_input_append_hist( console, get_req_data(), get_req_data_size() );
2141 release_object( console );
2144 /* appends a string to console's history */
2145 DECL_HANDLER(get_console_input_history)
2147 struct console_input *console;
2149 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
2150 reply->total = console_input_get_hist( console, req->index );
2151 release_object( console );
2154 /* creates a screen buffer */
2155 DECL_HANDLER(create_console_output)
2157 struct console_input *console;
2158 struct object *screen_buffer;
2159 int fd;
2161 if (req->fd != -1)
2163 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
2165 set_error( STATUS_INVALID_HANDLE );
2166 return;
2169 else fd = -1;
2170 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
2172 if (fd != -1) close( fd );
2173 return;
2175 if (console_input_is_bare( console ) ^ (fd != -1))
2177 if (fd != -1) close( fd );
2178 release_object( console );
2179 set_error( STATUS_INVALID_HANDLE );
2180 return;
2183 screen_buffer = create_console_output( console, fd );
2184 if (screen_buffer)
2186 /* FIXME: should store sharing and test it when opening the CONOUT$ device
2187 * see file.c on how this could be done */
2188 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
2189 release_object( screen_buffer );
2191 release_object( console );
2194 /* sends a signal to a console (process, group...) */
2195 DECL_HANDLER(send_console_signal)
2197 process_id_t group;
2199 group = req->group_id ? req->group_id : current->process->group_id;
2201 if (!group)
2202 set_error( STATUS_INVALID_PARAMETER );
2203 else
2204 propagate_console_signal( current->process->console, req->signal, group );
2207 /* get console which renderer is 'current' */
2208 static int cgwe_enum( struct process* process, void* user)
2210 if (process->console && process->console->renderer == current)
2212 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
2213 return 1;
2215 return 0;
2218 DECL_HANDLER(get_console_wait_event)
2220 struct console_input* console = NULL;
2221 struct object *obj;
2223 if (req->handle)
2225 if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
2226 if (obj->ops == &console_input_ops)
2227 console = (struct console_input *)grab_object( obj );
2228 else if (obj->ops == &screen_buffer_ops)
2229 console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
2230 else
2231 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2232 release_object( obj );
2234 else if (current->process->console)
2235 console = (struct console_input *)grab_object( current->process->console );
2236 else enum_processes(cgwe_enum, &console);
2238 if (console)
2240 reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
2241 release_object( console );
2243 else set_error( STATUS_INVALID_PARAMETER );