setupapi/tests: Fix some typos.
[wine/gsoc_dplay.git] / server / console.c
blobb931e056e935b00a5d423fa4b44cecc442473dd3
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 user_handle_t win; /* window handle if backend supports it */
67 struct event *event; /* event to wait on for input queue */
70 static unsigned int console_map_access( struct object *obj, unsigned int access );
72 static void console_input_dump( struct object *obj, int verbose );
73 static void console_input_destroy( struct object *obj );
75 static const struct object_ops console_input_ops =
77 sizeof(struct console_input), /* size */
78 console_input_dump, /* dump */
79 no_get_type, /* get_type */
80 no_add_queue, /* add_queue */
81 NULL, /* remove_queue */
82 NULL, /* signaled */
83 no_satisfied, /* satisfied */
84 no_signal, /* signal */
85 no_get_fd, /* get_fd */
86 console_map_access, /* map_access */
87 default_get_sd, /* get_sd */
88 default_set_sd, /* set_sd */
89 no_lookup_name, /* lookup_name */
90 no_open_file, /* open_file */
91 no_close_handle, /* close_handle */
92 console_input_destroy /* destroy */
95 static void console_input_events_dump( struct object *obj, int verbose );
96 static void console_input_events_destroy( struct object *obj );
97 static int console_input_events_signaled( struct object *obj, struct thread *thread );
99 struct console_input_events
101 struct object obj; /* object header */
102 int num_alloc; /* number of allocated events */
103 int num_used; /* number of actually used events */
104 struct console_renderer_event* events;
107 static const struct object_ops console_input_events_ops =
109 sizeof(struct console_input_events), /* size */
110 console_input_events_dump, /* dump */
111 no_get_type, /* get_type */
112 add_queue, /* add_queue */
113 remove_queue, /* remove_queue */
114 console_input_events_signaled, /* signaled */
115 no_satisfied, /* satisfied */
116 no_signal, /* signal */
117 no_get_fd, /* get_fd */
118 console_map_access, /* map_access */
119 default_get_sd, /* get_sd */
120 default_set_sd, /* set_sd */
121 no_lookup_name, /* lookup_name */
122 no_open_file, /* open_file */
123 no_close_handle, /* close_handle */
124 console_input_events_destroy /* destroy */
127 struct screen_buffer
129 struct object obj; /* object header */
130 struct list entry; /* entry in list of all screen buffers */
131 struct console_input *input; /* associated console input */
132 int mode; /* output mode */
133 int cursor_size; /* size of cursor (percentage filled) */
134 int cursor_visible;/* cursor visibility flag */
135 int cursor_x; /* position of cursor */
136 int cursor_y; /* position of cursor */
137 int width; /* size (w-h) of the screen buffer */
138 int height;
139 int max_width; /* size (w-h) of the window given font size */
140 int max_height;
141 char_info_t *data; /* the data for each cell - a width x height matrix */
142 unsigned short attr; /* default attribute for screen buffer */
143 rectangle_t win; /* current visible window on the screen buffer *
144 * as seen in wineconsole */
147 static void screen_buffer_dump( struct object *obj, int verbose );
148 static void screen_buffer_destroy( struct object *obj );
150 static const struct object_ops screen_buffer_ops =
152 sizeof(struct screen_buffer), /* size */
153 screen_buffer_dump, /* dump */
154 no_get_type, /* get_type */
155 no_add_queue, /* add_queue */
156 NULL, /* remove_queue */
157 NULL, /* signaled */
158 NULL, /* satisfied */
159 no_signal, /* signal */
160 no_get_fd, /* get_fd */
161 console_map_access, /* map_access */
162 default_get_sd, /* get_sd */
163 default_set_sd, /* set_sd */
164 no_lookup_name, /* lookup_name */
165 no_open_file, /* open_file */
166 no_close_handle, /* close_handle */
167 screen_buffer_destroy /* destroy */
170 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
172 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
174 /* access mapping for all console objects */
175 static unsigned int console_map_access( struct object *obj, unsigned int access )
177 if (access & GENERIC_READ) access |= SYNCHRONIZE | STANDARD_RIGHTS_READ | CONSOLE_READ;
178 if (access & GENERIC_WRITE) access |= SYNCHRONIZE | STANDARD_RIGHTS_WRITE | CONSOLE_WRITE;
179 if (access & GENERIC_EXECUTE) access |= SYNCHRONIZE | STANDARD_RIGHTS_EXECUTE;
180 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | CONSOLE_READ | CONSOLE_WRITE;
181 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
184 /* dumps the renderer events of a console */
185 static void console_input_events_dump( struct object *obj, int verbose )
187 struct console_input_events *evts = (struct console_input_events *)obj;
188 assert( obj->ops == &console_input_events_ops );
189 fprintf( stderr, "Console input events: %d/%d events\n",
190 evts->num_used, evts->num_alloc );
193 /* destroys the renderer events of a console */
194 static void console_input_events_destroy( struct object *obj )
196 struct console_input_events *evts = (struct console_input_events *)obj;
197 assert( obj->ops == &console_input_events_ops );
198 free( evts->events );
201 /* the renderer events list is signaled when it's not empty */
202 static int console_input_events_signaled( struct object *obj, struct thread *thread )
204 struct console_input_events *evts = (struct console_input_events *)obj;
205 assert( obj->ops == &console_input_events_ops );
206 return (evts->num_used != 0);
209 /* add an event to the console's renderer events list */
210 static void console_input_events_append( struct console_input_events* evts,
211 struct console_renderer_event* evt)
213 int collapsed = FALSE;
215 /* to be done even when evt has been generated by the rendere ? */
217 /* try to collapse evt into current queue's events */
218 if (evts->num_used)
220 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
222 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
223 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
225 /* if two update events overlap, collapse them into a single one */
226 if (last->u.update.bottom + 1 >= evt->u.update.top &&
227 evt->u.update.bottom + 1 >= last->u.update.top)
229 last->u.update.top = min(last->u.update.top, evt->u.update.top);
230 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
231 collapsed = TRUE;
235 if (!collapsed)
237 if (evts->num_used == evts->num_alloc)
239 evts->num_alloc += 16;
240 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
241 assert(evts->events);
243 evts->events[evts->num_used++] = *evt;
245 wake_up( &evts->obj, 0 );
248 /* retrieves events from the console's renderer events list */
249 static void console_input_events_get( struct console_input_events* evts )
251 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
253 if (num > evts->num_used) num = evts->num_used;
254 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
255 if (num < evts->num_used)
257 memmove( &evts->events[0], &evts->events[num],
258 (evts->num_used - num) * sizeof(evts->events[0]) );
260 evts->num_used -= num;
263 static struct console_input_events *create_console_input_events(void)
265 struct console_input_events* evt;
267 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
268 evt->num_alloc = evt->num_used = 0;
269 evt->events = NULL;
270 return evt;
273 static struct object *create_console_input( struct thread* renderer )
275 struct console_input *console_input;
277 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
278 console_input->renderer = renderer;
279 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
280 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
281 console_input->num_proc = 0;
282 console_input->active = NULL;
283 console_input->recnum = 0;
284 console_input->records = NULL;
285 console_input->evt = create_console_input_events();
286 console_input->title = NULL;
287 console_input->history_size = 50;
288 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
289 console_input->history_index = 0;
290 console_input->history_mode = 0;
291 console_input->edition_mode = 0;
292 console_input->input_cp = 0;
293 console_input->output_cp = 0;
294 console_input->win = 0;
295 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
297 if (!console_input->history || !console_input->evt)
299 release_object( console_input );
300 return NULL;
302 return &console_input->obj;
305 static void generate_sb_initial_events( struct console_input *console_input )
307 struct screen_buffer *screen_buffer = console_input->active;
308 struct console_renderer_event evt;
310 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
311 memset(&evt.u, 0, sizeof(evt.u));
312 console_input_events_append( console_input->evt, &evt );
314 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
315 evt.u.resize.width = screen_buffer->width;
316 evt.u.resize.height = screen_buffer->height;
317 console_input_events_append( console_input->evt, &evt );
319 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
320 evt.u.display.left = screen_buffer->win.left;
321 evt.u.display.top = screen_buffer->win.top;
322 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
323 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
324 console_input_events_append( console_input->evt, &evt );
326 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
327 evt.u.update.top = 0;
328 evt.u.update.bottom = screen_buffer->height - 1;
329 console_input_events_append( console_input->evt, &evt );
331 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
332 evt.u.cursor_geom.size = screen_buffer->cursor_size;
333 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
334 console_input_events_append( console_input->evt, &evt );
336 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
337 evt.u.cursor_pos.x = screen_buffer->cursor_x;
338 evt.u.cursor_pos.y = screen_buffer->cursor_y;
339 console_input_events_append( console_input->evt, &evt );
342 static struct screen_buffer *create_console_output( struct console_input *console_input )
344 struct screen_buffer *screen_buffer;
345 int i;
347 if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
348 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
349 screen_buffer->input = console_input;
350 screen_buffer->cursor_size = 100;
351 screen_buffer->cursor_visible = 1;
352 screen_buffer->width = 80;
353 screen_buffer->height = 150;
354 screen_buffer->max_width = 80;
355 screen_buffer->max_height = 25;
356 screen_buffer->cursor_x = 0;
357 screen_buffer->cursor_y = 0;
358 screen_buffer->attr = 0x0F;
359 screen_buffer->win.left = 0;
360 screen_buffer->win.right = screen_buffer->max_width - 1;
361 screen_buffer->win.top = 0;
362 screen_buffer->win.bottom = screen_buffer->max_height - 1;
364 list_add_head( &screen_buffer_list, &screen_buffer->entry );
366 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
367 sizeof(*screen_buffer->data) )))
369 release_object( screen_buffer );
370 return NULL;
372 /* clear the first row */
373 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
374 /* and copy it to all other rows */
375 for (i = 1; i < screen_buffer->height; i++)
376 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
377 screen_buffer->width * sizeof(char_info_t) );
379 if (!console_input->active)
381 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
382 generate_sb_initial_events( console_input );
384 return screen_buffer;
387 /* free the console for this process */
388 int free_console( struct process *process )
390 struct console_input* console = process->console;
392 if (!console || !console->renderer) return 0;
394 process->console = NULL;
395 if (--console->num_proc == 0)
397 /* all processes have terminated... tell the renderer to terminate too */
398 struct console_renderer_event evt;
399 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
400 memset(&evt.u, 0, sizeof(evt.u));
401 console_input_events_append( console->evt, &evt );
403 release_object( console );
405 return 1;
408 /* let process inherit the console from parent... this handle two cases :
409 * 1/ generic console inheritance
410 * 2/ parent is a renderer which launches process, and process should attach to the console
411 * renderered by parent
413 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
415 int done = 0;
416 struct process* parent = parent_thread->process;
418 /* if parent is a renderer, then attach current process to its console
419 * a bit hacky....
421 if (hconin)
423 struct console_input* console;
425 /* FIXME: should we check some access rights ? */
426 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
427 0, &console_input_ops )))
429 if (console->renderer == parent_thread)
431 process->console = (struct console_input*)grab_object( console );
432 process->console->num_proc++;
433 done = 1;
435 release_object( console );
437 else clear_error(); /* ignore error */
439 /* otherwise, if parent has a console, attach child to this console */
440 if (!done && parent->console)
442 assert(parent->console->renderer);
443 process->console = (struct console_input*)grab_object( parent->console );
444 process->console->num_proc++;
448 struct thread *console_get_renderer( struct console_input *console )
450 return console->renderer;
453 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
455 struct console_input* console = NULL;
457 if (handle)
458 console = (struct console_input *)get_handle_obj( current->process, handle,
459 access, &console_input_ops );
460 else if (current->process->console)
462 assert( current->process->console->renderer );
463 console = (struct console_input *)grab_object( current->process->console );
466 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
467 return console;
470 struct console_signal_info
472 struct console_input *console;
473 process_id_t group;
474 int signal;
477 static int propagate_console_signal_cb(struct process *process, void *user)
479 struct console_signal_info* csi = (struct console_signal_info*)user;
481 if (process->console == csi->console && process->running_threads &&
482 (!csi->group || process->group_id == csi->group))
484 /* find a suitable thread to signal */
485 struct thread *thread;
486 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
488 if (send_thread_signal( thread, csi->signal )) break;
491 return FALSE;
494 static void propagate_console_signal( struct console_input *console,
495 int sig, process_id_t group_id )
497 struct console_signal_info csi;
499 if (!console)
501 set_error( STATUS_INVALID_PARAMETER );
502 return;
504 /* FIXME: should support the other events (like CTRL_BREAK) */
505 if (sig != CTRL_C_EVENT)
507 set_error( STATUS_NOT_IMPLEMENTED );
508 return;
510 csi.console = console;
511 csi.signal = SIGINT;
512 csi.group = group_id;
514 enum_processes(propagate_console_signal_cb, &csi);
517 static int get_console_mode( obj_handle_t handle )
519 struct object *obj;
520 int ret = 0;
522 if ((obj = get_handle_obj( current->process, handle, CONSOLE_READ, NULL )))
524 if (obj->ops == &console_input_ops)
525 ret = ((struct console_input *)obj)->mode;
526 else if (obj->ops == &screen_buffer_ops)
527 ret = ((struct screen_buffer *)obj)->mode;
528 else
529 set_error( STATUS_OBJECT_TYPE_MISMATCH );
530 release_object( obj );
532 return ret;
535 /* changes the mode of either a console input or a screen buffer */
536 static int set_console_mode( obj_handle_t handle, int mode )
538 struct object *obj;
539 int ret = 0;
541 if (!(obj = get_handle_obj( current->process, handle, CONSOLE_WRITE, NULL )))
542 return 0;
543 if (obj->ops == &console_input_ops)
545 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
546 ((struct console_input *)obj)->mode = mode;
547 ret = 1;
549 else if (obj->ops == &screen_buffer_ops)
551 ((struct screen_buffer *)obj)->mode = mode;
552 ret = 1;
554 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
555 release_object( obj );
556 return ret;
559 /* add input events to a console input queue */
560 static int write_console_input( struct console_input* console, int count,
561 const INPUT_RECORD *records )
563 INPUT_RECORD *new_rec;
565 if (!count) return 0;
566 if (!(new_rec = realloc( console->records,
567 (console->recnum + count) * sizeof(INPUT_RECORD) )))
569 set_error( STATUS_NO_MEMORY );
570 release_object( console );
571 return -1;
573 console->records = new_rec;
574 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
576 if (console->mode & ENABLE_PROCESSED_INPUT)
578 int i = 0;
579 while (i < count)
581 if (records[i].EventType == KEY_EVENT &&
582 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
583 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
585 if (i != count - 1)
586 memcpy( &console->records[console->recnum + i],
587 &console->records[console->recnum + i + 1],
588 (count - i - 1) * sizeof(INPUT_RECORD) );
589 count--;
590 if (records[i].Event.KeyEvent.bKeyDown)
592 /* send SIGINT to all processes attached to this console */
593 propagate_console_signal( console, CTRL_C_EVENT, 0 );
596 else i++;
599 if (!console->recnum && count) set_event( console->event );
600 console->recnum += count;
601 return count;
604 /* retrieve a pointer to the console input records */
605 static int read_console_input( obj_handle_t handle, int count, int flush )
607 struct console_input *console;
609 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
610 CONSOLE_READ, &console_input_ops )))
611 return -1;
613 if (!count)
615 /* special case: do not retrieve anything, but return
616 * the total number of records available */
617 count = console->recnum;
619 else
621 if (count > console->recnum) count = console->recnum;
622 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
624 if (flush)
626 int i;
627 for (i = count; i < console->recnum; i++)
628 console->records[i-count] = console->records[i];
629 if ((console->recnum -= count) > 0)
631 INPUT_RECORD *new_rec = realloc( console->records,
632 console->recnum * sizeof(INPUT_RECORD) );
633 if (new_rec) console->records = new_rec;
635 else
637 free( console->records );
638 console->records = NULL;
639 reset_event( console->event );
642 release_object( console );
643 return count;
646 /* set misc console input information */
647 static int set_console_input_info( const struct set_console_input_info_request *req,
648 const WCHAR *title, data_size_t len )
650 struct console_input *console;
651 struct console_renderer_event evt;
653 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) goto error;
655 memset(&evt.u, 0, sizeof(evt.u));
656 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
658 struct screen_buffer *screen_buffer;
660 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
661 CONSOLE_WRITE, &screen_buffer_ops );
662 if (!screen_buffer || screen_buffer->input != console)
664 set_error( STATUS_INVALID_HANDLE );
665 if (screen_buffer) release_object( screen_buffer );
666 goto error;
669 if (screen_buffer != console->active)
671 if (console->active) release_object( console->active );
672 console->active = screen_buffer;
673 generate_sb_initial_events( console );
675 else
676 release_object( screen_buffer );
678 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
680 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
681 if (new_title)
683 memcpy( new_title, title, len );
684 new_title[len / sizeof(WCHAR)] = 0;
685 free( console->title );
686 console->title = new_title;
687 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
688 console_input_events_append( console->evt, &evt );
691 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
693 console->history_mode = req->history_mode;
695 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
696 console->history_size != req->history_size)
698 WCHAR** mem = NULL;
699 int i;
700 int delta;
702 if (req->history_size)
704 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
705 if (!mem) goto error;
706 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
709 delta = (console->history_index > req->history_size) ?
710 (console->history_index - req->history_size) : 0;
712 for (i = delta; i < console->history_index; i++)
714 mem[i - delta] = console->history[i];
715 console->history[i] = NULL;
717 console->history_index -= delta;
719 for (i = 0; i < console->history_size; i++)
720 free( console->history[i] );
721 free( console->history );
722 console->history = mem;
723 console->history_size = req->history_size;
725 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
727 console->edition_mode = req->edition_mode;
729 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
731 console->input_cp = req->input_cp;
733 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
735 console->output_cp = req->output_cp;
737 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
739 console->win = req->win;
741 release_object( console );
742 return 1;
743 error:
744 if (console) release_object( console );
745 return 0;
748 /* resize a screen buffer */
749 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
750 int new_width, int new_height )
752 int i, old_width, old_height, copy_width, copy_height;
753 char_info_t *new_data;
755 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
757 set_error( STATUS_NO_MEMORY );
758 return 0;
760 old_width = screen_buffer->width;
761 old_height = screen_buffer->height;
762 copy_width = min( old_width, new_width );
763 copy_height = min( old_height, new_height );
765 /* copy all the rows */
766 for (i = 0; i < copy_height; i++)
768 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
769 copy_width * sizeof(char_info_t) );
772 /* clear the end of each row */
773 if (new_width > old_width)
775 /* fill first row */
776 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
777 /* and blast it to the other rows */
778 for (i = 1; i < copy_height; i++)
779 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
780 (new_width - old_width) * sizeof(char_info_t) );
783 /* clear remaining rows */
784 if (new_height > old_height)
786 /* fill first row */
787 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
788 /* and blast it to the other rows */
789 for (i = old_height+1; i < new_height; i++)
790 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
791 new_width * sizeof(char_info_t) );
793 free( screen_buffer->data );
794 screen_buffer->data = new_data;
795 screen_buffer->width = new_width;
796 screen_buffer->height = new_height;
797 return 1;
800 /* set misc screen buffer information */
801 static int set_console_output_info( struct screen_buffer *screen_buffer,
802 const struct set_console_output_info_request *req )
804 struct console_renderer_event evt;
806 memset(&evt.u, 0, sizeof(evt.u));
807 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
809 if (req->cursor_size < 1 || req->cursor_size > 100)
811 set_error( STATUS_INVALID_PARAMETER );
812 return 0;
814 if (screen_buffer->cursor_size != req->cursor_size ||
815 screen_buffer->cursor_visible != req->cursor_visible)
817 screen_buffer->cursor_size = req->cursor_size;
818 screen_buffer->cursor_visible = req->cursor_visible;
819 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
820 evt.u.cursor_geom.size = req->cursor_size;
821 evt.u.cursor_geom.visible = req->cursor_visible;
822 console_input_events_append( screen_buffer->input->evt, &evt );
825 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
827 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
828 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
830 set_error( STATUS_INVALID_PARAMETER );
831 return 0;
833 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
835 screen_buffer->cursor_x = req->cursor_x;
836 screen_buffer->cursor_y = req->cursor_y;
837 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
838 evt.u.cursor_pos.x = req->cursor_x;
839 evt.u.cursor_pos.y = req->cursor_y;
840 console_input_events_append( screen_buffer->input->evt, &evt );
843 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
845 unsigned cc;
847 /* new screen-buffer cannot be smaller than actual window */
848 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
849 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
851 set_error( STATUS_INVALID_PARAMETER );
852 return 0;
854 /* FIXME: there are also some basic minimum and max size to deal with */
855 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
857 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
858 evt.u.resize.width = req->width;
859 evt.u.resize.height = req->height;
860 console_input_events_append( screen_buffer->input->evt, &evt );
862 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
863 evt.u.update.top = 0;
864 evt.u.update.bottom = screen_buffer->height - 1;
865 console_input_events_append( screen_buffer->input->evt, &evt );
867 /* scroll window to display sb */
868 if (screen_buffer->win.right >= req->width)
870 screen_buffer->win.right -= screen_buffer->win.left;
871 screen_buffer->win.left = 0;
873 if (screen_buffer->win.bottom >= req->height)
875 screen_buffer->win.bottom -= screen_buffer->win.top;
876 screen_buffer->win.top = 0;
878 /* reset cursor if needed (normally, if cursor was outside of new sb, the
879 * window has been shifted so that the new position of the cursor will be
880 * visible */
881 cc = 0;
882 if (screen_buffer->cursor_x >= req->width)
884 screen_buffer->cursor_x = req->width - 1;
885 cc++;
887 if (screen_buffer->cursor_y >= req->height)
889 screen_buffer->cursor_y = req->height - 1;
890 cc++;
892 if (cc)
894 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
895 evt.u.cursor_pos.x = req->cursor_x;
896 evt.u.cursor_pos.y = req->cursor_y;
897 console_input_events_append( screen_buffer->input->evt, &evt );
900 if (screen_buffer == screen_buffer->input->active &&
901 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
903 INPUT_RECORD ir;
904 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
905 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
906 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
907 write_console_input( screen_buffer->input, 1, &ir );
910 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
912 screen_buffer->attr = req->attr;
914 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
916 if (req->win_left < 0 || req->win_left > req->win_right ||
917 req->win_right >= screen_buffer->width ||
918 req->win_top < 0 || req->win_top > req->win_bottom ||
919 req->win_bottom >= screen_buffer->height)
921 set_error( STATUS_INVALID_PARAMETER );
922 return 0;
924 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
925 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
927 screen_buffer->win.left = req->win_left;
928 screen_buffer->win.top = req->win_top;
929 screen_buffer->win.right = req->win_right;
930 screen_buffer->win.bottom = req->win_bottom;
931 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
932 evt.u.display.left = req->win_left;
933 evt.u.display.top = req->win_top;
934 evt.u.display.width = req->win_right - req->win_left + 1;
935 evt.u.display.height = req->win_bottom - req->win_top + 1;
936 console_input_events_append( screen_buffer->input->evt, &evt );
939 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
941 /* can only be done by renderer */
942 if (current->process->console != screen_buffer->input)
944 set_error( STATUS_INVALID_PARAMETER );
945 return 0;
948 screen_buffer->max_width = req->max_width;
949 screen_buffer->max_height = req->max_height;
952 return 1;
955 /* appends a new line to history (history is a fixed size array) */
956 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
958 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
960 if (!ptr)
961 return;
963 if (!console || !console->history_size)
965 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
966 free( ptr );
967 return;
970 memcpy( ptr, buf, len * sizeof(WCHAR) );
971 ptr[len] = 0;
973 if (console->history_mode && console->history_index &&
974 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
976 /* ok, mode ask us to not use twice the same string...
977 * so just free mem and returns
979 set_error( STATUS_ALIAS_EXISTS );
980 free(ptr);
981 return;
984 if (console->history_index < console->history_size)
986 console->history[console->history_index++] = ptr;
988 else
990 free( console->history[0]) ;
991 memmove( &console->history[0], &console->history[1],
992 (console->history_size - 1) * sizeof(WCHAR*) );
993 console->history[console->history_size - 1] = ptr;
997 /* returns a line from the cache */
998 static data_size_t console_input_get_hist( struct console_input *console, int index )
1000 data_size_t ret = 0;
1002 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1003 else
1005 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1006 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1008 return ret;
1011 /* dumb dump */
1012 static void console_input_dump( struct object *obj, int verbose )
1014 struct console_input *console = (struct console_input *)obj;
1015 assert( obj->ops == &console_input_ops );
1016 fprintf( stderr, "Console input active=%p evt=%p\n",
1017 console->active, console->evt );
1020 static void console_input_destroy( struct object *obj )
1022 struct console_input* console_in = (struct console_input *)obj;
1023 struct screen_buffer* curr;
1024 int i;
1026 assert( obj->ops == &console_input_ops );
1027 free( console_in->title );
1028 free( console_in->records );
1030 if (console_in->active) release_object( console_in->active );
1031 console_in->active = NULL;
1033 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1035 if (curr->input == console_in) curr->input = NULL;
1038 release_object( console_in->evt );
1039 console_in->evt = NULL;
1040 release_object( console_in->event );
1042 for (i = 0; i < console_in->history_size; i++)
1043 free( console_in->history[i] );
1044 free( console_in->history );
1047 static void screen_buffer_dump( struct object *obj, int verbose )
1049 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1050 assert( obj->ops == &screen_buffer_ops );
1052 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1055 static void screen_buffer_destroy( struct object *obj )
1057 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1059 assert( obj->ops == &screen_buffer_ops );
1061 list_remove( &screen_buffer->entry );
1063 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1065 struct screen_buffer *sb;
1067 screen_buffer->input->active = NULL;
1068 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1070 if (sb->input == screen_buffer->input)
1072 sb->input->active = sb;
1073 break;
1077 free( screen_buffer->data );
1080 /* write data into a screen buffer */
1081 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1082 const void* data, enum char_info_mode mode,
1083 int x, int y, int wrap )
1085 unsigned int i;
1086 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1088 if (y >= screen_buffer->height) return 0;
1090 if (wrap)
1091 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1092 else
1093 end = screen_buffer->data + (y+1) * screen_buffer->width;
1095 switch(mode)
1097 case CHAR_INFO_MODE_TEXT:
1099 const WCHAR *ptr = data;
1100 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1102 break;
1103 case CHAR_INFO_MODE_ATTR:
1105 const unsigned short *ptr = data;
1106 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1108 break;
1109 case CHAR_INFO_MODE_TEXTATTR:
1111 const char_info_t *ptr = data;
1112 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1114 break;
1115 case CHAR_INFO_MODE_TEXTSTDATTR:
1117 const WCHAR *ptr = data;
1118 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1120 dest->ch = ptr[i];
1121 dest->attr = screen_buffer->attr;
1124 break;
1125 default:
1126 set_error( STATUS_INVALID_PARAMETER );
1127 return 0;
1130 if (i && screen_buffer == screen_buffer->input->active)
1132 struct console_renderer_event evt;
1133 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1134 memset(&evt.u, 0, sizeof(evt.u));
1135 evt.u.update.top = y + x / screen_buffer->width;
1136 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1137 console_input_events_append( screen_buffer->input->evt, &evt );
1139 return i;
1142 /* fill a screen buffer with uniform data */
1143 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1144 enum char_info_mode mode, int x, int y, int count, int wrap )
1146 int i;
1147 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1149 if (y >= screen_buffer->height) return 0;
1151 if (wrap)
1152 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1153 else
1154 end = screen_buffer->data + (y+1) * screen_buffer->width;
1156 if (count > end - dest) count = end - dest;
1158 switch(mode)
1160 case CHAR_INFO_MODE_TEXT:
1161 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1162 break;
1163 case CHAR_INFO_MODE_ATTR:
1164 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1165 break;
1166 case CHAR_INFO_MODE_TEXTATTR:
1167 for (i = 0; i < count; i++) dest[i] = data;
1168 break;
1169 case CHAR_INFO_MODE_TEXTSTDATTR:
1170 for (i = 0; i < count; i++)
1172 dest[i].ch = data.ch;
1173 dest[i].attr = screen_buffer->attr;
1175 break;
1176 default:
1177 set_error( STATUS_INVALID_PARAMETER );
1178 return 0;
1181 if (count && screen_buffer == screen_buffer->input->active)
1183 struct console_renderer_event evt;
1184 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1185 memset(&evt.u, 0, sizeof(evt.u));
1186 evt.u.update.top = y;
1187 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1188 console_input_events_append( screen_buffer->input->evt, &evt );
1190 return i;
1193 /* read data from a screen buffer */
1194 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1195 enum char_info_mode mode, int wrap )
1197 int i;
1198 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1200 if (y >= screen_buffer->height) return;
1202 if (wrap)
1203 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1204 else
1205 end = screen_buffer->data + (y+1) * screen_buffer->width;
1207 switch(mode)
1209 case CHAR_INFO_MODE_TEXT:
1211 WCHAR *data;
1212 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1213 if ((data = set_reply_data_size( count * sizeof(*data) )))
1215 for (i = 0; i < count; i++) data[i] = src[i].ch;
1218 break;
1219 case CHAR_INFO_MODE_ATTR:
1221 unsigned short *data;
1222 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1223 if ((data = set_reply_data_size( count * sizeof(*data) )))
1225 for (i = 0; i < count; i++) data[i] = src[i].attr;
1228 break;
1229 case CHAR_INFO_MODE_TEXTATTR:
1231 char_info_t *data;
1232 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1233 if ((data = set_reply_data_size( count * sizeof(*data) )))
1235 for (i = 0; i < count; i++) data[i] = src[i];
1238 break;
1239 default:
1240 set_error( STATUS_INVALID_PARAMETER );
1241 break;
1245 /* scroll parts of a screen buffer */
1246 static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1247 int w, int h )
1249 struct screen_buffer *screen_buffer;
1250 int j;
1251 char_info_t *psrc, *pdst;
1252 struct console_renderer_event evt;
1254 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1255 CONSOLE_READ, &screen_buffer_ops )))
1256 return;
1257 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1258 xsrc + w > screen_buffer->width ||
1259 xdst + w > screen_buffer->width ||
1260 ysrc + h > screen_buffer->height ||
1261 ydst + h > screen_buffer->height ||
1262 w == 0 || h == 0)
1264 set_error( STATUS_INVALID_PARAMETER );
1265 release_object( screen_buffer );
1266 return;
1269 if (ysrc < ydst)
1271 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1272 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1274 for (j = h; j > 0; j--)
1276 memcpy(pdst, psrc, w * sizeof(*pdst) );
1277 pdst -= screen_buffer->width;
1278 psrc -= screen_buffer->width;
1281 else
1283 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1284 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1286 for (j = 0; j < h; j++)
1288 /* we use memmove here because when psrc and pdst are the same,
1289 * copies are done on the same row, so the dst and src blocks
1290 * can overlap */
1291 memmove( pdst, psrc, w * sizeof(*pdst) );
1292 pdst += screen_buffer->width;
1293 psrc += screen_buffer->width;
1297 /* FIXME: this could be enhanced, by signalling scroll */
1298 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1299 memset(&evt.u, 0, sizeof(evt.u));
1300 evt.u.update.top = min(ysrc, ydst);
1301 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1302 console_input_events_append( screen_buffer->input->evt, &evt );
1304 release_object( screen_buffer );
1307 /* allocate a console for the renderer */
1308 DECL_HANDLER(alloc_console)
1310 obj_handle_t in = 0;
1311 obj_handle_t evt = 0;
1312 struct process *process;
1313 struct process *renderer = current->process;
1314 struct console_input *console;
1316 if (req->pid)
1318 if (!(process = get_process_from_id( req->pid ))) return;
1320 else
1322 if (!(process = renderer->parent))
1324 set_error( STATUS_ACCESS_DENIED );
1325 return;
1327 grab_object( process );
1330 if (process != renderer && process->console)
1332 set_error( STATUS_ACCESS_DENIED );
1333 goto the_end;
1335 if ((console = (struct console_input*)create_console_input( current )))
1337 if ((in = alloc_handle( renderer, console, req->access, req->attributes )))
1339 if ((evt = alloc_handle( renderer, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1341 if (process != renderer)
1343 process->console = (struct console_input*)grab_object( console );
1344 console->num_proc++;
1346 reply->handle_in = in;
1347 reply->event = evt;
1348 release_object( console );
1349 goto the_end;
1351 close_handle( renderer, in );
1353 free_console( process );
1355 the_end:
1356 release_object( process );
1359 /* free the console of the current process */
1360 DECL_HANDLER(free_console)
1362 free_console( current->process );
1365 /* let the renderer peek the events it's waiting on */
1366 DECL_HANDLER(get_console_renderer_events)
1368 struct console_input_events *evt;
1370 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1371 CONSOLE_READ, &console_input_events_ops );
1372 if (!evt) return;
1373 console_input_events_get( evt );
1374 release_object( evt );
1377 /* open a handle to the process console */
1378 DECL_HANDLER(open_console)
1380 struct object *obj = NULL;
1382 reply->handle = 0;
1383 if (req->from == (obj_handle_t)0)
1385 if (current->process->console && current->process->console->renderer)
1386 obj = grab_object( (struct object*)current->process->console );
1388 else if (req->from == (obj_handle_t)1)
1390 if (current->process->console && current->process->console->renderer &&
1391 current->process->console->active)
1392 obj = grab_object( (struct object*)current->process->console->active );
1394 else if ((obj = get_handle_obj( current->process, req->from,
1395 CONSOLE_READ|CONSOLE_WRITE, &console_input_ops )))
1397 struct console_input *console = (struct console_input *)obj;
1398 obj = (console->active) ? grab_object( console->active ) : NULL;
1399 release_object( console );
1402 /* FIXME: req->share is not used (as in screen buffer creation) */
1403 if (obj)
1405 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1406 release_object( obj );
1408 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1411 /* set info about a console input */
1412 DECL_HANDLER(set_console_input_info)
1414 set_console_input_info( req, get_req_data(), get_req_data_size() );
1417 /* get info about a console (output only) */
1418 DECL_HANDLER(get_console_input_info)
1420 struct console_input *console;
1422 if (!(console = console_input_get( req->handle, CONSOLE_READ ))) return;
1423 if (console->title)
1425 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1426 if (len > get_reply_max_size()) len = get_reply_max_size();
1427 set_reply_data( console->title, len );
1429 reply->history_mode = console->history_mode;
1430 reply->history_size = console->history_size;
1431 reply->history_index = console->history_index;
1432 reply->edition_mode = console->edition_mode;
1433 reply->input_cp = console->input_cp;
1434 reply->output_cp = console->output_cp;
1435 reply->win = console->win;
1437 release_object( console );
1440 /* get a console mode (input or output) */
1441 DECL_HANDLER(get_console_mode)
1443 reply->mode = get_console_mode( req->handle );
1446 /* set a console mode (input or output) */
1447 DECL_HANDLER(set_console_mode)
1449 set_console_mode( req->handle, req->mode );
1452 /* add input records to a console input queue */
1453 DECL_HANDLER(write_console_input)
1455 struct console_input *console;
1457 reply->written = 0;
1458 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1459 CONSOLE_WRITE, &console_input_ops )))
1460 return;
1461 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1462 get_req_data() );
1463 release_object( console );
1466 /* fetch input records from a console input queue */
1467 DECL_HANDLER(read_console_input)
1469 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1470 reply->read = read_console_input( req->handle, count, req->flush );
1473 /* appends a string to console's history */
1474 DECL_HANDLER(append_console_input_history)
1476 struct console_input *console;
1478 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1479 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1480 release_object( console );
1483 /* appends a string to console's history */
1484 DECL_HANDLER(get_console_input_history)
1486 struct console_input *console;
1488 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1489 reply->total = console_input_get_hist( console, req->index );
1490 release_object( console );
1493 /* creates a screen buffer */
1494 DECL_HANDLER(create_console_output)
1496 struct console_input* console;
1497 struct screen_buffer* screen_buffer;
1499 if (!(console = console_input_get( req->handle_in, CONSOLE_WRITE ))) return;
1501 screen_buffer = create_console_output( console );
1502 if (screen_buffer)
1504 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1505 * see file.c on how this could be done */
1506 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1507 release_object( screen_buffer );
1509 release_object( console );
1512 /* set info about a console screen buffer */
1513 DECL_HANDLER(set_console_output_info)
1515 struct screen_buffer *screen_buffer;
1517 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1518 CONSOLE_WRITE, &screen_buffer_ops)))
1520 set_console_output_info( screen_buffer, req );
1521 release_object( screen_buffer );
1525 /* get info about a console screen buffer */
1526 DECL_HANDLER(get_console_output_info)
1528 struct screen_buffer *screen_buffer;
1530 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1531 CONSOLE_READ, &screen_buffer_ops)))
1533 reply->cursor_size = screen_buffer->cursor_size;
1534 reply->cursor_visible = screen_buffer->cursor_visible;
1535 reply->cursor_x = screen_buffer->cursor_x;
1536 reply->cursor_y = screen_buffer->cursor_y;
1537 reply->width = screen_buffer->width;
1538 reply->height = screen_buffer->height;
1539 reply->attr = screen_buffer->attr;
1540 reply->win_left = screen_buffer->win.left;
1541 reply->win_top = screen_buffer->win.top;
1542 reply->win_right = screen_buffer->win.right;
1543 reply->win_bottom = screen_buffer->win.bottom;
1544 reply->max_width = screen_buffer->max_width;
1545 reply->max_height = screen_buffer->max_height;
1546 release_object( screen_buffer );
1550 /* read data (chars & attrs) from a screen buffer */
1551 DECL_HANDLER(read_console_output)
1553 struct screen_buffer *screen_buffer;
1555 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1556 CONSOLE_READ, &screen_buffer_ops )))
1558 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1559 reply->width = screen_buffer->width;
1560 reply->height = screen_buffer->height;
1561 release_object( screen_buffer );
1565 /* write data (char and/or attrs) to a screen buffer */
1566 DECL_HANDLER(write_console_output)
1568 struct screen_buffer *screen_buffer;
1570 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1571 CONSOLE_WRITE, &screen_buffer_ops)))
1573 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1574 req->mode, req->x, req->y, req->wrap );
1575 reply->width = screen_buffer->width;
1576 reply->height = screen_buffer->height;
1577 release_object( screen_buffer );
1581 /* fill a screen buffer with constant data (chars and/or attributes) */
1582 DECL_HANDLER(fill_console_output)
1584 struct screen_buffer *screen_buffer;
1586 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1587 CONSOLE_WRITE, &screen_buffer_ops)))
1589 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1590 req->x, req->y, req->count, req->wrap );
1591 release_object( screen_buffer );
1595 /* move a rect of data in a screen buffer */
1596 DECL_HANDLER(move_console_output)
1598 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1599 req->w, req->h );
1602 /* sends a signal to a console (process, group...) */
1603 DECL_HANDLER(send_console_signal)
1605 process_id_t group;
1607 group = req->group_id ? req->group_id : current->process->group_id;
1609 if (!group)
1610 set_error( STATUS_INVALID_PARAMETER );
1611 else
1612 propagate_console_signal( current->process->console, req->signal, group );
1615 /* get console which renderer is 'current' */
1616 static int cgwe_enum( struct process* process, void* user)
1618 if (process->console && process->console->renderer == current)
1620 *(struct console_input**)user = process->console;
1621 return 1;
1623 return 0;
1626 DECL_HANDLER(get_console_wait_event)
1628 struct console_input* console = NULL;
1630 if (current->process->console && current->process->console->renderer)
1631 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1632 else enum_processes(cgwe_enum, &console);
1634 if (console)
1636 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1637 release_object( console );
1639 else set_error( STATUS_INVALID_PARAMETER );