ntoskrnl.exe: Stub MmProtectMdlSystemAddress.
[wine.git] / server / console.c
blob5f295d41840439fb4c10ae380aad534abbac4258
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>
31 #include <sys/ioctl.h>
32 #include <termios.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "handle.h"
37 #include "process.h"
38 #include "request.h"
39 #include "file.h"
40 #include "unicode.h"
41 #include "wincon.h"
42 #include "winternl.h"
43 #include "wine/condrv.h"
45 struct screen_buffer;
46 struct console_input_events;
48 struct history_line
50 data_size_t len;
51 WCHAR text[1];
54 struct console_input
56 struct object obj; /* object header */
57 int num_proc; /* number of processes attached to this console */
58 struct thread *renderer; /* console renderer thread */
59 int mode; /* input mode */
60 struct screen_buffer *active; /* active screen buffer */
61 int recnum; /* number of input records */
62 INPUT_RECORD *records; /* input records */
63 struct console_input_events *evt; /* synchronization event with renderer */
64 struct console_server *server; /* console server object */
65 WCHAR *title; /* console title */
66 data_size_t title_len; /* length of console title */
67 struct history_line **history; /* lines history */
68 int history_size; /* number of entries in history array */
69 int history_index; /* number of used entries in history array */
70 int history_mode; /* mode of history (non zero means remove doubled strings */
71 int edition_mode; /* index to edition mode flavors */
72 int input_cp; /* console input codepage */
73 int output_cp; /* console output codepage */
74 user_handle_t win; /* window handle if backend supports it */
75 unsigned int last_id; /* id of last created console buffer */
76 struct event *event; /* event to wait on for input queue */
77 struct fd *fd; /* for bare console, attached input fd */
78 struct async_queue ioctl_q; /* ioctl queue */
79 struct async_queue read_q; /* read queue */
82 static void console_input_dump( struct object *obj, int verbose );
83 static void console_input_destroy( struct object *obj );
84 static struct fd *console_input_get_fd( struct object *obj );
85 static struct object *console_input_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
86 static struct object *console_input_open_file( struct object *obj, unsigned int access,
87 unsigned int sharing, unsigned int options );
89 static const struct object_ops console_input_ops =
91 sizeof(struct console_input), /* size */
92 console_input_dump, /* dump */
93 no_get_type, /* get_type */
94 no_add_queue, /* add_queue */
95 NULL, /* remove_queue */
96 NULL, /* signaled */
97 no_satisfied, /* satisfied */
98 no_signal, /* signal */
99 console_input_get_fd, /* get_fd */
100 default_fd_map_access, /* map_access */
101 default_get_sd, /* get_sd */
102 default_set_sd, /* set_sd */
103 no_get_full_name, /* get_full_name */
104 console_input_lookup_name, /* lookup_name */
105 no_link_name, /* link_name */
106 NULL, /* unlink_name */
107 console_input_open_file, /* open_file */
108 no_kernel_obj_list, /* get_kernel_obj_list */
109 no_close_handle, /* close_handle */
110 console_input_destroy /* destroy */
113 static enum server_fd_type console_get_fd_type( struct fd *fd );
114 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
116 static const struct fd_ops console_input_fd_ops =
118 default_fd_get_poll_events, /* get_poll_events */
119 default_poll_event, /* poll_event */
120 console_get_fd_type, /* get_fd_type */
121 no_fd_read, /* read */
122 no_fd_write, /* write */
123 no_fd_flush, /* flush */
124 no_fd_get_file_info, /* get_file_info */
125 no_fd_get_volume_info, /* get_volume_info */
126 console_input_ioctl, /* ioctl */
127 default_fd_queue_async, /* queue_async */
128 default_fd_reselect_async /* reselect_async */
131 static void console_input_events_dump( struct object *obj, int verbose );
132 static void console_input_events_destroy( struct object *obj );
133 static struct fd *console_input_events_get_fd( struct object *obj );
134 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
135 unsigned int sharing, unsigned int options );
137 struct console_input_events
139 struct object obj; /* object header */
140 struct fd *fd; /* pseudo-fd for ioctls */
141 struct console_input *console; /* attached console */
142 int num_alloc; /* number of allocated events */
143 int num_used; /* number of actually used events */
144 struct condrv_renderer_event *events;
145 struct async_queue read_q; /* read queue */
148 static const struct object_ops console_input_events_ops =
150 sizeof(struct console_input_events), /* size */
151 console_input_events_dump, /* dump */
152 no_get_type, /* get_type */
153 add_queue, /* add_queue */
154 remove_queue, /* remove_queue */
155 NULL, /* signaled */
156 no_satisfied, /* satisfied */
157 no_signal, /* signal */
158 console_input_events_get_fd, /* get_fd */
159 default_fd_map_access, /* map_access */
160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
162 no_get_full_name, /* get_full_name */
163 no_lookup_name, /* lookup_name */
164 no_link_name, /* link_name */
165 NULL, /* unlink_name */
166 console_input_events_open_file, /* open_file */
167 no_kernel_obj_list, /* get_kernel_obj_list */
168 no_close_handle, /* close_handle */
169 console_input_events_destroy /* destroy */
172 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
174 static const struct fd_ops console_input_events_fd_ops =
176 default_fd_get_poll_events, /* get_poll_events */
177 default_poll_event, /* poll_event */
178 console_get_fd_type, /* get_fd_type */
179 no_fd_read, /* read */
180 no_fd_write, /* write */
181 no_fd_flush, /* flush */
182 no_fd_get_file_info, /* get_file_info */
183 no_fd_get_volume_info, /* get_volume_info */
184 console_input_events_ioctl, /* ioctl */
185 default_fd_queue_async, /* queue_async */
186 default_fd_reselect_async /* reselect_async */
189 struct console_host_ioctl
191 unsigned int code; /* ioctl code */
192 int output; /* output id for screen buffer ioctls */
193 struct async *async; /* ioctl async */
194 struct list entry; /* list entry */
197 struct console_server
199 struct object obj; /* object header */
200 struct fd *fd; /* pseudo-fd for ioctls */
201 struct console_input *console; /* attached console */
202 struct list queue; /* ioctl queue */
203 struct list read_queue; /* blocking read queue */
204 int busy; /* flag if server processing an ioctl */
205 int term_fd; /* UNIX terminal fd */
206 struct termios termios; /* original termios */
209 static void console_server_dump( struct object *obj, int verbose );
210 static void console_server_destroy( struct object *obj );
211 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry );
212 static struct fd *console_server_get_fd( struct object *obj );
213 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
214 static struct object *console_server_open_file( struct object *obj, unsigned int access,
215 unsigned int sharing, unsigned int options );
217 static const struct object_ops console_server_ops =
219 sizeof(struct console_server), /* size */
220 console_server_dump, /* dump */
221 no_get_type, /* get_type */
222 add_queue, /* add_queue */
223 remove_queue, /* remove_queue */
224 console_server_signaled, /* signaled */
225 no_satisfied, /* satisfied */
226 no_signal, /* signal */
227 console_server_get_fd, /* get_fd */
228 default_fd_map_access, /* map_access */
229 default_get_sd, /* get_sd */
230 default_set_sd, /* set_sd */
231 no_get_full_name, /* get_full_name */
232 console_server_lookup_name, /* lookup_name */
233 no_link_name, /* link_name */
234 NULL, /* unlink_name */
235 console_server_open_file, /* open_file */
236 no_kernel_obj_list, /* get_kernel_obj_list */
237 fd_close_handle, /* close_handle */
238 console_server_destroy /* destroy */
241 static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
243 static const struct fd_ops console_server_fd_ops =
245 default_fd_get_poll_events, /* get_poll_events */
246 default_poll_event, /* poll_event */
247 console_get_fd_type, /* get_fd_type */
248 no_fd_read, /* read */
249 no_fd_write, /* write */
250 no_fd_flush, /* flush */
251 no_fd_get_file_info, /* get_file_info */
252 no_fd_get_volume_info, /* get_volume_info */
253 console_server_ioctl, /* ioctl */
254 default_fd_queue_async, /* queue_async */
255 default_fd_reselect_async /* reselect_async */
258 struct font_info
260 short int width;
261 short int height;
262 short int weight;
263 short int pitch_family;
264 WCHAR *face_name;
265 data_size_t face_len;
268 struct screen_buffer
270 struct object obj; /* object header */
271 struct list entry; /* entry in list of all screen buffers */
272 struct console_input *input; /* associated console input */
273 unsigned int id; /* buffer id */
274 unsigned int mode; /* output mode */
275 int cursor_size; /* size of cursor (percentage filled) */
276 int cursor_visible;/* cursor visibility flag */
277 int cursor_x; /* position of cursor */
278 int cursor_y; /* position of cursor */
279 int width; /* size (w-h) of the screen buffer */
280 int height;
281 int max_width; /* size (w-h) of the window given font size */
282 int max_height;
283 char_info_t *data; /* the data for each cell - a width x height matrix */
284 unsigned short attr; /* default fill attributes (screen colors) */
285 unsigned short popup_attr; /* pop-up color attributes */
286 unsigned int color_map[16]; /* color table */
287 rectangle_t win; /* current visible window on the screen buffer *
288 * as seen in wineconsole */
289 struct font_info font; /* console font information */
290 struct fd *fd; /* for bare console, attached output fd */
291 struct async_queue ioctl_q; /* ioctl queue */
294 static void screen_buffer_dump( struct object *obj, int verbose );
295 static void screen_buffer_destroy( struct object *obj );
296 static struct fd *screen_buffer_get_fd( struct object *obj );
297 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
298 unsigned int sharing, unsigned int options );
300 static const struct object_ops screen_buffer_ops =
302 sizeof(struct screen_buffer), /* size */
303 screen_buffer_dump, /* dump */
304 no_get_type, /* get_type */
305 no_add_queue, /* add_queue */
306 NULL, /* remove_queue */
307 NULL, /* signaled */
308 NULL, /* satisfied */
309 no_signal, /* signal */
310 screen_buffer_get_fd, /* get_fd */
311 default_fd_map_access, /* map_access */
312 default_get_sd, /* get_sd */
313 default_set_sd, /* set_sd */
314 no_get_full_name, /* get_full_name */
315 no_lookup_name, /* lookup_name */
316 no_link_name, /* link_name */
317 NULL, /* unlink_name */
318 screen_buffer_open_file, /* open_file */
319 no_kernel_obj_list, /* get_kernel_obj_list */
320 no_close_handle, /* close_handle */
321 screen_buffer_destroy /* destroy */
324 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
326 static const struct fd_ops screen_buffer_fd_ops =
328 default_fd_get_poll_events, /* get_poll_events */
329 default_poll_event, /* poll_event */
330 console_get_fd_type, /* get_fd_type */
331 no_fd_read, /* read */
332 no_fd_write, /* write */
333 no_fd_flush, /* flush */
334 no_fd_get_file_info, /* get_file_info */
335 no_fd_get_volume_info, /* get_volume_info */
336 screen_buffer_ioctl, /* ioctl */
337 default_fd_queue_async, /* queue_async */
338 default_fd_reselect_async /* reselect_async */
341 static struct object_type *console_device_get_type( struct object *obj );
342 static void console_device_dump( struct object *obj, int verbose );
343 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
344 static struct object *console_device_open_file( struct object *obj, unsigned int access,
345 unsigned int sharing, unsigned int options );
347 static const struct object_ops console_device_ops =
349 sizeof(struct object), /* size */
350 console_device_dump, /* dump */
351 console_device_get_type, /* get_type */
352 no_add_queue, /* add_queue */
353 NULL, /* remove_queue */
354 NULL, /* signaled */
355 no_satisfied, /* satisfied */
356 no_signal, /* signal */
357 no_get_fd, /* get_fd */
358 default_fd_map_access, /* map_access */
359 default_get_sd, /* get_sd */
360 default_set_sd, /* set_sd */
361 default_get_full_name, /* get_full_name */
362 console_device_lookup_name, /* lookup_name */
363 directory_link_name, /* link_name */
364 default_unlink_name, /* unlink_name */
365 console_device_open_file, /* open_file */
366 no_kernel_obj_list, /* get_kernel_obj_list */
367 no_close_handle, /* close_handle */
368 no_destroy /* destroy */
371 struct console_connection
373 struct object obj; /* object header */
374 struct fd *fd; /* pseudo-fd for ioctls */
377 static void console_connection_dump( struct object *obj, int verbose );
378 static struct fd *console_connection_get_fd( struct object *obj );
379 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
380 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
381 unsigned int sharing, unsigned int options );
382 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
383 static void console_connection_destroy( struct object *obj );
385 static const struct object_ops console_connection_ops =
387 sizeof(struct console_connection),/* size */
388 console_connection_dump, /* dump */
389 console_device_get_type, /* get_type */
390 no_add_queue, /* add_queue */
391 NULL, /* remove_queue */
392 NULL, /* signaled */
393 no_satisfied, /* satisfied */
394 no_signal, /* signal */
395 console_connection_get_fd, /* get_fd */
396 no_map_access, /* map_access */
397 default_get_sd, /* get_sd */
398 default_set_sd, /* set_sd */
399 no_get_full_name, /* get_full_name */
400 console_connection_lookup_name, /* lookup_name */
401 directory_link_name, /* link_name */
402 default_unlink_name, /* unlink_name */
403 console_connection_open_file, /* open_file */
404 no_kernel_obj_list, /* get_kernel_obj_list */
405 console_connection_close_handle, /* close_handle */
406 console_connection_destroy /* destroy */
409 static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
411 static const struct fd_ops console_connection_fd_ops =
413 default_fd_get_poll_events, /* get_poll_events */
414 default_poll_event, /* poll_event */
415 console_get_fd_type, /* get_fd_type */
416 no_fd_read, /* read */
417 no_fd_write, /* write */
418 no_fd_flush, /* flush */
419 no_fd_get_file_info, /* get_file_info */
420 no_fd_get_volume_info, /* get_volume_info */
421 console_connection_ioctl, /* ioctl */
422 default_fd_queue_async, /* queue_async */
423 default_fd_reselect_async /* reselect_async */
426 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
428 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
430 static int console_input_is_bare( struct console_input* cin )
432 return cin->evt == NULL;
435 static struct fd *console_input_get_fd( struct object* obj )
437 struct console_input *console_input = (struct console_input*)obj;
438 assert( obj->ops == &console_input_ops );
439 return (struct fd *)grab_object( console_input->fd );
442 static enum server_fd_type console_get_fd_type( struct fd *fd )
444 return FD_TYPE_CHAR;
447 /* dumps the renderer events of a console */
448 static void console_input_events_dump( struct object *obj, int verbose )
450 struct console_input_events *evts = (struct console_input_events *)obj;
451 assert( obj->ops == &console_input_events_ops );
452 fprintf( stderr, "Console input events: %d/%d events\n",
453 evts->num_used, evts->num_alloc );
456 /* destroys the renderer events of a console */
457 static void console_input_events_destroy( struct object *obj )
459 struct console_input_events *evts = (struct console_input_events *)obj;
460 assert( obj->ops == &console_input_events_ops );
461 if (evts->console) evts->console->evt = NULL;
462 free_async_queue( &evts->read_q );
463 if (evts->fd) release_object( evts->fd );
464 free( evts->events );
467 static struct fd *console_input_events_get_fd( struct object* obj )
469 struct console_input_events *evts = (struct console_input_events*)obj;
470 assert( obj->ops == &console_input_events_ops );
471 return (struct fd*)grab_object( evts->fd );
474 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
475 unsigned int sharing, unsigned int options )
477 return grab_object( obj );
480 /* retrieves events from the console's renderer events list */
481 static int get_renderer_events( struct console_input_events* evts, struct async *async )
483 struct iosb *iosb = async_get_iosb( async );
484 data_size_t num;
486 num = min( iosb->out_size / sizeof(evts->events[0]), evts->num_used );
487 if (num && !(iosb->out_data = malloc( num * sizeof(evts->events[0] ))))
489 async_terminate( async, STATUS_NO_MEMORY );
490 release_object( iosb );
491 return 0;
494 iosb->status = STATUS_SUCCESS;
495 iosb->out_size = iosb->result = num * sizeof(evts->events[0]);
496 if (num) memcpy( iosb->out_data, evts->events, iosb->result );
497 release_object( iosb );
498 async_terminate( async, STATUS_ALERTED );
500 if (num && num < evts->num_used)
502 memmove( &evts->events[0], &evts->events[num],
503 (evts->num_used - num) * sizeof(evts->events[0]) );
505 evts->num_used -= num;
506 return 1;
509 /* add an event to the console's renderer events list */
510 static void console_input_events_append( struct console_input* console,
511 struct condrv_renderer_event* evt)
513 struct console_input_events* evts;
514 int collapsed = FALSE;
515 struct async *async;
517 if (!(evts = console->evt)) return;
518 /* to be done even when evt has been generated by the renderer ? */
520 /* try to collapse evt into current queue's events */
521 if (evts->num_used)
523 struct condrv_renderer_event* last = &evts->events[evts->num_used - 1];
525 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
526 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
528 /* if two update events overlap, collapse them into a single one */
529 if (last->u.update.bottom + 1 >= evt->u.update.top &&
530 evt->u.update.bottom + 1 >= last->u.update.top)
532 last->u.update.top = min(last->u.update.top, evt->u.update.top);
533 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
534 collapsed = TRUE;
538 if (!collapsed)
540 if (evts->num_used == evts->num_alloc)
542 evts->num_alloc += 16;
543 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
544 assert(evts->events);
546 evts->events[evts->num_used++] = *evt;
548 while (evts->num_used && (async = find_pending_async( &evts->read_q )))
550 get_renderer_events( evts, async );
551 release_object( async );
555 static struct object *create_console_input_events(void)
557 struct console_input_events* evt;
559 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
560 evt->console = NULL;
561 evt->num_alloc = evt->num_used = 0;
562 evt->events = NULL;
563 init_async_queue( &evt->read_q );
564 if (!(evt->fd = alloc_pseudo_fd( &console_input_events_fd_ops, &evt->obj, 0 )))
566 release_object( evt );
567 return NULL;
569 return &evt->obj;
572 static struct object *create_console_input(void)
574 struct console_input *console_input;
576 if (!(console_input = alloc_object( &console_input_ops )))
577 return NULL;
579 console_input->renderer = NULL;
580 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
581 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
582 ENABLE_EXTENDED_FLAGS;
583 console_input->num_proc = 0;
584 console_input->active = NULL;
585 console_input->recnum = 0;
586 console_input->records = NULL;
587 console_input->evt = NULL;
588 console_input->server = NULL;
589 console_input->title = NULL;
590 console_input->title_len = 0;
591 console_input->history_size = 50;
592 console_input->history = calloc( console_input->history_size, sizeof(*console_input->history) );
593 console_input->history_index = 0;
594 console_input->history_mode = 0;
595 console_input->edition_mode = 0;
596 console_input->input_cp = 0;
597 console_input->output_cp = 0;
598 console_input->win = 0;
599 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
600 console_input->fd = NULL;
601 console_input->last_id = 0;
602 init_async_queue( &console_input->ioctl_q );
603 init_async_queue( &console_input->read_q );
605 if (!console_input->history || !console_input->event)
607 console_input->history_size = 0;
608 release_object( console_input );
609 return NULL;
612 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
613 FILE_SYNCHRONOUS_IO_NONALERT );
614 if (!console_input->fd)
616 release_object( console_input );
617 return NULL;
619 allow_fd_caching( console_input->fd );
620 return &console_input->obj;
623 static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
625 if (call->async)
627 async_terminate( call->async, status );
628 release_object( call->async );
630 free( call );
633 static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
634 struct async *async, struct async_queue *queue )
636 struct console_host_ioctl *ioctl;
638 if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
639 ioctl->code = code;
640 ioctl->output = output;
641 ioctl->async = NULL;
642 if (async)
644 ioctl->async = (struct async *)grab_object( async );
645 queue_async( queue, async );
647 list_add_tail( &server->queue, &ioctl->entry );
648 wake_up( &server->obj, 0 );
649 if (async) set_error( STATUS_PENDING );
650 return 0;
653 static void disconnect_console_server( struct console_server *server )
655 while (!list_empty( &server->queue ))
657 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
658 list_remove( &call->entry );
659 console_host_ioctl_terminate( call, STATUS_CANCELLED );
661 while (!list_empty( &server->read_queue ))
663 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
664 list_remove( &call->entry );
665 console_host_ioctl_terminate( call, STATUS_CANCELLED );
668 if (server->term_fd != -1)
670 tcsetattr( server->term_fd, TCSANOW, &server->termios );
671 close( server->term_fd );
672 server->term_fd = -1;
675 if (server->console)
677 assert( server->console->server == server );
678 server->console->server = NULL;
679 server->console = NULL;
680 wake_up( &server->obj, 0 );
684 static void set_active_screen_buffer( struct console_input *console_input, struct screen_buffer *screen_buffer )
686 struct condrv_renderer_event evt;
688 if (console_input->active == screen_buffer) return;
689 if (console_input->active) release_object( console_input->active );
690 console_input->active = (struct screen_buffer *)grab_object( screen_buffer );
692 if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_ACTIVATE,
693 screen_buffer->id, NULL, NULL );
695 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
696 evt.u.resize.width = screen_buffer->width;
697 evt.u.resize.height = screen_buffer->height;
698 console_input_events_append( console_input, &evt );
700 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
701 evt.u.display.left = screen_buffer->win.left;
702 evt.u.display.top = screen_buffer->win.top;
703 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
704 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
705 console_input_events_append( console_input, &evt );
707 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
708 evt.u.update.top = 0;
709 evt.u.update.bottom = screen_buffer->height - 1;
710 console_input_events_append( console_input, &evt );
712 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
713 evt.u.cursor_geom.size = screen_buffer->cursor_size;
714 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
715 console_input_events_append( console_input, &evt );
717 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
718 evt.u.cursor_pos.x = screen_buffer->cursor_x;
719 evt.u.cursor_pos.y = screen_buffer->cursor_y;
720 console_input_events_append( console_input, &evt );
723 static struct object *create_console_output( struct console_input *console_input )
725 struct screen_buffer *screen_buffer;
726 int i;
728 if (console_input->last_id == ~0)
730 set_error( STATUS_NO_MEMORY );
731 return NULL;
734 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
735 return NULL;
737 screen_buffer->id = ++console_input->last_id;
738 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
739 screen_buffer->input = console_input;
740 screen_buffer->cursor_size = 100;
741 screen_buffer->cursor_visible = 1;
742 screen_buffer->width = 80;
743 screen_buffer->height = 150;
744 screen_buffer->max_width = 80;
745 screen_buffer->max_height = 25;
746 screen_buffer->cursor_x = 0;
747 screen_buffer->cursor_y = 0;
748 screen_buffer->attr = 0x0F;
749 screen_buffer->popup_attr = 0xF5;
750 screen_buffer->win.left = 0;
751 screen_buffer->win.right = screen_buffer->max_width - 1;
752 screen_buffer->win.top = 0;
753 screen_buffer->win.bottom = screen_buffer->max_height - 1;
754 screen_buffer->data = NULL;
755 screen_buffer->font.width = 0;
756 screen_buffer->font.height = 0;
757 screen_buffer->font.weight = FW_NORMAL;
758 screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
759 screen_buffer->font.face_name = NULL;
760 screen_buffer->font.face_len = 0;
761 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
762 init_async_queue( &screen_buffer->ioctl_q );
763 list_add_head( &screen_buffer_list, &screen_buffer->entry );
765 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
766 FILE_SYNCHRONOUS_IO_NONALERT );
767 if (!screen_buffer->fd)
769 release_object( screen_buffer );
770 return NULL;
772 allow_fd_caching(screen_buffer->fd);
774 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
775 sizeof(*screen_buffer->data) )))
777 release_object( screen_buffer );
778 return NULL;
780 /* clear the first row */
781 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
782 /* and copy it to all other rows */
783 for (i = 1; i < screen_buffer->height; i++)
784 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
785 screen_buffer->width * sizeof(char_info_t) );
787 if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_INIT_OUTPUT,
788 screen_buffer->id, NULL, NULL );
789 if (!console_input->active) set_active_screen_buffer( console_input, screen_buffer );
790 return &screen_buffer->obj;
793 /* free the console for this process */
794 int free_console( struct process *process )
796 struct console_input* console = process->console;
798 if (!console) return 0;
800 process->console = NULL;
801 if (--console->num_proc == 0 && console->renderer)
803 /* all processes have terminated... tell the renderer to terminate too */
804 struct condrv_renderer_event evt;
805 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
806 memset(&evt.u, 0, sizeof(evt.u));
807 console_input_events_append( console, &evt );
809 release_object( console );
811 return 1;
814 /* let process inherit the console from parent... this handle two cases :
815 * 1/ generic console inheritance
816 * 2/ parent is a renderer which launches process, and process should attach to the console
817 * rendered by parent
819 obj_handle_t inherit_console( struct thread *parent_thread, obj_handle_t handle, struct process *process,
820 obj_handle_t hconin )
822 struct console_input *console = NULL;
824 if (handle) return duplicate_handle( current->process, handle, process, 0, 0, DUP_HANDLE_SAME_ACCESS );
826 /* if parent is a renderer, then attach current process to its console
827 * a bit hacky....
829 if (hconin && parent_thread)
831 /* FIXME: should we check some access rights ? */
832 if (!(console = (struct console_input *)get_handle_obj( parent_thread->process, hconin,
833 0, &console_input_ops )))
834 clear_error(); /* ignore error */
836 if (!console) return 0;
838 process->console = console;
839 console->num_proc++;
840 return alloc_handle( process, process->console,
841 SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE, 0 );
844 struct thread *console_get_renderer( struct console_input *console )
846 return console->renderer;
849 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
851 struct console_input* console = NULL;
853 if (handle)
854 console = (struct console_input *)get_handle_obj( current->process, handle,
855 access, &console_input_ops );
856 else if (current->process->console)
858 console = (struct console_input *)grab_object( current->process->console );
861 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
862 return console;
865 struct console_signal_info
867 struct console_input *console;
868 process_id_t group;
869 int signal;
872 static int propagate_console_signal_cb(struct process *process, void *user)
874 struct console_signal_info* csi = (struct console_signal_info*)user;
876 if (process->console == csi->console && process->running_threads &&
877 (!csi->group || process->group_id == csi->group))
879 /* find a suitable thread to signal */
880 struct thread *thread;
881 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
883 if (send_thread_signal( thread, csi->signal )) break;
886 return FALSE;
889 static void propagate_console_signal( struct console_input *console,
890 int sig, process_id_t group_id )
892 struct console_signal_info csi;
894 if (!console)
896 set_error( STATUS_INVALID_PARAMETER );
897 return;
899 /* FIXME: should support the other events (like CTRL_BREAK) */
900 if (sig != CTRL_C_EVENT)
902 set_error( STATUS_NOT_IMPLEMENTED );
903 return;
905 csi.console = console;
906 csi.signal = SIGINT;
907 csi.group = group_id;
909 enum_processes(propagate_console_signal_cb, &csi);
912 /* retrieve a pointer to the console input records */
913 static int read_console_input( struct console_input *console, struct async *async, int flush )
915 struct iosb *iosb = async_get_iosb( async );
916 data_size_t count;
918 count = min( iosb->out_size / sizeof(INPUT_RECORD), console->recnum );
919 if (count)
921 if (!(iosb->out_data = malloc( count * sizeof(INPUT_RECORD) )))
923 set_error( STATUS_NO_MEMORY );
924 release_object( iosb );
925 return 0;
927 iosb->out_size = iosb->result = count * sizeof(INPUT_RECORD);
928 memcpy( iosb->out_data, console->records, iosb->result );
929 iosb->status = STATUS_SUCCESS;
930 async_terminate( async, STATUS_ALERTED );
932 else
934 async_terminate( async, STATUS_SUCCESS );
937 release_object( iosb );
939 if (flush && count)
941 if (console->recnum > count)
943 INPUT_RECORD *new_rec;
944 memmove( console->records, console->records + count, (console->recnum - count) * sizeof(*console->records) );
945 console->recnum -= count;
946 new_rec = realloc( console->records, console->recnum * sizeof(*console->records) );
947 if (new_rec) console->records = new_rec;
949 else
951 console->recnum = 0;
952 free( console->records );
953 console->records = NULL;
954 reset_event( console->event );
958 return 1;
961 /* add input events to a console input queue */
962 static int write_console_input( struct console_input* console, int count,
963 const INPUT_RECORD *records )
965 INPUT_RECORD *new_rec;
966 struct async *async;
968 if (!count) return 1;
969 if (!(new_rec = realloc( console->records,
970 (console->recnum + count) * sizeof(INPUT_RECORD) )))
972 set_error( STATUS_NO_MEMORY );
973 return 0;
975 console->records = new_rec;
976 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
978 if (console->mode & ENABLE_PROCESSED_INPUT)
980 int i = 0;
981 while (i < count)
983 if (records[i].EventType == KEY_EVENT &&
984 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
985 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
987 if (i != count - 1)
988 memcpy( &console->records[console->recnum + i],
989 &console->records[console->recnum + i + 1],
990 (count - i - 1) * sizeof(INPUT_RECORD) );
991 count--;
992 if (records[i].Event.KeyEvent.bKeyDown)
994 /* send SIGINT to all processes attached to this console */
995 propagate_console_signal( console, CTRL_C_EVENT, 0 );
998 else i++;
1001 console->recnum += count;
1002 while (console->recnum && (async = find_pending_async( &console->read_q )))
1004 read_console_input( console, async, 1 );
1005 release_object( async );
1007 if (console->recnum) set_event( console->event );
1008 return 1;
1011 /* resize a screen buffer */
1012 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
1013 int new_width, int new_height )
1015 int i, old_width, old_height, copy_width, copy_height;
1016 char_info_t *new_data;
1018 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
1020 set_error( STATUS_NO_MEMORY );
1021 return 0;
1023 old_width = screen_buffer->width;
1024 old_height = screen_buffer->height;
1025 copy_width = min( old_width, new_width );
1026 copy_height = min( old_height, new_height );
1028 /* copy all the rows */
1029 for (i = 0; i < copy_height; i++)
1031 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
1032 copy_width * sizeof(char_info_t) );
1035 /* clear the end of each row */
1036 if (new_width > old_width)
1038 /* fill first row */
1039 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
1040 /* and blast it to the other rows */
1041 for (i = 1; i < copy_height; i++)
1042 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
1043 (new_width - old_width) * sizeof(char_info_t) );
1046 /* clear remaining rows */
1047 if (new_height > old_height)
1049 /* fill first row */
1050 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
1051 /* and blast it to the other rows */
1052 for (i = old_height+1; i < new_height; i++)
1053 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
1054 new_width * sizeof(char_info_t) );
1056 free( screen_buffer->data );
1057 screen_buffer->data = new_data;
1058 screen_buffer->width = new_width;
1059 screen_buffer->height = new_height;
1060 return 1;
1063 static int set_output_info( struct screen_buffer *screen_buffer,
1064 const struct condrv_output_info_params *params, data_size_t extra_size )
1066 const struct condrv_output_info *info = &params->info;
1067 struct condrv_renderer_event evt;
1068 WCHAR *font_name;
1070 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
1072 if (info->cursor_size < 1 || info->cursor_size > 100)
1074 set_error( STATUS_INVALID_PARAMETER );
1075 return 0;
1077 if (screen_buffer->cursor_size != info->cursor_size ||
1078 screen_buffer->cursor_visible != info->cursor_visible)
1080 screen_buffer->cursor_size = info->cursor_size;
1081 screen_buffer->cursor_visible = info->cursor_visible;
1082 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
1083 memset( &evt.u, 0, sizeof(evt.u) );
1084 evt.u.cursor_geom.size = info->cursor_size;
1085 evt.u.cursor_geom.visible = info->cursor_visible;
1086 console_input_events_append( screen_buffer->input, &evt );
1089 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
1091 if (info->cursor_x < 0 || info->cursor_x >= screen_buffer->width ||
1092 info->cursor_y < 0 || info->cursor_y >= screen_buffer->height)
1094 set_error( STATUS_INVALID_PARAMETER );
1095 return 0;
1097 if (screen_buffer->cursor_x != info->cursor_x || screen_buffer->cursor_y != info->cursor_y)
1099 screen_buffer->cursor_x = info->cursor_x;
1100 screen_buffer->cursor_y = info->cursor_y;
1101 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1102 memset( &evt.u, 0, sizeof(evt.u) );
1103 evt.u.cursor_pos.x = info->cursor_x;
1104 evt.u.cursor_pos.y = info->cursor_y;
1105 console_input_events_append( screen_buffer->input, &evt );
1108 if (params->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
1110 unsigned cc;
1112 /* new screen-buffer cannot be smaller than actual window */
1113 if (info->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
1114 info->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
1116 set_error( STATUS_INVALID_PARAMETER );
1117 return 0;
1119 /* FIXME: there are also some basic minimum and max size to deal with */
1120 if (!change_screen_buffer_size( screen_buffer, info->width, info->height )) return 0;
1122 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
1123 memset( &evt.u, 0, sizeof(evt.u) );
1124 evt.u.resize.width = info->width;
1125 evt.u.resize.height = info->height;
1126 console_input_events_append( screen_buffer->input, &evt );
1128 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1129 memset( &evt.u, 0, sizeof(evt.u) );
1130 evt.u.update.top = 0;
1131 evt.u.update.bottom = screen_buffer->height - 1;
1132 console_input_events_append( screen_buffer->input, &evt );
1134 /* scroll window to display sb */
1135 if (screen_buffer->win.right >= info->width)
1137 screen_buffer->win.right -= screen_buffer->win.left;
1138 screen_buffer->win.left = 0;
1140 if (screen_buffer->win.bottom >= info->height)
1142 screen_buffer->win.bottom -= screen_buffer->win.top;
1143 screen_buffer->win.top = 0;
1145 /* reset cursor if needed (normally, if cursor was outside of new sb, the
1146 * window has been shifted so that the new position of the cursor will be
1147 * visible */
1148 cc = 0;
1149 if (screen_buffer->cursor_x >= info->width)
1151 screen_buffer->cursor_x = info->width - 1;
1152 cc++;
1154 if (screen_buffer->cursor_y >= info->height)
1156 screen_buffer->cursor_y = info->height - 1;
1157 cc++;
1159 if (cc)
1161 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1162 memset( &evt.u, 0, sizeof(evt.u) );
1163 evt.u.cursor_pos.x = info->cursor_x;
1164 evt.u.cursor_pos.y = info->cursor_y;
1165 console_input_events_append( screen_buffer->input, &evt );
1168 if (screen_buffer == screen_buffer->input->active &&
1169 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
1171 INPUT_RECORD ir;
1172 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
1173 ir.Event.WindowBufferSizeEvent.dwSize.X = info->width;
1174 ir.Event.WindowBufferSizeEvent.dwSize.Y = info->height;
1175 write_console_input( screen_buffer->input, 1, &ir );
1178 if (params->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1180 screen_buffer->attr = info->attr;
1182 if (params->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1184 screen_buffer->popup_attr = info->popup_attr;
1186 if (params->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1188 if (info->win_left < 0 || info->win_left > info->win_right ||
1189 info->win_right >= screen_buffer->width ||
1190 info->win_top < 0 || info->win_top > info->win_bottom ||
1191 info->win_bottom >= screen_buffer->height)
1193 set_error( STATUS_INVALID_PARAMETER );
1194 return 0;
1196 if (screen_buffer->win.left != info->win_left || screen_buffer->win.top != info->win_top ||
1197 screen_buffer->win.right != info->win_right || screen_buffer->win.bottom != info->win_bottom)
1199 screen_buffer->win.left = info->win_left;
1200 screen_buffer->win.top = info->win_top;
1201 screen_buffer->win.right = info->win_right;
1202 screen_buffer->win.bottom = info->win_bottom;
1203 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1204 memset( &evt.u, 0, sizeof(evt.u) );
1205 evt.u.display.left = info->win_left;
1206 evt.u.display.top = info->win_top;
1207 evt.u.display.width = info->win_right - info->win_left + 1;
1208 evt.u.display.height = info->win_bottom - info->win_top + 1;
1209 console_input_events_append( screen_buffer->input, &evt );
1212 if (params->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1214 screen_buffer->max_width = info->max_width;
1215 screen_buffer->max_height = info->max_height;
1217 if (params->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1219 memcpy( screen_buffer->color_map, info->color_map, sizeof(info->color_map) );
1221 if (params->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1223 screen_buffer->font.width = info->font_width;
1224 screen_buffer->font.height = info->font_height;
1225 screen_buffer->font.weight = info->font_weight;
1226 screen_buffer->font.pitch_family = info->font_pitch_family;
1227 if (extra_size)
1229 extra_size = extra_size / sizeof(WCHAR) * sizeof(WCHAR);
1230 font_name = mem_alloc( extra_size );
1231 if (font_name)
1233 memcpy( font_name, info + 1, extra_size );
1234 free( screen_buffer->font.face_name );
1235 screen_buffer->font.face_name = font_name;
1236 screen_buffer->font.face_len = extra_size;
1241 return 1;
1244 /* appends a new line to history (history is a fixed size array) */
1245 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1247 struct history_line *ptr;
1249 if (!console || !console->history_size)
1251 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1252 return;
1255 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
1256 if (console->history_mode && console->history_index &&
1257 console->history[console->history_index - 1]->len == len &&
1258 !memcmp( console->history[console->history_index - 1]->text, buf, len ))
1260 /* don't duplicate entry */
1261 set_error( STATUS_ALIAS_EXISTS );
1262 return;
1264 if (!(ptr = mem_alloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
1265 ptr->len = len;
1266 memcpy( ptr->text, buf, len );
1268 if (console->history_index < console->history_size)
1270 console->history[console->history_index++] = ptr;
1272 else
1274 free( console->history[0]) ;
1275 memmove( &console->history[0], &console->history[1],
1276 (console->history_size - 1) * sizeof(*console->history) );
1277 console->history[console->history_size - 1] = ptr;
1281 /* returns a line from the cache */
1282 static data_size_t console_input_get_hist( struct console_input *console, int index )
1284 data_size_t ret = 0;
1286 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1287 else
1289 ret = console->history[index]->len;
1290 set_reply_data( console->history[index]->text, min( ret, get_reply_max_size() ));
1292 return ret;
1295 /* dumb dump */
1296 static void console_input_dump( struct object *obj, int verbose )
1298 struct console_input *console = (struct console_input *)obj;
1299 assert( obj->ops == &console_input_ops );
1300 fprintf( stderr, "Console input active=%p evt=%p\n",
1301 console->active, console->evt );
1304 static void console_input_destroy( struct object *obj )
1306 struct console_input* console_in = (struct console_input *)obj;
1307 struct screen_buffer* curr;
1308 int i;
1310 assert( obj->ops == &console_input_ops );
1312 if (console_in->server)
1314 assert( console_in->server->console == console_in );
1315 disconnect_console_server( console_in->server );
1318 free( console_in->title );
1319 free( console_in->records );
1321 if (console_in->active) release_object( console_in->active );
1322 console_in->active = NULL;
1324 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1326 if (curr->input == console_in) curr->input = NULL;
1329 free_async_queue( &console_in->ioctl_q );
1330 free_async_queue( &console_in->read_q );
1331 if (console_in->evt)
1332 console_in->evt->console = NULL;
1333 if (console_in->event)
1334 release_object( console_in->event );
1335 if (console_in->fd)
1336 release_object( console_in->fd );
1338 for (i = 0; i < console_in->history_size; i++)
1339 free( console_in->history[i] );
1340 free( console_in->history );
1343 static struct object *create_console_connection( struct console_input *console )
1345 struct console_connection *connection;
1347 if (current->process->console)
1349 set_error( STATUS_ACCESS_DENIED );
1350 return NULL;
1353 if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
1354 if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
1356 release_object( connection );
1357 return NULL;
1360 if (console)
1362 current->process->console = (struct console_input *)grab_object( console );
1363 console->num_proc++;
1366 return &connection->obj;
1369 static struct object *console_input_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1371 struct console_input *console = (struct console_input *)obj;
1372 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
1373 assert( obj->ops == &console_input_ops );
1375 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
1377 name->len = 0;
1378 return create_console_connection( console );
1381 return NULL;
1384 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1385 unsigned int sharing, unsigned int options )
1387 return grab_object( obj );
1390 static void screen_buffer_dump( struct object *obj, int verbose )
1392 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1393 assert( obj->ops == &screen_buffer_ops );
1395 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1398 static void screen_buffer_destroy( struct object *obj )
1400 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1402 assert( obj->ops == &screen_buffer_ops );
1404 list_remove( &screen_buffer->entry );
1405 if (screen_buffer->input && screen_buffer->input->server)
1406 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
1407 screen_buffer->id, NULL, NULL );
1408 if (screen_buffer->fd) release_object( screen_buffer->fd );
1409 free_async_queue( &screen_buffer->ioctl_q );
1410 free( screen_buffer->data );
1411 free( screen_buffer->font.face_name );
1414 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
1415 unsigned int sharing, unsigned int options )
1417 return grab_object( obj );
1420 static struct fd *screen_buffer_get_fd( struct object *obj )
1422 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1423 assert( obj->ops == &screen_buffer_ops );
1424 if (screen_buffer->fd)
1425 return (struct fd*)grab_object( screen_buffer->fd );
1426 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1427 return NULL;
1430 /* read data from a screen buffer */
1431 static void read_console_output( struct screen_buffer *screen_buffer, unsigned int x, unsigned int y,
1432 enum char_info_mode mode, unsigned int width )
1434 unsigned int i, count;
1435 char_info_t *src;
1437 if (x >= screen_buffer->width || y >= screen_buffer->height)
1439 if (width) set_error( STATUS_INVALID_PARAMETER );
1440 return;
1442 src = screen_buffer->data + y * screen_buffer->width + x;
1444 switch(mode)
1446 case CHAR_INFO_MODE_TEXT:
1448 WCHAR *data;
1449 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1450 get_reply_max_size() / sizeof(*data) );
1451 if ((data = set_reply_data_size( count * sizeof(*data) )))
1453 for (i = 0; i < count; i++) data[i] = src[i].ch;
1456 break;
1457 case CHAR_INFO_MODE_ATTR:
1459 unsigned short *data;
1460 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1461 get_reply_max_size() / sizeof(*data) );
1462 if ((data = set_reply_data_size( count * sizeof(*data) )))
1464 for (i = 0; i < count; i++) data[i] = src[i].attr;
1467 break;
1468 case CHAR_INFO_MODE_TEXTATTR:
1470 char_info_t *data;
1471 SMALL_RECT *region;
1472 if (!width || get_reply_max_size() < sizeof(*region))
1474 set_error( STATUS_INVALID_PARAMETER );
1475 return;
1477 count = min( (get_reply_max_size() - sizeof(*region)) / (width * sizeof(*data)), screen_buffer->height - y );
1478 width = min( width, screen_buffer->width - x );
1479 if (!(region = set_reply_data_size( sizeof(*region) + width * count * sizeof(*data) ))) return;
1480 region->Left = x;
1481 region->Top = y;
1482 region->Right = x + width - 1;
1483 region->Bottom = y + count - 1;
1484 data = (char_info_t *)(region + 1);
1485 for (i = 0; i < count; i++)
1487 memcpy( &data[i * width], &src[i * screen_buffer->width], width * sizeof(*data) );
1490 break;
1491 default:
1492 set_error( STATUS_INVALID_PARAMETER );
1493 break;
1497 /* write data into a screen buffer */
1498 static void write_console_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
1499 data_size_t size )
1501 unsigned int i, entry_size, entry_cnt, x, y;
1502 char_info_t *dest;
1503 char *src;
1505 entry_size = params->mode == CHAR_INFO_MODE_TEXTATTR ? sizeof(char_info_t) : sizeof(WCHAR);
1506 if (size % entry_size)
1508 set_error( STATUS_INVALID_PARAMETER );
1509 return;
1511 if (params->x >= screen_buffer->width) return;
1512 entry_cnt = size / entry_size;
1514 for (i = 0, src = (char *)(params + 1); i < entry_cnt; i++, src += entry_size)
1516 if (params->width)
1518 x = params->x + i % params->width;
1519 y = params->y + i / params->width;
1520 if (x >= screen_buffer->width) continue;
1522 else
1524 x = (params->x + i) % screen_buffer->width;
1525 y = params->y + (params->x + i) / screen_buffer->width;
1527 if (y >= screen_buffer->height) break;
1529 dest = &screen_buffer->data[y * screen_buffer->width + x];
1530 switch(params->mode)
1532 case CHAR_INFO_MODE_TEXT:
1533 dest->ch = *(const WCHAR *)src;
1534 break;
1535 case CHAR_INFO_MODE_ATTR:
1536 dest->attr = *(const unsigned short *)src;
1537 break;
1538 case CHAR_INFO_MODE_TEXTATTR:
1539 *dest = *(const char_info_t *)src;
1540 break;
1541 case CHAR_INFO_MODE_TEXTSTDATTR:
1542 dest->ch = *(const WCHAR *)src;
1543 dest->attr = screen_buffer->attr;
1544 break;
1545 default:
1546 set_error( STATUS_INVALID_PARAMETER );
1547 return;
1551 if (i && screen_buffer == screen_buffer->input->active)
1553 struct condrv_renderer_event evt;
1554 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1555 memset(&evt.u, 0, sizeof(evt.u));
1556 evt.u.update.top = params->y;
1557 evt.u.update.bottom = params->width
1558 ? min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1
1559 : params->y + (params->x + i - 1) / screen_buffer->width;
1560 console_input_events_append( screen_buffer->input, &evt );
1563 if (get_reply_max_size() == sizeof(SMALL_RECT))
1565 SMALL_RECT region;
1566 region.Left = params->x;
1567 region.Top = params->y;
1568 region.Right = min( params->x + params->width, screen_buffer->width ) - 1;
1569 region.Bottom = min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1;
1570 set_reply_data( &region, sizeof(region) );
1572 else set_reply_data( &i, sizeof(i) );
1575 /* fill a screen buffer with uniform data */
1576 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1577 enum char_info_mode mode, int x, int y, int count, int wrap )
1579 int i;
1580 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1582 if (y >= screen_buffer->height) return 0;
1584 if (wrap)
1585 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1586 else
1587 end = screen_buffer->data + (y+1) * screen_buffer->width;
1589 if (count > end - dest) count = end - dest;
1591 switch(mode)
1593 case CHAR_INFO_MODE_TEXT:
1594 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1595 break;
1596 case CHAR_INFO_MODE_ATTR:
1597 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1598 break;
1599 case CHAR_INFO_MODE_TEXTATTR:
1600 for (i = 0; i < count; i++) dest[i] = data;
1601 break;
1602 case CHAR_INFO_MODE_TEXTSTDATTR:
1603 for (i = 0; i < count; i++)
1605 dest[i].ch = data.ch;
1606 dest[i].attr = screen_buffer->attr;
1608 break;
1609 default:
1610 set_error( STATUS_INVALID_PARAMETER );
1611 return 0;
1614 if (count && screen_buffer == screen_buffer->input->active)
1616 struct condrv_renderer_event evt;
1617 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1618 memset(&evt.u, 0, sizeof(evt.u));
1619 evt.u.update.top = y;
1620 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1621 console_input_events_append( screen_buffer->input, &evt );
1623 return i;
1626 /* scroll parts of a screen buffer */
1627 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1628 int w, int h, const rectangle_t *clip, char_info_t fill )
1630 struct condrv_renderer_event evt;
1631 rectangle_t src, dst;
1632 int x, y;
1634 src.left = max( xsrc, clip->left );
1635 src.top = max( ysrc, clip->top );
1636 src.right = min( xsrc + w - 1, clip->right );
1637 src.bottom = min( ysrc + h - 1, clip->bottom );
1639 dst.left = xdst;
1640 dst.top = ydst;
1641 dst.right = xdst + w - 1;
1642 dst.bottom = ydst + h - 1;
1644 if (dst.left < clip->left)
1646 xsrc += clip->left - dst.left;
1647 w -= clip->left - dst.left;
1648 dst.left = clip->left;
1650 if (dst.top < clip->top)
1652 ysrc += clip->top - dst.top;
1653 h -= clip->top - dst.top;
1654 dst.top = clip->top;
1656 if (dst.right > clip->right) w -= dst.right - clip->right;
1657 if (dst.bottom > clip->bottom) h -= dst.bottom - clip->bottom;
1659 if (w > 0 && h > 0)
1661 if (ysrc < ydst)
1663 for (y = h; y > 0; y--)
1665 memcpy( &screen_buffer->data[(dst.top + y - 1) * screen_buffer->width + dst.left],
1666 &screen_buffer->data[(ysrc + y - 1) * screen_buffer->width + xsrc],
1667 w * sizeof(screen_buffer->data[0]) );
1670 else
1672 for (y = 0; y < h; y++)
1674 /* we use memmove here because when psrc and pdst are the same,
1675 * copies are done on the same row, so the dst and src blocks
1676 * can overlap */
1677 memmove( &screen_buffer->data[(dst.top + y) * screen_buffer->width + dst.left],
1678 &screen_buffer->data[(ysrc + y) * screen_buffer->width + xsrc],
1679 w * sizeof(screen_buffer->data[0]) );
1684 for (y = src.top; y <= src.bottom; y++)
1686 int left = src.left;
1687 int right = src.right;
1688 if (dst.top <= y && y <= dst.bottom)
1690 if (dst.left <= src.left) left = max( left, dst.right + 1 );
1691 if (dst.left >= src.left) right = min( right, dst.left - 1 );
1693 for (x = left; x <= right; x++) screen_buffer->data[y * screen_buffer->width + x] = fill;
1696 /* FIXME: this could be enhanced, by signalling scroll */
1697 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1698 memset(&evt.u, 0, sizeof(evt.u));
1699 evt.u.update.top = min( src.top, dst.top );
1700 evt.u.update.bottom = max( src.bottom, dst.bottom );
1701 console_input_events_append( screen_buffer->input, &evt );
1704 static void console_server_dump( struct object *obj, int verbose )
1706 assert( obj->ops == &console_server_ops );
1707 fprintf( stderr, "Console server\n" );
1710 static void console_server_destroy( struct object *obj )
1712 struct console_server *server = (struct console_server *)obj;
1713 assert( obj->ops == &console_server_ops );
1714 disconnect_console_server( server );
1715 if (server->fd) release_object( server->fd );
1718 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1720 struct console_server *server = (struct console_server*)obj;
1721 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
1722 assert( obj->ops == &console_server_ops );
1724 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
1726 struct screen_buffer *screen_buffer;
1727 name->len = 0;
1728 if (server->console)
1730 set_error( STATUS_INVALID_HANDLE );
1731 return 0;
1733 if (!(server->console = (struct console_input *)create_console_input())) return NULL;
1734 if (!(screen_buffer = (struct screen_buffer *)create_console_output( server->console )))
1736 release_object( server->console );
1737 server->console = NULL;
1738 return NULL;
1740 release_object( screen_buffer );
1741 server->console->server = server;
1743 return &server->console->obj;
1746 return NULL;
1749 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
1751 struct console_server *server = (struct console_server*)obj;
1752 assert( obj->ops == &console_server_ops );
1753 return !server->console || !list_empty( &server->queue );
1756 static struct fd *console_server_get_fd( struct object* obj )
1758 struct console_server *server = (struct console_server*)obj;
1759 assert( obj->ops == &console_server_ops );
1760 return (struct fd *)grab_object( server->fd );
1763 static struct object *console_server_open_file( struct object *obj, unsigned int access,
1764 unsigned int sharing, unsigned int options )
1766 return grab_object( obj );
1769 static struct object *create_console_server( void )
1771 struct console_server *server;
1773 if (!(server = alloc_object( &console_server_ops ))) return NULL;
1774 server->console = NULL;
1775 server->busy = 0;
1776 server->term_fd = -1;
1777 list_init( &server->queue );
1778 list_init( &server->read_queue );
1779 server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
1780 if (!server->fd)
1782 release_object( server );
1783 return NULL;
1785 allow_fd_caching(server->fd);
1787 return &server->obj;
1790 static int is_blocking_read_ioctl( unsigned int code )
1792 return code == IOCTL_CONDRV_READ_INPUT || code == IOCTL_CONDRV_READ_CONSOLE;
1795 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1797 struct console_input *console = get_fd_user( fd );
1799 switch (code)
1801 case IOCTL_CONDRV_GET_MODE:
1802 if (console->server)
1803 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1804 if (get_reply_max_size() != sizeof(console->mode))
1806 set_error( STATUS_INVALID_PARAMETER );
1807 return 0;
1809 return set_reply_data( &console->mode, sizeof(console->mode) ) != NULL;
1811 case IOCTL_CONDRV_SET_MODE:
1812 if (console->server)
1813 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1814 if (get_req_data_size() != sizeof(console->mode))
1816 set_error( STATUS_INVALID_PARAMETER );
1817 return 0;
1819 console->mode = *(unsigned int *)get_req_data();
1820 return 1;
1822 case IOCTL_CONDRV_READ_INPUT:
1824 int blocking = 0;
1825 if (console->server)
1826 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1827 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1829 set_error( STATUS_INVALID_PARAMETER );
1830 return 0;
1832 if (get_req_data_size())
1834 if (get_req_data_size() != sizeof(int))
1836 set_error( STATUS_INVALID_PARAMETER );
1837 return 0;
1839 blocking = *(int *)get_req_data();
1841 set_error( STATUS_PENDING );
1842 if (blocking && !console->recnum)
1844 queue_async( &console->read_q, async );
1845 return 1;
1847 return read_console_input( console, async, 1 );
1850 case IOCTL_CONDRV_WRITE_INPUT:
1851 if (console->server)
1852 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1853 return write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD), get_req_data() );
1855 case IOCTL_CONDRV_PEEK:
1856 if (console->server)
1857 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1858 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1860 set_error( STATUS_INVALID_PARAMETER );
1861 return 0;
1863 set_error( STATUS_PENDING );
1864 return read_console_input( console, async, 0 );
1866 case IOCTL_CONDRV_GET_INPUT_INFO:
1868 struct condrv_input_info info;
1869 if (console->server)
1870 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1871 if (get_reply_max_size() != sizeof(info))
1873 set_error( STATUS_INVALID_PARAMETER );
1874 return 0;
1876 info.input_cp = console->input_cp;
1877 info.output_cp = console->output_cp;
1878 info.history_mode = console->history_mode;
1879 info.history_size = console->history_size;
1880 info.history_index = console->history_index;
1881 info.edition_mode = console->edition_mode;
1882 info.input_count = console->recnum;
1883 info.win = console->win;
1884 return set_reply_data( &info, sizeof(info) ) != NULL;
1887 case IOCTL_CONDRV_SET_INPUT_INFO:
1889 const struct condrv_input_info_params *params = get_req_data();
1890 if (console->server)
1891 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1892 if (get_req_data_size() != sizeof(*params))
1894 set_error( STATUS_INVALID_PARAMETER );
1895 return 0;
1897 if (params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
1899 console->history_mode = params->info.history_mode;
1901 if ((params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
1902 console->history_size != params->info.history_size)
1904 struct history_line **mem = NULL;
1905 int i, delta;
1907 if (params->info.history_size)
1909 if (!(mem = mem_alloc( params->info.history_size * sizeof(*mem) ))) return 0;
1910 memset( mem, 0, params->info.history_size * sizeof(*mem) );
1913 delta = (console->history_index > params->info.history_size) ?
1914 (console->history_index - params->info.history_size) : 0;
1916 for (i = delta; i < console->history_index; i++)
1918 mem[i - delta] = console->history[i];
1919 console->history[i] = NULL;
1921 console->history_index -= delta;
1923 for (i = 0; i < console->history_size; i++)
1924 free( console->history[i] );
1925 free( console->history );
1926 console->history = mem;
1927 console->history_size = params->info.history_size;
1929 if (params->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
1931 console->edition_mode = params->info.edition_mode;
1933 if (params->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
1935 console->input_cp = params->info.input_cp;
1937 if (params->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
1939 console->output_cp = params->info.output_cp;
1941 if (params->mask & SET_CONSOLE_INPUT_INFO_WIN)
1943 console->win = params->info.win;
1945 return 1;
1948 case IOCTL_CONDRV_GET_TITLE:
1949 if (console->server)
1950 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1951 if (!console->title_len) return 1;
1952 return set_reply_data( console->title, min( console->title_len, get_reply_max_size() )) != NULL;
1954 case IOCTL_CONDRV_SET_TITLE:
1956 data_size_t len = get_req_data_size();
1957 struct condrv_renderer_event evt;
1958 WCHAR *title = NULL;
1960 if (console->server)
1961 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1962 if (len % sizeof(WCHAR))
1964 set_error( STATUS_INVALID_PARAMETER );
1965 return 0;
1968 if (len && !(title = memdup( get_req_data(), len ))) return 0;
1969 free( console->title );
1970 console->title = title;
1971 console->title_len = len;
1972 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
1973 console_input_events_append( console, &evt );
1974 return 1;
1977 case IOCTL_CONDRV_CTRL_EVENT:
1979 const struct condrv_ctrl_event *event = get_req_data();
1980 process_id_t group;
1981 if (get_req_data_size() != sizeof(*event))
1983 set_error( STATUS_INVALID_PARAMETER );
1984 return 0;
1986 group = event->group_id ? event->group_id : current->process->group_id;
1987 if (!group)
1989 set_error( STATUS_INVALID_PARAMETER );
1990 return 0;
1992 propagate_console_signal( console, event->event, group );
1993 return !get_error();
1996 default:
1997 if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
1999 set_error( STATUS_INVALID_HANDLE );
2000 return 0;
2002 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
2006 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2008 struct screen_buffer *screen_buffer = get_fd_user( fd );
2010 switch (code)
2012 case IOCTL_CONDRV_GET_MODE:
2013 if (screen_buffer->input && screen_buffer->input->server)
2014 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2015 async, &screen_buffer->ioctl_q );
2016 if (get_reply_max_size() != sizeof(screen_buffer->mode))
2018 set_error( STATUS_INVALID_PARAMETER );
2019 return 0;
2021 return set_reply_data( &screen_buffer->mode, sizeof(screen_buffer->mode) ) != NULL;
2023 case IOCTL_CONDRV_SET_MODE:
2024 if (screen_buffer->input && screen_buffer->input->server)
2025 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2026 async, &screen_buffer->ioctl_q );
2027 if (get_req_data_size() != sizeof(screen_buffer->mode))
2029 set_error( STATUS_INVALID_PARAMETER );
2030 return 0;
2032 screen_buffer->mode = *(unsigned int *)get_req_data();
2033 return 1;
2035 case IOCTL_CONDRV_READ_OUTPUT:
2037 const struct condrv_output_params *params = get_req_data();
2038 if (screen_buffer->input && screen_buffer->input->server)
2039 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2040 async, &screen_buffer->ioctl_q );
2041 if (get_req_data_size() != sizeof(*params))
2043 set_error( STATUS_INVALID_PARAMETER );
2044 return 0;
2046 if (console_input_is_bare( screen_buffer->input ))
2048 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2049 return 0;
2051 read_console_output( screen_buffer, params->x, params->y, params->mode, params->width );
2052 return !get_error();
2055 case IOCTL_CONDRV_WRITE_OUTPUT:
2056 if (screen_buffer->input && screen_buffer->input->server)
2057 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2058 async, &screen_buffer->ioctl_q );
2059 if (get_req_data_size() < sizeof(struct condrv_output_params) ||
2060 (get_reply_max_size() != sizeof(SMALL_RECT) && get_reply_max_size() != sizeof(unsigned int)))
2062 set_error( STATUS_INVALID_PARAMETER );
2063 return 0;
2065 if (console_input_is_bare( screen_buffer->input ))
2067 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2068 return 0;
2070 write_console_output( screen_buffer, get_req_data(), get_req_data_size() - sizeof(struct condrv_output_params) );
2071 return !get_error();
2073 case IOCTL_CONDRV_GET_OUTPUT_INFO:
2075 struct condrv_output_info *info;
2076 data_size_t size;
2078 if (screen_buffer->input && screen_buffer->input->server)
2079 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2080 async, &screen_buffer->ioctl_q );
2081 size = min( sizeof(*info) + screen_buffer->font.face_len, get_reply_max_size() );
2082 if (size < sizeof(*info))
2084 set_error( STATUS_INVALID_PARAMETER );
2085 return 0;
2087 if (!(info = set_reply_data_size( size ))) return 0;
2089 info->cursor_size = screen_buffer->cursor_size;
2090 info->cursor_visible = screen_buffer->cursor_visible;
2091 info->cursor_x = screen_buffer->cursor_x;
2092 info->cursor_y = screen_buffer->cursor_y;
2093 info->width = screen_buffer->width;
2094 info->height = screen_buffer->height;
2095 info->attr = screen_buffer->attr;
2096 info->popup_attr = screen_buffer->popup_attr;
2097 info->win_left = screen_buffer->win.left;
2098 info->win_top = screen_buffer->win.top;
2099 info->win_right = screen_buffer->win.right;
2100 info->win_bottom = screen_buffer->win.bottom;
2101 info->max_width = screen_buffer->max_width;
2102 info->max_height = screen_buffer->max_height;
2103 info->font_width = screen_buffer->font.width;
2104 info->font_height = screen_buffer->font.height;
2105 info->font_weight = screen_buffer->font.weight;
2106 info->font_pitch_family = screen_buffer->font.pitch_family;
2107 memcpy( info->color_map, screen_buffer->color_map, sizeof(info->color_map) );
2108 size -= sizeof(*info);
2109 if (size) memcpy( info + 1, screen_buffer->font.face_name, size );
2110 return 1;
2113 case IOCTL_CONDRV_SET_OUTPUT_INFO:
2115 const struct condrv_output_info_params *params = get_req_data();
2116 if (screen_buffer->input && screen_buffer->input->server)
2117 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2118 async, &screen_buffer->ioctl_q );
2119 if (get_req_data_size() < sizeof(*params))
2121 set_error( STATUS_INVALID_PARAMETER );
2122 return 0;
2124 if (!screen_buffer->input)
2126 set_error( STATUS_INVALID_HANDLE );
2127 return 0;
2129 return set_output_info( screen_buffer, params, get_req_data_size() - sizeof(*params) );
2132 case IOCTL_CONDRV_ACTIVATE:
2133 if (!screen_buffer->input)
2135 set_error( STATUS_INVALID_HANDLE );
2136 return 0;
2139 set_active_screen_buffer( screen_buffer->input, screen_buffer );
2140 return 1;
2142 case IOCTL_CONDRV_FILL_OUTPUT:
2144 const struct condrv_fill_output_params *params = get_req_data();
2145 char_info_t data;
2146 DWORD written;
2147 if (screen_buffer->input && screen_buffer->input->server)
2148 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2149 async, &screen_buffer->ioctl_q );
2150 if (get_req_data_size() != sizeof(*params) ||
2151 (get_reply_max_size() && get_reply_max_size() != sizeof(written)))
2153 set_error( STATUS_INVALID_PARAMETER );
2154 return 0;
2156 data.ch = params->ch;
2157 data.attr = params->attr;
2158 written = fill_console_output( screen_buffer, data, params->mode,
2159 params->x, params->y, params->count, params->wrap );
2160 if (written && get_reply_max_size() == sizeof(written))
2161 set_reply_data( &written, sizeof(written) );
2162 return !get_error();
2165 case IOCTL_CONDRV_SCROLL:
2167 const struct condrv_scroll_params *params = get_req_data();
2168 rectangle_t clip;
2170 if (screen_buffer->input && screen_buffer->input->server)
2171 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2172 async, &screen_buffer->ioctl_q );
2174 if (get_req_data_size() != sizeof(*params))
2176 set_error( STATUS_INVALID_PARAMETER );
2177 return 0;
2179 if (console_input_is_bare( screen_buffer->input ) || !screen_buffer->input)
2181 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2182 return 0;
2184 clip.left = max( params->clip.Left, 0 );
2185 clip.top = max( params->clip.Top, 0 );
2186 clip.right = min( params->clip.Right, screen_buffer->width - 1 );
2187 clip.bottom = min( params->clip.Bottom, screen_buffer->height - 1 );
2188 if (clip.left > clip.right || clip.top > clip.bottom || params->scroll.Left < 0 || params->scroll.Top < 0 ||
2189 params->scroll.Right >= screen_buffer->width || params->scroll.Bottom >= screen_buffer->height ||
2190 params->scroll.Right < params->scroll.Left || params->scroll.Top > params->scroll.Bottom ||
2191 params->origin.X < 0 || params->origin.X >= screen_buffer->width || params->origin.Y < 0 ||
2192 params->origin.Y >= screen_buffer->height)
2194 set_error( STATUS_INVALID_PARAMETER );
2195 return 0;
2198 scroll_console_output( screen_buffer, params->scroll.Left, params->scroll.Top, params->origin.X, params->origin.Y,
2199 params->scroll.Right - params->scroll.Left + 1, params->scroll.Bottom - params->scroll.Top + 1,
2200 &clip, params->fill );
2201 return !get_error();
2204 default:
2205 if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE)
2207 set_error( STATUS_INVALID_HANDLE );
2208 return 0;
2210 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
2211 async, &screen_buffer->ioctl_q );
2215 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2217 struct console_input_events *evts = get_fd_user( fd );
2219 switch (code)
2221 case IOCTL_CONDRV_GET_RENDERER_EVENTS:
2222 set_error( STATUS_PENDING );
2223 if (evts->num_used) return get_renderer_events( evts, async );
2224 queue_async( &evts->read_q, async );
2225 return 1;
2227 case IOCTL_CONDRV_ATTACH_RENDERER:
2229 struct console_input *console_input;
2230 if (get_req_data_size() != sizeof(condrv_handle_t))
2232 set_error( STATUS_INVALID_PARAMETER );
2233 return 0;
2235 console_input = (struct console_input *)get_handle_obj( current->process, *(condrv_handle_t *)get_req_data(),
2236 0, &console_input_ops );
2237 if (!console_input) return 0;
2239 if (!console_input->evt && !evts->console)
2241 console_input->evt = evts;
2242 console_input->renderer = current;
2243 evts->console = console_input;
2245 else set_error( STATUS_INVALID_HANDLE );
2247 release_object( console_input );
2248 return !get_error();
2251 case IOCTL_CONDRV_SCROLL:
2252 case IOCTL_CONDRV_SET_MODE:
2253 case IOCTL_CONDRV_WRITE_OUTPUT:
2254 case IOCTL_CONDRV_READ_OUTPUT:
2255 case IOCTL_CONDRV_FILL_OUTPUT:
2256 case IOCTL_CONDRV_GET_OUTPUT_INFO:
2257 case IOCTL_CONDRV_SET_OUTPUT_INFO:
2258 if (!evts->console || !evts->console->active)
2260 set_error( STATUS_INVALID_HANDLE );
2261 return 0;
2263 return screen_buffer_ioctl( evts->console->active->fd, code, async );
2265 default:
2266 if (!evts->console)
2268 set_error( STATUS_INVALID_HANDLE );
2269 return 0;
2271 return console_input_ioctl( evts->console->fd, code, async );
2275 static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2277 struct console_connection *console_connection = get_fd_user( fd );
2279 switch (code)
2281 case IOCTL_CONDRV_BIND_PID:
2283 struct process *process;
2284 unsigned int pid;
2285 if (get_req_data_size() != sizeof(unsigned int))
2287 set_error( STATUS_INVALID_PARAMETER );
2288 return 0;
2290 if (current->process->console)
2292 set_error( STATUS_INVALID_HANDLE );
2293 return 0;
2296 pid = *(unsigned int *)get_req_data();
2297 if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
2298 if (!(process = get_process_from_id( pid ))) return 0;
2300 if (process->console)
2302 current->process->console = (struct console_input *)grab_object( process->console );
2303 process->console->num_proc++;
2305 else set_error( STATUS_ACCESS_DENIED );
2306 release_object( process );
2307 return !get_error();
2310 default:
2311 return default_fd_ioctl( console_connection->fd, code, async );
2315 static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2317 struct console_server *server = get_fd_user( fd );
2319 switch (code)
2321 case IOCTL_CONDRV_CTRL_EVENT:
2323 const struct condrv_ctrl_event *event = get_req_data();
2324 if (get_req_data_size() != sizeof(*event))
2326 set_error( STATUS_INVALID_PARAMETER );
2327 return 0;
2329 if (!server->console)
2331 set_error( STATUS_INVALID_HANDLE );
2332 return 0;
2334 propagate_console_signal( server->console, event->event, event->group_id );
2335 return !get_error();
2338 case IOCTL_CONDRV_SETUP_INPUT:
2340 struct termios term;
2341 obj_handle_t handle;
2342 struct file *file;
2343 int unix_fd;
2345 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
2347 set_error( STATUS_INVALID_PARAMETER );
2348 return 0;
2350 if (server->term_fd != -1)
2352 tcsetattr( server->term_fd, TCSANOW, &server->termios );
2353 close( server->term_fd );
2354 server->term_fd = -1;
2356 handle = *(unsigned int *)get_req_data();
2357 if (!handle) return 1;
2358 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
2360 return 0;
2362 unix_fd = get_file_unix_fd( file );
2363 release_object( file );
2365 if (tcgetattr( unix_fd, &server->termios ))
2367 file_set_error();
2368 return 0;
2370 term = server->termios;
2371 term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
2372 term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
2373 term.c_cflag &= ~(CSIZE | PARENB);
2374 term.c_cflag |= CS8;
2375 term.c_cc[VMIN] = 1;
2376 term.c_cc[VTIME] = 0;
2377 if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
2379 file_set_error();
2380 return 0;
2382 return 1;
2385 default:
2386 set_error( STATUS_INVALID_HANDLE );
2387 return 0;
2391 static void console_connection_dump( struct object *obj, int verbose )
2393 fputs( "console connection\n", stderr );
2396 static struct fd *console_connection_get_fd( struct object *obj )
2398 struct console_connection *connection = (struct console_connection *)obj;
2399 return (struct fd *)grab_object( connection->fd );
2402 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
2404 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
2406 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
2408 if (!current->process->console)
2410 set_error( STATUS_INVALID_HANDLE );
2411 return NULL;
2413 name->len = 0;
2414 return grab_object( current->process->console );
2417 return NULL;
2420 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
2421 unsigned int sharing, unsigned int options )
2423 return grab_object( obj );
2426 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
2428 free_console( process );
2429 return 1;
2432 static void console_connection_destroy( struct object *obj )
2434 struct console_connection *connection = (struct console_connection *)obj;
2435 if (connection->fd) release_object( connection->fd );
2438 static struct object_type *console_device_get_type( struct object *obj )
2440 static const WCHAR name[] = {'D','e','v','i','c','e'};
2441 static const struct unicode_str str = { name, sizeof(name) };
2442 return get_object_type( &str );
2445 static void console_device_dump( struct object *obj, int verbose )
2447 fputs( "Console device\n", stderr );
2450 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
2452 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
2453 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
2454 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
2455 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
2456 static const WCHAR rendererW[] = {'R','e','n','d','e','r','e','r'};
2457 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
2458 static const WCHAR serverW[] = {'S','e','r','v','e','r'};
2460 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
2462 if (!current->process->console)
2464 set_error( STATUS_INVALID_HANDLE );
2465 return NULL;
2467 name->len = 0;
2468 return grab_object( current->process->console );
2471 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
2473 if (!current->process->console || !current->process->console->active)
2475 set_error( STATUS_INVALID_HANDLE );
2476 return NULL;
2478 name->len = 0;
2479 return grab_object( current->process->console->active );
2482 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
2484 name->len = 0;
2485 return grab_object( obj );
2488 if (name->len == sizeof(rendererW) && !memcmp( name->str, rendererW, name->len ))
2490 name->len = 0;
2491 return create_console_input_events();
2494 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
2496 if (!current->process->console)
2498 set_error( STATUS_INVALID_HANDLE );
2499 return NULL;
2501 name->len = 0;
2502 return create_console_output( current->process->console );
2505 if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
2507 name->len = 0;
2508 return create_console_server();
2511 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
2513 name->len = 0;
2514 return create_console_connection( NULL );
2517 return NULL;
2520 static struct object *console_device_open_file( struct object *obj, unsigned int access,
2521 unsigned int sharing, unsigned int options )
2523 int is_output;
2524 access = default_fd_map_access( obj, access );
2525 is_output = access & FILE_WRITE_DATA;
2526 if (!current->process->console || (is_output && !current->process->console))
2528 set_error( STATUS_INVALID_HANDLE );
2529 return NULL;
2531 if (is_output && (access & FILE_READ_DATA))
2533 set_error( STATUS_INVALID_PARAMETER );
2534 return NULL;
2536 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
2539 struct object *create_console_device( struct object *root, const struct unicode_str *name,
2540 unsigned int attr, const struct security_descriptor *sd )
2542 return create_named_object( root, &console_device_ops, name, attr, sd );
2545 /* allocate a console for the renderer */
2546 DECL_HANDLER(alloc_console)
2548 struct process *process;
2549 struct console_input *console;
2550 int attach = 0;
2552 switch (req->pid)
2554 case 0:
2555 /* console to be attached to parent process */
2556 if (!(process = get_process_from_id( current->process->parent_id )))
2558 set_error( STATUS_ACCESS_DENIED );
2559 return;
2561 attach = 1;
2562 break;
2563 default:
2564 /* console to be attached to req->pid */
2565 if (!(process = get_process_from_id( req->pid ))) return;
2568 if (attach && process->console)
2570 set_error( STATUS_ACCESS_DENIED );
2572 else if ((console = (struct console_input*)create_console_input()))
2574 if ((reply->handle_in = alloc_handle( current->process, console, req->access,
2575 req->attributes )) && attach)
2577 process->console = (struct console_input*)grab_object( console );
2578 console->num_proc++;
2580 release_object( console );
2582 release_object( process );
2585 /* free the console of the current process */
2586 DECL_HANDLER(free_console)
2588 free_console( current->process );
2591 /* appends a string to console's history */
2592 DECL_HANDLER(append_console_input_history)
2594 struct console_input *console;
2596 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
2597 console_input_append_hist( console, get_req_data(), get_req_data_size() );
2598 release_object( console );
2601 /* appends a string to console's history */
2602 DECL_HANDLER(get_console_input_history)
2604 struct console_input *console;
2606 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
2607 reply->total = console_input_get_hist( console, req->index );
2608 release_object( console );
2611 /* creates a screen buffer */
2612 DECL_HANDLER(create_console_output)
2614 struct console_input *console;
2615 struct object *screen_buffer;
2617 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
2618 return;
2620 screen_buffer = create_console_output( console );
2621 if (screen_buffer)
2623 /* FIXME: should store sharing and test it when opening the CONOUT$ device
2624 * see file.c on how this could be done */
2625 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
2626 release_object( screen_buffer );
2628 release_object( console );
2631 /* get console which renderer is 'current' */
2632 static int cgwe_enum( struct process* process, void* user)
2634 if (process->console && process->console->renderer == current)
2636 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
2637 return 1;
2639 return 0;
2642 DECL_HANDLER(get_console_wait_event)
2644 struct console_input* console = NULL;
2645 struct object *obj;
2647 if (req->handle)
2649 if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
2650 if (obj->ops == &console_input_ops)
2651 console = (struct console_input *)grab_object( obj );
2652 else if (obj->ops == &screen_buffer_ops)
2653 console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
2654 else
2655 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2656 release_object( obj );
2658 else if (current->process->console)
2659 console = (struct console_input *)grab_object( current->process->console );
2660 else enum_processes(cgwe_enum, &console);
2662 if (console)
2664 reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
2665 release_object( console );
2667 else set_error( STATUS_INVALID_PARAMETER );
2670 /* retrieve the next pending console ioctl request */
2671 DECL_HANDLER(get_next_console_request)
2673 struct console_host_ioctl *ioctl = NULL, *next;
2674 struct console_server *server;
2675 struct iosb *iosb = NULL;
2677 server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
2678 if (!server) return;
2680 if (!server->console)
2682 set_error( STATUS_INVALID_HANDLE );
2683 release_object( server );
2684 return;
2687 if (req->signal) set_event( server->console->event);
2688 else reset_event( server->console->event );
2690 if (req->read)
2692 /* set result of current pending ioctl */
2693 if (list_empty( &server->read_queue ))
2695 set_error( STATUS_INVALID_HANDLE );
2696 release_object( server );
2697 return;
2700 ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
2701 list_remove( &ioctl->entry );
2702 list_move_tail( &server->queue, &server->read_queue );
2704 else if (server->busy)
2706 /* set result of previous ioctl */
2707 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
2708 list_remove( &ioctl->entry );
2711 if (ioctl)
2713 unsigned int status = req->status;
2714 if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
2715 if (ioctl->async)
2717 iosb = async_get_iosb( ioctl->async );
2718 iosb->status = status;
2719 iosb->out_size = min( iosb->out_size, get_req_data_size() );
2720 if (iosb->out_size)
2722 if ((iosb->out_data = memdup( get_req_data(), iosb->out_size )))
2724 iosb->result = iosb->out_size;
2725 status = STATUS_ALERTED;
2727 else if (!status)
2729 iosb->status = STATUS_NO_MEMORY;
2730 iosb->out_size = 0;
2734 console_host_ioctl_terminate( ioctl, status );
2735 if (iosb) release_object( iosb );
2737 if (req->read)
2739 release_object( server );
2740 return;
2742 server->busy = 0;
2745 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
2746 * move it to read queue for execution after current read is complete. move all blocking
2747 * ioctl at the same time to preserve their order. */
2748 if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
2750 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
2751 if (is_blocking_read_ioctl( ioctl->code ))
2753 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
2755 if (!is_blocking_read_ioctl( ioctl->code )) continue;
2756 list_remove( &ioctl->entry );
2757 list_add_tail( &server->read_queue, &ioctl->entry );
2762 /* return the next ioctl */
2763 if (!list_empty( &server->queue ))
2765 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
2766 iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
2768 if (!iosb || get_reply_max_size() >= iosb->in_size)
2770 reply->code = ioctl->code;
2771 reply->output = ioctl->output;
2773 if (iosb)
2775 reply->out_size = iosb->out_size;
2776 set_reply_data_ptr( iosb->in_data, iosb->in_size );
2777 iosb->in_data = NULL;
2780 if (is_blocking_read_ioctl( ioctl->code ))
2782 list_remove( &ioctl->entry );
2783 assert( list_empty( &server->read_queue ));
2784 list_add_tail( &server->read_queue, &ioctl->entry );
2786 else server->busy = 1;
2788 else
2790 reply->out_size = iosb->in_size;
2791 set_error( STATUS_BUFFER_OVERFLOW );
2793 if (iosb) release_object( iosb );
2795 else
2797 set_error( STATUS_PENDING );
2800 release_object( server );