fsutil: Initialize ret in hardlink() (Coverity).
[wine.git] / server / console.c
blob4509905c2c0c04f3ef102c48134ea335673347ca
1 /*
2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
5 * 2001 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "handle.h"
35 #include "process.h"
36 #include "request.h"
37 #include "file.h"
38 #include "unicode.h"
39 #include "wincon.h"
40 #include "winternl.h"
41 #include "wine/condrv.h"
43 struct screen_buffer;
44 struct console_input_events;
46 struct history_line
48 data_size_t len;
49 WCHAR text[1];
52 struct console_input
54 struct object obj; /* object header */
55 int num_proc; /* number of processes attached to this console */
56 struct thread *renderer; /* console renderer thread */
57 int mode; /* input mode */
58 struct screen_buffer *active; /* active screen buffer */
59 int recnum; /* number of input records */
60 INPUT_RECORD *records; /* input records */
61 struct console_input_events *evt; /* synchronization event with renderer */
62 WCHAR *title; /* console title */
63 data_size_t title_len; /* length of console title */
64 struct history_line **history; /* lines history */
65 int history_size; /* number of entries in history array */
66 int history_index; /* number of used entries in history array */
67 int history_mode; /* mode of history (non zero means remove doubled strings */
68 int edition_mode; /* index to edition mode flavors */
69 int input_cp; /* console input codepage */
70 int output_cp; /* console output codepage */
71 user_handle_t win; /* window handle if backend supports it */
72 struct event *event; /* event to wait on for input queue */
73 struct fd *fd; /* for bare console, attached input fd */
74 struct async_queue read_q; /* read queue */
77 static void console_input_dump( struct object *obj, int verbose );
78 static void console_input_destroy( struct object *obj );
79 static struct fd *console_input_get_fd( struct object *obj );
80 static struct object *console_input_open_file( struct object *obj, unsigned int access,
81 unsigned int sharing, unsigned int options );
83 static const struct object_ops console_input_ops =
85 sizeof(struct console_input), /* size */
86 console_input_dump, /* dump */
87 no_get_type, /* get_type */
88 no_add_queue, /* add_queue */
89 NULL, /* remove_queue */
90 NULL, /* signaled */
91 no_satisfied, /* satisfied */
92 no_signal, /* signal */
93 console_input_get_fd, /* get_fd */
94 default_fd_map_access, /* map_access */
95 default_get_sd, /* get_sd */
96 default_set_sd, /* set_sd */
97 no_lookup_name, /* lookup_name */
98 no_link_name, /* link_name */
99 NULL, /* unlink_name */
100 console_input_open_file, /* open_file */
101 no_kernel_obj_list, /* get_kernel_obj_list */
102 no_close_handle, /* close_handle */
103 console_input_destroy /* destroy */
106 static enum server_fd_type console_get_fd_type( struct fd *fd );
107 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
109 static const struct fd_ops console_input_fd_ops =
111 default_fd_get_poll_events, /* get_poll_events */
112 default_poll_event, /* poll_event */
113 console_get_fd_type, /* get_fd_type */
114 no_fd_read, /* read */
115 no_fd_write, /* write */
116 no_fd_flush, /* flush */
117 no_fd_get_file_info, /* get_file_info */
118 no_fd_get_volume_info, /* get_volume_info */
119 console_input_ioctl, /* ioctl */
120 default_fd_queue_async, /* queue_async */
121 default_fd_reselect_async /* reselect_async */
124 static void console_input_events_dump( struct object *obj, int verbose );
125 static void console_input_events_destroy( struct object *obj );
126 static struct fd *console_input_events_get_fd( struct object *obj );
127 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
128 unsigned int sharing, unsigned int options );
130 struct console_input_events
132 struct object obj; /* object header */
133 struct fd *fd; /* pseudo-fd for ioctls */
134 struct console_input *console; /* attached console */
135 int num_alloc; /* number of allocated events */
136 int num_used; /* number of actually used events */
137 struct condrv_renderer_event *events;
138 struct async_queue read_q; /* read queue */
141 static const struct object_ops console_input_events_ops =
143 sizeof(struct console_input_events), /* size */
144 console_input_events_dump, /* dump */
145 no_get_type, /* get_type */
146 add_queue, /* add_queue */
147 remove_queue, /* remove_queue */
148 NULL, /* signaled */
149 no_satisfied, /* satisfied */
150 no_signal, /* signal */
151 console_input_events_get_fd, /* get_fd */
152 default_fd_map_access, /* map_access */
153 default_get_sd, /* get_sd */
154 default_set_sd, /* set_sd */
155 no_lookup_name, /* lookup_name */
156 no_link_name, /* link_name */
157 NULL, /* unlink_name */
158 console_input_events_open_file, /* open_file */
159 no_kernel_obj_list, /* get_kernel_obj_list */
160 no_close_handle, /* close_handle */
161 console_input_events_destroy /* destroy */
164 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
166 static const struct fd_ops console_input_events_fd_ops =
168 default_fd_get_poll_events, /* get_poll_events */
169 default_poll_event, /* poll_event */
170 console_get_fd_type, /* get_fd_type */
171 no_fd_read, /* read */
172 no_fd_write, /* write */
173 no_fd_flush, /* flush */
174 no_fd_get_file_info, /* get_file_info */
175 no_fd_get_volume_info, /* get_volume_info */
176 console_input_events_ioctl, /* ioctl */
177 default_fd_queue_async, /* queue_async */
178 default_fd_reselect_async /* reselect_async */
181 struct font_info
183 short int width;
184 short int height;
185 short int weight;
186 short int pitch_family;
187 WCHAR *face_name;
188 data_size_t face_len;
191 struct screen_buffer
193 struct object obj; /* object header */
194 struct list entry; /* entry in list of all screen buffers */
195 struct console_input *input; /* associated console input */
196 unsigned int mode; /* output mode */
197 int cursor_size; /* size of cursor (percentage filled) */
198 int cursor_visible;/* cursor visibility flag */
199 int cursor_x; /* position of cursor */
200 int cursor_y; /* position of cursor */
201 int width; /* size (w-h) of the screen buffer */
202 int height;
203 int max_width; /* size (w-h) of the window given font size */
204 int max_height;
205 char_info_t *data; /* the data for each cell - a width x height matrix */
206 unsigned short attr; /* default fill attributes (screen colors) */
207 unsigned short popup_attr; /* pop-up color attributes */
208 unsigned int color_map[16]; /* color table */
209 rectangle_t win; /* current visible window on the screen buffer *
210 * as seen in wineconsole */
211 struct font_info font; /* console font information */
212 struct fd *fd; /* for bare console, attached output fd */
215 static void screen_buffer_dump( struct object *obj, int verbose );
216 static void screen_buffer_destroy( struct object *obj );
217 static struct fd *screen_buffer_get_fd( struct object *obj );
218 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
219 unsigned int sharing, unsigned int options );
221 static const struct object_ops screen_buffer_ops =
223 sizeof(struct screen_buffer), /* size */
224 screen_buffer_dump, /* dump */
225 no_get_type, /* get_type */
226 no_add_queue, /* add_queue */
227 NULL, /* remove_queue */
228 NULL, /* signaled */
229 NULL, /* satisfied */
230 no_signal, /* signal */
231 screen_buffer_get_fd, /* get_fd */
232 default_fd_map_access, /* map_access */
233 default_get_sd, /* get_sd */
234 default_set_sd, /* set_sd */
235 no_lookup_name, /* lookup_name */
236 no_link_name, /* link_name */
237 NULL, /* unlink_name */
238 screen_buffer_open_file, /* open_file */
239 no_kernel_obj_list, /* get_kernel_obj_list */
240 no_close_handle, /* close_handle */
241 screen_buffer_destroy /* destroy */
244 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
246 static const struct fd_ops screen_buffer_fd_ops =
248 default_fd_get_poll_events, /* get_poll_events */
249 default_poll_event, /* poll_event */
250 console_get_fd_type, /* get_fd_type */
251 no_fd_read, /* read */
252 no_fd_write, /* write */
253 no_fd_flush, /* flush */
254 no_fd_get_file_info, /* get_file_info */
255 no_fd_get_volume_info, /* get_volume_info */
256 screen_buffer_ioctl, /* ioctl */
257 default_fd_queue_async, /* queue_async */
258 default_fd_reselect_async /* reselect_async */
261 static struct object_type *console_device_get_type( struct object *obj );
262 static void console_device_dump( struct object *obj, int verbose );
263 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
264 static struct object *console_device_open_file( struct object *obj, unsigned int access,
265 unsigned int sharing, unsigned int options );
267 static const struct object_ops console_device_ops =
269 sizeof(struct object), /* size */
270 console_device_dump, /* dump */
271 console_device_get_type, /* get_type */
272 no_add_queue, /* add_queue */
273 NULL, /* remove_queue */
274 NULL, /* signaled */
275 no_satisfied, /* satisfied */
276 no_signal, /* signal */
277 no_get_fd, /* get_fd */
278 default_fd_map_access, /* map_access */
279 default_get_sd, /* get_sd */
280 default_set_sd, /* set_sd */
281 console_device_lookup_name, /* lookup_name */
282 directory_link_name, /* link_name */
283 default_unlink_name, /* unlink_name */
284 console_device_open_file, /* open_file */
285 no_kernel_obj_list, /* get_kernel_obj_list */
286 no_close_handle, /* close_handle */
287 no_destroy /* destroy */
290 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
292 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
294 static int console_input_is_bare( struct console_input* cin )
296 return cin->evt == NULL;
299 static struct fd *console_input_get_fd( struct object* obj )
301 struct console_input *console_input = (struct console_input*)obj;
302 assert( obj->ops == &console_input_ops );
303 return (struct fd *)grab_object( console_input->fd );
306 static enum server_fd_type console_get_fd_type( struct fd *fd )
308 return FD_TYPE_CHAR;
311 /* dumps the renderer events of a console */
312 static void console_input_events_dump( struct object *obj, int verbose )
314 struct console_input_events *evts = (struct console_input_events *)obj;
315 assert( obj->ops == &console_input_events_ops );
316 fprintf( stderr, "Console input events: %d/%d events\n",
317 evts->num_used, evts->num_alloc );
320 /* destroys the renderer events of a console */
321 static void console_input_events_destroy( struct object *obj )
323 struct console_input_events *evts = (struct console_input_events *)obj;
324 assert( obj->ops == &console_input_events_ops );
325 if (evts->console) evts->console->evt = NULL;
326 free_async_queue( &evts->read_q );
327 if (evts->fd) release_object( evts->fd );
328 free( evts->events );
331 static struct fd *console_input_events_get_fd( struct object* obj )
333 struct console_input_events *evts = (struct console_input_events*)obj;
334 assert( obj->ops == &console_input_events_ops );
335 return (struct fd*)grab_object( evts->fd );
338 static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
339 unsigned int sharing, unsigned int options )
341 return grab_object( obj );
344 /* retrieves events from the console's renderer events list */
345 static int get_renderer_events( struct console_input_events* evts, struct async *async )
347 struct iosb *iosb = async_get_iosb( async );
348 data_size_t num;
350 num = min( iosb->out_size / sizeof(evts->events[0]), evts->num_used );
351 if (num && !(iosb->out_data = malloc( num * sizeof(evts->events[0] ))))
353 async_terminate( async, STATUS_NO_MEMORY );
354 release_object( iosb );
355 return 0;
358 iosb->status = STATUS_SUCCESS;
359 iosb->out_size = iosb->result = num * sizeof(evts->events[0]);
360 if (num) memcpy( iosb->out_data, evts->events, iosb->result );
361 release_object( iosb );
362 async_terminate( async, STATUS_ALERTED );
364 if (num && num < evts->num_used)
366 memmove( &evts->events[0], &evts->events[num],
367 (evts->num_used - num) * sizeof(evts->events[0]) );
369 evts->num_used -= num;
370 return 1;
373 /* add an event to the console's renderer events list */
374 static void console_input_events_append( struct console_input* console,
375 struct condrv_renderer_event* evt)
377 struct console_input_events* evts;
378 int collapsed = FALSE;
379 struct async *async;
381 if (!(evts = console->evt)) return;
382 /* to be done even when evt has been generated by the renderer ? */
384 /* try to collapse evt into current queue's events */
385 if (evts->num_used)
387 struct condrv_renderer_event* last = &evts->events[evts->num_used - 1];
389 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
390 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
392 /* if two update events overlap, collapse them into a single one */
393 if (last->u.update.bottom + 1 >= evt->u.update.top &&
394 evt->u.update.bottom + 1 >= last->u.update.top)
396 last->u.update.top = min(last->u.update.top, evt->u.update.top);
397 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
398 collapsed = TRUE;
402 if (!collapsed)
404 if (evts->num_used == evts->num_alloc)
406 evts->num_alloc += 16;
407 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
408 assert(evts->events);
410 evts->events[evts->num_used++] = *evt;
412 while (evts->num_used && (async = find_pending_async( &evts->read_q )))
414 get_renderer_events( evts, async );
415 release_object( async );
419 static struct object *create_console_input_events(void)
421 struct console_input_events* evt;
423 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
424 evt->console = NULL;
425 evt->num_alloc = evt->num_used = 0;
426 evt->events = NULL;
427 init_async_queue( &evt->read_q );
428 if (!(evt->fd = alloc_pseudo_fd( &console_input_events_fd_ops, &evt->obj, 0 )))
430 release_object( evt );
431 return NULL;
433 return &evt->obj;
436 static struct object *create_console_input( int fd )
438 struct console_input *console_input;
440 if (!(console_input = alloc_object( &console_input_ops )))
442 if (fd != -1) close( fd );
443 return NULL;
445 console_input->renderer = NULL;
446 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
447 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
448 ENABLE_EXTENDED_FLAGS;
449 console_input->num_proc = 0;
450 console_input->active = NULL;
451 console_input->recnum = 0;
452 console_input->records = NULL;
453 console_input->evt = NULL;
454 console_input->title = NULL;
455 console_input->title_len = 0;
456 console_input->history_size = 50;
457 console_input->history = calloc( console_input->history_size, sizeof(*console_input->history) );
458 console_input->history_index = 0;
459 console_input->history_mode = 0;
460 console_input->edition_mode = 0;
461 console_input->input_cp = 0;
462 console_input->output_cp = 0;
463 console_input->win = 0;
464 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
465 console_input->fd = NULL;
466 init_async_queue( &console_input->read_q );
468 if (!console_input->history || !console_input->event)
470 if (fd != -1) close( fd );
471 console_input->history_size = 0;
472 release_object( console_input );
473 return NULL;
475 if (fd != -1) /* bare console */
477 console_input->fd = create_anonymous_fd( &console_input_fd_ops, fd, &console_input->obj,
478 FILE_SYNCHRONOUS_IO_NONALERT );
480 else
482 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
483 FILE_SYNCHRONOUS_IO_NONALERT );
485 if (!console_input->fd)
487 release_object( console_input );
488 return NULL;
490 allow_fd_caching( console_input->fd );
491 return &console_input->obj;
494 static void generate_sb_initial_events( struct console_input *console_input )
496 struct screen_buffer *screen_buffer = console_input->active;
497 struct condrv_renderer_event evt;
499 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
500 evt.u.resize.width = screen_buffer->width;
501 evt.u.resize.height = screen_buffer->height;
502 console_input_events_append( console_input, &evt );
504 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
505 evt.u.display.left = screen_buffer->win.left;
506 evt.u.display.top = screen_buffer->win.top;
507 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
508 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
509 console_input_events_append( console_input, &evt );
511 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
512 evt.u.update.top = 0;
513 evt.u.update.bottom = screen_buffer->height - 1;
514 console_input_events_append( console_input, &evt );
516 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
517 evt.u.cursor_geom.size = screen_buffer->cursor_size;
518 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
519 console_input_events_append( console_input, &evt );
521 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
522 evt.u.cursor_pos.x = screen_buffer->cursor_x;
523 evt.u.cursor_pos.y = screen_buffer->cursor_y;
524 console_input_events_append( console_input, &evt );
527 static struct object *create_console_output( struct console_input *console_input, int fd )
529 struct screen_buffer *screen_buffer;
530 int i;
532 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
534 if (fd != -1) close( fd );
535 return NULL;
537 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
538 screen_buffer->input = console_input;
539 screen_buffer->cursor_size = 100;
540 screen_buffer->cursor_visible = 1;
541 screen_buffer->width = 80;
542 screen_buffer->height = 150;
543 screen_buffer->max_width = 80;
544 screen_buffer->max_height = 25;
545 screen_buffer->cursor_x = 0;
546 screen_buffer->cursor_y = 0;
547 screen_buffer->attr = 0x0F;
548 screen_buffer->popup_attr = 0xF5;
549 screen_buffer->win.left = 0;
550 screen_buffer->win.right = screen_buffer->max_width - 1;
551 screen_buffer->win.top = 0;
552 screen_buffer->win.bottom = screen_buffer->max_height - 1;
553 screen_buffer->data = NULL;
554 screen_buffer->font.width = 0;
555 screen_buffer->font.height = 0;
556 screen_buffer->font.weight = FW_NORMAL;
557 screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
558 screen_buffer->font.face_name = NULL;
559 screen_buffer->font.face_len = 0;
560 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
561 list_add_head( &screen_buffer_list, &screen_buffer->entry );
563 if (fd != -1)
564 screen_buffer->fd = create_anonymous_fd( &screen_buffer_fd_ops, fd, &screen_buffer->obj,
565 FILE_SYNCHRONOUS_IO_NONALERT );
566 else
567 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
568 FILE_SYNCHRONOUS_IO_NONALERT );
569 if (!screen_buffer->fd)
571 release_object( screen_buffer );
572 return NULL;
574 allow_fd_caching(screen_buffer->fd);
576 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
577 sizeof(*screen_buffer->data) )))
579 release_object( screen_buffer );
580 return NULL;
582 /* clear the first row */
583 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
584 /* and copy it to all other rows */
585 for (i = 1; i < screen_buffer->height; i++)
586 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
587 screen_buffer->width * sizeof(char_info_t) );
589 if (!console_input->active)
591 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
592 generate_sb_initial_events( console_input );
594 return &screen_buffer->obj;
597 /* free the console for this process */
598 int free_console( struct process *process )
600 struct console_input* console = process->console;
602 if (!console) return 0;
604 process->console = NULL;
605 if (--console->num_proc == 0 && console->renderer)
607 /* all processes have terminated... tell the renderer to terminate too */
608 struct condrv_renderer_event evt;
609 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
610 memset(&evt.u, 0, sizeof(evt.u));
611 console_input_events_append( console, &evt );
613 release_object( console );
615 return 1;
618 /* let process inherit the console from parent... this handle two cases :
619 * 1/ generic console inheritance
620 * 2/ parent is a renderer which launches process, and process should attach to the console
621 * rendered by parent
623 obj_handle_t inherit_console( struct thread *parent_thread, obj_handle_t handle, struct process *process,
624 obj_handle_t hconin )
626 struct console_input *console = NULL;
628 if (handle && !(console = (struct console_input *)get_handle_obj( current->process, handle, 0,
629 &console_input_ops )))
630 return 0;
632 /* if parent is a renderer, then attach current process to its console
633 * a bit hacky....
635 if (!console && hconin && parent_thread)
637 /* FIXME: should we check some access rights ? */
638 if (!(console = (struct console_input *)get_handle_obj( parent_thread->process, hconin,
639 0, &console_input_ops )))
640 clear_error(); /* ignore error */
642 if (!console) return 0;
644 process->console = console;
645 console->num_proc++;
646 return alloc_handle( process, process->console,
647 SYNCHRONIZE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, 0 );
650 struct thread *console_get_renderer( struct console_input *console )
652 return console->renderer;
655 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
657 struct console_input* console = NULL;
659 if (handle)
660 console = (struct console_input *)get_handle_obj( current->process, handle,
661 access, &console_input_ops );
662 else if (current->process->console)
664 console = (struct console_input *)grab_object( current->process->console );
667 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
668 return console;
671 struct console_signal_info
673 struct console_input *console;
674 process_id_t group;
675 int signal;
678 static int propagate_console_signal_cb(struct process *process, void *user)
680 struct console_signal_info* csi = (struct console_signal_info*)user;
682 if (process->console == csi->console && process->running_threads &&
683 (!csi->group || process->group_id == csi->group))
685 /* find a suitable thread to signal */
686 struct thread *thread;
687 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
689 if (send_thread_signal( thread, csi->signal )) break;
692 return FALSE;
695 static void propagate_console_signal( struct console_input *console,
696 int sig, process_id_t group_id )
698 struct console_signal_info csi;
700 if (!console)
702 set_error( STATUS_INVALID_PARAMETER );
703 return;
705 /* FIXME: should support the other events (like CTRL_BREAK) */
706 if (sig != CTRL_C_EVENT)
708 set_error( STATUS_NOT_IMPLEMENTED );
709 return;
711 csi.console = console;
712 csi.signal = SIGINT;
713 csi.group = group_id;
715 enum_processes(propagate_console_signal_cb, &csi);
718 /* retrieve a pointer to the console input records */
719 static int read_console_input( struct console_input *console, struct async *async, int flush )
721 struct iosb *iosb = async_get_iosb( async );
722 data_size_t count;
724 count = min( iosb->out_size / sizeof(INPUT_RECORD), console->recnum );
725 if (count)
727 if (!(iosb->out_data = malloc( count * sizeof(INPUT_RECORD) )))
729 set_error( STATUS_NO_MEMORY );
730 release_object( iosb );
731 return 0;
733 iosb->out_size = iosb->result = count * sizeof(INPUT_RECORD);
734 memcpy( iosb->out_data, console->records, iosb->result );
735 iosb->status = STATUS_SUCCESS;
736 async_terminate( async, STATUS_ALERTED );
738 else
740 async_terminate( async, STATUS_SUCCESS );
743 release_object( iosb );
745 if (flush && count)
747 if (console->recnum > count)
749 INPUT_RECORD *new_rec;
750 memmove( console->records, console->records + count, (console->recnum - count) * sizeof(*console->records) );
751 console->recnum -= count;
752 new_rec = realloc( console->records, console->recnum * sizeof(*console->records) );
753 if (new_rec) console->records = new_rec;
755 else
757 console->recnum = 0;
758 free( console->records );
759 console->records = NULL;
760 reset_event( console->event );
764 return 1;
767 /* add input events to a console input queue */
768 static int write_console_input( struct console_input* console, int count,
769 const INPUT_RECORD *records )
771 INPUT_RECORD *new_rec;
772 struct async *async;
774 if (!count) return 1;
775 if (!(new_rec = realloc( console->records,
776 (console->recnum + count) * sizeof(INPUT_RECORD) )))
778 set_error( STATUS_NO_MEMORY );
779 return 0;
781 console->records = new_rec;
782 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
784 if (console->mode & ENABLE_PROCESSED_INPUT)
786 int i = 0;
787 while (i < count)
789 if (records[i].EventType == KEY_EVENT &&
790 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
791 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
793 if (i != count - 1)
794 memcpy( &console->records[console->recnum + i],
795 &console->records[console->recnum + i + 1],
796 (count - i - 1) * sizeof(INPUT_RECORD) );
797 count--;
798 if (records[i].Event.KeyEvent.bKeyDown)
800 /* send SIGINT to all processes attached to this console */
801 propagate_console_signal( console, CTRL_C_EVENT, 0 );
804 else i++;
807 console->recnum += count;
808 while (console->recnum && (async = find_pending_async( &console->read_q )))
810 read_console_input( console, async, 1 );
811 release_object( async );
813 if (console->recnum) set_event( console->event );
814 return 1;
817 /* resize a screen buffer */
818 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
819 int new_width, int new_height )
821 int i, old_width, old_height, copy_width, copy_height;
822 char_info_t *new_data;
824 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
826 set_error( STATUS_NO_MEMORY );
827 return 0;
829 old_width = screen_buffer->width;
830 old_height = screen_buffer->height;
831 copy_width = min( old_width, new_width );
832 copy_height = min( old_height, new_height );
834 /* copy all the rows */
835 for (i = 0; i < copy_height; i++)
837 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
838 copy_width * sizeof(char_info_t) );
841 /* clear the end of each row */
842 if (new_width > old_width)
844 /* fill first row */
845 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
846 /* and blast it to the other rows */
847 for (i = 1; i < copy_height; i++)
848 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
849 (new_width - old_width) * sizeof(char_info_t) );
852 /* clear remaining rows */
853 if (new_height > old_height)
855 /* fill first row */
856 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
857 /* and blast it to the other rows */
858 for (i = old_height+1; i < new_height; i++)
859 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
860 new_width * sizeof(char_info_t) );
862 free( screen_buffer->data );
863 screen_buffer->data = new_data;
864 screen_buffer->width = new_width;
865 screen_buffer->height = new_height;
866 return 1;
869 static int set_output_info( struct screen_buffer *screen_buffer,
870 const struct condrv_output_info_params *params, data_size_t extra_size )
872 const struct condrv_output_info *info = &params->info;
873 struct condrv_renderer_event evt;
874 WCHAR *font_name;
876 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
878 if (info->cursor_size < 1 || info->cursor_size > 100)
880 set_error( STATUS_INVALID_PARAMETER );
881 return 0;
883 if (screen_buffer->cursor_size != info->cursor_size ||
884 screen_buffer->cursor_visible != info->cursor_visible)
886 screen_buffer->cursor_size = info->cursor_size;
887 screen_buffer->cursor_visible = info->cursor_visible;
888 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
889 memset( &evt.u, 0, sizeof(evt.u) );
890 evt.u.cursor_geom.size = info->cursor_size;
891 evt.u.cursor_geom.visible = info->cursor_visible;
892 console_input_events_append( screen_buffer->input, &evt );
895 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
897 if (info->cursor_x < 0 || info->cursor_x >= screen_buffer->width ||
898 info->cursor_y < 0 || info->cursor_y >= screen_buffer->height)
900 set_error( STATUS_INVALID_PARAMETER );
901 return 0;
903 if (screen_buffer->cursor_x != info->cursor_x || screen_buffer->cursor_y != info->cursor_y)
905 screen_buffer->cursor_x = info->cursor_x;
906 screen_buffer->cursor_y = info->cursor_y;
907 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
908 memset( &evt.u, 0, sizeof(evt.u) );
909 evt.u.cursor_pos.x = info->cursor_x;
910 evt.u.cursor_pos.y = info->cursor_y;
911 console_input_events_append( screen_buffer->input, &evt );
914 if (params->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
916 unsigned cc;
918 /* new screen-buffer cannot be smaller than actual window */
919 if (info->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
920 info->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
922 set_error( STATUS_INVALID_PARAMETER );
923 return 0;
925 /* FIXME: there are also some basic minimum and max size to deal with */
926 if (!change_screen_buffer_size( screen_buffer, info->width, info->height )) return 0;
928 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
929 memset( &evt.u, 0, sizeof(evt.u) );
930 evt.u.resize.width = info->width;
931 evt.u.resize.height = info->height;
932 console_input_events_append( screen_buffer->input, &evt );
934 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
935 memset( &evt.u, 0, sizeof(evt.u) );
936 evt.u.update.top = 0;
937 evt.u.update.bottom = screen_buffer->height - 1;
938 console_input_events_append( screen_buffer->input, &evt );
940 /* scroll window to display sb */
941 if (screen_buffer->win.right >= info->width)
943 screen_buffer->win.right -= screen_buffer->win.left;
944 screen_buffer->win.left = 0;
946 if (screen_buffer->win.bottom >= info->height)
948 screen_buffer->win.bottom -= screen_buffer->win.top;
949 screen_buffer->win.top = 0;
951 /* reset cursor if needed (normally, if cursor was outside of new sb, the
952 * window has been shifted so that the new position of the cursor will be
953 * visible */
954 cc = 0;
955 if (screen_buffer->cursor_x >= info->width)
957 screen_buffer->cursor_x = info->width - 1;
958 cc++;
960 if (screen_buffer->cursor_y >= info->height)
962 screen_buffer->cursor_y = info->height - 1;
963 cc++;
965 if (cc)
967 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
968 memset( &evt.u, 0, sizeof(evt.u) );
969 evt.u.cursor_pos.x = info->cursor_x;
970 evt.u.cursor_pos.y = info->cursor_y;
971 console_input_events_append( screen_buffer->input, &evt );
974 if (screen_buffer == screen_buffer->input->active &&
975 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
977 INPUT_RECORD ir;
978 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
979 ir.Event.WindowBufferSizeEvent.dwSize.X = info->width;
980 ir.Event.WindowBufferSizeEvent.dwSize.Y = info->height;
981 write_console_input( screen_buffer->input, 1, &ir );
984 if (params->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
986 screen_buffer->attr = info->attr;
988 if (params->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
990 screen_buffer->popup_attr = info->popup_attr;
992 if (params->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
994 if (info->win_left < 0 || info->win_left > info->win_right ||
995 info->win_right >= screen_buffer->width ||
996 info->win_top < 0 || info->win_top > info->win_bottom ||
997 info->win_bottom >= screen_buffer->height)
999 set_error( STATUS_INVALID_PARAMETER );
1000 return 0;
1002 if (screen_buffer->win.left != info->win_left || screen_buffer->win.top != info->win_top ||
1003 screen_buffer->win.right != info->win_right || screen_buffer->win.bottom != info->win_bottom)
1005 screen_buffer->win.left = info->win_left;
1006 screen_buffer->win.top = info->win_top;
1007 screen_buffer->win.right = info->win_right;
1008 screen_buffer->win.bottom = info->win_bottom;
1009 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1010 memset( &evt.u, 0, sizeof(evt.u) );
1011 evt.u.display.left = info->win_left;
1012 evt.u.display.top = info->win_top;
1013 evt.u.display.width = info->win_right - info->win_left + 1;
1014 evt.u.display.height = info->win_bottom - info->win_top + 1;
1015 console_input_events_append( screen_buffer->input, &evt );
1018 if (params->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1020 screen_buffer->max_width = info->max_width;
1021 screen_buffer->max_height = info->max_height;
1023 if (params->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1025 memcpy( screen_buffer->color_map, info->color_map, sizeof(info->color_map) );
1027 if (params->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1029 screen_buffer->font.width = info->font_width;
1030 screen_buffer->font.height = info->font_height;
1031 screen_buffer->font.weight = info->font_weight;
1032 screen_buffer->font.pitch_family = info->font_pitch_family;
1033 if (extra_size)
1035 extra_size = extra_size / sizeof(WCHAR) * sizeof(WCHAR);
1036 font_name = mem_alloc( extra_size );
1037 if (font_name)
1039 memcpy( font_name, info + 1, extra_size );
1040 free( screen_buffer->font.face_name );
1041 screen_buffer->font.face_name = font_name;
1042 screen_buffer->font.face_len = extra_size;
1047 return 1;
1050 /* appends a new line to history (history is a fixed size array) */
1051 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1053 struct history_line *ptr;
1055 if (!console || !console->history_size)
1057 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1058 return;
1061 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
1062 if (console->history_mode && console->history_index &&
1063 console->history[console->history_index - 1]->len == len &&
1064 !memcmp( console->history[console->history_index - 1]->text, buf, len ))
1066 /* don't duplicate entry */
1067 set_error( STATUS_ALIAS_EXISTS );
1068 return;
1070 if (!(ptr = mem_alloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
1071 ptr->len = len;
1072 memcpy( ptr->text, buf, len );
1074 if (console->history_index < console->history_size)
1076 console->history[console->history_index++] = ptr;
1078 else
1080 free( console->history[0]) ;
1081 memmove( &console->history[0], &console->history[1],
1082 (console->history_size - 1) * sizeof(*console->history) );
1083 console->history[console->history_size - 1] = ptr;
1087 /* returns a line from the cache */
1088 static data_size_t console_input_get_hist( struct console_input *console, int index )
1090 data_size_t ret = 0;
1092 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1093 else
1095 ret = console->history[index]->len;
1096 set_reply_data( console->history[index]->text, min( ret, get_reply_max_size() ));
1098 return ret;
1101 /* dumb dump */
1102 static void console_input_dump( struct object *obj, int verbose )
1104 struct console_input *console = (struct console_input *)obj;
1105 assert( obj->ops == &console_input_ops );
1106 fprintf( stderr, "Console input active=%p evt=%p\n",
1107 console->active, console->evt );
1110 static void console_input_destroy( struct object *obj )
1112 struct console_input* console_in = (struct console_input *)obj;
1113 struct screen_buffer* curr;
1114 int i;
1116 assert( obj->ops == &console_input_ops );
1117 free( console_in->title );
1118 free( console_in->records );
1120 if (console_in->active) release_object( console_in->active );
1121 console_in->active = NULL;
1123 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1125 if (curr->input == console_in) curr->input = NULL;
1128 free_async_queue( &console_in->read_q );
1129 if (console_in->evt)
1130 console_in->evt->console = NULL;
1131 if (console_in->event)
1132 release_object( console_in->event );
1133 if (console_in->fd)
1134 release_object( console_in->fd );
1136 for (i = 0; i < console_in->history_size; i++)
1137 free( console_in->history[i] );
1138 free( console_in->history );
1141 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1142 unsigned int sharing, unsigned int options )
1144 return grab_object( obj );
1147 static void screen_buffer_dump( struct object *obj, int verbose )
1149 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1150 assert( obj->ops == &screen_buffer_ops );
1152 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1155 static void screen_buffer_destroy( struct object *obj )
1157 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1159 assert( obj->ops == &screen_buffer_ops );
1161 list_remove( &screen_buffer->entry );
1163 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1165 struct screen_buffer *sb;
1167 screen_buffer->input->active = NULL;
1168 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1170 if (sb->input == screen_buffer->input)
1172 sb->input->active = sb;
1173 break;
1177 if (screen_buffer->fd) release_object( screen_buffer->fd );
1178 free( screen_buffer->data );
1179 free( screen_buffer->font.face_name );
1182 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
1183 unsigned int sharing, unsigned int options )
1185 return grab_object( obj );
1188 static struct fd *screen_buffer_get_fd( struct object *obj )
1190 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1191 assert( obj->ops == &screen_buffer_ops );
1192 if (screen_buffer->fd)
1193 return (struct fd*)grab_object( screen_buffer->fd );
1194 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1195 return NULL;
1198 /* read data from a screen buffer */
1199 static void read_console_output( struct screen_buffer *screen_buffer, unsigned int x, unsigned int y,
1200 enum char_info_mode mode, unsigned int width )
1202 unsigned int i, count;
1203 char_info_t *src;
1205 if (x >= screen_buffer->width || y >= screen_buffer->height)
1207 if (width) set_error( STATUS_INVALID_PARAMETER );
1208 return;
1210 src = screen_buffer->data + y * screen_buffer->width + x;
1212 switch(mode)
1214 case CHAR_INFO_MODE_TEXT:
1216 WCHAR *data;
1217 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1218 get_reply_max_size() / sizeof(*data) );
1219 if ((data = set_reply_data_size( count * sizeof(*data) )))
1221 for (i = 0; i < count; i++) data[i] = src[i].ch;
1224 break;
1225 case CHAR_INFO_MODE_ATTR:
1227 unsigned short *data;
1228 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
1229 get_reply_max_size() / sizeof(*data) );
1230 if ((data = set_reply_data_size( count * sizeof(*data) )))
1232 for (i = 0; i < count; i++) data[i] = src[i].attr;
1235 break;
1236 case CHAR_INFO_MODE_TEXTATTR:
1238 char_info_t *data;
1239 SMALL_RECT *region;
1240 if (!width || get_reply_max_size() < sizeof(*region))
1242 set_error( STATUS_INVALID_PARAMETER );
1243 return;
1245 count = min( (get_reply_max_size() - sizeof(*region)) / (width * sizeof(*data)), screen_buffer->height - y );
1246 width = min( width, screen_buffer->width - x );
1247 if (!(region = set_reply_data_size( sizeof(*region) + width * count * sizeof(*data) ))) return;
1248 region->Left = x;
1249 region->Top = y;
1250 region->Right = x + width - 1;
1251 region->Bottom = y + count - 1;
1252 data = (char_info_t *)(region + 1);
1253 for (i = 0; i < count; i++)
1255 memcpy( &data[i * width], &src[i * screen_buffer->width], width * sizeof(*data) );
1258 break;
1259 default:
1260 set_error( STATUS_INVALID_PARAMETER );
1261 break;
1265 /* write data into a screen buffer */
1266 static void write_console_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
1267 data_size_t size )
1269 unsigned int i, entry_size, entry_cnt, x, y;
1270 char_info_t *dest;
1271 char *src;
1273 entry_size = params->mode == CHAR_INFO_MODE_TEXTATTR ? sizeof(char_info_t) : sizeof(WCHAR);
1274 if (size % entry_size)
1276 set_error( STATUS_INVALID_PARAMETER );
1277 return;
1279 if (params->x >= screen_buffer->width) return;
1280 entry_cnt = size / entry_size;
1282 for (i = 0, src = (char *)(params + 1); i < entry_cnt; i++, src += entry_size)
1284 if (params->width)
1286 x = params->x + i % params->width;
1287 y = params->y + i / params->width;
1288 if (x >= screen_buffer->width) continue;
1290 else
1292 x = (params->x + i) % screen_buffer->width;
1293 y = params->y + (params->x + i) / screen_buffer->width;
1295 if (y >= screen_buffer->height) break;
1297 dest = &screen_buffer->data[y * screen_buffer->width + x];
1298 switch(params->mode)
1300 case CHAR_INFO_MODE_TEXT:
1301 dest->ch = *(const WCHAR *)src;
1302 break;
1303 case CHAR_INFO_MODE_ATTR:
1304 dest->attr = *(const unsigned short *)src;
1305 break;
1306 case CHAR_INFO_MODE_TEXTATTR:
1307 *dest = *(const char_info_t *)src;
1308 break;
1309 case CHAR_INFO_MODE_TEXTSTDATTR:
1310 dest->ch = *(const WCHAR *)src;
1311 dest->attr = screen_buffer->attr;
1312 break;
1313 default:
1314 set_error( STATUS_INVALID_PARAMETER );
1315 return;
1319 if (i && screen_buffer == screen_buffer->input->active)
1321 struct condrv_renderer_event evt;
1322 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1323 memset(&evt.u, 0, sizeof(evt.u));
1324 evt.u.update.top = params->y;
1325 evt.u.update.bottom = params->width
1326 ? min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1
1327 : params->y + (params->x + i - 1) / screen_buffer->width;
1328 console_input_events_append( screen_buffer->input, &evt );
1331 if (get_reply_max_size() == sizeof(SMALL_RECT))
1333 SMALL_RECT region;
1334 region.Left = params->x;
1335 region.Top = params->y;
1336 region.Right = min( params->x + params->width, screen_buffer->width ) - 1;
1337 region.Bottom = min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1;
1338 set_reply_data( &region, sizeof(region) );
1340 else set_reply_data( &i, sizeof(i) );
1343 /* fill a screen buffer with uniform data */
1344 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1345 enum char_info_mode mode, int x, int y, int count, int wrap )
1347 int i;
1348 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1350 if (y >= screen_buffer->height) return 0;
1352 if (wrap)
1353 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1354 else
1355 end = screen_buffer->data + (y+1) * screen_buffer->width;
1357 if (count > end - dest) count = end - dest;
1359 switch(mode)
1361 case CHAR_INFO_MODE_TEXT:
1362 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1363 break;
1364 case CHAR_INFO_MODE_ATTR:
1365 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1366 break;
1367 case CHAR_INFO_MODE_TEXTATTR:
1368 for (i = 0; i < count; i++) dest[i] = data;
1369 break;
1370 case CHAR_INFO_MODE_TEXTSTDATTR:
1371 for (i = 0; i < count; i++)
1373 dest[i].ch = data.ch;
1374 dest[i].attr = screen_buffer->attr;
1376 break;
1377 default:
1378 set_error( STATUS_INVALID_PARAMETER );
1379 return 0;
1382 if (count && screen_buffer == screen_buffer->input->active)
1384 struct condrv_renderer_event evt;
1385 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1386 memset(&evt.u, 0, sizeof(evt.u));
1387 evt.u.update.top = y;
1388 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1389 console_input_events_append( screen_buffer->input, &evt );
1391 return i;
1394 /* scroll parts of a screen buffer */
1395 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1396 int w, int h, const rectangle_t *clip, char_info_t fill )
1398 struct condrv_renderer_event evt;
1399 rectangle_t src, dst;
1400 int x, y;
1402 src.left = max( xsrc, clip->left );
1403 src.top = max( ysrc, clip->top );
1404 src.right = min( xsrc + w - 1, clip->right );
1405 src.bottom = min( ysrc + h - 1, clip->bottom );
1407 dst.left = xdst;
1408 dst.top = ydst;
1409 dst.right = xdst + w - 1;
1410 dst.bottom = ydst + h - 1;
1412 if (dst.left < clip->left)
1414 xsrc += clip->left - dst.left;
1415 w -= clip->left - dst.left;
1416 dst.left = clip->left;
1418 if (dst.top < clip->top)
1420 ysrc += clip->top - dst.top;
1421 h -= clip->top - dst.top;
1422 dst.top = clip->top;
1424 if (dst.right > clip->right) w -= dst.right - clip->right;
1425 if (dst.bottom > clip->bottom) h -= dst.bottom - clip->bottom;
1427 if (w > 0 && h > 0)
1429 if (ysrc < ydst)
1431 for (y = h; y > 0; y--)
1433 memcpy( &screen_buffer->data[(dst.top + y - 1) * screen_buffer->width + dst.left],
1434 &screen_buffer->data[(ysrc + y - 1) * screen_buffer->width + xsrc],
1435 w * sizeof(screen_buffer->data[0]) );
1438 else
1440 for (y = 0; y < h; y++)
1442 /* we use memmove here because when psrc and pdst are the same,
1443 * copies are done on the same row, so the dst and src blocks
1444 * can overlap */
1445 memmove( &screen_buffer->data[(dst.top + y) * screen_buffer->width + dst.left],
1446 &screen_buffer->data[(ysrc + y) * screen_buffer->width + xsrc],
1447 w * sizeof(screen_buffer->data[0]) );
1452 for (y = src.top; y <= src.bottom; y++)
1454 int left = src.left;
1455 int right = src.right;
1456 if (dst.top <= y && y <= dst.bottom)
1458 if (dst.left <= src.left) left = max( left, dst.right + 1 );
1459 if (dst.left >= src.left) right = min( right, dst.left - 1 );
1461 for (x = left; x <= right; x++) screen_buffer->data[y * screen_buffer->width + x] = fill;
1464 /* FIXME: this could be enhanced, by signalling scroll */
1465 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1466 memset(&evt.u, 0, sizeof(evt.u));
1467 evt.u.update.top = min( src.top, dst.top );
1468 evt.u.update.bottom = max( src.bottom, dst.bottom );
1469 console_input_events_append( screen_buffer->input, &evt );
1472 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1474 struct console_input *console = get_fd_user( fd );
1476 switch (code)
1478 case IOCTL_CONDRV_GET_MODE:
1479 if (get_reply_max_size() != sizeof(console->mode))
1481 set_error( STATUS_INVALID_PARAMETER );
1482 return 0;
1484 return set_reply_data( &console->mode, sizeof(console->mode) ) != NULL;
1486 case IOCTL_CONDRV_SET_MODE:
1487 if (get_req_data_size() != sizeof(console->mode))
1489 set_error( STATUS_INVALID_PARAMETER );
1490 return 0;
1492 console->mode = *(unsigned int *)get_req_data();
1493 return 1;
1495 case IOCTL_CONDRV_READ_INPUT:
1497 int blocking = 0;
1498 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1500 set_error( STATUS_INVALID_PARAMETER );
1501 return 0;
1503 if (get_req_data_size())
1505 if (get_req_data_size() != sizeof(int))
1507 set_error( STATUS_INVALID_PARAMETER );
1508 return 0;
1510 blocking = *(int *)get_req_data();
1512 set_error( STATUS_PENDING );
1513 if (blocking && !console->recnum)
1515 queue_async( &console->read_q, async );
1516 return 1;
1518 return read_console_input( console, async, 1 );
1521 case IOCTL_CONDRV_WRITE_INPUT:
1522 return write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD), get_req_data() );
1524 case IOCTL_CONDRV_PEEK:
1525 if (get_reply_max_size() % sizeof(INPUT_RECORD))
1527 set_error( STATUS_INVALID_PARAMETER );
1528 return 0;
1530 set_error( STATUS_PENDING );
1531 return read_console_input( console, async, 0 );
1533 case IOCTL_CONDRV_GET_INPUT_INFO:
1535 struct condrv_input_info info;
1536 if (get_reply_max_size() != sizeof(info))
1538 set_error( STATUS_INVALID_PARAMETER );
1539 return 0;
1541 info.input_cp = console->input_cp;
1542 info.output_cp = console->output_cp;
1543 info.history_mode = console->history_mode;
1544 info.history_size = console->history_size;
1545 info.history_index = console->history_index;
1546 info.edition_mode = console->edition_mode;
1547 info.input_count = console->recnum;
1548 info.win = console->win;
1549 return set_reply_data( &info, sizeof(info) ) != NULL;
1552 case IOCTL_CONDRV_SET_INPUT_INFO:
1554 const struct condrv_input_info_params *params = get_req_data();
1555 if (get_req_data_size() != sizeof(*params))
1557 set_error( STATUS_INVALID_PARAMETER );
1558 return 0;
1560 if (params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
1562 console->history_mode = params->info.history_mode;
1564 if ((params->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
1565 console->history_size != params->info.history_size)
1567 struct history_line **mem = NULL;
1568 int i, delta;
1570 if (params->info.history_size)
1572 if (!(mem = mem_alloc( params->info.history_size * sizeof(*mem) ))) return 0;
1573 memset( mem, 0, params->info.history_size * sizeof(*mem) );
1576 delta = (console->history_index > params->info.history_size) ?
1577 (console->history_index - params->info.history_size) : 0;
1579 for (i = delta; i < console->history_index; i++)
1581 mem[i - delta] = console->history[i];
1582 console->history[i] = NULL;
1584 console->history_index -= delta;
1586 for (i = 0; i < console->history_size; i++)
1587 free( console->history[i] );
1588 free( console->history );
1589 console->history = mem;
1590 console->history_size = params->info.history_size;
1592 if (params->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
1594 console->edition_mode = params->info.edition_mode;
1596 if (params->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
1598 console->input_cp = params->info.input_cp;
1600 if (params->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
1602 console->output_cp = params->info.output_cp;
1604 if (params->mask & SET_CONSOLE_INPUT_INFO_WIN)
1606 console->win = params->info.win;
1608 return 1;
1611 case IOCTL_CONDRV_GET_TITLE:
1612 if (!console->title_len) return 1;
1613 return set_reply_data( console->title, min( console->title_len, get_reply_max_size() )) != NULL;
1615 case IOCTL_CONDRV_SET_TITLE:
1617 data_size_t len = get_req_data_size();
1618 struct condrv_renderer_event evt;
1619 WCHAR *title = NULL;
1621 if (len % sizeof(WCHAR))
1623 set_error( STATUS_INVALID_PARAMETER );
1624 return 0;
1627 if (len && !(title = memdup( get_req_data(), len ))) return 0;
1628 free( console->title );
1629 console->title = title;
1630 console->title_len = len;
1631 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
1632 console_input_events_append( console, &evt );
1633 return 1;
1636 default:
1637 set_error( STATUS_INVALID_HANDLE );
1638 return 0;
1642 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1644 struct screen_buffer *screen_buffer = get_fd_user( fd );
1646 switch (code)
1648 case IOCTL_CONDRV_GET_MODE:
1649 if (get_reply_max_size() != sizeof(screen_buffer->mode))
1651 set_error( STATUS_INVALID_PARAMETER );
1652 return 0;
1654 return set_reply_data( &screen_buffer->mode, sizeof(screen_buffer->mode) ) != NULL;
1656 case IOCTL_CONDRV_SET_MODE:
1657 if (get_req_data_size() != sizeof(screen_buffer->mode))
1659 set_error( STATUS_INVALID_PARAMETER );
1660 return 0;
1662 screen_buffer->mode = *(unsigned int *)get_req_data();
1663 return 1;
1665 case IOCTL_CONDRV_READ_OUTPUT:
1667 const struct condrv_output_params *params = get_req_data();
1668 if (get_req_data_size() != sizeof(*params))
1670 set_error( STATUS_INVALID_PARAMETER );
1671 return 0;
1673 if (console_input_is_bare( screen_buffer->input ))
1675 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1676 return 0;
1678 read_console_output( screen_buffer, params->x, params->y, params->mode, params->width );
1679 return !get_error();
1682 case IOCTL_CONDRV_WRITE_OUTPUT:
1683 if (get_req_data_size() < sizeof(struct condrv_output_params) ||
1684 (get_reply_max_size() != sizeof(SMALL_RECT) && get_reply_max_size() != sizeof(unsigned int)))
1686 set_error( STATUS_INVALID_PARAMETER );
1687 return 0;
1689 if (console_input_is_bare( screen_buffer->input ))
1691 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1692 return 0;
1694 write_console_output( screen_buffer, get_req_data(), get_req_data_size() - sizeof(struct condrv_output_params) );
1695 return !get_error();
1697 case IOCTL_CONDRV_GET_OUTPUT_INFO:
1699 struct condrv_output_info *info;
1700 data_size_t size;
1702 size = min( sizeof(*info) + screen_buffer->font.face_len, get_reply_max_size() );
1703 if (size < sizeof(*info))
1705 set_error( STATUS_INVALID_PARAMETER );
1706 return 0;
1708 if (!(info = set_reply_data_size( size ))) return 0;
1710 info->cursor_size = screen_buffer->cursor_size;
1711 info->cursor_visible = screen_buffer->cursor_visible;
1712 info->cursor_x = screen_buffer->cursor_x;
1713 info->cursor_y = screen_buffer->cursor_y;
1714 info->width = screen_buffer->width;
1715 info->height = screen_buffer->height;
1716 info->attr = screen_buffer->attr;
1717 info->popup_attr = screen_buffer->popup_attr;
1718 info->win_left = screen_buffer->win.left;
1719 info->win_top = screen_buffer->win.top;
1720 info->win_right = screen_buffer->win.right;
1721 info->win_bottom = screen_buffer->win.bottom;
1722 info->max_width = screen_buffer->max_width;
1723 info->max_height = screen_buffer->max_height;
1724 info->font_width = screen_buffer->font.width;
1725 info->font_height = screen_buffer->font.height;
1726 info->font_weight = screen_buffer->font.weight;
1727 info->font_pitch_family = screen_buffer->font.pitch_family;
1728 memcpy( info->color_map, screen_buffer->color_map, sizeof(info->color_map) );
1729 size -= sizeof(*info);
1730 if (size) memcpy( info + 1, screen_buffer->font.face_name, size );
1731 return 1;
1734 case IOCTL_CONDRV_SET_OUTPUT_INFO:
1736 const struct condrv_output_info_params *params = get_req_data();
1737 if (get_req_data_size() < sizeof(*params))
1739 set_error( STATUS_INVALID_PARAMETER );
1740 return 0;
1742 if (!screen_buffer->input)
1744 set_error( STATUS_INVALID_HANDLE );
1745 return 0;
1747 return set_output_info( screen_buffer, params, get_req_data_size() - sizeof(*params) );
1750 case IOCTL_CONDRV_ACTIVATE:
1751 if (!screen_buffer->input)
1753 set_error( STATUS_INVALID_HANDLE );
1754 return 0;
1757 if (screen_buffer != screen_buffer->input->active)
1759 if (screen_buffer->input->active) release_object( screen_buffer->input->active );
1760 screen_buffer->input->active = (struct screen_buffer *)grab_object( screen_buffer );
1761 generate_sb_initial_events( screen_buffer->input );
1763 return 1;
1765 case IOCTL_CONDRV_FILL_OUTPUT:
1767 const struct condrv_fill_output_params *params = get_req_data();
1768 char_info_t data;
1769 DWORD written;
1770 if (get_req_data_size() != sizeof(*params) ||
1771 (get_reply_max_size() && get_reply_max_size() != sizeof(written)))
1773 set_error( STATUS_INVALID_PARAMETER );
1774 return 0;
1776 data.ch = params->ch;
1777 data.attr = params->attr;
1778 written = fill_console_output( screen_buffer, data, params->mode,
1779 params->x, params->y, params->count, params->wrap );
1780 if (written && get_reply_max_size() == sizeof(written))
1781 set_reply_data( &written, sizeof(written) );
1782 return !get_error();
1785 case IOCTL_CONDRV_SCROLL:
1787 const struct condrv_scroll_params *params = get_req_data();
1788 rectangle_t clip;
1789 if (get_req_data_size() != sizeof(*params))
1791 set_error( STATUS_INVALID_PARAMETER );
1792 return 0;
1794 if (console_input_is_bare( screen_buffer->input ) || !screen_buffer->input)
1796 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1797 return 0;
1799 clip.left = max( params->clip.Left, 0 );
1800 clip.top = max( params->clip.Top, 0 );
1801 clip.right = min( params->clip.Right, screen_buffer->width - 1 );
1802 clip.bottom = min( params->clip.Bottom, screen_buffer->height - 1 );
1803 if (clip.left > clip.right || clip.top > clip.bottom || params->scroll.Left < 0 || params->scroll.Top < 0 ||
1804 params->scroll.Right >= screen_buffer->width || params->scroll.Bottom >= screen_buffer->height ||
1805 params->scroll.Right < params->scroll.Left || params->scroll.Top > params->scroll.Bottom ||
1806 params->origin.X < 0 || params->origin.X >= screen_buffer->width || params->origin.Y < 0 ||
1807 params->origin.Y >= screen_buffer->height)
1809 set_error( STATUS_INVALID_PARAMETER );
1810 return 0;
1813 scroll_console_output( screen_buffer, params->scroll.Left, params->scroll.Top, params->origin.X, params->origin.Y,
1814 params->scroll.Right - params->scroll.Left + 1, params->scroll.Bottom - params->scroll.Top + 1,
1815 &clip, params->fill );
1816 return !get_error();
1819 default:
1820 set_error( STATUS_INVALID_HANDLE );
1821 return 0;
1825 static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1827 struct console_input_events *evts = get_fd_user( fd );
1829 switch (code)
1831 case IOCTL_CONDRV_GET_RENDERER_EVENTS:
1832 set_error( STATUS_PENDING );
1833 if (evts->num_used) return get_renderer_events( evts, async );
1834 queue_async( &evts->read_q, async );
1835 return 1;
1837 case IOCTL_CONDRV_ATTACH_RENDERER:
1839 struct console_input *console_input;
1840 if (get_req_data_size() != sizeof(condrv_handle_t))
1842 set_error( STATUS_INVALID_PARAMETER );
1843 return 0;
1845 console_input = (struct console_input *)get_handle_obj( current->process, *(condrv_handle_t *)get_req_data(),
1846 0, &console_input_ops );
1847 if (!console_input) return 0;
1849 if (!console_input->evt && !evts->console)
1851 console_input->evt = evts;
1852 console_input->renderer = current;
1853 evts->console = console_input;
1855 else set_error( STATUS_INVALID_HANDLE );
1857 release_object( console_input );
1858 return !get_error();
1861 case IOCTL_CONDRV_SCROLL:
1862 case IOCTL_CONDRV_SET_MODE:
1863 case IOCTL_CONDRV_WRITE_OUTPUT:
1864 case IOCTL_CONDRV_READ_OUTPUT:
1865 case IOCTL_CONDRV_FILL_OUTPUT:
1866 case IOCTL_CONDRV_GET_OUTPUT_INFO:
1867 case IOCTL_CONDRV_SET_OUTPUT_INFO:
1868 if (!evts->console || !evts->console->active)
1870 set_error( STATUS_INVALID_HANDLE );
1871 return 0;
1873 return screen_buffer_ioctl( evts->console->active->fd, code, async );
1875 default:
1876 if (!evts->console)
1878 set_error( STATUS_INVALID_HANDLE );
1879 return 0;
1881 return console_input_ioctl( evts->console->fd, code, async );
1885 static struct object_type *console_device_get_type( struct object *obj )
1887 static const WCHAR name[] = {'D','e','v','i','c','e'};
1888 static const struct unicode_str str = { name, sizeof(name) };
1889 return get_object_type( &str );
1892 static void console_device_dump( struct object *obj, int verbose )
1894 fputs( "Console device\n", stderr );
1897 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1899 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1900 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1901 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1902 static const WCHAR rendererW[] = {'R','e','n','d','e','r','e','r'};
1903 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1905 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1907 if (!current->process->console)
1909 set_error( STATUS_INVALID_HANDLE );
1910 return NULL;
1912 name->len = 0;
1913 return grab_object( current->process->console );
1916 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1918 if (!current->process->console || !current->process->console->active)
1920 set_error( STATUS_INVALID_HANDLE );
1921 return NULL;
1923 name->len = 0;
1924 return grab_object( current->process->console->active );
1927 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1929 name->len = 0;
1930 return grab_object( obj );
1933 if (name->len == sizeof(rendererW) && !memcmp( name->str, rendererW, name->len ))
1935 name->len = 0;
1936 return create_console_input_events();
1939 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1941 if (!current->process->console)
1943 set_error( STATUS_INVALID_HANDLE );
1944 return NULL;
1946 name->len = 0;
1947 return create_console_output( current->process->console, -1 );
1950 return NULL;
1953 static struct object *console_device_open_file( struct object *obj, unsigned int access,
1954 unsigned int sharing, unsigned int options )
1956 int is_output;
1957 access = default_fd_map_access( obj, access );
1958 is_output = access & FILE_WRITE_DATA;
1959 if (!current->process->console || (is_output && !current->process->console))
1961 set_error( STATUS_INVALID_HANDLE );
1962 return NULL;
1964 if (is_output && (access & FILE_READ_DATA))
1966 set_error( STATUS_INVALID_PARAMETER );
1967 return NULL;
1969 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
1972 struct object *create_console_device( struct object *root, const struct unicode_str *name )
1974 return create_named_object( root, &console_device_ops, name, 0, NULL );
1977 /* allocate a console for the renderer */
1978 DECL_HANDLER(alloc_console)
1980 struct process *process;
1981 struct console_input *console;
1982 int fd;
1983 int attach = 0;
1985 if (req->input_fd != -1)
1987 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1989 set_error( STATUS_INVALID_PARAMETER );
1990 return;
1993 else fd = -1;
1995 switch (req->pid)
1997 case 0:
1998 /* console to be attached to parent process */
1999 if (!(process = get_process_from_id( current->process->parent_id )))
2001 if (fd != -1) close( fd );
2002 set_error( STATUS_ACCESS_DENIED );
2003 return;
2005 attach = 1;
2006 break;
2007 case 0xffffffff:
2008 /* console to be attached to current process */
2009 process = current->process;
2010 grab_object( process );
2011 attach = 1;
2012 break;
2013 default:
2014 /* console to be attached to req->pid */
2015 if (!(process = get_process_from_id( req->pid )))
2017 if (fd != -1) close( fd );
2018 return;
2022 if (attach && process->console)
2024 if (fd != -1) close( fd );
2025 set_error( STATUS_ACCESS_DENIED );
2027 else if ((console = (struct console_input*)create_console_input( fd )))
2029 if ((reply->handle_in = alloc_handle( current->process, console, req->access,
2030 req->attributes )) && attach)
2032 process->console = (struct console_input*)grab_object( console );
2033 console->num_proc++;
2035 release_object( console );
2037 release_object( process );
2040 /* free the console of the current process */
2041 DECL_HANDLER(free_console)
2043 free_console( current->process );
2046 /* attach to a other process's console */
2047 DECL_HANDLER(attach_console)
2049 struct process *process;
2051 if (current->process->console)
2053 set_error( STATUS_ACCESS_DENIED );
2054 return;
2057 process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
2058 ? current->process->parent_id : req->pid );
2059 if (!process) return;
2061 if (process->console && process->console->active)
2063 current->process->console = (struct console_input *)grab_object( process->console );
2064 current->process->console->num_proc++;
2066 else
2068 set_error( STATUS_INVALID_HANDLE );
2071 release_object( process );
2072 return;
2075 /* appends a string to console's history */
2076 DECL_HANDLER(append_console_input_history)
2078 struct console_input *console;
2080 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
2081 console_input_append_hist( console, get_req_data(), get_req_data_size() );
2082 release_object( console );
2085 /* appends a string to console's history */
2086 DECL_HANDLER(get_console_input_history)
2088 struct console_input *console;
2090 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
2091 reply->total = console_input_get_hist( console, req->index );
2092 release_object( console );
2095 /* creates a screen buffer */
2096 DECL_HANDLER(create_console_output)
2098 struct console_input *console;
2099 struct object *screen_buffer;
2100 int fd;
2102 if (req->fd != -1)
2104 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
2106 set_error( STATUS_INVALID_HANDLE );
2107 return;
2110 else fd = -1;
2111 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
2113 if (fd != -1) close( fd );
2114 return;
2116 if (console_input_is_bare( console ) ^ (fd != -1))
2118 if (fd != -1) close( fd );
2119 release_object( console );
2120 set_error( STATUS_INVALID_HANDLE );
2121 return;
2124 screen_buffer = create_console_output( console, fd );
2125 if (screen_buffer)
2127 /* FIXME: should store sharing and test it when opening the CONOUT$ device
2128 * see file.c on how this could be done */
2129 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
2130 release_object( screen_buffer );
2132 release_object( console );
2135 /* sends a signal to a console (process, group...) */
2136 DECL_HANDLER(send_console_signal)
2138 process_id_t group;
2140 group = req->group_id ? req->group_id : current->process->group_id;
2142 if (!group)
2143 set_error( STATUS_INVALID_PARAMETER );
2144 else
2145 propagate_console_signal( current->process->console, req->signal, group );
2148 /* get console which renderer is 'current' */
2149 static int cgwe_enum( struct process* process, void* user)
2151 if (process->console && process->console->renderer == current)
2153 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
2154 return 1;
2156 return 0;
2159 DECL_HANDLER(get_console_wait_event)
2161 struct console_input* console = NULL;
2162 struct object *obj;
2164 if (req->handle)
2166 if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
2167 if (obj->ops == &console_input_ops)
2168 console = (struct console_input *)grab_object( obj );
2169 else if (obj->ops == &screen_buffer_ops)
2170 console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
2171 else
2172 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2173 release_object( obj );
2175 else if (current->process->console)
2176 console = (struct console_input *)grab_object( current->process->console );
2177 else enum_processes(cgwe_enum, &console);
2179 if (console)
2181 reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
2182 release_object( console );
2184 else set_error( STATUS_INVALID_PARAMETER );