winegstreamer: Add better support for unsupported audio/video.
[wine/multimedia.git] / server / console.c
blob42d985deaf5a30fe000eef8541bf5cd18d5d0e0b
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_open_file, /* open_file */
88 no_close_handle, /* close_handle */
89 console_input_destroy /* destroy */
92 static void console_input_events_dump( struct object *obj, int verbose );
93 static void console_input_events_destroy( struct object *obj );
94 static int console_input_events_signaled( struct object *obj, struct thread *thread );
96 struct console_input_events
98 struct object obj; /* object header */
99 int num_alloc; /* number of allocated events */
100 int num_used; /* number of actually used events */
101 struct console_renderer_event* events;
104 static const struct object_ops console_input_events_ops =
106 sizeof(struct console_input_events), /* size */
107 console_input_events_dump, /* dump */
108 no_get_type, /* get_type */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 console_input_events_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 default_fd_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 no_close_handle, /* close_handle */
121 console_input_events_destroy /* destroy */
124 struct screen_buffer
126 struct object obj; /* object header */
127 struct list entry; /* entry in list of all screen buffers */
128 struct console_input *input; /* associated console input */
129 int mode; /* output mode */
130 int cursor_size; /* size of cursor (percentage filled) */
131 int cursor_visible;/* cursor visibility flag */
132 int cursor_x; /* position of cursor */
133 int cursor_y; /* position of cursor */
134 int width; /* size (w-h) of the screen buffer */
135 int height;
136 int max_width; /* size (w-h) of the window given font size */
137 int max_height;
138 char_info_t *data; /* the data for each cell - a width x height matrix */
139 unsigned short attr; /* default attribute for screen buffer */
140 rectangle_t win; /* current visible window on the screen buffer *
141 * as seen in wineconsole */
142 struct fd *fd; /* for bare console, attached output fd */
145 static void screen_buffer_dump( struct object *obj, int verbose );
146 static void screen_buffer_destroy( struct object *obj );
147 static struct fd *screen_buffer_get_fd( struct object *obj );
149 static const struct object_ops screen_buffer_ops =
151 sizeof(struct screen_buffer), /* size */
152 screen_buffer_dump, /* dump */
153 no_get_type, /* get_type */
154 no_add_queue, /* add_queue */
155 NULL, /* remove_queue */
156 NULL, /* signaled */
157 NULL, /* satisfied */
158 no_signal, /* signal */
159 screen_buffer_get_fd, /* get_fd */
160 default_fd_map_access, /* map_access */
161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
163 no_lookup_name, /* lookup_name */
164 no_open_file, /* open_file */
165 no_close_handle, /* close_handle */
166 screen_buffer_destroy /* destroy */
169 static enum server_fd_type console_get_fd_type( struct fd *fd );
171 static const struct fd_ops console_fd_ops =
173 default_fd_get_poll_events, /* get_poll_events */
174 default_poll_event, /* poll_event */
175 no_flush, /* flush */
176 console_get_fd_type, /* get_fd_type */
177 default_fd_ioctl, /* ioctl */
178 default_fd_queue_async, /* queue_async */
179 default_fd_reselect_async, /* reselect_async */
180 default_fd_cancel_async /* cancel_async */
183 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
185 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
187 static int console_input_is_bare( struct console_input* cin )
189 return cin->evt == NULL;
192 static struct fd *console_input_get_fd( struct object* obj )
194 struct console_input *console_input = (struct console_input*)obj;
195 assert( obj->ops == &console_input_ops );
196 if (console_input->fd)
197 return (struct fd*)grab_object( console_input->fd );
198 set_error( STATUS_OBJECT_TYPE_MISMATCH );
199 return NULL;
202 static enum server_fd_type console_get_fd_type( struct fd *fd )
204 return FD_TYPE_CHAR;
207 /* dumps the renderer events of a console */
208 static void console_input_events_dump( struct object *obj, int verbose )
210 struct console_input_events *evts = (struct console_input_events *)obj;
211 assert( obj->ops == &console_input_events_ops );
212 fprintf( stderr, "Console input events: %d/%d events\n",
213 evts->num_used, evts->num_alloc );
216 /* destroys the renderer events of a console */
217 static void console_input_events_destroy( struct object *obj )
219 struct console_input_events *evts = (struct console_input_events *)obj;
220 assert( obj->ops == &console_input_events_ops );
221 free( evts->events );
224 /* the renderer events list is signaled when it's not empty */
225 static int console_input_events_signaled( struct object *obj, struct thread *thread )
227 struct console_input_events *evts = (struct console_input_events *)obj;
228 assert( obj->ops == &console_input_events_ops );
229 return (evts->num_used != 0);
232 /* add an event to the console's renderer events list */
233 static void console_input_events_append( struct console_input* console,
234 struct console_renderer_event* evt)
236 struct console_input_events* evts;
237 int collapsed = FALSE;
239 if (!(evts = console->evt)) return;
240 /* to be done even when evt has been generated by the renderer ? */
242 /* try to collapse evt into current queue's events */
243 if (evts->num_used)
245 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
247 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
248 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
250 /* if two update events overlap, collapse them into a single one */
251 if (last->u.update.bottom + 1 >= evt->u.update.top &&
252 evt->u.update.bottom + 1 >= last->u.update.top)
254 last->u.update.top = min(last->u.update.top, evt->u.update.top);
255 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
256 collapsed = TRUE;
260 if (!collapsed)
262 if (evts->num_used == evts->num_alloc)
264 evts->num_alloc += 16;
265 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
266 assert(evts->events);
268 evts->events[evts->num_used++] = *evt;
270 wake_up( &evts->obj, 0 );
273 /* retrieves events from the console's renderer events list */
274 static void console_input_events_get( struct console_input_events* evts )
276 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
278 if (num > evts->num_used) num = evts->num_used;
279 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
280 if (num < evts->num_used)
282 memmove( &evts->events[0], &evts->events[num],
283 (evts->num_used - num) * sizeof(evts->events[0]) );
285 evts->num_used -= num;
288 static struct console_input_events *create_console_input_events(void)
290 struct console_input_events* evt;
292 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
293 evt->num_alloc = evt->num_used = 0;
294 evt->events = NULL;
295 return evt;
298 static struct object *create_console_input( struct thread* renderer, int fd )
300 struct console_input *console_input;
302 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
303 console_input->renderer = renderer;
304 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
305 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
306 console_input->num_proc = 0;
307 console_input->active = NULL;
308 console_input->recnum = 0;
309 console_input->records = NULL;
310 console_input->evt = renderer ? create_console_input_events() : NULL;
311 console_input->title = NULL;
312 console_input->history_size = 50;
313 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
314 console_input->history_index = 0;
315 console_input->history_mode = 0;
316 console_input->edition_mode = 0;
317 console_input->input_cp = 0;
318 console_input->output_cp = 0;
319 console_input->win = 0;
320 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
321 console_input->fd = NULL;
323 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
325 release_object( console_input );
326 return NULL;
328 if (fd != -1) /* bare console */
330 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
331 FILE_SYNCHRONOUS_IO_NONALERT )))
333 release_object( console_input );
334 return NULL;
336 allow_fd_caching( console_input->fd );
339 return &console_input->obj;
342 static void generate_sb_initial_events( struct console_input *console_input )
344 struct screen_buffer *screen_buffer = console_input->active;
345 struct console_renderer_event evt;
347 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
348 memset(&evt.u, 0, sizeof(evt.u));
349 console_input_events_append( console_input, &evt );
351 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
352 evt.u.resize.width = screen_buffer->width;
353 evt.u.resize.height = screen_buffer->height;
354 console_input_events_append( console_input, &evt );
356 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
357 evt.u.display.left = screen_buffer->win.left;
358 evt.u.display.top = screen_buffer->win.top;
359 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
360 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
361 console_input_events_append( console_input, &evt );
363 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
364 evt.u.update.top = 0;
365 evt.u.update.bottom = screen_buffer->height - 1;
366 console_input_events_append( console_input, &evt );
368 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
369 evt.u.cursor_geom.size = screen_buffer->cursor_size;
370 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
371 console_input_events_append( console_input, &evt );
373 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
374 evt.u.cursor_pos.x = screen_buffer->cursor_x;
375 evt.u.cursor_pos.y = screen_buffer->cursor_y;
376 console_input_events_append( console_input, &evt );
379 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
381 struct screen_buffer *screen_buffer;
382 int i;
384 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
386 if (fd != -1) close( fd );
387 return NULL;
389 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
390 screen_buffer->input = console_input;
391 screen_buffer->cursor_size = 100;
392 screen_buffer->cursor_visible = 1;
393 screen_buffer->width = 80;
394 screen_buffer->height = 150;
395 screen_buffer->max_width = 80;
396 screen_buffer->max_height = 25;
397 screen_buffer->cursor_x = 0;
398 screen_buffer->cursor_y = 0;
399 screen_buffer->attr = 0x0F;
400 screen_buffer->win.left = 0;
401 screen_buffer->win.right = screen_buffer->max_width - 1;
402 screen_buffer->win.top = 0;
403 screen_buffer->win.bottom = screen_buffer->max_height - 1;
404 if (fd == -1)
405 screen_buffer->fd = NULL;
406 else
408 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
409 FILE_SYNCHRONOUS_IO_NONALERT )))
411 release_object( screen_buffer );
412 return NULL;
414 allow_fd_caching(screen_buffer->fd);
417 list_add_head( &screen_buffer_list, &screen_buffer->entry );
419 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
420 sizeof(*screen_buffer->data) )))
422 release_object( screen_buffer );
423 return NULL;
425 /* clear the first row */
426 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
427 /* and copy it to all other rows */
428 for (i = 1; i < screen_buffer->height; i++)
429 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
430 screen_buffer->width * sizeof(char_info_t) );
432 if (!console_input->active)
434 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
435 generate_sb_initial_events( console_input );
437 return screen_buffer;
440 /* free the console for this process */
441 int free_console( struct process *process )
443 struct console_input* console = process->console;
445 if (!console) return 0;
447 process->console = NULL;
448 if (--console->num_proc == 0 && console->renderer)
450 /* all processes have terminated... tell the renderer to terminate too */
451 struct console_renderer_event evt;
452 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
453 memset(&evt.u, 0, sizeof(evt.u));
454 console_input_events_append( console, &evt );
456 release_object( console );
458 return 1;
461 /* let process inherit the console from parent... this handle two cases :
462 * 1/ generic console inheritance
463 * 2/ parent is a renderer which launches process, and process should attach to the console
464 * renderered by parent
466 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
468 int done = 0;
469 struct process* parent = parent_thread->process;
471 /* if parent is a renderer, then attach current process to its console
472 * a bit hacky....
474 if (hconin)
476 struct console_input* console;
478 /* FIXME: should we check some access rights ? */
479 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
480 0, &console_input_ops )))
482 if (console->renderer == parent_thread)
484 process->console = (struct console_input*)grab_object( console );
485 process->console->num_proc++;
486 done = 1;
488 release_object( console );
490 else clear_error(); /* ignore error */
492 /* otherwise, if parent has a console, attach child to this console */
493 if (!done && parent->console)
495 process->console = (struct console_input*)grab_object( parent->console );
496 process->console->num_proc++;
500 struct thread *console_get_renderer( struct console_input *console )
502 return console->renderer;
505 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
507 struct console_input* console = NULL;
509 if (handle)
510 console = (struct console_input *)get_handle_obj( current->process, handle,
511 access, &console_input_ops );
512 else if (current->process->console)
514 console = (struct console_input *)grab_object( current->process->console );
517 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
518 return console;
521 struct console_signal_info
523 struct console_input *console;
524 process_id_t group;
525 int signal;
528 static int propagate_console_signal_cb(struct process *process, void *user)
530 struct console_signal_info* csi = (struct console_signal_info*)user;
532 if (process->console == csi->console && process->running_threads &&
533 (!csi->group || process->group_id == csi->group))
535 /* find a suitable thread to signal */
536 struct thread *thread;
537 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
539 if (send_thread_signal( thread, csi->signal )) break;
542 return FALSE;
545 static void propagate_console_signal( struct console_input *console,
546 int sig, process_id_t group_id )
548 struct console_signal_info csi;
550 if (!console)
552 set_error( STATUS_INVALID_PARAMETER );
553 return;
555 /* FIXME: should support the other events (like CTRL_BREAK) */
556 if (sig != CTRL_C_EVENT)
558 set_error( STATUS_NOT_IMPLEMENTED );
559 return;
561 csi.console = console;
562 csi.signal = SIGINT;
563 csi.group = group_id;
565 enum_processes(propagate_console_signal_cb, &csi);
568 static int get_console_mode( obj_handle_t handle )
570 struct object *obj;
571 int ret = 0;
573 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
575 if (obj->ops == &console_input_ops)
577 ret = ((struct console_input *)obj)->mode;
579 else if (obj->ops == &screen_buffer_ops)
581 ret = ((struct screen_buffer *)obj)->mode;
583 else
584 set_error( STATUS_OBJECT_TYPE_MISMATCH );
585 release_object( obj );
587 return ret;
590 /* changes the mode of either a console input or a screen buffer */
591 static int set_console_mode( obj_handle_t handle, int mode )
593 struct object *obj;
594 int ret = 0;
596 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
597 return 0;
598 if (obj->ops == &console_input_ops)
600 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
601 ((struct console_input *)obj)->mode = mode;
602 ret = 1;
604 else if (obj->ops == &screen_buffer_ops)
606 ((struct screen_buffer *)obj)->mode = mode;
607 ret = 1;
609 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
610 release_object( obj );
611 return ret;
614 /* add input events to a console input queue */
615 static int write_console_input( struct console_input* console, int count,
616 const INPUT_RECORD *records )
618 INPUT_RECORD *new_rec;
620 if (!count) return 0;
621 if (!(new_rec = realloc( console->records,
622 (console->recnum + count) * sizeof(INPUT_RECORD) )))
624 set_error( STATUS_NO_MEMORY );
625 release_object( console );
626 return -1;
628 console->records = new_rec;
629 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
631 if (console->mode & ENABLE_PROCESSED_INPUT)
633 int i = 0;
634 while (i < count)
636 if (records[i].EventType == KEY_EVENT &&
637 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
638 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
640 if (i != count - 1)
641 memcpy( &console->records[console->recnum + i],
642 &console->records[console->recnum + i + 1],
643 (count - i - 1) * sizeof(INPUT_RECORD) );
644 count--;
645 if (records[i].Event.KeyEvent.bKeyDown)
647 /* send SIGINT to all processes attached to this console */
648 propagate_console_signal( console, CTRL_C_EVENT, 0 );
651 else i++;
654 if (!console->recnum && count) set_event( console->event );
655 console->recnum += count;
656 return count;
659 /* retrieve a pointer to the console input records */
660 static int read_console_input( obj_handle_t handle, int count, int flush )
662 struct console_input *console;
664 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
665 FILE_READ_DATA, &console_input_ops )))
666 return -1;
668 if (!count)
670 /* special case: do not retrieve anything, but return
671 * the total number of records available */
672 count = console->recnum;
674 else
676 if (count > console->recnum) count = console->recnum;
677 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
679 if (flush)
681 int i;
682 for (i = count; i < console->recnum; i++)
683 console->records[i-count] = console->records[i];
684 if ((console->recnum -= count) > 0)
686 INPUT_RECORD *new_rec = realloc( console->records,
687 console->recnum * sizeof(INPUT_RECORD) );
688 if (new_rec) console->records = new_rec;
690 else
692 free( console->records );
693 console->records = NULL;
694 reset_event( console->event );
697 release_object( console );
698 return count;
701 /* set misc console input information */
702 static int set_console_input_info( const struct set_console_input_info_request *req,
703 const WCHAR *title, data_size_t len )
705 struct console_input *console;
706 struct console_renderer_event evt;
708 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
709 if (console_input_is_bare(console) &&
710 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
711 SET_CONSOLE_INPUT_INFO_WIN)))
713 set_error( STATUS_UNSUCCESSFUL );
714 goto error;
717 memset(&evt.u, 0, sizeof(evt.u));
718 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
720 struct screen_buffer *screen_buffer;
722 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
723 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
724 if (!screen_buffer || screen_buffer->input != console)
726 set_error( STATUS_INVALID_HANDLE );
727 if (screen_buffer) release_object( screen_buffer );
728 goto error;
731 if (screen_buffer != console->active)
733 if (console->active) release_object( console->active );
734 console->active = screen_buffer;
735 generate_sb_initial_events( console );
737 else
738 release_object( screen_buffer );
740 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
742 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
743 if (new_title)
745 memcpy( new_title, title, len );
746 new_title[len / sizeof(WCHAR)] = 0;
747 free( console->title );
748 console->title = new_title;
749 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
750 console_input_events_append( console, &evt );
753 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
755 console->history_mode = req->history_mode;
757 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
758 console->history_size != req->history_size)
760 WCHAR** mem = NULL;
761 int i;
762 int delta;
764 if (req->history_size)
766 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
767 if (!mem) goto error;
768 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
771 delta = (console->history_index > req->history_size) ?
772 (console->history_index - req->history_size) : 0;
774 for (i = delta; i < console->history_index; i++)
776 mem[i - delta] = console->history[i];
777 console->history[i] = NULL;
779 console->history_index -= delta;
781 for (i = 0; i < console->history_size; i++)
782 free( console->history[i] );
783 free( console->history );
784 console->history = mem;
785 console->history_size = req->history_size;
787 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
789 console->edition_mode = req->edition_mode;
791 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
793 console->input_cp = req->input_cp;
795 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
797 console->output_cp = req->output_cp;
799 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
801 console->win = req->win;
803 release_object( console );
804 return 1;
805 error:
806 if (console) release_object( console );
807 return 0;
810 /* resize a screen buffer */
811 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
812 int new_width, int new_height )
814 int i, old_width, old_height, copy_width, copy_height;
815 char_info_t *new_data;
817 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
819 set_error( STATUS_NO_MEMORY );
820 return 0;
822 old_width = screen_buffer->width;
823 old_height = screen_buffer->height;
824 copy_width = min( old_width, new_width );
825 copy_height = min( old_height, new_height );
827 /* copy all the rows */
828 for (i = 0; i < copy_height; i++)
830 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
831 copy_width * sizeof(char_info_t) );
834 /* clear the end of each row */
835 if (new_width > old_width)
837 /* fill first row */
838 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
839 /* and blast it to the other rows */
840 for (i = 1; i < copy_height; i++)
841 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
842 (new_width - old_width) * sizeof(char_info_t) );
845 /* clear remaining rows */
846 if (new_height > old_height)
848 /* fill first row */
849 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
850 /* and blast it to the other rows */
851 for (i = old_height+1; i < new_height; i++)
852 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
853 new_width * sizeof(char_info_t) );
855 free( screen_buffer->data );
856 screen_buffer->data = new_data;
857 screen_buffer->width = new_width;
858 screen_buffer->height = new_height;
859 return 1;
862 /* set misc screen buffer information */
863 static int set_console_output_info( struct screen_buffer *screen_buffer,
864 const struct set_console_output_info_request *req )
866 struct console_renderer_event evt;
868 memset(&evt.u, 0, sizeof(evt.u));
869 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
871 if (req->cursor_size < 1 || req->cursor_size > 100)
873 set_error( STATUS_INVALID_PARAMETER );
874 return 0;
876 if (screen_buffer->cursor_size != req->cursor_size ||
877 screen_buffer->cursor_visible != req->cursor_visible)
879 screen_buffer->cursor_size = req->cursor_size;
880 screen_buffer->cursor_visible = req->cursor_visible;
881 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
882 evt.u.cursor_geom.size = req->cursor_size;
883 evt.u.cursor_geom.visible = req->cursor_visible;
884 console_input_events_append( screen_buffer->input, &evt );
887 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
889 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
890 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
892 set_error( STATUS_INVALID_PARAMETER );
893 return 0;
895 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
897 screen_buffer->cursor_x = req->cursor_x;
898 screen_buffer->cursor_y = req->cursor_y;
899 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
900 evt.u.cursor_pos.x = req->cursor_x;
901 evt.u.cursor_pos.y = req->cursor_y;
902 console_input_events_append( screen_buffer->input, &evt );
905 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
907 unsigned cc;
909 /* new screen-buffer cannot be smaller than actual window */
910 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
911 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
913 set_error( STATUS_INVALID_PARAMETER );
914 return 0;
916 /* FIXME: there are also some basic minimum and max size to deal with */
917 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
919 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
920 evt.u.resize.width = req->width;
921 evt.u.resize.height = req->height;
922 console_input_events_append( screen_buffer->input, &evt );
924 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
925 evt.u.update.top = 0;
926 evt.u.update.bottom = screen_buffer->height - 1;
927 console_input_events_append( screen_buffer->input, &evt );
929 /* scroll window to display sb */
930 if (screen_buffer->win.right >= req->width)
932 screen_buffer->win.right -= screen_buffer->win.left;
933 screen_buffer->win.left = 0;
935 if (screen_buffer->win.bottom >= req->height)
937 screen_buffer->win.bottom -= screen_buffer->win.top;
938 screen_buffer->win.top = 0;
940 /* reset cursor if needed (normally, if cursor was outside of new sb, the
941 * window has been shifted so that the new position of the cursor will be
942 * visible */
943 cc = 0;
944 if (screen_buffer->cursor_x >= req->width)
946 screen_buffer->cursor_x = req->width - 1;
947 cc++;
949 if (screen_buffer->cursor_y >= req->height)
951 screen_buffer->cursor_y = req->height - 1;
952 cc++;
954 if (cc)
956 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
957 evt.u.cursor_pos.x = req->cursor_x;
958 evt.u.cursor_pos.y = req->cursor_y;
959 console_input_events_append( screen_buffer->input, &evt );
962 if (screen_buffer == screen_buffer->input->active &&
963 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
965 INPUT_RECORD ir;
966 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
967 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
968 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
969 write_console_input( screen_buffer->input, 1, &ir );
972 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
974 screen_buffer->attr = req->attr;
976 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
978 if (req->win_left < 0 || req->win_left > req->win_right ||
979 req->win_right >= screen_buffer->width ||
980 req->win_top < 0 || req->win_top > req->win_bottom ||
981 req->win_bottom >= screen_buffer->height)
983 set_error( STATUS_INVALID_PARAMETER );
984 return 0;
986 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
987 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
989 screen_buffer->win.left = req->win_left;
990 screen_buffer->win.top = req->win_top;
991 screen_buffer->win.right = req->win_right;
992 screen_buffer->win.bottom = req->win_bottom;
993 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
994 evt.u.display.left = req->win_left;
995 evt.u.display.top = req->win_top;
996 evt.u.display.width = req->win_right - req->win_left + 1;
997 evt.u.display.height = req->win_bottom - req->win_top + 1;
998 console_input_events_append( screen_buffer->input, &evt );
1001 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1003 /* can only be done by renderer */
1004 if (current->process->console != screen_buffer->input)
1006 set_error( STATUS_INVALID_PARAMETER );
1007 return 0;
1010 screen_buffer->max_width = req->max_width;
1011 screen_buffer->max_height = req->max_height;
1014 return 1;
1017 /* appends a new line to history (history is a fixed size array) */
1018 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1020 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1022 if (!ptr)
1023 return;
1025 if (!console || !console->history_size)
1027 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1028 free( ptr );
1029 return;
1032 memcpy( ptr, buf, len * sizeof(WCHAR) );
1033 ptr[len] = 0;
1035 if (console->history_mode && console->history_index &&
1036 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
1038 /* ok, mode ask us to not use twice the same string...
1039 * so just free mem and returns
1041 set_error( STATUS_ALIAS_EXISTS );
1042 free(ptr);
1043 return;
1046 if (console->history_index < console->history_size)
1048 console->history[console->history_index++] = ptr;
1050 else
1052 free( console->history[0]) ;
1053 memmove( &console->history[0], &console->history[1],
1054 (console->history_size - 1) * sizeof(WCHAR*) );
1055 console->history[console->history_size - 1] = ptr;
1059 /* returns a line from the cache */
1060 static data_size_t console_input_get_hist( struct console_input *console, int index )
1062 data_size_t ret = 0;
1064 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1065 else
1067 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1068 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1070 return ret;
1073 /* dumb dump */
1074 static void console_input_dump( struct object *obj, int verbose )
1076 struct console_input *console = (struct console_input *)obj;
1077 assert( obj->ops == &console_input_ops );
1078 fprintf( stderr, "Console input active=%p evt=%p\n",
1079 console->active, console->evt );
1082 static void console_input_destroy( struct object *obj )
1084 struct console_input* console_in = (struct console_input *)obj;
1085 struct screen_buffer* curr;
1086 int i;
1088 assert( obj->ops == &console_input_ops );
1089 free( console_in->title );
1090 free( console_in->records );
1092 if (console_in->active) release_object( console_in->active );
1093 console_in->active = NULL;
1095 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1097 if (curr->input == console_in) curr->input = NULL;
1100 if (console_in->evt)
1102 release_object( console_in->evt );
1103 console_in->evt = NULL;
1105 release_object( console_in->event );
1106 if (console_in->fd)
1107 release_object( console_in->fd );
1109 for (i = 0; i < console_in->history_size; i++)
1110 free( console_in->history[i] );
1111 free( console_in->history );
1114 static void screen_buffer_dump( struct object *obj, int verbose )
1116 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1117 assert( obj->ops == &screen_buffer_ops );
1119 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1122 static void screen_buffer_destroy( struct object *obj )
1124 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1126 assert( obj->ops == &screen_buffer_ops );
1128 list_remove( &screen_buffer->entry );
1130 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1132 struct screen_buffer *sb;
1134 screen_buffer->input->active = NULL;
1135 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1137 if (sb->input == screen_buffer->input)
1139 sb->input->active = sb;
1140 break;
1144 if (screen_buffer->fd) release_object( screen_buffer->fd );
1145 free( screen_buffer->data );
1148 static struct fd *screen_buffer_get_fd( struct object *obj )
1150 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1151 assert( obj->ops == &screen_buffer_ops );
1152 if (screen_buffer->fd)
1153 return (struct fd*)grab_object( screen_buffer->fd );
1154 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1155 return NULL;
1158 /* write data into a screen buffer */
1159 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1160 const void* data, enum char_info_mode mode,
1161 int x, int y, int wrap )
1163 unsigned int i;
1164 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1166 if (y >= screen_buffer->height) return 0;
1168 if (wrap)
1169 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1170 else
1171 end = screen_buffer->data + (y+1) * screen_buffer->width;
1173 switch(mode)
1175 case CHAR_INFO_MODE_TEXT:
1177 const WCHAR *ptr = data;
1178 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1180 break;
1181 case CHAR_INFO_MODE_ATTR:
1183 const unsigned short *ptr = data;
1184 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1186 break;
1187 case CHAR_INFO_MODE_TEXTATTR:
1189 const char_info_t *ptr = data;
1190 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1192 break;
1193 case CHAR_INFO_MODE_TEXTSTDATTR:
1195 const WCHAR *ptr = data;
1196 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1198 dest->ch = ptr[i];
1199 dest->attr = screen_buffer->attr;
1202 break;
1203 default:
1204 set_error( STATUS_INVALID_PARAMETER );
1205 return 0;
1208 if (i && screen_buffer == screen_buffer->input->active)
1210 struct console_renderer_event evt;
1211 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1212 memset(&evt.u, 0, sizeof(evt.u));
1213 evt.u.update.top = y + x / screen_buffer->width;
1214 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1215 console_input_events_append( screen_buffer->input, &evt );
1217 return i;
1220 /* fill a screen buffer with uniform data */
1221 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1222 enum char_info_mode mode, int x, int y, int count, int wrap )
1224 int i;
1225 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1227 if (y >= screen_buffer->height) return 0;
1229 if (wrap)
1230 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1231 else
1232 end = screen_buffer->data + (y+1) * screen_buffer->width;
1234 if (count > end - dest) count = end - dest;
1236 switch(mode)
1238 case CHAR_INFO_MODE_TEXT:
1239 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1240 break;
1241 case CHAR_INFO_MODE_ATTR:
1242 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1243 break;
1244 case CHAR_INFO_MODE_TEXTATTR:
1245 for (i = 0; i < count; i++) dest[i] = data;
1246 break;
1247 case CHAR_INFO_MODE_TEXTSTDATTR:
1248 for (i = 0; i < count; i++)
1250 dest[i].ch = data.ch;
1251 dest[i].attr = screen_buffer->attr;
1253 break;
1254 default:
1255 set_error( STATUS_INVALID_PARAMETER );
1256 return 0;
1259 if (count && screen_buffer == screen_buffer->input->active)
1261 struct console_renderer_event evt;
1262 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1263 memset(&evt.u, 0, sizeof(evt.u));
1264 evt.u.update.top = y;
1265 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1266 console_input_events_append( screen_buffer->input, &evt );
1268 return i;
1271 /* read data from a screen buffer */
1272 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1273 enum char_info_mode mode, int wrap )
1275 int i;
1276 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1278 if (y >= screen_buffer->height) return;
1280 if (wrap)
1281 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1282 else
1283 end = screen_buffer->data + (y+1) * screen_buffer->width;
1285 switch(mode)
1287 case CHAR_INFO_MODE_TEXT:
1289 WCHAR *data;
1290 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1291 if ((data = set_reply_data_size( count * sizeof(*data) )))
1293 for (i = 0; i < count; i++) data[i] = src[i].ch;
1296 break;
1297 case CHAR_INFO_MODE_ATTR:
1299 unsigned short *data;
1300 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1301 if ((data = set_reply_data_size( count * sizeof(*data) )))
1303 for (i = 0; i < count; i++) data[i] = src[i].attr;
1306 break;
1307 case CHAR_INFO_MODE_TEXTATTR:
1309 char_info_t *data;
1310 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1311 if ((data = set_reply_data_size( count * sizeof(*data) )))
1313 for (i = 0; i < count; i++) data[i] = src[i];
1316 break;
1317 default:
1318 set_error( STATUS_INVALID_PARAMETER );
1319 break;
1323 /* scroll parts of a screen buffer */
1324 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1325 int w, int h )
1327 int j;
1328 char_info_t *psrc, *pdst;
1329 struct console_renderer_event evt;
1331 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1332 xsrc + w > screen_buffer->width ||
1333 xdst + w > screen_buffer->width ||
1334 ysrc + h > screen_buffer->height ||
1335 ydst + h > screen_buffer->height ||
1336 w == 0 || h == 0)
1338 set_error( STATUS_INVALID_PARAMETER );
1339 return;
1342 if (ysrc < ydst)
1344 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1345 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1347 for (j = h; j > 0; j--)
1349 memcpy(pdst, psrc, w * sizeof(*pdst) );
1350 pdst -= screen_buffer->width;
1351 psrc -= screen_buffer->width;
1354 else
1356 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1357 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1359 for (j = 0; j < h; j++)
1361 /* we use memmove here because when psrc and pdst are the same,
1362 * copies are done on the same row, so the dst and src blocks
1363 * can overlap */
1364 memmove( pdst, psrc, w * sizeof(*pdst) );
1365 pdst += screen_buffer->width;
1366 psrc += screen_buffer->width;
1370 /* FIXME: this could be enhanced, by signalling scroll */
1371 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1372 memset(&evt.u, 0, sizeof(evt.u));
1373 evt.u.update.top = min(ysrc, ydst);
1374 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1375 console_input_events_append( screen_buffer->input, &evt );
1378 /* allocate a console for the renderer */
1379 DECL_HANDLER(alloc_console)
1381 obj_handle_t in = 0;
1382 obj_handle_t evt = 0;
1383 struct process *process;
1384 struct thread *renderer;
1385 struct console_input *console;
1386 int fd;
1387 int attach = 0;
1389 switch (req->pid)
1391 case 0:
1392 /* renderer is current, console to be attached to parent process */
1393 renderer = current;
1394 if (!(process = current->process->parent))
1396 set_error( STATUS_ACCESS_DENIED );
1397 return;
1399 grab_object( process );
1400 attach = 1;
1401 break;
1402 case 0xffffffff:
1403 /* no renderer, console to be attached to current process */
1404 renderer = NULL;
1405 process = current->process;
1406 grab_object( process );
1407 attach = 1;
1408 break;
1409 default:
1410 /* renderer is current, console to be attached to req->pid */
1411 renderer = current;
1412 if (!(process = get_process_from_id( req->pid ))) return;
1415 if (attach && process->console)
1417 set_error( STATUS_ACCESS_DENIED );
1418 goto the_end;
1420 if (req->input_fd != -1)
1422 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1424 set_error( STATUS_INVALID_PARAMETER );
1425 goto the_end;
1428 else fd = -1;
1430 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1432 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1434 if (!console->evt ||
1435 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1437 if (attach)
1439 process->console = (struct console_input*)grab_object( console );
1440 console->num_proc++;
1442 reply->handle_in = in;
1443 reply->event = evt;
1444 release_object( console );
1445 goto the_end;
1447 close_handle( current->process, in );
1449 release_object( console );
1451 the_end:
1452 release_object( process );
1455 /* free the console of the current process */
1456 DECL_HANDLER(free_console)
1458 free_console( current->process );
1461 /* let the renderer peek the events it's waiting on */
1462 DECL_HANDLER(get_console_renderer_events)
1464 struct console_input_events *evt;
1466 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1467 FILE_READ_PROPERTIES, &console_input_events_ops );
1468 if (!evt) return;
1469 console_input_events_get( evt );
1470 release_object( evt );
1473 /* open a handle to the process console */
1474 DECL_HANDLER(open_console)
1476 struct object *obj = NULL;
1478 reply->handle = 0;
1479 if (!req->from)
1481 if (current->process->console)
1482 obj = grab_object( (struct object*)current->process->console );
1484 else if (req->from == (obj_handle_t)1)
1486 if (current->process->console && current->process->console->active)
1487 obj = grab_object( (struct object*)current->process->console->active );
1489 else if ((obj = get_handle_obj( current->process, req->from,
1490 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1492 struct console_input *console = (struct console_input *)obj;
1493 obj = (console->active) ? grab_object( console->active ) : NULL;
1494 release_object( console );
1497 /* FIXME: req->share is not used (as in screen buffer creation) */
1498 if (obj)
1500 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1501 release_object( obj );
1503 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1506 /* set info about a console input */
1507 DECL_HANDLER(set_console_input_info)
1509 set_console_input_info( req, get_req_data(), get_req_data_size() );
1512 /* get info about a console (output only) */
1513 DECL_HANDLER(get_console_input_info)
1515 struct console_input *console;
1517 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1518 if (console->title)
1520 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1521 if (len > get_reply_max_size()) len = get_reply_max_size();
1522 set_reply_data( console->title, len );
1524 reply->history_mode = console->history_mode;
1525 reply->history_size = console->history_size;
1526 reply->history_index = console->history_index;
1527 reply->edition_mode = console->edition_mode;
1528 reply->input_cp = console->input_cp;
1529 reply->output_cp = console->output_cp;
1530 reply->win = console->win;
1532 release_object( console );
1535 /* get a console mode (input or output) */
1536 DECL_HANDLER(get_console_mode)
1538 reply->mode = get_console_mode( req->handle );
1541 /* set a console mode (input or output) */
1542 DECL_HANDLER(set_console_mode)
1544 set_console_mode( req->handle, req->mode );
1547 /* add input records to a console input queue */
1548 DECL_HANDLER(write_console_input)
1550 struct console_input *console;
1552 reply->written = 0;
1553 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1554 FILE_WRITE_PROPERTIES, &console_input_ops )))
1555 return;
1556 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1557 get_req_data() );
1558 release_object( console );
1561 /* fetch input records from a console input queue */
1562 DECL_HANDLER(read_console_input)
1564 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1565 reply->read = read_console_input( req->handle, count, req->flush );
1568 /* appends a string to console's history */
1569 DECL_HANDLER(append_console_input_history)
1571 struct console_input *console;
1573 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1574 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1575 release_object( console );
1578 /* appends a string to console's history */
1579 DECL_HANDLER(get_console_input_history)
1581 struct console_input *console;
1583 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1584 reply->total = console_input_get_hist( console, req->index );
1585 release_object( console );
1588 /* creates a screen buffer */
1589 DECL_HANDLER(create_console_output)
1591 struct console_input* console;
1592 struct screen_buffer* screen_buffer;
1593 int fd;
1595 if (req->fd != -1)
1597 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1599 set_error( STATUS_INVALID_HANDLE );
1600 return;
1603 else fd = -1;
1604 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1606 close(fd);
1607 return;
1609 if (console_input_is_bare( console ) ^ (fd != -1))
1611 close( fd );
1612 release_object( console );
1613 set_error( STATUS_INVALID_HANDLE );
1614 return;
1617 screen_buffer = create_console_output( console, fd );
1618 if (screen_buffer)
1620 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1621 * see file.c on how this could be done */
1622 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1623 release_object( screen_buffer );
1625 release_object( console );
1628 /* set info about a console screen buffer */
1629 DECL_HANDLER(set_console_output_info)
1631 struct screen_buffer *screen_buffer;
1633 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1634 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1636 set_console_output_info( screen_buffer, req );
1637 release_object( screen_buffer );
1641 /* get info about a console screen buffer */
1642 DECL_HANDLER(get_console_output_info)
1644 struct screen_buffer *screen_buffer;
1646 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1647 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1649 reply->cursor_size = screen_buffer->cursor_size;
1650 reply->cursor_visible = screen_buffer->cursor_visible;
1651 reply->cursor_x = screen_buffer->cursor_x;
1652 reply->cursor_y = screen_buffer->cursor_y;
1653 reply->width = screen_buffer->width;
1654 reply->height = screen_buffer->height;
1655 reply->attr = screen_buffer->attr;
1656 reply->win_left = screen_buffer->win.left;
1657 reply->win_top = screen_buffer->win.top;
1658 reply->win_right = screen_buffer->win.right;
1659 reply->win_bottom = screen_buffer->win.bottom;
1660 reply->max_width = screen_buffer->max_width;
1661 reply->max_height = screen_buffer->max_height;
1662 release_object( screen_buffer );
1666 /* read data (chars & attrs) from a screen buffer */
1667 DECL_HANDLER(read_console_output)
1669 struct screen_buffer *screen_buffer;
1671 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1672 FILE_READ_DATA, &screen_buffer_ops )))
1674 if (console_input_is_bare( screen_buffer->input ))
1676 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1677 release_object( screen_buffer );
1678 return;
1680 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1681 reply->width = screen_buffer->width;
1682 reply->height = screen_buffer->height;
1683 release_object( screen_buffer );
1687 /* write data (char and/or attrs) to a screen buffer */
1688 DECL_HANDLER(write_console_output)
1690 struct screen_buffer *screen_buffer;
1692 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1693 FILE_WRITE_DATA, &screen_buffer_ops)))
1695 if (console_input_is_bare( screen_buffer->input ))
1697 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1698 release_object( screen_buffer );
1699 return;
1701 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1702 req->mode, req->x, req->y, req->wrap );
1703 reply->width = screen_buffer->width;
1704 reply->height = screen_buffer->height;
1705 release_object( screen_buffer );
1709 /* fill a screen buffer with constant data (chars and/or attributes) */
1710 DECL_HANDLER(fill_console_output)
1712 struct screen_buffer *screen_buffer;
1714 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1715 FILE_WRITE_DATA, &screen_buffer_ops)))
1717 if (console_input_is_bare( screen_buffer->input ))
1719 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1720 release_object( screen_buffer );
1721 return;
1723 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1724 req->x, req->y, req->count, req->wrap );
1725 release_object( screen_buffer );
1729 /* move a rect of data in a screen buffer */
1730 DECL_HANDLER(move_console_output)
1732 struct screen_buffer *screen_buffer;
1734 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1735 FILE_WRITE_DATA, &screen_buffer_ops)))
1737 if (console_input_is_bare( screen_buffer->input ))
1739 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1740 release_object( screen_buffer );
1741 return;
1743 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1744 req->w, req->h );
1745 release_object( screen_buffer );
1749 /* sends a signal to a console (process, group...) */
1750 DECL_HANDLER(send_console_signal)
1752 process_id_t group;
1754 group = req->group_id ? req->group_id : current->process->group_id;
1756 if (!group)
1757 set_error( STATUS_INVALID_PARAMETER );
1758 else
1759 propagate_console_signal( current->process->console, req->signal, group );
1762 /* get console which renderer is 'current' */
1763 static int cgwe_enum( struct process* process, void* user)
1765 if (process->console && process->console->renderer == current)
1767 *(struct console_input**)user = process->console;
1768 return 1;
1770 return 0;
1773 DECL_HANDLER(get_console_wait_event)
1775 struct console_input* console = NULL;
1777 if (current->process->console)
1778 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1779 else enum_processes(cgwe_enum, &console);
1781 if (console)
1783 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1784 release_object( console );
1786 else set_error( STATUS_INVALID_PARAMETER );