ntdll/unix: Use Pc instead of Rip in signal_arm64.
[wine.git] / server / console.c
blob729cfa40612d316479915886c37bb6b475fcef36
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"
42 struct screen_buffer;
43 struct console_input_events;
45 struct history_line
47 data_size_t len;
48 WCHAR text[1];
51 struct console_input
53 struct object obj; /* object header */
54 int num_proc; /* number of processes attached to this console */
55 struct thread *renderer; /* console renderer thread */
56 int mode; /* input mode */
57 struct screen_buffer *active; /* active screen buffer */
58 int recnum; /* number of input records */
59 INPUT_RECORD *records; /* input records */
60 struct console_input_events *evt; /* synchronization event with renderer */
61 WCHAR *title; /* console title */
62 data_size_t title_len; /* length of console title */
63 struct history_line **history; /* lines history */
64 int history_size; /* number of entries in history array */
65 int history_index; /* number of used entries in history array */
66 int history_mode; /* mode of history (non zero means remove doubled strings */
67 int edition_mode; /* index to edition mode flavors */
68 int input_cp; /* console input codepage */
69 int output_cp; /* console output codepage */
70 user_handle_t win; /* window handle if backend supports it */
71 struct event *event; /* event to wait on for input queue */
72 struct fd *fd; /* for bare console, attached input fd */
75 static void console_input_dump( struct object *obj, int verbose );
76 static void console_input_destroy( struct object *obj );
77 static struct fd *console_input_get_fd( struct object *obj );
78 static struct object *console_input_open_file( struct object *obj, unsigned int access,
79 unsigned int sharing, unsigned int options );
81 static const struct object_ops console_input_ops =
83 sizeof(struct console_input), /* size */
84 console_input_dump, /* dump */
85 no_get_type, /* get_type */
86 no_add_queue, /* add_queue */
87 NULL, /* remove_queue */
88 NULL, /* signaled */
89 no_satisfied, /* satisfied */
90 no_signal, /* signal */
91 console_input_get_fd, /* get_fd */
92 default_fd_map_access, /* map_access */
93 default_get_sd, /* get_sd */
94 default_set_sd, /* set_sd */
95 no_lookup_name, /* lookup_name */
96 no_link_name, /* link_name */
97 NULL, /* unlink_name */
98 console_input_open_file, /* open_file */
99 no_kernel_obj_list, /* get_kernel_obj_list */
100 no_close_handle, /* close_handle */
101 console_input_destroy /* destroy */
104 static void console_input_events_dump( struct object *obj, int verbose );
105 static void console_input_events_destroy( struct object *obj );
106 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
108 struct console_input_events
110 struct object obj; /* object header */
111 int num_alloc; /* number of allocated events */
112 int num_used; /* number of actually used events */
113 struct console_renderer_event* events;
116 static const struct object_ops console_input_events_ops =
118 sizeof(struct console_input_events), /* size */
119 console_input_events_dump, /* dump */
120 no_get_type, /* get_type */
121 add_queue, /* add_queue */
122 remove_queue, /* remove_queue */
123 console_input_events_signaled, /* signaled */
124 no_satisfied, /* satisfied */
125 no_signal, /* signal */
126 no_get_fd, /* get_fd */
127 default_fd_map_access, /* map_access */
128 default_get_sd, /* get_sd */
129 default_set_sd, /* set_sd */
130 no_lookup_name, /* lookup_name */
131 no_link_name, /* link_name */
132 NULL, /* unlink_name */
133 no_open_file, /* open_file */
134 no_kernel_obj_list, /* get_kernel_obj_list */
135 no_close_handle, /* close_handle */
136 console_input_events_destroy /* destroy */
139 struct font_info
141 short int width;
142 short int height;
143 short int weight;
144 short int pitch_family;
145 WCHAR *face_name;
146 data_size_t face_len;
149 struct screen_buffer
151 struct object obj; /* object header */
152 struct list entry; /* entry in list of all screen buffers */
153 struct console_input *input; /* associated console input */
154 int mode; /* output mode */
155 int cursor_size; /* size of cursor (percentage filled) */
156 int cursor_visible;/* cursor visibility flag */
157 int cursor_x; /* position of cursor */
158 int cursor_y; /* position of cursor */
159 int width; /* size (w-h) of the screen buffer */
160 int height;
161 int max_width; /* size (w-h) of the window given font size */
162 int max_height;
163 char_info_t *data; /* the data for each cell - a width x height matrix */
164 unsigned short attr; /* default fill attributes (screen colors) */
165 unsigned short popup_attr; /* pop-up color attributes */
166 unsigned int color_map[16]; /* color table */
167 rectangle_t win; /* current visible window on the screen buffer *
168 * as seen in wineconsole */
169 struct font_info font; /* console font information */
170 struct fd *fd; /* for bare console, attached output fd */
173 static void screen_buffer_dump( struct object *obj, int verbose );
174 static void screen_buffer_destroy( struct object *obj );
175 static struct fd *screen_buffer_get_fd( struct object *obj );
176 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
177 unsigned int sharing, unsigned int options );
179 static const struct object_ops screen_buffer_ops =
181 sizeof(struct screen_buffer), /* size */
182 screen_buffer_dump, /* dump */
183 no_get_type, /* get_type */
184 no_add_queue, /* add_queue */
185 NULL, /* remove_queue */
186 NULL, /* signaled */
187 NULL, /* satisfied */
188 no_signal, /* signal */
189 screen_buffer_get_fd, /* get_fd */
190 default_fd_map_access, /* map_access */
191 default_get_sd, /* get_sd */
192 default_set_sd, /* set_sd */
193 no_lookup_name, /* lookup_name */
194 no_link_name, /* link_name */
195 NULL, /* unlink_name */
196 screen_buffer_open_file, /* open_file */
197 no_kernel_obj_list, /* get_kernel_obj_list */
198 no_close_handle, /* close_handle */
199 screen_buffer_destroy /* destroy */
202 static enum server_fd_type console_get_fd_type( struct fd *fd );
204 static const struct fd_ops console_fd_ops =
206 default_fd_get_poll_events, /* get_poll_events */
207 default_poll_event, /* poll_event */
208 console_get_fd_type, /* get_fd_type */
209 no_fd_read, /* read */
210 no_fd_write, /* write */
211 no_fd_flush, /* flush */
212 no_fd_get_file_info, /* get_file_info */
213 no_fd_get_volume_info, /* get_volume_info */
214 default_fd_ioctl, /* ioctl */
215 default_fd_queue_async, /* queue_async */
216 default_fd_reselect_async /* reselect_async */
219 static struct object_type *console_device_get_type( struct object *obj );
220 static void console_device_dump( struct object *obj, int verbose );
221 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
222 static struct object *console_device_open_file( struct object *obj, unsigned int access,
223 unsigned int sharing, unsigned int options );
225 static const struct object_ops console_device_ops =
227 sizeof(struct object), /* size */
228 console_device_dump, /* dump */
229 console_device_get_type, /* get_type */
230 no_add_queue, /* add_queue */
231 NULL, /* remove_queue */
232 NULL, /* signaled */
233 no_satisfied, /* satisfied */
234 no_signal, /* signal */
235 no_get_fd, /* get_fd */
236 default_fd_map_access, /* map_access */
237 default_get_sd, /* get_sd */
238 default_set_sd, /* set_sd */
239 console_device_lookup_name, /* lookup_name */
240 directory_link_name, /* link_name */
241 default_unlink_name, /* unlink_name */
242 console_device_open_file, /* open_file */
243 no_kernel_obj_list, /* get_kernel_obj_list */
244 no_close_handle, /* close_handle */
245 no_destroy /* destroy */
248 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
250 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
252 static int console_input_is_bare( struct console_input* cin )
254 return cin->evt == NULL;
257 static struct fd *console_input_get_fd( struct object* obj )
259 struct console_input *console_input = (struct console_input*)obj;
260 assert( obj->ops == &console_input_ops );
261 if (console_input->fd)
262 return (struct fd*)grab_object( console_input->fd );
263 set_error( STATUS_OBJECT_TYPE_MISMATCH );
264 return NULL;
267 static enum server_fd_type console_get_fd_type( struct fd *fd )
269 return FD_TYPE_CHAR;
272 /* dumps the renderer events of a console */
273 static void console_input_events_dump( struct object *obj, int verbose )
275 struct console_input_events *evts = (struct console_input_events *)obj;
276 assert( obj->ops == &console_input_events_ops );
277 fprintf( stderr, "Console input events: %d/%d events\n",
278 evts->num_used, evts->num_alloc );
281 /* destroys the renderer events of a console */
282 static void console_input_events_destroy( struct object *obj )
284 struct console_input_events *evts = (struct console_input_events *)obj;
285 assert( obj->ops == &console_input_events_ops );
286 free( evts->events );
289 /* the renderer events list is signaled when it's not empty */
290 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
292 struct console_input_events *evts = (struct console_input_events *)obj;
293 assert( obj->ops == &console_input_events_ops );
294 return (evts->num_used != 0);
297 /* add an event to the console's renderer events list */
298 static void console_input_events_append( struct console_input* console,
299 struct console_renderer_event* evt)
301 struct console_input_events* evts;
302 int collapsed = FALSE;
304 if (!(evts = console->evt)) return;
305 /* to be done even when evt has been generated by the renderer ? */
307 /* try to collapse evt into current queue's events */
308 if (evts->num_used)
310 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
312 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
313 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
315 /* if two update events overlap, collapse them into a single one */
316 if (last->u.update.bottom + 1 >= evt->u.update.top &&
317 evt->u.update.bottom + 1 >= last->u.update.top)
319 last->u.update.top = min(last->u.update.top, evt->u.update.top);
320 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
321 collapsed = TRUE;
325 if (!collapsed)
327 if (evts->num_used == evts->num_alloc)
329 evts->num_alloc += 16;
330 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
331 assert(evts->events);
333 evts->events[evts->num_used++] = *evt;
335 wake_up( &evts->obj, 0 );
338 /* retrieves events from the console's renderer events list */
339 static void console_input_events_get( struct console_input_events* evts )
341 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
343 if (num > evts->num_used) num = evts->num_used;
344 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
345 if (num < evts->num_used)
347 memmove( &evts->events[0], &evts->events[num],
348 (evts->num_used - num) * sizeof(evts->events[0]) );
350 evts->num_used -= num;
353 static struct console_input_events *create_console_input_events(void)
355 struct console_input_events* evt;
357 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
358 evt->num_alloc = evt->num_used = 0;
359 evt->events = NULL;
360 return evt;
363 static struct object *create_console_input( struct thread* renderer, int fd )
365 struct console_input *console_input;
367 if (!(console_input = alloc_object( &console_input_ops )))
369 if (fd != -1) close( fd );
370 return NULL;
372 console_input->renderer = renderer;
373 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
374 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
375 ENABLE_EXTENDED_FLAGS;
376 console_input->num_proc = 0;
377 console_input->active = NULL;
378 console_input->recnum = 0;
379 console_input->records = NULL;
380 console_input->evt = renderer ? create_console_input_events() : NULL;
381 console_input->title = NULL;
382 console_input->title_len = 0;
383 console_input->history_size = 50;
384 console_input->history = calloc( console_input->history_size, sizeof(*console_input->history) );
385 console_input->history_index = 0;
386 console_input->history_mode = 0;
387 console_input->edition_mode = 0;
388 console_input->input_cp = 0;
389 console_input->output_cp = 0;
390 console_input->win = 0;
391 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
392 console_input->fd = NULL;
394 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
396 if (fd != -1) close( fd );
397 console_input->history_size = 0;
398 release_object( console_input );
399 return NULL;
401 if (fd != -1) /* bare console */
403 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
404 FILE_SYNCHRONOUS_IO_NONALERT )))
406 release_object( console_input );
407 return NULL;
409 allow_fd_caching( console_input->fd );
412 return &console_input->obj;
415 static void generate_sb_initial_events( struct console_input *console_input )
417 struct screen_buffer *screen_buffer = console_input->active;
418 struct console_renderer_event evt;
420 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
421 memset(&evt.u, 0, sizeof(evt.u));
422 console_input_events_append( console_input, &evt );
424 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
425 evt.u.resize.width = screen_buffer->width;
426 evt.u.resize.height = screen_buffer->height;
427 console_input_events_append( console_input, &evt );
429 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
430 evt.u.display.left = screen_buffer->win.left;
431 evt.u.display.top = screen_buffer->win.top;
432 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
433 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
434 console_input_events_append( console_input, &evt );
436 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
437 evt.u.update.top = 0;
438 evt.u.update.bottom = screen_buffer->height - 1;
439 console_input_events_append( console_input, &evt );
441 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
442 evt.u.cursor_geom.size = screen_buffer->cursor_size;
443 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
444 console_input_events_append( console_input, &evt );
446 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
447 evt.u.cursor_pos.x = screen_buffer->cursor_x;
448 evt.u.cursor_pos.y = screen_buffer->cursor_y;
449 console_input_events_append( console_input, &evt );
452 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
454 struct screen_buffer *screen_buffer;
455 int i;
457 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
459 if (fd != -1) close( fd );
460 return NULL;
462 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
463 screen_buffer->input = console_input;
464 screen_buffer->cursor_size = 100;
465 screen_buffer->cursor_visible = 1;
466 screen_buffer->width = 80;
467 screen_buffer->height = 150;
468 screen_buffer->max_width = 80;
469 screen_buffer->max_height = 25;
470 screen_buffer->cursor_x = 0;
471 screen_buffer->cursor_y = 0;
472 screen_buffer->attr = 0x0F;
473 screen_buffer->popup_attr = 0xF5;
474 screen_buffer->win.left = 0;
475 screen_buffer->win.right = screen_buffer->max_width - 1;
476 screen_buffer->win.top = 0;
477 screen_buffer->win.bottom = screen_buffer->max_height - 1;
478 screen_buffer->data = NULL;
479 screen_buffer->font.width = 0;
480 screen_buffer->font.height = 0;
481 screen_buffer->font.weight = FW_NORMAL;
482 screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
483 screen_buffer->font.face_name = NULL;
484 screen_buffer->font.face_len = 0;
485 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
486 list_add_head( &screen_buffer_list, &screen_buffer->entry );
488 if (fd == -1)
489 screen_buffer->fd = NULL;
490 else
492 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
493 FILE_SYNCHRONOUS_IO_NONALERT )))
495 release_object( screen_buffer );
496 return NULL;
498 allow_fd_caching(screen_buffer->fd);
501 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
502 sizeof(*screen_buffer->data) )))
504 release_object( screen_buffer );
505 return NULL;
507 /* clear the first row */
508 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
509 /* and copy it to all other rows */
510 for (i = 1; i < screen_buffer->height; i++)
511 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
512 screen_buffer->width * sizeof(char_info_t) );
514 if (!console_input->active)
516 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
517 generate_sb_initial_events( console_input );
519 return screen_buffer;
522 /* free the console for this process */
523 int free_console( struct process *process )
525 struct console_input* console = process->console;
527 if (!console) return 0;
529 process->console = NULL;
530 if (--console->num_proc == 0 && console->renderer)
532 /* all processes have terminated... tell the renderer to terminate too */
533 struct console_renderer_event evt;
534 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
535 memset(&evt.u, 0, sizeof(evt.u));
536 console_input_events_append( console, &evt );
538 release_object( console );
540 return 1;
543 /* let process inherit the console from parent... this handle two cases :
544 * 1/ generic console inheritance
545 * 2/ parent is a renderer which launches process, and process should attach to the console
546 * rendered by parent
548 void inherit_console( struct thread *parent_thread, struct process *parent, struct process *process,
549 obj_handle_t hconin )
551 int done = 0;
553 /* if parent is a renderer, then attach current process to its console
554 * a bit hacky....
556 if (hconin && parent_thread)
558 struct console_input *console;
560 /* FIXME: should we check some access rights ? */
561 if ((console = (struct console_input *)get_handle_obj( parent, hconin,
562 0, &console_input_ops )))
564 if (console->renderer == parent_thread)
566 process->console = (struct console_input *)grab_object( console );
567 process->console->num_proc++;
568 done = 1;
570 release_object( console );
572 else clear_error(); /* ignore error */
574 /* otherwise, if parent has a console, attach child to this console */
575 if (!done && parent->console)
577 process->console = (struct console_input *)grab_object( parent->console );
578 process->console->num_proc++;
582 struct thread *console_get_renderer( struct console_input *console )
584 return console->renderer;
587 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
589 struct console_input* console = NULL;
591 if (handle)
592 console = (struct console_input *)get_handle_obj( current->process, handle,
593 access, &console_input_ops );
594 else if (current->process->console)
596 console = (struct console_input *)grab_object( current->process->console );
599 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
600 return console;
603 struct console_signal_info
605 struct console_input *console;
606 process_id_t group;
607 int signal;
610 static int propagate_console_signal_cb(struct process *process, void *user)
612 struct console_signal_info* csi = (struct console_signal_info*)user;
614 if (process->console == csi->console && process->running_threads &&
615 (!csi->group || process->group_id == csi->group))
617 /* find a suitable thread to signal */
618 struct thread *thread;
619 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
621 if (send_thread_signal( thread, csi->signal )) break;
624 return FALSE;
627 static void propagate_console_signal( struct console_input *console,
628 int sig, process_id_t group_id )
630 struct console_signal_info csi;
632 if (!console)
634 set_error( STATUS_INVALID_PARAMETER );
635 return;
637 /* FIXME: should support the other events (like CTRL_BREAK) */
638 if (sig != CTRL_C_EVENT)
640 set_error( STATUS_NOT_IMPLEMENTED );
641 return;
643 csi.console = console;
644 csi.signal = SIGINT;
645 csi.group = group_id;
647 enum_processes(propagate_console_signal_cb, &csi);
650 static int get_console_mode( obj_handle_t handle )
652 struct object *obj;
653 int ret = 0;
655 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
657 if (obj->ops == &console_input_ops)
659 ret = ((struct console_input *)obj)->mode;
661 else if (obj->ops == &screen_buffer_ops)
663 ret = ((struct screen_buffer *)obj)->mode;
665 else
666 set_error( STATUS_OBJECT_TYPE_MISMATCH );
667 release_object( obj );
669 return ret;
672 /* changes the mode of either a console input or a screen buffer */
673 static int set_console_mode( obj_handle_t handle, int mode )
675 struct object *obj;
676 int ret = 0;
678 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
679 return 0;
680 if (obj->ops == &console_input_ops)
682 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
683 ((struct console_input *)obj)->mode = mode;
684 ret = 1;
686 else if (obj->ops == &screen_buffer_ops)
688 ((struct screen_buffer *)obj)->mode = mode;
689 ret = 1;
691 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
692 release_object( obj );
693 return ret;
696 /* add input events to a console input queue */
697 static int write_console_input( struct console_input* console, int count,
698 const INPUT_RECORD *records )
700 INPUT_RECORD *new_rec;
702 if (!count) return 0;
703 if (!(new_rec = realloc( console->records,
704 (console->recnum + count) * sizeof(INPUT_RECORD) )))
706 set_error( STATUS_NO_MEMORY );
707 return -1;
709 console->records = new_rec;
710 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
712 if (console->mode & ENABLE_PROCESSED_INPUT)
714 int i = 0;
715 while (i < count)
717 if (records[i].EventType == KEY_EVENT &&
718 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
719 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
721 if (i != count - 1)
722 memcpy( &console->records[console->recnum + i],
723 &console->records[console->recnum + i + 1],
724 (count - i - 1) * sizeof(INPUT_RECORD) );
725 count--;
726 if (records[i].Event.KeyEvent.bKeyDown)
728 /* send SIGINT to all processes attached to this console */
729 propagate_console_signal( console, CTRL_C_EVENT, 0 );
732 else i++;
735 if (!console->recnum && count) set_event( console->event );
736 console->recnum += count;
737 return count;
740 /* retrieve a pointer to the console input records */
741 static int read_console_input( obj_handle_t handle, int count, int flush )
743 struct console_input *console;
745 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
746 FILE_READ_DATA, &console_input_ops )))
747 return -1;
749 if (!count)
751 /* special case: do not retrieve anything, but return
752 * the total number of records available */
753 count = console->recnum;
755 else
757 if (count > console->recnum) count = console->recnum;
758 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
760 if (flush)
762 int i;
763 for (i = count; i < console->recnum; i++)
764 console->records[i-count] = console->records[i];
765 if ((console->recnum -= count) > 0)
767 INPUT_RECORD *new_rec = realloc( console->records,
768 console->recnum * sizeof(INPUT_RECORD) );
769 if (new_rec) console->records = new_rec;
771 else
773 free( console->records );
774 console->records = NULL;
775 reset_event( console->event );
778 release_object( console );
779 return count;
782 /* set misc console input information */
783 static int set_console_input_info( const struct set_console_input_info_request *req,
784 const WCHAR *title, data_size_t len )
786 struct console_input *console;
787 struct console_renderer_event evt;
789 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
790 if (console_input_is_bare(console) &&
791 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
792 SET_CONSOLE_INPUT_INFO_WIN)))
794 set_error( STATUS_UNSUCCESSFUL );
795 goto error;
798 memset(&evt.u, 0, sizeof(evt.u));
799 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
801 struct screen_buffer *screen_buffer;
803 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
804 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
805 if (!screen_buffer || screen_buffer->input != console)
807 set_error( STATUS_INVALID_HANDLE );
808 if (screen_buffer) release_object( screen_buffer );
809 goto error;
812 if (screen_buffer != console->active)
814 if (console->active) release_object( console->active );
815 console->active = screen_buffer;
816 generate_sb_initial_events( console );
818 else
819 release_object( screen_buffer );
821 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
823 WCHAR *new_title = NULL;
825 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
826 if (len && !(new_title = memdup( title, len ))) goto error;
827 free( console->title );
828 console->title = new_title;
829 console->title_len = len;
830 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
831 console_input_events_append( console, &evt );
833 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
835 console->history_mode = req->history_mode;
837 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
838 console->history_size != req->history_size)
840 struct history_line **mem = NULL;
841 int i, delta;
843 if (req->history_size)
845 if (!(mem = mem_alloc( req->history_size * sizeof(*mem) ))) goto error;
846 memset( mem, 0, req->history_size * sizeof(*mem) );
849 delta = (console->history_index > req->history_size) ?
850 (console->history_index - req->history_size) : 0;
852 for (i = delta; i < console->history_index; i++)
854 mem[i - delta] = console->history[i];
855 console->history[i] = NULL;
857 console->history_index -= delta;
859 for (i = 0; i < console->history_size; i++)
860 free( console->history[i] );
861 free( console->history );
862 console->history = mem;
863 console->history_size = req->history_size;
865 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
867 console->edition_mode = req->edition_mode;
869 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
871 console->input_cp = req->input_cp;
873 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
875 console->output_cp = req->output_cp;
877 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
879 console->win = req->win;
881 release_object( console );
882 return 1;
883 error:
884 if (console) release_object( console );
885 return 0;
888 /* resize a screen buffer */
889 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
890 int new_width, int new_height )
892 int i, old_width, old_height, copy_width, copy_height;
893 char_info_t *new_data;
895 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
897 set_error( STATUS_NO_MEMORY );
898 return 0;
900 old_width = screen_buffer->width;
901 old_height = screen_buffer->height;
902 copy_width = min( old_width, new_width );
903 copy_height = min( old_height, new_height );
905 /* copy all the rows */
906 for (i = 0; i < copy_height; i++)
908 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
909 copy_width * sizeof(char_info_t) );
912 /* clear the end of each row */
913 if (new_width > old_width)
915 /* fill first row */
916 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
917 /* and blast it to the other rows */
918 for (i = 1; i < copy_height; i++)
919 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
920 (new_width - old_width) * sizeof(char_info_t) );
923 /* clear remaining rows */
924 if (new_height > old_height)
926 /* fill first row */
927 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
928 /* and blast it to the other rows */
929 for (i = old_height+1; i < new_height; i++)
930 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
931 new_width * sizeof(char_info_t) );
933 free( screen_buffer->data );
934 screen_buffer->data = new_data;
935 screen_buffer->width = new_width;
936 screen_buffer->height = new_height;
937 return 1;
940 /* set misc screen buffer information */
941 static int set_console_output_info( struct screen_buffer *screen_buffer,
942 const struct set_console_output_info_request *req )
944 struct console_renderer_event evt;
945 data_size_t font_name_len, offset;
946 WCHAR *font_name;
948 memset(&evt.u, 0, sizeof(evt.u));
949 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
951 if (req->cursor_size < 1 || req->cursor_size > 100)
953 set_error( STATUS_INVALID_PARAMETER );
954 return 0;
956 if (screen_buffer->cursor_size != req->cursor_size ||
957 screen_buffer->cursor_visible != req->cursor_visible)
959 screen_buffer->cursor_size = req->cursor_size;
960 screen_buffer->cursor_visible = req->cursor_visible;
961 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
962 evt.u.cursor_geom.size = req->cursor_size;
963 evt.u.cursor_geom.visible = req->cursor_visible;
964 console_input_events_append( screen_buffer->input, &evt );
967 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
969 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
970 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
972 set_error( STATUS_INVALID_PARAMETER );
973 return 0;
975 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
977 screen_buffer->cursor_x = req->cursor_x;
978 screen_buffer->cursor_y = req->cursor_y;
979 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
980 evt.u.cursor_pos.x = req->cursor_x;
981 evt.u.cursor_pos.y = req->cursor_y;
982 console_input_events_append( screen_buffer->input, &evt );
985 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
987 unsigned cc;
989 /* new screen-buffer cannot be smaller than actual window */
990 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
991 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
993 set_error( STATUS_INVALID_PARAMETER );
994 return 0;
996 /* FIXME: there are also some basic minimum and max size to deal with */
997 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
999 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
1000 evt.u.resize.width = req->width;
1001 evt.u.resize.height = req->height;
1002 console_input_events_append( screen_buffer->input, &evt );
1004 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1005 evt.u.update.top = 0;
1006 evt.u.update.bottom = screen_buffer->height - 1;
1007 console_input_events_append( screen_buffer->input, &evt );
1009 /* scroll window to display sb */
1010 if (screen_buffer->win.right >= req->width)
1012 screen_buffer->win.right -= screen_buffer->win.left;
1013 screen_buffer->win.left = 0;
1015 if (screen_buffer->win.bottom >= req->height)
1017 screen_buffer->win.bottom -= screen_buffer->win.top;
1018 screen_buffer->win.top = 0;
1020 /* reset cursor if needed (normally, if cursor was outside of new sb, the
1021 * window has been shifted so that the new position of the cursor will be
1022 * visible */
1023 cc = 0;
1024 if (screen_buffer->cursor_x >= req->width)
1026 screen_buffer->cursor_x = req->width - 1;
1027 cc++;
1029 if (screen_buffer->cursor_y >= req->height)
1031 screen_buffer->cursor_y = req->height - 1;
1032 cc++;
1034 if (cc)
1036 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1037 evt.u.cursor_pos.x = req->cursor_x;
1038 evt.u.cursor_pos.y = req->cursor_y;
1039 console_input_events_append( screen_buffer->input, &evt );
1042 if (screen_buffer == screen_buffer->input->active &&
1043 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
1045 INPUT_RECORD ir;
1046 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
1047 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
1048 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
1049 write_console_input( screen_buffer->input, 1, &ir );
1052 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1054 screen_buffer->attr = req->attr;
1056 if (req->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1058 screen_buffer->popup_attr = req->popup_attr;
1060 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1062 if (req->win_left < 0 || req->win_left > req->win_right ||
1063 req->win_right >= screen_buffer->width ||
1064 req->win_top < 0 || req->win_top > req->win_bottom ||
1065 req->win_bottom >= screen_buffer->height)
1067 set_error( STATUS_INVALID_PARAMETER );
1068 return 0;
1070 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1071 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1073 screen_buffer->win.left = req->win_left;
1074 screen_buffer->win.top = req->win_top;
1075 screen_buffer->win.right = req->win_right;
1076 screen_buffer->win.bottom = req->win_bottom;
1077 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1078 evt.u.display.left = req->win_left;
1079 evt.u.display.top = req->win_top;
1080 evt.u.display.width = req->win_right - req->win_left + 1;
1081 evt.u.display.height = req->win_bottom - req->win_top + 1;
1082 console_input_events_append( screen_buffer->input, &evt );
1085 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1087 screen_buffer->max_width = req->max_width;
1088 screen_buffer->max_height = req->max_height;
1090 if (req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1092 memcpy( screen_buffer->color_map, get_req_data(), min( get_req_data_size(), sizeof(screen_buffer->color_map) ));
1094 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1096 screen_buffer->font.width = req->font_width;
1097 screen_buffer->font.height = req->font_height;
1098 screen_buffer->font.weight = req->font_weight;
1099 screen_buffer->font.pitch_family = req->font_pitch_family;
1100 offset = req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE ? sizeof(screen_buffer->color_map) : 0;
1101 if (get_req_data_size() > offset)
1103 font_name_len = (get_req_data_size() - offset) / sizeof(WCHAR) * sizeof(WCHAR);
1104 font_name = mem_alloc( font_name_len );
1105 if (font_name)
1107 memcpy( font_name, (char *)get_req_data() + offset, font_name_len );
1108 free( screen_buffer->font.face_name );
1109 screen_buffer->font.face_name = font_name;
1110 screen_buffer->font.face_len = font_name_len;
1115 return 1;
1118 /* appends a new line to history (history is a fixed size array) */
1119 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1121 struct history_line *ptr;
1123 if (!console || !console->history_size)
1125 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1126 return;
1129 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
1130 if (console->history_mode && console->history_index &&
1131 console->history[console->history_index - 1]->len == len &&
1132 !memcmp( console->history[console->history_index - 1]->text, buf, len ))
1134 /* don't duplicate entry */
1135 set_error( STATUS_ALIAS_EXISTS );
1136 return;
1138 if (!(ptr = mem_alloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
1139 ptr->len = len;
1140 memcpy( ptr->text, buf, len );
1142 if (console->history_index < console->history_size)
1144 console->history[console->history_index++] = ptr;
1146 else
1148 free( console->history[0]) ;
1149 memmove( &console->history[0], &console->history[1],
1150 (console->history_size - 1) * sizeof(*console->history) );
1151 console->history[console->history_size - 1] = ptr;
1155 /* returns a line from the cache */
1156 static data_size_t console_input_get_hist( struct console_input *console, int index )
1158 data_size_t ret = 0;
1160 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1161 else
1163 ret = console->history[index]->len;
1164 set_reply_data( console->history[index]->text, min( ret, get_reply_max_size() ));
1166 return ret;
1169 /* dumb dump */
1170 static void console_input_dump( struct object *obj, int verbose )
1172 struct console_input *console = (struct console_input *)obj;
1173 assert( obj->ops == &console_input_ops );
1174 fprintf( stderr, "Console input active=%p evt=%p\n",
1175 console->active, console->evt );
1178 static void console_input_destroy( struct object *obj )
1180 struct console_input* console_in = (struct console_input *)obj;
1181 struct screen_buffer* curr;
1182 int i;
1184 assert( obj->ops == &console_input_ops );
1185 free( console_in->title );
1186 free( console_in->records );
1188 if (console_in->active) release_object( console_in->active );
1189 console_in->active = NULL;
1191 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1193 if (curr->input == console_in) curr->input = NULL;
1196 if (console_in->evt)
1198 release_object( console_in->evt );
1199 console_in->evt = NULL;
1201 if (console_in->event)
1202 release_object( console_in->event );
1203 if (console_in->fd)
1204 release_object( console_in->fd );
1206 for (i = 0; i < console_in->history_size; i++)
1207 free( console_in->history[i] );
1208 free( console_in->history );
1211 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1212 unsigned int sharing, unsigned int options )
1214 return grab_object( obj );
1217 static void screen_buffer_dump( struct object *obj, int verbose )
1219 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1220 assert( obj->ops == &screen_buffer_ops );
1222 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1225 static void screen_buffer_destroy( struct object *obj )
1227 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1229 assert( obj->ops == &screen_buffer_ops );
1231 list_remove( &screen_buffer->entry );
1233 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1235 struct screen_buffer *sb;
1237 screen_buffer->input->active = NULL;
1238 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1240 if (sb->input == screen_buffer->input)
1242 sb->input->active = sb;
1243 break;
1247 if (screen_buffer->fd) release_object( screen_buffer->fd );
1248 free( screen_buffer->data );
1249 free( screen_buffer->font.face_name );
1252 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
1253 unsigned int sharing, unsigned int options )
1255 return grab_object( obj );
1258 static struct fd *screen_buffer_get_fd( struct object *obj )
1260 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1261 assert( obj->ops == &screen_buffer_ops );
1262 if (screen_buffer->fd)
1263 return (struct fd*)grab_object( screen_buffer->fd );
1264 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1265 return NULL;
1268 /* write data into a screen buffer */
1269 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1270 const void* data, enum char_info_mode mode,
1271 int x, int y, int wrap )
1273 unsigned int i;
1274 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1276 if (y >= screen_buffer->height) return 0;
1278 if (wrap)
1279 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1280 else
1281 end = screen_buffer->data + (y+1) * screen_buffer->width;
1283 switch(mode)
1285 case CHAR_INFO_MODE_TEXT:
1287 const WCHAR *ptr = data;
1288 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1290 break;
1291 case CHAR_INFO_MODE_ATTR:
1293 const unsigned short *ptr = data;
1294 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1296 break;
1297 case CHAR_INFO_MODE_TEXTATTR:
1299 const char_info_t *ptr = data;
1300 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1302 break;
1303 case CHAR_INFO_MODE_TEXTSTDATTR:
1305 const WCHAR *ptr = data;
1306 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1308 dest->ch = ptr[i];
1309 dest->attr = screen_buffer->attr;
1312 break;
1313 default:
1314 set_error( STATUS_INVALID_PARAMETER );
1315 return 0;
1318 if (i && screen_buffer == screen_buffer->input->active)
1320 struct console_renderer_event evt;
1321 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1322 memset(&evt.u, 0, sizeof(evt.u));
1323 evt.u.update.top = y + x / screen_buffer->width;
1324 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1325 console_input_events_append( screen_buffer->input, &evt );
1327 return i;
1330 /* fill a screen buffer with uniform data */
1331 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1332 enum char_info_mode mode, int x, int y, int count, int wrap )
1334 int i;
1335 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1337 if (y >= screen_buffer->height) return 0;
1339 if (wrap)
1340 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1341 else
1342 end = screen_buffer->data + (y+1) * screen_buffer->width;
1344 if (count > end - dest) count = end - dest;
1346 switch(mode)
1348 case CHAR_INFO_MODE_TEXT:
1349 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1350 break;
1351 case CHAR_INFO_MODE_ATTR:
1352 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1353 break;
1354 case CHAR_INFO_MODE_TEXTATTR:
1355 for (i = 0; i < count; i++) dest[i] = data;
1356 break;
1357 case CHAR_INFO_MODE_TEXTSTDATTR:
1358 for (i = 0; i < count; i++)
1360 dest[i].ch = data.ch;
1361 dest[i].attr = screen_buffer->attr;
1363 break;
1364 default:
1365 set_error( STATUS_INVALID_PARAMETER );
1366 return 0;
1369 if (count && screen_buffer == screen_buffer->input->active)
1371 struct console_renderer_event evt;
1372 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1373 memset(&evt.u, 0, sizeof(evt.u));
1374 evt.u.update.top = y;
1375 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1376 console_input_events_append( screen_buffer->input, &evt );
1378 return i;
1381 /* read data from a screen buffer */
1382 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1383 enum char_info_mode mode, int wrap )
1385 int i;
1386 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1388 if (y >= screen_buffer->height) return;
1390 if (wrap)
1391 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1392 else
1393 end = screen_buffer->data + (y+1) * screen_buffer->width;
1395 switch(mode)
1397 case CHAR_INFO_MODE_TEXT:
1399 WCHAR *data;
1400 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1401 if ((data = set_reply_data_size( count * sizeof(*data) )))
1403 for (i = 0; i < count; i++) data[i] = src[i].ch;
1406 break;
1407 case CHAR_INFO_MODE_ATTR:
1409 unsigned short *data;
1410 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1411 if ((data = set_reply_data_size( count * sizeof(*data) )))
1413 for (i = 0; i < count; i++) data[i] = src[i].attr;
1416 break;
1417 case CHAR_INFO_MODE_TEXTATTR:
1419 char_info_t *data;
1420 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1421 if ((data = set_reply_data_size( count * sizeof(*data) )))
1423 for (i = 0; i < count; i++) data[i] = src[i];
1426 break;
1427 default:
1428 set_error( STATUS_INVALID_PARAMETER );
1429 break;
1433 /* scroll parts of a screen buffer */
1434 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1435 int w, int h )
1437 int j;
1438 char_info_t *psrc, *pdst;
1439 struct console_renderer_event evt;
1441 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1442 xsrc + w > screen_buffer->width ||
1443 xdst + w > screen_buffer->width ||
1444 ysrc + h > screen_buffer->height ||
1445 ydst + h > screen_buffer->height ||
1446 w == 0 || h == 0)
1448 set_error( STATUS_INVALID_PARAMETER );
1449 return;
1452 if (ysrc < ydst)
1454 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1455 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1457 for (j = h; j > 0; j--)
1459 memcpy(pdst, psrc, w * sizeof(*pdst) );
1460 pdst -= screen_buffer->width;
1461 psrc -= screen_buffer->width;
1464 else
1466 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1467 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1469 for (j = 0; j < h; j++)
1471 /* we use memmove here because when psrc and pdst are the same,
1472 * copies are done on the same row, so the dst and src blocks
1473 * can overlap */
1474 memmove( pdst, psrc, w * sizeof(*pdst) );
1475 pdst += screen_buffer->width;
1476 psrc += screen_buffer->width;
1480 /* FIXME: this could be enhanced, by signalling scroll */
1481 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1482 memset(&evt.u, 0, sizeof(evt.u));
1483 evt.u.update.top = min(ysrc, ydst);
1484 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1485 console_input_events_append( screen_buffer->input, &evt );
1488 static struct object_type *console_device_get_type( struct object *obj )
1490 static const WCHAR name[] = {'D','e','v','i','c','e'};
1491 static const struct unicode_str str = { name, sizeof(name) };
1492 return get_object_type( &str );
1495 static void console_device_dump( struct object *obj, int verbose )
1497 fputs( "Console device\n", stderr );
1500 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1502 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1503 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1504 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1506 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1508 if (!current->process->console)
1510 set_error( STATUS_INVALID_HANDLE );
1511 return NULL;
1513 name->len = 0;
1514 return grab_object( current->process->console );
1517 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1519 if (!current->process->console || !current->process->console->active)
1521 set_error( STATUS_INVALID_HANDLE );
1522 return NULL;
1524 name->len = 0;
1525 return grab_object( current->process->console->active );
1528 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1530 name->len = 0;
1531 return grab_object( obj );
1534 return NULL;
1537 static struct object *console_device_open_file( struct object *obj, unsigned int access,
1538 unsigned int sharing, unsigned int options )
1540 int is_output;
1541 access = default_fd_map_access( obj, access );
1542 is_output = access & FILE_WRITE_DATA;
1543 if (!current->process->console || (is_output && !current->process->console))
1545 set_error( STATUS_INVALID_HANDLE );
1546 return NULL;
1548 if (is_output && (access & FILE_READ_DATA))
1550 set_error( STATUS_INVALID_PARAMETER );
1551 return NULL;
1553 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
1556 struct object *create_console_device( struct object *root, const struct unicode_str *name )
1558 return create_named_object( root, &console_device_ops, name, 0, NULL );
1561 /* allocate a console for the renderer */
1562 DECL_HANDLER(alloc_console)
1564 obj_handle_t in = 0;
1565 obj_handle_t evt = 0;
1566 struct process *process;
1567 struct thread *renderer;
1568 struct console_input *console;
1569 int fd;
1570 int attach = 0;
1572 if (req->input_fd != -1)
1574 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1576 set_error( STATUS_INVALID_PARAMETER );
1577 return;
1580 else fd = -1;
1582 switch (req->pid)
1584 case 0:
1585 /* renderer is current, console to be attached to parent process */
1586 renderer = current;
1587 if (!(process = get_process_from_id( current->process->parent_id )))
1589 if (fd != -1) close( fd );
1590 set_error( STATUS_ACCESS_DENIED );
1591 return;
1593 attach = 1;
1594 break;
1595 case 0xffffffff:
1596 /* no renderer, console to be attached to current process */
1597 renderer = NULL;
1598 process = current->process;
1599 grab_object( process );
1600 attach = 1;
1601 break;
1602 default:
1603 /* renderer is current, console to be attached to req->pid */
1604 renderer = current;
1605 if (!(process = get_process_from_id( req->pid )))
1607 if (fd != -1) close( fd );
1608 return;
1612 if (attach && process->console)
1614 if (fd != -1) close( fd );
1615 set_error( STATUS_ACCESS_DENIED );
1616 goto the_end;
1619 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1621 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1623 if (!console->evt ||
1624 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1626 if (attach)
1628 process->console = (struct console_input*)grab_object( console );
1629 console->num_proc++;
1631 reply->handle_in = in;
1632 reply->event = evt;
1633 release_object( console );
1634 goto the_end;
1636 close_handle( current->process, in );
1638 release_object( console );
1640 the_end:
1641 release_object( process );
1644 /* free the console of the current process */
1645 DECL_HANDLER(free_console)
1647 free_console( current->process );
1650 /* let the renderer peek the events it's waiting on */
1651 DECL_HANDLER(get_console_renderer_events)
1653 struct console_input_events *evt;
1655 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1656 FILE_READ_PROPERTIES, &console_input_events_ops );
1657 if (!evt) return;
1658 console_input_events_get( evt );
1659 release_object( evt );
1662 /* open a handle to the process console */
1663 DECL_HANDLER(open_console)
1665 struct object *obj = NULL;
1667 if ((obj = get_handle_obj( current->process, req->from,
1668 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1670 struct console_input *console = (struct console_input *)obj;
1671 obj = (console->active) ? grab_object( console->active ) : NULL;
1672 release_object( console );
1675 /* FIXME: req->share is not used (as in screen buffer creation) */
1676 if (obj)
1678 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1679 release_object( obj );
1681 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1684 /* attach to a other process's console */
1685 DECL_HANDLER(attach_console)
1687 struct process *process;
1689 if (current->process->console)
1691 set_error( STATUS_ACCESS_DENIED );
1692 return;
1695 process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
1696 ? current->process->parent_id : req->pid );
1697 if (!process) return;
1699 if (process->console && process->console->active)
1701 reply->std_in = alloc_handle( current->process, process->console, GENERIC_READ, 0 );
1702 if (!reply->std_in) goto error;
1704 reply->std_out = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
1705 if (!reply->std_out) goto error;
1707 reply->std_err = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
1708 if (!reply->std_err) goto error;
1710 current->process->console = (struct console_input *)grab_object( process->console );
1711 current->process->console->num_proc++;
1713 else
1715 set_error( STATUS_INVALID_HANDLE );
1718 release_object( process );
1719 return;
1721 error:
1722 if (reply->std_in) close_handle( current->process, reply->std_in );
1723 if (reply->std_out) close_handle( current->process, reply->std_out );
1724 release_object( process );
1727 /* set info about a console input */
1728 DECL_HANDLER(set_console_input_info)
1730 set_console_input_info( req, get_req_data(), get_req_data_size() );
1733 /* get info about a console (output only) */
1734 DECL_HANDLER(get_console_input_info)
1736 struct console_input *console;
1738 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1739 if (console->title) set_reply_data( console->title, min( console->title_len, get_reply_max_size() ));
1740 reply->history_mode = console->history_mode;
1741 reply->history_size = console->history_size;
1742 reply->history_index = console->history_index;
1743 reply->edition_mode = console->edition_mode;
1744 reply->input_cp = console->input_cp;
1745 reply->output_cp = console->output_cp;
1746 reply->win = console->win;
1748 release_object( console );
1751 /* get a console mode (input or output) */
1752 DECL_HANDLER(get_console_mode)
1754 reply->mode = get_console_mode( req->handle );
1757 /* set a console mode (input or output) */
1758 DECL_HANDLER(set_console_mode)
1760 set_console_mode( req->handle, req->mode );
1763 /* add input records to a console input queue */
1764 DECL_HANDLER(write_console_input)
1766 struct console_input *console;
1768 reply->written = 0;
1769 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1770 FILE_WRITE_PROPERTIES, &console_input_ops )))
1771 return;
1772 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1773 get_req_data() );
1774 release_object( console );
1777 /* fetch input records from a console input queue */
1778 DECL_HANDLER(read_console_input)
1780 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1781 reply->read = read_console_input( req->handle, count, req->flush );
1784 /* appends a string to console's history */
1785 DECL_HANDLER(append_console_input_history)
1787 struct console_input *console;
1789 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1790 console_input_append_hist( console, get_req_data(), get_req_data_size() );
1791 release_object( console );
1794 /* appends a string to console's history */
1795 DECL_HANDLER(get_console_input_history)
1797 struct console_input *console;
1799 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1800 reply->total = console_input_get_hist( console, req->index );
1801 release_object( console );
1804 /* creates a screen buffer */
1805 DECL_HANDLER(create_console_output)
1807 struct console_input* console;
1808 struct screen_buffer* screen_buffer;
1809 int fd;
1811 if (req->fd != -1)
1813 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1815 set_error( STATUS_INVALID_HANDLE );
1816 return;
1819 else fd = -1;
1820 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1822 if (fd != -1) close( fd );
1823 return;
1825 if (console_input_is_bare( console ) ^ (fd != -1))
1827 if (fd != -1) close( fd );
1828 release_object( console );
1829 set_error( STATUS_INVALID_HANDLE );
1830 return;
1833 screen_buffer = create_console_output( console, fd );
1834 if (screen_buffer)
1836 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1837 * see file.c on how this could be done */
1838 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1839 release_object( screen_buffer );
1841 release_object( console );
1844 /* set info about a console screen buffer */
1845 DECL_HANDLER(set_console_output_info)
1847 struct screen_buffer *screen_buffer;
1849 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1850 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1852 set_console_output_info( screen_buffer, req );
1853 release_object( screen_buffer );
1857 /* get info about a console screen buffer */
1858 DECL_HANDLER(get_console_output_info)
1860 struct screen_buffer *screen_buffer;
1861 void *data;
1862 data_size_t total;
1864 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1865 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1867 reply->cursor_size = screen_buffer->cursor_size;
1868 reply->cursor_visible = screen_buffer->cursor_visible;
1869 reply->cursor_x = screen_buffer->cursor_x;
1870 reply->cursor_y = screen_buffer->cursor_y;
1871 reply->width = screen_buffer->width;
1872 reply->height = screen_buffer->height;
1873 reply->attr = screen_buffer->attr;
1874 reply->popup_attr = screen_buffer->popup_attr;
1875 reply->win_left = screen_buffer->win.left;
1876 reply->win_top = screen_buffer->win.top;
1877 reply->win_right = screen_buffer->win.right;
1878 reply->win_bottom = screen_buffer->win.bottom;
1879 reply->max_width = screen_buffer->max_width;
1880 reply->max_height = screen_buffer->max_height;
1881 reply->font_width = screen_buffer->font.width;
1882 reply->font_height = screen_buffer->font.height;
1883 reply->font_weight = screen_buffer->font.weight;
1884 reply->font_pitch_family = screen_buffer->font.pitch_family;
1885 total = min( sizeof(screen_buffer->color_map) + screen_buffer->font.face_len, get_reply_max_size() );
1886 if (total)
1888 data = set_reply_data_size( total );
1889 memcpy( data, screen_buffer->color_map, min( total, sizeof(screen_buffer->color_map) ));
1890 if (screen_buffer->font.face_len && total > sizeof(screen_buffer->color_map))
1892 memcpy( (char *)data + sizeof(screen_buffer->color_map), screen_buffer->font.face_name,
1893 min( total - sizeof(screen_buffer->color_map), screen_buffer->font.face_len ));
1896 release_object( screen_buffer );
1900 /* read data (chars & attrs) from a screen buffer */
1901 DECL_HANDLER(read_console_output)
1903 struct screen_buffer *screen_buffer;
1905 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1906 FILE_READ_DATA, &screen_buffer_ops )))
1908 if (console_input_is_bare( screen_buffer->input ))
1910 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1911 release_object( screen_buffer );
1912 return;
1914 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1915 reply->width = screen_buffer->width;
1916 reply->height = screen_buffer->height;
1917 release_object( screen_buffer );
1921 /* write data (char and/or attrs) to a screen buffer */
1922 DECL_HANDLER(write_console_output)
1924 struct screen_buffer *screen_buffer;
1926 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1927 FILE_WRITE_DATA, &screen_buffer_ops)))
1929 if (console_input_is_bare( screen_buffer->input ))
1931 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1932 release_object( screen_buffer );
1933 return;
1935 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1936 req->mode, req->x, req->y, req->wrap );
1937 reply->width = screen_buffer->width;
1938 reply->height = screen_buffer->height;
1939 release_object( screen_buffer );
1943 /* fill a screen buffer with constant data (chars and/or attributes) */
1944 DECL_HANDLER(fill_console_output)
1946 struct screen_buffer *screen_buffer;
1948 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1949 FILE_WRITE_DATA, &screen_buffer_ops)))
1951 if (console_input_is_bare( screen_buffer->input ))
1953 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1954 release_object( screen_buffer );
1955 return;
1957 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1958 req->x, req->y, req->count, req->wrap );
1959 release_object( screen_buffer );
1963 /* move a rect of data in a screen buffer */
1964 DECL_HANDLER(move_console_output)
1966 struct screen_buffer *screen_buffer;
1968 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1969 FILE_WRITE_DATA, &screen_buffer_ops)))
1971 if (console_input_is_bare( screen_buffer->input ))
1973 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1974 release_object( screen_buffer );
1975 return;
1977 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1978 req->w, req->h );
1979 release_object( screen_buffer );
1983 /* sends a signal to a console (process, group...) */
1984 DECL_HANDLER(send_console_signal)
1986 process_id_t group;
1988 group = req->group_id ? req->group_id : current->process->group_id;
1990 if (!group)
1991 set_error( STATUS_INVALID_PARAMETER );
1992 else
1993 propagate_console_signal( current->process->console, req->signal, group );
1996 /* get console which renderer is 'current' */
1997 static int cgwe_enum( struct process* process, void* user)
1999 if (process->console && process->console->renderer == current)
2001 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
2002 return 1;
2004 return 0;
2007 DECL_HANDLER(get_console_wait_event)
2009 struct console_input* console = NULL;
2010 struct object *obj;
2012 if (req->handle)
2014 if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
2015 if (obj->ops == &console_input_ops)
2016 console = (struct console_input *)grab_object( obj );
2017 else if (obj->ops == &screen_buffer_ops)
2018 console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
2019 else
2020 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2021 release_object( obj );
2023 else if (current->process->console)
2024 console = (struct console_input *)grab_object( current->process->console );
2025 else enum_processes(cgwe_enum, &console);
2027 if (console)
2029 reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
2030 release_object( console );
2032 else set_error( STATUS_INVALID_PARAMETER );