kernel32: Update English resource.
[wine/testsucceed.git] / server / console.c
blob3f9093729675391ccf8d1db74a822a87e275f82c
1 /*
2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
5 * 2001 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "handle.h"
35 #include "process.h"
36 #include "request.h"
37 #include "file.h"
38 #include "unicode.h"
39 #include "wincon.h"
40 #include "winternl.h"
42 struct screen_buffer;
43 struct console_input_events;
45 struct console_input
47 struct object obj; /* object header */
48 int num_proc; /* number of processes attached to this console */
49 struct thread *renderer; /* console renderer thread */
50 int mode; /* input mode */
51 struct screen_buffer *active; /* active screen buffer */
52 int recnum; /* number of input records */
53 INPUT_RECORD *records; /* input records */
54 struct console_input_events *evt; /* synchronization event with renderer */
55 WCHAR *title; /* console title */
56 WCHAR **history; /* lines history */
57 int history_size; /* number of entries in history array */
58 int history_index; /* number of used entries in history array */
59 int history_mode; /* mode of history (non zero means remove doubled strings */
60 int edition_mode; /* index to edition mode flavors */
61 int input_cp; /* console input codepage */
62 int output_cp; /* console output codepage */
63 user_handle_t win; /* window handle if backend supports it */
64 struct event *event; /* event to wait on for input queue */
65 struct fd *fd; /* for bare console, attached input fd */
68 static void console_input_dump( struct object *obj, int verbose );
69 static void console_input_destroy( struct object *obj );
70 static struct fd *console_input_get_fd( struct object *obj );
72 static const struct object_ops console_input_ops =
74 sizeof(struct console_input), /* size */
75 console_input_dump, /* dump */
76 no_get_type, /* get_type */
77 no_add_queue, /* add_queue */
78 NULL, /* remove_queue */
79 NULL, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 console_input_get_fd, /* get_fd */
83 default_fd_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_open_file, /* open_file */
88 no_close_handle, /* close_handle */
89 console_input_destroy /* destroy */
92 static void console_input_events_dump( struct object *obj, int verbose );
93 static void console_input_events_destroy( struct object *obj );
94 static int console_input_events_signaled( struct object *obj, struct thread *thread );
96 struct console_input_events
98 struct object obj; /* object header */
99 int num_alloc; /* number of allocated events */
100 int num_used; /* number of actually used events */
101 struct console_renderer_event* events;
104 static const struct object_ops console_input_events_ops =
106 sizeof(struct console_input_events), /* size */
107 console_input_events_dump, /* dump */
108 no_get_type, /* get_type */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 console_input_events_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 default_fd_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 no_close_handle, /* close_handle */
121 console_input_events_destroy /* destroy */
124 struct screen_buffer
126 struct object obj; /* object header */
127 struct list entry; /* entry in list of all screen buffers */
128 struct console_input *input; /* associated console input */
129 int mode; /* output mode */
130 int cursor_size; /* size of cursor (percentage filled) */
131 int cursor_visible;/* cursor visibility flag */
132 int cursor_x; /* position of cursor */
133 int cursor_y; /* position of cursor */
134 int width; /* size (w-h) of the screen buffer */
135 int height;
136 int max_width; /* size (w-h) of the window given font size */
137 int max_height;
138 char_info_t *data; /* the data for each cell - a width x height matrix */
139 unsigned short attr; /* default attribute for screen buffer */
140 rectangle_t win; /* current visible window on the screen buffer *
141 * as seen in wineconsole */
142 struct fd *fd; /* for bare console, attached output fd */
145 static void screen_buffer_dump( struct object *obj, int verbose );
146 static void screen_buffer_destroy( struct object *obj );
147 static struct fd *screen_buffer_get_fd( struct object *obj );
149 static const struct object_ops screen_buffer_ops =
151 sizeof(struct screen_buffer), /* size */
152 screen_buffer_dump, /* dump */
153 no_get_type, /* get_type */
154 no_add_queue, /* add_queue */
155 NULL, /* remove_queue */
156 NULL, /* signaled */
157 NULL, /* satisfied */
158 no_signal, /* signal */
159 screen_buffer_get_fd, /* get_fd */
160 default_fd_map_access, /* map_access */
161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
163 no_lookup_name, /* lookup_name */
164 no_open_file, /* open_file */
165 no_close_handle, /* close_handle */
166 screen_buffer_destroy /* destroy */
169 static enum server_fd_type console_get_fd_type( struct fd *fd );
171 static const struct fd_ops console_fd_ops =
173 default_fd_get_poll_events, /* get_poll_events */
174 default_poll_event, /* poll_event */
175 no_flush, /* flush */
176 console_get_fd_type, /* get_fd_type */
177 default_fd_ioctl, /* ioctl */
178 default_fd_queue_async, /* queue_async */
179 default_fd_reselect_async, /* reselect_async */
180 default_fd_cancel_async /* cancel_async */
183 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
185 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
187 static int console_input_is_bare( struct console_input* cin )
189 return cin->evt == NULL;
192 static struct fd *console_input_get_fd( struct object* obj )
194 struct console_input *console_input = (struct console_input*)obj;
195 assert( obj->ops == &console_input_ops );
196 if (console_input->fd)
197 return (struct fd*)grab_object( console_input->fd );
198 set_error( STATUS_OBJECT_TYPE_MISMATCH );
199 return NULL;
202 static enum server_fd_type console_get_fd_type( struct fd *fd )
204 return FD_TYPE_CHAR;
207 /* dumps the renderer events of a console */
208 static void console_input_events_dump( struct object *obj, int verbose )
210 struct console_input_events *evts = (struct console_input_events *)obj;
211 assert( obj->ops == &console_input_events_ops );
212 fprintf( stderr, "Console input events: %d/%d events\n",
213 evts->num_used, evts->num_alloc );
216 /* destroys the renderer events of a console */
217 static void console_input_events_destroy( struct object *obj )
219 struct console_input_events *evts = (struct console_input_events *)obj;
220 assert( obj->ops == &console_input_events_ops );
221 free( evts->events );
224 /* the renderer events list is signaled when it's not empty */
225 static int console_input_events_signaled( struct object *obj, struct thread *thread )
227 struct console_input_events *evts = (struct console_input_events *)obj;
228 assert( obj->ops == &console_input_events_ops );
229 return (evts->num_used != 0);
232 /* add an event to the console's renderer events list */
233 static void console_input_events_append( struct console_input* console,
234 struct console_renderer_event* evt)
236 struct console_input_events* evts;
237 int collapsed = FALSE;
239 if (!(evts = console->evt)) return;
240 /* to be done even when evt has been generated by the renderer ? */
242 /* try to collapse evt into current queue's events */
243 if (evts->num_used)
245 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
247 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
248 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
250 /* if two update events overlap, collapse them into a single one */
251 if (last->u.update.bottom + 1 >= evt->u.update.top &&
252 evt->u.update.bottom + 1 >= last->u.update.top)
254 last->u.update.top = min(last->u.update.top, evt->u.update.top);
255 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
256 collapsed = TRUE;
260 if (!collapsed)
262 if (evts->num_used == evts->num_alloc)
264 evts->num_alloc += 16;
265 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
266 assert(evts->events);
268 evts->events[evts->num_used++] = *evt;
270 wake_up( &evts->obj, 0 );
273 /* retrieves events from the console's renderer events list */
274 static void console_input_events_get( struct console_input_events* evts )
276 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
278 if (num > evts->num_used) num = evts->num_used;
279 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
280 if (num < evts->num_used)
282 memmove( &evts->events[0], &evts->events[num],
283 (evts->num_used - num) * sizeof(evts->events[0]) );
285 evts->num_used -= num;
288 static struct console_input_events *create_console_input_events(void)
290 struct console_input_events* evt;
292 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
293 evt->num_alloc = evt->num_used = 0;
294 evt->events = NULL;
295 return evt;
298 static struct object *create_console_input( struct thread* renderer, int fd )
300 struct console_input *console_input;
302 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
303 console_input->renderer = renderer;
304 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
305 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
306 ENABLE_EXTENDED_FLAGS;
307 console_input->num_proc = 0;
308 console_input->active = NULL;
309 console_input->recnum = 0;
310 console_input->records = NULL;
311 console_input->evt = renderer ? create_console_input_events() : NULL;
312 console_input->title = NULL;
313 console_input->history_size = 50;
314 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
315 console_input->history_index = 0;
316 console_input->history_mode = 0;
317 console_input->edition_mode = 0;
318 console_input->input_cp = 0;
319 console_input->output_cp = 0;
320 console_input->win = 0;
321 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
322 console_input->fd = NULL;
324 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
326 release_object( console_input );
327 return NULL;
329 if (fd != -1) /* bare console */
331 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
332 FILE_SYNCHRONOUS_IO_NONALERT )))
334 release_object( console_input );
335 return NULL;
337 allow_fd_caching( console_input->fd );
340 return &console_input->obj;
343 static void generate_sb_initial_events( struct console_input *console_input )
345 struct screen_buffer *screen_buffer = console_input->active;
346 struct console_renderer_event evt;
348 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
349 memset(&evt.u, 0, sizeof(evt.u));
350 console_input_events_append( console_input, &evt );
352 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
353 evt.u.resize.width = screen_buffer->width;
354 evt.u.resize.height = screen_buffer->height;
355 console_input_events_append( console_input, &evt );
357 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
358 evt.u.display.left = screen_buffer->win.left;
359 evt.u.display.top = screen_buffer->win.top;
360 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
361 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
362 console_input_events_append( console_input, &evt );
364 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
365 evt.u.update.top = 0;
366 evt.u.update.bottom = screen_buffer->height - 1;
367 console_input_events_append( console_input, &evt );
369 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
370 evt.u.cursor_geom.size = screen_buffer->cursor_size;
371 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
372 console_input_events_append( console_input, &evt );
374 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
375 evt.u.cursor_pos.x = screen_buffer->cursor_x;
376 evt.u.cursor_pos.y = screen_buffer->cursor_y;
377 console_input_events_append( console_input, &evt );
380 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
382 struct screen_buffer *screen_buffer;
383 int i;
385 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
387 if (fd != -1) close( fd );
388 return NULL;
390 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
391 screen_buffer->input = console_input;
392 screen_buffer->cursor_size = 100;
393 screen_buffer->cursor_visible = 1;
394 screen_buffer->width = 80;
395 screen_buffer->height = 150;
396 screen_buffer->max_width = 80;
397 screen_buffer->max_height = 25;
398 screen_buffer->cursor_x = 0;
399 screen_buffer->cursor_y = 0;
400 screen_buffer->attr = 0x0F;
401 screen_buffer->win.left = 0;
402 screen_buffer->win.right = screen_buffer->max_width - 1;
403 screen_buffer->win.top = 0;
404 screen_buffer->win.bottom = screen_buffer->max_height - 1;
405 if (fd == -1)
406 screen_buffer->fd = NULL;
407 else
409 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
410 FILE_SYNCHRONOUS_IO_NONALERT )))
412 release_object( screen_buffer );
413 return NULL;
415 allow_fd_caching(screen_buffer->fd);
418 list_add_head( &screen_buffer_list, &screen_buffer->entry );
420 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
421 sizeof(*screen_buffer->data) )))
423 release_object( screen_buffer );
424 return NULL;
426 /* clear the first row */
427 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
428 /* and copy it to all other rows */
429 for (i = 1; i < screen_buffer->height; i++)
430 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
431 screen_buffer->width * sizeof(char_info_t) );
433 if (!console_input->active)
435 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
436 generate_sb_initial_events( console_input );
438 return screen_buffer;
441 /* free the console for this process */
442 int free_console( struct process *process )
444 struct console_input* console = process->console;
446 if (!console) return 0;
448 process->console = NULL;
449 if (--console->num_proc == 0 && console->renderer)
451 /* all processes have terminated... tell the renderer to terminate too */
452 struct console_renderer_event evt;
453 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
454 memset(&evt.u, 0, sizeof(evt.u));
455 console_input_events_append( console, &evt );
457 release_object( console );
459 return 1;
462 /* let process inherit the console from parent... this handle two cases :
463 * 1/ generic console inheritance
464 * 2/ parent is a renderer which launches process, and process should attach to the console
465 * renderered by parent
467 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
469 int done = 0;
470 struct process* parent = parent_thread->process;
472 /* if parent is a renderer, then attach current process to its console
473 * a bit hacky....
475 if (hconin)
477 struct console_input* console;
479 /* FIXME: should we check some access rights ? */
480 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
481 0, &console_input_ops )))
483 if (console->renderer == parent_thread)
485 process->console = (struct console_input*)grab_object( console );
486 process->console->num_proc++;
487 done = 1;
489 release_object( console );
491 else clear_error(); /* ignore error */
493 /* otherwise, if parent has a console, attach child to this console */
494 if (!done && parent->console)
496 process->console = (struct console_input*)grab_object( parent->console );
497 process->console->num_proc++;
501 struct thread *console_get_renderer( struct console_input *console )
503 return console->renderer;
506 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
508 struct console_input* console = NULL;
510 if (handle)
511 console = (struct console_input *)get_handle_obj( current->process, handle,
512 access, &console_input_ops );
513 else if (current->process->console)
515 console = (struct console_input *)grab_object( current->process->console );
518 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
519 return console;
522 struct console_signal_info
524 struct console_input *console;
525 process_id_t group;
526 int signal;
529 static int propagate_console_signal_cb(struct process *process, void *user)
531 struct console_signal_info* csi = (struct console_signal_info*)user;
533 if (process->console == csi->console && process->running_threads &&
534 (!csi->group || process->group_id == csi->group))
536 /* find a suitable thread to signal */
537 struct thread *thread;
538 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
540 if (send_thread_signal( thread, csi->signal )) break;
543 return FALSE;
546 static void propagate_console_signal( struct console_input *console,
547 int sig, process_id_t group_id )
549 struct console_signal_info csi;
551 if (!console)
553 set_error( STATUS_INVALID_PARAMETER );
554 return;
556 /* FIXME: should support the other events (like CTRL_BREAK) */
557 if (sig != CTRL_C_EVENT)
559 set_error( STATUS_NOT_IMPLEMENTED );
560 return;
562 csi.console = console;
563 csi.signal = SIGINT;
564 csi.group = group_id;
566 enum_processes(propagate_console_signal_cb, &csi);
569 static int get_console_mode( obj_handle_t handle )
571 struct object *obj;
572 int ret = 0;
574 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
576 if (obj->ops == &console_input_ops)
578 ret = ((struct console_input *)obj)->mode;
580 else if (obj->ops == &screen_buffer_ops)
582 ret = ((struct screen_buffer *)obj)->mode;
584 else
585 set_error( STATUS_OBJECT_TYPE_MISMATCH );
586 release_object( obj );
588 return ret;
591 /* changes the mode of either a console input or a screen buffer */
592 static int set_console_mode( obj_handle_t handle, int mode )
594 struct object *obj;
595 int ret = 0;
597 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
598 return 0;
599 if (obj->ops == &console_input_ops)
601 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
602 ((struct console_input *)obj)->mode = mode;
603 ret = 1;
605 else if (obj->ops == &screen_buffer_ops)
607 ((struct screen_buffer *)obj)->mode = mode;
608 ret = 1;
610 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
611 release_object( obj );
612 return ret;
615 /* add input events to a console input queue */
616 static int write_console_input( struct console_input* console, int count,
617 const INPUT_RECORD *records )
619 INPUT_RECORD *new_rec;
621 if (!count) return 0;
622 if (!(new_rec = realloc( console->records,
623 (console->recnum + count) * sizeof(INPUT_RECORD) )))
625 set_error( STATUS_NO_MEMORY );
626 release_object( console );
627 return -1;
629 console->records = new_rec;
630 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
632 if (console->mode & ENABLE_PROCESSED_INPUT)
634 int i = 0;
635 while (i < count)
637 if (records[i].EventType == KEY_EVENT &&
638 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
639 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
641 if (i != count - 1)
642 memcpy( &console->records[console->recnum + i],
643 &console->records[console->recnum + i + 1],
644 (count - i - 1) * sizeof(INPUT_RECORD) );
645 count--;
646 if (records[i].Event.KeyEvent.bKeyDown)
648 /* send SIGINT to all processes attached to this console */
649 propagate_console_signal( console, CTRL_C_EVENT, 0 );
652 else i++;
655 if (!console->recnum && count) set_event( console->event );
656 console->recnum += count;
657 return count;
660 /* retrieve a pointer to the console input records */
661 static int read_console_input( obj_handle_t handle, int count, int flush )
663 struct console_input *console;
665 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
666 FILE_READ_DATA, &console_input_ops )))
667 return -1;
669 if (!count)
671 /* special case: do not retrieve anything, but return
672 * the total number of records available */
673 count = console->recnum;
675 else
677 if (count > console->recnum) count = console->recnum;
678 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
680 if (flush)
682 int i;
683 for (i = count; i < console->recnum; i++)
684 console->records[i-count] = console->records[i];
685 if ((console->recnum -= count) > 0)
687 INPUT_RECORD *new_rec = realloc( console->records,
688 console->recnum * sizeof(INPUT_RECORD) );
689 if (new_rec) console->records = new_rec;
691 else
693 free( console->records );
694 console->records = NULL;
695 reset_event( console->event );
698 release_object( console );
699 return count;
702 /* set misc console input information */
703 static int set_console_input_info( const struct set_console_input_info_request *req,
704 const WCHAR *title, data_size_t len )
706 struct console_input *console;
707 struct console_renderer_event evt;
709 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
710 if (console_input_is_bare(console) &&
711 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
712 SET_CONSOLE_INPUT_INFO_WIN)))
714 set_error( STATUS_UNSUCCESSFUL );
715 goto error;
718 memset(&evt.u, 0, sizeof(evt.u));
719 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
721 struct screen_buffer *screen_buffer;
723 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
724 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
725 if (!screen_buffer || screen_buffer->input != console)
727 set_error( STATUS_INVALID_HANDLE );
728 if (screen_buffer) release_object( screen_buffer );
729 goto error;
732 if (screen_buffer != console->active)
734 if (console->active) release_object( console->active );
735 console->active = screen_buffer;
736 generate_sb_initial_events( console );
738 else
739 release_object( screen_buffer );
741 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
743 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
744 if (new_title)
746 memcpy( new_title, title, len );
747 new_title[len / sizeof(WCHAR)] = 0;
748 free( console->title );
749 console->title = new_title;
750 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
751 console_input_events_append( console, &evt );
754 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
756 console->history_mode = req->history_mode;
758 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
759 console->history_size != req->history_size)
761 WCHAR** mem = NULL;
762 int i;
763 int delta;
765 if (req->history_size)
767 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
768 if (!mem) goto error;
769 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
772 delta = (console->history_index > req->history_size) ?
773 (console->history_index - req->history_size) : 0;
775 for (i = delta; i < console->history_index; i++)
777 mem[i - delta] = console->history[i];
778 console->history[i] = NULL;
780 console->history_index -= delta;
782 for (i = 0; i < console->history_size; i++)
783 free( console->history[i] );
784 free( console->history );
785 console->history = mem;
786 console->history_size = req->history_size;
788 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
790 console->edition_mode = req->edition_mode;
792 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
794 console->input_cp = req->input_cp;
796 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
798 console->output_cp = req->output_cp;
800 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
802 console->win = req->win;
804 release_object( console );
805 return 1;
806 error:
807 if (console) release_object( console );
808 return 0;
811 /* resize a screen buffer */
812 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
813 int new_width, int new_height )
815 int i, old_width, old_height, copy_width, copy_height;
816 char_info_t *new_data;
818 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
820 set_error( STATUS_NO_MEMORY );
821 return 0;
823 old_width = screen_buffer->width;
824 old_height = screen_buffer->height;
825 copy_width = min( old_width, new_width );
826 copy_height = min( old_height, new_height );
828 /* copy all the rows */
829 for (i = 0; i < copy_height; i++)
831 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
832 copy_width * sizeof(char_info_t) );
835 /* clear the end of each row */
836 if (new_width > old_width)
838 /* fill first row */
839 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
840 /* and blast it to the other rows */
841 for (i = 1; i < copy_height; i++)
842 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
843 (new_width - old_width) * sizeof(char_info_t) );
846 /* clear remaining rows */
847 if (new_height > old_height)
849 /* fill first row */
850 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
851 /* and blast it to the other rows */
852 for (i = old_height+1; i < new_height; i++)
853 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
854 new_width * sizeof(char_info_t) );
856 free( screen_buffer->data );
857 screen_buffer->data = new_data;
858 screen_buffer->width = new_width;
859 screen_buffer->height = new_height;
860 return 1;
863 /* set misc screen buffer information */
864 static int set_console_output_info( struct screen_buffer *screen_buffer,
865 const struct set_console_output_info_request *req )
867 struct console_renderer_event evt;
869 memset(&evt.u, 0, sizeof(evt.u));
870 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
872 if (req->cursor_size < 1 || req->cursor_size > 100)
874 set_error( STATUS_INVALID_PARAMETER );
875 return 0;
877 if (screen_buffer->cursor_size != req->cursor_size ||
878 screen_buffer->cursor_visible != req->cursor_visible)
880 screen_buffer->cursor_size = req->cursor_size;
881 screen_buffer->cursor_visible = req->cursor_visible;
882 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
883 evt.u.cursor_geom.size = req->cursor_size;
884 evt.u.cursor_geom.visible = req->cursor_visible;
885 console_input_events_append( screen_buffer->input, &evt );
888 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
890 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
891 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
893 set_error( STATUS_INVALID_PARAMETER );
894 return 0;
896 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
898 screen_buffer->cursor_x = req->cursor_x;
899 screen_buffer->cursor_y = req->cursor_y;
900 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
901 evt.u.cursor_pos.x = req->cursor_x;
902 evt.u.cursor_pos.y = req->cursor_y;
903 console_input_events_append( screen_buffer->input, &evt );
906 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
908 unsigned cc;
910 /* new screen-buffer cannot be smaller than actual window */
911 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
912 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
914 set_error( STATUS_INVALID_PARAMETER );
915 return 0;
917 /* FIXME: there are also some basic minimum and max size to deal with */
918 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
920 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
921 evt.u.resize.width = req->width;
922 evt.u.resize.height = req->height;
923 console_input_events_append( screen_buffer->input, &evt );
925 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
926 evt.u.update.top = 0;
927 evt.u.update.bottom = screen_buffer->height - 1;
928 console_input_events_append( screen_buffer->input, &evt );
930 /* scroll window to display sb */
931 if (screen_buffer->win.right >= req->width)
933 screen_buffer->win.right -= screen_buffer->win.left;
934 screen_buffer->win.left = 0;
936 if (screen_buffer->win.bottom >= req->height)
938 screen_buffer->win.bottom -= screen_buffer->win.top;
939 screen_buffer->win.top = 0;
941 /* reset cursor if needed (normally, if cursor was outside of new sb, the
942 * window has been shifted so that the new position of the cursor will be
943 * visible */
944 cc = 0;
945 if (screen_buffer->cursor_x >= req->width)
947 screen_buffer->cursor_x = req->width - 1;
948 cc++;
950 if (screen_buffer->cursor_y >= req->height)
952 screen_buffer->cursor_y = req->height - 1;
953 cc++;
955 if (cc)
957 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
958 evt.u.cursor_pos.x = req->cursor_x;
959 evt.u.cursor_pos.y = req->cursor_y;
960 console_input_events_append( screen_buffer->input, &evt );
963 if (screen_buffer == screen_buffer->input->active &&
964 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
966 INPUT_RECORD ir;
967 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
968 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
969 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
970 write_console_input( screen_buffer->input, 1, &ir );
973 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
975 screen_buffer->attr = req->attr;
977 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
979 if (req->win_left < 0 || req->win_left > req->win_right ||
980 req->win_right >= screen_buffer->width ||
981 req->win_top < 0 || req->win_top > req->win_bottom ||
982 req->win_bottom >= screen_buffer->height)
984 set_error( STATUS_INVALID_PARAMETER );
985 return 0;
987 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
988 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
990 screen_buffer->win.left = req->win_left;
991 screen_buffer->win.top = req->win_top;
992 screen_buffer->win.right = req->win_right;
993 screen_buffer->win.bottom = req->win_bottom;
994 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
995 evt.u.display.left = req->win_left;
996 evt.u.display.top = req->win_top;
997 evt.u.display.width = req->win_right - req->win_left + 1;
998 evt.u.display.height = req->win_bottom - req->win_top + 1;
999 console_input_events_append( screen_buffer->input, &evt );
1002 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1004 /* can only be done by renderer */
1005 if (current->process->console != screen_buffer->input)
1007 set_error( STATUS_INVALID_PARAMETER );
1008 return 0;
1011 screen_buffer->max_width = req->max_width;
1012 screen_buffer->max_height = req->max_height;
1015 return 1;
1018 /* appends a new line to history (history is a fixed size array) */
1019 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1021 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1023 if (!ptr)
1024 return;
1026 if (!console || !console->history_size)
1028 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1029 free( ptr );
1030 return;
1033 memcpy( ptr, buf, len * sizeof(WCHAR) );
1034 ptr[len] = 0;
1036 if (console->history_mode && console->history_index &&
1037 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
1039 /* ok, mode ask us to not use twice the same string...
1040 * so just free mem and returns
1042 set_error( STATUS_ALIAS_EXISTS );
1043 free(ptr);
1044 return;
1047 if (console->history_index < console->history_size)
1049 console->history[console->history_index++] = ptr;
1051 else
1053 free( console->history[0]) ;
1054 memmove( &console->history[0], &console->history[1],
1055 (console->history_size - 1) * sizeof(WCHAR*) );
1056 console->history[console->history_size - 1] = ptr;
1060 /* returns a line from the cache */
1061 static data_size_t console_input_get_hist( struct console_input *console, int index )
1063 data_size_t ret = 0;
1065 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1066 else
1068 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1069 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1071 return ret;
1074 /* dumb dump */
1075 static void console_input_dump( struct object *obj, int verbose )
1077 struct console_input *console = (struct console_input *)obj;
1078 assert( obj->ops == &console_input_ops );
1079 fprintf( stderr, "Console input active=%p evt=%p\n",
1080 console->active, console->evt );
1083 static void console_input_destroy( struct object *obj )
1085 struct console_input* console_in = (struct console_input *)obj;
1086 struct screen_buffer* curr;
1087 int i;
1089 assert( obj->ops == &console_input_ops );
1090 free( console_in->title );
1091 free( console_in->records );
1093 if (console_in->active) release_object( console_in->active );
1094 console_in->active = NULL;
1096 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1098 if (curr->input == console_in) curr->input = NULL;
1101 if (console_in->evt)
1103 release_object( console_in->evt );
1104 console_in->evt = NULL;
1106 release_object( console_in->event );
1107 if (console_in->fd)
1108 release_object( console_in->fd );
1110 for (i = 0; i < console_in->history_size; i++)
1111 free( console_in->history[i] );
1112 free( console_in->history );
1115 static void screen_buffer_dump( struct object *obj, int verbose )
1117 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1118 assert( obj->ops == &screen_buffer_ops );
1120 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1123 static void screen_buffer_destroy( struct object *obj )
1125 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1127 assert( obj->ops == &screen_buffer_ops );
1129 list_remove( &screen_buffer->entry );
1131 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1133 struct screen_buffer *sb;
1135 screen_buffer->input->active = NULL;
1136 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1138 if (sb->input == screen_buffer->input)
1140 sb->input->active = sb;
1141 break;
1145 if (screen_buffer->fd) release_object( screen_buffer->fd );
1146 free( screen_buffer->data );
1149 static struct fd *screen_buffer_get_fd( struct object *obj )
1151 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1152 assert( obj->ops == &screen_buffer_ops );
1153 if (screen_buffer->fd)
1154 return (struct fd*)grab_object( screen_buffer->fd );
1155 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1156 return NULL;
1159 /* write data into a screen buffer */
1160 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1161 const void* data, enum char_info_mode mode,
1162 int x, int y, int wrap )
1164 unsigned int i;
1165 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1167 if (y >= screen_buffer->height) return 0;
1169 if (wrap)
1170 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1171 else
1172 end = screen_buffer->data + (y+1) * screen_buffer->width;
1174 switch(mode)
1176 case CHAR_INFO_MODE_TEXT:
1178 const WCHAR *ptr = data;
1179 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1181 break;
1182 case CHAR_INFO_MODE_ATTR:
1184 const unsigned short *ptr = data;
1185 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1187 break;
1188 case CHAR_INFO_MODE_TEXTATTR:
1190 const char_info_t *ptr = data;
1191 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1193 break;
1194 case CHAR_INFO_MODE_TEXTSTDATTR:
1196 const WCHAR *ptr = data;
1197 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1199 dest->ch = ptr[i];
1200 dest->attr = screen_buffer->attr;
1203 break;
1204 default:
1205 set_error( STATUS_INVALID_PARAMETER );
1206 return 0;
1209 if (i && screen_buffer == screen_buffer->input->active)
1211 struct console_renderer_event evt;
1212 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1213 memset(&evt.u, 0, sizeof(evt.u));
1214 evt.u.update.top = y + x / screen_buffer->width;
1215 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1216 console_input_events_append( screen_buffer->input, &evt );
1218 return i;
1221 /* fill a screen buffer with uniform data */
1222 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1223 enum char_info_mode mode, int x, int y, int count, int wrap )
1225 int i;
1226 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1228 if (y >= screen_buffer->height) return 0;
1230 if (wrap)
1231 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1232 else
1233 end = screen_buffer->data + (y+1) * screen_buffer->width;
1235 if (count > end - dest) count = end - dest;
1237 switch(mode)
1239 case CHAR_INFO_MODE_TEXT:
1240 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1241 break;
1242 case CHAR_INFO_MODE_ATTR:
1243 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1244 break;
1245 case CHAR_INFO_MODE_TEXTATTR:
1246 for (i = 0; i < count; i++) dest[i] = data;
1247 break;
1248 case CHAR_INFO_MODE_TEXTSTDATTR:
1249 for (i = 0; i < count; i++)
1251 dest[i].ch = data.ch;
1252 dest[i].attr = screen_buffer->attr;
1254 break;
1255 default:
1256 set_error( STATUS_INVALID_PARAMETER );
1257 return 0;
1260 if (count && screen_buffer == screen_buffer->input->active)
1262 struct console_renderer_event evt;
1263 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1264 memset(&evt.u, 0, sizeof(evt.u));
1265 evt.u.update.top = y;
1266 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1267 console_input_events_append( screen_buffer->input, &evt );
1269 return i;
1272 /* read data from a screen buffer */
1273 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1274 enum char_info_mode mode, int wrap )
1276 int i;
1277 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1279 if (y >= screen_buffer->height) return;
1281 if (wrap)
1282 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1283 else
1284 end = screen_buffer->data + (y+1) * screen_buffer->width;
1286 switch(mode)
1288 case CHAR_INFO_MODE_TEXT:
1290 WCHAR *data;
1291 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1292 if ((data = set_reply_data_size( count * sizeof(*data) )))
1294 for (i = 0; i < count; i++) data[i] = src[i].ch;
1297 break;
1298 case CHAR_INFO_MODE_ATTR:
1300 unsigned short *data;
1301 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1302 if ((data = set_reply_data_size( count * sizeof(*data) )))
1304 for (i = 0; i < count; i++) data[i] = src[i].attr;
1307 break;
1308 case CHAR_INFO_MODE_TEXTATTR:
1310 char_info_t *data;
1311 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1312 if ((data = set_reply_data_size( count * sizeof(*data) )))
1314 for (i = 0; i < count; i++) data[i] = src[i];
1317 break;
1318 default:
1319 set_error( STATUS_INVALID_PARAMETER );
1320 break;
1324 /* scroll parts of a screen buffer */
1325 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1326 int w, int h )
1328 int j;
1329 char_info_t *psrc, *pdst;
1330 struct console_renderer_event evt;
1332 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1333 xsrc + w > screen_buffer->width ||
1334 xdst + w > screen_buffer->width ||
1335 ysrc + h > screen_buffer->height ||
1336 ydst + h > screen_buffer->height ||
1337 w == 0 || h == 0)
1339 set_error( STATUS_INVALID_PARAMETER );
1340 return;
1343 if (ysrc < ydst)
1345 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1346 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1348 for (j = h; j > 0; j--)
1350 memcpy(pdst, psrc, w * sizeof(*pdst) );
1351 pdst -= screen_buffer->width;
1352 psrc -= screen_buffer->width;
1355 else
1357 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1358 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1360 for (j = 0; j < h; j++)
1362 /* we use memmove here because when psrc and pdst are the same,
1363 * copies are done on the same row, so the dst and src blocks
1364 * can overlap */
1365 memmove( pdst, psrc, w * sizeof(*pdst) );
1366 pdst += screen_buffer->width;
1367 psrc += screen_buffer->width;
1371 /* FIXME: this could be enhanced, by signalling scroll */
1372 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1373 memset(&evt.u, 0, sizeof(evt.u));
1374 evt.u.update.top = min(ysrc, ydst);
1375 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1376 console_input_events_append( screen_buffer->input, &evt );
1379 /* allocate a console for the renderer */
1380 DECL_HANDLER(alloc_console)
1382 obj_handle_t in = 0;
1383 obj_handle_t evt = 0;
1384 struct process *process;
1385 struct thread *renderer;
1386 struct console_input *console;
1387 int fd;
1388 int attach = 0;
1390 switch (req->pid)
1392 case 0:
1393 /* renderer is current, console to be attached to parent process */
1394 renderer = current;
1395 if (!(process = current->process->parent))
1397 set_error( STATUS_ACCESS_DENIED );
1398 return;
1400 grab_object( process );
1401 attach = 1;
1402 break;
1403 case 0xffffffff:
1404 /* no renderer, console to be attached to current process */
1405 renderer = NULL;
1406 process = current->process;
1407 grab_object( process );
1408 attach = 1;
1409 break;
1410 default:
1411 /* renderer is current, console to be attached to req->pid */
1412 renderer = current;
1413 if (!(process = get_process_from_id( req->pid ))) return;
1416 if (attach && process->console)
1418 set_error( STATUS_ACCESS_DENIED );
1419 goto the_end;
1421 if (req->input_fd != -1)
1423 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1425 set_error( STATUS_INVALID_PARAMETER );
1426 goto the_end;
1429 else fd = -1;
1431 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1433 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1435 if (!console->evt ||
1436 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1438 if (attach)
1440 process->console = (struct console_input*)grab_object( console );
1441 console->num_proc++;
1443 reply->handle_in = in;
1444 reply->event = evt;
1445 release_object( console );
1446 goto the_end;
1448 close_handle( current->process, in );
1450 release_object( console );
1452 the_end:
1453 release_object( process );
1456 /* free the console of the current process */
1457 DECL_HANDLER(free_console)
1459 free_console( current->process );
1462 /* let the renderer peek the events it's waiting on */
1463 DECL_HANDLER(get_console_renderer_events)
1465 struct console_input_events *evt;
1467 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1468 FILE_READ_PROPERTIES, &console_input_events_ops );
1469 if (!evt) return;
1470 console_input_events_get( evt );
1471 release_object( evt );
1474 /* open a handle to the process console */
1475 DECL_HANDLER(open_console)
1477 struct object *obj = NULL;
1479 reply->handle = 0;
1480 if (!req->from)
1482 if (current->process->console)
1483 obj = grab_object( (struct object*)current->process->console );
1485 else if (req->from == (obj_handle_t)1)
1487 if (current->process->console && current->process->console->active)
1488 obj = grab_object( (struct object*)current->process->console->active );
1490 else if ((obj = get_handle_obj( current->process, req->from,
1491 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1493 struct console_input *console = (struct console_input *)obj;
1494 obj = (console->active) ? grab_object( console->active ) : NULL;
1495 release_object( console );
1498 /* FIXME: req->share is not used (as in screen buffer creation) */
1499 if (obj)
1501 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1502 release_object( obj );
1504 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1507 /* set info about a console input */
1508 DECL_HANDLER(set_console_input_info)
1510 set_console_input_info( req, get_req_data(), get_req_data_size() );
1513 /* get info about a console (output only) */
1514 DECL_HANDLER(get_console_input_info)
1516 struct console_input *console;
1518 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1519 if (console->title)
1521 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1522 if (len > get_reply_max_size()) len = get_reply_max_size();
1523 set_reply_data( console->title, len );
1525 reply->history_mode = console->history_mode;
1526 reply->history_size = console->history_size;
1527 reply->history_index = console->history_index;
1528 reply->edition_mode = console->edition_mode;
1529 reply->input_cp = console->input_cp;
1530 reply->output_cp = console->output_cp;
1531 reply->win = console->win;
1533 release_object( console );
1536 /* get a console mode (input or output) */
1537 DECL_HANDLER(get_console_mode)
1539 reply->mode = get_console_mode( req->handle );
1542 /* set a console mode (input or output) */
1543 DECL_HANDLER(set_console_mode)
1545 set_console_mode( req->handle, req->mode );
1548 /* add input records to a console input queue */
1549 DECL_HANDLER(write_console_input)
1551 struct console_input *console;
1553 reply->written = 0;
1554 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1555 FILE_WRITE_PROPERTIES, &console_input_ops )))
1556 return;
1557 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1558 get_req_data() );
1559 release_object( console );
1562 /* fetch input records from a console input queue */
1563 DECL_HANDLER(read_console_input)
1565 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1566 reply->read = read_console_input( req->handle, count, req->flush );
1569 /* appends a string to console's history */
1570 DECL_HANDLER(append_console_input_history)
1572 struct console_input *console;
1574 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1575 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1576 release_object( console );
1579 /* appends a string to console's history */
1580 DECL_HANDLER(get_console_input_history)
1582 struct console_input *console;
1584 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1585 reply->total = console_input_get_hist( console, req->index );
1586 release_object( console );
1589 /* creates a screen buffer */
1590 DECL_HANDLER(create_console_output)
1592 struct console_input* console;
1593 struct screen_buffer* screen_buffer;
1594 int fd;
1596 if (req->fd != -1)
1598 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1600 set_error( STATUS_INVALID_HANDLE );
1601 return;
1604 else fd = -1;
1605 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1607 close(fd);
1608 return;
1610 if (console_input_is_bare( console ) ^ (fd != -1))
1612 close( fd );
1613 release_object( console );
1614 set_error( STATUS_INVALID_HANDLE );
1615 return;
1618 screen_buffer = create_console_output( console, fd );
1619 if (screen_buffer)
1621 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1622 * see file.c on how this could be done */
1623 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1624 release_object( screen_buffer );
1626 release_object( console );
1629 /* set info about a console screen buffer */
1630 DECL_HANDLER(set_console_output_info)
1632 struct screen_buffer *screen_buffer;
1634 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1635 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1637 set_console_output_info( screen_buffer, req );
1638 release_object( screen_buffer );
1642 /* get info about a console screen buffer */
1643 DECL_HANDLER(get_console_output_info)
1645 struct screen_buffer *screen_buffer;
1647 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1648 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1650 reply->cursor_size = screen_buffer->cursor_size;
1651 reply->cursor_visible = screen_buffer->cursor_visible;
1652 reply->cursor_x = screen_buffer->cursor_x;
1653 reply->cursor_y = screen_buffer->cursor_y;
1654 reply->width = screen_buffer->width;
1655 reply->height = screen_buffer->height;
1656 reply->attr = screen_buffer->attr;
1657 reply->win_left = screen_buffer->win.left;
1658 reply->win_top = screen_buffer->win.top;
1659 reply->win_right = screen_buffer->win.right;
1660 reply->win_bottom = screen_buffer->win.bottom;
1661 reply->max_width = screen_buffer->max_width;
1662 reply->max_height = screen_buffer->max_height;
1663 release_object( screen_buffer );
1667 /* read data (chars & attrs) from a screen buffer */
1668 DECL_HANDLER(read_console_output)
1670 struct screen_buffer *screen_buffer;
1672 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1673 FILE_READ_DATA, &screen_buffer_ops )))
1675 if (console_input_is_bare( screen_buffer->input ))
1677 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1678 release_object( screen_buffer );
1679 return;
1681 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1682 reply->width = screen_buffer->width;
1683 reply->height = screen_buffer->height;
1684 release_object( screen_buffer );
1688 /* write data (char and/or attrs) to a screen buffer */
1689 DECL_HANDLER(write_console_output)
1691 struct screen_buffer *screen_buffer;
1693 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1694 FILE_WRITE_DATA, &screen_buffer_ops)))
1696 if (console_input_is_bare( screen_buffer->input ))
1698 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1699 release_object( screen_buffer );
1700 return;
1702 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1703 req->mode, req->x, req->y, req->wrap );
1704 reply->width = screen_buffer->width;
1705 reply->height = screen_buffer->height;
1706 release_object( screen_buffer );
1710 /* fill a screen buffer with constant data (chars and/or attributes) */
1711 DECL_HANDLER(fill_console_output)
1713 struct screen_buffer *screen_buffer;
1715 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1716 FILE_WRITE_DATA, &screen_buffer_ops)))
1718 if (console_input_is_bare( screen_buffer->input ))
1720 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1721 release_object( screen_buffer );
1722 return;
1724 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1725 req->x, req->y, req->count, req->wrap );
1726 release_object( screen_buffer );
1730 /* move a rect of data in a screen buffer */
1731 DECL_HANDLER(move_console_output)
1733 struct screen_buffer *screen_buffer;
1735 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1736 FILE_WRITE_DATA, &screen_buffer_ops)))
1738 if (console_input_is_bare( screen_buffer->input ))
1740 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1741 release_object( screen_buffer );
1742 return;
1744 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1745 req->w, req->h );
1746 release_object( screen_buffer );
1750 /* sends a signal to a console (process, group...) */
1751 DECL_HANDLER(send_console_signal)
1753 process_id_t group;
1755 group = req->group_id ? req->group_id : current->process->group_id;
1757 if (!group)
1758 set_error( STATUS_INVALID_PARAMETER );
1759 else
1760 propagate_console_signal( current->process->console, req->signal, group );
1763 /* get console which renderer is 'current' */
1764 static int cgwe_enum( struct process* process, void* user)
1766 if (process->console && process->console->renderer == current)
1768 *(struct console_input**)user = process->console;
1769 return 1;
1771 return 0;
1774 DECL_HANDLER(get_console_wait_event)
1776 struct console_input* console = NULL;
1778 if (current->process->console)
1779 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1780 else enum_processes(cgwe_enum, &console);
1782 if (console)
1784 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1785 release_object( console );
1787 else set_error( STATUS_INVALID_PARAMETER );