dplayx: Don't print an error when no Lobby Providers are found.
[wine.git] / server / console.c
blob5b69e769a61b268e1906b5a14a7c7eefa985c37c
1 /*
2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
5 * 2001 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "handle.h"
35 #include "process.h"
36 #include "request.h"
37 #include "file.h"
38 #include "unicode.h"
39 #include "wincon.h"
40 #include "winternl.h"
42 struct screen_buffer;
43 struct console_input_events;
45 struct console_input
47 struct object obj; /* object header */
48 int num_proc; /* number of processes attached to this console */
49 struct thread *renderer; /* console renderer thread */
50 int mode; /* input mode */
51 struct screen_buffer *active; /* active screen buffer */
52 int recnum; /* number of input records */
53 INPUT_RECORD *records; /* input records */
54 struct console_input_events *evt; /* synchronization event with renderer */
55 WCHAR *title; /* console title */
56 WCHAR **history; /* lines history */
57 int history_size; /* number of entries in history array */
58 int history_index; /* number of used entries in history array */
59 int history_mode; /* mode of history (non zero means remove doubled strings */
60 int edition_mode; /* index to edition mode flavors */
61 int input_cp; /* console input codepage */
62 int output_cp; /* console output codepage */
63 user_handle_t win; /* window handle if backend supports it */
64 struct event *event; /* event to wait on for input queue */
65 struct fd *fd; /* for bare console, attached input fd */
68 static void console_input_dump( struct object *obj, int verbose );
69 static void console_input_destroy( struct object *obj );
70 static struct fd *console_input_get_fd( struct object *obj );
72 static const struct object_ops console_input_ops =
74 sizeof(struct console_input), /* size */
75 console_input_dump, /* dump */
76 no_get_type, /* get_type */
77 no_add_queue, /* add_queue */
78 NULL, /* remove_queue */
79 NULL, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 console_input_get_fd, /* get_fd */
83 default_fd_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_link_name, /* link_name */
88 NULL, /* unlink_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 wait_queue_entry *entry );
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 no_get_type, /* get_type */
111 add_queue, /* add_queue */
112 remove_queue, /* remove_queue */
113 console_input_events_signaled, /* signaled */
114 no_satisfied, /* satisfied */
115 no_signal, /* signal */
116 no_get_fd, /* get_fd */
117 default_fd_map_access, /* map_access */
118 default_get_sd, /* get_sd */
119 default_set_sd, /* set_sd */
120 no_lookup_name, /* lookup_name */
121 no_link_name, /* link_name */
122 NULL, /* unlink_name */
123 no_open_file, /* open_file */
124 no_close_handle, /* close_handle */
125 console_input_events_destroy /* destroy */
128 struct font_info
130 short int width;
131 short int height;
134 struct screen_buffer
136 struct object obj; /* object header */
137 struct list entry; /* entry in list of all screen buffers */
138 struct console_input *input; /* associated console input */
139 int mode; /* output mode */
140 int cursor_size; /* size of cursor (percentage filled) */
141 int cursor_visible;/* cursor visibility flag */
142 int cursor_x; /* position of cursor */
143 int cursor_y; /* position of cursor */
144 int width; /* size (w-h) of the screen buffer */
145 int height;
146 int max_width; /* size (w-h) of the window given font size */
147 int max_height;
148 char_info_t *data; /* the data for each cell - a width x height matrix */
149 unsigned short attr; /* default fill attributes (screen colors) */
150 unsigned short popup_attr; /* pop-up color attributes */
151 unsigned int color_map[16]; /* color table */
152 rectangle_t win; /* current visible window on the screen buffer *
153 * as seen in wineconsole */
154 struct font_info font; /* console font information */
155 struct fd *fd; /* for bare console, attached output fd */
158 static void screen_buffer_dump( struct object *obj, int verbose );
159 static void screen_buffer_destroy( struct object *obj );
160 static struct fd *screen_buffer_get_fd( struct object *obj );
162 static const struct object_ops screen_buffer_ops =
164 sizeof(struct screen_buffer), /* size */
165 screen_buffer_dump, /* dump */
166 no_get_type, /* get_type */
167 no_add_queue, /* add_queue */
168 NULL, /* remove_queue */
169 NULL, /* signaled */
170 NULL, /* satisfied */
171 no_signal, /* signal */
172 screen_buffer_get_fd, /* get_fd */
173 default_fd_map_access, /* map_access */
174 default_get_sd, /* get_sd */
175 default_set_sd, /* set_sd */
176 no_lookup_name, /* lookup_name */
177 no_link_name, /* link_name */
178 NULL, /* unlink_name */
179 no_open_file, /* open_file */
180 no_close_handle, /* close_handle */
181 screen_buffer_destroy /* destroy */
184 static enum server_fd_type console_get_fd_type( struct fd *fd );
186 static const struct fd_ops console_fd_ops =
188 default_fd_get_poll_events, /* get_poll_events */
189 default_poll_event, /* poll_event */
190 console_get_fd_type, /* get_fd_type */
191 no_fd_read, /* read */
192 no_fd_write, /* write */
193 no_fd_flush, /* flush */
194 default_fd_ioctl, /* ioctl */
195 default_fd_queue_async, /* queue_async */
196 default_fd_reselect_async /* reselect_async */
199 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
201 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
203 static int console_input_is_bare( struct console_input* cin )
205 return cin->evt == NULL;
208 static struct fd *console_input_get_fd( struct object* obj )
210 struct console_input *console_input = (struct console_input*)obj;
211 assert( obj->ops == &console_input_ops );
212 if (console_input->fd)
213 return (struct fd*)grab_object( console_input->fd );
214 set_error( STATUS_OBJECT_TYPE_MISMATCH );
215 return NULL;
218 static enum server_fd_type console_get_fd_type( struct fd *fd )
220 return FD_TYPE_CHAR;
223 /* dumps the renderer events of a console */
224 static void console_input_events_dump( struct object *obj, int verbose )
226 struct console_input_events *evts = (struct console_input_events *)obj;
227 assert( obj->ops == &console_input_events_ops );
228 fprintf( stderr, "Console input events: %d/%d events\n",
229 evts->num_used, evts->num_alloc );
232 /* destroys the renderer events of a console */
233 static void console_input_events_destroy( struct object *obj )
235 struct console_input_events *evts = (struct console_input_events *)obj;
236 assert( obj->ops == &console_input_events_ops );
237 free( evts->events );
240 /* the renderer events list is signaled when it's not empty */
241 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
243 struct console_input_events *evts = (struct console_input_events *)obj;
244 assert( obj->ops == &console_input_events_ops );
245 return (evts->num_used != 0);
248 /* add an event to the console's renderer events list */
249 static void console_input_events_append( struct console_input* console,
250 struct console_renderer_event* evt)
252 struct console_input_events* evts;
253 int collapsed = FALSE;
255 if (!(evts = console->evt)) return;
256 /* to be done even when evt has been generated by the renderer ? */
258 /* try to collapse evt into current queue's events */
259 if (evts->num_used)
261 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
263 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
264 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
266 /* if two update events overlap, collapse them into a single one */
267 if (last->u.update.bottom + 1 >= evt->u.update.top &&
268 evt->u.update.bottom + 1 >= last->u.update.top)
270 last->u.update.top = min(last->u.update.top, evt->u.update.top);
271 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
272 collapsed = TRUE;
276 if (!collapsed)
278 if (evts->num_used == evts->num_alloc)
280 evts->num_alloc += 16;
281 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
282 assert(evts->events);
284 evts->events[evts->num_used++] = *evt;
286 wake_up( &evts->obj, 0 );
289 /* retrieves events from the console's renderer events list */
290 static void console_input_events_get( struct console_input_events* evts )
292 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
294 if (num > evts->num_used) num = evts->num_used;
295 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
296 if (num < evts->num_used)
298 memmove( &evts->events[0], &evts->events[num],
299 (evts->num_used - num) * sizeof(evts->events[0]) );
301 evts->num_used -= num;
304 static struct console_input_events *create_console_input_events(void)
306 struct console_input_events* evt;
308 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
309 evt->num_alloc = evt->num_used = 0;
310 evt->events = NULL;
311 return evt;
314 static struct object *create_console_input( struct thread* renderer, int fd )
316 struct console_input *console_input;
318 if (!(console_input = alloc_object( &console_input_ops )))
320 if (fd != -1) close( fd );
321 return NULL;
323 console_input->renderer = renderer;
324 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
325 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
326 ENABLE_EXTENDED_FLAGS;
327 console_input->num_proc = 0;
328 console_input->active = NULL;
329 console_input->recnum = 0;
330 console_input->records = NULL;
331 console_input->evt = renderer ? create_console_input_events() : NULL;
332 console_input->title = NULL;
333 console_input->history_size = 50;
334 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
335 console_input->history_index = 0;
336 console_input->history_mode = 0;
337 console_input->edition_mode = 0;
338 console_input->input_cp = 0;
339 console_input->output_cp = 0;
340 console_input->win = 0;
341 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
342 console_input->fd = NULL;
344 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
346 if (fd != -1) close( fd );
347 console_input->history_size = 0;
348 release_object( console_input );
349 return NULL;
351 if (fd != -1) /* bare console */
353 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
354 FILE_SYNCHRONOUS_IO_NONALERT )))
356 release_object( console_input );
357 return NULL;
359 allow_fd_caching( console_input->fd );
362 return &console_input->obj;
365 static void generate_sb_initial_events( struct console_input *console_input )
367 struct screen_buffer *screen_buffer = console_input->active;
368 struct console_renderer_event evt;
370 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
371 memset(&evt.u, 0, sizeof(evt.u));
372 console_input_events_append( console_input, &evt );
374 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
375 evt.u.resize.width = screen_buffer->width;
376 evt.u.resize.height = screen_buffer->height;
377 console_input_events_append( console_input, &evt );
379 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
380 evt.u.display.left = screen_buffer->win.left;
381 evt.u.display.top = screen_buffer->win.top;
382 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
383 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
384 console_input_events_append( console_input, &evt );
386 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
387 evt.u.update.top = 0;
388 evt.u.update.bottom = screen_buffer->height - 1;
389 console_input_events_append( console_input, &evt );
391 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
392 evt.u.cursor_geom.size = screen_buffer->cursor_size;
393 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
394 console_input_events_append( console_input, &evt );
396 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
397 evt.u.cursor_pos.x = screen_buffer->cursor_x;
398 evt.u.cursor_pos.y = screen_buffer->cursor_y;
399 console_input_events_append( console_input, &evt );
402 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
404 struct screen_buffer *screen_buffer;
405 int i;
407 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
409 if (fd != -1) close( fd );
410 return NULL;
412 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
413 screen_buffer->input = console_input;
414 screen_buffer->cursor_size = 100;
415 screen_buffer->cursor_visible = 1;
416 screen_buffer->width = 80;
417 screen_buffer->height = 150;
418 screen_buffer->max_width = 80;
419 screen_buffer->max_height = 25;
420 screen_buffer->cursor_x = 0;
421 screen_buffer->cursor_y = 0;
422 screen_buffer->attr = 0x0F;
423 screen_buffer->popup_attr = 0xF5;
424 screen_buffer->win.left = 0;
425 screen_buffer->win.right = screen_buffer->max_width - 1;
426 screen_buffer->win.top = 0;
427 screen_buffer->win.bottom = screen_buffer->max_height - 1;
428 screen_buffer->data = NULL;
429 screen_buffer->font.width = 0;
430 screen_buffer->font.height = 0;
431 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
432 list_add_head( &screen_buffer_list, &screen_buffer->entry );
434 if (fd == -1)
435 screen_buffer->fd = NULL;
436 else
438 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
439 FILE_SYNCHRONOUS_IO_NONALERT )))
441 release_object( screen_buffer );
442 return NULL;
444 allow_fd_caching(screen_buffer->fd);
447 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
448 sizeof(*screen_buffer->data) )))
450 release_object( screen_buffer );
451 return NULL;
453 /* clear the first row */
454 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
455 /* and copy it to all other rows */
456 for (i = 1; i < screen_buffer->height; i++)
457 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
458 screen_buffer->width * sizeof(char_info_t) );
460 if (!console_input->active)
462 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
463 generate_sb_initial_events( console_input );
465 return screen_buffer;
468 /* free the console for this process */
469 int free_console( struct process *process )
471 struct console_input* console = process->console;
473 if (!console) return 0;
475 process->console = NULL;
476 if (--console->num_proc == 0 && console->renderer)
478 /* all processes have terminated... tell the renderer to terminate too */
479 struct console_renderer_event evt;
480 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
481 memset(&evt.u, 0, sizeof(evt.u));
482 console_input_events_append( console, &evt );
484 release_object( console );
486 return 1;
489 /* let process inherit the console from parent... this handle two cases :
490 * 1/ generic console inheritance
491 * 2/ parent is a renderer which launches process, and process should attach to the console
492 * rendered by parent
494 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
496 int done = 0;
497 struct process* parent = parent_thread->process;
499 /* if parent is a renderer, then attach current process to its console
500 * a bit hacky....
502 if (hconin)
504 struct console_input* console;
506 /* FIXME: should we check some access rights ? */
507 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
508 0, &console_input_ops )))
510 if (console->renderer == parent_thread)
512 process->console = (struct console_input*)grab_object( console );
513 process->console->num_proc++;
514 done = 1;
516 release_object( console );
518 else clear_error(); /* ignore error */
520 /* otherwise, if parent has a console, attach child to this console */
521 if (!done && parent->console)
523 process->console = (struct console_input*)grab_object( parent->console );
524 process->console->num_proc++;
528 struct thread *console_get_renderer( struct console_input *console )
530 return console->renderer;
533 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
535 struct console_input* console = NULL;
537 if (handle)
538 console = (struct console_input *)get_handle_obj( current->process, handle,
539 access, &console_input_ops );
540 else if (current->process->console)
542 console = (struct console_input *)grab_object( current->process->console );
545 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
546 return console;
549 struct console_signal_info
551 struct console_input *console;
552 process_id_t group;
553 int signal;
556 static int propagate_console_signal_cb(struct process *process, void *user)
558 struct console_signal_info* csi = (struct console_signal_info*)user;
560 if (process->console == csi->console && process->running_threads &&
561 (!csi->group || process->group_id == csi->group))
563 /* find a suitable thread to signal */
564 struct thread *thread;
565 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
567 if (send_thread_signal( thread, csi->signal )) break;
570 return FALSE;
573 static void propagate_console_signal( struct console_input *console,
574 int sig, process_id_t group_id )
576 struct console_signal_info csi;
578 if (!console)
580 set_error( STATUS_INVALID_PARAMETER );
581 return;
583 /* FIXME: should support the other events (like CTRL_BREAK) */
584 if (sig != CTRL_C_EVENT)
586 set_error( STATUS_NOT_IMPLEMENTED );
587 return;
589 csi.console = console;
590 csi.signal = SIGINT;
591 csi.group = group_id;
593 enum_processes(propagate_console_signal_cb, &csi);
596 static int get_console_mode( obj_handle_t handle )
598 struct object *obj;
599 int ret = 0;
601 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
603 if (obj->ops == &console_input_ops)
605 ret = ((struct console_input *)obj)->mode;
607 else if (obj->ops == &screen_buffer_ops)
609 ret = ((struct screen_buffer *)obj)->mode;
611 else
612 set_error( STATUS_OBJECT_TYPE_MISMATCH );
613 release_object( obj );
615 return ret;
618 /* changes the mode of either a console input or a screen buffer */
619 static int set_console_mode( obj_handle_t handle, int mode )
621 struct object *obj;
622 int ret = 0;
624 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
625 return 0;
626 if (obj->ops == &console_input_ops)
628 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
629 ((struct console_input *)obj)->mode = mode;
630 ret = 1;
632 else if (obj->ops == &screen_buffer_ops)
634 ((struct screen_buffer *)obj)->mode = mode;
635 ret = 1;
637 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
638 release_object( obj );
639 return ret;
642 /* add input events to a console input queue */
643 static int write_console_input( struct console_input* console, int count,
644 const INPUT_RECORD *records )
646 INPUT_RECORD *new_rec;
648 if (!count) return 0;
649 if (!(new_rec = realloc( console->records,
650 (console->recnum + count) * sizeof(INPUT_RECORD) )))
652 set_error( STATUS_NO_MEMORY );
653 return -1;
655 console->records = new_rec;
656 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
658 if (console->mode & ENABLE_PROCESSED_INPUT)
660 int i = 0;
661 while (i < count)
663 if (records[i].EventType == KEY_EVENT &&
664 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
665 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
667 if (i != count - 1)
668 memcpy( &console->records[console->recnum + i],
669 &console->records[console->recnum + i + 1],
670 (count - i - 1) * sizeof(INPUT_RECORD) );
671 count--;
672 if (records[i].Event.KeyEvent.bKeyDown)
674 /* send SIGINT to all processes attached to this console */
675 propagate_console_signal( console, CTRL_C_EVENT, 0 );
678 else i++;
681 if (!console->recnum && count) set_event( console->event );
682 console->recnum += count;
683 return count;
686 /* retrieve a pointer to the console input records */
687 static int read_console_input( obj_handle_t handle, int count, int flush )
689 struct console_input *console;
691 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
692 FILE_READ_DATA, &console_input_ops )))
693 return -1;
695 if (!count)
697 /* special case: do not retrieve anything, but return
698 * the total number of records available */
699 count = console->recnum;
701 else
703 if (count > console->recnum) count = console->recnum;
704 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
706 if (flush)
708 int i;
709 for (i = count; i < console->recnum; i++)
710 console->records[i-count] = console->records[i];
711 if ((console->recnum -= count) > 0)
713 INPUT_RECORD *new_rec = realloc( console->records,
714 console->recnum * sizeof(INPUT_RECORD) );
715 if (new_rec) console->records = new_rec;
717 else
719 free( console->records );
720 console->records = NULL;
721 reset_event( console->event );
724 release_object( console );
725 return count;
728 /* set misc console input information */
729 static int set_console_input_info( const struct set_console_input_info_request *req,
730 const WCHAR *title, data_size_t len )
732 struct console_input *console;
733 struct console_renderer_event evt;
735 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
736 if (console_input_is_bare(console) &&
737 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
738 SET_CONSOLE_INPUT_INFO_WIN)))
740 set_error( STATUS_UNSUCCESSFUL );
741 goto error;
744 memset(&evt.u, 0, sizeof(evt.u));
745 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
747 struct screen_buffer *screen_buffer;
749 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
750 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
751 if (!screen_buffer || screen_buffer->input != console)
753 set_error( STATUS_INVALID_HANDLE );
754 if (screen_buffer) release_object( screen_buffer );
755 goto error;
758 if (screen_buffer != console->active)
760 if (console->active) release_object( console->active );
761 console->active = screen_buffer;
762 generate_sb_initial_events( console );
764 else
765 release_object( screen_buffer );
767 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
769 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
770 if (new_title)
772 memcpy( new_title, title, len );
773 new_title[len / sizeof(WCHAR)] = 0;
774 free( console->title );
775 console->title = new_title;
776 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
777 console_input_events_append( console, &evt );
780 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
782 console->history_mode = req->history_mode;
784 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
785 console->history_size != req->history_size)
787 WCHAR** mem = NULL;
788 int i;
789 int delta;
791 if (req->history_size)
793 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
794 if (!mem) goto error;
795 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
798 delta = (console->history_index > req->history_size) ?
799 (console->history_index - req->history_size) : 0;
801 for (i = delta; i < console->history_index; i++)
803 mem[i - delta] = console->history[i];
804 console->history[i] = NULL;
806 console->history_index -= delta;
808 for (i = 0; i < console->history_size; i++)
809 free( console->history[i] );
810 free( console->history );
811 console->history = mem;
812 console->history_size = req->history_size;
814 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
816 console->edition_mode = req->edition_mode;
818 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
820 console->input_cp = req->input_cp;
822 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
824 console->output_cp = req->output_cp;
826 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
828 console->win = req->win;
830 release_object( console );
831 return 1;
832 error:
833 if (console) release_object( console );
834 return 0;
837 /* resize a screen buffer */
838 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
839 int new_width, int new_height )
841 int i, old_width, old_height, copy_width, copy_height;
842 char_info_t *new_data;
844 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
846 set_error( STATUS_NO_MEMORY );
847 return 0;
849 old_width = screen_buffer->width;
850 old_height = screen_buffer->height;
851 copy_width = min( old_width, new_width );
852 copy_height = min( old_height, new_height );
854 /* copy all the rows */
855 for (i = 0; i < copy_height; i++)
857 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
858 copy_width * sizeof(char_info_t) );
861 /* clear the end of each row */
862 if (new_width > old_width)
864 /* fill first row */
865 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
866 /* and blast it to the other rows */
867 for (i = 1; i < copy_height; i++)
868 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
869 (new_width - old_width) * sizeof(char_info_t) );
872 /* clear remaining rows */
873 if (new_height > old_height)
875 /* fill first row */
876 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
877 /* and blast it to the other rows */
878 for (i = old_height+1; i < new_height; i++)
879 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
880 new_width * sizeof(char_info_t) );
882 free( screen_buffer->data );
883 screen_buffer->data = new_data;
884 screen_buffer->width = new_width;
885 screen_buffer->height = new_height;
886 return 1;
889 /* set misc screen buffer information */
890 static int set_console_output_info( struct screen_buffer *screen_buffer,
891 const struct set_console_output_info_request *req )
893 struct console_renderer_event evt;
895 memset(&evt.u, 0, sizeof(evt.u));
896 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
898 if (req->cursor_size < 1 || req->cursor_size > 100)
900 set_error( STATUS_INVALID_PARAMETER );
901 return 0;
903 if (screen_buffer->cursor_size != req->cursor_size ||
904 screen_buffer->cursor_visible != req->cursor_visible)
906 screen_buffer->cursor_size = req->cursor_size;
907 screen_buffer->cursor_visible = req->cursor_visible;
908 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
909 evt.u.cursor_geom.size = req->cursor_size;
910 evt.u.cursor_geom.visible = req->cursor_visible;
911 console_input_events_append( screen_buffer->input, &evt );
914 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
916 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
917 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
919 set_error( STATUS_INVALID_PARAMETER );
920 return 0;
922 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
924 screen_buffer->cursor_x = req->cursor_x;
925 screen_buffer->cursor_y = req->cursor_y;
926 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
927 evt.u.cursor_pos.x = req->cursor_x;
928 evt.u.cursor_pos.y = req->cursor_y;
929 console_input_events_append( screen_buffer->input, &evt );
932 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
934 unsigned cc;
936 /* new screen-buffer cannot be smaller than actual window */
937 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
938 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
940 set_error( STATUS_INVALID_PARAMETER );
941 return 0;
943 /* FIXME: there are also some basic minimum and max size to deal with */
944 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
946 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
947 evt.u.resize.width = req->width;
948 evt.u.resize.height = req->height;
949 console_input_events_append( screen_buffer->input, &evt );
951 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
952 evt.u.update.top = 0;
953 evt.u.update.bottom = screen_buffer->height - 1;
954 console_input_events_append( screen_buffer->input, &evt );
956 /* scroll window to display sb */
957 if (screen_buffer->win.right >= req->width)
959 screen_buffer->win.right -= screen_buffer->win.left;
960 screen_buffer->win.left = 0;
962 if (screen_buffer->win.bottom >= req->height)
964 screen_buffer->win.bottom -= screen_buffer->win.top;
965 screen_buffer->win.top = 0;
967 /* reset cursor if needed (normally, if cursor was outside of new sb, the
968 * window has been shifted so that the new position of the cursor will be
969 * visible */
970 cc = 0;
971 if (screen_buffer->cursor_x >= req->width)
973 screen_buffer->cursor_x = req->width - 1;
974 cc++;
976 if (screen_buffer->cursor_y >= req->height)
978 screen_buffer->cursor_y = req->height - 1;
979 cc++;
981 if (cc)
983 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
984 evt.u.cursor_pos.x = req->cursor_x;
985 evt.u.cursor_pos.y = req->cursor_y;
986 console_input_events_append( screen_buffer->input, &evt );
989 if (screen_buffer == screen_buffer->input->active &&
990 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
992 INPUT_RECORD ir;
993 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
994 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
995 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
996 write_console_input( screen_buffer->input, 1, &ir );
999 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1001 screen_buffer->attr = req->attr;
1003 if (req->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1005 screen_buffer->popup_attr = req->popup_attr;
1007 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1009 if (req->win_left < 0 || req->win_left > req->win_right ||
1010 req->win_right >= screen_buffer->width ||
1011 req->win_top < 0 || req->win_top > req->win_bottom ||
1012 req->win_bottom >= screen_buffer->height)
1014 set_error( STATUS_INVALID_PARAMETER );
1015 return 0;
1017 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1018 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1020 screen_buffer->win.left = req->win_left;
1021 screen_buffer->win.top = req->win_top;
1022 screen_buffer->win.right = req->win_right;
1023 screen_buffer->win.bottom = req->win_bottom;
1024 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1025 evt.u.display.left = req->win_left;
1026 evt.u.display.top = req->win_top;
1027 evt.u.display.width = req->win_right - req->win_left + 1;
1028 evt.u.display.height = req->win_bottom - req->win_top + 1;
1029 console_input_events_append( screen_buffer->input, &evt );
1032 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1034 screen_buffer->max_width = req->max_width;
1035 screen_buffer->max_height = req->max_height;
1037 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1039 screen_buffer->font.width = req->font_width;
1040 screen_buffer->font.height = req->font_height;
1042 if (req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1044 memcpy( screen_buffer->color_map, get_req_data(),
1045 min( sizeof(screen_buffer->color_map), get_req_data_size() ));
1048 return 1;
1051 /* appends a new line to history (history is a fixed size array) */
1052 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1054 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1056 if (!ptr)
1057 return;
1059 if (!console || !console->history_size)
1061 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1062 free( ptr );
1063 return;
1066 memcpy( ptr, buf, len * sizeof(WCHAR) );
1067 ptr[len] = 0;
1069 if (console->history_mode && console->history_index &&
1070 !strcmpW( console->history[console->history_index - 1], ptr ))
1072 /* ok, mode ask us to not use twice the same string...
1073 * so just free mem and returns
1075 set_error( STATUS_ALIAS_EXISTS );
1076 free(ptr);
1077 return;
1080 if (console->history_index < console->history_size)
1082 console->history[console->history_index++] = ptr;
1084 else
1086 free( console->history[0]) ;
1087 memmove( &console->history[0], &console->history[1],
1088 (console->history_size - 1) * sizeof(WCHAR*) );
1089 console->history[console->history_size - 1] = ptr;
1093 /* returns a line from the cache */
1094 static data_size_t console_input_get_hist( struct console_input *console, int index )
1096 data_size_t ret = 0;
1098 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1099 else
1101 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1102 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1104 return ret;
1107 /* dumb dump */
1108 static void console_input_dump( struct object *obj, int verbose )
1110 struct console_input *console = (struct console_input *)obj;
1111 assert( obj->ops == &console_input_ops );
1112 fprintf( stderr, "Console input active=%p evt=%p\n",
1113 console->active, console->evt );
1116 static void console_input_destroy( struct object *obj )
1118 struct console_input* console_in = (struct console_input *)obj;
1119 struct screen_buffer* curr;
1120 int i;
1122 assert( obj->ops == &console_input_ops );
1123 free( console_in->title );
1124 free( console_in->records );
1126 if (console_in->active) release_object( console_in->active );
1127 console_in->active = NULL;
1129 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1131 if (curr->input == console_in) curr->input = NULL;
1134 if (console_in->evt)
1136 release_object( console_in->evt );
1137 console_in->evt = NULL;
1139 if (console_in->event)
1140 release_object( console_in->event );
1141 if (console_in->fd)
1142 release_object( console_in->fd );
1144 for (i = 0; i < console_in->history_size; i++)
1145 free( console_in->history[i] );
1146 free( console_in->history );
1149 static void screen_buffer_dump( struct object *obj, int verbose )
1151 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1152 assert( obj->ops == &screen_buffer_ops );
1154 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1157 static void screen_buffer_destroy( struct object *obj )
1159 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1161 assert( obj->ops == &screen_buffer_ops );
1163 list_remove( &screen_buffer->entry );
1165 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1167 struct screen_buffer *sb;
1169 screen_buffer->input->active = NULL;
1170 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1172 if (sb->input == screen_buffer->input)
1174 sb->input->active = sb;
1175 break;
1179 if (screen_buffer->fd) release_object( screen_buffer->fd );
1180 free( screen_buffer->data );
1183 static struct fd *screen_buffer_get_fd( struct object *obj )
1185 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1186 assert( obj->ops == &screen_buffer_ops );
1187 if (screen_buffer->fd)
1188 return (struct fd*)grab_object( screen_buffer->fd );
1189 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1190 return NULL;
1193 /* write data into a screen buffer */
1194 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1195 const void* data, enum char_info_mode mode,
1196 int x, int y, int wrap )
1198 unsigned int i;
1199 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1201 if (y >= screen_buffer->height) return 0;
1203 if (wrap)
1204 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1205 else
1206 end = screen_buffer->data + (y+1) * screen_buffer->width;
1208 switch(mode)
1210 case CHAR_INFO_MODE_TEXT:
1212 const WCHAR *ptr = data;
1213 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1215 break;
1216 case CHAR_INFO_MODE_ATTR:
1218 const unsigned short *ptr = data;
1219 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1221 break;
1222 case CHAR_INFO_MODE_TEXTATTR:
1224 const char_info_t *ptr = data;
1225 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1227 break;
1228 case CHAR_INFO_MODE_TEXTSTDATTR:
1230 const WCHAR *ptr = data;
1231 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1233 dest->ch = ptr[i];
1234 dest->attr = screen_buffer->attr;
1237 break;
1238 default:
1239 set_error( STATUS_INVALID_PARAMETER );
1240 return 0;
1243 if (i && screen_buffer == screen_buffer->input->active)
1245 struct console_renderer_event evt;
1246 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1247 memset(&evt.u, 0, sizeof(evt.u));
1248 evt.u.update.top = y + x / screen_buffer->width;
1249 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1250 console_input_events_append( screen_buffer->input, &evt );
1252 return i;
1255 /* fill a screen buffer with uniform data */
1256 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1257 enum char_info_mode mode, int x, int y, int count, int wrap )
1259 int i;
1260 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1262 if (y >= screen_buffer->height) return 0;
1264 if (wrap)
1265 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1266 else
1267 end = screen_buffer->data + (y+1) * screen_buffer->width;
1269 if (count > end - dest) count = end - dest;
1271 switch(mode)
1273 case CHAR_INFO_MODE_TEXT:
1274 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1275 break;
1276 case CHAR_INFO_MODE_ATTR:
1277 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1278 break;
1279 case CHAR_INFO_MODE_TEXTATTR:
1280 for (i = 0; i < count; i++) dest[i] = data;
1281 break;
1282 case CHAR_INFO_MODE_TEXTSTDATTR:
1283 for (i = 0; i < count; i++)
1285 dest[i].ch = data.ch;
1286 dest[i].attr = screen_buffer->attr;
1288 break;
1289 default:
1290 set_error( STATUS_INVALID_PARAMETER );
1291 return 0;
1294 if (count && screen_buffer == screen_buffer->input->active)
1296 struct console_renderer_event evt;
1297 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1298 memset(&evt.u, 0, sizeof(evt.u));
1299 evt.u.update.top = y;
1300 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1301 console_input_events_append( screen_buffer->input, &evt );
1303 return i;
1306 /* read data from a screen buffer */
1307 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1308 enum char_info_mode mode, int wrap )
1310 int i;
1311 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1313 if (y >= screen_buffer->height) return;
1315 if (wrap)
1316 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1317 else
1318 end = screen_buffer->data + (y+1) * screen_buffer->width;
1320 switch(mode)
1322 case CHAR_INFO_MODE_TEXT:
1324 WCHAR *data;
1325 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1326 if ((data = set_reply_data_size( count * sizeof(*data) )))
1328 for (i = 0; i < count; i++) data[i] = src[i].ch;
1331 break;
1332 case CHAR_INFO_MODE_ATTR:
1334 unsigned short *data;
1335 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1336 if ((data = set_reply_data_size( count * sizeof(*data) )))
1338 for (i = 0; i < count; i++) data[i] = src[i].attr;
1341 break;
1342 case CHAR_INFO_MODE_TEXTATTR:
1344 char_info_t *data;
1345 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1346 if ((data = set_reply_data_size( count * sizeof(*data) )))
1348 for (i = 0; i < count; i++) data[i] = src[i];
1351 break;
1352 default:
1353 set_error( STATUS_INVALID_PARAMETER );
1354 break;
1358 /* scroll parts of a screen buffer */
1359 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1360 int w, int h )
1362 int j;
1363 char_info_t *psrc, *pdst;
1364 struct console_renderer_event evt;
1366 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1367 xsrc + w > screen_buffer->width ||
1368 xdst + w > screen_buffer->width ||
1369 ysrc + h > screen_buffer->height ||
1370 ydst + h > screen_buffer->height ||
1371 w == 0 || h == 0)
1373 set_error( STATUS_INVALID_PARAMETER );
1374 return;
1377 if (ysrc < ydst)
1379 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1380 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1382 for (j = h; j > 0; j--)
1384 memcpy(pdst, psrc, w * sizeof(*pdst) );
1385 pdst -= screen_buffer->width;
1386 psrc -= screen_buffer->width;
1389 else
1391 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1392 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1394 for (j = 0; j < h; j++)
1396 /* we use memmove here because when psrc and pdst are the same,
1397 * copies are done on the same row, so the dst and src blocks
1398 * can overlap */
1399 memmove( pdst, psrc, w * sizeof(*pdst) );
1400 pdst += screen_buffer->width;
1401 psrc += screen_buffer->width;
1405 /* FIXME: this could be enhanced, by signalling scroll */
1406 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1407 memset(&evt.u, 0, sizeof(evt.u));
1408 evt.u.update.top = min(ysrc, ydst);
1409 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1410 console_input_events_append( screen_buffer->input, &evt );
1413 /* allocate a console for the renderer */
1414 DECL_HANDLER(alloc_console)
1416 obj_handle_t in = 0;
1417 obj_handle_t evt = 0;
1418 struct process *process;
1419 struct thread *renderer;
1420 struct console_input *console;
1421 int fd;
1422 int attach = 0;
1424 if (req->input_fd != -1)
1426 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1428 set_error( STATUS_INVALID_PARAMETER );
1429 return;
1432 else fd = -1;
1434 switch (req->pid)
1436 case 0:
1437 /* renderer is current, console to be attached to parent process */
1438 renderer = current;
1439 if (!(process = get_process_from_id( current->process->parent_id )))
1441 if (fd != -1) close( fd );
1442 set_error( STATUS_ACCESS_DENIED );
1443 return;
1445 attach = 1;
1446 break;
1447 case 0xffffffff:
1448 /* no renderer, console to be attached to current process */
1449 renderer = NULL;
1450 process = current->process;
1451 grab_object( process );
1452 attach = 1;
1453 break;
1454 default:
1455 /* renderer is current, console to be attached to req->pid */
1456 renderer = current;
1457 if (!(process = get_process_from_id( req->pid )))
1459 if (fd != -1) close( fd );
1460 return;
1464 if (attach && process->console)
1466 if (fd != -1) close( fd );
1467 set_error( STATUS_ACCESS_DENIED );
1468 goto the_end;
1471 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1473 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1475 if (!console->evt ||
1476 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1478 if (attach)
1480 process->console = (struct console_input*)grab_object( console );
1481 console->num_proc++;
1483 reply->handle_in = in;
1484 reply->event = evt;
1485 release_object( console );
1486 goto the_end;
1488 close_handle( current->process, in );
1490 release_object( console );
1492 the_end:
1493 release_object( process );
1496 /* free the console of the current process */
1497 DECL_HANDLER(free_console)
1499 free_console( current->process );
1502 /* let the renderer peek the events it's waiting on */
1503 DECL_HANDLER(get_console_renderer_events)
1505 struct console_input_events *evt;
1507 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1508 FILE_READ_PROPERTIES, &console_input_events_ops );
1509 if (!evt) return;
1510 console_input_events_get( evt );
1511 release_object( evt );
1514 /* open a handle to the process console */
1515 DECL_HANDLER(open_console)
1517 struct object *obj = NULL;
1519 reply->handle = 0;
1520 if (!req->from)
1522 if (current->process->console)
1523 obj = grab_object( (struct object*)current->process->console );
1525 else if (req->from == (obj_handle_t)1)
1527 if (current->process->console && current->process->console->active)
1528 obj = grab_object( (struct object*)current->process->console->active );
1530 else if ((obj = get_handle_obj( current->process, req->from,
1531 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1533 struct console_input *console = (struct console_input *)obj;
1534 obj = (console->active) ? grab_object( console->active ) : NULL;
1535 release_object( console );
1538 /* FIXME: req->share is not used (as in screen buffer creation) */
1539 if (obj)
1541 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1542 release_object( obj );
1544 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1547 /* set info about a console input */
1548 DECL_HANDLER(set_console_input_info)
1550 set_console_input_info( req, get_req_data(), get_req_data_size() );
1553 /* get info about a console (output only) */
1554 DECL_HANDLER(get_console_input_info)
1556 struct console_input *console;
1558 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1559 if (console->title)
1561 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1562 if (len > get_reply_max_size()) len = get_reply_max_size();
1563 set_reply_data( console->title, len );
1565 reply->history_mode = console->history_mode;
1566 reply->history_size = console->history_size;
1567 reply->history_index = console->history_index;
1568 reply->edition_mode = console->edition_mode;
1569 reply->input_cp = console->input_cp;
1570 reply->output_cp = console->output_cp;
1571 reply->win = console->win;
1573 release_object( console );
1576 /* get a console mode (input or output) */
1577 DECL_HANDLER(get_console_mode)
1579 reply->mode = get_console_mode( req->handle );
1582 /* set a console mode (input or output) */
1583 DECL_HANDLER(set_console_mode)
1585 set_console_mode( req->handle, req->mode );
1588 /* add input records to a console input queue */
1589 DECL_HANDLER(write_console_input)
1591 struct console_input *console;
1593 reply->written = 0;
1594 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1595 FILE_WRITE_PROPERTIES, &console_input_ops )))
1596 return;
1597 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1598 get_req_data() );
1599 release_object( console );
1602 /* fetch input records from a console input queue */
1603 DECL_HANDLER(read_console_input)
1605 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1606 reply->read = read_console_input( req->handle, count, req->flush );
1609 /* appends a string to console's history */
1610 DECL_HANDLER(append_console_input_history)
1612 struct console_input *console;
1614 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1615 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1616 release_object( console );
1619 /* appends a string to console's history */
1620 DECL_HANDLER(get_console_input_history)
1622 struct console_input *console;
1624 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1625 reply->total = console_input_get_hist( console, req->index );
1626 release_object( console );
1629 /* creates a screen buffer */
1630 DECL_HANDLER(create_console_output)
1632 struct console_input* console;
1633 struct screen_buffer* screen_buffer;
1634 int fd;
1636 if (req->fd != -1)
1638 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1640 set_error( STATUS_INVALID_HANDLE );
1641 return;
1644 else fd = -1;
1645 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1647 if (fd != -1) close( fd );
1648 return;
1650 if (console_input_is_bare( console ) ^ (fd != -1))
1652 if (fd != -1) close( fd );
1653 release_object( console );
1654 set_error( STATUS_INVALID_HANDLE );
1655 return;
1658 screen_buffer = create_console_output( console, fd );
1659 if (screen_buffer)
1661 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1662 * see file.c on how this could be done */
1663 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1664 release_object( screen_buffer );
1666 release_object( console );
1669 /* set info about a console screen buffer */
1670 DECL_HANDLER(set_console_output_info)
1672 struct screen_buffer *screen_buffer;
1674 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1675 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1677 set_console_output_info( screen_buffer, req );
1678 release_object( screen_buffer );
1682 /* get info about a console screen buffer */
1683 DECL_HANDLER(get_console_output_info)
1685 struct screen_buffer *screen_buffer;
1687 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1688 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1690 reply->cursor_size = screen_buffer->cursor_size;
1691 reply->cursor_visible = screen_buffer->cursor_visible;
1692 reply->cursor_x = screen_buffer->cursor_x;
1693 reply->cursor_y = screen_buffer->cursor_y;
1694 reply->width = screen_buffer->width;
1695 reply->height = screen_buffer->height;
1696 reply->attr = screen_buffer->attr;
1697 reply->popup_attr = screen_buffer->popup_attr;
1698 reply->win_left = screen_buffer->win.left;
1699 reply->win_top = screen_buffer->win.top;
1700 reply->win_right = screen_buffer->win.right;
1701 reply->win_bottom = screen_buffer->win.bottom;
1702 reply->max_width = screen_buffer->max_width;
1703 reply->max_height = screen_buffer->max_height;
1704 reply->font_width = screen_buffer->font.width;
1705 reply->font_height = screen_buffer->font.height;
1706 set_reply_data( screen_buffer->color_map,
1707 min( sizeof(screen_buffer->color_map), get_reply_max_size() ));
1708 release_object( screen_buffer );
1712 /* read data (chars & attrs) from a screen buffer */
1713 DECL_HANDLER(read_console_output)
1715 struct screen_buffer *screen_buffer;
1717 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1718 FILE_READ_DATA, &screen_buffer_ops )))
1720 if (console_input_is_bare( screen_buffer->input ))
1722 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1723 release_object( screen_buffer );
1724 return;
1726 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1727 reply->width = screen_buffer->width;
1728 reply->height = screen_buffer->height;
1729 release_object( screen_buffer );
1733 /* write data (char and/or attrs) to a screen buffer */
1734 DECL_HANDLER(write_console_output)
1736 struct screen_buffer *screen_buffer;
1738 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1739 FILE_WRITE_DATA, &screen_buffer_ops)))
1741 if (console_input_is_bare( screen_buffer->input ))
1743 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1744 release_object( screen_buffer );
1745 return;
1747 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1748 req->mode, req->x, req->y, req->wrap );
1749 reply->width = screen_buffer->width;
1750 reply->height = screen_buffer->height;
1751 release_object( screen_buffer );
1755 /* fill a screen buffer with constant data (chars and/or attributes) */
1756 DECL_HANDLER(fill_console_output)
1758 struct screen_buffer *screen_buffer;
1760 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1761 FILE_WRITE_DATA, &screen_buffer_ops)))
1763 if (console_input_is_bare( screen_buffer->input ))
1765 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1766 release_object( screen_buffer );
1767 return;
1769 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1770 req->x, req->y, req->count, req->wrap );
1771 release_object( screen_buffer );
1775 /* move a rect of data in a screen buffer */
1776 DECL_HANDLER(move_console_output)
1778 struct screen_buffer *screen_buffer;
1780 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1781 FILE_WRITE_DATA, &screen_buffer_ops)))
1783 if (console_input_is_bare( screen_buffer->input ))
1785 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1786 release_object( screen_buffer );
1787 return;
1789 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1790 req->w, req->h );
1791 release_object( screen_buffer );
1795 /* sends a signal to a console (process, group...) */
1796 DECL_HANDLER(send_console_signal)
1798 process_id_t group;
1800 group = req->group_id ? req->group_id : current->process->group_id;
1802 if (!group)
1803 set_error( STATUS_INVALID_PARAMETER );
1804 else
1805 propagate_console_signal( current->process->console, req->signal, group );
1808 /* get console which renderer is 'current' */
1809 static int cgwe_enum( struct process* process, void* user)
1811 if (process->console && process->console->renderer == current)
1813 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1814 return 1;
1816 return 0;
1819 DECL_HANDLER(get_console_wait_event)
1821 struct console_input* console = NULL;
1823 if (current->process->console)
1824 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1825 else enum_processes(cgwe_enum, &console);
1827 if (console)
1829 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1830 release_object( console );
1832 else set_error( STATUS_INVALID_PARAMETER );