wined3d: Fix handling of a special case in IWineD3DImpl_FillGLCaps() and adjust type...
[wine/wine64.git] / server / console.c
blob5f005884672ae6ecb5fd67b2aeb9b31f942d8d6b
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_add_queue, /* add_queue */
80 NULL, /* remove_queue */
81 NULL, /* signaled */
82 no_satisfied, /* satisfied */
83 no_signal, /* signal */
84 no_get_fd, /* get_fd */
85 console_map_access, /* map_access */
86 default_get_sd, /* get_sd */
87 default_set_sd, /* set_sd */
88 no_lookup_name, /* lookup_name */
89 no_open_file, /* open_file */
90 no_close_handle, /* close_handle */
91 console_input_destroy /* destroy */
94 static void console_input_events_dump( struct object *obj, int verbose );
95 static void console_input_events_destroy( struct object *obj );
96 static int console_input_events_signaled( struct object *obj, struct thread *thread );
98 struct console_input_events
100 struct object obj; /* object header */
101 int num_alloc; /* number of allocated events */
102 int num_used; /* number of actually used events */
103 struct console_renderer_event* events;
106 static const struct object_ops console_input_events_ops =
108 sizeof(struct console_input_events), /* size */
109 console_input_events_dump, /* dump */
110 add_queue, /* add_queue */
111 remove_queue, /* remove_queue */
112 console_input_events_signaled, /* signaled */
113 no_satisfied, /* satisfied */
114 no_signal, /* signal */
115 no_get_fd, /* get_fd */
116 console_map_access, /* map_access */
117 default_get_sd, /* get_sd */
118 default_set_sd, /* set_sd */
119 no_lookup_name, /* lookup_name */
120 no_open_file, /* open_file */
121 no_close_handle, /* close_handle */
122 console_input_events_destroy /* destroy */
125 struct screen_buffer
127 struct object obj; /* object header */
128 struct list entry; /* entry in list of all screen buffers */
129 struct console_input *input; /* associated console input */
130 int mode; /* output mode */
131 int cursor_size; /* size of cursor (percentage filled) */
132 int cursor_visible;/* cursor visibility flag */
133 int cursor_x; /* position of cursor */
134 int cursor_y; /* position of cursor */
135 int width; /* size (w-h) of the screen buffer */
136 int height;
137 int max_width; /* size (w-h) of the window given font size */
138 int max_height;
139 char_info_t *data; /* the data for each cell - a width x height matrix */
140 unsigned short attr; /* default attribute for screen buffer */
141 rectangle_t win; /* current visible window on the screen buffer *
142 * as seen in wineconsole */
145 static void screen_buffer_dump( struct object *obj, int verbose );
146 static void screen_buffer_destroy( struct object *obj );
148 static const struct object_ops screen_buffer_ops =
150 sizeof(struct screen_buffer), /* size */
151 screen_buffer_dump, /* dump */
152 no_add_queue, /* add_queue */
153 NULL, /* remove_queue */
154 NULL, /* signaled */
155 NULL, /* satisfied */
156 no_signal, /* signal */
157 no_get_fd, /* get_fd */
158 console_map_access, /* map_access */
159 default_get_sd, /* get_sd */
160 default_set_sd, /* set_sd */
161 no_lookup_name, /* lookup_name */
162 no_open_file, /* open_file */
163 no_close_handle, /* close_handle */
164 screen_buffer_destroy /* destroy */
167 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
169 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
171 /* access mapping for all console objects */
172 static unsigned int console_map_access( struct object *obj, unsigned int access )
174 if (access & GENERIC_READ) access |= SYNCHRONIZE | STANDARD_RIGHTS_READ | CONSOLE_READ;
175 if (access & GENERIC_WRITE) access |= SYNCHRONIZE | STANDARD_RIGHTS_WRITE | CONSOLE_WRITE;
176 if (access & GENERIC_EXECUTE) access |= SYNCHRONIZE | STANDARD_RIGHTS_EXECUTE;
177 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | CONSOLE_READ | CONSOLE_WRITE;
178 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
181 /* dumps the renderer events of a console */
182 static void console_input_events_dump( struct object *obj, int verbose )
184 struct console_input_events *evts = (struct console_input_events *)obj;
185 assert( obj->ops == &console_input_events_ops );
186 fprintf( stderr, "Console input events: %d/%d events\n",
187 evts->num_used, evts->num_alloc );
190 /* destroys the renderer events of a console */
191 static void console_input_events_destroy( struct object *obj )
193 struct console_input_events *evts = (struct console_input_events *)obj;
194 assert( obj->ops == &console_input_events_ops );
195 free( evts->events );
198 /* the renderer events list is signaled when it's not empty */
199 static int console_input_events_signaled( struct object *obj, struct thread *thread )
201 struct console_input_events *evts = (struct console_input_events *)obj;
202 assert( obj->ops == &console_input_events_ops );
203 return (evts->num_used != 0);
206 /* add an event to the console's renderer events list */
207 static void console_input_events_append( struct console_input_events* evts,
208 struct console_renderer_event* evt)
210 int collapsed = FALSE;
212 /* to be done even when evt has been generated by the rendere ? */
214 /* try to collapse evt into current queue's events */
215 if (evts->num_used)
217 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
219 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
220 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
222 /* if two update events overlap, collapse them into a single one */
223 if (last->u.update.bottom + 1 >= evt->u.update.top &&
224 evt->u.update.bottom + 1 >= last->u.update.top)
226 last->u.update.top = min(last->u.update.top, evt->u.update.top);
227 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
228 collapsed = TRUE;
232 if (!collapsed)
234 if (evts->num_used == evts->num_alloc)
236 evts->num_alloc += 16;
237 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
238 assert(evts->events);
240 evts->events[evts->num_used++] = *evt;
242 wake_up( &evts->obj, 0 );
245 /* retrieves events from the console's renderer events list */
246 static void console_input_events_get( struct console_input_events* evts )
248 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
250 if (num > evts->num_used) num = evts->num_used;
251 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
252 if (num < evts->num_used)
254 memmove( &evts->events[0], &evts->events[num],
255 (evts->num_used - num) * sizeof(evts->events[0]) );
257 evts->num_used -= num;
260 static struct console_input_events *create_console_input_events(void)
262 struct console_input_events* evt;
264 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
265 evt->num_alloc = evt->num_used = 0;
266 evt->events = NULL;
267 return evt;
270 static struct object *create_console_input( struct thread* renderer )
272 struct console_input *console_input;
274 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
275 console_input->renderer = renderer;
276 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
277 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
278 console_input->num_proc = 0;
279 console_input->active = NULL;
280 console_input->recnum = 0;
281 console_input->records = NULL;
282 console_input->evt = create_console_input_events();
283 console_input->title = NULL;
284 console_input->history_size = 50;
285 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
286 console_input->history_index = 0;
287 console_input->history_mode = 0;
288 console_input->edition_mode = 0;
289 console_input->input_cp = 0;
290 console_input->output_cp = 0;
291 console_input->win = 0;
292 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
294 if (!console_input->history || !console_input->evt)
296 release_object( console_input );
297 return NULL;
299 return &console_input->obj;
302 static struct screen_buffer *create_console_output( struct console_input *console_input )
304 struct screen_buffer *screen_buffer;
305 int i;
307 if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
308 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
309 screen_buffer->input = console_input;
310 screen_buffer->cursor_size = 100;
311 screen_buffer->cursor_visible = 1;
312 screen_buffer->width = 80;
313 screen_buffer->height = 150;
314 screen_buffer->max_width = 80;
315 screen_buffer->max_height = 25;
316 screen_buffer->cursor_x = 0;
317 screen_buffer->cursor_y = 0;
318 screen_buffer->attr = 0x0F;
319 screen_buffer->win.left = 0;
320 screen_buffer->win.right = screen_buffer->max_width - 1;
321 screen_buffer->win.top = 0;
322 screen_buffer->win.bottom = screen_buffer->max_height - 1;
324 list_add_head( &screen_buffer_list, &screen_buffer->entry );
326 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
327 sizeof(*screen_buffer->data) )))
329 release_object( screen_buffer );
330 return NULL;
332 /* clear the first row */
333 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
334 /* and copy it to all other rows */
335 for (i = 1; i < screen_buffer->height; i++)
336 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
337 screen_buffer->width * sizeof(char_info_t) );
339 if (!console_input->active)
341 struct console_renderer_event evt;
342 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
344 /* generate the initial events */
345 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
346 memset(&evt.u, 0, sizeof(evt.u));
347 console_input_events_append( console_input->evt, &evt );
349 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
350 evt.u.resize.width = screen_buffer->width;
351 evt.u.resize.height = screen_buffer->height;
352 console_input_events_append( console_input->evt, &evt );
354 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
355 evt.u.display.left = screen_buffer->win.left;
356 evt.u.display.top = screen_buffer->win.top;
357 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
358 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
359 console_input_events_append( console_input->evt, &evt );
361 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
362 evt.u.update.top = 0;
363 evt.u.update.bottom = screen_buffer->height - 1;
364 console_input_events_append( console_input->evt, &evt );
366 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
367 evt.u.cursor_geom.size = screen_buffer->cursor_size;
368 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
369 console_input_events_append( console_input->evt, &evt );
371 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
372 evt.u.cursor_pos.x = screen_buffer->cursor_x;
373 evt.u.cursor_pos.y = screen_buffer->cursor_y;
374 console_input_events_append( console_input->evt, &evt );
376 return screen_buffer;
379 /* free the console for this process */
380 int free_console( struct process *process )
382 struct console_input* console = process->console;
384 if (!console || !console->renderer) return 0;
386 process->console = NULL;
387 if (--console->num_proc == 0)
389 /* all processes have terminated... tell the renderer to terminate too */
390 struct console_renderer_event evt;
391 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
392 memset(&evt.u, 0, sizeof(evt.u));
393 console_input_events_append( console->evt, &evt );
395 release_object( console );
397 return 1;
400 /* let process inherit the console from parent... this handle two cases :
401 * 1/ generic console inheritance
402 * 2/ parent is a renderer which launches process, and process should attach to the console
403 * renderered by parent
405 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
407 int done = 0;
408 struct process* parent = parent_thread->process;
410 /* if parent is a renderer, then attach current process to its console
411 * a bit hacky....
413 if (hconin)
415 struct console_input* console;
417 /* FIXME: should we check some access rights ? */
418 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
419 0, &console_input_ops )))
421 if (console->renderer == parent_thread)
423 process->console = (struct console_input*)grab_object( console );
424 process->console->num_proc++;
425 done = 1;
427 release_object( console );
429 else clear_error(); /* ignore error */
431 /* otherwise, if parent has a console, attach child to this console */
432 if (!done && parent->console)
434 assert(parent->console->renderer);
435 process->console = (struct console_input*)grab_object( parent->console );
436 process->console->num_proc++;
440 struct thread *console_get_renderer( struct console_input *console )
442 return console->renderer;
445 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
447 struct console_input* console = NULL;
449 if (handle)
450 console = (struct console_input *)get_handle_obj( current->process, handle,
451 access, &console_input_ops );
452 else if (current->process->console)
454 assert( current->process->console->renderer );
455 console = (struct console_input *)grab_object( current->process->console );
458 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
459 return console;
462 struct console_signal_info
464 struct console_input *console;
465 process_id_t group;
466 int signal;
469 static int propagate_console_signal_cb(struct process *process, void *user)
471 struct console_signal_info* csi = (struct console_signal_info*)user;
473 if (process->console == csi->console && process->running_threads &&
474 (!csi->group || process->group_id == csi->group))
476 /* find a suitable thread to signal */
477 struct thread *thread;
478 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
480 if (send_thread_signal( thread, csi->signal )) break;
483 return FALSE;
486 static void propagate_console_signal( struct console_input *console,
487 int sig, process_id_t group_id )
489 struct console_signal_info csi;
491 if (!console)
493 set_error( STATUS_INVALID_PARAMETER );
494 return;
496 /* FIXME: should support the other events (like CTRL_BREAK) */
497 if (sig != CTRL_C_EVENT)
499 set_error( STATUS_NOT_IMPLEMENTED );
500 return;
502 csi.console = console;
503 csi.signal = SIGINT;
504 csi.group = group_id;
506 enum_processes(propagate_console_signal_cb, &csi);
509 static int get_console_mode( obj_handle_t handle )
511 struct object *obj;
512 int ret = 0;
514 if ((obj = get_handle_obj( current->process, handle, CONSOLE_READ, NULL )))
516 if (obj->ops == &console_input_ops)
517 ret = ((struct console_input *)obj)->mode;
518 else if (obj->ops == &screen_buffer_ops)
519 ret = ((struct screen_buffer *)obj)->mode;
520 else
521 set_error( STATUS_OBJECT_TYPE_MISMATCH );
522 release_object( obj );
524 return ret;
527 /* changes the mode of either a console input or a screen buffer */
528 static int set_console_mode( obj_handle_t handle, int mode )
530 struct object *obj;
531 int ret = 0;
533 if (!(obj = get_handle_obj( current->process, handle, CONSOLE_WRITE, NULL )))
534 return 0;
535 if (obj->ops == &console_input_ops)
537 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
538 ((struct console_input *)obj)->mode = mode;
539 ret = 1;
541 else if (obj->ops == &screen_buffer_ops)
543 ((struct screen_buffer *)obj)->mode = mode;
544 ret = 1;
546 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
547 release_object( obj );
548 return ret;
551 /* add input events to a console input queue */
552 static int write_console_input( struct console_input* console, int count,
553 const INPUT_RECORD *records )
555 INPUT_RECORD *new_rec;
557 if (!count) return 0;
558 if (!(new_rec = realloc( console->records,
559 (console->recnum + count) * sizeof(INPUT_RECORD) )))
561 set_error( STATUS_NO_MEMORY );
562 release_object( console );
563 return -1;
565 console->records = new_rec;
566 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
568 if (console->mode & ENABLE_PROCESSED_INPUT)
570 int i = 0;
571 while (i < count)
573 if (records[i].EventType == KEY_EVENT &&
574 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
575 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
577 if (i != count - 1)
578 memcpy( &console->records[console->recnum + i],
579 &console->records[console->recnum + i + 1],
580 (count - i - 1) * sizeof(INPUT_RECORD) );
581 count--;
582 if (records[i].Event.KeyEvent.bKeyDown)
584 /* send SIGINT to all processes attached to this console */
585 propagate_console_signal( console, CTRL_C_EVENT, 0 );
588 else i++;
591 if (!console->recnum && count) set_event( console->event );
592 console->recnum += count;
593 return count;
596 /* retrieve a pointer to the console input records */
597 static int read_console_input( obj_handle_t handle, int count, int flush )
599 struct console_input *console;
601 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
602 CONSOLE_READ, &console_input_ops )))
603 return -1;
605 if (!count)
607 /* special case: do not retrieve anything, but return
608 * the total number of records available */
609 count = console->recnum;
611 else
613 if (count > console->recnum) count = console->recnum;
614 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
616 if (flush)
618 int i;
619 for (i = count; i < console->recnum; i++)
620 console->records[i-count] = console->records[i];
621 if ((console->recnum -= count) > 0)
623 INPUT_RECORD *new_rec = realloc( console->records,
624 console->recnum * sizeof(INPUT_RECORD) );
625 if (new_rec) console->records = new_rec;
627 else
629 free( console->records );
630 console->records = NULL;
631 reset_event( console->event );
634 release_object( console );
635 return count;
638 /* set misc console input information */
639 static int set_console_input_info( const struct set_console_input_info_request *req,
640 const WCHAR *title, data_size_t len )
642 struct console_input *console;
643 struct console_renderer_event evt;
645 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) goto error;
647 memset(&evt.u, 0, sizeof(evt.u));
648 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
650 struct screen_buffer *screen_buffer;
652 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
653 CONSOLE_WRITE, &screen_buffer_ops );
654 if (!screen_buffer || screen_buffer->input != console)
656 set_error( STATUS_INVALID_HANDLE );
657 if (screen_buffer) release_object( screen_buffer );
658 goto error;
661 if (screen_buffer != console->active)
663 if (console->active) release_object( console->active );
664 console->active = screen_buffer;
665 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
666 console_input_events_append( console->evt, &evt );
668 else
669 release_object( screen_buffer );
671 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
673 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
674 if (new_title)
676 memcpy( new_title, title, len );
677 new_title[len / sizeof(WCHAR)] = 0;
678 free( console->title );
679 console->title = new_title;
680 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
681 console_input_events_append( console->evt, &evt );
684 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
686 console->history_mode = req->history_mode;
688 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
689 console->history_size != req->history_size)
691 WCHAR** mem = NULL;
692 int i;
693 int delta;
695 if (req->history_size)
697 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
698 if (!mem) goto error;
699 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
702 delta = (console->history_index > req->history_size) ?
703 (console->history_index - req->history_size) : 0;
705 for (i = delta; i < console->history_index; i++)
707 mem[i - delta] = console->history[i];
708 console->history[i] = NULL;
710 console->history_index -= delta;
712 for (i = 0; i < console->history_size; i++)
713 free( console->history[i] );
714 free( console->history );
715 console->history = mem;
716 console->history_size = req->history_size;
718 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
720 console->edition_mode = req->edition_mode;
722 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
724 console->input_cp = req->input_cp;
726 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
728 console->output_cp = req->output_cp;
730 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
732 console->win = req->win;
734 release_object( console );
735 return 1;
736 error:
737 if (console) release_object( console );
738 return 0;
741 /* resize a screen buffer */
742 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
743 int new_width, int new_height )
745 int i, old_width, old_height, copy_width, copy_height;
746 char_info_t *new_data;
748 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
750 set_error( STATUS_NO_MEMORY );
751 return 0;
753 old_width = screen_buffer->width;
754 old_height = screen_buffer->height;
755 copy_width = min( old_width, new_width );
756 copy_height = min( old_height, new_height );
758 /* copy all the rows */
759 for (i = 0; i < copy_height; i++)
761 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
762 copy_width * sizeof(char_info_t) );
765 /* clear the end of each row */
766 if (new_width > old_width)
768 /* fill first row */
769 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
770 /* and blast it to the other rows */
771 for (i = 1; i < copy_height; i++)
772 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
773 (new_width - old_width) * sizeof(char_info_t) );
776 /* clear remaining rows */
777 if (new_height > old_height)
779 /* fill first row */
780 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
781 /* and blast it to the other rows */
782 for (i = old_height+1; i < new_height; i++)
783 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
784 new_width * sizeof(char_info_t) );
786 free( screen_buffer->data );
787 screen_buffer->data = new_data;
788 screen_buffer->width = new_width;
789 screen_buffer->height = new_height;
790 return 1;
793 /* set misc screen buffer information */
794 static int set_console_output_info( struct screen_buffer *screen_buffer,
795 const struct set_console_output_info_request *req )
797 struct console_renderer_event evt;
799 memset(&evt.u, 0, sizeof(evt.u));
800 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
802 if (req->cursor_size < 1 || req->cursor_size > 100)
804 set_error( STATUS_INVALID_PARAMETER );
805 return 0;
807 if (screen_buffer->cursor_size != req->cursor_size ||
808 screen_buffer->cursor_visible != req->cursor_visible)
810 screen_buffer->cursor_size = req->cursor_size;
811 screen_buffer->cursor_visible = req->cursor_visible;
812 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
813 evt.u.cursor_geom.size = req->cursor_size;
814 evt.u.cursor_geom.visible = req->cursor_visible;
815 console_input_events_append( screen_buffer->input->evt, &evt );
818 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
820 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
821 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
823 set_error( STATUS_INVALID_PARAMETER );
824 return 0;
826 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
828 screen_buffer->cursor_x = req->cursor_x;
829 screen_buffer->cursor_y = req->cursor_y;
830 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
831 evt.u.cursor_pos.x = req->cursor_x;
832 evt.u.cursor_pos.y = req->cursor_y;
833 console_input_events_append( screen_buffer->input->evt, &evt );
836 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
838 unsigned cc;
840 /* new screen-buffer cannot be smaller than actual window */
841 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
842 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
844 set_error( STATUS_INVALID_PARAMETER );
845 return 0;
847 /* FIXME: there are also some basic minimum and max size to deal with */
848 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
850 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
851 evt.u.resize.width = req->width;
852 evt.u.resize.height = req->height;
853 console_input_events_append( screen_buffer->input->evt, &evt );
855 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
856 evt.u.update.top = 0;
857 evt.u.update.bottom = screen_buffer->height - 1;
858 console_input_events_append( screen_buffer->input->evt, &evt );
860 /* scroll window to display sb */
861 if (screen_buffer->win.right >= req->width)
863 screen_buffer->win.right -= screen_buffer->win.left;
864 screen_buffer->win.left = 0;
866 if (screen_buffer->win.bottom >= req->height)
868 screen_buffer->win.bottom -= screen_buffer->win.top;
869 screen_buffer->win.top = 0;
871 /* reset cursor if needed (normally, if cursor was outside of new sb, the
872 * window has been shifted so that the new position of the cursor will be
873 * visible */
874 cc = 0;
875 if (screen_buffer->cursor_x >= req->width)
877 screen_buffer->cursor_x = req->width - 1;
878 cc++;
880 if (screen_buffer->cursor_y >= req->height)
882 screen_buffer->cursor_y = req->height - 1;
883 cc++;
885 if (cc)
887 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
888 evt.u.cursor_pos.x = req->cursor_x;
889 evt.u.cursor_pos.y = req->cursor_y;
890 console_input_events_append( screen_buffer->input->evt, &evt );
893 if (screen_buffer == screen_buffer->input->active &&
894 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
896 INPUT_RECORD ir;
897 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
898 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
899 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
900 write_console_input( screen_buffer->input, 1, &ir );
903 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
905 screen_buffer->attr = req->attr;
907 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
909 if (req->win_left < 0 || req->win_left > req->win_right ||
910 req->win_right >= screen_buffer->width ||
911 req->win_top < 0 || req->win_top > req->win_bottom ||
912 req->win_bottom >= screen_buffer->height)
914 set_error( STATUS_INVALID_PARAMETER );
915 return 0;
917 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
918 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
920 screen_buffer->win.left = req->win_left;
921 screen_buffer->win.top = req->win_top;
922 screen_buffer->win.right = req->win_right;
923 screen_buffer->win.bottom = req->win_bottom;
924 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
925 evt.u.display.left = req->win_left;
926 evt.u.display.top = req->win_top;
927 evt.u.display.width = req->win_right - req->win_left + 1;
928 evt.u.display.height = req->win_bottom - req->win_top + 1;
929 console_input_events_append( screen_buffer->input->evt, &evt );
932 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
934 /* can only be done by renderer */
935 if (current->process->console != screen_buffer->input)
937 set_error( STATUS_INVALID_PARAMETER );
938 return 0;
941 screen_buffer->max_width = req->max_width;
942 screen_buffer->max_height = req->max_height;
945 return 1;
948 /* appends a new line to history (history is a fixed size array) */
949 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
951 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
953 if (!ptr)
954 return;
956 if (!console || !console->history_size)
958 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
959 free( ptr );
960 return;
963 memcpy( ptr, buf, len * sizeof(WCHAR) );
964 ptr[len] = 0;
966 if (console->history_mode && console->history_index &&
967 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
969 /* ok, mode ask us to not use twice the same string...
970 * so just free mem and returns
972 set_error( STATUS_ALIAS_EXISTS );
973 free(ptr);
974 return;
977 if (console->history_index < console->history_size)
979 console->history[console->history_index++] = ptr;
981 else
983 free( console->history[0]) ;
984 memmove( &console->history[0], &console->history[1],
985 (console->history_size - 1) * sizeof(WCHAR*) );
986 console->history[console->history_size - 1] = ptr;
990 /* returns a line from the cache */
991 static data_size_t console_input_get_hist( struct console_input *console, int index )
993 data_size_t ret = 0;
995 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
996 else
998 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
999 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1001 return ret;
1004 /* dumb dump */
1005 static void console_input_dump( struct object *obj, int verbose )
1007 struct console_input *console = (struct console_input *)obj;
1008 assert( obj->ops == &console_input_ops );
1009 fprintf( stderr, "Console input active=%p evt=%p\n",
1010 console->active, console->evt );
1013 static void console_input_destroy( struct object *obj )
1015 struct console_input* console_in = (struct console_input *)obj;
1016 struct screen_buffer* curr;
1017 int i;
1019 assert( obj->ops == &console_input_ops );
1020 free( console_in->title );
1021 free( console_in->records );
1023 if (console_in->active) release_object( console_in->active );
1024 console_in->active = NULL;
1026 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1028 if (curr->input == console_in) curr->input = NULL;
1031 release_object( console_in->evt );
1032 console_in->evt = NULL;
1033 release_object( console_in->event );
1035 for (i = 0; i < console_in->history_size; i++)
1036 free( console_in->history[i] );
1037 free( console_in->history );
1040 static void screen_buffer_dump( struct object *obj, int verbose )
1042 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1043 assert( obj->ops == &screen_buffer_ops );
1045 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1048 static void screen_buffer_destroy( struct object *obj )
1050 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1052 assert( obj->ops == &screen_buffer_ops );
1054 list_remove( &screen_buffer->entry );
1056 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1058 struct screen_buffer *sb;
1060 screen_buffer->input->active = NULL;
1061 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1063 if (sb->input == screen_buffer->input)
1065 sb->input->active = sb;
1066 break;
1070 free( screen_buffer->data );
1073 /* write data into a screen buffer */
1074 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1075 const void* data, enum char_info_mode mode,
1076 int x, int y, int wrap )
1078 unsigned int i;
1079 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1081 if (y >= screen_buffer->height) return 0;
1083 if (wrap)
1084 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1085 else
1086 end = screen_buffer->data + (y+1) * screen_buffer->width;
1088 switch(mode)
1090 case CHAR_INFO_MODE_TEXT:
1092 const WCHAR *ptr = data;
1093 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1095 break;
1096 case CHAR_INFO_MODE_ATTR:
1098 const unsigned short *ptr = data;
1099 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1101 break;
1102 case CHAR_INFO_MODE_TEXTATTR:
1104 const char_info_t *ptr = data;
1105 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1107 break;
1108 case CHAR_INFO_MODE_TEXTSTDATTR:
1110 const WCHAR *ptr = data;
1111 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1113 dest->ch = ptr[i];
1114 dest->attr = screen_buffer->attr;
1117 break;
1118 default:
1119 set_error( STATUS_INVALID_PARAMETER );
1120 return 0;
1123 if (i && screen_buffer == screen_buffer->input->active)
1125 struct console_renderer_event evt;
1126 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1127 memset(&evt.u, 0, sizeof(evt.u));
1128 evt.u.update.top = y + x / screen_buffer->width;
1129 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1130 console_input_events_append( screen_buffer->input->evt, &evt );
1132 return i;
1135 /* fill a screen buffer with uniform data */
1136 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1137 enum char_info_mode mode, int x, int y, int count, int wrap )
1139 int i;
1140 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1142 if (y >= screen_buffer->height) return 0;
1144 if (wrap)
1145 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1146 else
1147 end = screen_buffer->data + (y+1) * screen_buffer->width;
1149 if (count > end - dest) count = end - dest;
1151 switch(mode)
1153 case CHAR_INFO_MODE_TEXT:
1154 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1155 break;
1156 case CHAR_INFO_MODE_ATTR:
1157 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1158 break;
1159 case CHAR_INFO_MODE_TEXTATTR:
1160 for (i = 0; i < count; i++) dest[i] = data;
1161 break;
1162 case CHAR_INFO_MODE_TEXTSTDATTR:
1163 for (i = 0; i < count; i++)
1165 dest[i].ch = data.ch;
1166 dest[i].attr = screen_buffer->attr;
1168 break;
1169 default:
1170 set_error( STATUS_INVALID_PARAMETER );
1171 return 0;
1174 if (count && screen_buffer == screen_buffer->input->active)
1176 struct console_renderer_event evt;
1177 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1178 memset(&evt.u, 0, sizeof(evt.u));
1179 evt.u.update.top = y;
1180 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1181 console_input_events_append( screen_buffer->input->evt, &evt );
1183 return i;
1186 /* read data from a screen buffer */
1187 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1188 enum char_info_mode mode, int wrap )
1190 int i;
1191 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1193 if (y >= screen_buffer->height) return;
1195 if (wrap)
1196 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1197 else
1198 end = screen_buffer->data + (y+1) * screen_buffer->width;
1200 switch(mode)
1202 case CHAR_INFO_MODE_TEXT:
1204 WCHAR *data;
1205 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1206 if ((data = set_reply_data_size( count * sizeof(*data) )))
1208 for (i = 0; i < count; i++) data[i] = src[i].ch;
1211 break;
1212 case CHAR_INFO_MODE_ATTR:
1214 unsigned short *data;
1215 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1216 if ((data = set_reply_data_size( count * sizeof(*data) )))
1218 for (i = 0; i < count; i++) data[i] = src[i].attr;
1221 break;
1222 case CHAR_INFO_MODE_TEXTATTR:
1224 char_info_t *data;
1225 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1226 if ((data = set_reply_data_size( count * sizeof(*data) )))
1228 for (i = 0; i < count; i++) data[i] = src[i];
1231 break;
1232 default:
1233 set_error( STATUS_INVALID_PARAMETER );
1234 break;
1238 /* scroll parts of a screen buffer */
1239 static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1240 int w, int h )
1242 struct screen_buffer *screen_buffer;
1243 int j;
1244 char_info_t *psrc, *pdst;
1245 struct console_renderer_event evt;
1247 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1248 CONSOLE_READ, &screen_buffer_ops )))
1249 return;
1250 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1251 xsrc + w > screen_buffer->width ||
1252 xdst + w > screen_buffer->width ||
1253 ysrc + h > screen_buffer->height ||
1254 ydst + h > screen_buffer->height ||
1255 w == 0 || h == 0)
1257 set_error( STATUS_INVALID_PARAMETER );
1258 release_object( screen_buffer );
1259 return;
1262 if (ysrc < ydst)
1264 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1265 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1267 for (j = h; j > 0; j--)
1269 memcpy(pdst, psrc, w * sizeof(*pdst) );
1270 pdst -= screen_buffer->width;
1271 psrc -= screen_buffer->width;
1274 else
1276 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1277 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1279 for (j = 0; j < h; j++)
1281 /* we use memmove here because when psrc and pdst are the same,
1282 * copies are done on the same row, so the dst and src blocks
1283 * can overlap */
1284 memmove( pdst, psrc, w * sizeof(*pdst) );
1285 pdst += screen_buffer->width;
1286 psrc += screen_buffer->width;
1290 /* FIXME: this could be enhanced, by signalling scroll */
1291 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1292 memset(&evt.u, 0, sizeof(evt.u));
1293 evt.u.update.top = min(ysrc, ydst);
1294 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1295 console_input_events_append( screen_buffer->input->evt, &evt );
1297 release_object( screen_buffer );
1300 /* allocate a console for the renderer */
1301 DECL_HANDLER(alloc_console)
1303 obj_handle_t in = 0;
1304 obj_handle_t evt = 0;
1305 struct process *process;
1306 struct process *renderer = current->process;
1307 struct console_input *console;
1309 if (req->pid)
1311 if (!(process = get_process_from_id( req->pid ))) return;
1313 else
1315 if (!(process = renderer->parent))
1317 set_error( STATUS_ACCESS_DENIED );
1318 return;
1320 grab_object( process );
1323 if (process != renderer && process->console)
1325 set_error( STATUS_ACCESS_DENIED );
1326 goto the_end;
1328 if ((console = (struct console_input*)create_console_input( current )))
1330 if ((in = alloc_handle( renderer, console, req->access, req->attributes )))
1332 if ((evt = alloc_handle( renderer, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1334 if (process != renderer)
1336 process->console = (struct console_input*)grab_object( console );
1337 console->num_proc++;
1339 reply->handle_in = in;
1340 reply->event = evt;
1341 release_object( console );
1342 goto the_end;
1344 close_handle( renderer, in );
1346 free_console( process );
1348 the_end:
1349 release_object( process );
1352 /* free the console of the current process */
1353 DECL_HANDLER(free_console)
1355 free_console( current->process );
1358 /* let the renderer peek the events it's waiting on */
1359 DECL_HANDLER(get_console_renderer_events)
1361 struct console_input_events *evt;
1363 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1364 CONSOLE_READ, &console_input_events_ops );
1365 if (!evt) return;
1366 console_input_events_get( evt );
1367 release_object( evt );
1370 /* open a handle to the process console */
1371 DECL_HANDLER(open_console)
1373 struct object *obj = NULL;
1375 reply->handle = 0;
1376 if (req->from == (obj_handle_t)0)
1378 if (current->process->console && current->process->console->renderer)
1379 obj = grab_object( (struct object*)current->process->console );
1381 else if (req->from == (obj_handle_t)1)
1383 if (current->process->console && current->process->console->renderer &&
1384 current->process->console->active)
1385 obj = grab_object( (struct object*)current->process->console->active );
1387 else if ((obj = get_handle_obj( current->process, req->from,
1388 CONSOLE_READ|CONSOLE_WRITE, &console_input_ops )))
1390 struct console_input *console = (struct console_input *)obj;
1391 obj = (console->active) ? grab_object( console->active ) : NULL;
1392 release_object( console );
1395 /* FIXME: req->share is not used (as in screen buffer creation) */
1396 if (obj)
1398 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1399 release_object( obj );
1401 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1404 /* set info about a console input */
1405 DECL_HANDLER(set_console_input_info)
1407 set_console_input_info( req, get_req_data(), get_req_data_size() );
1410 /* get info about a console (output only) */
1411 DECL_HANDLER(get_console_input_info)
1413 struct console_input *console;
1415 if (!(console = console_input_get( req->handle, CONSOLE_READ ))) return;
1416 if (console->title)
1418 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1419 if (len > get_reply_max_size()) len = get_reply_max_size();
1420 set_reply_data( console->title, len );
1422 reply->history_mode = console->history_mode;
1423 reply->history_size = console->history_size;
1424 reply->history_index = console->history_index;
1425 reply->edition_mode = console->edition_mode;
1426 reply->input_cp = console->input_cp;
1427 reply->output_cp = console->output_cp;
1428 reply->win = console->win;
1430 release_object( console );
1433 /* get a console mode (input or output) */
1434 DECL_HANDLER(get_console_mode)
1436 reply->mode = get_console_mode( req->handle );
1439 /* set a console mode (input or output) */
1440 DECL_HANDLER(set_console_mode)
1442 set_console_mode( req->handle, req->mode );
1445 /* add input records to a console input queue */
1446 DECL_HANDLER(write_console_input)
1448 struct console_input *console;
1450 reply->written = 0;
1451 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1452 CONSOLE_WRITE, &console_input_ops )))
1453 return;
1454 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1455 get_req_data() );
1456 release_object( console );
1459 /* fetch input records from a console input queue */
1460 DECL_HANDLER(read_console_input)
1462 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1463 reply->read = read_console_input( req->handle, count, req->flush );
1466 /* appends a string to console's history */
1467 DECL_HANDLER(append_console_input_history)
1469 struct console_input *console;
1471 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1472 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1473 release_object( console );
1476 /* appends a string to console's history */
1477 DECL_HANDLER(get_console_input_history)
1479 struct console_input *console;
1481 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) return;
1482 reply->total = console_input_get_hist( console, req->index );
1483 release_object( console );
1486 /* creates a screen buffer */
1487 DECL_HANDLER(create_console_output)
1489 struct console_input* console;
1490 struct screen_buffer* screen_buffer;
1492 if (!(console = console_input_get( req->handle_in, CONSOLE_WRITE ))) return;
1494 screen_buffer = create_console_output( console );
1495 if (screen_buffer)
1497 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1498 * see file.c on how this could be done */
1499 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1500 release_object( screen_buffer );
1502 release_object( console );
1505 /* set info about a console screen buffer */
1506 DECL_HANDLER(set_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_WRITE, &screen_buffer_ops)))
1513 set_console_output_info( screen_buffer, req );
1514 release_object( screen_buffer );
1518 /* get info about a console screen buffer */
1519 DECL_HANDLER(get_console_output_info)
1521 struct screen_buffer *screen_buffer;
1523 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1524 CONSOLE_READ, &screen_buffer_ops)))
1526 reply->cursor_size = screen_buffer->cursor_size;
1527 reply->cursor_visible = screen_buffer->cursor_visible;
1528 reply->cursor_x = screen_buffer->cursor_x;
1529 reply->cursor_y = screen_buffer->cursor_y;
1530 reply->width = screen_buffer->width;
1531 reply->height = screen_buffer->height;
1532 reply->attr = screen_buffer->attr;
1533 reply->win_left = screen_buffer->win.left;
1534 reply->win_top = screen_buffer->win.top;
1535 reply->win_right = screen_buffer->win.right;
1536 reply->win_bottom = screen_buffer->win.bottom;
1537 reply->max_width = screen_buffer->max_width;
1538 reply->max_height = screen_buffer->max_height;
1539 release_object( screen_buffer );
1543 /* read data (chars & attrs) from a screen buffer */
1544 DECL_HANDLER(read_console_output)
1546 struct screen_buffer *screen_buffer;
1548 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1549 CONSOLE_READ, &screen_buffer_ops )))
1551 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1552 reply->width = screen_buffer->width;
1553 reply->height = screen_buffer->height;
1554 release_object( screen_buffer );
1558 /* write data (char and/or attrs) to a screen buffer */
1559 DECL_HANDLER(write_console_output)
1561 struct screen_buffer *screen_buffer;
1563 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1564 CONSOLE_WRITE, &screen_buffer_ops)))
1566 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1567 req->mode, req->x, req->y, req->wrap );
1568 reply->width = screen_buffer->width;
1569 reply->height = screen_buffer->height;
1570 release_object( screen_buffer );
1574 /* fill a screen buffer with constant data (chars and/or attributes) */
1575 DECL_HANDLER(fill_console_output)
1577 struct screen_buffer *screen_buffer;
1579 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1580 CONSOLE_WRITE, &screen_buffer_ops)))
1582 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1583 req->x, req->y, req->count, req->wrap );
1584 release_object( screen_buffer );
1588 /* move a rect of data in a screen buffer */
1589 DECL_HANDLER(move_console_output)
1591 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1592 req->w, req->h );
1595 /* sends a signal to a console (process, group...) */
1596 DECL_HANDLER(send_console_signal)
1598 process_id_t group;
1600 group = req->group_id ? req->group_id : current->process->group_id;
1602 if (!group)
1603 set_error( STATUS_INVALID_PARAMETER );
1604 else
1605 propagate_console_signal( current->process->console, req->signal, group );
1608 /* get console which renderer is 'current' */
1609 static int cgwe_enum( struct process* process, void* user)
1611 if (process->console && process->console->renderer == current)
1613 *(struct console_input**)user = process->console;
1614 return 1;
1616 return 0;
1619 DECL_HANDLER(get_console_wait_event)
1621 struct console_input* console = NULL;
1623 if (current->process->console && current->process->console->renderer)
1624 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1625 else enum_processes(cgwe_enum, &console);
1627 if (console)
1629 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1630 release_object( console );
1632 else set_error( STATUS_INVALID_PARAMETER );