shell32/tests: Remove unused variables (Clang).
[wine/multimedia.git] / server / console.c
blob4c2d61dda4a8d55d19b2f1df7c0d49b53196332e
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 wait_queue_entry *entry );
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 wait_queue_entry *entry )
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 )))
304 if (fd != -1) close( fd );
305 return NULL;
307 console_input->renderer = renderer;
308 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
309 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
310 ENABLE_EXTENDED_FLAGS;
311 console_input->num_proc = 0;
312 console_input->active = NULL;
313 console_input->recnum = 0;
314 console_input->records = NULL;
315 console_input->evt = renderer ? create_console_input_events() : NULL;
316 console_input->title = NULL;
317 console_input->history_size = 50;
318 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
319 console_input->history_index = 0;
320 console_input->history_mode = 0;
321 console_input->edition_mode = 0;
322 console_input->input_cp = 0;
323 console_input->output_cp = 0;
324 console_input->win = 0;
325 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
326 console_input->fd = NULL;
328 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
330 if (fd != -1) close( fd );
331 release_object( console_input );
332 return NULL;
334 if (fd != -1) /* bare console */
336 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
337 FILE_SYNCHRONOUS_IO_NONALERT )))
339 release_object( console_input );
340 return NULL;
342 allow_fd_caching( console_input->fd );
345 return &console_input->obj;
348 static void generate_sb_initial_events( struct console_input *console_input )
350 struct screen_buffer *screen_buffer = console_input->active;
351 struct console_renderer_event evt;
353 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
354 memset(&evt.u, 0, sizeof(evt.u));
355 console_input_events_append( console_input, &evt );
357 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
358 evt.u.resize.width = screen_buffer->width;
359 evt.u.resize.height = screen_buffer->height;
360 console_input_events_append( console_input, &evt );
362 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
363 evt.u.display.left = screen_buffer->win.left;
364 evt.u.display.top = screen_buffer->win.top;
365 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
366 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
367 console_input_events_append( console_input, &evt );
369 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
370 evt.u.update.top = 0;
371 evt.u.update.bottom = screen_buffer->height - 1;
372 console_input_events_append( console_input, &evt );
374 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
375 evt.u.cursor_geom.size = screen_buffer->cursor_size;
376 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
377 console_input_events_append( console_input, &evt );
379 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
380 evt.u.cursor_pos.x = screen_buffer->cursor_x;
381 evt.u.cursor_pos.y = screen_buffer->cursor_y;
382 console_input_events_append( console_input, &evt );
385 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
387 struct screen_buffer *screen_buffer;
388 int i;
390 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
392 if (fd != -1) close( fd );
393 return NULL;
395 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
396 screen_buffer->input = console_input;
397 screen_buffer->cursor_size = 100;
398 screen_buffer->cursor_visible = 1;
399 screen_buffer->width = 80;
400 screen_buffer->height = 150;
401 screen_buffer->max_width = 80;
402 screen_buffer->max_height = 25;
403 screen_buffer->cursor_x = 0;
404 screen_buffer->cursor_y = 0;
405 screen_buffer->attr = 0x0F;
406 screen_buffer->win.left = 0;
407 screen_buffer->win.right = screen_buffer->max_width - 1;
408 screen_buffer->win.top = 0;
409 screen_buffer->win.bottom = screen_buffer->max_height - 1;
410 if (fd == -1)
411 screen_buffer->fd = NULL;
412 else
414 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
415 FILE_SYNCHRONOUS_IO_NONALERT )))
417 release_object( screen_buffer );
418 return NULL;
420 allow_fd_caching(screen_buffer->fd);
423 list_add_head( &screen_buffer_list, &screen_buffer->entry );
425 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
426 sizeof(*screen_buffer->data) )))
428 release_object( screen_buffer );
429 return NULL;
431 /* clear the first row */
432 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
433 /* and copy it to all other rows */
434 for (i = 1; i < screen_buffer->height; i++)
435 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
436 screen_buffer->width * sizeof(char_info_t) );
438 if (!console_input->active)
440 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
441 generate_sb_initial_events( console_input );
443 return screen_buffer;
446 /* free the console for this process */
447 int free_console( struct process *process )
449 struct console_input* console = process->console;
451 if (!console) return 0;
453 process->console = NULL;
454 if (--console->num_proc == 0 && console->renderer)
456 /* all processes have terminated... tell the renderer to terminate too */
457 struct console_renderer_event evt;
458 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
459 memset(&evt.u, 0, sizeof(evt.u));
460 console_input_events_append( console, &evt );
462 release_object( console );
464 return 1;
467 /* let process inherit the console from parent... this handle two cases :
468 * 1/ generic console inheritance
469 * 2/ parent is a renderer which launches process, and process should attach to the console
470 * renderered by parent
472 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
474 int done = 0;
475 struct process* parent = parent_thread->process;
477 /* if parent is a renderer, then attach current process to its console
478 * a bit hacky....
480 if (hconin)
482 struct console_input* console;
484 /* FIXME: should we check some access rights ? */
485 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
486 0, &console_input_ops )))
488 if (console->renderer == parent_thread)
490 process->console = (struct console_input*)grab_object( console );
491 process->console->num_proc++;
492 done = 1;
494 release_object( console );
496 else clear_error(); /* ignore error */
498 /* otherwise, if parent has a console, attach child to this console */
499 if (!done && parent->console)
501 process->console = (struct console_input*)grab_object( parent->console );
502 process->console->num_proc++;
506 struct thread *console_get_renderer( struct console_input *console )
508 return console->renderer;
511 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
513 struct console_input* console = NULL;
515 if (handle)
516 console = (struct console_input *)get_handle_obj( current->process, handle,
517 access, &console_input_ops );
518 else if (current->process->console)
520 console = (struct console_input *)grab_object( current->process->console );
523 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
524 return console;
527 struct console_signal_info
529 struct console_input *console;
530 process_id_t group;
531 int signal;
534 static int propagate_console_signal_cb(struct process *process, void *user)
536 struct console_signal_info* csi = (struct console_signal_info*)user;
538 if (process->console == csi->console && process->running_threads &&
539 (!csi->group || process->group_id == csi->group))
541 /* find a suitable thread to signal */
542 struct thread *thread;
543 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
545 if (send_thread_signal( thread, csi->signal )) break;
548 return FALSE;
551 static void propagate_console_signal( struct console_input *console,
552 int sig, process_id_t group_id )
554 struct console_signal_info csi;
556 if (!console)
558 set_error( STATUS_INVALID_PARAMETER );
559 return;
561 /* FIXME: should support the other events (like CTRL_BREAK) */
562 if (sig != CTRL_C_EVENT)
564 set_error( STATUS_NOT_IMPLEMENTED );
565 return;
567 csi.console = console;
568 csi.signal = SIGINT;
569 csi.group = group_id;
571 enum_processes(propagate_console_signal_cb, &csi);
574 static int get_console_mode( obj_handle_t handle )
576 struct object *obj;
577 int ret = 0;
579 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
581 if (obj->ops == &console_input_ops)
583 ret = ((struct console_input *)obj)->mode;
585 else if (obj->ops == &screen_buffer_ops)
587 ret = ((struct screen_buffer *)obj)->mode;
589 else
590 set_error( STATUS_OBJECT_TYPE_MISMATCH );
591 release_object( obj );
593 return ret;
596 /* changes the mode of either a console input or a screen buffer */
597 static int set_console_mode( obj_handle_t handle, int mode )
599 struct object *obj;
600 int ret = 0;
602 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
603 return 0;
604 if (obj->ops == &console_input_ops)
606 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
607 ((struct console_input *)obj)->mode = mode;
608 ret = 1;
610 else if (obj->ops == &screen_buffer_ops)
612 ((struct screen_buffer *)obj)->mode = mode;
613 ret = 1;
615 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
616 release_object( obj );
617 return ret;
620 /* add input events to a console input queue */
621 static int write_console_input( struct console_input* console, int count,
622 const INPUT_RECORD *records )
624 INPUT_RECORD *new_rec;
626 if (!count) return 0;
627 if (!(new_rec = realloc( console->records,
628 (console->recnum + count) * sizeof(INPUT_RECORD) )))
630 set_error( STATUS_NO_MEMORY );
631 release_object( console );
632 return -1;
634 console->records = new_rec;
635 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
637 if (console->mode & ENABLE_PROCESSED_INPUT)
639 int i = 0;
640 while (i < count)
642 if (records[i].EventType == KEY_EVENT &&
643 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
644 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
646 if (i != count - 1)
647 memcpy( &console->records[console->recnum + i],
648 &console->records[console->recnum + i + 1],
649 (count - i - 1) * sizeof(INPUT_RECORD) );
650 count--;
651 if (records[i].Event.KeyEvent.bKeyDown)
653 /* send SIGINT to all processes attached to this console */
654 propagate_console_signal( console, CTRL_C_EVENT, 0 );
657 else i++;
660 if (!console->recnum && count) set_event( console->event );
661 console->recnum += count;
662 return count;
665 /* retrieve a pointer to the console input records */
666 static int read_console_input( obj_handle_t handle, int count, int flush )
668 struct console_input *console;
670 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
671 FILE_READ_DATA, &console_input_ops )))
672 return -1;
674 if (!count)
676 /* special case: do not retrieve anything, but return
677 * the total number of records available */
678 count = console->recnum;
680 else
682 if (count > console->recnum) count = console->recnum;
683 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
685 if (flush)
687 int i;
688 for (i = count; i < console->recnum; i++)
689 console->records[i-count] = console->records[i];
690 if ((console->recnum -= count) > 0)
692 INPUT_RECORD *new_rec = realloc( console->records,
693 console->recnum * sizeof(INPUT_RECORD) );
694 if (new_rec) console->records = new_rec;
696 else
698 free( console->records );
699 console->records = NULL;
700 reset_event( console->event );
703 release_object( console );
704 return count;
707 /* set misc console input information */
708 static int set_console_input_info( const struct set_console_input_info_request *req,
709 const WCHAR *title, data_size_t len )
711 struct console_input *console;
712 struct console_renderer_event evt;
714 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
715 if (console_input_is_bare(console) &&
716 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
717 SET_CONSOLE_INPUT_INFO_WIN)))
719 set_error( STATUS_UNSUCCESSFUL );
720 goto error;
723 memset(&evt.u, 0, sizeof(evt.u));
724 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
726 struct screen_buffer *screen_buffer;
728 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
729 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
730 if (!screen_buffer || screen_buffer->input != console)
732 set_error( STATUS_INVALID_HANDLE );
733 if (screen_buffer) release_object( screen_buffer );
734 goto error;
737 if (screen_buffer != console->active)
739 if (console->active) release_object( console->active );
740 console->active = screen_buffer;
741 generate_sb_initial_events( console );
743 else
744 release_object( screen_buffer );
746 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
748 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
749 if (new_title)
751 memcpy( new_title, title, len );
752 new_title[len / sizeof(WCHAR)] = 0;
753 free( console->title );
754 console->title = new_title;
755 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
756 console_input_events_append( console, &evt );
759 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
761 console->history_mode = req->history_mode;
763 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
764 console->history_size != req->history_size)
766 WCHAR** mem = NULL;
767 int i;
768 int delta;
770 if (req->history_size)
772 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
773 if (!mem) goto error;
774 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
777 delta = (console->history_index > req->history_size) ?
778 (console->history_index - req->history_size) : 0;
780 for (i = delta; i < console->history_index; i++)
782 mem[i - delta] = console->history[i];
783 console->history[i] = NULL;
785 console->history_index -= delta;
787 for (i = 0; i < console->history_size; i++)
788 free( console->history[i] );
789 free( console->history );
790 console->history = mem;
791 console->history_size = req->history_size;
793 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
795 console->edition_mode = req->edition_mode;
797 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
799 console->input_cp = req->input_cp;
801 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
803 console->output_cp = req->output_cp;
805 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
807 console->win = req->win;
809 release_object( console );
810 return 1;
811 error:
812 if (console) release_object( console );
813 return 0;
816 /* resize a screen buffer */
817 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
818 int new_width, int new_height )
820 int i, old_width, old_height, copy_width, copy_height;
821 char_info_t *new_data;
823 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
825 set_error( STATUS_NO_MEMORY );
826 return 0;
828 old_width = screen_buffer->width;
829 old_height = screen_buffer->height;
830 copy_width = min( old_width, new_width );
831 copy_height = min( old_height, new_height );
833 /* copy all the rows */
834 for (i = 0; i < copy_height; i++)
836 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
837 copy_width * sizeof(char_info_t) );
840 /* clear the end of each row */
841 if (new_width > old_width)
843 /* fill first row */
844 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
845 /* and blast it to the other rows */
846 for (i = 1; i < copy_height; i++)
847 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
848 (new_width - old_width) * sizeof(char_info_t) );
851 /* clear remaining rows */
852 if (new_height > old_height)
854 /* fill first row */
855 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
856 /* and blast it to the other rows */
857 for (i = old_height+1; i < new_height; i++)
858 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
859 new_width * sizeof(char_info_t) );
861 free( screen_buffer->data );
862 screen_buffer->data = new_data;
863 screen_buffer->width = new_width;
864 screen_buffer->height = new_height;
865 return 1;
868 /* set misc screen buffer information */
869 static int set_console_output_info( struct screen_buffer *screen_buffer,
870 const struct set_console_output_info_request *req )
872 struct console_renderer_event evt;
874 memset(&evt.u, 0, sizeof(evt.u));
875 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
877 if (req->cursor_size < 1 || req->cursor_size > 100)
879 set_error( STATUS_INVALID_PARAMETER );
880 return 0;
882 if (screen_buffer->cursor_size != req->cursor_size ||
883 screen_buffer->cursor_visible != req->cursor_visible)
885 screen_buffer->cursor_size = req->cursor_size;
886 screen_buffer->cursor_visible = req->cursor_visible;
887 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
888 evt.u.cursor_geom.size = req->cursor_size;
889 evt.u.cursor_geom.visible = req->cursor_visible;
890 console_input_events_append( screen_buffer->input, &evt );
893 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
895 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
896 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
898 set_error( STATUS_INVALID_PARAMETER );
899 return 0;
901 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
903 screen_buffer->cursor_x = req->cursor_x;
904 screen_buffer->cursor_y = req->cursor_y;
905 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
906 evt.u.cursor_pos.x = req->cursor_x;
907 evt.u.cursor_pos.y = req->cursor_y;
908 console_input_events_append( screen_buffer->input, &evt );
911 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
913 unsigned cc;
915 /* new screen-buffer cannot be smaller than actual window */
916 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
917 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
919 set_error( STATUS_INVALID_PARAMETER );
920 return 0;
922 /* FIXME: there are also some basic minimum and max size to deal with */
923 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
925 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
926 evt.u.resize.width = req->width;
927 evt.u.resize.height = req->height;
928 console_input_events_append( screen_buffer->input, &evt );
930 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
931 evt.u.update.top = 0;
932 evt.u.update.bottom = screen_buffer->height - 1;
933 console_input_events_append( screen_buffer->input, &evt );
935 /* scroll window to display sb */
936 if (screen_buffer->win.right >= req->width)
938 screen_buffer->win.right -= screen_buffer->win.left;
939 screen_buffer->win.left = 0;
941 if (screen_buffer->win.bottom >= req->height)
943 screen_buffer->win.bottom -= screen_buffer->win.top;
944 screen_buffer->win.top = 0;
946 /* reset cursor if needed (normally, if cursor was outside of new sb, the
947 * window has been shifted so that the new position of the cursor will be
948 * visible */
949 cc = 0;
950 if (screen_buffer->cursor_x >= req->width)
952 screen_buffer->cursor_x = req->width - 1;
953 cc++;
955 if (screen_buffer->cursor_y >= req->height)
957 screen_buffer->cursor_y = req->height - 1;
958 cc++;
960 if (cc)
962 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
963 evt.u.cursor_pos.x = req->cursor_x;
964 evt.u.cursor_pos.y = req->cursor_y;
965 console_input_events_append( screen_buffer->input, &evt );
968 if (screen_buffer == screen_buffer->input->active &&
969 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
971 INPUT_RECORD ir;
972 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
973 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
974 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
975 write_console_input( screen_buffer->input, 1, &ir );
978 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
980 screen_buffer->attr = req->attr;
982 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
984 if (req->win_left < 0 || req->win_left > req->win_right ||
985 req->win_right >= screen_buffer->width ||
986 req->win_top < 0 || req->win_top > req->win_bottom ||
987 req->win_bottom >= screen_buffer->height)
989 set_error( STATUS_INVALID_PARAMETER );
990 return 0;
992 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
993 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
995 screen_buffer->win.left = req->win_left;
996 screen_buffer->win.top = req->win_top;
997 screen_buffer->win.right = req->win_right;
998 screen_buffer->win.bottom = req->win_bottom;
999 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1000 evt.u.display.left = req->win_left;
1001 evt.u.display.top = req->win_top;
1002 evt.u.display.width = req->win_right - req->win_left + 1;
1003 evt.u.display.height = req->win_bottom - req->win_top + 1;
1004 console_input_events_append( screen_buffer->input, &evt );
1007 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1009 /* can only be done by renderer */
1010 if (current->process->console != screen_buffer->input)
1012 set_error( STATUS_INVALID_PARAMETER );
1013 return 0;
1016 screen_buffer->max_width = req->max_width;
1017 screen_buffer->max_height = req->max_height;
1020 return 1;
1023 /* appends a new line to history (history is a fixed size array) */
1024 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1026 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1028 if (!ptr)
1029 return;
1031 if (!console || !console->history_size)
1033 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1034 free( ptr );
1035 return;
1038 memcpy( ptr, buf, len * sizeof(WCHAR) );
1039 ptr[len] = 0;
1041 if (console->history_mode && console->history_index &&
1042 strncmpW( console->history[console->history_index - 1], ptr, len ) == 0)
1044 /* ok, mode ask us to not use twice the same string...
1045 * so just free mem and returns
1047 set_error( STATUS_ALIAS_EXISTS );
1048 free(ptr);
1049 return;
1052 if (console->history_index < console->history_size)
1054 console->history[console->history_index++] = ptr;
1056 else
1058 free( console->history[0]) ;
1059 memmove( &console->history[0], &console->history[1],
1060 (console->history_size - 1) * sizeof(WCHAR*) );
1061 console->history[console->history_size - 1] = ptr;
1065 /* returns a line from the cache */
1066 static data_size_t console_input_get_hist( struct console_input *console, int index )
1068 data_size_t ret = 0;
1070 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1071 else
1073 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1074 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1076 return ret;
1079 /* dumb dump */
1080 static void console_input_dump( struct object *obj, int verbose )
1082 struct console_input *console = (struct console_input *)obj;
1083 assert( obj->ops == &console_input_ops );
1084 fprintf( stderr, "Console input active=%p evt=%p\n",
1085 console->active, console->evt );
1088 static void console_input_destroy( struct object *obj )
1090 struct console_input* console_in = (struct console_input *)obj;
1091 struct screen_buffer* curr;
1092 int i;
1094 assert( obj->ops == &console_input_ops );
1095 free( console_in->title );
1096 free( console_in->records );
1098 if (console_in->active) release_object( console_in->active );
1099 console_in->active = NULL;
1101 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1103 if (curr->input == console_in) curr->input = NULL;
1106 if (console_in->evt)
1108 release_object( console_in->evt );
1109 console_in->evt = NULL;
1111 release_object( console_in->event );
1112 if (console_in->fd)
1113 release_object( console_in->fd );
1115 for (i = 0; i < console_in->history_size; i++)
1116 free( console_in->history[i] );
1117 free( console_in->history );
1120 static void screen_buffer_dump( struct object *obj, int verbose )
1122 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1123 assert( obj->ops == &screen_buffer_ops );
1125 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1128 static void screen_buffer_destroy( struct object *obj )
1130 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1132 assert( obj->ops == &screen_buffer_ops );
1134 list_remove( &screen_buffer->entry );
1136 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1138 struct screen_buffer *sb;
1140 screen_buffer->input->active = NULL;
1141 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1143 if (sb->input == screen_buffer->input)
1145 sb->input->active = sb;
1146 break;
1150 if (screen_buffer->fd) release_object( screen_buffer->fd );
1151 free( screen_buffer->data );
1154 static struct fd *screen_buffer_get_fd( struct object *obj )
1156 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1157 assert( obj->ops == &screen_buffer_ops );
1158 if (screen_buffer->fd)
1159 return (struct fd*)grab_object( screen_buffer->fd );
1160 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1161 return NULL;
1164 /* write data into a screen buffer */
1165 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1166 const void* data, enum char_info_mode mode,
1167 int x, int y, int wrap )
1169 unsigned int i;
1170 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1172 if (y >= screen_buffer->height) return 0;
1174 if (wrap)
1175 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1176 else
1177 end = screen_buffer->data + (y+1) * screen_buffer->width;
1179 switch(mode)
1181 case CHAR_INFO_MODE_TEXT:
1183 const WCHAR *ptr = data;
1184 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1186 break;
1187 case CHAR_INFO_MODE_ATTR:
1189 const unsigned short *ptr = data;
1190 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1192 break;
1193 case CHAR_INFO_MODE_TEXTATTR:
1195 const char_info_t *ptr = data;
1196 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1198 break;
1199 case CHAR_INFO_MODE_TEXTSTDATTR:
1201 const WCHAR *ptr = data;
1202 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1204 dest->ch = ptr[i];
1205 dest->attr = screen_buffer->attr;
1208 break;
1209 default:
1210 set_error( STATUS_INVALID_PARAMETER );
1211 return 0;
1214 if (i && screen_buffer == screen_buffer->input->active)
1216 struct console_renderer_event evt;
1217 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1218 memset(&evt.u, 0, sizeof(evt.u));
1219 evt.u.update.top = y + x / screen_buffer->width;
1220 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1221 console_input_events_append( screen_buffer->input, &evt );
1223 return i;
1226 /* fill a screen buffer with uniform data */
1227 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1228 enum char_info_mode mode, int x, int y, int count, int wrap )
1230 int i;
1231 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1233 if (y >= screen_buffer->height) return 0;
1235 if (wrap)
1236 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1237 else
1238 end = screen_buffer->data + (y+1) * screen_buffer->width;
1240 if (count > end - dest) count = end - dest;
1242 switch(mode)
1244 case CHAR_INFO_MODE_TEXT:
1245 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1246 break;
1247 case CHAR_INFO_MODE_ATTR:
1248 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1249 break;
1250 case CHAR_INFO_MODE_TEXTATTR:
1251 for (i = 0; i < count; i++) dest[i] = data;
1252 break;
1253 case CHAR_INFO_MODE_TEXTSTDATTR:
1254 for (i = 0; i < count; i++)
1256 dest[i].ch = data.ch;
1257 dest[i].attr = screen_buffer->attr;
1259 break;
1260 default:
1261 set_error( STATUS_INVALID_PARAMETER );
1262 return 0;
1265 if (count && screen_buffer == screen_buffer->input->active)
1267 struct console_renderer_event evt;
1268 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1269 memset(&evt.u, 0, sizeof(evt.u));
1270 evt.u.update.top = y;
1271 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1272 console_input_events_append( screen_buffer->input, &evt );
1274 return i;
1277 /* read data from a screen buffer */
1278 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1279 enum char_info_mode mode, int wrap )
1281 int i;
1282 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1284 if (y >= screen_buffer->height) return;
1286 if (wrap)
1287 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1288 else
1289 end = screen_buffer->data + (y+1) * screen_buffer->width;
1291 switch(mode)
1293 case CHAR_INFO_MODE_TEXT:
1295 WCHAR *data;
1296 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1297 if ((data = set_reply_data_size( count * sizeof(*data) )))
1299 for (i = 0; i < count; i++) data[i] = src[i].ch;
1302 break;
1303 case CHAR_INFO_MODE_ATTR:
1305 unsigned short *data;
1306 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1307 if ((data = set_reply_data_size( count * sizeof(*data) )))
1309 for (i = 0; i < count; i++) data[i] = src[i].attr;
1312 break;
1313 case CHAR_INFO_MODE_TEXTATTR:
1315 char_info_t *data;
1316 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1317 if ((data = set_reply_data_size( count * sizeof(*data) )))
1319 for (i = 0; i < count; i++) data[i] = src[i];
1322 break;
1323 default:
1324 set_error( STATUS_INVALID_PARAMETER );
1325 break;
1329 /* scroll parts of a screen buffer */
1330 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1331 int w, int h )
1333 int j;
1334 char_info_t *psrc, *pdst;
1335 struct console_renderer_event evt;
1337 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1338 xsrc + w > screen_buffer->width ||
1339 xdst + w > screen_buffer->width ||
1340 ysrc + h > screen_buffer->height ||
1341 ydst + h > screen_buffer->height ||
1342 w == 0 || h == 0)
1344 set_error( STATUS_INVALID_PARAMETER );
1345 return;
1348 if (ysrc < ydst)
1350 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1351 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1353 for (j = h; j > 0; j--)
1355 memcpy(pdst, psrc, w * sizeof(*pdst) );
1356 pdst -= screen_buffer->width;
1357 psrc -= screen_buffer->width;
1360 else
1362 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1363 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1365 for (j = 0; j < h; j++)
1367 /* we use memmove here because when psrc and pdst are the same,
1368 * copies are done on the same row, so the dst and src blocks
1369 * can overlap */
1370 memmove( pdst, psrc, w * sizeof(*pdst) );
1371 pdst += screen_buffer->width;
1372 psrc += screen_buffer->width;
1376 /* FIXME: this could be enhanced, by signalling scroll */
1377 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1378 memset(&evt.u, 0, sizeof(evt.u));
1379 evt.u.update.top = min(ysrc, ydst);
1380 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1381 console_input_events_append( screen_buffer->input, &evt );
1384 /* allocate a console for the renderer */
1385 DECL_HANDLER(alloc_console)
1387 obj_handle_t in = 0;
1388 obj_handle_t evt = 0;
1389 struct process *process;
1390 struct thread *renderer;
1391 struct console_input *console;
1392 int fd;
1393 int attach = 0;
1395 if (req->input_fd != -1)
1397 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1399 set_error( STATUS_INVALID_PARAMETER );
1400 return;
1403 else fd = -1;
1405 switch (req->pid)
1407 case 0:
1408 /* renderer is current, console to be attached to parent process */
1409 renderer = current;
1410 if (!(process = current->process->parent))
1412 if (fd != -1) close( fd );
1413 set_error( STATUS_ACCESS_DENIED );
1414 return;
1416 grab_object( process );
1417 attach = 1;
1418 break;
1419 case 0xffffffff:
1420 /* no renderer, console to be attached to current process */
1421 renderer = NULL;
1422 process = current->process;
1423 grab_object( process );
1424 attach = 1;
1425 break;
1426 default:
1427 /* renderer is current, console to be attached to req->pid */
1428 renderer = current;
1429 if (!(process = get_process_from_id( req->pid )))
1431 if (fd != -1) close( fd );
1432 return;
1436 if (attach && process->console)
1438 if (fd != -1) close( fd );
1439 set_error( STATUS_ACCESS_DENIED );
1440 goto the_end;
1443 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1445 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1447 if (!console->evt ||
1448 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1450 if (attach)
1452 process->console = (struct console_input*)grab_object( console );
1453 console->num_proc++;
1455 reply->handle_in = in;
1456 reply->event = evt;
1457 release_object( console );
1458 goto the_end;
1460 close_handle( current->process, in );
1462 release_object( console );
1464 the_end:
1465 release_object( process );
1468 /* free the console of the current process */
1469 DECL_HANDLER(free_console)
1471 free_console( current->process );
1474 /* let the renderer peek the events it's waiting on */
1475 DECL_HANDLER(get_console_renderer_events)
1477 struct console_input_events *evt;
1479 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1480 FILE_READ_PROPERTIES, &console_input_events_ops );
1481 if (!evt) return;
1482 console_input_events_get( evt );
1483 release_object( evt );
1486 /* open a handle to the process console */
1487 DECL_HANDLER(open_console)
1489 struct object *obj = NULL;
1491 reply->handle = 0;
1492 if (!req->from)
1494 if (current->process->console)
1495 obj = grab_object( (struct object*)current->process->console );
1497 else if (req->from == (obj_handle_t)1)
1499 if (current->process->console && current->process->console->active)
1500 obj = grab_object( (struct object*)current->process->console->active );
1502 else if ((obj = get_handle_obj( current->process, req->from,
1503 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1505 struct console_input *console = (struct console_input *)obj;
1506 obj = (console->active) ? grab_object( console->active ) : NULL;
1507 release_object( console );
1510 /* FIXME: req->share is not used (as in screen buffer creation) */
1511 if (obj)
1513 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1514 release_object( obj );
1516 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1519 /* set info about a console input */
1520 DECL_HANDLER(set_console_input_info)
1522 set_console_input_info( req, get_req_data(), get_req_data_size() );
1525 /* get info about a console (output only) */
1526 DECL_HANDLER(get_console_input_info)
1528 struct console_input *console;
1530 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1531 if (console->title)
1533 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1534 if (len > get_reply_max_size()) len = get_reply_max_size();
1535 set_reply_data( console->title, len );
1537 reply->history_mode = console->history_mode;
1538 reply->history_size = console->history_size;
1539 reply->history_index = console->history_index;
1540 reply->edition_mode = console->edition_mode;
1541 reply->input_cp = console->input_cp;
1542 reply->output_cp = console->output_cp;
1543 reply->win = console->win;
1545 release_object( console );
1548 /* get a console mode (input or output) */
1549 DECL_HANDLER(get_console_mode)
1551 reply->mode = get_console_mode( req->handle );
1554 /* set a console mode (input or output) */
1555 DECL_HANDLER(set_console_mode)
1557 set_console_mode( req->handle, req->mode );
1560 /* add input records to a console input queue */
1561 DECL_HANDLER(write_console_input)
1563 struct console_input *console;
1565 reply->written = 0;
1566 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1567 FILE_WRITE_PROPERTIES, &console_input_ops )))
1568 return;
1569 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1570 get_req_data() );
1571 release_object( console );
1574 /* fetch input records from a console input queue */
1575 DECL_HANDLER(read_console_input)
1577 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1578 reply->read = read_console_input( req->handle, count, req->flush );
1581 /* appends a string to console's history */
1582 DECL_HANDLER(append_console_input_history)
1584 struct console_input *console;
1586 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1587 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1588 release_object( console );
1591 /* appends a string to console's history */
1592 DECL_HANDLER(get_console_input_history)
1594 struct console_input *console;
1596 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1597 reply->total = console_input_get_hist( console, req->index );
1598 release_object( console );
1601 /* creates a screen buffer */
1602 DECL_HANDLER(create_console_output)
1604 struct console_input* console;
1605 struct screen_buffer* screen_buffer;
1606 int fd;
1608 if (req->fd != -1)
1610 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1612 set_error( STATUS_INVALID_HANDLE );
1613 return;
1616 else fd = -1;
1617 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1619 if (fd != -1) close( fd );
1620 return;
1622 if (console_input_is_bare( console ) ^ (fd != -1))
1624 if (fd != -1) close( fd );
1625 release_object( console );
1626 set_error( STATUS_INVALID_HANDLE );
1627 return;
1630 screen_buffer = create_console_output( console, fd );
1631 if (screen_buffer)
1633 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1634 * see file.c on how this could be done */
1635 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1636 release_object( screen_buffer );
1638 release_object( console );
1641 /* set info about a console screen buffer */
1642 DECL_HANDLER(set_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_WRITE_PROPERTIES, &screen_buffer_ops)))
1649 set_console_output_info( screen_buffer, req );
1650 release_object( screen_buffer );
1654 /* get info about a console screen buffer */
1655 DECL_HANDLER(get_console_output_info)
1657 struct screen_buffer *screen_buffer;
1659 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1660 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1662 reply->cursor_size = screen_buffer->cursor_size;
1663 reply->cursor_visible = screen_buffer->cursor_visible;
1664 reply->cursor_x = screen_buffer->cursor_x;
1665 reply->cursor_y = screen_buffer->cursor_y;
1666 reply->width = screen_buffer->width;
1667 reply->height = screen_buffer->height;
1668 reply->attr = screen_buffer->attr;
1669 reply->win_left = screen_buffer->win.left;
1670 reply->win_top = screen_buffer->win.top;
1671 reply->win_right = screen_buffer->win.right;
1672 reply->win_bottom = screen_buffer->win.bottom;
1673 reply->max_width = screen_buffer->max_width;
1674 reply->max_height = screen_buffer->max_height;
1675 release_object( screen_buffer );
1679 /* read data (chars & attrs) from a screen buffer */
1680 DECL_HANDLER(read_console_output)
1682 struct screen_buffer *screen_buffer;
1684 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1685 FILE_READ_DATA, &screen_buffer_ops )))
1687 if (console_input_is_bare( screen_buffer->input ))
1689 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1690 release_object( screen_buffer );
1691 return;
1693 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1694 reply->width = screen_buffer->width;
1695 reply->height = screen_buffer->height;
1696 release_object( screen_buffer );
1700 /* write data (char and/or attrs) to a screen buffer */
1701 DECL_HANDLER(write_console_output)
1703 struct screen_buffer *screen_buffer;
1705 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1706 FILE_WRITE_DATA, &screen_buffer_ops)))
1708 if (console_input_is_bare( screen_buffer->input ))
1710 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1711 release_object( screen_buffer );
1712 return;
1714 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1715 req->mode, req->x, req->y, req->wrap );
1716 reply->width = screen_buffer->width;
1717 reply->height = screen_buffer->height;
1718 release_object( screen_buffer );
1722 /* fill a screen buffer with constant data (chars and/or attributes) */
1723 DECL_HANDLER(fill_console_output)
1725 struct screen_buffer *screen_buffer;
1727 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1728 FILE_WRITE_DATA, &screen_buffer_ops)))
1730 if (console_input_is_bare( screen_buffer->input ))
1732 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1733 release_object( screen_buffer );
1734 return;
1736 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1737 req->x, req->y, req->count, req->wrap );
1738 release_object( screen_buffer );
1742 /* move a rect of data in a screen buffer */
1743 DECL_HANDLER(move_console_output)
1745 struct screen_buffer *screen_buffer;
1747 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1748 FILE_WRITE_DATA, &screen_buffer_ops)))
1750 if (console_input_is_bare( screen_buffer->input ))
1752 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1753 release_object( screen_buffer );
1754 return;
1756 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1757 req->w, req->h );
1758 release_object( screen_buffer );
1762 /* sends a signal to a console (process, group...) */
1763 DECL_HANDLER(send_console_signal)
1765 process_id_t group;
1767 group = req->group_id ? req->group_id : current->process->group_id;
1769 if (!group)
1770 set_error( STATUS_INVALID_PARAMETER );
1771 else
1772 propagate_console_signal( current->process->console, req->signal, group );
1775 /* get console which renderer is 'current' */
1776 static int cgwe_enum( struct process* process, void* user)
1778 if (process->console && process->console->renderer == current)
1780 *(struct console_input**)user = process->console;
1781 return 1;
1783 return 0;
1786 DECL_HANDLER(get_console_wait_event)
1788 struct console_input* console = NULL;
1790 if (current->process->console)
1791 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1792 else enum_processes(cgwe_enum, &console);
1794 if (console)
1796 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1797 release_object( console );
1799 else set_error( STATUS_INVALID_PARAMETER );