hidclass.sys: Implement IOCTL_HID_GET_SERIALNUMBER_STRING.
[wine.git] / server / console.c
blob78e91d6c90f64031b4d5343f00afdc18c5da2713
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 console_get_fd_type, /* get_fd_type */
176 no_fd_read, /* read */
177 no_fd_write, /* write */
178 no_fd_flush, /* flush */
179 default_fd_ioctl, /* ioctl */
180 default_fd_queue_async, /* queue_async */
181 default_fd_reselect_async, /* reselect_async */
182 default_fd_cancel_async /* cancel_async */
185 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
187 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
189 static int console_input_is_bare( struct console_input* cin )
191 return cin->evt == NULL;
194 static struct fd *console_input_get_fd( struct object* obj )
196 struct console_input *console_input = (struct console_input*)obj;
197 assert( obj->ops == &console_input_ops );
198 if (console_input->fd)
199 return (struct fd*)grab_object( console_input->fd );
200 set_error( STATUS_OBJECT_TYPE_MISMATCH );
201 return NULL;
204 static enum server_fd_type console_get_fd_type( struct fd *fd )
206 return FD_TYPE_CHAR;
209 /* dumps the renderer events of a console */
210 static void console_input_events_dump( struct object *obj, int verbose )
212 struct console_input_events *evts = (struct console_input_events *)obj;
213 assert( obj->ops == &console_input_events_ops );
214 fprintf( stderr, "Console input events: %d/%d events\n",
215 evts->num_used, evts->num_alloc );
218 /* destroys the renderer events of a console */
219 static void console_input_events_destroy( struct object *obj )
221 struct console_input_events *evts = (struct console_input_events *)obj;
222 assert( obj->ops == &console_input_events_ops );
223 free( evts->events );
226 /* the renderer events list is signaled when it's not empty */
227 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
229 struct console_input_events *evts = (struct console_input_events *)obj;
230 assert( obj->ops == &console_input_events_ops );
231 return (evts->num_used != 0);
234 /* add an event to the console's renderer events list */
235 static void console_input_events_append( struct console_input* console,
236 struct console_renderer_event* evt)
238 struct console_input_events* evts;
239 int collapsed = FALSE;
241 if (!(evts = console->evt)) return;
242 /* to be done even when evt has been generated by the renderer ? */
244 /* try to collapse evt into current queue's events */
245 if (evts->num_used)
247 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
249 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
250 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
252 /* if two update events overlap, collapse them into a single one */
253 if (last->u.update.bottom + 1 >= evt->u.update.top &&
254 evt->u.update.bottom + 1 >= last->u.update.top)
256 last->u.update.top = min(last->u.update.top, evt->u.update.top);
257 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
258 collapsed = TRUE;
262 if (!collapsed)
264 if (evts->num_used == evts->num_alloc)
266 evts->num_alloc += 16;
267 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
268 assert(evts->events);
270 evts->events[evts->num_used++] = *evt;
272 wake_up( &evts->obj, 0 );
275 /* retrieves events from the console's renderer events list */
276 static void console_input_events_get( struct console_input_events* evts )
278 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
280 if (num > evts->num_used) num = evts->num_used;
281 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
282 if (num < evts->num_used)
284 memmove( &evts->events[0], &evts->events[num],
285 (evts->num_used - num) * sizeof(evts->events[0]) );
287 evts->num_used -= num;
290 static struct console_input_events *create_console_input_events(void)
292 struct console_input_events* evt;
294 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
295 evt->num_alloc = evt->num_used = 0;
296 evt->events = NULL;
297 return evt;
300 static struct object *create_console_input( struct thread* renderer, int fd )
302 struct console_input *console_input;
304 if (!(console_input = alloc_object( &console_input_ops )))
306 if (fd != -1) close( fd );
307 return NULL;
309 console_input->renderer = renderer;
310 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
311 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
312 ENABLE_EXTENDED_FLAGS;
313 console_input->num_proc = 0;
314 console_input->active = NULL;
315 console_input->recnum = 0;
316 console_input->records = NULL;
317 console_input->evt = renderer ? create_console_input_events() : NULL;
318 console_input->title = NULL;
319 console_input->history_size = 50;
320 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
321 console_input->history_index = 0;
322 console_input->history_mode = 0;
323 console_input->edition_mode = 0;
324 console_input->input_cp = 0;
325 console_input->output_cp = 0;
326 console_input->win = 0;
327 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
328 console_input->fd = NULL;
330 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
332 if (fd != -1) close( fd );
333 console_input->history_size = 0;
334 release_object( console_input );
335 return NULL;
337 if (fd != -1) /* bare console */
339 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
340 FILE_SYNCHRONOUS_IO_NONALERT )))
342 release_object( console_input );
343 return NULL;
345 allow_fd_caching( console_input->fd );
348 return &console_input->obj;
351 static void generate_sb_initial_events( struct console_input *console_input )
353 struct screen_buffer *screen_buffer = console_input->active;
354 struct console_renderer_event evt;
356 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
357 memset(&evt.u, 0, sizeof(evt.u));
358 console_input_events_append( console_input, &evt );
360 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
361 evt.u.resize.width = screen_buffer->width;
362 evt.u.resize.height = screen_buffer->height;
363 console_input_events_append( console_input, &evt );
365 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
366 evt.u.display.left = screen_buffer->win.left;
367 evt.u.display.top = screen_buffer->win.top;
368 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
369 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
370 console_input_events_append( console_input, &evt );
372 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
373 evt.u.update.top = 0;
374 evt.u.update.bottom = screen_buffer->height - 1;
375 console_input_events_append( console_input, &evt );
377 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
378 evt.u.cursor_geom.size = screen_buffer->cursor_size;
379 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
380 console_input_events_append( console_input, &evt );
382 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
383 evt.u.cursor_pos.x = screen_buffer->cursor_x;
384 evt.u.cursor_pos.y = screen_buffer->cursor_y;
385 console_input_events_append( console_input, &evt );
388 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
390 struct screen_buffer *screen_buffer;
391 int i;
393 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
395 if (fd != -1) close( fd );
396 return NULL;
398 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
399 screen_buffer->input = console_input;
400 screen_buffer->cursor_size = 100;
401 screen_buffer->cursor_visible = 1;
402 screen_buffer->width = 80;
403 screen_buffer->height = 150;
404 screen_buffer->max_width = 80;
405 screen_buffer->max_height = 25;
406 screen_buffer->cursor_x = 0;
407 screen_buffer->cursor_y = 0;
408 screen_buffer->attr = 0x0F;
409 screen_buffer->win.left = 0;
410 screen_buffer->win.right = screen_buffer->max_width - 1;
411 screen_buffer->win.top = 0;
412 screen_buffer->win.bottom = screen_buffer->max_height - 1;
413 screen_buffer->data = NULL;
414 list_add_head( &screen_buffer_list, &screen_buffer->entry );
416 if (fd == -1)
417 screen_buffer->fd = NULL;
418 else
420 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
421 FILE_SYNCHRONOUS_IO_NONALERT )))
423 release_object( screen_buffer );
424 return NULL;
426 allow_fd_caching(screen_buffer->fd);
429 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
430 sizeof(*screen_buffer->data) )))
432 release_object( screen_buffer );
433 return NULL;
435 /* clear the first row */
436 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
437 /* and copy it to all other rows */
438 for (i = 1; i < screen_buffer->height; i++)
439 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
440 screen_buffer->width * sizeof(char_info_t) );
442 if (!console_input->active)
444 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
445 generate_sb_initial_events( console_input );
447 return screen_buffer;
450 /* free the console for this process */
451 int free_console( struct process *process )
453 struct console_input* console = process->console;
455 if (!console) return 0;
457 process->console = NULL;
458 if (--console->num_proc == 0 && console->renderer)
460 /* all processes have terminated... tell the renderer to terminate too */
461 struct console_renderer_event evt;
462 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
463 memset(&evt.u, 0, sizeof(evt.u));
464 console_input_events_append( console, &evt );
466 release_object( console );
468 return 1;
471 /* let process inherit the console from parent... this handle two cases :
472 * 1/ generic console inheritance
473 * 2/ parent is a renderer which launches process, and process should attach to the console
474 * renderered by parent
476 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
478 int done = 0;
479 struct process* parent = parent_thread->process;
481 /* if parent is a renderer, then attach current process to its console
482 * a bit hacky....
484 if (hconin)
486 struct console_input* console;
488 /* FIXME: should we check some access rights ? */
489 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
490 0, &console_input_ops )))
492 if (console->renderer == parent_thread)
494 process->console = (struct console_input*)grab_object( console );
495 process->console->num_proc++;
496 done = 1;
498 release_object( console );
500 else clear_error(); /* ignore error */
502 /* otherwise, if parent has a console, attach child to this console */
503 if (!done && parent->console)
505 process->console = (struct console_input*)grab_object( parent->console );
506 process->console->num_proc++;
510 struct thread *console_get_renderer( struct console_input *console )
512 return console->renderer;
515 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
517 struct console_input* console = NULL;
519 if (handle)
520 console = (struct console_input *)get_handle_obj( current->process, handle,
521 access, &console_input_ops );
522 else if (current->process->console)
524 console = (struct console_input *)grab_object( current->process->console );
527 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
528 return console;
531 struct console_signal_info
533 struct console_input *console;
534 process_id_t group;
535 int signal;
538 static int propagate_console_signal_cb(struct process *process, void *user)
540 struct console_signal_info* csi = (struct console_signal_info*)user;
542 if (process->console == csi->console && process->running_threads &&
543 (!csi->group || process->group_id == csi->group))
545 /* find a suitable thread to signal */
546 struct thread *thread;
547 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
549 if (send_thread_signal( thread, csi->signal )) break;
552 return FALSE;
555 static void propagate_console_signal( struct console_input *console,
556 int sig, process_id_t group_id )
558 struct console_signal_info csi;
560 if (!console)
562 set_error( STATUS_INVALID_PARAMETER );
563 return;
565 /* FIXME: should support the other events (like CTRL_BREAK) */
566 if (sig != CTRL_C_EVENT)
568 set_error( STATUS_NOT_IMPLEMENTED );
569 return;
571 csi.console = console;
572 csi.signal = SIGINT;
573 csi.group = group_id;
575 enum_processes(propagate_console_signal_cb, &csi);
578 static int get_console_mode( obj_handle_t handle )
580 struct object *obj;
581 int ret = 0;
583 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
585 if (obj->ops == &console_input_ops)
587 ret = ((struct console_input *)obj)->mode;
589 else if (obj->ops == &screen_buffer_ops)
591 ret = ((struct screen_buffer *)obj)->mode;
593 else
594 set_error( STATUS_OBJECT_TYPE_MISMATCH );
595 release_object( obj );
597 return ret;
600 /* changes the mode of either a console input or a screen buffer */
601 static int set_console_mode( obj_handle_t handle, int mode )
603 struct object *obj;
604 int ret = 0;
606 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
607 return 0;
608 if (obj->ops == &console_input_ops)
610 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
611 ((struct console_input *)obj)->mode = mode;
612 ret = 1;
614 else if (obj->ops == &screen_buffer_ops)
616 ((struct screen_buffer *)obj)->mode = mode;
617 ret = 1;
619 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
620 release_object( obj );
621 return ret;
624 /* add input events to a console input queue */
625 static int write_console_input( struct console_input* console, int count,
626 const INPUT_RECORD *records )
628 INPUT_RECORD *new_rec;
630 if (!count) return 0;
631 if (!(new_rec = realloc( console->records,
632 (console->recnum + count) * sizeof(INPUT_RECORD) )))
634 set_error( STATUS_NO_MEMORY );
635 return -1;
637 console->records = new_rec;
638 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
640 if (console->mode & ENABLE_PROCESSED_INPUT)
642 int i = 0;
643 while (i < count)
645 if (records[i].EventType == KEY_EVENT &&
646 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
647 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
649 if (i != count - 1)
650 memcpy( &console->records[console->recnum + i],
651 &console->records[console->recnum + i + 1],
652 (count - i - 1) * sizeof(INPUT_RECORD) );
653 count--;
654 if (records[i].Event.KeyEvent.bKeyDown)
656 /* send SIGINT to all processes attached to this console */
657 propagate_console_signal( console, CTRL_C_EVENT, 0 );
660 else i++;
663 if (!console->recnum && count) set_event( console->event );
664 console->recnum += count;
665 return count;
668 /* retrieve a pointer to the console input records */
669 static int read_console_input( obj_handle_t handle, int count, int flush )
671 struct console_input *console;
673 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
674 FILE_READ_DATA, &console_input_ops )))
675 return -1;
677 if (!count)
679 /* special case: do not retrieve anything, but return
680 * the total number of records available */
681 count = console->recnum;
683 else
685 if (count > console->recnum) count = console->recnum;
686 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
688 if (flush)
690 int i;
691 for (i = count; i < console->recnum; i++)
692 console->records[i-count] = console->records[i];
693 if ((console->recnum -= count) > 0)
695 INPUT_RECORD *new_rec = realloc( console->records,
696 console->recnum * sizeof(INPUT_RECORD) );
697 if (new_rec) console->records = new_rec;
699 else
701 free( console->records );
702 console->records = NULL;
703 reset_event( console->event );
706 release_object( console );
707 return count;
710 /* set misc console input information */
711 static int set_console_input_info( const struct set_console_input_info_request *req,
712 const WCHAR *title, data_size_t len )
714 struct console_input *console;
715 struct console_renderer_event evt;
717 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
718 if (console_input_is_bare(console) &&
719 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
720 SET_CONSOLE_INPUT_INFO_WIN)))
722 set_error( STATUS_UNSUCCESSFUL );
723 goto error;
726 memset(&evt.u, 0, sizeof(evt.u));
727 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
729 struct screen_buffer *screen_buffer;
731 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
732 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
733 if (!screen_buffer || screen_buffer->input != console)
735 set_error( STATUS_INVALID_HANDLE );
736 if (screen_buffer) release_object( screen_buffer );
737 goto error;
740 if (screen_buffer != console->active)
742 if (console->active) release_object( console->active );
743 console->active = screen_buffer;
744 generate_sb_initial_events( console );
746 else
747 release_object( screen_buffer );
749 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
751 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
752 if (new_title)
754 memcpy( new_title, title, len );
755 new_title[len / sizeof(WCHAR)] = 0;
756 free( console->title );
757 console->title = new_title;
758 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
759 console_input_events_append( console, &evt );
762 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
764 console->history_mode = req->history_mode;
766 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
767 console->history_size != req->history_size)
769 WCHAR** mem = NULL;
770 int i;
771 int delta;
773 if (req->history_size)
775 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
776 if (!mem) goto error;
777 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
780 delta = (console->history_index > req->history_size) ?
781 (console->history_index - req->history_size) : 0;
783 for (i = delta; i < console->history_index; i++)
785 mem[i - delta] = console->history[i];
786 console->history[i] = NULL;
788 console->history_index -= delta;
790 for (i = 0; i < console->history_size; i++)
791 free( console->history[i] );
792 free( console->history );
793 console->history = mem;
794 console->history_size = req->history_size;
796 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
798 console->edition_mode = req->edition_mode;
800 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
802 console->input_cp = req->input_cp;
804 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
806 console->output_cp = req->output_cp;
808 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
810 console->win = req->win;
812 release_object( console );
813 return 1;
814 error:
815 if (console) release_object( console );
816 return 0;
819 /* resize a screen buffer */
820 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
821 int new_width, int new_height )
823 int i, old_width, old_height, copy_width, copy_height;
824 char_info_t *new_data;
826 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
828 set_error( STATUS_NO_MEMORY );
829 return 0;
831 old_width = screen_buffer->width;
832 old_height = screen_buffer->height;
833 copy_width = min( old_width, new_width );
834 copy_height = min( old_height, new_height );
836 /* copy all the rows */
837 for (i = 0; i < copy_height; i++)
839 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
840 copy_width * sizeof(char_info_t) );
843 /* clear the end of each row */
844 if (new_width > old_width)
846 /* fill first row */
847 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
848 /* and blast it to the other rows */
849 for (i = 1; i < copy_height; i++)
850 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
851 (new_width - old_width) * sizeof(char_info_t) );
854 /* clear remaining rows */
855 if (new_height > old_height)
857 /* fill first row */
858 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
859 /* and blast it to the other rows */
860 for (i = old_height+1; i < new_height; i++)
861 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
862 new_width * sizeof(char_info_t) );
864 free( screen_buffer->data );
865 screen_buffer->data = new_data;
866 screen_buffer->width = new_width;
867 screen_buffer->height = new_height;
868 return 1;
871 /* set misc screen buffer information */
872 static int set_console_output_info( struct screen_buffer *screen_buffer,
873 const struct set_console_output_info_request *req )
875 struct console_renderer_event evt;
877 memset(&evt.u, 0, sizeof(evt.u));
878 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
880 if (req->cursor_size < 1 || req->cursor_size > 100)
882 set_error( STATUS_INVALID_PARAMETER );
883 return 0;
885 if (screen_buffer->cursor_size != req->cursor_size ||
886 screen_buffer->cursor_visible != req->cursor_visible)
888 screen_buffer->cursor_size = req->cursor_size;
889 screen_buffer->cursor_visible = req->cursor_visible;
890 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
891 evt.u.cursor_geom.size = req->cursor_size;
892 evt.u.cursor_geom.visible = req->cursor_visible;
893 console_input_events_append( screen_buffer->input, &evt );
896 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
898 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
899 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
901 set_error( STATUS_INVALID_PARAMETER );
902 return 0;
904 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
906 screen_buffer->cursor_x = req->cursor_x;
907 screen_buffer->cursor_y = req->cursor_y;
908 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
909 evt.u.cursor_pos.x = req->cursor_x;
910 evt.u.cursor_pos.y = req->cursor_y;
911 console_input_events_append( screen_buffer->input, &evt );
914 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
916 unsigned cc;
918 /* new screen-buffer cannot be smaller than actual window */
919 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
920 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
922 set_error( STATUS_INVALID_PARAMETER );
923 return 0;
925 /* FIXME: there are also some basic minimum and max size to deal with */
926 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
928 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
929 evt.u.resize.width = req->width;
930 evt.u.resize.height = req->height;
931 console_input_events_append( screen_buffer->input, &evt );
933 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
934 evt.u.update.top = 0;
935 evt.u.update.bottom = screen_buffer->height - 1;
936 console_input_events_append( screen_buffer->input, &evt );
938 /* scroll window to display sb */
939 if (screen_buffer->win.right >= req->width)
941 screen_buffer->win.right -= screen_buffer->win.left;
942 screen_buffer->win.left = 0;
944 if (screen_buffer->win.bottom >= req->height)
946 screen_buffer->win.bottom -= screen_buffer->win.top;
947 screen_buffer->win.top = 0;
949 /* reset cursor if needed (normally, if cursor was outside of new sb, the
950 * window has been shifted so that the new position of the cursor will be
951 * visible */
952 cc = 0;
953 if (screen_buffer->cursor_x >= req->width)
955 screen_buffer->cursor_x = req->width - 1;
956 cc++;
958 if (screen_buffer->cursor_y >= req->height)
960 screen_buffer->cursor_y = req->height - 1;
961 cc++;
963 if (cc)
965 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
966 evt.u.cursor_pos.x = req->cursor_x;
967 evt.u.cursor_pos.y = req->cursor_y;
968 console_input_events_append( screen_buffer->input, &evt );
971 if (screen_buffer == screen_buffer->input->active &&
972 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
974 INPUT_RECORD ir;
975 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
976 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
977 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
978 write_console_input( screen_buffer->input, 1, &ir );
981 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
983 screen_buffer->attr = req->attr;
985 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
987 if (req->win_left < 0 || req->win_left > req->win_right ||
988 req->win_right >= screen_buffer->width ||
989 req->win_top < 0 || req->win_top > req->win_bottom ||
990 req->win_bottom >= screen_buffer->height)
992 set_error( STATUS_INVALID_PARAMETER );
993 return 0;
995 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
996 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
998 screen_buffer->win.left = req->win_left;
999 screen_buffer->win.top = req->win_top;
1000 screen_buffer->win.right = req->win_right;
1001 screen_buffer->win.bottom = req->win_bottom;
1002 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1003 evt.u.display.left = req->win_left;
1004 evt.u.display.top = req->win_top;
1005 evt.u.display.width = req->win_right - req->win_left + 1;
1006 evt.u.display.height = req->win_bottom - req->win_top + 1;
1007 console_input_events_append( screen_buffer->input, &evt );
1010 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1012 /* can only be done by renderer */
1013 if (current->process->console != screen_buffer->input)
1015 set_error( STATUS_INVALID_PARAMETER );
1016 return 0;
1019 screen_buffer->max_width = req->max_width;
1020 screen_buffer->max_height = req->max_height;
1023 return 1;
1026 /* appends a new line to history (history is a fixed size array) */
1027 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1029 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1031 if (!ptr)
1032 return;
1034 if (!console || !console->history_size)
1036 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1037 free( ptr );
1038 return;
1041 memcpy( ptr, buf, len * sizeof(WCHAR) );
1042 ptr[len] = 0;
1044 if (console->history_mode && console->history_index &&
1045 strncmpW( console->history[console->history_index - 1], ptr, len ) == 0)
1047 /* ok, mode ask us to not use twice the same string...
1048 * so just free mem and returns
1050 set_error( STATUS_ALIAS_EXISTS );
1051 free(ptr);
1052 return;
1055 if (console->history_index < console->history_size)
1057 console->history[console->history_index++] = ptr;
1059 else
1061 free( console->history[0]) ;
1062 memmove( &console->history[0], &console->history[1],
1063 (console->history_size - 1) * sizeof(WCHAR*) );
1064 console->history[console->history_size - 1] = ptr;
1068 /* returns a line from the cache */
1069 static data_size_t console_input_get_hist( struct console_input *console, int index )
1071 data_size_t ret = 0;
1073 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1074 else
1076 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1077 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1079 return ret;
1082 /* dumb dump */
1083 static void console_input_dump( struct object *obj, int verbose )
1085 struct console_input *console = (struct console_input *)obj;
1086 assert( obj->ops == &console_input_ops );
1087 fprintf( stderr, "Console input active=%p evt=%p\n",
1088 console->active, console->evt );
1091 static void console_input_destroy( struct object *obj )
1093 struct console_input* console_in = (struct console_input *)obj;
1094 struct screen_buffer* curr;
1095 int i;
1097 assert( obj->ops == &console_input_ops );
1098 free( console_in->title );
1099 free( console_in->records );
1101 if (console_in->active) release_object( console_in->active );
1102 console_in->active = NULL;
1104 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1106 if (curr->input == console_in) curr->input = NULL;
1109 if (console_in->evt)
1111 release_object( console_in->evt );
1112 console_in->evt = NULL;
1114 if (console_in->event)
1115 release_object( console_in->event );
1116 if (console_in->fd)
1117 release_object( console_in->fd );
1119 for (i = 0; i < console_in->history_size; i++)
1120 free( console_in->history[i] );
1121 free( console_in->history );
1124 static void screen_buffer_dump( struct object *obj, int verbose )
1126 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1127 assert( obj->ops == &screen_buffer_ops );
1129 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1132 static void screen_buffer_destroy( struct object *obj )
1134 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1136 assert( obj->ops == &screen_buffer_ops );
1138 list_remove( &screen_buffer->entry );
1140 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1142 struct screen_buffer *sb;
1144 screen_buffer->input->active = NULL;
1145 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1147 if (sb->input == screen_buffer->input)
1149 sb->input->active = sb;
1150 break;
1154 if (screen_buffer->fd) release_object( screen_buffer->fd );
1155 free( screen_buffer->data );
1158 static struct fd *screen_buffer_get_fd( struct object *obj )
1160 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1161 assert( obj->ops == &screen_buffer_ops );
1162 if (screen_buffer->fd)
1163 return (struct fd*)grab_object( screen_buffer->fd );
1164 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1165 return NULL;
1168 /* write data into a screen buffer */
1169 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1170 const void* data, enum char_info_mode mode,
1171 int x, int y, int wrap )
1173 unsigned int i;
1174 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1176 if (y >= screen_buffer->height) return 0;
1178 if (wrap)
1179 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1180 else
1181 end = screen_buffer->data + (y+1) * screen_buffer->width;
1183 switch(mode)
1185 case CHAR_INFO_MODE_TEXT:
1187 const WCHAR *ptr = data;
1188 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1190 break;
1191 case CHAR_INFO_MODE_ATTR:
1193 const unsigned short *ptr = data;
1194 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1196 break;
1197 case CHAR_INFO_MODE_TEXTATTR:
1199 const char_info_t *ptr = data;
1200 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1202 break;
1203 case CHAR_INFO_MODE_TEXTSTDATTR:
1205 const WCHAR *ptr = data;
1206 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1208 dest->ch = ptr[i];
1209 dest->attr = screen_buffer->attr;
1212 break;
1213 default:
1214 set_error( STATUS_INVALID_PARAMETER );
1215 return 0;
1218 if (i && screen_buffer == screen_buffer->input->active)
1220 struct console_renderer_event evt;
1221 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1222 memset(&evt.u, 0, sizeof(evt.u));
1223 evt.u.update.top = y + x / screen_buffer->width;
1224 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1225 console_input_events_append( screen_buffer->input, &evt );
1227 return i;
1230 /* fill a screen buffer with uniform data */
1231 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1232 enum char_info_mode mode, int x, int y, int count, int wrap )
1234 int i;
1235 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1237 if (y >= screen_buffer->height) return 0;
1239 if (wrap)
1240 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1241 else
1242 end = screen_buffer->data + (y+1) * screen_buffer->width;
1244 if (count > end - dest) count = end - dest;
1246 switch(mode)
1248 case CHAR_INFO_MODE_TEXT:
1249 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1250 break;
1251 case CHAR_INFO_MODE_ATTR:
1252 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1253 break;
1254 case CHAR_INFO_MODE_TEXTATTR:
1255 for (i = 0; i < count; i++) dest[i] = data;
1256 break;
1257 case CHAR_INFO_MODE_TEXTSTDATTR:
1258 for (i = 0; i < count; i++)
1260 dest[i].ch = data.ch;
1261 dest[i].attr = screen_buffer->attr;
1263 break;
1264 default:
1265 set_error( STATUS_INVALID_PARAMETER );
1266 return 0;
1269 if (count && screen_buffer == screen_buffer->input->active)
1271 struct console_renderer_event evt;
1272 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1273 memset(&evt.u, 0, sizeof(evt.u));
1274 evt.u.update.top = y;
1275 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1276 console_input_events_append( screen_buffer->input, &evt );
1278 return i;
1281 /* read data from a screen buffer */
1282 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1283 enum char_info_mode mode, int wrap )
1285 int i;
1286 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1288 if (y >= screen_buffer->height) return;
1290 if (wrap)
1291 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1292 else
1293 end = screen_buffer->data + (y+1) * screen_buffer->width;
1295 switch(mode)
1297 case CHAR_INFO_MODE_TEXT:
1299 WCHAR *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].ch;
1306 break;
1307 case CHAR_INFO_MODE_ATTR:
1309 unsigned short *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].attr;
1316 break;
1317 case CHAR_INFO_MODE_TEXTATTR:
1319 char_info_t *data;
1320 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1321 if ((data = set_reply_data_size( count * sizeof(*data) )))
1323 for (i = 0; i < count; i++) data[i] = src[i];
1326 break;
1327 default:
1328 set_error( STATUS_INVALID_PARAMETER );
1329 break;
1333 /* scroll parts of a screen buffer */
1334 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1335 int w, int h )
1337 int j;
1338 char_info_t *psrc, *pdst;
1339 struct console_renderer_event evt;
1341 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1342 xsrc + w > screen_buffer->width ||
1343 xdst + w > screen_buffer->width ||
1344 ysrc + h > screen_buffer->height ||
1345 ydst + h > screen_buffer->height ||
1346 w == 0 || h == 0)
1348 set_error( STATUS_INVALID_PARAMETER );
1349 return;
1352 if (ysrc < ydst)
1354 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1355 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1357 for (j = h; j > 0; j--)
1359 memcpy(pdst, psrc, w * sizeof(*pdst) );
1360 pdst -= screen_buffer->width;
1361 psrc -= screen_buffer->width;
1364 else
1366 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1367 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1369 for (j = 0; j < h; j++)
1371 /* we use memmove here because when psrc and pdst are the same,
1372 * copies are done on the same row, so the dst and src blocks
1373 * can overlap */
1374 memmove( pdst, psrc, w * sizeof(*pdst) );
1375 pdst += screen_buffer->width;
1376 psrc += screen_buffer->width;
1380 /* FIXME: this could be enhanced, by signalling scroll */
1381 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1382 memset(&evt.u, 0, sizeof(evt.u));
1383 evt.u.update.top = min(ysrc, ydst);
1384 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1385 console_input_events_append( screen_buffer->input, &evt );
1388 /* allocate a console for the renderer */
1389 DECL_HANDLER(alloc_console)
1391 obj_handle_t in = 0;
1392 obj_handle_t evt = 0;
1393 struct process *process;
1394 struct thread *renderer;
1395 struct console_input *console;
1396 int fd;
1397 int attach = 0;
1399 if (req->input_fd != -1)
1401 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1403 set_error( STATUS_INVALID_PARAMETER );
1404 return;
1407 else fd = -1;
1409 switch (req->pid)
1411 case 0:
1412 /* renderer is current, console to be attached to parent process */
1413 renderer = current;
1414 if (!(process = current->process->parent))
1416 if (fd != -1) close( fd );
1417 set_error( STATUS_ACCESS_DENIED );
1418 return;
1420 grab_object( process );
1421 attach = 1;
1422 break;
1423 case 0xffffffff:
1424 /* no renderer, console to be attached to current process */
1425 renderer = NULL;
1426 process = current->process;
1427 grab_object( process );
1428 attach = 1;
1429 break;
1430 default:
1431 /* renderer is current, console to be attached to req->pid */
1432 renderer = current;
1433 if (!(process = get_process_from_id( req->pid )))
1435 if (fd != -1) close( fd );
1436 return;
1440 if (attach && process->console)
1442 if (fd != -1) close( fd );
1443 set_error( STATUS_ACCESS_DENIED );
1444 goto the_end;
1447 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1449 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1451 if (!console->evt ||
1452 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1454 if (attach)
1456 process->console = (struct console_input*)grab_object( console );
1457 console->num_proc++;
1459 reply->handle_in = in;
1460 reply->event = evt;
1461 release_object( console );
1462 goto the_end;
1464 close_handle( current->process, in );
1466 release_object( console );
1468 the_end:
1469 release_object( process );
1472 /* free the console of the current process */
1473 DECL_HANDLER(free_console)
1475 free_console( current->process );
1478 /* let the renderer peek the events it's waiting on */
1479 DECL_HANDLER(get_console_renderer_events)
1481 struct console_input_events *evt;
1483 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1484 FILE_READ_PROPERTIES, &console_input_events_ops );
1485 if (!evt) return;
1486 console_input_events_get( evt );
1487 release_object( evt );
1490 /* open a handle to the process console */
1491 DECL_HANDLER(open_console)
1493 struct object *obj = NULL;
1495 reply->handle = 0;
1496 if (!req->from)
1498 if (current->process->console)
1499 obj = grab_object( (struct object*)current->process->console );
1501 else if (req->from == (obj_handle_t)1)
1503 if (current->process->console && current->process->console->active)
1504 obj = grab_object( (struct object*)current->process->console->active );
1506 else if ((obj = get_handle_obj( current->process, req->from,
1507 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1509 struct console_input *console = (struct console_input *)obj;
1510 obj = (console->active) ? grab_object( console->active ) : NULL;
1511 release_object( console );
1514 /* FIXME: req->share is not used (as in screen buffer creation) */
1515 if (obj)
1517 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1518 release_object( obj );
1520 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1523 /* set info about a console input */
1524 DECL_HANDLER(set_console_input_info)
1526 set_console_input_info( req, get_req_data(), get_req_data_size() );
1529 /* get info about a console (output only) */
1530 DECL_HANDLER(get_console_input_info)
1532 struct console_input *console;
1534 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1535 if (console->title)
1537 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1538 if (len > get_reply_max_size()) len = get_reply_max_size();
1539 set_reply_data( console->title, len );
1541 reply->history_mode = console->history_mode;
1542 reply->history_size = console->history_size;
1543 reply->history_index = console->history_index;
1544 reply->edition_mode = console->edition_mode;
1545 reply->input_cp = console->input_cp;
1546 reply->output_cp = console->output_cp;
1547 reply->win = console->win;
1549 release_object( console );
1552 /* get a console mode (input or output) */
1553 DECL_HANDLER(get_console_mode)
1555 reply->mode = get_console_mode( req->handle );
1558 /* set a console mode (input or output) */
1559 DECL_HANDLER(set_console_mode)
1561 set_console_mode( req->handle, req->mode );
1564 /* add input records to a console input queue */
1565 DECL_HANDLER(write_console_input)
1567 struct console_input *console;
1569 reply->written = 0;
1570 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1571 FILE_WRITE_PROPERTIES, &console_input_ops )))
1572 return;
1573 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1574 get_req_data() );
1575 release_object( console );
1578 /* fetch input records from a console input queue */
1579 DECL_HANDLER(read_console_input)
1581 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1582 reply->read = read_console_input( req->handle, count, req->flush );
1585 /* appends a string to console's history */
1586 DECL_HANDLER(append_console_input_history)
1588 struct console_input *console;
1590 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1591 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1592 release_object( console );
1595 /* appends a string to console's history */
1596 DECL_HANDLER(get_console_input_history)
1598 struct console_input *console;
1600 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1601 reply->total = console_input_get_hist( console, req->index );
1602 release_object( console );
1605 /* creates a screen buffer */
1606 DECL_HANDLER(create_console_output)
1608 struct console_input* console;
1609 struct screen_buffer* screen_buffer;
1610 int fd;
1612 if (req->fd != -1)
1614 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1616 set_error( STATUS_INVALID_HANDLE );
1617 return;
1620 else fd = -1;
1621 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1623 if (fd != -1) close( fd );
1624 return;
1626 if (console_input_is_bare( console ) ^ (fd != -1))
1628 if (fd != -1) close( fd );
1629 release_object( console );
1630 set_error( STATUS_INVALID_HANDLE );
1631 return;
1634 screen_buffer = create_console_output( console, fd );
1635 if (screen_buffer)
1637 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1638 * see file.c on how this could be done */
1639 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1640 release_object( screen_buffer );
1642 release_object( console );
1645 /* set info about a console screen buffer */
1646 DECL_HANDLER(set_console_output_info)
1648 struct screen_buffer *screen_buffer;
1650 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1651 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1653 set_console_output_info( screen_buffer, req );
1654 release_object( screen_buffer );
1658 /* get info about a console screen buffer */
1659 DECL_HANDLER(get_console_output_info)
1661 struct screen_buffer *screen_buffer;
1663 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1664 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1666 reply->cursor_size = screen_buffer->cursor_size;
1667 reply->cursor_visible = screen_buffer->cursor_visible;
1668 reply->cursor_x = screen_buffer->cursor_x;
1669 reply->cursor_y = screen_buffer->cursor_y;
1670 reply->width = screen_buffer->width;
1671 reply->height = screen_buffer->height;
1672 reply->attr = screen_buffer->attr;
1673 reply->win_left = screen_buffer->win.left;
1674 reply->win_top = screen_buffer->win.top;
1675 reply->win_right = screen_buffer->win.right;
1676 reply->win_bottom = screen_buffer->win.bottom;
1677 reply->max_width = screen_buffer->max_width;
1678 reply->max_height = screen_buffer->max_height;
1679 release_object( screen_buffer );
1683 /* read data (chars & attrs) from a screen buffer */
1684 DECL_HANDLER(read_console_output)
1686 struct screen_buffer *screen_buffer;
1688 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1689 FILE_READ_DATA, &screen_buffer_ops )))
1691 if (console_input_is_bare( screen_buffer->input ))
1693 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1694 release_object( screen_buffer );
1695 return;
1697 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1698 reply->width = screen_buffer->width;
1699 reply->height = screen_buffer->height;
1700 release_object( screen_buffer );
1704 /* write data (char and/or attrs) to a screen buffer */
1705 DECL_HANDLER(write_console_output)
1707 struct screen_buffer *screen_buffer;
1709 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1710 FILE_WRITE_DATA, &screen_buffer_ops)))
1712 if (console_input_is_bare( screen_buffer->input ))
1714 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1715 release_object( screen_buffer );
1716 return;
1718 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1719 req->mode, req->x, req->y, req->wrap );
1720 reply->width = screen_buffer->width;
1721 reply->height = screen_buffer->height;
1722 release_object( screen_buffer );
1726 /* fill a screen buffer with constant data (chars and/or attributes) */
1727 DECL_HANDLER(fill_console_output)
1729 struct screen_buffer *screen_buffer;
1731 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1732 FILE_WRITE_DATA, &screen_buffer_ops)))
1734 if (console_input_is_bare( screen_buffer->input ))
1736 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1737 release_object( screen_buffer );
1738 return;
1740 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1741 req->x, req->y, req->count, req->wrap );
1742 release_object( screen_buffer );
1746 /* move a rect of data in a screen buffer */
1747 DECL_HANDLER(move_console_output)
1749 struct screen_buffer *screen_buffer;
1751 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1752 FILE_WRITE_DATA, &screen_buffer_ops)))
1754 if (console_input_is_bare( screen_buffer->input ))
1756 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1757 release_object( screen_buffer );
1758 return;
1760 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1761 req->w, req->h );
1762 release_object( screen_buffer );
1766 /* sends a signal to a console (process, group...) */
1767 DECL_HANDLER(send_console_signal)
1769 process_id_t group;
1771 group = req->group_id ? req->group_id : current->process->group_id;
1773 if (!group)
1774 set_error( STATUS_INVALID_PARAMETER );
1775 else
1776 propagate_console_signal( current->process->console, req->signal, group );
1779 /* get console which renderer is 'current' */
1780 static int cgwe_enum( struct process* process, void* user)
1782 if (process->console && process->console->renderer == current)
1784 *(struct console_input**)user = process->console;
1785 return 1;
1787 return 0;
1790 DECL_HANDLER(get_console_wait_event)
1792 struct console_input* console = NULL;
1794 if (current->process->console)
1795 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1796 else enum_processes(cgwe_enum, &console);
1798 if (console)
1800 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1801 release_object( console );
1803 else set_error( STATUS_INVALID_PARAMETER );