advpack: Strip single quotes (with tests) on reg keys.
[wine/multimedia.git] / server / console.c
blob88a24e83d9e01cbd0778ff0014def37fa50c132f
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 "unicode.h"
38 #include "wincon.h"
39 #include "winternl.h"
41 /* specific access rights (FIXME: should use finer-grained access rights) */
42 #define CONSOLE_READ 0x01
43 #define CONSOLE_WRITE 0x02
45 struct screen_buffer;
46 struct console_input_events;
48 struct console_input
50 struct object obj; /* object header */
51 int num_proc; /* number of processes attached to this console */
52 struct thread *renderer; /* console renderer thread */
53 int mode; /* input mode */
54 struct screen_buffer *active; /* active screen buffer */
55 int recnum; /* number of input records */
56 INPUT_RECORD *records; /* input records */
57 struct console_input_events *evt; /* synchronization event with renderer */
58 WCHAR *title; /* console title */
59 WCHAR **history; /* lines history */
60 int history_size; /* number of entries in history array */
61 int history_index; /* number of used entries in history array */
62 int history_mode; /* mode of history (non zero means remove doubled strings */
63 int edition_mode; /* index to edition mode flavors */
64 int input_cp; /* console input codepage */
65 int output_cp; /* console output codepage */
66 struct event *event; /* event to wait on for input queue */
69 static unsigned int console_map_access( struct object *obj, unsigned int access );
71 static void console_input_dump( struct object *obj, int verbose );
72 static void console_input_destroy( struct object *obj );
74 static const struct object_ops console_input_ops =
76 sizeof(struct console_input), /* size */
77 console_input_dump, /* dump */
78 no_add_queue, /* add_queue */
79 NULL, /* remove_queue */
80 NULL, /* signaled */
81 no_satisfied, /* satisfied */
82 no_signal, /* signal */
83 no_get_fd, /* get_fd */
84 console_map_access, /* map_access */
85 no_lookup_name, /* lookup_name */
86 no_open_file, /* open_file */
87 no_close_handle, /* close_handle */
88 console_input_destroy /* destroy */
91 static void console_input_events_dump( struct object *obj, int verbose );
92 static void console_input_events_destroy( struct object *obj );
93 static int console_input_events_signaled( struct object *obj, struct thread *thread );
95 struct console_input_events
97 struct object obj; /* object header */
98 int num_alloc; /* number of allocated events */
99 int num_used; /* number of actually used events */
100 struct console_renderer_event* events;
103 static const struct object_ops console_input_events_ops =
105 sizeof(struct console_input_events), /* size */
106 console_input_events_dump, /* dump */
107 add_queue, /* add_queue */
108 remove_queue, /* remove_queue */
109 console_input_events_signaled, /* signaled */
110 no_satisfied, /* satisfied */
111 no_signal, /* signal */
112 no_get_fd, /* get_fd */
113 console_map_access, /* map_access */
114 no_lookup_name, /* lookup_name */
115 no_open_file, /* open_file */
116 no_close_handle, /* close_handle */
117 console_input_events_destroy /* destroy */
120 struct screen_buffer
122 struct object obj; /* object header */
123 struct list entry; /* entry in list of all screen buffers */
124 struct console_input *input; /* associated console input */
125 int mode; /* output mode */
126 int cursor_size; /* size of cursor (percentage filled) */
127 int cursor_visible;/* cursor visibility flag */
128 int cursor_x; /* position of cursor */
129 int cursor_y; /* position of cursor */
130 int width; /* size (w-h) of the screen buffer */
131 int height;
132 int max_width; /* size (w-h) of the window given font size */
133 int max_height;
134 char_info_t *data; /* the data for each cell - a width x height matrix */
135 unsigned short attr; /* default attribute for screen buffer */
136 rectangle_t win; /* current visible window on the screen buffer *
137 * as seen in wineconsole */
140 static void screen_buffer_dump( struct object *obj, int verbose );
141 static void screen_buffer_destroy( struct object *obj );
143 static const struct object_ops screen_buffer_ops =
145 sizeof(struct screen_buffer), /* size */
146 screen_buffer_dump, /* dump */
147 no_add_queue, /* add_queue */
148 NULL, /* remove_queue */
149 NULL, /* signaled */
150 NULL, /* satisfied */
151 no_signal, /* signal */
152 no_get_fd, /* get_fd */
153 console_map_access, /* map_access */
154 no_lookup_name, /* lookup_name */
155 no_open_file, /* open_file */
156 no_close_handle, /* close_handle */
157 screen_buffer_destroy /* destroy */
160 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
162 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
164 /* access mapping for all console objects */
165 static unsigned int console_map_access( struct object *obj, unsigned int access )
167 if (access & GENERIC_READ) access |= SYNCHRONIZE | STANDARD_RIGHTS_READ | CONSOLE_READ;
168 if (access & GENERIC_WRITE) access |= SYNCHRONIZE | STANDARD_RIGHTS_WRITE | CONSOLE_WRITE;
169 if (access & GENERIC_EXECUTE) access |= SYNCHRONIZE | STANDARD_RIGHTS_EXECUTE;
170 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | CONSOLE_READ | CONSOLE_WRITE;
171 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
174 /* dumps the renderer events of a console */
175 static void console_input_events_dump( struct object *obj, int verbose )
177 struct console_input_events *evts = (struct console_input_events *)obj;
178 assert( obj->ops == &console_input_events_ops );
179 fprintf( stderr, "Console input events: %d/%d events\n",
180 evts->num_used, evts->num_alloc );
183 /* destroys the renderer events of a console */
184 static void console_input_events_destroy( struct object *obj )
186 struct console_input_events *evts = (struct console_input_events *)obj;
187 assert( obj->ops == &console_input_events_ops );
188 free( evts->events );
191 /* the renderer events list is signaled when it's not empty */
192 static int console_input_events_signaled( struct object *obj, struct thread *thread )
194 struct console_input_events *evts = (struct console_input_events *)obj;
195 assert( obj->ops == &console_input_events_ops );
196 return (evts->num_used != 0);
199 /* add an event to the console's renderer events list */
200 static void console_input_events_append( struct console_input_events* evts,
201 struct console_renderer_event* evt)
203 int collapsed = FALSE;
205 /* to be done even when evt has been generated by the rendere ? */
207 /* try to collapse evt into current queue's events */
208 if (evts->num_used)
210 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
212 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
213 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
215 /* if two update events overlap, collapse them into a single one */
216 if (last->u.update.bottom + 1 >= evt->u.update.top &&
217 evt->u.update.bottom + 1 >= last->u.update.top)
219 last->u.update.top = min(last->u.update.top, evt->u.update.top);
220 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
221 collapsed = TRUE;
225 if (!collapsed)
227 if (evts->num_used == evts->num_alloc)
229 evts->num_alloc += 16;
230 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
231 assert(evts->events);
233 evts->events[evts->num_used++] = *evt;
235 wake_up( &evts->obj, 0 );
238 /* retrieves events from the console's renderer events list */
239 static void console_input_events_get( struct console_input_events* evts )
241 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
243 if (num > evts->num_used) num = evts->num_used;
244 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
245 if (num < evts->num_used)
247 memmove( &evts->events[0], &evts->events[num],
248 (evts->num_used - num) * sizeof(evts->events[0]) );
250 evts->num_used -= num;
253 static struct console_input_events *create_console_input_events(void)
255 struct console_input_events* evt;
257 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
258 evt->num_alloc = evt->num_used = 0;
259 evt->events = NULL;
260 return evt;
263 static struct object *create_console_input( struct thread* renderer )
265 struct console_input *console_input;
267 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
268 console_input->renderer = renderer;
269 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
270 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
271 console_input->num_proc = 0;
272 console_input->active = NULL;
273 console_input->recnum = 0;
274 console_input->records = NULL;
275 console_input->evt = create_console_input_events();
276 console_input->title = NULL;
277 console_input->history_size = 50;
278 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
279 console_input->history_index = 0;
280 console_input->history_mode = 0;
281 console_input->edition_mode = 0;
282 console_input->input_cp = 0;
283 console_input->output_cp = 0;
284 console_input->event = create_event( NULL, NULL, 0, 1, 0 );
286 if (!console_input->history || !console_input->evt)
288 release_object( console_input );
289 return NULL;
291 return &console_input->obj;
294 static struct screen_buffer *create_console_output( struct console_input *console_input )
296 struct screen_buffer *screen_buffer;
297 int i;
299 if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
300 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
301 screen_buffer->input = console_input;
302 screen_buffer->cursor_size = 100;
303 screen_buffer->cursor_visible = 1;
304 screen_buffer->width = 80;
305 screen_buffer->height = 150;
306 screen_buffer->max_width = 80;
307 screen_buffer->max_height = 25;
308 screen_buffer->cursor_x = 0;
309 screen_buffer->cursor_y = 0;
310 screen_buffer->attr = 0x0F;
311 screen_buffer->win.left = 0;
312 screen_buffer->win.right = screen_buffer->max_width - 1;
313 screen_buffer->win.top = 0;
314 screen_buffer->win.bottom = screen_buffer->max_height - 1;
316 list_add_head( &screen_buffer_list, &screen_buffer->entry );
318 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
319 sizeof(*screen_buffer->data) )))
321 release_object( screen_buffer );
322 return NULL;
324 /* clear the first row */
325 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
326 /* and copy it to all other rows */
327 for (i = 1; i < screen_buffer->height; i++)
328 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
329 screen_buffer->width * sizeof(char_info_t) );
331 if (!console_input->active)
333 struct console_renderer_event evt;
334 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
336 /* generate the initial events */
337 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
338 memset(&evt.u, 0, sizeof(evt.u));
339 console_input_events_append( console_input->evt, &evt );
341 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
342 evt.u.resize.width = screen_buffer->width;
343 evt.u.resize.height = screen_buffer->height;
344 console_input_events_append( console_input->evt, &evt );
346 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
347 evt.u.display.left = screen_buffer->win.left;
348 evt.u.display.top = screen_buffer->win.top;
349 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
350 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
351 console_input_events_append( console_input->evt, &evt );
353 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
354 evt.u.update.top = 0;
355 evt.u.update.bottom = screen_buffer->height - 1;
356 console_input_events_append( console_input->evt, &evt );
358 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
359 evt.u.cursor_geom.size = screen_buffer->cursor_size;
360 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
361 console_input_events_append( console_input->evt, &evt );
363 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
364 evt.u.cursor_pos.x = screen_buffer->cursor_x;
365 evt.u.cursor_pos.y = screen_buffer->cursor_y;
366 console_input_events_append( console_input->evt, &evt );
368 return screen_buffer;
371 /* free the console for this process */
372 int free_console( struct process *process )
374 struct console_input* console = process->console;
376 if (!console || !console->renderer) return 0;
378 process->console = NULL;
379 if (--console->num_proc == 0)
381 /* all processes have terminated... tell the renderer to terminate too */
382 struct console_renderer_event evt;
383 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
384 memset(&evt.u, 0, sizeof(evt.u));
385 console_input_events_append( console->evt, &evt );
387 release_object( console );
389 return 1;
392 /* let process inherit the console from parent... this handle two cases :
393 * 1/ generic console inheritance
394 * 2/ parent is a renderer which launches process, and process should attach to the console
395 * renderered by parent
397 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
399 int done = 0;
400 struct process* parent = parent_thread->process;
402 /* if parent is a renderer, then attach current process to its console
403 * a bit hacky....
405 if (hconin)
407 struct console_input* console;
409 /* FIXME: should we check some access rights ? */
410 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
411 0, &console_input_ops )))
413 if (console->renderer == parent_thread)
415 process->console = (struct console_input*)grab_object( console );
416 process->console->num_proc++;
417 done = 1;
419 release_object( console );
421 else clear_error(); /* ignore error */
423 /* otherwise, if parent has a console, attach child to this console */
424 if (!done && parent->console)
426 assert(parent->console->renderer);
427 process->console = (struct console_input*)grab_object( parent->console );
428 process->console->num_proc++;
432 struct thread *console_get_renderer( struct console_input *console )
434 return console->renderer;
437 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
439 struct console_input* console = NULL;
441 if (handle)
442 console = (struct console_input *)get_handle_obj( current->process, handle,
443 access, &console_input_ops );
444 else if (current->process->console)
446 assert( current->process->console->renderer );
447 console = (struct console_input *)grab_object( current->process->console );
450 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
451 return console;
454 struct console_signal_info
456 struct console_input *console;
457 process_id_t group;
458 int signal;
461 static int propagate_console_signal_cb(struct process *process, void *user)
463 struct console_signal_info* csi = (struct console_signal_info*)user;
465 if (process->console == csi->console && process->running_threads &&
466 (!csi->group || process->group_id == csi->group))
468 /* find a suitable thread to signal */
469 struct thread *thread;
470 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
472 if (send_thread_signal( thread, csi->signal )) break;
475 return FALSE;
478 static void propagate_console_signal( struct console_input *console,
479 int sig, process_id_t group_id )
481 struct console_signal_info csi;
483 if (!console)
485 set_error( STATUS_INVALID_PARAMETER );
486 return;
488 /* FIXME: should support the other events (like CTRL_BREAK) */
489 if (sig != CTRL_C_EVENT)
491 set_error( STATUS_NOT_IMPLEMENTED );
492 return;
494 csi.console = console;
495 csi.signal = SIGINT;
496 csi.group = group_id;
498 enum_processes(propagate_console_signal_cb, &csi);
501 static int get_console_mode( obj_handle_t handle )
503 struct object *obj;
504 int ret = 0;
506 if ((obj = get_handle_obj( current->process, handle, CONSOLE_READ, NULL )))
508 if (obj->ops == &console_input_ops)
509 ret = ((struct console_input *)obj)->mode;
510 else if (obj->ops == &screen_buffer_ops)
511 ret = ((struct screen_buffer *)obj)->mode;
512 else
513 set_error( STATUS_OBJECT_TYPE_MISMATCH );
514 release_object( obj );
516 return ret;
519 /* changes the mode of either a console input or a screen buffer */
520 static int set_console_mode( obj_handle_t handle, int mode )
522 struct object *obj;
523 int ret = 0;
525 if (!(obj = get_handle_obj( current->process, handle, CONSOLE_WRITE, NULL )))
526 return 0;
527 if (obj->ops == &console_input_ops)
529 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
530 ((struct console_input *)obj)->mode = mode;
531 ret = 1;
533 else if (obj->ops == &screen_buffer_ops)
535 ((struct screen_buffer *)obj)->mode = mode;
536 ret = 1;
538 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
539 release_object( obj );
540 return ret;
543 /* add input events to a console input queue */
544 static int write_console_input( struct console_input* console, int count,
545 const INPUT_RECORD *records )
547 INPUT_RECORD *new_rec;
549 if (!count) return 0;
550 if (!(new_rec = realloc( console->records,
551 (console->recnum + count) * sizeof(INPUT_RECORD) )))
553 set_error( STATUS_NO_MEMORY );
554 release_object( console );
555 return -1;
557 console->records = new_rec;
558 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
560 if (console->mode & ENABLE_PROCESSED_INPUT)
562 int i = 0;
563 while (i < count)
565 if (records[i].EventType == KEY_EVENT &&
566 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
567 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
569 if (i != count - 1)
570 memcpy( &console->records[console->recnum + i],
571 &console->records[console->recnum + i + 1],
572 (count - i - 1) * sizeof(INPUT_RECORD) );
573 count--;
574 if (records[i].Event.KeyEvent.bKeyDown)
576 /* send SIGINT to all processes attached to this console */
577 propagate_console_signal( console, CTRL_C_EVENT, 0 );
580 else i++;
583 if (!console->recnum && count) set_event( console->event );
584 console->recnum += count;
585 return count;
588 /* retrieve a pointer to the console input records */
589 static int read_console_input( obj_handle_t handle, int count, int flush )
591 struct console_input *console;
593 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
594 CONSOLE_READ, &console_input_ops )))
595 return -1;
597 if (!count)
599 /* special case: do not retrieve anything, but return
600 * the total number of records available */
601 count = console->recnum;
603 else
605 if (count > console->recnum) count = console->recnum;
606 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
608 if (flush)
610 int i;
611 for (i = count; i < console->recnum; i++)
612 console->records[i-count] = console->records[i];
613 if ((console->recnum -= count) > 0)
615 INPUT_RECORD *new_rec = realloc( console->records,
616 console->recnum * sizeof(INPUT_RECORD) );
617 if (new_rec) console->records = new_rec;
619 else
621 free( console->records );
622 console->records = NULL;
623 reset_event( console->event );
626 release_object( console );
627 return count;
630 /* set misc console input information */
631 static int set_console_input_info( const struct set_console_input_info_request *req,
632 const WCHAR *title, data_size_t len )
634 struct console_input *console;
635 struct console_renderer_event evt;
637 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) goto error;
639 memset(&evt.u, 0, sizeof(evt.u));
640 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
642 struct screen_buffer *screen_buffer;
644 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
645 CONSOLE_WRITE, &screen_buffer_ops );
646 if (!screen_buffer || screen_buffer->input != console)
648 set_error( STATUS_INVALID_HANDLE );
649 if (screen_buffer) release_object( screen_buffer );
650 goto error;
653 if (screen_buffer != console->active)
655 if (console->active) release_object( console->active );
656 console->active = screen_buffer;
657 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
658 console_input_events_append( console->evt, &evt );
660 else
661 release_object( screen_buffer );
663 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
665 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
666 if (new_title)
668 memcpy( new_title, title, len );
669 new_title[len / sizeof(WCHAR)] = 0;
670 free( console->title );
671 console->title = new_title;
672 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
673 console_input_events_append( console->evt, &evt );
676 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
678 console->history_mode = req->history_mode;
680 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
681 console->history_size != req->history_size)
683 WCHAR** mem = NULL;
684 int i;
685 int delta;
687 if (req->history_size)
689 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
690 if (!mem) goto error;
691 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
694 delta = (console->history_index > req->history_size) ?
695 (console->history_index - req->history_size) : 0;
697 for (i = delta; i < console->history_index; i++)
699 mem[i - delta] = console->history[i];
700 console->history[i] = NULL;
702 console->history_index -= delta;
704 for (i = 0; i < console->history_size; i++)
705 if (console->history[i]) free( console->history[i] );
706 free( console->history );
707 console->history = mem;
708 console->history_size = req->history_size;
710 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
712 console->edition_mode = req->edition_mode;
714 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
716 console->input_cp = req->input_cp;
718 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
720 console->output_cp = req->output_cp;
722 release_object( console );
723 return 1;
724 error:
725 if (console) release_object( console );
726 return 0;
729 /* resize a screen buffer */
730 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
731 int new_width, int new_height )
733 int i, old_width, old_height, copy_width, copy_height;
734 char_info_t *new_data;
736 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
738 set_error( STATUS_NO_MEMORY );
739 return 0;
741 old_width = screen_buffer->width;
742 old_height = screen_buffer->height;
743 copy_width = min( old_width, new_width );
744 copy_height = min( old_height, new_height );
746 /* copy all the rows */
747 for (i = 0; i < copy_height; i++)
749 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
750 copy_width * sizeof(char_info_t) );
753 /* clear the end of each row */
754 if (new_width > old_width)
756 /* fill first row */
757 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
758 /* and blast it to the other rows */
759 for (i = 1; i < copy_height; i++)
760 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
761 (new_width - old_width) * sizeof(char_info_t) );
764 /* clear remaining rows */
765 if (new_height > old_height)
767 /* fill first row */
768 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
769 /* and blast it to the other rows */
770 for (i = old_height+1; i < new_height; i++)
771 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
772 new_width * sizeof(char_info_t) );
774 free( screen_buffer->data );
775 screen_buffer->data = new_data;
776 screen_buffer->width = new_width;
777 screen_buffer->height = new_height;
778 return 1;
781 /* set misc screen buffer information */
782 static int set_console_output_info( struct screen_buffer *screen_buffer,
783 const struct set_console_output_info_request *req )
785 struct console_renderer_event evt;
787 memset(&evt.u, 0, sizeof(evt.u));
788 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
790 if (req->cursor_size < 1 || req->cursor_size > 100)
792 set_error( STATUS_INVALID_PARAMETER );
793 return 0;
795 if (screen_buffer->cursor_size != req->cursor_size ||
796 screen_buffer->cursor_visible != req->cursor_visible)
798 screen_buffer->cursor_size = req->cursor_size;
799 screen_buffer->cursor_visible = req->cursor_visible;
800 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
801 evt.u.cursor_geom.size = req->cursor_size;
802 evt.u.cursor_geom.visible = req->cursor_visible;
803 console_input_events_append( screen_buffer->input->evt, &evt );
806 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
808 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
809 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
811 set_error( STATUS_INVALID_PARAMETER );
812 return 0;
814 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
816 screen_buffer->cursor_x = req->cursor_x;
817 screen_buffer->cursor_y = req->cursor_y;
818 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
819 evt.u.cursor_pos.x = req->cursor_x;
820 evt.u.cursor_pos.y = req->cursor_y;
821 console_input_events_append( screen_buffer->input->evt, &evt );
824 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
826 unsigned cc;
828 /* new screen-buffer cannot be smaller than actual window */
829 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
830 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
832 set_error( STATUS_INVALID_PARAMETER );
833 return 0;
835 /* FIXME: there are also some basic minimum and max size to deal with */
836 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
838 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
839 evt.u.resize.width = req->width;
840 evt.u.resize.height = req->height;
841 console_input_events_append( screen_buffer->input->evt, &evt );
843 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
844 evt.u.update.top = 0;
845 evt.u.update.bottom = screen_buffer->height - 1;
846 console_input_events_append( screen_buffer->input->evt, &evt );
848 /* scroll window to display sb */
849 if (screen_buffer->win.right >= req->width)
851 screen_buffer->win.right -= screen_buffer->win.left;
852 screen_buffer->win.left = 0;
854 if (screen_buffer->win.bottom >= req->height)
856 screen_buffer->win.bottom -= screen_buffer->win.top;
857 screen_buffer->win.top = 0;
859 /* reset cursor if needed (normally, if cursor was outside of new sb, the
860 * window has been shifted so that the new position of the cursor will be
861 * visible */
862 cc = 0;
863 if (screen_buffer->cursor_x >= req->width)
865 screen_buffer->cursor_x = req->width - 1;
866 cc++;
868 if (screen_buffer->cursor_y >= req->height)
870 screen_buffer->cursor_y = req->height - 1;
871 cc++;
873 if (cc)
875 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
876 evt.u.cursor_pos.x = req->cursor_x;
877 evt.u.cursor_pos.y = req->cursor_y;
878 console_input_events_append( screen_buffer->input->evt, &evt );
881 if (screen_buffer == screen_buffer->input->active &&
882 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
884 INPUT_RECORD ir;
885 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
886 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
887 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
888 write_console_input( screen_buffer->input, 1, &ir );
891 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
893 screen_buffer->attr = req->attr;
895 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
897 if (req->win_left < 0 || req->win_left > req->win_right ||
898 req->win_right >= screen_buffer->width ||
899 req->win_top < 0 || req->win_top > req->win_bottom ||
900 req->win_bottom >= screen_buffer->height)
902 set_error( STATUS_INVALID_PARAMETER );
903 return 0;
905 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
906 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
908 screen_buffer->win.left = req->win_left;
909 screen_buffer->win.top = req->win_top;
910 screen_buffer->win.right = req->win_right;
911 screen_buffer->win.bottom = req->win_bottom;
912 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
913 evt.u.display.left = req->win_left;
914 evt.u.display.top = req->win_top;
915 evt.u.display.width = req->win_right - req->win_left + 1;
916 evt.u.display.height = req->win_bottom - req->win_top + 1;
917 console_input_events_append( screen_buffer->input->evt, &evt );
920 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
922 /* can only be done by renderer */
923 if (current->process->console != screen_buffer->input)
925 set_error( STATUS_INVALID_PARAMETER );
926 return 0;
929 screen_buffer->max_width = req->max_width;
930 screen_buffer->max_height = req->max_height;
933 return 1;
936 /* appends a new line to history (history is a fixed size array) */
937 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
939 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
941 if (!ptr)
942 return;
944 if (!console || !console->history_size)
946 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
947 free( ptr );
948 return;
951 memcpy( ptr, buf, len * sizeof(WCHAR) );
952 ptr[len] = 0;
954 if (console->history_mode && console->history_index &&
955 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
957 /* ok, mode ask us to not use twice the same string...
958 * so just free mem and returns
960 set_error( STATUS_ALIAS_EXISTS );
961 free(ptr);
962 return;
965 if (console->history_index < console->history_size)
967 console->history[console->history_index++] = ptr;
969 else
971 free( console->history[0]) ;
972 memmove( &console->history[0], &console->history[1],
973 (console->history_size - 1) * sizeof(WCHAR*) );
974 console->history[console->history_size - 1] = ptr;
978 /* returns a line from the cache */
979 static data_size_t console_input_get_hist( struct console_input *console, int index )
981 data_size_t ret = 0;
983 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
984 else
986 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
987 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
989 return ret;
992 /* dumb dump */
993 static void console_input_dump( struct object *obj, int verbose )
995 struct console_input *console = (struct console_input *)obj;
996 assert( obj->ops == &console_input_ops );
997 fprintf( stderr, "Console input active=%p evt=%p\n",
998 console->active, console->evt );
1001 static void console_input_destroy( struct object *obj )
1003 struct console_input* console_in = (struct console_input *)obj;
1004 struct screen_buffer* curr;
1005 int i;
1007 assert( obj->ops == &console_input_ops );
1008 free( console_in->title );
1009 free( console_in->records );
1011 if (console_in->active) release_object( console_in->active );
1012 console_in->active = NULL;
1014 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1016 if (curr->input == console_in) curr->input = NULL;
1019 release_object( console_in->evt );
1020 console_in->evt = NULL;
1021 release_object( console_in->event );
1023 for (i = 0; i < console_in->history_size; i++)
1024 if (console_in->history[i]) free( console_in->history[i] );
1025 free( console_in->history );
1028 static void screen_buffer_dump( struct object *obj, int verbose )
1030 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1031 assert( obj->ops == &screen_buffer_ops );
1033 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1036 static void screen_buffer_destroy( struct object *obj )
1038 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1040 assert( obj->ops == &screen_buffer_ops );
1042 list_remove( &screen_buffer->entry );
1044 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1046 struct screen_buffer *sb;
1048 screen_buffer->input->active = NULL;
1049 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1051 if (sb->input == screen_buffer->input)
1053 sb->input->active = sb;
1054 break;
1058 free( screen_buffer->data );
1061 /* write data into a screen buffer */
1062 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1063 const void* data, enum char_info_mode mode,
1064 int x, int y, int wrap )
1066 unsigned int i;
1067 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1069 if (y >= screen_buffer->height) return 0;
1071 if (wrap)
1072 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1073 else
1074 end = screen_buffer->data + (y+1) * screen_buffer->width;
1076 switch(mode)
1078 case CHAR_INFO_MODE_TEXT:
1080 const WCHAR *ptr = data;
1081 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1083 break;
1084 case CHAR_INFO_MODE_ATTR:
1086 const unsigned short *ptr = data;
1087 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1089 break;
1090 case CHAR_INFO_MODE_TEXTATTR:
1092 const char_info_t *ptr = data;
1093 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1095 break;
1096 case CHAR_INFO_MODE_TEXTSTDATTR:
1098 const WCHAR *ptr = data;
1099 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1101 dest->ch = ptr[i];
1102 dest->attr = screen_buffer->attr;
1105 break;
1106 default:
1107 set_error( STATUS_INVALID_PARAMETER );
1108 return 0;
1111 if (i && screen_buffer == screen_buffer->input->active)
1113 struct console_renderer_event evt;
1114 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1115 memset(&evt.u, 0, sizeof(evt.u));
1116 evt.u.update.top = y + x / screen_buffer->width;
1117 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1118 console_input_events_append( screen_buffer->input->evt, &evt );
1120 return i;
1123 /* fill a screen buffer with uniform data */
1124 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1125 enum char_info_mode mode, int x, int y, int count, int wrap )
1127 int i;
1128 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1130 if (y >= screen_buffer->height) return 0;
1132 if (wrap)
1133 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1134 else
1135 end = screen_buffer->data + (y+1) * screen_buffer->width;
1137 if (count > end - dest) count = end - dest;
1139 switch(mode)
1141 case CHAR_INFO_MODE_TEXT:
1142 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1143 break;
1144 case CHAR_INFO_MODE_ATTR:
1145 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1146 break;
1147 case CHAR_INFO_MODE_TEXTATTR:
1148 for (i = 0; i < count; i++) dest[i] = data;
1149 break;
1150 case CHAR_INFO_MODE_TEXTSTDATTR:
1151 for (i = 0; i < count; i++)
1153 dest[i].ch = data.ch;
1154 dest[i].attr = screen_buffer->attr;
1156 break;
1157 default:
1158 set_error( STATUS_INVALID_PARAMETER );
1159 return 0;
1162 if (count && screen_buffer == screen_buffer->input->active)
1164 struct console_renderer_event evt;
1165 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1166 memset(&evt.u, 0, sizeof(evt.u));
1167 evt.u.update.top = y;
1168 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1169 console_input_events_append( screen_buffer->input->evt, &evt );
1171 return i;
1174 /* read data from a screen buffer */
1175 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1176 enum char_info_mode mode, int wrap )
1178 int i;
1179 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1181 if (y >= screen_buffer->height) return;
1183 if (wrap)
1184 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1185 else
1186 end = screen_buffer->data + (y+1) * screen_buffer->width;
1188 switch(mode)
1190 case CHAR_INFO_MODE_TEXT:
1192 WCHAR *data;
1193 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1194 if ((data = set_reply_data_size( count * sizeof(*data) )))
1196 for (i = 0; i < count; i++) data[i] = src[i].ch;
1199 break;
1200 case CHAR_INFO_MODE_ATTR:
1202 unsigned short *data;
1203 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1204 if ((data = set_reply_data_size( count * sizeof(*data) )))
1206 for (i = 0; i < count; i++) data[i] = src[i].attr;
1209 break;
1210 case CHAR_INFO_MODE_TEXTATTR:
1212 char_info_t *data;
1213 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1214 if ((data = set_reply_data_size( count * sizeof(*data) )))
1216 for (i = 0; i < count; i++) data[i] = src[i];
1219 break;
1220 default:
1221 set_error( STATUS_INVALID_PARAMETER );
1222 break;
1226 /* scroll parts of a screen buffer */
1227 static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1228 int w, int h )
1230 struct screen_buffer *screen_buffer;
1231 int j;
1232 char_info_t *psrc, *pdst;
1233 struct console_renderer_event evt;
1235 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1236 CONSOLE_READ, &screen_buffer_ops )))
1237 return;
1238 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1239 xsrc + w > screen_buffer->width ||
1240 xdst + w > screen_buffer->width ||
1241 ysrc + h > screen_buffer->height ||
1242 ydst + h > screen_buffer->height ||
1243 w == 0 || h == 0)
1245 set_error( STATUS_INVALID_PARAMETER );
1246 release_object( screen_buffer );
1247 return;
1250 if (ysrc < ydst)
1252 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1253 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1255 for (j = h; j > 0; j--)
1257 memcpy(pdst, psrc, w * sizeof(*pdst) );
1258 pdst -= screen_buffer->width;
1259 psrc -= screen_buffer->width;
1262 else
1264 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1265 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1267 for (j = 0; j < h; j++)
1269 /* we use memmove here because when psrc and pdst are the same,
1270 * copies are done on the same row, so the dst and src blocks
1271 * can overlap */
1272 memmove( pdst, psrc, w * sizeof(*pdst) );
1273 pdst += screen_buffer->width;
1274 psrc += screen_buffer->width;
1278 /* FIXME: this could be enhanced, by signalling scroll */
1279 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1280 memset(&evt.u, 0, sizeof(evt.u));
1281 evt.u.update.top = min(ysrc, ydst);
1282 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1283 console_input_events_append( screen_buffer->input->evt, &evt );
1285 release_object( screen_buffer );
1288 /* allocate a console for the renderer */
1289 DECL_HANDLER(alloc_console)
1291 obj_handle_t in = 0;
1292 obj_handle_t evt = 0;
1293 struct process *process;
1294 struct process *renderer = current->process;
1295 struct console_input *console;
1297 if (req->pid)
1299 if (!(process = get_process_from_id( req->pid ))) return;
1301 else
1303 if (!(process = renderer->parent))
1305 set_error( STATUS_ACCESS_DENIED );
1306 return;
1308 grab_object( process );
1311 if (process != renderer && process->console)
1313 set_error( STATUS_ACCESS_DENIED );
1314 goto the_end;
1316 if ((console = (struct console_input*)create_console_input( current )))
1318 if ((in = alloc_handle( renderer, console, req->access, req->attributes )))
1320 if ((evt = alloc_handle( renderer, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1322 if (process != renderer)
1324 process->console = (struct console_input*)grab_object( console );
1325 console->num_proc++;
1327 reply->handle_in = in;
1328 reply->event = evt;
1329 release_object( console );
1330 goto the_end;
1332 close_handle( renderer, in );
1334 free_console( process );
1336 the_end:
1337 release_object( process );
1340 /* free the console of the current process */
1341 DECL_HANDLER(free_console)
1343 free_console( current->process );
1346 /* let the renderer peek the events it's waiting on */
1347 DECL_HANDLER(get_console_renderer_events)
1349 struct console_input_events *evt;
1351 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1352 CONSOLE_READ, &console_input_events_ops );
1353 if (!evt) return;
1354 console_input_events_get( evt );
1355 release_object( evt );
1358 /* open a handle to the process console */
1359 DECL_HANDLER(open_console)
1361 struct object *obj = NULL;
1363 reply->handle = 0;
1364 if (req->from == (obj_handle_t)0)
1366 if (current->process->console && current->process->console->renderer)
1367 obj = grab_object( (struct object*)current->process->console );
1369 else if (req->from == (obj_handle_t)1)
1371 if (current->process->console && current->process->console->renderer &&
1372 current->process->console->active)
1373 obj = grab_object( (struct object*)current->process->console->active );
1375 else if ((obj = get_handle_obj( current->process, req->from,
1376 CONSOLE_READ|CONSOLE_WRITE, &console_input_ops )))
1378 struct console_input *console = (struct console_input *)obj;
1379 obj = (console->active) ? grab_object( console->active ) : NULL;
1380 release_object( console );
1383 /* FIXME: req->share is not used (as in screen buffer creation) */
1384 if (obj)
1386 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1387 release_object( obj );
1389 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1392 /* set info about a console input */
1393 DECL_HANDLER(set_console_input_info)
1395 set_console_input_info( req, get_req_data(), get_req_data_size() );
1398 /* get info about a console (output only) */
1399 DECL_HANDLER(get_console_input_info)
1401 struct console_input *console;
1403 if (!(console = console_input_get( req->handle, CONSOLE_READ ))) return;
1404 if (console->title)
1406 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1407 if (len > get_reply_max_size()) len = get_reply_max_size();
1408 set_reply_data( console->title, len );
1410 reply->history_mode = console->history_mode;
1411 reply->history_size = console->history_size;
1412 reply->history_index = console->history_index;
1413 reply->edition_mode = console->edition_mode;
1414 reply->input_cp = console->input_cp;
1415 reply->output_cp = console->output_cp;
1417 release_object( console );
1420 /* get a console mode (input or output) */
1421 DECL_HANDLER(get_console_mode)
1423 reply->mode = get_console_mode( req->handle );
1426 /* set a console mode (input or output) */
1427 DECL_HANDLER(set_console_mode)
1429 set_console_mode( req->handle, req->mode );
1432 /* add input records to a console input queue */
1433 DECL_HANDLER(write_console_input)
1435 struct console_input *console;
1437 reply->written = 0;
1438 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1439 CONSOLE_WRITE, &console_input_ops )))
1440 return;
1441 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1442 get_req_data() );
1443 release_object( console );
1446 /* fetch input records from a console input queue */
1447 DECL_HANDLER(read_console_input)
1449 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1450 reply->read = read_console_input( req->handle, count, req->flush );
1453 /* appends a string to console's history */
1454 DECL_HANDLER(append_console_input_history)
1456 struct console_input *console;
1458 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1459 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1460 release_object( console );
1463 /* appends a string to console's history */
1464 DECL_HANDLER(get_console_input_history)
1466 struct console_input *console;
1468 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1469 reply->total = console_input_get_hist( console, req->index );
1470 release_object( console );
1473 /* creates a screen buffer */
1474 DECL_HANDLER(create_console_output)
1476 struct console_input* console;
1477 struct screen_buffer* screen_buffer;
1479 if (!(console = console_input_get( req->handle_in, CONSOLE_WRITE ))) return;
1481 screen_buffer = create_console_output( console );
1482 if (screen_buffer)
1484 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1485 * see file.c on how this could be done */
1486 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1487 release_object( screen_buffer );
1489 release_object( console );
1492 /* set info about a console screen buffer */
1493 DECL_HANDLER(set_console_output_info)
1495 struct screen_buffer *screen_buffer;
1497 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1498 CONSOLE_WRITE, &screen_buffer_ops)))
1500 set_console_output_info( screen_buffer, req );
1501 release_object( screen_buffer );
1505 /* get info about a console screen buffer */
1506 DECL_HANDLER(get_console_output_info)
1508 struct screen_buffer *screen_buffer;
1510 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1511 CONSOLE_READ, &screen_buffer_ops)))
1513 reply->cursor_size = screen_buffer->cursor_size;
1514 reply->cursor_visible = screen_buffer->cursor_visible;
1515 reply->cursor_x = screen_buffer->cursor_x;
1516 reply->cursor_y = screen_buffer->cursor_y;
1517 reply->width = screen_buffer->width;
1518 reply->height = screen_buffer->height;
1519 reply->attr = screen_buffer->attr;
1520 reply->win_left = screen_buffer->win.left;
1521 reply->win_top = screen_buffer->win.top;
1522 reply->win_right = screen_buffer->win.right;
1523 reply->win_bottom = screen_buffer->win.bottom;
1524 reply->max_width = screen_buffer->max_width;
1525 reply->max_height = screen_buffer->max_height;
1526 release_object( screen_buffer );
1530 /* read data (chars & attrs) from a screen buffer */
1531 DECL_HANDLER(read_console_output)
1533 struct screen_buffer *screen_buffer;
1535 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1536 CONSOLE_READ, &screen_buffer_ops )))
1538 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1539 reply->width = screen_buffer->width;
1540 reply->height = screen_buffer->height;
1541 release_object( screen_buffer );
1545 /* write data (char and/or attrs) to a screen buffer */
1546 DECL_HANDLER(write_console_output)
1548 struct screen_buffer *screen_buffer;
1550 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1551 CONSOLE_WRITE, &screen_buffer_ops)))
1553 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1554 req->mode, req->x, req->y, req->wrap );
1555 reply->width = screen_buffer->width;
1556 reply->height = screen_buffer->height;
1557 release_object( screen_buffer );
1561 /* fill a screen buffer with constant data (chars and/or attributes) */
1562 DECL_HANDLER(fill_console_output)
1564 struct screen_buffer *screen_buffer;
1566 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1567 CONSOLE_WRITE, &screen_buffer_ops)))
1569 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1570 req->x, req->y, req->count, req->wrap );
1571 release_object( screen_buffer );
1575 /* move a rect of data in a screen buffer */
1576 DECL_HANDLER(move_console_output)
1578 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1579 req->w, req->h );
1582 /* sends a signal to a console (process, group...) */
1583 DECL_HANDLER(send_console_signal)
1585 process_id_t group;
1587 group = req->group_id ? req->group_id : current->process->group_id;
1589 if (!group)
1590 set_error( STATUS_INVALID_PARAMETER );
1591 else
1592 propagate_console_signal( current->process->console, req->signal, group );
1595 /* get console which renderer is 'current' */
1596 static int cgwe_enum( struct process* process, void* user)
1598 if (process->console && process->console->renderer == current)
1600 *(struct console_input**)user = process->console;
1601 return 1;
1603 return 0;
1606 DECL_HANDLER(get_console_wait_event)
1608 struct console_input* console = NULL;
1610 if (current->process->console && current->process->console->renderer)
1611 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1612 else enum_processes(cgwe_enum, &console);
1614 if (console)
1616 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1617 release_object( console );
1619 else set_error( STATUS_INVALID_PARAMETER );