wineboot: Fix do_cpuid() calling convention.
[wine.git] / server / console.c
blob7d1fc5d2680bc31150e724dd93c0eaa88a2ffa3c
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_kernel_obj_list, /* get_kernel_obj_list */
91 no_close_handle, /* close_handle */
92 console_input_destroy /* destroy */
95 static void console_input_events_dump( struct object *obj, int verbose );
96 static void console_input_events_destroy( struct object *obj );
97 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
99 struct console_input_events
101 struct object obj; /* object header */
102 int num_alloc; /* number of allocated events */
103 int num_used; /* number of actually used events */
104 struct console_renderer_event* events;
107 static const struct object_ops console_input_events_ops =
109 sizeof(struct console_input_events), /* size */
110 console_input_events_dump, /* dump */
111 no_get_type, /* get_type */
112 add_queue, /* add_queue */
113 remove_queue, /* remove_queue */
114 console_input_events_signaled, /* signaled */
115 no_satisfied, /* satisfied */
116 no_signal, /* signal */
117 no_get_fd, /* get_fd */
118 default_fd_map_access, /* map_access */
119 default_get_sd, /* get_sd */
120 default_set_sd, /* set_sd */
121 no_lookup_name, /* lookup_name */
122 no_link_name, /* link_name */
123 NULL, /* unlink_name */
124 no_open_file, /* open_file */
125 no_kernel_obj_list, /* get_kernel_obj_list */
126 no_close_handle, /* close_handle */
127 console_input_events_destroy /* destroy */
130 struct font_info
132 short int width;
133 short int height;
136 struct screen_buffer
138 struct object obj; /* object header */
139 struct list entry; /* entry in list of all screen buffers */
140 struct console_input *input; /* associated console input */
141 int mode; /* output mode */
142 int cursor_size; /* size of cursor (percentage filled) */
143 int cursor_visible;/* cursor visibility flag */
144 int cursor_x; /* position of cursor */
145 int cursor_y; /* position of cursor */
146 int width; /* size (w-h) of the screen buffer */
147 int height;
148 int max_width; /* size (w-h) of the window given font size */
149 int max_height;
150 char_info_t *data; /* the data for each cell - a width x height matrix */
151 unsigned short attr; /* default fill attributes (screen colors) */
152 unsigned short popup_attr; /* pop-up color attributes */
153 unsigned int color_map[16]; /* color table */
154 rectangle_t win; /* current visible window on the screen buffer *
155 * as seen in wineconsole */
156 struct font_info font; /* console font information */
157 struct fd *fd; /* for bare console, attached output fd */
160 static void screen_buffer_dump( struct object *obj, int verbose );
161 static void screen_buffer_destroy( struct object *obj );
162 static struct fd *screen_buffer_get_fd( struct object *obj );
164 static const struct object_ops screen_buffer_ops =
166 sizeof(struct screen_buffer), /* size */
167 screen_buffer_dump, /* dump */
168 no_get_type, /* get_type */
169 no_add_queue, /* add_queue */
170 NULL, /* remove_queue */
171 NULL, /* signaled */
172 NULL, /* satisfied */
173 no_signal, /* signal */
174 screen_buffer_get_fd, /* get_fd */
175 default_fd_map_access, /* map_access */
176 default_get_sd, /* get_sd */
177 default_set_sd, /* set_sd */
178 no_lookup_name, /* lookup_name */
179 no_link_name, /* link_name */
180 NULL, /* unlink_name */
181 no_open_file, /* open_file */
182 no_kernel_obj_list, /* get_kernel_obj_list */
183 no_close_handle, /* close_handle */
184 screen_buffer_destroy /* destroy */
187 static enum server_fd_type console_get_fd_type( struct fd *fd );
189 static const struct fd_ops console_fd_ops =
191 default_fd_get_poll_events, /* get_poll_events */
192 default_poll_event, /* poll_event */
193 console_get_fd_type, /* get_fd_type */
194 no_fd_read, /* read */
195 no_fd_write, /* write */
196 no_fd_flush, /* flush */
197 no_fd_get_file_info, /* get_file_info */
198 no_fd_get_volume_info, /* get_volume_info */
199 default_fd_ioctl, /* ioctl */
200 default_fd_queue_async, /* queue_async */
201 default_fd_reselect_async /* reselect_async */
204 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
206 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
208 static int console_input_is_bare( struct console_input* cin )
210 return cin->evt == NULL;
213 static struct fd *console_input_get_fd( struct object* obj )
215 struct console_input *console_input = (struct console_input*)obj;
216 assert( obj->ops == &console_input_ops );
217 if (console_input->fd)
218 return (struct fd*)grab_object( console_input->fd );
219 set_error( STATUS_OBJECT_TYPE_MISMATCH );
220 return NULL;
223 static enum server_fd_type console_get_fd_type( struct fd *fd )
225 return FD_TYPE_CHAR;
228 /* dumps the renderer events of a console */
229 static void console_input_events_dump( struct object *obj, int verbose )
231 struct console_input_events *evts = (struct console_input_events *)obj;
232 assert( obj->ops == &console_input_events_ops );
233 fprintf( stderr, "Console input events: %d/%d events\n",
234 evts->num_used, evts->num_alloc );
237 /* destroys the renderer events of a console */
238 static void console_input_events_destroy( struct object *obj )
240 struct console_input_events *evts = (struct console_input_events *)obj;
241 assert( obj->ops == &console_input_events_ops );
242 free( evts->events );
245 /* the renderer events list is signaled when it's not empty */
246 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
248 struct console_input_events *evts = (struct console_input_events *)obj;
249 assert( obj->ops == &console_input_events_ops );
250 return (evts->num_used != 0);
253 /* add an event to the console's renderer events list */
254 static void console_input_events_append( struct console_input* console,
255 struct console_renderer_event* evt)
257 struct console_input_events* evts;
258 int collapsed = FALSE;
260 if (!(evts = console->evt)) return;
261 /* to be done even when evt has been generated by the renderer ? */
263 /* try to collapse evt into current queue's events */
264 if (evts->num_used)
266 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
268 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
269 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
271 /* if two update events overlap, collapse them into a single one */
272 if (last->u.update.bottom + 1 >= evt->u.update.top &&
273 evt->u.update.bottom + 1 >= last->u.update.top)
275 last->u.update.top = min(last->u.update.top, evt->u.update.top);
276 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
277 collapsed = TRUE;
281 if (!collapsed)
283 if (evts->num_used == evts->num_alloc)
285 evts->num_alloc += 16;
286 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
287 assert(evts->events);
289 evts->events[evts->num_used++] = *evt;
291 wake_up( &evts->obj, 0 );
294 /* retrieves events from the console's renderer events list */
295 static void console_input_events_get( struct console_input_events* evts )
297 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
299 if (num > evts->num_used) num = evts->num_used;
300 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
301 if (num < evts->num_used)
303 memmove( &evts->events[0], &evts->events[num],
304 (evts->num_used - num) * sizeof(evts->events[0]) );
306 evts->num_used -= num;
309 static struct console_input_events *create_console_input_events(void)
311 struct console_input_events* evt;
313 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
314 evt->num_alloc = evt->num_used = 0;
315 evt->events = NULL;
316 return evt;
319 static struct object *create_console_input( struct thread* renderer, int fd )
321 struct console_input *console_input;
323 if (!(console_input = alloc_object( &console_input_ops )))
325 if (fd != -1) close( fd );
326 return NULL;
328 console_input->renderer = renderer;
329 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
330 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
331 ENABLE_EXTENDED_FLAGS;
332 console_input->num_proc = 0;
333 console_input->active = NULL;
334 console_input->recnum = 0;
335 console_input->records = NULL;
336 console_input->evt = renderer ? create_console_input_events() : NULL;
337 console_input->title = NULL;
338 console_input->history_size = 50;
339 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
340 console_input->history_index = 0;
341 console_input->history_mode = 0;
342 console_input->edition_mode = 0;
343 console_input->input_cp = 0;
344 console_input->output_cp = 0;
345 console_input->win = 0;
346 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
347 console_input->fd = NULL;
349 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
351 if (fd != -1) close( fd );
352 console_input->history_size = 0;
353 release_object( console_input );
354 return NULL;
356 if (fd != -1) /* bare console */
358 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
359 FILE_SYNCHRONOUS_IO_NONALERT )))
361 release_object( console_input );
362 return NULL;
364 allow_fd_caching( console_input->fd );
367 return &console_input->obj;
370 static void generate_sb_initial_events( struct console_input *console_input )
372 struct screen_buffer *screen_buffer = console_input->active;
373 struct console_renderer_event evt;
375 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
376 memset(&evt.u, 0, sizeof(evt.u));
377 console_input_events_append( console_input, &evt );
379 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
380 evt.u.resize.width = screen_buffer->width;
381 evt.u.resize.height = screen_buffer->height;
382 console_input_events_append( console_input, &evt );
384 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
385 evt.u.display.left = screen_buffer->win.left;
386 evt.u.display.top = screen_buffer->win.top;
387 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
388 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
389 console_input_events_append( console_input, &evt );
391 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
392 evt.u.update.top = 0;
393 evt.u.update.bottom = screen_buffer->height - 1;
394 console_input_events_append( console_input, &evt );
396 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
397 evt.u.cursor_geom.size = screen_buffer->cursor_size;
398 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
399 console_input_events_append( console_input, &evt );
401 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
402 evt.u.cursor_pos.x = screen_buffer->cursor_x;
403 evt.u.cursor_pos.y = screen_buffer->cursor_y;
404 console_input_events_append( console_input, &evt );
407 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
409 struct screen_buffer *screen_buffer;
410 int i;
412 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
414 if (fd != -1) close( fd );
415 return NULL;
417 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
418 screen_buffer->input = console_input;
419 screen_buffer->cursor_size = 100;
420 screen_buffer->cursor_visible = 1;
421 screen_buffer->width = 80;
422 screen_buffer->height = 150;
423 screen_buffer->max_width = 80;
424 screen_buffer->max_height = 25;
425 screen_buffer->cursor_x = 0;
426 screen_buffer->cursor_y = 0;
427 screen_buffer->attr = 0x0F;
428 screen_buffer->popup_attr = 0xF5;
429 screen_buffer->win.left = 0;
430 screen_buffer->win.right = screen_buffer->max_width - 1;
431 screen_buffer->win.top = 0;
432 screen_buffer->win.bottom = screen_buffer->max_height - 1;
433 screen_buffer->data = NULL;
434 screen_buffer->font.width = 0;
435 screen_buffer->font.height = 0;
436 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
437 list_add_head( &screen_buffer_list, &screen_buffer->entry );
439 if (fd == -1)
440 screen_buffer->fd = NULL;
441 else
443 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
444 FILE_SYNCHRONOUS_IO_NONALERT )))
446 release_object( screen_buffer );
447 return NULL;
449 allow_fd_caching(screen_buffer->fd);
452 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
453 sizeof(*screen_buffer->data) )))
455 release_object( screen_buffer );
456 return NULL;
458 /* clear the first row */
459 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
460 /* and copy it to all other rows */
461 for (i = 1; i < screen_buffer->height; i++)
462 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
463 screen_buffer->width * sizeof(char_info_t) );
465 if (!console_input->active)
467 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
468 generate_sb_initial_events( console_input );
470 return screen_buffer;
473 /* free the console for this process */
474 int free_console( struct process *process )
476 struct console_input* console = process->console;
478 if (!console) return 0;
480 process->console = NULL;
481 if (--console->num_proc == 0 && console->renderer)
483 /* all processes have terminated... tell the renderer to terminate too */
484 struct console_renderer_event evt;
485 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
486 memset(&evt.u, 0, sizeof(evt.u));
487 console_input_events_append( console, &evt );
489 release_object( console );
491 return 1;
494 /* let process inherit the console from parent... this handle two cases :
495 * 1/ generic console inheritance
496 * 2/ parent is a renderer which launches process, and process should attach to the console
497 * rendered by parent
499 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
501 int done = 0;
502 struct process* parent = parent_thread->process;
504 /* if parent is a renderer, then attach current process to its console
505 * a bit hacky....
507 if (hconin)
509 struct console_input* console;
511 /* FIXME: should we check some access rights ? */
512 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
513 0, &console_input_ops )))
515 if (console->renderer == parent_thread)
517 process->console = (struct console_input*)grab_object( console );
518 process->console->num_proc++;
519 done = 1;
521 release_object( console );
523 else clear_error(); /* ignore error */
525 /* otherwise, if parent has a console, attach child to this console */
526 if (!done && parent->console)
528 process->console = (struct console_input*)grab_object( parent->console );
529 process->console->num_proc++;
533 struct thread *console_get_renderer( struct console_input *console )
535 return console->renderer;
538 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
540 struct console_input* console = NULL;
542 if (handle)
543 console = (struct console_input *)get_handle_obj( current->process, handle,
544 access, &console_input_ops );
545 else if (current->process->console)
547 console = (struct console_input *)grab_object( current->process->console );
550 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
551 return console;
554 struct console_signal_info
556 struct console_input *console;
557 process_id_t group;
558 int signal;
561 static int propagate_console_signal_cb(struct process *process, void *user)
563 struct console_signal_info* csi = (struct console_signal_info*)user;
565 if (process->console == csi->console && process->running_threads &&
566 (!csi->group || process->group_id == csi->group))
568 /* find a suitable thread to signal */
569 struct thread *thread;
570 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
572 if (send_thread_signal( thread, csi->signal )) break;
575 return FALSE;
578 static void propagate_console_signal( struct console_input *console,
579 int sig, process_id_t group_id )
581 struct console_signal_info csi;
583 if (!console)
585 set_error( STATUS_INVALID_PARAMETER );
586 return;
588 /* FIXME: should support the other events (like CTRL_BREAK) */
589 if (sig != CTRL_C_EVENT)
591 set_error( STATUS_NOT_IMPLEMENTED );
592 return;
594 csi.console = console;
595 csi.signal = SIGINT;
596 csi.group = group_id;
598 enum_processes(propagate_console_signal_cb, &csi);
601 static int get_console_mode( obj_handle_t handle )
603 struct object *obj;
604 int ret = 0;
606 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
608 if (obj->ops == &console_input_ops)
610 ret = ((struct console_input *)obj)->mode;
612 else if (obj->ops == &screen_buffer_ops)
614 ret = ((struct screen_buffer *)obj)->mode;
616 else
617 set_error( STATUS_OBJECT_TYPE_MISMATCH );
618 release_object( obj );
620 return ret;
623 /* changes the mode of either a console input or a screen buffer */
624 static int set_console_mode( obj_handle_t handle, int mode )
626 struct object *obj;
627 int ret = 0;
629 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
630 return 0;
631 if (obj->ops == &console_input_ops)
633 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
634 ((struct console_input *)obj)->mode = mode;
635 ret = 1;
637 else if (obj->ops == &screen_buffer_ops)
639 ((struct screen_buffer *)obj)->mode = mode;
640 ret = 1;
642 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
643 release_object( obj );
644 return ret;
647 /* add input events to a console input queue */
648 static int write_console_input( struct console_input* console, int count,
649 const INPUT_RECORD *records )
651 INPUT_RECORD *new_rec;
653 if (!count) return 0;
654 if (!(new_rec = realloc( console->records,
655 (console->recnum + count) * sizeof(INPUT_RECORD) )))
657 set_error( STATUS_NO_MEMORY );
658 return -1;
660 console->records = new_rec;
661 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
663 if (console->mode & ENABLE_PROCESSED_INPUT)
665 int i = 0;
666 while (i < count)
668 if (records[i].EventType == KEY_EVENT &&
669 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
670 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
672 if (i != count - 1)
673 memcpy( &console->records[console->recnum + i],
674 &console->records[console->recnum + i + 1],
675 (count - i - 1) * sizeof(INPUT_RECORD) );
676 count--;
677 if (records[i].Event.KeyEvent.bKeyDown)
679 /* send SIGINT to all processes attached to this console */
680 propagate_console_signal( console, CTRL_C_EVENT, 0 );
683 else i++;
686 if (!console->recnum && count) set_event( console->event );
687 console->recnum += count;
688 return count;
691 /* retrieve a pointer to the console input records */
692 static int read_console_input( obj_handle_t handle, int count, int flush )
694 struct console_input *console;
696 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
697 FILE_READ_DATA, &console_input_ops )))
698 return -1;
700 if (!count)
702 /* special case: do not retrieve anything, but return
703 * the total number of records available */
704 count = console->recnum;
706 else
708 if (count > console->recnum) count = console->recnum;
709 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
711 if (flush)
713 int i;
714 for (i = count; i < console->recnum; i++)
715 console->records[i-count] = console->records[i];
716 if ((console->recnum -= count) > 0)
718 INPUT_RECORD *new_rec = realloc( console->records,
719 console->recnum * sizeof(INPUT_RECORD) );
720 if (new_rec) console->records = new_rec;
722 else
724 free( console->records );
725 console->records = NULL;
726 reset_event( console->event );
729 release_object( console );
730 return count;
733 /* set misc console input information */
734 static int set_console_input_info( const struct set_console_input_info_request *req,
735 const WCHAR *title, data_size_t len )
737 struct console_input *console;
738 struct console_renderer_event evt;
740 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
741 if (console_input_is_bare(console) &&
742 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
743 SET_CONSOLE_INPUT_INFO_WIN)))
745 set_error( STATUS_UNSUCCESSFUL );
746 goto error;
749 memset(&evt.u, 0, sizeof(evt.u));
750 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
752 struct screen_buffer *screen_buffer;
754 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
755 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
756 if (!screen_buffer || screen_buffer->input != console)
758 set_error( STATUS_INVALID_HANDLE );
759 if (screen_buffer) release_object( screen_buffer );
760 goto error;
763 if (screen_buffer != console->active)
765 if (console->active) release_object( console->active );
766 console->active = screen_buffer;
767 generate_sb_initial_events( console );
769 else
770 release_object( screen_buffer );
772 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
774 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
775 if (new_title)
777 memcpy( new_title, title, len );
778 new_title[len / sizeof(WCHAR)] = 0;
779 free( console->title );
780 console->title = new_title;
781 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
782 console_input_events_append( console, &evt );
785 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
787 console->history_mode = req->history_mode;
789 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
790 console->history_size != req->history_size)
792 WCHAR** mem = NULL;
793 int i;
794 int delta;
796 if (req->history_size)
798 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
799 if (!mem) goto error;
800 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
803 delta = (console->history_index > req->history_size) ?
804 (console->history_index - req->history_size) : 0;
806 for (i = delta; i < console->history_index; i++)
808 mem[i - delta] = console->history[i];
809 console->history[i] = NULL;
811 console->history_index -= delta;
813 for (i = 0; i < console->history_size; i++)
814 free( console->history[i] );
815 free( console->history );
816 console->history = mem;
817 console->history_size = req->history_size;
819 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
821 console->edition_mode = req->edition_mode;
823 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
825 console->input_cp = req->input_cp;
827 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
829 console->output_cp = req->output_cp;
831 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
833 console->win = req->win;
835 release_object( console );
836 return 1;
837 error:
838 if (console) release_object( console );
839 return 0;
842 /* resize a screen buffer */
843 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
844 int new_width, int new_height )
846 int i, old_width, old_height, copy_width, copy_height;
847 char_info_t *new_data;
849 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
851 set_error( STATUS_NO_MEMORY );
852 return 0;
854 old_width = screen_buffer->width;
855 old_height = screen_buffer->height;
856 copy_width = min( old_width, new_width );
857 copy_height = min( old_height, new_height );
859 /* copy all the rows */
860 for (i = 0; i < copy_height; i++)
862 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
863 copy_width * sizeof(char_info_t) );
866 /* clear the end of each row */
867 if (new_width > old_width)
869 /* fill first row */
870 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
871 /* and blast it to the other rows */
872 for (i = 1; i < copy_height; i++)
873 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
874 (new_width - old_width) * sizeof(char_info_t) );
877 /* clear remaining rows */
878 if (new_height > old_height)
880 /* fill first row */
881 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
882 /* and blast it to the other rows */
883 for (i = old_height+1; i < new_height; i++)
884 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
885 new_width * sizeof(char_info_t) );
887 free( screen_buffer->data );
888 screen_buffer->data = new_data;
889 screen_buffer->width = new_width;
890 screen_buffer->height = new_height;
891 return 1;
894 /* set misc screen buffer information */
895 static int set_console_output_info( struct screen_buffer *screen_buffer,
896 const struct set_console_output_info_request *req )
898 struct console_renderer_event evt;
900 memset(&evt.u, 0, sizeof(evt.u));
901 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
903 if (req->cursor_size < 1 || req->cursor_size > 100)
905 set_error( STATUS_INVALID_PARAMETER );
906 return 0;
908 if (screen_buffer->cursor_size != req->cursor_size ||
909 screen_buffer->cursor_visible != req->cursor_visible)
911 screen_buffer->cursor_size = req->cursor_size;
912 screen_buffer->cursor_visible = req->cursor_visible;
913 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
914 evt.u.cursor_geom.size = req->cursor_size;
915 evt.u.cursor_geom.visible = req->cursor_visible;
916 console_input_events_append( screen_buffer->input, &evt );
919 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
921 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
922 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
924 set_error( STATUS_INVALID_PARAMETER );
925 return 0;
927 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
929 screen_buffer->cursor_x = req->cursor_x;
930 screen_buffer->cursor_y = req->cursor_y;
931 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
932 evt.u.cursor_pos.x = req->cursor_x;
933 evt.u.cursor_pos.y = req->cursor_y;
934 console_input_events_append( screen_buffer->input, &evt );
937 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
939 unsigned cc;
941 /* new screen-buffer cannot be smaller than actual window */
942 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
943 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
945 set_error( STATUS_INVALID_PARAMETER );
946 return 0;
948 /* FIXME: there are also some basic minimum and max size to deal with */
949 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
951 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
952 evt.u.resize.width = req->width;
953 evt.u.resize.height = req->height;
954 console_input_events_append( screen_buffer->input, &evt );
956 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
957 evt.u.update.top = 0;
958 evt.u.update.bottom = screen_buffer->height - 1;
959 console_input_events_append( screen_buffer->input, &evt );
961 /* scroll window to display sb */
962 if (screen_buffer->win.right >= req->width)
964 screen_buffer->win.right -= screen_buffer->win.left;
965 screen_buffer->win.left = 0;
967 if (screen_buffer->win.bottom >= req->height)
969 screen_buffer->win.bottom -= screen_buffer->win.top;
970 screen_buffer->win.top = 0;
972 /* reset cursor if needed (normally, if cursor was outside of new sb, the
973 * window has been shifted so that the new position of the cursor will be
974 * visible */
975 cc = 0;
976 if (screen_buffer->cursor_x >= req->width)
978 screen_buffer->cursor_x = req->width - 1;
979 cc++;
981 if (screen_buffer->cursor_y >= req->height)
983 screen_buffer->cursor_y = req->height - 1;
984 cc++;
986 if (cc)
988 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
989 evt.u.cursor_pos.x = req->cursor_x;
990 evt.u.cursor_pos.y = req->cursor_y;
991 console_input_events_append( screen_buffer->input, &evt );
994 if (screen_buffer == screen_buffer->input->active &&
995 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
997 INPUT_RECORD ir;
998 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
999 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
1000 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
1001 write_console_input( screen_buffer->input, 1, &ir );
1004 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1006 screen_buffer->attr = req->attr;
1008 if (req->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1010 screen_buffer->popup_attr = req->popup_attr;
1012 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1014 if (req->win_left < 0 || req->win_left > req->win_right ||
1015 req->win_right >= screen_buffer->width ||
1016 req->win_top < 0 || req->win_top > req->win_bottom ||
1017 req->win_bottom >= screen_buffer->height)
1019 set_error( STATUS_INVALID_PARAMETER );
1020 return 0;
1022 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1023 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1025 screen_buffer->win.left = req->win_left;
1026 screen_buffer->win.top = req->win_top;
1027 screen_buffer->win.right = req->win_right;
1028 screen_buffer->win.bottom = req->win_bottom;
1029 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1030 evt.u.display.left = req->win_left;
1031 evt.u.display.top = req->win_top;
1032 evt.u.display.width = req->win_right - req->win_left + 1;
1033 evt.u.display.height = req->win_bottom - req->win_top + 1;
1034 console_input_events_append( screen_buffer->input, &evt );
1037 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1039 screen_buffer->max_width = req->max_width;
1040 screen_buffer->max_height = req->max_height;
1042 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1044 screen_buffer->font.width = req->font_width;
1045 screen_buffer->font.height = req->font_height;
1047 if (req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1049 memcpy( screen_buffer->color_map, get_req_data(),
1050 min( sizeof(screen_buffer->color_map), get_req_data_size() ));
1053 return 1;
1056 /* appends a new line to history (history is a fixed size array) */
1057 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1059 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1061 if (!ptr)
1062 return;
1064 if (!console || !console->history_size)
1066 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1067 free( ptr );
1068 return;
1071 memcpy( ptr, buf, len * sizeof(WCHAR) );
1072 ptr[len] = 0;
1074 if (console->history_mode && console->history_index &&
1075 !strcmpW( console->history[console->history_index - 1], ptr ))
1077 /* ok, mode ask us to not use twice the same string...
1078 * so just free mem and returns
1080 set_error( STATUS_ALIAS_EXISTS );
1081 free(ptr);
1082 return;
1085 if (console->history_index < console->history_size)
1087 console->history[console->history_index++] = ptr;
1089 else
1091 free( console->history[0]) ;
1092 memmove( &console->history[0], &console->history[1],
1093 (console->history_size - 1) * sizeof(WCHAR*) );
1094 console->history[console->history_size - 1] = ptr;
1098 /* returns a line from the cache */
1099 static data_size_t console_input_get_hist( struct console_input *console, int index )
1101 data_size_t ret = 0;
1103 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1104 else
1106 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1107 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1109 return ret;
1112 /* dumb dump */
1113 static void console_input_dump( struct object *obj, int verbose )
1115 struct console_input *console = (struct console_input *)obj;
1116 assert( obj->ops == &console_input_ops );
1117 fprintf( stderr, "Console input active=%p evt=%p\n",
1118 console->active, console->evt );
1121 static void console_input_destroy( struct object *obj )
1123 struct console_input* console_in = (struct console_input *)obj;
1124 struct screen_buffer* curr;
1125 int i;
1127 assert( obj->ops == &console_input_ops );
1128 free( console_in->title );
1129 free( console_in->records );
1131 if (console_in->active) release_object( console_in->active );
1132 console_in->active = NULL;
1134 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1136 if (curr->input == console_in) curr->input = NULL;
1139 if (console_in->evt)
1141 release_object( console_in->evt );
1142 console_in->evt = NULL;
1144 if (console_in->event)
1145 release_object( console_in->event );
1146 if (console_in->fd)
1147 release_object( console_in->fd );
1149 for (i = 0; i < console_in->history_size; i++)
1150 free( console_in->history[i] );
1151 free( console_in->history );
1154 static void screen_buffer_dump( struct object *obj, int verbose )
1156 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1157 assert( obj->ops == &screen_buffer_ops );
1159 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1162 static void screen_buffer_destroy( struct object *obj )
1164 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1166 assert( obj->ops == &screen_buffer_ops );
1168 list_remove( &screen_buffer->entry );
1170 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1172 struct screen_buffer *sb;
1174 screen_buffer->input->active = NULL;
1175 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1177 if (sb->input == screen_buffer->input)
1179 sb->input->active = sb;
1180 break;
1184 if (screen_buffer->fd) release_object( screen_buffer->fd );
1185 free( screen_buffer->data );
1188 static struct fd *screen_buffer_get_fd( struct object *obj )
1190 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1191 assert( obj->ops == &screen_buffer_ops );
1192 if (screen_buffer->fd)
1193 return (struct fd*)grab_object( screen_buffer->fd );
1194 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1195 return NULL;
1198 /* write data into a screen buffer */
1199 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1200 const void* data, enum char_info_mode mode,
1201 int x, int y, int wrap )
1203 unsigned int i;
1204 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1206 if (y >= screen_buffer->height) return 0;
1208 if (wrap)
1209 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1210 else
1211 end = screen_buffer->data + (y+1) * screen_buffer->width;
1213 switch(mode)
1215 case CHAR_INFO_MODE_TEXT:
1217 const WCHAR *ptr = data;
1218 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1220 break;
1221 case CHAR_INFO_MODE_ATTR:
1223 const unsigned short *ptr = data;
1224 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1226 break;
1227 case CHAR_INFO_MODE_TEXTATTR:
1229 const char_info_t *ptr = data;
1230 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1232 break;
1233 case CHAR_INFO_MODE_TEXTSTDATTR:
1235 const WCHAR *ptr = data;
1236 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1238 dest->ch = ptr[i];
1239 dest->attr = screen_buffer->attr;
1242 break;
1243 default:
1244 set_error( STATUS_INVALID_PARAMETER );
1245 return 0;
1248 if (i && screen_buffer == screen_buffer->input->active)
1250 struct console_renderer_event evt;
1251 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1252 memset(&evt.u, 0, sizeof(evt.u));
1253 evt.u.update.top = y + x / screen_buffer->width;
1254 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1255 console_input_events_append( screen_buffer->input, &evt );
1257 return i;
1260 /* fill a screen buffer with uniform data */
1261 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1262 enum char_info_mode mode, int x, int y, int count, int wrap )
1264 int i;
1265 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1267 if (y >= screen_buffer->height) return 0;
1269 if (wrap)
1270 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1271 else
1272 end = screen_buffer->data + (y+1) * screen_buffer->width;
1274 if (count > end - dest) count = end - dest;
1276 switch(mode)
1278 case CHAR_INFO_MODE_TEXT:
1279 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1280 break;
1281 case CHAR_INFO_MODE_ATTR:
1282 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1283 break;
1284 case CHAR_INFO_MODE_TEXTATTR:
1285 for (i = 0; i < count; i++) dest[i] = data;
1286 break;
1287 case CHAR_INFO_MODE_TEXTSTDATTR:
1288 for (i = 0; i < count; i++)
1290 dest[i].ch = data.ch;
1291 dest[i].attr = screen_buffer->attr;
1293 break;
1294 default:
1295 set_error( STATUS_INVALID_PARAMETER );
1296 return 0;
1299 if (count && screen_buffer == screen_buffer->input->active)
1301 struct console_renderer_event evt;
1302 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1303 memset(&evt.u, 0, sizeof(evt.u));
1304 evt.u.update.top = y;
1305 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1306 console_input_events_append( screen_buffer->input, &evt );
1308 return i;
1311 /* read data from a screen buffer */
1312 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1313 enum char_info_mode mode, int wrap )
1315 int i;
1316 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1318 if (y >= screen_buffer->height) return;
1320 if (wrap)
1321 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1322 else
1323 end = screen_buffer->data + (y+1) * screen_buffer->width;
1325 switch(mode)
1327 case CHAR_INFO_MODE_TEXT:
1329 WCHAR *data;
1330 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1331 if ((data = set_reply_data_size( count * sizeof(*data) )))
1333 for (i = 0; i < count; i++) data[i] = src[i].ch;
1336 break;
1337 case CHAR_INFO_MODE_ATTR:
1339 unsigned short *data;
1340 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1341 if ((data = set_reply_data_size( count * sizeof(*data) )))
1343 for (i = 0; i < count; i++) data[i] = src[i].attr;
1346 break;
1347 case CHAR_INFO_MODE_TEXTATTR:
1349 char_info_t *data;
1350 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1351 if ((data = set_reply_data_size( count * sizeof(*data) )))
1353 for (i = 0; i < count; i++) data[i] = src[i];
1356 break;
1357 default:
1358 set_error( STATUS_INVALID_PARAMETER );
1359 break;
1363 /* scroll parts of a screen buffer */
1364 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1365 int w, int h )
1367 int j;
1368 char_info_t *psrc, *pdst;
1369 struct console_renderer_event evt;
1371 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1372 xsrc + w > screen_buffer->width ||
1373 xdst + w > screen_buffer->width ||
1374 ysrc + h > screen_buffer->height ||
1375 ydst + h > screen_buffer->height ||
1376 w == 0 || h == 0)
1378 set_error( STATUS_INVALID_PARAMETER );
1379 return;
1382 if (ysrc < ydst)
1384 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1385 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1387 for (j = h; j > 0; j--)
1389 memcpy(pdst, psrc, w * sizeof(*pdst) );
1390 pdst -= screen_buffer->width;
1391 psrc -= screen_buffer->width;
1394 else
1396 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1397 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1399 for (j = 0; j < h; j++)
1401 /* we use memmove here because when psrc and pdst are the same,
1402 * copies are done on the same row, so the dst and src blocks
1403 * can overlap */
1404 memmove( pdst, psrc, w * sizeof(*pdst) );
1405 pdst += screen_buffer->width;
1406 psrc += screen_buffer->width;
1410 /* FIXME: this could be enhanced, by signalling scroll */
1411 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1412 memset(&evt.u, 0, sizeof(evt.u));
1413 evt.u.update.top = min(ysrc, ydst);
1414 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1415 console_input_events_append( screen_buffer->input, &evt );
1418 /* allocate a console for the renderer */
1419 DECL_HANDLER(alloc_console)
1421 obj_handle_t in = 0;
1422 obj_handle_t evt = 0;
1423 struct process *process;
1424 struct thread *renderer;
1425 struct console_input *console;
1426 int fd;
1427 int attach = 0;
1429 if (req->input_fd != -1)
1431 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1433 set_error( STATUS_INVALID_PARAMETER );
1434 return;
1437 else fd = -1;
1439 switch (req->pid)
1441 case 0:
1442 /* renderer is current, console to be attached to parent process */
1443 renderer = current;
1444 if (!(process = get_process_from_id( current->process->parent_id )))
1446 if (fd != -1) close( fd );
1447 set_error( STATUS_ACCESS_DENIED );
1448 return;
1450 attach = 1;
1451 break;
1452 case 0xffffffff:
1453 /* no renderer, console to be attached to current process */
1454 renderer = NULL;
1455 process = current->process;
1456 grab_object( process );
1457 attach = 1;
1458 break;
1459 default:
1460 /* renderer is current, console to be attached to req->pid */
1461 renderer = current;
1462 if (!(process = get_process_from_id( req->pid )))
1464 if (fd != -1) close( fd );
1465 return;
1469 if (attach && process->console)
1471 if (fd != -1) close( fd );
1472 set_error( STATUS_ACCESS_DENIED );
1473 goto the_end;
1476 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1478 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1480 if (!console->evt ||
1481 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1483 if (attach)
1485 process->console = (struct console_input*)grab_object( console );
1486 console->num_proc++;
1488 reply->handle_in = in;
1489 reply->event = evt;
1490 release_object( console );
1491 goto the_end;
1493 close_handle( current->process, in );
1495 release_object( console );
1497 the_end:
1498 release_object( process );
1501 /* free the console of the current process */
1502 DECL_HANDLER(free_console)
1504 free_console( current->process );
1507 /* let the renderer peek the events it's waiting on */
1508 DECL_HANDLER(get_console_renderer_events)
1510 struct console_input_events *evt;
1512 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1513 FILE_READ_PROPERTIES, &console_input_events_ops );
1514 if (!evt) return;
1515 console_input_events_get( evt );
1516 release_object( evt );
1519 /* open a handle to the process console */
1520 DECL_HANDLER(open_console)
1522 struct object *obj = NULL;
1524 reply->handle = 0;
1525 if (!req->from)
1527 if (current->process->console)
1528 obj = grab_object( (struct object*)current->process->console );
1530 else if (req->from == (obj_handle_t)1)
1532 if (current->process->console && current->process->console->active)
1533 obj = grab_object( (struct object*)current->process->console->active );
1535 else if ((obj = get_handle_obj( current->process, req->from,
1536 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1538 struct console_input *console = (struct console_input *)obj;
1539 obj = (console->active) ? grab_object( console->active ) : NULL;
1540 release_object( console );
1543 /* FIXME: req->share is not used (as in screen buffer creation) */
1544 if (obj)
1546 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1547 release_object( obj );
1549 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1552 /* attach to a other process's console */
1553 DECL_HANDLER(attach_console)
1555 struct process *process;
1557 if (current->process->console)
1559 set_error( STATUS_ACCESS_DENIED );
1560 return;
1563 process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
1564 ? current->process->parent_id : req->pid );
1565 if (!process) return;
1567 if (process->console && process->console->active)
1569 reply->std_in = alloc_handle( current->process, process->console, GENERIC_READ, 0 );
1570 if (!reply->std_in) goto error;
1572 reply->std_out = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
1573 if (!reply->std_out) goto error;
1575 reply->std_err = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
1576 if (!reply->std_err) goto error;
1578 current->process->console = (struct console_input *)grab_object( process->console );
1579 current->process->console->num_proc++;
1581 else
1583 set_error( STATUS_INVALID_HANDLE );
1586 release_object( process );
1587 return;
1589 error:
1590 if (reply->std_in) close_handle( current->process, reply->std_in );
1591 if (reply->std_out) close_handle( current->process, reply->std_out );
1592 release_object( process );
1595 /* set info about a console input */
1596 DECL_HANDLER(set_console_input_info)
1598 set_console_input_info( req, get_req_data(), get_req_data_size() );
1601 /* get info about a console (output only) */
1602 DECL_HANDLER(get_console_input_info)
1604 struct console_input *console;
1606 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1607 if (console->title)
1609 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1610 if (len > get_reply_max_size()) len = get_reply_max_size();
1611 set_reply_data( console->title, len );
1613 reply->history_mode = console->history_mode;
1614 reply->history_size = console->history_size;
1615 reply->history_index = console->history_index;
1616 reply->edition_mode = console->edition_mode;
1617 reply->input_cp = console->input_cp;
1618 reply->output_cp = console->output_cp;
1619 reply->win = console->win;
1621 release_object( console );
1624 /* get a console mode (input or output) */
1625 DECL_HANDLER(get_console_mode)
1627 reply->mode = get_console_mode( req->handle );
1630 /* set a console mode (input or output) */
1631 DECL_HANDLER(set_console_mode)
1633 set_console_mode( req->handle, req->mode );
1636 /* add input records to a console input queue */
1637 DECL_HANDLER(write_console_input)
1639 struct console_input *console;
1641 reply->written = 0;
1642 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1643 FILE_WRITE_PROPERTIES, &console_input_ops )))
1644 return;
1645 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1646 get_req_data() );
1647 release_object( console );
1650 /* fetch input records from a console input queue */
1651 DECL_HANDLER(read_console_input)
1653 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1654 reply->read = read_console_input( req->handle, count, req->flush );
1657 /* appends a string to console's history */
1658 DECL_HANDLER(append_console_input_history)
1660 struct console_input *console;
1662 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1663 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1664 release_object( console );
1667 /* appends a string to console's history */
1668 DECL_HANDLER(get_console_input_history)
1670 struct console_input *console;
1672 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1673 reply->total = console_input_get_hist( console, req->index );
1674 release_object( console );
1677 /* creates a screen buffer */
1678 DECL_HANDLER(create_console_output)
1680 struct console_input* console;
1681 struct screen_buffer* screen_buffer;
1682 int fd;
1684 if (req->fd != -1)
1686 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1688 set_error( STATUS_INVALID_HANDLE );
1689 return;
1692 else fd = -1;
1693 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1695 if (fd != -1) close( fd );
1696 return;
1698 if (console_input_is_bare( console ) ^ (fd != -1))
1700 if (fd != -1) close( fd );
1701 release_object( console );
1702 set_error( STATUS_INVALID_HANDLE );
1703 return;
1706 screen_buffer = create_console_output( console, fd );
1707 if (screen_buffer)
1709 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1710 * see file.c on how this could be done */
1711 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1712 release_object( screen_buffer );
1714 release_object( console );
1717 /* set info about a console screen buffer */
1718 DECL_HANDLER(set_console_output_info)
1720 struct screen_buffer *screen_buffer;
1722 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1723 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1725 set_console_output_info( screen_buffer, req );
1726 release_object( screen_buffer );
1730 /* get info about a console screen buffer */
1731 DECL_HANDLER(get_console_output_info)
1733 struct screen_buffer *screen_buffer;
1735 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1736 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1738 reply->cursor_size = screen_buffer->cursor_size;
1739 reply->cursor_visible = screen_buffer->cursor_visible;
1740 reply->cursor_x = screen_buffer->cursor_x;
1741 reply->cursor_y = screen_buffer->cursor_y;
1742 reply->width = screen_buffer->width;
1743 reply->height = screen_buffer->height;
1744 reply->attr = screen_buffer->attr;
1745 reply->popup_attr = screen_buffer->popup_attr;
1746 reply->win_left = screen_buffer->win.left;
1747 reply->win_top = screen_buffer->win.top;
1748 reply->win_right = screen_buffer->win.right;
1749 reply->win_bottom = screen_buffer->win.bottom;
1750 reply->max_width = screen_buffer->max_width;
1751 reply->max_height = screen_buffer->max_height;
1752 reply->font_width = screen_buffer->font.width;
1753 reply->font_height = screen_buffer->font.height;
1754 set_reply_data( screen_buffer->color_map,
1755 min( sizeof(screen_buffer->color_map), get_reply_max_size() ));
1756 release_object( screen_buffer );
1760 /* read data (chars & attrs) from a screen buffer */
1761 DECL_HANDLER(read_console_output)
1763 struct screen_buffer *screen_buffer;
1765 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1766 FILE_READ_DATA, &screen_buffer_ops )))
1768 if (console_input_is_bare( screen_buffer->input ))
1770 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1771 release_object( screen_buffer );
1772 return;
1774 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1775 reply->width = screen_buffer->width;
1776 reply->height = screen_buffer->height;
1777 release_object( screen_buffer );
1781 /* write data (char and/or attrs) to a screen buffer */
1782 DECL_HANDLER(write_console_output)
1784 struct screen_buffer *screen_buffer;
1786 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1787 FILE_WRITE_DATA, &screen_buffer_ops)))
1789 if (console_input_is_bare( screen_buffer->input ))
1791 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1792 release_object( screen_buffer );
1793 return;
1795 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1796 req->mode, req->x, req->y, req->wrap );
1797 reply->width = screen_buffer->width;
1798 reply->height = screen_buffer->height;
1799 release_object( screen_buffer );
1803 /* fill a screen buffer with constant data (chars and/or attributes) */
1804 DECL_HANDLER(fill_console_output)
1806 struct screen_buffer *screen_buffer;
1808 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1809 FILE_WRITE_DATA, &screen_buffer_ops)))
1811 if (console_input_is_bare( screen_buffer->input ))
1813 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1814 release_object( screen_buffer );
1815 return;
1817 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1818 req->x, req->y, req->count, req->wrap );
1819 release_object( screen_buffer );
1823 /* move a rect of data in a screen buffer */
1824 DECL_HANDLER(move_console_output)
1826 struct screen_buffer *screen_buffer;
1828 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1829 FILE_WRITE_DATA, &screen_buffer_ops)))
1831 if (console_input_is_bare( screen_buffer->input ))
1833 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1834 release_object( screen_buffer );
1835 return;
1837 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1838 req->w, req->h );
1839 release_object( screen_buffer );
1843 /* sends a signal to a console (process, group...) */
1844 DECL_HANDLER(send_console_signal)
1846 process_id_t group;
1848 group = req->group_id ? req->group_id : current->process->group_id;
1850 if (!group)
1851 set_error( STATUS_INVALID_PARAMETER );
1852 else
1853 propagate_console_signal( current->process->console, req->signal, group );
1856 /* get console which renderer is 'current' */
1857 static int cgwe_enum( struct process* process, void* user)
1859 if (process->console && process->console->renderer == current)
1861 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1862 return 1;
1864 return 0;
1867 DECL_HANDLER(get_console_wait_event)
1869 struct console_input* console = NULL;
1871 if (current->process->console)
1872 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1873 else enum_processes(cgwe_enum, &console);
1875 if (console)
1877 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1878 release_object( console );
1880 else set_error( STATUS_INVALID_PARAMETER );