windowscodecs/tests: Add pixel format bpp to test_data.
[wine.git] / server / console.c
blob5cdf3e78224f601bdb6bcbed2281a6faf876fff0
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 struct console_server *server; /* console server object */
63 WCHAR *title; /* console title */
64 data_size_t title_len; /* length of console title */
65 struct history_line **history; /* lines history */
66 int history_size; /* number of entries in history array */
67 int history_index; /* number of used entries in history array */
68 int history_mode; /* mode of history (non zero means remove doubled strings */
69 int edition_mode; /* index to edition mode flavors */
70 int input_cp; /* console input codepage */
71 int output_cp; /* console output codepage */
72 user_handle_t win; /* window handle if backend supports it */
73 struct event *event; /* event to wait on for input queue */
74 struct fd *fd; /* for bare console, attached input fd */
75 struct async_queue ioctl_q; /* ioctl queue */
76 struct async_queue read_q; /* read queue */
79 static void console_input_dump( struct object *obj, int verbose );
80 static void console_input_destroy( struct object *obj );
81 static struct fd *console_input_get_fd( struct object *obj );
82 static struct object *console_input_open_file( struct object *obj, unsigned int access,
83 unsigned int sharing, unsigned int options );
85 static const struct object_ops console_input_ops =
87 sizeof(struct console_input), /* size */
88 console_input_dump, /* dump */
89 no_get_type, /* get_type */
90 no_add_queue, /* add_queue */
91 NULL, /* remove_queue */
92 NULL, /* signaled */
93 no_satisfied, /* satisfied */
94 no_signal, /* signal */
95 console_input_get_fd, /* get_fd */
96 default_fd_map_access, /* map_access */
97 default_get_sd, /* get_sd */
98 default_set_sd, /* set_sd */
99 no_lookup_name, /* lookup_name */
100 no_link_name, /* link_name */
101 NULL, /* unlink_name */
102 console_input_open_file, /* open_file */
103 no_kernel_obj_list, /* get_kernel_obj_list */
104 no_close_handle, /* close_handle */
105 console_input_destroy /* destroy */
108 static enum server_fd_type console_get_fd_type( struct fd *fd );
109 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
111 static const struct fd_ops console_input_fd_ops =
113 default_fd_get_poll_events, /* get_poll_events */
114 default_poll_event, /* poll_event */
115 console_get_fd_type, /* get_fd_type */
116 no_fd_read, /* read */
117 no_fd_write, /* write */
118 no_fd_flush, /* flush */
119 no_fd_get_file_info, /* get_file_info */
120 no_fd_get_volume_info, /* get_volume_info */
121 console_input_ioctl, /* ioctl */
122 default_fd_queue_async, /* queue_async */
123 default_fd_reselect_async /* reselect_async */
126 static void console_input_events_dump( struct object *obj, int verbose );
127 static void console_input_events_destroy( struct object *obj );
128 static struct fd *console_input_events_get_fd( struct object *obj );
129 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
130 unsigned int sharing, unsigned int options );
132 struct console_input_events
134 struct object obj; /* object header */
135 struct fd *fd; /* pseudo-fd for ioctls */
136 struct console_input *console; /* attached console */
137 int num_alloc; /* number of allocated events */
138 int num_used; /* number of actually used events */
139 struct condrv_renderer_event *events;
140 struct async_queue read_q; /* read queue */
143 static const struct object_ops console_input_events_ops =
145 sizeof(struct console_input_events), /* size */
146 console_input_events_dump, /* dump */
147 no_get_type, /* get_type */
148 add_queue, /* add_queue */
149 remove_queue, /* remove_queue */
150 NULL, /* signaled */
151 no_satisfied, /* satisfied */
152 no_signal, /* signal */
153 console_input_events_get_fd, /* get_fd */
154 default_fd_map_access, /* map_access */
155 default_get_sd, /* get_sd */
156 default_set_sd, /* set_sd */
157 no_lookup_name, /* lookup_name */
158 no_link_name, /* link_name */
159 NULL, /* unlink_name */
160 console_input_events_open_file, /* open_file */
161 no_kernel_obj_list, /* get_kernel_obj_list */
162 no_close_handle, /* close_handle */
163 console_input_events_destroy /* destroy */
166 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
168 static const struct fd_ops console_input_events_fd_ops =
170 default_fd_get_poll_events, /* get_poll_events */
171 default_poll_event, /* poll_event */
172 console_get_fd_type, /* get_fd_type */
173 no_fd_read, /* read */
174 no_fd_write, /* write */
175 no_fd_flush, /* flush */
176 no_fd_get_file_info, /* get_file_info */
177 no_fd_get_volume_info, /* get_volume_info */
178 console_input_events_ioctl, /* ioctl */
179 default_fd_queue_async, /* queue_async */
180 default_fd_reselect_async /* reselect_async */
183 struct console_host_ioctl
185 unsigned int code; /* ioctl code */
186 struct async *async; /* ioctl async */
187 struct list entry; /* list entry */
190 struct console_server
192 struct object obj; /* object header */
193 struct console_input *console; /* attached console */
194 struct list queue; /* ioctl queue */
195 int busy; /* flag if server processing an ioctl */
198 static void console_server_dump( struct object *obj, int verbose );
199 static void console_server_destroy( struct object *obj );
200 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry );
201 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
202 static struct object *console_server_open_file( struct object *obj, unsigned int access,
203 unsigned int sharing, unsigned int options );
205 static const struct object_ops console_server_ops =
207 sizeof(struct console_server), /* size */
208 console_server_dump, /* dump */
209 no_get_type, /* get_type */
210 add_queue, /* add_queue */
211 remove_queue, /* remove_queue */
212 console_server_signaled, /* signaled */
213 no_satisfied, /* satisfied */
214 no_signal, /* signal */
215 no_get_fd, /* get_fd */
216 default_fd_map_access, /* map_access */
217 default_get_sd, /* get_sd */
218 default_set_sd, /* set_sd */
219 console_server_lookup_name, /* lookup_name */
220 no_link_name, /* link_name */
221 NULL, /* unlink_name */
222 console_server_open_file, /* open_file */
223 no_kernel_obj_list, /* get_kernel_obj_list */
224 fd_close_handle, /* close_handle */
225 console_server_destroy /* destroy */
228 struct font_info
230 short int width;
231 short int height;
232 short int weight;
233 short int pitch_family;
234 WCHAR *face_name;
235 data_size_t face_len;
238 struct screen_buffer
240 struct object obj; /* object header */
241 struct list entry; /* entry in list of all screen buffers */
242 struct console_input *input; /* associated console input */
243 unsigned int mode; /* output mode */
244 int cursor_size; /* size of cursor (percentage filled) */
245 int cursor_visible;/* cursor visibility flag */
246 int cursor_x; /* position of cursor */
247 int cursor_y; /* position of cursor */
248 int width; /* size (w-h) of the screen buffer */
249 int height;
250 int max_width; /* size (w-h) of the window given font size */
251 int max_height;
252 char_info_t *data; /* the data for each cell - a width x height matrix */
253 unsigned short attr; /* default fill attributes (screen colors) */
254 unsigned short popup_attr; /* pop-up color attributes */
255 unsigned int color_map[16]; /* color table */
256 rectangle_t win; /* current visible window on the screen buffer *
257 * as seen in wineconsole */
258 struct font_info font; /* console font information */
259 struct fd *fd; /* for bare console, attached output fd */
262 static void screen_buffer_dump( struct object *obj, int verbose );
263 static void screen_buffer_destroy( struct object *obj );
264 static struct fd *screen_buffer_get_fd( struct object *obj );
265 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
266 unsigned int sharing, unsigned int options );
268 static const struct object_ops screen_buffer_ops =
270 sizeof(struct screen_buffer), /* size */
271 screen_buffer_dump, /* dump */
272 no_get_type, /* get_type */
273 no_add_queue, /* add_queue */
274 NULL, /* remove_queue */
275 NULL, /* signaled */
276 NULL, /* satisfied */
277 no_signal, /* signal */
278 screen_buffer_get_fd, /* get_fd */
279 default_fd_map_access, /* map_access */
280 default_get_sd, /* get_sd */
281 default_set_sd, /* set_sd */
282 no_lookup_name, /* lookup_name */
283 no_link_name, /* link_name */
284 NULL, /* unlink_name */
285 screen_buffer_open_file, /* open_file */
286 no_kernel_obj_list, /* get_kernel_obj_list */
287 no_close_handle, /* close_handle */
288 screen_buffer_destroy /* destroy */
291 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
293 static const struct fd_ops screen_buffer_fd_ops =
295 default_fd_get_poll_events, /* get_poll_events */
296 default_poll_event, /* poll_event */
297 console_get_fd_type, /* get_fd_type */
298 no_fd_read, /* read */
299 no_fd_write, /* write */
300 no_fd_flush, /* flush */
301 no_fd_get_file_info, /* get_file_info */
302 no_fd_get_volume_info, /* get_volume_info */
303 screen_buffer_ioctl, /* ioctl */
304 default_fd_queue_async, /* queue_async */
305 default_fd_reselect_async /* reselect_async */
308 static struct object_type *console_device_get_type( struct object *obj );
309 static void console_device_dump( struct object *obj, int verbose );
310 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
311 static struct object *console_device_open_file( struct object *obj, unsigned int access,
312 unsigned int sharing, unsigned int options );
314 static const struct object_ops console_device_ops =
316 sizeof(struct object), /* size */
317 console_device_dump, /* dump */
318 console_device_get_type, /* get_type */
319 no_add_queue, /* add_queue */
320 NULL, /* remove_queue */
321 NULL, /* signaled */
322 no_satisfied, /* satisfied */
323 no_signal, /* signal */
324 no_get_fd, /* get_fd */
325 default_fd_map_access, /* map_access */
326 default_get_sd, /* get_sd */
327 default_set_sd, /* set_sd */
328 console_device_lookup_name, /* lookup_name */
329 directory_link_name, /* link_name */
330 default_unlink_name, /* unlink_name */
331 console_device_open_file, /* open_file */
332 no_kernel_obj_list, /* get_kernel_obj_list */
333 no_close_handle, /* close_handle */
334 no_destroy /* destroy */
337 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
339 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
341 static int console_input_is_bare( struct console_input* cin )
343 return cin->evt == NULL;
346 static struct fd *console_input_get_fd( struct object* obj )
348 struct console_input *console_input = (struct console_input*)obj;
349 assert( obj->ops == &console_input_ops );
350 return (struct fd *)grab_object( console_input->fd );
353 static enum server_fd_type console_get_fd_type( struct fd *fd )
355 return FD_TYPE_CHAR;
358 /* dumps the renderer events of a console */
359 static void console_input_events_dump( struct object *obj, int verbose )
361 struct console_input_events *evts = (struct console_input_events *)obj;
362 assert( obj->ops == &console_input_events_ops );
363 fprintf( stderr, "Console input events: %d/%d events\n",
364 evts->num_used, evts->num_alloc );
367 /* destroys the renderer events of a console */
368 static void console_input_events_destroy( struct object *obj )
370 struct console_input_events *evts = (struct console_input_events *)obj;
371 assert( obj->ops == &console_input_events_ops );
372 if (evts->console) evts->console->evt = NULL;
373 free_async_queue( &evts->read_q );
374 if (evts->fd) release_object( evts->fd );
375 free( evts->events );
378 static struct fd *console_input_events_get_fd( struct object* obj )
380 struct console_input_events *evts = (struct console_input_events*)obj;
381 assert( obj->ops == &console_input_events_ops );
382 return (struct fd*)grab_object( evts->fd );
385 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
386 unsigned int sharing, unsigned int options )
388 return grab_object( obj );
391 /* retrieves events from the console's renderer events list */
392 static int get_renderer_events( struct console_input_events* evts, struct async *async )
394 struct iosb *iosb = async_get_iosb( async );
395 data_size_t num;
397 num = min( iosb->out_size / sizeof(evts->events[0]), evts->num_used );
398 if (num && !(iosb->out_data = malloc( num * sizeof(evts->events[0] ))))
400 async_terminate( async, STATUS_NO_MEMORY );
401 release_object( iosb );
402 return 0;
405 iosb->status = STATUS_SUCCESS;
406 iosb->out_size = iosb->result = num * sizeof(evts->events[0]);
407 if (num) memcpy( iosb->out_data, evts->events, iosb->result );
408 release_object( iosb );
409 async_terminate( async, STATUS_ALERTED );
411 if (num && num < evts->num_used)
413 memmove( &evts->events[0], &evts->events[num],
414 (evts->num_used - num) * sizeof(evts->events[0]) );
416 evts->num_used -= num;
417 return 1;
420 /* add an event to the console's renderer events list */
421 static void console_input_events_append( struct console_input* console,
422 struct condrv_renderer_event* evt)
424 struct console_input_events* evts;
425 int collapsed = FALSE;
426 struct async *async;
428 if (!(evts = console->evt)) return;
429 /* to be done even when evt has been generated by the renderer ? */
431 /* try to collapse evt into current queue's events */
432 if (evts->num_used)
434 struct condrv_renderer_event* last = &evts->events[evts->num_used - 1];
436 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
437 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
439 /* if two update events overlap, collapse them into a single one */
440 if (last->u.update.bottom + 1 >= evt->u.update.top &&
441 evt->u.update.bottom + 1 >= last->u.update.top)
443 last->u.update.top = min(last->u.update.top, evt->u.update.top);
444 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
445 collapsed = TRUE;
449 if (!collapsed)
451 if (evts->num_used == evts->num_alloc)
453 evts->num_alloc += 16;
454 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
455 assert(evts->events);
457 evts->events[evts->num_used++] = *evt;
459 while (evts->num_used && (async = find_pending_async( &evts->read_q )))
461 get_renderer_events( evts, async );
462 release_object( async );
466 static struct object *create_console_input_events(void)
468 struct console_input_events* evt;
470 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
471 evt->console = NULL;
472 evt->num_alloc = evt->num_used = 0;
473 evt->events = NULL;
474 init_async_queue( &evt->read_q );
475 if (!(evt->fd = alloc_pseudo_fd( &console_input_events_fd_ops, &evt->obj, 0 )))
477 release_object( evt );
478 return NULL;
480 return &evt->obj;
483 static struct object *create_console_input( int fd )
485 struct console_input *console_input;
487 if (!(console_input = alloc_object( &console_input_ops )))
489 if (fd != -1) close( fd );
490 return NULL;
492 console_input->renderer = NULL;
493 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
494 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
495 ENABLE_EXTENDED_FLAGS;
496 console_input->num_proc = 0;
497 console_input->active = NULL;
498 console_input->recnum = 0;
499 console_input->records = NULL;
500 console_input->evt = NULL;
501 console_input->server = NULL;
502 console_input->title = NULL;
503 console_input->title_len = 0;
504 console_input->history_size = 50;
505 console_input->history = calloc( console_input->history_size, sizeof(*console_input->history) );
506 console_input->history_index = 0;
507 console_input->history_mode = 0;
508 console_input->edition_mode = 0;
509 console_input->input_cp = 0;
510 console_input->output_cp = 0;
511 console_input->win = 0;
512 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
513 console_input->fd = NULL;
514 init_async_queue( &console_input->ioctl_q );
515 init_async_queue( &console_input->read_q );
517 if (!console_input->history || !console_input->event)
519 if (fd != -1) close( fd );
520 console_input->history_size = 0;
521 release_object( console_input );
522 return NULL;
524 if (fd != -1) /* bare console */
526 console_input->fd = create_anonymous_fd( &console_input_fd_ops, fd, &console_input->obj,
527 FILE_SYNCHRONOUS_IO_NONALERT );
529 else
531 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
532 FILE_SYNCHRONOUS_IO_NONALERT );
534 if (!console_input->fd)
536 release_object( console_input );
537 return NULL;
539 allow_fd_caching( console_input->fd );
540 return &console_input->obj;
543 static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
545 if (call->async)
547 async_terminate( call->async, status );
548 release_object( call->async );
550 free( call );
553 static int queue_host_ioctl( struct console_server *server, unsigned int code,
554 struct async *async, struct async_queue *queue )
556 struct console_host_ioctl *ioctl;
558 if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
559 ioctl->code = code;
560 ioctl->async = NULL;
561 if (async)
563 ioctl->async = (struct async *)grab_object( async );
564 queue_async( queue, async );
566 list_add_tail( &server->queue, &ioctl->entry );
567 wake_up( &server->obj, 0 );
568 if (async) set_error( STATUS_PENDING );
569 return 0;
572 static void disconnect_console_server( struct console_server *server )
574 while (!list_empty( &server->queue ))
576 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
577 list_remove( &call->entry );
578 console_host_ioctl_terminate( call, STATUS_CANCELLED );
581 if (server->console)
583 assert( server->console->server == server );
584 server->console->server = NULL;
585 server->console = NULL;
586 wake_up( &server->obj, 0 );
590 static void generate_sb_initial_events( struct console_input *console_input )
592 struct screen_buffer *screen_buffer = console_input->active;
593 struct condrv_renderer_event evt;
595 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
596 evt.u.resize.width = screen_buffer->width;
597 evt.u.resize.height = screen_buffer->height;
598 console_input_events_append( console_input, &evt );
600 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
601 evt.u.display.left = screen_buffer->win.left;
602 evt.u.display.top = screen_buffer->win.top;
603 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
604 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
605 console_input_events_append( console_input, &evt );
607 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
608 evt.u.update.top = 0;
609 evt.u.update.bottom = screen_buffer->height - 1;
610 console_input_events_append( console_input, &evt );
612 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
613 evt.u.cursor_geom.size = screen_buffer->cursor_size;
614 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
615 console_input_events_append( console_input, &evt );
617 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
618 evt.u.cursor_pos.x = screen_buffer->cursor_x;
619 evt.u.cursor_pos.y = screen_buffer->cursor_y;
620 console_input_events_append( console_input, &evt );
623 static struct object *create_console_output( struct console_input *console_input, int fd )
625 struct screen_buffer *screen_buffer;
626 int i;
628 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
630 if (fd != -1) close( fd );
631 return NULL;
633 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
634 screen_buffer->input = console_input;
635 screen_buffer->cursor_size = 100;
636 screen_buffer->cursor_visible = 1;
637 screen_buffer->width = 80;
638 screen_buffer->height = 150;
639 screen_buffer->max_width = 80;
640 screen_buffer->max_height = 25;
641 screen_buffer->cursor_x = 0;
642 screen_buffer->cursor_y = 0;
643 screen_buffer->attr = 0x0F;
644 screen_buffer->popup_attr = 0xF5;
645 screen_buffer->win.left = 0;
646 screen_buffer->win.right = screen_buffer->max_width - 1;
647 screen_buffer->win.top = 0;
648 screen_buffer->win.bottom = screen_buffer->max_height - 1;
649 screen_buffer->data = NULL;
650 screen_buffer->font.width = 0;
651 screen_buffer->font.height = 0;
652 screen_buffer->font.weight = FW_NORMAL;
653 screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
654 screen_buffer->font.face_name = NULL;
655 screen_buffer->font.face_len = 0;
656 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
657 list_add_head( &screen_buffer_list, &screen_buffer->entry );
659 if (fd != -1)
660 screen_buffer->fd = create_anonymous_fd( &screen_buffer_fd_ops, fd, &screen_buffer->obj,
661 FILE_SYNCHRONOUS_IO_NONALERT );
662 else
663 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
664 FILE_SYNCHRONOUS_IO_NONALERT );
665 if (!screen_buffer->fd)
667 release_object( screen_buffer );
668 return NULL;
670 allow_fd_caching(screen_buffer->fd);
672 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
673 sizeof(*screen_buffer->data) )))
675 release_object( screen_buffer );
676 return NULL;
678 /* clear the first row */
679 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
680 /* and copy it to all other rows */
681 for (i = 1; i < screen_buffer->height; i++)
682 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
683 screen_buffer->width * sizeof(char_info_t) );
685 if (!console_input->active)
687 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
688 generate_sb_initial_events( console_input );
690 return &screen_buffer->obj;
693 /* free the console for this process */
694 int free_console( struct process *process )
696 struct console_input* console = process->console;
698 if (!console) return 0;
700 process->console = NULL;
701 if (--console->num_proc == 0 && console->renderer)
703 /* all processes have terminated... tell the renderer to terminate too */
704 struct condrv_renderer_event evt;
705 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
706 memset(&evt.u, 0, sizeof(evt.u));
707 console_input_events_append( console, &evt );
709 release_object( console );
711 return 1;
714 /* let process inherit the console from parent... this handle two cases :
715 * 1/ generic console inheritance
716 * 2/ parent is a renderer which launches process, and process should attach to the console
717 * rendered by parent
719 obj_handle_t inherit_console( struct thread *parent_thread, obj_handle_t handle, struct process *process,
720 obj_handle_t hconin )
722 struct console_input *console = NULL;
724 if (handle && !(console = (struct console_input *)get_handle_obj( current->process, handle, 0,
725 &console_input_ops )))
726 return 0;
728 /* if parent is a renderer, then attach current process to its console
729 * a bit hacky....
731 if (!console && hconin && parent_thread)
733 /* FIXME: should we check some access rights ? */
734 if (!(console = (struct console_input *)get_handle_obj( parent_thread->process, hconin,
735 0, &console_input_ops )))
736 clear_error(); /* ignore error */
738 if (!console) return 0;
740 process->console = console;
741 console->num_proc++;
742 return alloc_handle( process, process->console,
743 SYNCHRONIZE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, 0 );
746 struct thread *console_get_renderer( struct console_input *console )
748 return console->renderer;
751 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
753 struct console_input* console = NULL;
755 if (handle)
756 console = (struct console_input *)get_handle_obj( current->process, handle,
757 access, &console_input_ops );
758 else if (current->process->console)
760 console = (struct console_input *)grab_object( current->process->console );
763 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
764 return console;
767 struct console_signal_info
769 struct console_input *console;
770 process_id_t group;
771 int signal;
774 static int propagate_console_signal_cb(struct process *process, void *user)
776 struct console_signal_info* csi = (struct console_signal_info*)user;
778 if (process->console == csi->console && process->running_threads &&
779 (!csi->group || process->group_id == csi->group))
781 /* find a suitable thread to signal */
782 struct thread *thread;
783 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
785 if (send_thread_signal( thread, csi->signal )) break;
788 return FALSE;
791 static void propagate_console_signal( struct console_input *console,
792 int sig, process_id_t group_id )
794 struct console_signal_info csi;
796 if (!console)
798 set_error( STATUS_INVALID_PARAMETER );
799 return;
801 /* FIXME: should support the other events (like CTRL_BREAK) */
802 if (sig != CTRL_C_EVENT)
804 set_error( STATUS_NOT_IMPLEMENTED );
805 return;
807 csi.console = console;
808 csi.signal = SIGINT;
809 csi.group = group_id;
811 enum_processes(propagate_console_signal_cb, &csi);
814 /* retrieve a pointer to the console input records */
815 static int read_console_input( struct console_input *console, struct async *async, int flush )
817 struct iosb *iosb = async_get_iosb( async );
818 data_size_t count;
820 count = min( iosb->out_size / sizeof(INPUT_RECORD), console->recnum );
821 if (count)
823 if (!(iosb->out_data = malloc( count * sizeof(INPUT_RECORD) )))
825 set_error( STATUS_NO_MEMORY );
826 release_object( iosb );
827 return 0;
829 iosb->out_size = iosb->result = count * sizeof(INPUT_RECORD);
830 memcpy( iosb->out_data, console->records, iosb->result );
831 iosb->status = STATUS_SUCCESS;
832 async_terminate( async, STATUS_ALERTED );
834 else
836 async_terminate( async, STATUS_SUCCESS );
839 release_object( iosb );
841 if (flush && count)
843 if (console->recnum > count)
845 INPUT_RECORD *new_rec;
846 memmove( console->records, console->records + count, (console->recnum - count) * sizeof(*console->records) );
847 console->recnum -= count;
848 new_rec = realloc( console->records, console->recnum * sizeof(*console->records) );
849 if (new_rec) console->records = new_rec;
851 else
853 console->recnum = 0;
854 free( console->records );
855 console->records = NULL;
856 reset_event( console->event );
860 return 1;
863 /* add input events to a console input queue */
864 static int write_console_input( struct console_input* console, int count,
865 const INPUT_RECORD *records )
867 INPUT_RECORD *new_rec;
868 struct async *async;
870 if (!count) return 1;
871 if (!(new_rec = realloc( console->records,
872 (console->recnum + count) * sizeof(INPUT_RECORD) )))
874 set_error( STATUS_NO_MEMORY );
875 return 0;
877 console->records = new_rec;
878 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
880 if (console->mode & ENABLE_PROCESSED_INPUT)
882 int i = 0;
883 while (i < count)
885 if (records[i].EventType == KEY_EVENT &&
886 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
887 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
889 if (i != count - 1)
890 memcpy( &console->records[console->recnum + i],
891 &console->records[console->recnum + i + 1],
892 (count - i - 1) * sizeof(INPUT_RECORD) );
893 count--;
894 if (records[i].Event.KeyEvent.bKeyDown)
896 /* send SIGINT to all processes attached to this console */
897 propagate_console_signal( console, CTRL_C_EVENT, 0 );
900 else i++;
903 console->recnum += count;
904 while (console->recnum && (async = find_pending_async( &console->read_q )))
906 read_console_input( console, async, 1 );
907 release_object( async );
909 if (console->recnum) set_event( console->event );
910 return 1;
913 /* resize a screen buffer */
914 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
915 int new_width, int new_height )
917 int i, old_width, old_height, copy_width, copy_height;
918 char_info_t *new_data;
920 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
922 set_error( STATUS_NO_MEMORY );
923 return 0;
925 old_width = screen_buffer->width;
926 old_height = screen_buffer->height;
927 copy_width = min( old_width, new_width );
928 copy_height = min( old_height, new_height );
930 /* copy all the rows */
931 for (i = 0; i < copy_height; i++)
933 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
934 copy_width * sizeof(char_info_t) );
937 /* clear the end of each row */
938 if (new_width > old_width)
940 /* fill first row */
941 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
942 /* and blast it to the other rows */
943 for (i = 1; i < copy_height; i++)
944 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
945 (new_width - old_width) * sizeof(char_info_t) );
948 /* clear remaining rows */
949 if (new_height > old_height)
951 /* fill first row */
952 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
953 /* and blast it to the other rows */
954 for (i = old_height+1; i < new_height; i++)
955 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
956 new_width * sizeof(char_info_t) );
958 free( screen_buffer->data );
959 screen_buffer->data = new_data;
960 screen_buffer->width = new_width;
961 screen_buffer->height = new_height;
962 return 1;
965 static int set_output_info( struct screen_buffer *screen_buffer,
966 const struct condrv_output_info_params *params, data_size_t extra_size )
968 const struct condrv_output_info *info = &params->info;
969 struct condrv_renderer_event evt;
970 WCHAR *font_name;
972 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
974 if (info->cursor_size < 1 || info->cursor_size > 100)
976 set_error( STATUS_INVALID_PARAMETER );
977 return 0;
979 if (screen_buffer->cursor_size != info->cursor_size ||
980 screen_buffer->cursor_visible != info->cursor_visible)
982 screen_buffer->cursor_size = info->cursor_size;
983 screen_buffer->cursor_visible = info->cursor_visible;
984 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
985 memset( &evt.u, 0, sizeof(evt.u) );
986 evt.u.cursor_geom.size = info->cursor_size;
987 evt.u.cursor_geom.visible = info->cursor_visible;
988 console_input_events_append( screen_buffer->input, &evt );
991 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
993 if (info->cursor_x < 0 || info->cursor_x >= screen_buffer->width ||
994 info->cursor_y < 0 || info->cursor_y >= screen_buffer->height)
996 set_error( STATUS_INVALID_PARAMETER );
997 return 0;
999 if (screen_buffer->cursor_x != info->cursor_x || screen_buffer->cursor_y != info->cursor_y)
1001 screen_buffer->cursor_x = info->cursor_x;
1002 screen_buffer->cursor_y = info->cursor_y;
1003 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1004 memset( &evt.u, 0, sizeof(evt.u) );
1005 evt.u.cursor_pos.x = info->cursor_x;
1006 evt.u.cursor_pos.y = info->cursor_y;
1007 console_input_events_append( screen_buffer->input, &evt );
1010 if (params->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
1012 unsigned cc;
1014 /* new screen-buffer cannot be smaller than actual window */
1015 if (info->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
1016 info->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
1018 set_error( STATUS_INVALID_PARAMETER );
1019 return 0;
1021 /* FIXME: there are also some basic minimum and max size to deal with */
1022 if (!change_screen_buffer_size( screen_buffer, info->width, info->height )) return 0;
1024 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
1025 memset( &evt.u, 0, sizeof(evt.u) );
1026 evt.u.resize.width = info->width;
1027 evt.u.resize.height = info->height;
1028 console_input_events_append( screen_buffer->input, &evt );
1030 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1031 memset( &evt.u, 0, sizeof(evt.u) );
1032 evt.u.update.top = 0;
1033 evt.u.update.bottom = screen_buffer->height - 1;
1034 console_input_events_append( screen_buffer->input, &evt );
1036 /* scroll window to display sb */
1037 if (screen_buffer->win.right >= info->width)
1039 screen_buffer->win.right -= screen_buffer->win.left;
1040 screen_buffer->win.left = 0;
1042 if (screen_buffer->win.bottom >= info->height)
1044 screen_buffer->win.bottom -= screen_buffer->win.top;
1045 screen_buffer->win.top = 0;
1047 /* reset cursor if needed (normally, if cursor was outside of new sb, the
1048 * window has been shifted so that the new position of the cursor will be
1049 * visible */
1050 cc = 0;
1051 if (screen_buffer->cursor_x >= info->width)
1053 screen_buffer->cursor_x = info->width - 1;
1054 cc++;
1056 if (screen_buffer->cursor_y >= info->height)
1058 screen_buffer->cursor_y = info->height - 1;
1059 cc++;
1061 if (cc)
1063 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1064 memset( &evt.u, 0, sizeof(evt.u) );
1065 evt.u.cursor_pos.x = info->cursor_x;
1066 evt.u.cursor_pos.y = info->cursor_y;
1067 console_input_events_append( screen_buffer->input, &evt );
1070 if (screen_buffer == screen_buffer->input->active &&
1071 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
1073 INPUT_RECORD ir;
1074 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
1075 ir.Event.WindowBufferSizeEvent.dwSize.X = info->width;
1076 ir.Event.WindowBufferSizeEvent.dwSize.Y = info->height;
1077 write_console_input( screen_buffer->input, 1, &ir );
1080 if (params->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1082 screen_buffer->attr = info->attr;
1084 if (params->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1086 screen_buffer->popup_attr = info->popup_attr;
1088 if (params->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1090 if (info->win_left < 0 || info->win_left > info->win_right ||
1091 info->win_right >= screen_buffer->width ||
1092 info->win_top < 0 || info->win_top > info->win_bottom ||
1093 info->win_bottom >= screen_buffer->height)
1095 set_error( STATUS_INVALID_PARAMETER );
1096 return 0;
1098 if (screen_buffer->win.left != info->win_left || screen_buffer->win.top != info->win_top ||
1099 screen_buffer->win.right != info->win_right || screen_buffer->win.bottom != info->win_bottom)
1101 screen_buffer->win.left = info->win_left;
1102 screen_buffer->win.top = info->win_top;
1103 screen_buffer->win.right = info->win_right;
1104 screen_buffer->win.bottom = info->win_bottom;
1105 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1106 memset( &evt.u, 0, sizeof(evt.u) );
1107 evt.u.display.left = info->win_left;
1108 evt.u.display.top = info->win_top;
1109 evt.u.display.width = info->win_right - info->win_left + 1;
1110 evt.u.display.height = info->win_bottom - info->win_top + 1;
1111 console_input_events_append( screen_buffer->input, &evt );
1114 if (params->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1116 screen_buffer->max_width = info->max_width;
1117 screen_buffer->max_height = info->max_height;
1119 if (params->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1121 memcpy( screen_buffer->color_map, info->color_map, sizeof(info->color_map) );
1123 if (params->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1125 screen_buffer->font.width = info->font_width;
1126 screen_buffer->font.height = info->font_height;
1127 screen_buffer->font.weight = info->font_weight;
1128 screen_buffer->font.pitch_family = info->font_pitch_family;
1129 if (extra_size)
1131 extra_size = extra_size / sizeof(WCHAR) * sizeof(WCHAR);
1132 font_name = mem_alloc( extra_size );
1133 if (font_name)
1135 memcpy( font_name, info + 1, extra_size );
1136 free( screen_buffer->font.face_name );
1137 screen_buffer->font.face_name = font_name;
1138 screen_buffer->font.face_len = extra_size;
1143 return 1;
1146 /* appends a new line to history (history is a fixed size array) */
1147 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1149 struct history_line *ptr;
1151 if (!console || !console->history_size)
1153 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1154 return;
1157 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
1158 if (console->history_mode && console->history_index &&
1159 console->history[console->history_index - 1]->len == len &&
1160 !memcmp( console->history[console->history_index - 1]->text, buf, len ))
1162 /* don't duplicate entry */
1163 set_error( STATUS_ALIAS_EXISTS );
1164 return;
1166 if (!(ptr = mem_alloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
1167 ptr->len = len;
1168 memcpy( ptr->text, buf, len );
1170 if (console->history_index < console->history_size)
1172 console->history[console->history_index++] = ptr;
1174 else
1176 free( console->history[0]) ;
1177 memmove( &console->history[0], &console->history[1],
1178 (console->history_size - 1) * sizeof(*console->history) );
1179 console->history[console->history_size - 1] = ptr;
1183 /* returns a line from the cache */
1184 static data_size_t console_input_get_hist( struct console_input *console, int index )
1186 data_size_t ret = 0;
1188 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1189 else
1191 ret = console->history[index]->len;
1192 set_reply_data( console->history[index]->text, min( ret, get_reply_max_size() ));
1194 return ret;
1197 /* dumb dump */
1198 static void console_input_dump( struct object *obj, int verbose )
1200 struct console_input *console = (struct console_input *)obj;
1201 assert( obj->ops == &console_input_ops );
1202 fprintf( stderr, "Console input active=%p evt=%p\n",
1203 console->active, console->evt );
1206 static void console_input_destroy( struct object *obj )
1208 struct console_input* console_in = (struct console_input *)obj;
1209 struct screen_buffer* curr;
1210 int i;
1212 assert( obj->ops == &console_input_ops );
1214 if (console_in->server)
1216 assert( console_in->server->console == console_in );
1217 disconnect_console_server( console_in->server );
1220 free( console_in->title );
1221 free( console_in->records );
1223 if (console_in->active) release_object( console_in->active );
1224 console_in->active = NULL;
1226 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1228 if (curr->input == console_in) curr->input = NULL;
1231 free_async_queue( &console_in->ioctl_q );
1232 free_async_queue( &console_in->read_q );
1233 if (console_in->evt)
1234 console_in->evt->console = NULL;
1235 if (console_in->event)
1236 release_object( console_in->event );
1237 if (console_in->fd)
1238 release_object( console_in->fd );
1240 for (i = 0; i < console_in->history_size; i++)
1241 free( console_in->history[i] );
1242 free( console_in->history );
1245 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1246 unsigned int sharing, unsigned int options )
1248 return grab_object( obj );
1251 static void screen_buffer_dump( struct object *obj, int verbose )
1253 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1254 assert( obj->ops == &screen_buffer_ops );
1256 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1259 static void screen_buffer_destroy( struct object *obj )
1261 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1263 assert( obj->ops == &screen_buffer_ops );
1265 list_remove( &screen_buffer->entry );
1267 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1269 struct screen_buffer *sb;
1271 screen_buffer->input->active = NULL;
1272 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1274 if (sb->input == screen_buffer->input)
1276 sb->input->active = sb;
1277 break;
1281 if (screen_buffer->fd) release_object( screen_buffer->fd );
1282 free( screen_buffer->data );
1283 free( screen_buffer->font.face_name );
1286 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
1287 unsigned int sharing, unsigned int options )
1289 return grab_object( obj );
1292 static struct fd *screen_buffer_get_fd( struct object *obj )
1294 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1295 assert( obj->ops == &screen_buffer_ops );
1296 if (screen_buffer->fd)
1297 return (struct fd*)grab_object( screen_buffer->fd );
1298 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1299 return NULL;
1302 /* read data from a screen buffer */
1303 static void read_console_output( struct screen_buffer *screen_buffer, unsigned int x, unsigned int y,
1304 enum char_info_mode mode, unsigned int width )
1306 unsigned int i, count;
1307 char_info_t *src;
1309 if (x >= screen_buffer->width || y >= screen_buffer->height)
1311 if (width) set_error( STATUS_INVALID_PARAMETER );
1312 return;
1314 src = screen_buffer->data + y * screen_buffer->width + x;
1316 switch(mode)
1318 case CHAR_INFO_MODE_TEXT:
1320 WCHAR *data;
1321 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1322 get_reply_max_size() / sizeof(*data) );
1323 if ((data = set_reply_data_size( count * sizeof(*data) )))
1325 for (i = 0; i < count; i++) data[i] = src[i].ch;
1328 break;
1329 case CHAR_INFO_MODE_ATTR:
1331 unsigned short *data;
1332 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1333 get_reply_max_size() / sizeof(*data) );
1334 if ((data = set_reply_data_size( count * sizeof(*data) )))
1336 for (i = 0; i < count; i++) data[i] = src[i].attr;
1339 break;
1340 case CHAR_INFO_MODE_TEXTATTR:
1342 char_info_t *data;
1343 SMALL_RECT *region;
1344 if (!width || get_reply_max_size() < sizeof(*region))
1346 set_error( STATUS_INVALID_PARAMETER );
1347 return;
1349 count = min( (get_reply_max_size() - sizeof(*region)) / (width * sizeof(*data)), screen_buffer->height - y );
1350 width = min( width, screen_buffer->width - x );
1351 if (!(region = set_reply_data_size( sizeof(*region) + width * count * sizeof(*data) ))) return;
1352 region->Left = x;
1353 region->Top = y;
1354 region->Right = x + width - 1;
1355 region->Bottom = y + count - 1;
1356 data = (char_info_t *)(region + 1);
1357 for (i = 0; i < count; i++)
1359 memcpy( &data[i * width], &src[i * screen_buffer->width], width * sizeof(*data) );
1362 break;
1363 default:
1364 set_error( STATUS_INVALID_PARAMETER );
1365 break;
1369 /* write data into a screen buffer */
1370 static void write_console_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
1371 data_size_t size )
1373 unsigned int i, entry_size, entry_cnt, x, y;
1374 char_info_t *dest;
1375 char *src;
1377 entry_size = params->mode == CHAR_INFO_MODE_TEXTATTR ? sizeof(char_info_t) : sizeof(WCHAR);
1378 if (size % entry_size)
1380 set_error( STATUS_INVALID_PARAMETER );
1381 return;
1383 if (params->x >= screen_buffer->width) return;
1384 entry_cnt = size / entry_size;
1386 for (i = 0, src = (char *)(params + 1); i < entry_cnt; i++, src += entry_size)
1388 if (params->width)
1390 x = params->x + i % params->width;
1391 y = params->y + i / params->width;
1392 if (x >= screen_buffer->width) continue;
1394 else
1396 x = (params->x + i) % screen_buffer->width;
1397 y = params->y + (params->x + i) / screen_buffer->width;
1399 if (y >= screen_buffer->height) break;
1401 dest = &screen_buffer->data[y * screen_buffer->width + x];
1402 switch(params->mode)
1404 case CHAR_INFO_MODE_TEXT:
1405 dest->ch = *(const WCHAR *)src;
1406 break;
1407 case CHAR_INFO_MODE_ATTR:
1408 dest->attr = *(const unsigned short *)src;
1409 break;
1410 case CHAR_INFO_MODE_TEXTATTR:
1411 *dest = *(const char_info_t *)src;
1412 break;
1413 case CHAR_INFO_MODE_TEXTSTDATTR:
1414 dest->ch = *(const WCHAR *)src;
1415 dest->attr = screen_buffer->attr;
1416 break;
1417 default:
1418 set_error( STATUS_INVALID_PARAMETER );
1419 return;
1423 if (i && screen_buffer == screen_buffer->input->active)
1425 struct condrv_renderer_event evt;
1426 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1427 memset(&evt.u, 0, sizeof(evt.u));
1428 evt.u.update.top = params->y;
1429 evt.u.update.bottom = params->width
1430 ? min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1
1431 : params->y + (params->x + i - 1) / screen_buffer->width;
1432 console_input_events_append( screen_buffer->input, &evt );
1435 if (get_reply_max_size() == sizeof(SMALL_RECT))
1437 SMALL_RECT region;
1438 region.Left = params->x;
1439 region.Top = params->y;
1440 region.Right = min( params->x + params->width, screen_buffer->width ) - 1;
1441 region.Bottom = min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1;
1442 set_reply_data( &region, sizeof(region) );
1444 else set_reply_data( &i, sizeof(i) );
1447 /* fill a screen buffer with uniform data */
1448 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1449 enum char_info_mode mode, int x, int y, int count, int wrap )
1451 int i;
1452 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1454 if (y >= screen_buffer->height) return 0;
1456 if (wrap)
1457 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1458 else
1459 end = screen_buffer->data + (y+1) * screen_buffer->width;
1461 if (count > end - dest) count = end - dest;
1463 switch(mode)
1465 case CHAR_INFO_MODE_TEXT:
1466 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1467 break;
1468 case CHAR_INFO_MODE_ATTR:
1469 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1470 break;
1471 case CHAR_INFO_MODE_TEXTATTR:
1472 for (i = 0; i < count; i++) dest[i] = data;
1473 break;
1474 case CHAR_INFO_MODE_TEXTSTDATTR:
1475 for (i = 0; i < count; i++)
1477 dest[i].ch = data.ch;
1478 dest[i].attr = screen_buffer->attr;
1480 break;
1481 default:
1482 set_error( STATUS_INVALID_PARAMETER );
1483 return 0;
1486 if (count && screen_buffer == screen_buffer->input->active)
1488 struct condrv_renderer_event evt;
1489 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1490 memset(&evt.u, 0, sizeof(evt.u));
1491 evt.u.update.top = y;
1492 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1493 console_input_events_append( screen_buffer->input, &evt );
1495 return i;
1498 /* scroll parts of a screen buffer */
1499 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1500 int w, int h, const rectangle_t *clip, char_info_t fill )
1502 struct condrv_renderer_event evt;
1503 rectangle_t src, dst;
1504 int x, y;
1506 src.left = max( xsrc, clip->left );
1507 src.top = max( ysrc, clip->top );
1508 src.right = min( xsrc + w - 1, clip->right );
1509 src.bottom = min( ysrc + h - 1, clip->bottom );
1511 dst.left = xdst;
1512 dst.top = ydst;
1513 dst.right = xdst + w - 1;
1514 dst.bottom = ydst + h - 1;
1516 if (dst.left < clip->left)
1518 xsrc += clip->left - dst.left;
1519 w -= clip->left - dst.left;
1520 dst.left = clip->left;
1522 if (dst.top < clip->top)
1524 ysrc += clip->top - dst.top;
1525 h -= clip->top - dst.top;
1526 dst.top = clip->top;
1528 if (dst.right > clip->right) w -= dst.right - clip->right;
1529 if (dst.bottom > clip->bottom) h -= dst.bottom - clip->bottom;
1531 if (w > 0 && h > 0)
1533 if (ysrc < ydst)
1535 for (y = h; y > 0; y--)
1537 memcpy( &screen_buffer->data[(dst.top + y - 1) * screen_buffer->width + dst.left],
1538 &screen_buffer->data[(ysrc + y - 1) * screen_buffer->width + xsrc],
1539 w * sizeof(screen_buffer->data[0]) );
1542 else
1544 for (y = 0; y < h; y++)
1546 /* we use memmove here because when psrc and pdst are the same,
1547 * copies are done on the same row, so the dst and src blocks
1548 * can overlap */
1549 memmove( &screen_buffer->data[(dst.top + y) * screen_buffer->width + dst.left],
1550 &screen_buffer->data[(ysrc + y) * screen_buffer->width + xsrc],
1551 w * sizeof(screen_buffer->data[0]) );
1556 for (y = src.top; y <= src.bottom; y++)
1558 int left = src.left;
1559 int right = src.right;
1560 if (dst.top <= y && y <= dst.bottom)
1562 if (dst.left <= src.left) left = max( left, dst.right + 1 );
1563 if (dst.left >= src.left) right = min( right, dst.left - 1 );
1565 for (x = left; x <= right; x++) screen_buffer->data[y * screen_buffer->width + x] = fill;
1568 /* FIXME: this could be enhanced, by signalling scroll */
1569 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1570 memset(&evt.u, 0, sizeof(evt.u));
1571 evt.u.update.top = min( src.top, dst.top );
1572 evt.u.update.bottom = max( src.bottom, dst.bottom );
1573 console_input_events_append( screen_buffer->input, &evt );
1576 static void console_server_dump( struct object *obj, int verbose )
1578 assert( obj->ops == &console_server_ops );
1579 fprintf( stderr, "Console server\n" );
1582 static void console_server_destroy( struct object *obj )
1584 struct console_server *server = (struct console_server *)obj;
1585 assert( obj->ops == &console_server_ops );
1586 disconnect_console_server( server );
1589 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1591 struct console_server *server = (struct console_server*)obj;
1592 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
1593 assert( obj->ops == &console_server_ops );
1595 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
1597 struct screen_buffer *screen_buffer;
1598 name->len = 0;
1599 if (server->console)
1601 set_error( STATUS_INVALID_HANDLE );
1602 return 0;
1604 if (!(server->console = (struct console_input *)create_console_input( -1 ))) return NULL;
1605 if (!(screen_buffer = (struct screen_buffer *)create_console_output( server->console, -1 )))
1607 release_object( server->console );
1608 server->console = NULL;
1609 return NULL;
1611 release_object( screen_buffer );
1612 server->console->server = server;
1614 return &server->console->obj;
1617 return NULL;
1620 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
1622 struct console_server *server = (struct console_server*)obj;
1623 assert( obj->ops == &console_server_ops );
1624 return !server->console || !list_empty( &server->queue );
1627 static struct object *console_server_open_file( struct object *obj, unsigned int access,
1628 unsigned int sharing, unsigned int options )
1630 return grab_object( obj );
1633 static struct object *create_console_server( void )
1635 struct console_server *server;
1637 if (!(server = alloc_object( &console_server_ops ))) return NULL;
1638 server->console = NULL;
1639 server->busy = 0;
1640 list_init( &server->queue );
1642 return &server->obj;
1645 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1647 struct console_input *console = get_fd_user( fd );
1649 switch (code)
1651 case IOCTL_CONDRV_GET_MODE:
1652 if (console->server)
1653 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1654 if (get_reply_max_size() != sizeof(console->mode))
1656 set_error( STATUS_INVALID_PARAMETER );
1657 return 0;
1659 return set_reply_data( &console->mode, sizeof(console->mode) ) != NULL;
1661 case IOCTL_CONDRV_SET_MODE:
1662 if (console->server)
1663 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1664 if (get_req_data_size() != sizeof(console->mode))
1666 set_error( STATUS_INVALID_PARAMETER );
1667 return 0;
1669 console->mode = *(unsigned int *)get_req_data();
1670 return 1;
1672 case IOCTL_CONDRV_READ_INPUT:
1674 int blocking = 0;
1675 if (console->server)
1676 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1677 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1679 set_error( STATUS_INVALID_PARAMETER );
1680 return 0;
1682 if (get_req_data_size())
1684 if (get_req_data_size() != sizeof(int))
1686 set_error( STATUS_INVALID_PARAMETER );
1687 return 0;
1689 blocking = *(int *)get_req_data();
1691 set_error( STATUS_PENDING );
1692 if (blocking && !console->recnum)
1694 queue_async( &console->read_q, async );
1695 return 1;
1697 return read_console_input( console, async, 1 );
1700 case IOCTL_CONDRV_WRITE_INPUT:
1701 if (console->server)
1702 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1703 return write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD), get_req_data() );
1705 case IOCTL_CONDRV_PEEK:
1706 if (console->server)
1707 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1708 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1710 set_error( STATUS_INVALID_PARAMETER );
1711 return 0;
1713 set_error( STATUS_PENDING );
1714 return read_console_input( console, async, 0 );
1716 case IOCTL_CONDRV_GET_INPUT_INFO:
1718 struct condrv_input_info info;
1719 if (console->server)
1720 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1721 if (get_reply_max_size() != sizeof(info))
1723 set_error( STATUS_INVALID_PARAMETER );
1724 return 0;
1726 info.input_cp = console->input_cp;
1727 info.output_cp = console->output_cp;
1728 info.history_mode = console->history_mode;
1729 info.history_size = console->history_size;
1730 info.history_index = console->history_index;
1731 info.edition_mode = console->edition_mode;
1732 info.input_count = console->recnum;
1733 info.win = console->win;
1734 return set_reply_data( &info, sizeof(info) ) != NULL;
1737 case IOCTL_CONDRV_SET_INPUT_INFO:
1739 const struct condrv_input_info_params *params = get_req_data();
1740 if (console->server)
1741 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1742 if (get_req_data_size() != sizeof(*params))
1744 set_error( STATUS_INVALID_PARAMETER );
1745 return 0;
1747 if (params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
1749 console->history_mode = params->info.history_mode;
1751 if ((params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
1752 console->history_size != params->info.history_size)
1754 struct history_line **mem = NULL;
1755 int i, delta;
1757 if (params->info.history_size)
1759 if (!(mem = mem_alloc( params->info.history_size * sizeof(*mem) ))) return 0;
1760 memset( mem, 0, params->info.history_size * sizeof(*mem) );
1763 delta = (console->history_index > params->info.history_size) ?
1764 (console->history_index - params->info.history_size) : 0;
1766 for (i = delta; i < console->history_index; i++)
1768 mem[i - delta] = console->history[i];
1769 console->history[i] = NULL;
1771 console->history_index -= delta;
1773 for (i = 0; i < console->history_size; i++)
1774 free( console->history[i] );
1775 free( console->history );
1776 console->history = mem;
1777 console->history_size = params->info.history_size;
1779 if (params->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
1781 console->edition_mode = params->info.edition_mode;
1783 if (params->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
1785 console->input_cp = params->info.input_cp;
1787 if (params->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
1789 console->output_cp = params->info.output_cp;
1791 if (params->mask & SET_CONSOLE_INPUT_INFO_WIN)
1793 console->win = params->info.win;
1795 return 1;
1798 case IOCTL_CONDRV_GET_TITLE:
1799 if (console->server)
1800 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1801 if (!console->title_len) return 1;
1802 return set_reply_data( console->title, min( console->title_len, get_reply_max_size() )) != NULL;
1804 case IOCTL_CONDRV_SET_TITLE:
1806 data_size_t len = get_req_data_size();
1807 struct condrv_renderer_event evt;
1808 WCHAR *title = NULL;
1810 if (console->server)
1811 return queue_host_ioctl( console->server, code, async, &console->ioctl_q );
1812 if (len % sizeof(WCHAR))
1814 set_error( STATUS_INVALID_PARAMETER );
1815 return 0;
1818 if (len && !(title = memdup( get_req_data(), len ))) return 0;
1819 free( console->title );
1820 console->title = title;
1821 console->title_len = len;
1822 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
1823 console_input_events_append( console, &evt );
1824 return 1;
1827 default:
1828 set_error( STATUS_INVALID_HANDLE );
1829 return 0;
1833 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1835 struct screen_buffer *screen_buffer = get_fd_user( fd );
1837 switch (code)
1839 case IOCTL_CONDRV_GET_MODE:
1840 if (get_reply_max_size() != sizeof(screen_buffer->mode))
1842 set_error( STATUS_INVALID_PARAMETER );
1843 return 0;
1845 return set_reply_data( &screen_buffer->mode, sizeof(screen_buffer->mode) ) != NULL;
1847 case IOCTL_CONDRV_SET_MODE:
1848 if (get_req_data_size() != sizeof(screen_buffer->mode))
1850 set_error( STATUS_INVALID_PARAMETER );
1851 return 0;
1853 screen_buffer->mode = *(unsigned int *)get_req_data();
1854 return 1;
1856 case IOCTL_CONDRV_READ_OUTPUT:
1858 const struct condrv_output_params *params = get_req_data();
1859 if (get_req_data_size() != sizeof(*params))
1861 set_error( STATUS_INVALID_PARAMETER );
1862 return 0;
1864 if (console_input_is_bare( screen_buffer->input ))
1866 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1867 return 0;
1869 read_console_output( screen_buffer, params->x, params->y, params->mode, params->width );
1870 return !get_error();
1873 case IOCTL_CONDRV_WRITE_OUTPUT:
1874 if (get_req_data_size() < sizeof(struct condrv_output_params) ||
1875 (get_reply_max_size() != sizeof(SMALL_RECT) && get_reply_max_size() != sizeof(unsigned int)))
1877 set_error( STATUS_INVALID_PARAMETER );
1878 return 0;
1880 if (console_input_is_bare( screen_buffer->input ))
1882 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1883 return 0;
1885 write_console_output( screen_buffer, get_req_data(), get_req_data_size() - sizeof(struct condrv_output_params) );
1886 return !get_error();
1888 case IOCTL_CONDRV_GET_OUTPUT_INFO:
1890 struct condrv_output_info *info;
1891 data_size_t size;
1893 size = min( sizeof(*info) + screen_buffer->font.face_len, get_reply_max_size() );
1894 if (size < sizeof(*info))
1896 set_error( STATUS_INVALID_PARAMETER );
1897 return 0;
1899 if (!(info = set_reply_data_size( size ))) return 0;
1901 info->cursor_size = screen_buffer->cursor_size;
1902 info->cursor_visible = screen_buffer->cursor_visible;
1903 info->cursor_x = screen_buffer->cursor_x;
1904 info->cursor_y = screen_buffer->cursor_y;
1905 info->width = screen_buffer->width;
1906 info->height = screen_buffer->height;
1907 info->attr = screen_buffer->attr;
1908 info->popup_attr = screen_buffer->popup_attr;
1909 info->win_left = screen_buffer->win.left;
1910 info->win_top = screen_buffer->win.top;
1911 info->win_right = screen_buffer->win.right;
1912 info->win_bottom = screen_buffer->win.bottom;
1913 info->max_width = screen_buffer->max_width;
1914 info->max_height = screen_buffer->max_height;
1915 info->font_width = screen_buffer->font.width;
1916 info->font_height = screen_buffer->font.height;
1917 info->font_weight = screen_buffer->font.weight;
1918 info->font_pitch_family = screen_buffer->font.pitch_family;
1919 memcpy( info->color_map, screen_buffer->color_map, sizeof(info->color_map) );
1920 size -= sizeof(*info);
1921 if (size) memcpy( info + 1, screen_buffer->font.face_name, size );
1922 return 1;
1925 case IOCTL_CONDRV_SET_OUTPUT_INFO:
1927 const struct condrv_output_info_params *params = get_req_data();
1928 if (get_req_data_size() < sizeof(*params))
1930 set_error( STATUS_INVALID_PARAMETER );
1931 return 0;
1933 if (!screen_buffer->input)
1935 set_error( STATUS_INVALID_HANDLE );
1936 return 0;
1938 return set_output_info( screen_buffer, params, get_req_data_size() - sizeof(*params) );
1941 case IOCTL_CONDRV_ACTIVATE:
1942 if (!screen_buffer->input)
1944 set_error( STATUS_INVALID_HANDLE );
1945 return 0;
1948 if (screen_buffer != screen_buffer->input->active)
1950 if (screen_buffer->input->active) release_object( screen_buffer->input->active );
1951 screen_buffer->input->active = (struct screen_buffer *)grab_object( screen_buffer );
1952 generate_sb_initial_events( screen_buffer->input );
1954 return 1;
1956 case IOCTL_CONDRV_FILL_OUTPUT:
1958 const struct condrv_fill_output_params *params = get_req_data();
1959 char_info_t data;
1960 DWORD written;
1961 if (get_req_data_size() != sizeof(*params) ||
1962 (get_reply_max_size() && get_reply_max_size() != sizeof(written)))
1964 set_error( STATUS_INVALID_PARAMETER );
1965 return 0;
1967 data.ch = params->ch;
1968 data.attr = params->attr;
1969 written = fill_console_output( screen_buffer, data, params->mode,
1970 params->x, params->y, params->count, params->wrap );
1971 if (written && get_reply_max_size() == sizeof(written))
1972 set_reply_data( &written, sizeof(written) );
1973 return !get_error();
1976 case IOCTL_CONDRV_SCROLL:
1978 const struct condrv_scroll_params *params = get_req_data();
1979 rectangle_t clip;
1980 if (get_req_data_size() != sizeof(*params))
1982 set_error( STATUS_INVALID_PARAMETER );
1983 return 0;
1985 if (console_input_is_bare( screen_buffer->input ) || !screen_buffer->input)
1987 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1988 return 0;
1990 clip.left = max( params->clip.Left, 0 );
1991 clip.top = max( params->clip.Top, 0 );
1992 clip.right = min( params->clip.Right, screen_buffer->width - 1 );
1993 clip.bottom = min( params->clip.Bottom, screen_buffer->height - 1 );
1994 if (clip.left > clip.right || clip.top > clip.bottom || params->scroll.Left < 0 || params->scroll.Top < 0 ||
1995 params->scroll.Right >= screen_buffer->width || params->scroll.Bottom >= screen_buffer->height ||
1996 params->scroll.Right < params->scroll.Left || params->scroll.Top > params->scroll.Bottom ||
1997 params->origin.X < 0 || params->origin.X >= screen_buffer->width || params->origin.Y < 0 ||
1998 params->origin.Y >= screen_buffer->height)
2000 set_error( STATUS_INVALID_PARAMETER );
2001 return 0;
2004 scroll_console_output( screen_buffer, params->scroll.Left, params->scroll.Top, params->origin.X, params->origin.Y,
2005 params->scroll.Right - params->scroll.Left + 1, params->scroll.Bottom - params->scroll.Top + 1,
2006 &clip, params->fill );
2007 return !get_error();
2010 default:
2011 set_error( STATUS_INVALID_HANDLE );
2012 return 0;
2016 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2018 struct console_input_events *evts = get_fd_user( fd );
2020 switch (code)
2022 case IOCTL_CONDRV_GET_RENDERER_EVENTS:
2023 set_error( STATUS_PENDING );
2024 if (evts->num_used) return get_renderer_events( evts, async );
2025 queue_async( &evts->read_q, async );
2026 return 1;
2028 case IOCTL_CONDRV_ATTACH_RENDERER:
2030 struct console_input *console_input;
2031 if (get_req_data_size() != sizeof(condrv_handle_t))
2033 set_error( STATUS_INVALID_PARAMETER );
2034 return 0;
2036 console_input = (struct console_input *)get_handle_obj( current->process, *(condrv_handle_t *)get_req_data(),
2037 0, &console_input_ops );
2038 if (!console_input) return 0;
2040 if (!console_input->evt && !evts->console)
2042 console_input->evt = evts;
2043 console_input->renderer = current;
2044 evts->console = console_input;
2046 else set_error( STATUS_INVALID_HANDLE );
2048 release_object( console_input );
2049 return !get_error();
2052 case IOCTL_CONDRV_SCROLL:
2053 case IOCTL_CONDRV_SET_MODE:
2054 case IOCTL_CONDRV_WRITE_OUTPUT:
2055 case IOCTL_CONDRV_READ_OUTPUT:
2056 case IOCTL_CONDRV_FILL_OUTPUT:
2057 case IOCTL_CONDRV_GET_OUTPUT_INFO:
2058 case IOCTL_CONDRV_SET_OUTPUT_INFO:
2059 if (!evts->console || !evts->console->active)
2061 set_error( STATUS_INVALID_HANDLE );
2062 return 0;
2064 return screen_buffer_ioctl( evts->console->active->fd, code, async );
2066 default:
2067 if (!evts->console)
2069 set_error( STATUS_INVALID_HANDLE );
2070 return 0;
2072 return console_input_ioctl( evts->console->fd, code, async );
2076 static struct object_type *console_device_get_type( struct object *obj )
2078 static const WCHAR name[] = {'D','e','v','i','c','e'};
2079 static const struct unicode_str str = { name, sizeof(name) };
2080 return get_object_type( &str );
2083 static void console_device_dump( struct object *obj, int verbose )
2085 fputs( "Console device\n", stderr );
2088 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
2090 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
2091 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
2092 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
2093 static const WCHAR rendererW[] = {'R','e','n','d','e','r','e','r'};
2094 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
2095 static const WCHAR serverW[] = {'S','e','r','v','e','r'};
2097 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
2099 if (!current->process->console)
2101 set_error( STATUS_INVALID_HANDLE );
2102 return NULL;
2104 name->len = 0;
2105 return grab_object( current->process->console );
2108 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
2110 if (!current->process->console || !current->process->console->active)
2112 set_error( STATUS_INVALID_HANDLE );
2113 return NULL;
2115 name->len = 0;
2116 return grab_object( current->process->console->active );
2119 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
2121 name->len = 0;
2122 return grab_object( obj );
2125 if (name->len == sizeof(rendererW) && !memcmp( name->str, rendererW, name->len ))
2127 name->len = 0;
2128 return create_console_input_events();
2131 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
2133 if (!current->process->console)
2135 set_error( STATUS_INVALID_HANDLE );
2136 return NULL;
2138 name->len = 0;
2139 return create_console_output( current->process->console, -1 );
2142 if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
2144 name->len = 0;
2145 return create_console_server();
2148 return NULL;
2151 static struct object *console_device_open_file( struct object *obj, unsigned int access,
2152 unsigned int sharing, unsigned int options )
2154 int is_output;
2155 access = default_fd_map_access( obj, access );
2156 is_output = access & FILE_WRITE_DATA;
2157 if (!current->process->console || (is_output && !current->process->console))
2159 set_error( STATUS_INVALID_HANDLE );
2160 return NULL;
2162 if (is_output && (access & FILE_READ_DATA))
2164 set_error( STATUS_INVALID_PARAMETER );
2165 return NULL;
2167 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
2170 struct object *create_console_device( struct object *root, const struct unicode_str *name )
2172 return create_named_object( root, &console_device_ops, name, 0, NULL );
2175 /* allocate a console for the renderer */
2176 DECL_HANDLER(alloc_console)
2178 struct process *process;
2179 struct console_input *console;
2180 int fd;
2181 int attach = 0;
2183 if (req->input_fd != -1)
2185 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
2187 set_error( STATUS_INVALID_PARAMETER );
2188 return;
2191 else fd = -1;
2193 switch (req->pid)
2195 case 0:
2196 /* console to be attached to parent process */
2197 if (!(process = get_process_from_id( current->process->parent_id )))
2199 if (fd != -1) close( fd );
2200 set_error( STATUS_ACCESS_DENIED );
2201 return;
2203 attach = 1;
2204 break;
2205 case 0xffffffff:
2206 /* console to be attached to current process */
2207 process = current->process;
2208 grab_object( process );
2209 attach = 1;
2210 break;
2211 default:
2212 /* console to be attached to req->pid */
2213 if (!(process = get_process_from_id( req->pid )))
2215 if (fd != -1) close( fd );
2216 return;
2220 if (attach && process->console)
2222 if (fd != -1) close( fd );
2223 set_error( STATUS_ACCESS_DENIED );
2225 else if ((console = (struct console_input*)create_console_input( fd )))
2227 if ((reply->handle_in = alloc_handle( current->process, console, req->access,
2228 req->attributes )) && attach)
2230 process->console = (struct console_input*)grab_object( console );
2231 console->num_proc++;
2233 release_object( console );
2235 release_object( process );
2238 /* free the console of the current process */
2239 DECL_HANDLER(free_console)
2241 free_console( current->process );
2244 /* attach to a other process's console */
2245 DECL_HANDLER(attach_console)
2247 struct process *process;
2249 if (current->process->console)
2251 set_error( STATUS_ACCESS_DENIED );
2252 return;
2255 process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
2256 ? current->process->parent_id : req->pid );
2257 if (!process) return;
2259 if (process->console && process->console->active)
2261 current->process->console = (struct console_input *)grab_object( process->console );
2262 current->process->console->num_proc++;
2264 else
2266 set_error( STATUS_INVALID_HANDLE );
2269 release_object( process );
2270 return;
2273 /* appends a string to console's history */
2274 DECL_HANDLER(append_console_input_history)
2276 struct console_input *console;
2278 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
2279 console_input_append_hist( console, get_req_data(), get_req_data_size() );
2280 release_object( console );
2283 /* appends a string to console's history */
2284 DECL_HANDLER(get_console_input_history)
2286 struct console_input *console;
2288 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
2289 reply->total = console_input_get_hist( console, req->index );
2290 release_object( console );
2293 /* creates a screen buffer */
2294 DECL_HANDLER(create_console_output)
2296 struct console_input *console;
2297 struct object *screen_buffer;
2298 int fd;
2300 if (req->fd != -1)
2302 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
2304 set_error( STATUS_INVALID_HANDLE );
2305 return;
2308 else fd = -1;
2309 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
2311 if (fd != -1) close( fd );
2312 return;
2314 if (console_input_is_bare( console ) ^ (fd != -1))
2316 if (fd != -1) close( fd );
2317 release_object( console );
2318 set_error( STATUS_INVALID_HANDLE );
2319 return;
2322 screen_buffer = create_console_output( console, fd );
2323 if (screen_buffer)
2325 /* FIXME: should store sharing and test it when opening the CONOUT$ device
2326 * see file.c on how this could be done */
2327 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
2328 release_object( screen_buffer );
2330 release_object( console );
2333 /* sends a signal to a console (process, group...) */
2334 DECL_HANDLER(send_console_signal)
2336 process_id_t group;
2338 group = req->group_id ? req->group_id : current->process->group_id;
2340 if (!group)
2341 set_error( STATUS_INVALID_PARAMETER );
2342 else
2343 propagate_console_signal( current->process->console, req->signal, group );
2346 /* get console which renderer is 'current' */
2347 static int cgwe_enum( struct process* process, void* user)
2349 if (process->console && process->console->renderer == current)
2351 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
2352 return 1;
2354 return 0;
2357 DECL_HANDLER(get_console_wait_event)
2359 struct console_input* console = NULL;
2360 struct object *obj;
2362 if (req->handle)
2364 if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
2365 if (obj->ops == &console_input_ops)
2366 console = (struct console_input *)grab_object( obj );
2367 else if (obj->ops == &screen_buffer_ops)
2368 console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
2369 else
2370 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2371 release_object( obj );
2373 else if (current->process->console)
2374 console = (struct console_input *)grab_object( current->process->console );
2375 else enum_processes(cgwe_enum, &console);
2377 if (console)
2379 reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
2380 release_object( console );
2382 else set_error( STATUS_INVALID_PARAMETER );
2385 /* retrieve the next pending console ioctl request */
2386 DECL_HANDLER(get_next_console_request)
2388 struct console_host_ioctl *ioctl = NULL;
2389 struct console_server *server;
2390 struct iosb *iosb = NULL;
2393 server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
2394 if (!server) return;
2396 if (!server->console)
2398 set_error( STATUS_INVALID_HANDLE );
2399 release_object( server );
2400 return;
2403 if (req->signal) set_event( server->console->event);
2404 else reset_event( server->console->event );
2406 /* set result of previous ioctl, if any */
2407 if (server->busy)
2409 unsigned int status = req->status;
2410 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
2411 list_remove( &ioctl->entry );
2412 if (ioctl->async)
2414 iosb = async_get_iosb( ioctl->async );
2415 iosb->status = req->status;
2416 iosb->out_size = min( iosb->out_size, get_req_data_size() );
2417 if (iosb->out_size)
2419 if ((iosb->out_data = memdup( get_req_data(), iosb->out_size )))
2421 iosb->result = iosb->out_size;
2422 status = STATUS_ALERTED;
2424 else if (!status)
2426 iosb->status = STATUS_NO_MEMORY;
2427 iosb->out_size = 0;
2431 console_host_ioctl_terminate( ioctl, status );
2432 if (iosb) release_object( iosb );
2433 server->busy = 0;
2436 /* return the next ioctl */
2437 if (!list_empty( &server->queue ))
2439 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
2440 iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
2442 if (!iosb || get_reply_max_size() >= iosb->in_size)
2444 reply->code = ioctl->code;
2446 if (iosb)
2448 reply->out_size = iosb->out_size;
2449 set_reply_data_ptr( iosb->in_data, iosb->in_size );
2450 iosb->in_data = NULL;
2453 server->busy = 1;
2455 else
2457 reply->out_size = iosb->in_size;
2458 set_error( STATUS_BUFFER_OVERFLOW );
2460 if (iosb) release_object( iosb );
2462 else
2464 set_error( STATUS_PENDING );
2467 release_object( server );