winex11: Use debugstr_a to print strings that can be NULL.
[wine.git] / server / console.c
blob0d98b78868b781f28b4374192b9fce7b5f5adc2f
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 console_input
47 struct object obj; /* object header */
48 int num_proc; /* number of processes attached to this console */
49 struct thread *renderer; /* console renderer thread */
50 int mode; /* input mode */
51 struct screen_buffer *active; /* active screen buffer */
52 int recnum; /* number of input records */
53 INPUT_RECORD *records; /* input records */
54 struct console_input_events *evt; /* synchronization event with renderer */
55 WCHAR *title; /* console title */
56 WCHAR **history; /* lines history */
57 int history_size; /* number of entries in history array */
58 int history_index; /* number of used entries in history array */
59 int history_mode; /* mode of history (non zero means remove doubled strings */
60 int edition_mode; /* index to edition mode flavors */
61 int input_cp; /* console input codepage */
62 int output_cp; /* console output codepage */
63 user_handle_t win; /* window handle if backend supports it */
64 struct event *event; /* event to wait on for input queue */
65 struct fd *fd; /* for bare console, attached input fd */
68 static void console_input_dump( struct object *obj, int verbose );
69 static void console_input_destroy( struct object *obj );
70 static struct fd *console_input_get_fd( struct object *obj );
72 static const struct object_ops console_input_ops =
74 sizeof(struct console_input), /* size */
75 console_input_dump, /* dump */
76 no_get_type, /* get_type */
77 no_add_queue, /* add_queue */
78 NULL, /* remove_queue */
79 NULL, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 console_input_get_fd, /* get_fd */
83 default_fd_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_link_name, /* link_name */
88 NULL, /* unlink_name */
89 no_open_file, /* open_file */
90 no_close_handle, /* close_handle */
91 console_input_destroy /* destroy */
94 static void console_input_events_dump( struct object *obj, int verbose );
95 static void console_input_events_destroy( struct object *obj );
96 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
98 struct console_input_events
100 struct object obj; /* object header */
101 int num_alloc; /* number of allocated events */
102 int num_used; /* number of actually used events */
103 struct console_renderer_event* events;
106 static const struct object_ops console_input_events_ops =
108 sizeof(struct console_input_events), /* size */
109 console_input_events_dump, /* dump */
110 no_get_type, /* get_type */
111 add_queue, /* add_queue */
112 remove_queue, /* remove_queue */
113 console_input_events_signaled, /* signaled */
114 no_satisfied, /* satisfied */
115 no_signal, /* signal */
116 no_get_fd, /* get_fd */
117 default_fd_map_access, /* map_access */
118 default_get_sd, /* get_sd */
119 default_set_sd, /* set_sd */
120 no_lookup_name, /* lookup_name */
121 no_link_name, /* link_name */
122 NULL, /* unlink_name */
123 no_open_file, /* open_file */
124 no_close_handle, /* close_handle */
125 console_input_events_destroy /* destroy */
128 struct font_info
130 short int width;
131 short int height;
134 struct screen_buffer
136 struct object obj; /* object header */
137 struct list entry; /* entry in list of all screen buffers */
138 struct console_input *input; /* associated console input */
139 int mode; /* output mode */
140 int cursor_size; /* size of cursor (percentage filled) */
141 int cursor_visible;/* cursor visibility flag */
142 int cursor_x; /* position of cursor */
143 int cursor_y; /* position of cursor */
144 int width; /* size (w-h) of the screen buffer */
145 int height;
146 int max_width; /* size (w-h) of the window given font size */
147 int max_height;
148 char_info_t *data; /* the data for each cell - a width x height matrix */
149 unsigned short attr; /* default attribute for screen buffer */
150 rectangle_t win; /* current visible window on the screen buffer *
151 * as seen in wineconsole */
152 struct font_info font; /* console font information */
153 struct fd *fd; /* for bare console, attached output fd */
156 static void screen_buffer_dump( struct object *obj, int verbose );
157 static void screen_buffer_destroy( struct object *obj );
158 static struct fd *screen_buffer_get_fd( struct object *obj );
160 static const struct object_ops screen_buffer_ops =
162 sizeof(struct screen_buffer), /* size */
163 screen_buffer_dump, /* dump */
164 no_get_type, /* get_type */
165 no_add_queue, /* add_queue */
166 NULL, /* remove_queue */
167 NULL, /* signaled */
168 NULL, /* satisfied */
169 no_signal, /* signal */
170 screen_buffer_get_fd, /* get_fd */
171 default_fd_map_access, /* map_access */
172 default_get_sd, /* get_sd */
173 default_set_sd, /* set_sd */
174 no_lookup_name, /* lookup_name */
175 no_link_name, /* link_name */
176 NULL, /* unlink_name */
177 no_open_file, /* open_file */
178 no_close_handle, /* close_handle */
179 screen_buffer_destroy /* destroy */
182 static enum server_fd_type console_get_fd_type( struct fd *fd );
184 static const struct fd_ops console_fd_ops =
186 default_fd_get_poll_events, /* get_poll_events */
187 default_poll_event, /* poll_event */
188 console_get_fd_type, /* get_fd_type */
189 no_fd_read, /* read */
190 no_fd_write, /* write */
191 no_fd_flush, /* flush */
192 default_fd_ioctl, /* ioctl */
193 default_fd_queue_async, /* queue_async */
194 default_fd_reselect_async, /* reselect_async */
195 default_fd_cancel_async /* cancel_async */
198 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
200 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
202 static int console_input_is_bare( struct console_input* cin )
204 return cin->evt == NULL;
207 static struct fd *console_input_get_fd( struct object* obj )
209 struct console_input *console_input = (struct console_input*)obj;
210 assert( obj->ops == &console_input_ops );
211 if (console_input->fd)
212 return (struct fd*)grab_object( console_input->fd );
213 set_error( STATUS_OBJECT_TYPE_MISMATCH );
214 return NULL;
217 static enum server_fd_type console_get_fd_type( struct fd *fd )
219 return FD_TYPE_CHAR;
222 /* dumps the renderer events of a console */
223 static void console_input_events_dump( struct object *obj, int verbose )
225 struct console_input_events *evts = (struct console_input_events *)obj;
226 assert( obj->ops == &console_input_events_ops );
227 fprintf( stderr, "Console input events: %d/%d events\n",
228 evts->num_used, evts->num_alloc );
231 /* destroys the renderer events of a console */
232 static void console_input_events_destroy( struct object *obj )
234 struct console_input_events *evts = (struct console_input_events *)obj;
235 assert( obj->ops == &console_input_events_ops );
236 free( evts->events );
239 /* the renderer events list is signaled when it's not empty */
240 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
242 struct console_input_events *evts = (struct console_input_events *)obj;
243 assert( obj->ops == &console_input_events_ops );
244 return (evts->num_used != 0);
247 /* add an event to the console's renderer events list */
248 static void console_input_events_append( struct console_input* console,
249 struct console_renderer_event* evt)
251 struct console_input_events* evts;
252 int collapsed = FALSE;
254 if (!(evts = console->evt)) return;
255 /* to be done even when evt has been generated by the renderer ? */
257 /* try to collapse evt into current queue's events */
258 if (evts->num_used)
260 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
262 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
263 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
265 /* if two update events overlap, collapse them into a single one */
266 if (last->u.update.bottom + 1 >= evt->u.update.top &&
267 evt->u.update.bottom + 1 >= last->u.update.top)
269 last->u.update.top = min(last->u.update.top, evt->u.update.top);
270 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
271 collapsed = TRUE;
275 if (!collapsed)
277 if (evts->num_used == evts->num_alloc)
279 evts->num_alloc += 16;
280 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
281 assert(evts->events);
283 evts->events[evts->num_used++] = *evt;
285 wake_up( &evts->obj, 0 );
288 /* retrieves events from the console's renderer events list */
289 static void console_input_events_get( struct console_input_events* evts )
291 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
293 if (num > evts->num_used) num = evts->num_used;
294 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
295 if (num < evts->num_used)
297 memmove( &evts->events[0], &evts->events[num],
298 (evts->num_used - num) * sizeof(evts->events[0]) );
300 evts->num_used -= num;
303 static struct console_input_events *create_console_input_events(void)
305 struct console_input_events* evt;
307 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
308 evt->num_alloc = evt->num_used = 0;
309 evt->events = NULL;
310 return evt;
313 static struct object *create_console_input( struct thread* renderer, int fd )
315 struct console_input *console_input;
317 if (!(console_input = alloc_object( &console_input_ops )))
319 if (fd != -1) close( fd );
320 return NULL;
322 console_input->renderer = renderer;
323 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
324 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
325 ENABLE_EXTENDED_FLAGS;
326 console_input->num_proc = 0;
327 console_input->active = NULL;
328 console_input->recnum = 0;
329 console_input->records = NULL;
330 console_input->evt = renderer ? create_console_input_events() : NULL;
331 console_input->title = NULL;
332 console_input->history_size = 50;
333 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
334 console_input->history_index = 0;
335 console_input->history_mode = 0;
336 console_input->edition_mode = 0;
337 console_input->input_cp = 0;
338 console_input->output_cp = 0;
339 console_input->win = 0;
340 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
341 console_input->fd = NULL;
343 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
345 if (fd != -1) close( fd );
346 console_input->history_size = 0;
347 release_object( console_input );
348 return NULL;
350 if (fd != -1) /* bare console */
352 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
353 FILE_SYNCHRONOUS_IO_NONALERT )))
355 release_object( console_input );
356 return NULL;
358 allow_fd_caching( console_input->fd );
361 return &console_input->obj;
364 static void generate_sb_initial_events( struct console_input *console_input )
366 struct screen_buffer *screen_buffer = console_input->active;
367 struct console_renderer_event evt;
369 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
370 memset(&evt.u, 0, sizeof(evt.u));
371 console_input_events_append( console_input, &evt );
373 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
374 evt.u.resize.width = screen_buffer->width;
375 evt.u.resize.height = screen_buffer->height;
376 console_input_events_append( console_input, &evt );
378 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
379 evt.u.display.left = screen_buffer->win.left;
380 evt.u.display.top = screen_buffer->win.top;
381 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
382 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
383 console_input_events_append( console_input, &evt );
385 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
386 evt.u.update.top = 0;
387 evt.u.update.bottom = screen_buffer->height - 1;
388 console_input_events_append( console_input, &evt );
390 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
391 evt.u.cursor_geom.size = screen_buffer->cursor_size;
392 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
393 console_input_events_append( console_input, &evt );
395 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
396 evt.u.cursor_pos.x = screen_buffer->cursor_x;
397 evt.u.cursor_pos.y = screen_buffer->cursor_y;
398 console_input_events_append( console_input, &evt );
401 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
403 struct screen_buffer *screen_buffer;
404 int i;
406 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
408 if (fd != -1) close( fd );
409 return NULL;
411 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
412 screen_buffer->input = console_input;
413 screen_buffer->cursor_size = 100;
414 screen_buffer->cursor_visible = 1;
415 screen_buffer->width = 80;
416 screen_buffer->height = 150;
417 screen_buffer->max_width = 80;
418 screen_buffer->max_height = 25;
419 screen_buffer->cursor_x = 0;
420 screen_buffer->cursor_y = 0;
421 screen_buffer->attr = 0x0F;
422 screen_buffer->win.left = 0;
423 screen_buffer->win.right = screen_buffer->max_width - 1;
424 screen_buffer->win.top = 0;
425 screen_buffer->win.bottom = screen_buffer->max_height - 1;
426 screen_buffer->data = NULL;
427 screen_buffer->font.width = 0;
428 screen_buffer->font.height = 0;
429 list_add_head( &screen_buffer_list, &screen_buffer->entry );
431 if (fd == -1)
432 screen_buffer->fd = NULL;
433 else
435 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
436 FILE_SYNCHRONOUS_IO_NONALERT )))
438 release_object( screen_buffer );
439 return NULL;
441 allow_fd_caching(screen_buffer->fd);
444 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
445 sizeof(*screen_buffer->data) )))
447 release_object( screen_buffer );
448 return NULL;
450 /* clear the first row */
451 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
452 /* and copy it to all other rows */
453 for (i = 1; i < screen_buffer->height; i++)
454 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
455 screen_buffer->width * sizeof(char_info_t) );
457 if (!console_input->active)
459 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
460 generate_sb_initial_events( console_input );
462 return screen_buffer;
465 /* free the console for this process */
466 int free_console( struct process *process )
468 struct console_input* console = process->console;
470 if (!console) return 0;
472 process->console = NULL;
473 if (--console->num_proc == 0 && console->renderer)
475 /* all processes have terminated... tell the renderer to terminate too */
476 struct console_renderer_event evt;
477 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
478 memset(&evt.u, 0, sizeof(evt.u));
479 console_input_events_append( console, &evt );
481 release_object( console );
483 return 1;
486 /* let process inherit the console from parent... this handle two cases :
487 * 1/ generic console inheritance
488 * 2/ parent is a renderer which launches process, and process should attach to the console
489 * renderered by parent
491 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
493 int done = 0;
494 struct process* parent = parent_thread->process;
496 /* if parent is a renderer, then attach current process to its console
497 * a bit hacky....
499 if (hconin)
501 struct console_input* console;
503 /* FIXME: should we check some access rights ? */
504 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
505 0, &console_input_ops )))
507 if (console->renderer == parent_thread)
509 process->console = (struct console_input*)grab_object( console );
510 process->console->num_proc++;
511 done = 1;
513 release_object( console );
515 else clear_error(); /* ignore error */
517 /* otherwise, if parent has a console, attach child to this console */
518 if (!done && parent->console)
520 process->console = (struct console_input*)grab_object( parent->console );
521 process->console->num_proc++;
525 struct thread *console_get_renderer( struct console_input *console )
527 return console->renderer;
530 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
532 struct console_input* console = NULL;
534 if (handle)
535 console = (struct console_input *)get_handle_obj( current->process, handle,
536 access, &console_input_ops );
537 else if (current->process->console)
539 console = (struct console_input *)grab_object( current->process->console );
542 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
543 return console;
546 struct console_signal_info
548 struct console_input *console;
549 process_id_t group;
550 int signal;
553 static int propagate_console_signal_cb(struct process *process, void *user)
555 struct console_signal_info* csi = (struct console_signal_info*)user;
557 if (process->console == csi->console && process->running_threads &&
558 (!csi->group || process->group_id == csi->group))
560 /* find a suitable thread to signal */
561 struct thread *thread;
562 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
564 if (send_thread_signal( thread, csi->signal )) break;
567 return FALSE;
570 static void propagate_console_signal( struct console_input *console,
571 int sig, process_id_t group_id )
573 struct console_signal_info csi;
575 if (!console)
577 set_error( STATUS_INVALID_PARAMETER );
578 return;
580 /* FIXME: should support the other events (like CTRL_BREAK) */
581 if (sig != CTRL_C_EVENT)
583 set_error( STATUS_NOT_IMPLEMENTED );
584 return;
586 csi.console = console;
587 csi.signal = SIGINT;
588 csi.group = group_id;
590 enum_processes(propagate_console_signal_cb, &csi);
593 static int get_console_mode( obj_handle_t handle )
595 struct object *obj;
596 int ret = 0;
598 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
600 if (obj->ops == &console_input_ops)
602 ret = ((struct console_input *)obj)->mode;
604 else if (obj->ops == &screen_buffer_ops)
606 ret = ((struct screen_buffer *)obj)->mode;
608 else
609 set_error( STATUS_OBJECT_TYPE_MISMATCH );
610 release_object( obj );
612 return ret;
615 /* changes the mode of either a console input or a screen buffer */
616 static int set_console_mode( obj_handle_t handle, int mode )
618 struct object *obj;
619 int ret = 0;
621 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
622 return 0;
623 if (obj->ops == &console_input_ops)
625 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
626 ((struct console_input *)obj)->mode = mode;
627 ret = 1;
629 else if (obj->ops == &screen_buffer_ops)
631 ((struct screen_buffer *)obj)->mode = mode;
632 ret = 1;
634 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
635 release_object( obj );
636 return ret;
639 /* add input events to a console input queue */
640 static int write_console_input( struct console_input* console, int count,
641 const INPUT_RECORD *records )
643 INPUT_RECORD *new_rec;
645 if (!count) return 0;
646 if (!(new_rec = realloc( console->records,
647 (console->recnum + count) * sizeof(INPUT_RECORD) )))
649 set_error( STATUS_NO_MEMORY );
650 return -1;
652 console->records = new_rec;
653 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
655 if (console->mode & ENABLE_PROCESSED_INPUT)
657 int i = 0;
658 while (i < count)
660 if (records[i].EventType == KEY_EVENT &&
661 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
662 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
664 if (i != count - 1)
665 memcpy( &console->records[console->recnum + i],
666 &console->records[console->recnum + i + 1],
667 (count - i - 1) * sizeof(INPUT_RECORD) );
668 count--;
669 if (records[i].Event.KeyEvent.bKeyDown)
671 /* send SIGINT to all processes attached to this console */
672 propagate_console_signal( console, CTRL_C_EVENT, 0 );
675 else i++;
678 if (!console->recnum && count) set_event( console->event );
679 console->recnum += count;
680 return count;
683 /* retrieve a pointer to the console input records */
684 static int read_console_input( obj_handle_t handle, int count, int flush )
686 struct console_input *console;
688 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
689 FILE_READ_DATA, &console_input_ops )))
690 return -1;
692 if (!count)
694 /* special case: do not retrieve anything, but return
695 * the total number of records available */
696 count = console->recnum;
698 else
700 if (count > console->recnum) count = console->recnum;
701 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
703 if (flush)
705 int i;
706 for (i = count; i < console->recnum; i++)
707 console->records[i-count] = console->records[i];
708 if ((console->recnum -= count) > 0)
710 INPUT_RECORD *new_rec = realloc( console->records,
711 console->recnum * sizeof(INPUT_RECORD) );
712 if (new_rec) console->records = new_rec;
714 else
716 free( console->records );
717 console->records = NULL;
718 reset_event( console->event );
721 release_object( console );
722 return count;
725 /* set misc console input information */
726 static int set_console_input_info( const struct set_console_input_info_request *req,
727 const WCHAR *title, data_size_t len )
729 struct console_input *console;
730 struct console_renderer_event evt;
732 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
733 if (console_input_is_bare(console) &&
734 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
735 SET_CONSOLE_INPUT_INFO_WIN)))
737 set_error( STATUS_UNSUCCESSFUL );
738 goto error;
741 memset(&evt.u, 0, sizeof(evt.u));
742 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
744 struct screen_buffer *screen_buffer;
746 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
747 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
748 if (!screen_buffer || screen_buffer->input != console)
750 set_error( STATUS_INVALID_HANDLE );
751 if (screen_buffer) release_object( screen_buffer );
752 goto error;
755 if (screen_buffer != console->active)
757 if (console->active) release_object( console->active );
758 console->active = screen_buffer;
759 generate_sb_initial_events( console );
761 else
762 release_object( screen_buffer );
764 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
766 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
767 if (new_title)
769 memcpy( new_title, title, len );
770 new_title[len / sizeof(WCHAR)] = 0;
771 free( console->title );
772 console->title = new_title;
773 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
774 console_input_events_append( console, &evt );
777 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
779 console->history_mode = req->history_mode;
781 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
782 console->history_size != req->history_size)
784 WCHAR** mem = NULL;
785 int i;
786 int delta;
788 if (req->history_size)
790 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
791 if (!mem) goto error;
792 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
795 delta = (console->history_index > req->history_size) ?
796 (console->history_index - req->history_size) : 0;
798 for (i = delta; i < console->history_index; i++)
800 mem[i - delta] = console->history[i];
801 console->history[i] = NULL;
803 console->history_index -= delta;
805 for (i = 0; i < console->history_size; i++)
806 free( console->history[i] );
807 free( console->history );
808 console->history = mem;
809 console->history_size = req->history_size;
811 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
813 console->edition_mode = req->edition_mode;
815 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
817 console->input_cp = req->input_cp;
819 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
821 console->output_cp = req->output_cp;
823 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
825 console->win = req->win;
827 release_object( console );
828 return 1;
829 error:
830 if (console) release_object( console );
831 return 0;
834 /* resize a screen buffer */
835 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
836 int new_width, int new_height )
838 int i, old_width, old_height, copy_width, copy_height;
839 char_info_t *new_data;
841 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
843 set_error( STATUS_NO_MEMORY );
844 return 0;
846 old_width = screen_buffer->width;
847 old_height = screen_buffer->height;
848 copy_width = min( old_width, new_width );
849 copy_height = min( old_height, new_height );
851 /* copy all the rows */
852 for (i = 0; i < copy_height; i++)
854 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
855 copy_width * sizeof(char_info_t) );
858 /* clear the end of each row */
859 if (new_width > old_width)
861 /* fill first row */
862 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
863 /* and blast it to the other rows */
864 for (i = 1; i < copy_height; i++)
865 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
866 (new_width - old_width) * sizeof(char_info_t) );
869 /* clear remaining rows */
870 if (new_height > old_height)
872 /* fill first row */
873 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
874 /* and blast it to the other rows */
875 for (i = old_height+1; i < new_height; i++)
876 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
877 new_width * sizeof(char_info_t) );
879 free( screen_buffer->data );
880 screen_buffer->data = new_data;
881 screen_buffer->width = new_width;
882 screen_buffer->height = new_height;
883 return 1;
886 /* set misc screen buffer information */
887 static int set_console_output_info( struct screen_buffer *screen_buffer,
888 const struct set_console_output_info_request *req )
890 struct console_renderer_event evt;
892 memset(&evt.u, 0, sizeof(evt.u));
893 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
895 if (req->cursor_size < 1 || req->cursor_size > 100)
897 set_error( STATUS_INVALID_PARAMETER );
898 return 0;
900 if (screen_buffer->cursor_size != req->cursor_size ||
901 screen_buffer->cursor_visible != req->cursor_visible)
903 screen_buffer->cursor_size = req->cursor_size;
904 screen_buffer->cursor_visible = req->cursor_visible;
905 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
906 evt.u.cursor_geom.size = req->cursor_size;
907 evt.u.cursor_geom.visible = req->cursor_visible;
908 console_input_events_append( screen_buffer->input, &evt );
911 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
913 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
914 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
916 set_error( STATUS_INVALID_PARAMETER );
917 return 0;
919 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
921 screen_buffer->cursor_x = req->cursor_x;
922 screen_buffer->cursor_y = req->cursor_y;
923 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
924 evt.u.cursor_pos.x = req->cursor_x;
925 evt.u.cursor_pos.y = req->cursor_y;
926 console_input_events_append( screen_buffer->input, &evt );
929 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
931 unsigned cc;
933 /* new screen-buffer cannot be smaller than actual window */
934 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
935 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
937 set_error( STATUS_INVALID_PARAMETER );
938 return 0;
940 /* FIXME: there are also some basic minimum and max size to deal with */
941 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
943 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
944 evt.u.resize.width = req->width;
945 evt.u.resize.height = req->height;
946 console_input_events_append( screen_buffer->input, &evt );
948 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
949 evt.u.update.top = 0;
950 evt.u.update.bottom = screen_buffer->height - 1;
951 console_input_events_append( screen_buffer->input, &evt );
953 /* scroll window to display sb */
954 if (screen_buffer->win.right >= req->width)
956 screen_buffer->win.right -= screen_buffer->win.left;
957 screen_buffer->win.left = 0;
959 if (screen_buffer->win.bottom >= req->height)
961 screen_buffer->win.bottom -= screen_buffer->win.top;
962 screen_buffer->win.top = 0;
964 /* reset cursor if needed (normally, if cursor was outside of new sb, the
965 * window has been shifted so that the new position of the cursor will be
966 * visible */
967 cc = 0;
968 if (screen_buffer->cursor_x >= req->width)
970 screen_buffer->cursor_x = req->width - 1;
971 cc++;
973 if (screen_buffer->cursor_y >= req->height)
975 screen_buffer->cursor_y = req->height - 1;
976 cc++;
978 if (cc)
980 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
981 evt.u.cursor_pos.x = req->cursor_x;
982 evt.u.cursor_pos.y = req->cursor_y;
983 console_input_events_append( screen_buffer->input, &evt );
986 if (screen_buffer == screen_buffer->input->active &&
987 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
989 INPUT_RECORD ir;
990 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
991 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
992 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
993 write_console_input( screen_buffer->input, 1, &ir );
996 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
998 screen_buffer->attr = req->attr;
1000 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1002 if (req->win_left < 0 || req->win_left > req->win_right ||
1003 req->win_right >= screen_buffer->width ||
1004 req->win_top < 0 || req->win_top > req->win_bottom ||
1005 req->win_bottom >= screen_buffer->height)
1007 set_error( STATUS_INVALID_PARAMETER );
1008 return 0;
1010 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1011 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1013 screen_buffer->win.left = req->win_left;
1014 screen_buffer->win.top = req->win_top;
1015 screen_buffer->win.right = req->win_right;
1016 screen_buffer->win.bottom = req->win_bottom;
1017 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1018 evt.u.display.left = req->win_left;
1019 evt.u.display.top = req->win_top;
1020 evt.u.display.width = req->win_right - req->win_left + 1;
1021 evt.u.display.height = req->win_bottom - req->win_top + 1;
1022 console_input_events_append( screen_buffer->input, &evt );
1025 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1027 screen_buffer->max_width = req->max_width;
1028 screen_buffer->max_height = req->max_height;
1030 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1032 screen_buffer->font.width = req->font_width;
1033 screen_buffer->font.height = req->font_height;
1036 return 1;
1039 /* appends a new line to history (history is a fixed size array) */
1040 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1042 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1044 if (!ptr)
1045 return;
1047 if (!console || !console->history_size)
1049 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1050 free( ptr );
1051 return;
1054 memcpy( ptr, buf, len * sizeof(WCHAR) );
1055 ptr[len] = 0;
1057 if (console->history_mode && console->history_index &&
1058 strncmpW( console->history[console->history_index - 1], ptr, len ) == 0)
1060 /* ok, mode ask us to not use twice the same string...
1061 * so just free mem and returns
1063 set_error( STATUS_ALIAS_EXISTS );
1064 free(ptr);
1065 return;
1068 if (console->history_index < console->history_size)
1070 console->history[console->history_index++] = ptr;
1072 else
1074 free( console->history[0]) ;
1075 memmove( &console->history[0], &console->history[1],
1076 (console->history_size - 1) * sizeof(WCHAR*) );
1077 console->history[console->history_size - 1] = ptr;
1081 /* returns a line from the cache */
1082 static data_size_t console_input_get_hist( struct console_input *console, int index )
1084 data_size_t ret = 0;
1086 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1087 else
1089 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1090 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1092 return ret;
1095 /* dumb dump */
1096 static void console_input_dump( struct object *obj, int verbose )
1098 struct console_input *console = (struct console_input *)obj;
1099 assert( obj->ops == &console_input_ops );
1100 fprintf( stderr, "Console input active=%p evt=%p\n",
1101 console->active, console->evt );
1104 static void console_input_destroy( struct object *obj )
1106 struct console_input* console_in = (struct console_input *)obj;
1107 struct screen_buffer* curr;
1108 int i;
1110 assert( obj->ops == &console_input_ops );
1111 free( console_in->title );
1112 free( console_in->records );
1114 if (console_in->active) release_object( console_in->active );
1115 console_in->active = NULL;
1117 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1119 if (curr->input == console_in) curr->input = NULL;
1122 if (console_in->evt)
1124 release_object( console_in->evt );
1125 console_in->evt = NULL;
1127 if (console_in->event)
1128 release_object( console_in->event );
1129 if (console_in->fd)
1130 release_object( console_in->fd );
1132 for (i = 0; i < console_in->history_size; i++)
1133 free( console_in->history[i] );
1134 free( console_in->history );
1137 static void screen_buffer_dump( struct object *obj, int verbose )
1139 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1140 assert( obj->ops == &screen_buffer_ops );
1142 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1145 static void screen_buffer_destroy( struct object *obj )
1147 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1149 assert( obj->ops == &screen_buffer_ops );
1151 list_remove( &screen_buffer->entry );
1153 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1155 struct screen_buffer *sb;
1157 screen_buffer->input->active = NULL;
1158 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1160 if (sb->input == screen_buffer->input)
1162 sb->input->active = sb;
1163 break;
1167 if (screen_buffer->fd) release_object( screen_buffer->fd );
1168 free( screen_buffer->data );
1171 static struct fd *screen_buffer_get_fd( struct object *obj )
1173 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1174 assert( obj->ops == &screen_buffer_ops );
1175 if (screen_buffer->fd)
1176 return (struct fd*)grab_object( screen_buffer->fd );
1177 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1178 return NULL;
1181 /* write data into a screen buffer */
1182 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1183 const void* data, enum char_info_mode mode,
1184 int x, int y, int wrap )
1186 unsigned int i;
1187 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1189 if (y >= screen_buffer->height) return 0;
1191 if (wrap)
1192 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1193 else
1194 end = screen_buffer->data + (y+1) * screen_buffer->width;
1196 switch(mode)
1198 case CHAR_INFO_MODE_TEXT:
1200 const WCHAR *ptr = data;
1201 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1203 break;
1204 case CHAR_INFO_MODE_ATTR:
1206 const unsigned short *ptr = data;
1207 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1209 break;
1210 case CHAR_INFO_MODE_TEXTATTR:
1212 const char_info_t *ptr = data;
1213 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1215 break;
1216 case CHAR_INFO_MODE_TEXTSTDATTR:
1218 const WCHAR *ptr = data;
1219 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1221 dest->ch = ptr[i];
1222 dest->attr = screen_buffer->attr;
1225 break;
1226 default:
1227 set_error( STATUS_INVALID_PARAMETER );
1228 return 0;
1231 if (i && screen_buffer == screen_buffer->input->active)
1233 struct console_renderer_event evt;
1234 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1235 memset(&evt.u, 0, sizeof(evt.u));
1236 evt.u.update.top = y + x / screen_buffer->width;
1237 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1238 console_input_events_append( screen_buffer->input, &evt );
1240 return i;
1243 /* fill a screen buffer with uniform data */
1244 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1245 enum char_info_mode mode, int x, int y, int count, int wrap )
1247 int i;
1248 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1250 if (y >= screen_buffer->height) return 0;
1252 if (wrap)
1253 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1254 else
1255 end = screen_buffer->data + (y+1) * screen_buffer->width;
1257 if (count > end - dest) count = end - dest;
1259 switch(mode)
1261 case CHAR_INFO_MODE_TEXT:
1262 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1263 break;
1264 case CHAR_INFO_MODE_ATTR:
1265 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1266 break;
1267 case CHAR_INFO_MODE_TEXTATTR:
1268 for (i = 0; i < count; i++) dest[i] = data;
1269 break;
1270 case CHAR_INFO_MODE_TEXTSTDATTR:
1271 for (i = 0; i < count; i++)
1273 dest[i].ch = data.ch;
1274 dest[i].attr = screen_buffer->attr;
1276 break;
1277 default:
1278 set_error( STATUS_INVALID_PARAMETER );
1279 return 0;
1282 if (count && screen_buffer == screen_buffer->input->active)
1284 struct console_renderer_event evt;
1285 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1286 memset(&evt.u, 0, sizeof(evt.u));
1287 evt.u.update.top = y;
1288 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1289 console_input_events_append( screen_buffer->input, &evt );
1291 return i;
1294 /* read data from a screen buffer */
1295 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1296 enum char_info_mode mode, int wrap )
1298 int i;
1299 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1301 if (y >= screen_buffer->height) return;
1303 if (wrap)
1304 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1305 else
1306 end = screen_buffer->data + (y+1) * screen_buffer->width;
1308 switch(mode)
1310 case CHAR_INFO_MODE_TEXT:
1312 WCHAR *data;
1313 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1314 if ((data = set_reply_data_size( count * sizeof(*data) )))
1316 for (i = 0; i < count; i++) data[i] = src[i].ch;
1319 break;
1320 case CHAR_INFO_MODE_ATTR:
1322 unsigned short *data;
1323 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1324 if ((data = set_reply_data_size( count * sizeof(*data) )))
1326 for (i = 0; i < count; i++) data[i] = src[i].attr;
1329 break;
1330 case CHAR_INFO_MODE_TEXTATTR:
1332 char_info_t *data;
1333 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1334 if ((data = set_reply_data_size( count * sizeof(*data) )))
1336 for (i = 0; i < count; i++) data[i] = src[i];
1339 break;
1340 default:
1341 set_error( STATUS_INVALID_PARAMETER );
1342 break;
1346 /* scroll parts of a screen buffer */
1347 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1348 int w, int h )
1350 int j;
1351 char_info_t *psrc, *pdst;
1352 struct console_renderer_event evt;
1354 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1355 xsrc + w > screen_buffer->width ||
1356 xdst + w > screen_buffer->width ||
1357 ysrc + h > screen_buffer->height ||
1358 ydst + h > screen_buffer->height ||
1359 w == 0 || h == 0)
1361 set_error( STATUS_INVALID_PARAMETER );
1362 return;
1365 if (ysrc < ydst)
1367 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1368 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1370 for (j = h; j > 0; j--)
1372 memcpy(pdst, psrc, w * sizeof(*pdst) );
1373 pdst -= screen_buffer->width;
1374 psrc -= screen_buffer->width;
1377 else
1379 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1380 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1382 for (j = 0; j < h; j++)
1384 /* we use memmove here because when psrc and pdst are the same,
1385 * copies are done on the same row, so the dst and src blocks
1386 * can overlap */
1387 memmove( pdst, psrc, w * sizeof(*pdst) );
1388 pdst += screen_buffer->width;
1389 psrc += screen_buffer->width;
1393 /* FIXME: this could be enhanced, by signalling scroll */
1394 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1395 memset(&evt.u, 0, sizeof(evt.u));
1396 evt.u.update.top = min(ysrc, ydst);
1397 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1398 console_input_events_append( screen_buffer->input, &evt );
1401 /* allocate a console for the renderer */
1402 DECL_HANDLER(alloc_console)
1404 obj_handle_t in = 0;
1405 obj_handle_t evt = 0;
1406 struct process *process;
1407 struct thread *renderer;
1408 struct console_input *console;
1409 int fd;
1410 int attach = 0;
1412 if (req->input_fd != -1)
1414 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1416 set_error( STATUS_INVALID_PARAMETER );
1417 return;
1420 else fd = -1;
1422 switch (req->pid)
1424 case 0:
1425 /* renderer is current, console to be attached to parent process */
1426 renderer = current;
1427 if (!(process = current->process->parent))
1429 if (fd != -1) close( fd );
1430 set_error( STATUS_ACCESS_DENIED );
1431 return;
1433 grab_object( process );
1434 attach = 1;
1435 break;
1436 case 0xffffffff:
1437 /* no renderer, console to be attached to current process */
1438 renderer = NULL;
1439 process = current->process;
1440 grab_object( process );
1441 attach = 1;
1442 break;
1443 default:
1444 /* renderer is current, console to be attached to req->pid */
1445 renderer = current;
1446 if (!(process = get_process_from_id( req->pid )))
1448 if (fd != -1) close( fd );
1449 return;
1453 if (attach && process->console)
1455 if (fd != -1) close( fd );
1456 set_error( STATUS_ACCESS_DENIED );
1457 goto the_end;
1460 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1462 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1464 if (!console->evt ||
1465 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1467 if (attach)
1469 process->console = (struct console_input*)grab_object( console );
1470 console->num_proc++;
1472 reply->handle_in = in;
1473 reply->event = evt;
1474 release_object( console );
1475 goto the_end;
1477 close_handle( current->process, in );
1479 release_object( console );
1481 the_end:
1482 release_object( process );
1485 /* free the console of the current process */
1486 DECL_HANDLER(free_console)
1488 free_console( current->process );
1491 /* let the renderer peek the events it's waiting on */
1492 DECL_HANDLER(get_console_renderer_events)
1494 struct console_input_events *evt;
1496 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1497 FILE_READ_PROPERTIES, &console_input_events_ops );
1498 if (!evt) return;
1499 console_input_events_get( evt );
1500 release_object( evt );
1503 /* open a handle to the process console */
1504 DECL_HANDLER(open_console)
1506 struct object *obj = NULL;
1508 reply->handle = 0;
1509 if (!req->from)
1511 if (current->process->console)
1512 obj = grab_object( (struct object*)current->process->console );
1514 else if (req->from == (obj_handle_t)1)
1516 if (current->process->console && current->process->console->active)
1517 obj = grab_object( (struct object*)current->process->console->active );
1519 else if ((obj = get_handle_obj( current->process, req->from,
1520 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1522 struct console_input *console = (struct console_input *)obj;
1523 obj = (console->active) ? grab_object( console->active ) : NULL;
1524 release_object( console );
1527 /* FIXME: req->share is not used (as in screen buffer creation) */
1528 if (obj)
1530 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1531 release_object( obj );
1533 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1536 /* set info about a console input */
1537 DECL_HANDLER(set_console_input_info)
1539 set_console_input_info( req, get_req_data(), get_req_data_size() );
1542 /* get info about a console (output only) */
1543 DECL_HANDLER(get_console_input_info)
1545 struct console_input *console;
1547 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1548 if (console->title)
1550 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1551 if (len > get_reply_max_size()) len = get_reply_max_size();
1552 set_reply_data( console->title, len );
1554 reply->history_mode = console->history_mode;
1555 reply->history_size = console->history_size;
1556 reply->history_index = console->history_index;
1557 reply->edition_mode = console->edition_mode;
1558 reply->input_cp = console->input_cp;
1559 reply->output_cp = console->output_cp;
1560 reply->win = console->win;
1562 release_object( console );
1565 /* get a console mode (input or output) */
1566 DECL_HANDLER(get_console_mode)
1568 reply->mode = get_console_mode( req->handle );
1571 /* set a console mode (input or output) */
1572 DECL_HANDLER(set_console_mode)
1574 set_console_mode( req->handle, req->mode );
1577 /* add input records to a console input queue */
1578 DECL_HANDLER(write_console_input)
1580 struct console_input *console;
1582 reply->written = 0;
1583 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1584 FILE_WRITE_PROPERTIES, &console_input_ops )))
1585 return;
1586 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1587 get_req_data() );
1588 release_object( console );
1591 /* fetch input records from a console input queue */
1592 DECL_HANDLER(read_console_input)
1594 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1595 reply->read = read_console_input( req->handle, count, req->flush );
1598 /* appends a string to console's history */
1599 DECL_HANDLER(append_console_input_history)
1601 struct console_input *console;
1603 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1604 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1605 release_object( console );
1608 /* appends a string to console's history */
1609 DECL_HANDLER(get_console_input_history)
1611 struct console_input *console;
1613 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1614 reply->total = console_input_get_hist( console, req->index );
1615 release_object( console );
1618 /* creates a screen buffer */
1619 DECL_HANDLER(create_console_output)
1621 struct console_input* console;
1622 struct screen_buffer* screen_buffer;
1623 int fd;
1625 if (req->fd != -1)
1627 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1629 set_error( STATUS_INVALID_HANDLE );
1630 return;
1633 else fd = -1;
1634 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1636 if (fd != -1) close( fd );
1637 return;
1639 if (console_input_is_bare( console ) ^ (fd != -1))
1641 if (fd != -1) close( fd );
1642 release_object( console );
1643 set_error( STATUS_INVALID_HANDLE );
1644 return;
1647 screen_buffer = create_console_output( console, fd );
1648 if (screen_buffer)
1650 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1651 * see file.c on how this could be done */
1652 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1653 release_object( screen_buffer );
1655 release_object( console );
1658 /* set info about a console screen buffer */
1659 DECL_HANDLER(set_console_output_info)
1661 struct screen_buffer *screen_buffer;
1663 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1664 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1666 set_console_output_info( screen_buffer, req );
1667 release_object( screen_buffer );
1671 /* get info about a console screen buffer */
1672 DECL_HANDLER(get_console_output_info)
1674 struct screen_buffer *screen_buffer;
1676 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1677 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1679 reply->cursor_size = screen_buffer->cursor_size;
1680 reply->cursor_visible = screen_buffer->cursor_visible;
1681 reply->cursor_x = screen_buffer->cursor_x;
1682 reply->cursor_y = screen_buffer->cursor_y;
1683 reply->width = screen_buffer->width;
1684 reply->height = screen_buffer->height;
1685 reply->attr = screen_buffer->attr;
1686 reply->win_left = screen_buffer->win.left;
1687 reply->win_top = screen_buffer->win.top;
1688 reply->win_right = screen_buffer->win.right;
1689 reply->win_bottom = screen_buffer->win.bottom;
1690 reply->max_width = screen_buffer->max_width;
1691 reply->max_height = screen_buffer->max_height;
1692 reply->font_width = screen_buffer->font.width;
1693 reply->font_height = screen_buffer->font.height;
1694 release_object( screen_buffer );
1698 /* read data (chars & attrs) from a screen buffer */
1699 DECL_HANDLER(read_console_output)
1701 struct screen_buffer *screen_buffer;
1703 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1704 FILE_READ_DATA, &screen_buffer_ops )))
1706 if (console_input_is_bare( screen_buffer->input ))
1708 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1709 release_object( screen_buffer );
1710 return;
1712 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1713 reply->width = screen_buffer->width;
1714 reply->height = screen_buffer->height;
1715 release_object( screen_buffer );
1719 /* write data (char and/or attrs) to a screen buffer */
1720 DECL_HANDLER(write_console_output)
1722 struct screen_buffer *screen_buffer;
1724 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1725 FILE_WRITE_DATA, &screen_buffer_ops)))
1727 if (console_input_is_bare( screen_buffer->input ))
1729 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1730 release_object( screen_buffer );
1731 return;
1733 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1734 req->mode, req->x, req->y, req->wrap );
1735 reply->width = screen_buffer->width;
1736 reply->height = screen_buffer->height;
1737 release_object( screen_buffer );
1741 /* fill a screen buffer with constant data (chars and/or attributes) */
1742 DECL_HANDLER(fill_console_output)
1744 struct screen_buffer *screen_buffer;
1746 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1747 FILE_WRITE_DATA, &screen_buffer_ops)))
1749 if (console_input_is_bare( screen_buffer->input ))
1751 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1752 release_object( screen_buffer );
1753 return;
1755 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1756 req->x, req->y, req->count, req->wrap );
1757 release_object( screen_buffer );
1761 /* move a rect of data in a screen buffer */
1762 DECL_HANDLER(move_console_output)
1764 struct screen_buffer *screen_buffer;
1766 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1767 FILE_WRITE_DATA, &screen_buffer_ops)))
1769 if (console_input_is_bare( screen_buffer->input ))
1771 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1772 release_object( screen_buffer );
1773 return;
1775 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1776 req->w, req->h );
1777 release_object( screen_buffer );
1781 /* sends a signal to a console (process, group...) */
1782 DECL_HANDLER(send_console_signal)
1784 process_id_t group;
1786 group = req->group_id ? req->group_id : current->process->group_id;
1788 if (!group)
1789 set_error( STATUS_INVALID_PARAMETER );
1790 else
1791 propagate_console_signal( current->process->console, req->signal, group );
1794 /* get console which renderer is 'current' */
1795 static int cgwe_enum( struct process* process, void* user)
1797 if (process->console && process->console->renderer == current)
1799 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1800 return 1;
1802 return 0;
1805 DECL_HANDLER(get_console_wait_event)
1807 struct console_input* console = NULL;
1809 if (current->process->console)
1810 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1811 else enum_processes(cgwe_enum, &console);
1813 if (console)
1815 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1816 release_object( console );
1818 else set_error( STATUS_INVALID_PARAMETER );