ntdll: Add a helper for platform-specific threading initialization.
[wine.git] / server / console.c
blob5feb1c2ba7ac6842d6cb5508f181d9eadbc085f4
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 history_line
47 data_size_t len;
48 WCHAR text[1];
51 struct console_input
53 struct object obj; /* object header */
54 int num_proc; /* number of processes attached to this console */
55 struct thread *renderer; /* console renderer thread */
56 int mode; /* input mode */
57 struct screen_buffer *active; /* active screen buffer */
58 int recnum; /* number of input records */
59 INPUT_RECORD *records; /* input records */
60 struct console_input_events *evt; /* synchronization event with renderer */
61 WCHAR *title; /* console title */
62 data_size_t title_len; /* length of console title */
63 struct history_line **history; /* lines history */
64 int history_size; /* number of entries in history array */
65 int history_index; /* number of used entries in history array */
66 int history_mode; /* mode of history (non zero means remove doubled strings */
67 int edition_mode; /* index to edition mode flavors */
68 int input_cp; /* console input codepage */
69 int output_cp; /* console output codepage */
70 user_handle_t win; /* window handle if backend supports it */
71 struct event *event; /* event to wait on for input queue */
72 struct fd *fd; /* for bare console, attached input fd */
75 static void console_input_dump( struct object *obj, int verbose );
76 static void console_input_destroy( struct object *obj );
77 static struct fd *console_input_get_fd( struct object *obj );
79 static const struct object_ops console_input_ops =
81 sizeof(struct console_input), /* size */
82 console_input_dump, /* dump */
83 no_get_type, /* get_type */
84 no_add_queue, /* add_queue */
85 NULL, /* remove_queue */
86 NULL, /* signaled */
87 no_satisfied, /* satisfied */
88 no_signal, /* signal */
89 console_input_get_fd, /* get_fd */
90 default_fd_map_access, /* map_access */
91 default_get_sd, /* get_sd */
92 default_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 no_link_name, /* link_name */
95 NULL, /* unlink_name */
96 no_open_file, /* open_file */
97 no_kernel_obj_list, /* get_kernel_obj_list */
98 no_close_handle, /* close_handle */
99 console_input_destroy /* destroy */
102 static void console_input_events_dump( struct object *obj, int verbose );
103 static void console_input_events_destroy( struct object *obj );
104 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
106 struct console_input_events
108 struct object obj; /* object header */
109 int num_alloc; /* number of allocated events */
110 int num_used; /* number of actually used events */
111 struct console_renderer_event* events;
114 static const struct object_ops console_input_events_ops =
116 sizeof(struct console_input_events), /* size */
117 console_input_events_dump, /* dump */
118 no_get_type, /* get_type */
119 add_queue, /* add_queue */
120 remove_queue, /* remove_queue */
121 console_input_events_signaled, /* signaled */
122 no_satisfied, /* satisfied */
123 no_signal, /* signal */
124 no_get_fd, /* get_fd */
125 default_fd_map_access, /* map_access */
126 default_get_sd, /* get_sd */
127 default_set_sd, /* set_sd */
128 no_lookup_name, /* lookup_name */
129 no_link_name, /* link_name */
130 NULL, /* unlink_name */
131 no_open_file, /* open_file */
132 no_kernel_obj_list, /* get_kernel_obj_list */
133 no_close_handle, /* close_handle */
134 console_input_events_destroy /* destroy */
137 struct font_info
139 short int width;
140 short int height;
141 short int weight;
142 short int pitch_family;
143 WCHAR *face_name;
144 data_size_t face_len;
147 struct screen_buffer
149 struct object obj; /* object header */
150 struct list entry; /* entry in list of all screen buffers */
151 struct console_input *input; /* associated console input */
152 int mode; /* output mode */
153 int cursor_size; /* size of cursor (percentage filled) */
154 int cursor_visible;/* cursor visibility flag */
155 int cursor_x; /* position of cursor */
156 int cursor_y; /* position of cursor */
157 int width; /* size (w-h) of the screen buffer */
158 int height;
159 int max_width; /* size (w-h) of the window given font size */
160 int max_height;
161 char_info_t *data; /* the data for each cell - a width x height matrix */
162 unsigned short attr; /* default fill attributes (screen colors) */
163 unsigned short popup_attr; /* pop-up color attributes */
164 unsigned int color_map[16]; /* color table */
165 rectangle_t win; /* current visible window on the screen buffer *
166 * as seen in wineconsole */
167 struct font_info font; /* console font information */
168 struct fd *fd; /* for bare console, attached output fd */
171 static void screen_buffer_dump( struct object *obj, int verbose );
172 static void screen_buffer_destroy( struct object *obj );
173 static struct fd *screen_buffer_get_fd( struct object *obj );
175 static const struct object_ops screen_buffer_ops =
177 sizeof(struct screen_buffer), /* size */
178 screen_buffer_dump, /* dump */
179 no_get_type, /* get_type */
180 no_add_queue, /* add_queue */
181 NULL, /* remove_queue */
182 NULL, /* signaled */
183 NULL, /* satisfied */
184 no_signal, /* signal */
185 screen_buffer_get_fd, /* get_fd */
186 default_fd_map_access, /* map_access */
187 default_get_sd, /* get_sd */
188 default_set_sd, /* set_sd */
189 no_lookup_name, /* lookup_name */
190 no_link_name, /* link_name */
191 NULL, /* unlink_name */
192 no_open_file, /* open_file */
193 no_kernel_obj_list, /* get_kernel_obj_list */
194 no_close_handle, /* close_handle */
195 screen_buffer_destroy /* destroy */
198 static enum server_fd_type console_get_fd_type( struct fd *fd );
200 static const struct fd_ops console_fd_ops =
202 default_fd_get_poll_events, /* get_poll_events */
203 default_poll_event, /* poll_event */
204 console_get_fd_type, /* get_fd_type */
205 no_fd_read, /* read */
206 no_fd_write, /* write */
207 no_fd_flush, /* flush */
208 no_fd_get_file_info, /* get_file_info */
209 no_fd_get_volume_info, /* get_volume_info */
210 default_fd_ioctl, /* ioctl */
211 default_fd_queue_async, /* queue_async */
212 default_fd_reselect_async /* reselect_async */
215 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
217 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
219 static int console_input_is_bare( struct console_input* cin )
221 return cin->evt == NULL;
224 static struct fd *console_input_get_fd( struct object* obj )
226 struct console_input *console_input = (struct console_input*)obj;
227 assert( obj->ops == &console_input_ops );
228 if (console_input->fd)
229 return (struct fd*)grab_object( console_input->fd );
230 set_error( STATUS_OBJECT_TYPE_MISMATCH );
231 return NULL;
234 static enum server_fd_type console_get_fd_type( struct fd *fd )
236 return FD_TYPE_CHAR;
239 /* dumps the renderer events of a console */
240 static void console_input_events_dump( struct object *obj, int verbose )
242 struct console_input_events *evts = (struct console_input_events *)obj;
243 assert( obj->ops == &console_input_events_ops );
244 fprintf( stderr, "Console input events: %d/%d events\n",
245 evts->num_used, evts->num_alloc );
248 /* destroys the renderer events of a console */
249 static void console_input_events_destroy( struct object *obj )
251 struct console_input_events *evts = (struct console_input_events *)obj;
252 assert( obj->ops == &console_input_events_ops );
253 free( evts->events );
256 /* the renderer events list is signaled when it's not empty */
257 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
259 struct console_input_events *evts = (struct console_input_events *)obj;
260 assert( obj->ops == &console_input_events_ops );
261 return (evts->num_used != 0);
264 /* add an event to the console's renderer events list */
265 static void console_input_events_append( struct console_input* console,
266 struct console_renderer_event* evt)
268 struct console_input_events* evts;
269 int collapsed = FALSE;
271 if (!(evts = console->evt)) return;
272 /* to be done even when evt has been generated by the renderer ? */
274 /* try to collapse evt into current queue's events */
275 if (evts->num_used)
277 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
279 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
280 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
282 /* if two update events overlap, collapse them into a single one */
283 if (last->u.update.bottom + 1 >= evt->u.update.top &&
284 evt->u.update.bottom + 1 >= last->u.update.top)
286 last->u.update.top = min(last->u.update.top, evt->u.update.top);
287 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
288 collapsed = TRUE;
292 if (!collapsed)
294 if (evts->num_used == evts->num_alloc)
296 evts->num_alloc += 16;
297 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
298 assert(evts->events);
300 evts->events[evts->num_used++] = *evt;
302 wake_up( &evts->obj, 0 );
305 /* retrieves events from the console's renderer events list */
306 static void console_input_events_get( struct console_input_events* evts )
308 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
310 if (num > evts->num_used) num = evts->num_used;
311 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
312 if (num < evts->num_used)
314 memmove( &evts->events[0], &evts->events[num],
315 (evts->num_used - num) * sizeof(evts->events[0]) );
317 evts->num_used -= num;
320 static struct console_input_events *create_console_input_events(void)
322 struct console_input_events* evt;
324 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
325 evt->num_alloc = evt->num_used = 0;
326 evt->events = NULL;
327 return evt;
330 static struct object *create_console_input( struct thread* renderer, int fd )
332 struct console_input *console_input;
334 if (!(console_input = alloc_object( &console_input_ops )))
336 if (fd != -1) close( fd );
337 return NULL;
339 console_input->renderer = renderer;
340 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
341 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
342 ENABLE_EXTENDED_FLAGS;
343 console_input->num_proc = 0;
344 console_input->active = NULL;
345 console_input->recnum = 0;
346 console_input->records = NULL;
347 console_input->evt = renderer ? create_console_input_events() : NULL;
348 console_input->title = NULL;
349 console_input->title_len = 0;
350 console_input->history_size = 50;
351 console_input->history = calloc( console_input->history_size, sizeof(*console_input->history) );
352 console_input->history_index = 0;
353 console_input->history_mode = 0;
354 console_input->edition_mode = 0;
355 console_input->input_cp = 0;
356 console_input->output_cp = 0;
357 console_input->win = 0;
358 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
359 console_input->fd = NULL;
361 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
363 if (fd != -1) close( fd );
364 console_input->history_size = 0;
365 release_object( console_input );
366 return NULL;
368 if (fd != -1) /* bare console */
370 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
371 FILE_SYNCHRONOUS_IO_NONALERT )))
373 release_object( console_input );
374 return NULL;
376 allow_fd_caching( console_input->fd );
379 return &console_input->obj;
382 static void generate_sb_initial_events( struct console_input *console_input )
384 struct screen_buffer *screen_buffer = console_input->active;
385 struct console_renderer_event evt;
387 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
388 memset(&evt.u, 0, sizeof(evt.u));
389 console_input_events_append( console_input, &evt );
391 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
392 evt.u.resize.width = screen_buffer->width;
393 evt.u.resize.height = screen_buffer->height;
394 console_input_events_append( console_input, &evt );
396 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
397 evt.u.display.left = screen_buffer->win.left;
398 evt.u.display.top = screen_buffer->win.top;
399 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
400 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
401 console_input_events_append( console_input, &evt );
403 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
404 evt.u.update.top = 0;
405 evt.u.update.bottom = screen_buffer->height - 1;
406 console_input_events_append( console_input, &evt );
408 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
409 evt.u.cursor_geom.size = screen_buffer->cursor_size;
410 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
411 console_input_events_append( console_input, &evt );
413 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
414 evt.u.cursor_pos.x = screen_buffer->cursor_x;
415 evt.u.cursor_pos.y = screen_buffer->cursor_y;
416 console_input_events_append( console_input, &evt );
419 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
421 struct screen_buffer *screen_buffer;
422 int i;
424 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
426 if (fd != -1) close( fd );
427 return NULL;
429 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
430 screen_buffer->input = console_input;
431 screen_buffer->cursor_size = 100;
432 screen_buffer->cursor_visible = 1;
433 screen_buffer->width = 80;
434 screen_buffer->height = 150;
435 screen_buffer->max_width = 80;
436 screen_buffer->max_height = 25;
437 screen_buffer->cursor_x = 0;
438 screen_buffer->cursor_y = 0;
439 screen_buffer->attr = 0x0F;
440 screen_buffer->popup_attr = 0xF5;
441 screen_buffer->win.left = 0;
442 screen_buffer->win.right = screen_buffer->max_width - 1;
443 screen_buffer->win.top = 0;
444 screen_buffer->win.bottom = screen_buffer->max_height - 1;
445 screen_buffer->data = NULL;
446 screen_buffer->font.width = 0;
447 screen_buffer->font.height = 0;
448 screen_buffer->font.weight = FW_NORMAL;
449 screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
450 screen_buffer->font.face_name = NULL;
451 screen_buffer->font.face_len = 0;
452 memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
453 list_add_head( &screen_buffer_list, &screen_buffer->entry );
455 if (fd == -1)
456 screen_buffer->fd = NULL;
457 else
459 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
460 FILE_SYNCHRONOUS_IO_NONALERT )))
462 release_object( screen_buffer );
463 return NULL;
465 allow_fd_caching(screen_buffer->fd);
468 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
469 sizeof(*screen_buffer->data) )))
471 release_object( screen_buffer );
472 return NULL;
474 /* clear the first row */
475 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
476 /* and copy it to all other rows */
477 for (i = 1; i < screen_buffer->height; i++)
478 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
479 screen_buffer->width * sizeof(char_info_t) );
481 if (!console_input->active)
483 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
484 generate_sb_initial_events( console_input );
486 return screen_buffer;
489 /* free the console for this process */
490 int free_console( struct process *process )
492 struct console_input* console = process->console;
494 if (!console) return 0;
496 process->console = NULL;
497 if (--console->num_proc == 0 && console->renderer)
499 /* all processes have terminated... tell the renderer to terminate too */
500 struct console_renderer_event evt;
501 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
502 memset(&evt.u, 0, sizeof(evt.u));
503 console_input_events_append( console, &evt );
505 release_object( console );
507 return 1;
510 /* let process inherit the console from parent... this handle two cases :
511 * 1/ generic console inheritance
512 * 2/ parent is a renderer which launches process, and process should attach to the console
513 * rendered by parent
515 void inherit_console( struct thread *parent_thread, struct process *parent, struct process *process,
516 obj_handle_t hconin )
518 int done = 0;
520 /* if parent is a renderer, then attach current process to its console
521 * a bit hacky....
523 if (hconin && parent_thread)
525 struct console_input *console;
527 /* FIXME: should we check some access rights ? */
528 if ((console = (struct console_input *)get_handle_obj( parent, hconin,
529 0, &console_input_ops )))
531 if (console->renderer == parent_thread)
533 process->console = (struct console_input *)grab_object( console );
534 process->console->num_proc++;
535 done = 1;
537 release_object( console );
539 else clear_error(); /* ignore error */
541 /* otherwise, if parent has a console, attach child to this console */
542 if (!done && parent->console)
544 process->console = (struct console_input *)grab_object( parent->console );
545 process->console->num_proc++;
549 struct thread *console_get_renderer( struct console_input *console )
551 return console->renderer;
554 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
556 struct console_input* console = NULL;
558 if (handle)
559 console = (struct console_input *)get_handle_obj( current->process, handle,
560 access, &console_input_ops );
561 else if (current->process->console)
563 console = (struct console_input *)grab_object( current->process->console );
566 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
567 return console;
570 struct console_signal_info
572 struct console_input *console;
573 process_id_t group;
574 int signal;
577 static int propagate_console_signal_cb(struct process *process, void *user)
579 struct console_signal_info* csi = (struct console_signal_info*)user;
581 if (process->console == csi->console && process->running_threads &&
582 (!csi->group || process->group_id == csi->group))
584 /* find a suitable thread to signal */
585 struct thread *thread;
586 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
588 if (send_thread_signal( thread, csi->signal )) break;
591 return FALSE;
594 static void propagate_console_signal( struct console_input *console,
595 int sig, process_id_t group_id )
597 struct console_signal_info csi;
599 if (!console)
601 set_error( STATUS_INVALID_PARAMETER );
602 return;
604 /* FIXME: should support the other events (like CTRL_BREAK) */
605 if (sig != CTRL_C_EVENT)
607 set_error( STATUS_NOT_IMPLEMENTED );
608 return;
610 csi.console = console;
611 csi.signal = SIGINT;
612 csi.group = group_id;
614 enum_processes(propagate_console_signal_cb, &csi);
617 static int get_console_mode( obj_handle_t handle )
619 struct object *obj;
620 int ret = 0;
622 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
624 if (obj->ops == &console_input_ops)
626 ret = ((struct console_input *)obj)->mode;
628 else if (obj->ops == &screen_buffer_ops)
630 ret = ((struct screen_buffer *)obj)->mode;
632 else
633 set_error( STATUS_OBJECT_TYPE_MISMATCH );
634 release_object( obj );
636 return ret;
639 /* changes the mode of either a console input or a screen buffer */
640 static int set_console_mode( obj_handle_t handle, int mode )
642 struct object *obj;
643 int ret = 0;
645 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
646 return 0;
647 if (obj->ops == &console_input_ops)
649 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
650 ((struct console_input *)obj)->mode = mode;
651 ret = 1;
653 else if (obj->ops == &screen_buffer_ops)
655 ((struct screen_buffer *)obj)->mode = mode;
656 ret = 1;
658 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
659 release_object( obj );
660 return ret;
663 /* add input events to a console input queue */
664 static int write_console_input( struct console_input* console, int count,
665 const INPUT_RECORD *records )
667 INPUT_RECORD *new_rec;
669 if (!count) return 0;
670 if (!(new_rec = realloc( console->records,
671 (console->recnum + count) * sizeof(INPUT_RECORD) )))
673 set_error( STATUS_NO_MEMORY );
674 return -1;
676 console->records = new_rec;
677 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
679 if (console->mode & ENABLE_PROCESSED_INPUT)
681 int i = 0;
682 while (i < count)
684 if (records[i].EventType == KEY_EVENT &&
685 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
686 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
688 if (i != count - 1)
689 memcpy( &console->records[console->recnum + i],
690 &console->records[console->recnum + i + 1],
691 (count - i - 1) * sizeof(INPUT_RECORD) );
692 count--;
693 if (records[i].Event.KeyEvent.bKeyDown)
695 /* send SIGINT to all processes attached to this console */
696 propagate_console_signal( console, CTRL_C_EVENT, 0 );
699 else i++;
702 if (!console->recnum && count) set_event( console->event );
703 console->recnum += count;
704 return count;
707 /* retrieve a pointer to the console input records */
708 static int read_console_input( obj_handle_t handle, int count, int flush )
710 struct console_input *console;
712 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
713 FILE_READ_DATA, &console_input_ops )))
714 return -1;
716 if (!count)
718 /* special case: do not retrieve anything, but return
719 * the total number of records available */
720 count = console->recnum;
722 else
724 if (count > console->recnum) count = console->recnum;
725 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
727 if (flush)
729 int i;
730 for (i = count; i < console->recnum; i++)
731 console->records[i-count] = console->records[i];
732 if ((console->recnum -= count) > 0)
734 INPUT_RECORD *new_rec = realloc( console->records,
735 console->recnum * sizeof(INPUT_RECORD) );
736 if (new_rec) console->records = new_rec;
738 else
740 free( console->records );
741 console->records = NULL;
742 reset_event( console->event );
745 release_object( console );
746 return count;
749 /* set misc console input information */
750 static int set_console_input_info( const struct set_console_input_info_request *req,
751 const WCHAR *title, data_size_t len )
753 struct console_input *console;
754 struct console_renderer_event evt;
756 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
757 if (console_input_is_bare(console) &&
758 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
759 SET_CONSOLE_INPUT_INFO_WIN)))
761 set_error( STATUS_UNSUCCESSFUL );
762 goto error;
765 memset(&evt.u, 0, sizeof(evt.u));
766 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
768 struct screen_buffer *screen_buffer;
770 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
771 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
772 if (!screen_buffer || screen_buffer->input != console)
774 set_error( STATUS_INVALID_HANDLE );
775 if (screen_buffer) release_object( screen_buffer );
776 goto error;
779 if (screen_buffer != console->active)
781 if (console->active) release_object( console->active );
782 console->active = screen_buffer;
783 generate_sb_initial_events( console );
785 else
786 release_object( screen_buffer );
788 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
790 WCHAR *new_title = NULL;
792 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
793 if (len && !(new_title = memdup( title, len ))) goto error;
794 free( console->title );
795 console->title = new_title;
796 console->title_len = len;
797 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
798 console_input_events_append( console, &evt );
800 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
802 console->history_mode = req->history_mode;
804 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
805 console->history_size != req->history_size)
807 struct history_line **mem = NULL;
808 int i, delta;
810 if (req->history_size)
812 if (!(mem = mem_alloc( req->history_size * sizeof(*mem) ))) goto error;
813 memset( mem, 0, req->history_size * sizeof(*mem) );
816 delta = (console->history_index > req->history_size) ?
817 (console->history_index - req->history_size) : 0;
819 for (i = delta; i < console->history_index; i++)
821 mem[i - delta] = console->history[i];
822 console->history[i] = NULL;
824 console->history_index -= delta;
826 for (i = 0; i < console->history_size; i++)
827 free( console->history[i] );
828 free( console->history );
829 console->history = mem;
830 console->history_size = req->history_size;
832 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
834 console->edition_mode = req->edition_mode;
836 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
838 console->input_cp = req->input_cp;
840 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
842 console->output_cp = req->output_cp;
844 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
846 console->win = req->win;
848 release_object( console );
849 return 1;
850 error:
851 if (console) release_object( console );
852 return 0;
855 /* resize a screen buffer */
856 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
857 int new_width, int new_height )
859 int i, old_width, old_height, copy_width, copy_height;
860 char_info_t *new_data;
862 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
864 set_error( STATUS_NO_MEMORY );
865 return 0;
867 old_width = screen_buffer->width;
868 old_height = screen_buffer->height;
869 copy_width = min( old_width, new_width );
870 copy_height = min( old_height, new_height );
872 /* copy all the rows */
873 for (i = 0; i < copy_height; i++)
875 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
876 copy_width * sizeof(char_info_t) );
879 /* clear the end of each row */
880 if (new_width > old_width)
882 /* fill first row */
883 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
884 /* and blast it to the other rows */
885 for (i = 1; i < copy_height; i++)
886 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
887 (new_width - old_width) * sizeof(char_info_t) );
890 /* clear remaining rows */
891 if (new_height > old_height)
893 /* fill first row */
894 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
895 /* and blast it to the other rows */
896 for (i = old_height+1; i < new_height; i++)
897 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
898 new_width * sizeof(char_info_t) );
900 free( screen_buffer->data );
901 screen_buffer->data = new_data;
902 screen_buffer->width = new_width;
903 screen_buffer->height = new_height;
904 return 1;
907 /* set misc screen buffer information */
908 static int set_console_output_info( struct screen_buffer *screen_buffer,
909 const struct set_console_output_info_request *req )
911 struct console_renderer_event evt;
912 data_size_t font_name_len, offset;
913 WCHAR *font_name;
915 memset(&evt.u, 0, sizeof(evt.u));
916 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
918 if (req->cursor_size < 1 || req->cursor_size > 100)
920 set_error( STATUS_INVALID_PARAMETER );
921 return 0;
923 if (screen_buffer->cursor_size != req->cursor_size ||
924 screen_buffer->cursor_visible != req->cursor_visible)
926 screen_buffer->cursor_size = req->cursor_size;
927 screen_buffer->cursor_visible = req->cursor_visible;
928 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
929 evt.u.cursor_geom.size = req->cursor_size;
930 evt.u.cursor_geom.visible = req->cursor_visible;
931 console_input_events_append( screen_buffer->input, &evt );
934 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
936 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
937 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
939 set_error( STATUS_INVALID_PARAMETER );
940 return 0;
942 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
944 screen_buffer->cursor_x = req->cursor_x;
945 screen_buffer->cursor_y = req->cursor_y;
946 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
947 evt.u.cursor_pos.x = req->cursor_x;
948 evt.u.cursor_pos.y = req->cursor_y;
949 console_input_events_append( screen_buffer->input, &evt );
952 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
954 unsigned cc;
956 /* new screen-buffer cannot be smaller than actual window */
957 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
958 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
960 set_error( STATUS_INVALID_PARAMETER );
961 return 0;
963 /* FIXME: there are also some basic minimum and max size to deal with */
964 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
966 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
967 evt.u.resize.width = req->width;
968 evt.u.resize.height = req->height;
969 console_input_events_append( screen_buffer->input, &evt );
971 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
972 evt.u.update.top = 0;
973 evt.u.update.bottom = screen_buffer->height - 1;
974 console_input_events_append( screen_buffer->input, &evt );
976 /* scroll window to display sb */
977 if (screen_buffer->win.right >= req->width)
979 screen_buffer->win.right -= screen_buffer->win.left;
980 screen_buffer->win.left = 0;
982 if (screen_buffer->win.bottom >= req->height)
984 screen_buffer->win.bottom -= screen_buffer->win.top;
985 screen_buffer->win.top = 0;
987 /* reset cursor if needed (normally, if cursor was outside of new sb, the
988 * window has been shifted so that the new position of the cursor will be
989 * visible */
990 cc = 0;
991 if (screen_buffer->cursor_x >= req->width)
993 screen_buffer->cursor_x = req->width - 1;
994 cc++;
996 if (screen_buffer->cursor_y >= req->height)
998 screen_buffer->cursor_y = req->height - 1;
999 cc++;
1001 if (cc)
1003 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
1004 evt.u.cursor_pos.x = req->cursor_x;
1005 evt.u.cursor_pos.y = req->cursor_y;
1006 console_input_events_append( screen_buffer->input, &evt );
1009 if (screen_buffer == screen_buffer->input->active &&
1010 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
1012 INPUT_RECORD ir;
1013 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
1014 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
1015 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
1016 write_console_input( screen_buffer->input, 1, &ir );
1019 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
1021 screen_buffer->attr = req->attr;
1023 if (req->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
1025 screen_buffer->popup_attr = req->popup_attr;
1027 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1029 if (req->win_left < 0 || req->win_left > req->win_right ||
1030 req->win_right >= screen_buffer->width ||
1031 req->win_top < 0 || req->win_top > req->win_bottom ||
1032 req->win_bottom >= screen_buffer->height)
1034 set_error( STATUS_INVALID_PARAMETER );
1035 return 0;
1037 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1038 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1040 screen_buffer->win.left = req->win_left;
1041 screen_buffer->win.top = req->win_top;
1042 screen_buffer->win.right = req->win_right;
1043 screen_buffer->win.bottom = req->win_bottom;
1044 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1045 evt.u.display.left = req->win_left;
1046 evt.u.display.top = req->win_top;
1047 evt.u.display.width = req->win_right - req->win_left + 1;
1048 evt.u.display.height = req->win_bottom - req->win_top + 1;
1049 console_input_events_append( screen_buffer->input, &evt );
1052 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1054 screen_buffer->max_width = req->max_width;
1055 screen_buffer->max_height = req->max_height;
1057 if (req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
1059 memcpy( screen_buffer->color_map, get_req_data(), min( get_req_data_size(), sizeof(screen_buffer->color_map) ));
1061 if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
1063 screen_buffer->font.width = req->font_width;
1064 screen_buffer->font.height = req->font_height;
1065 screen_buffer->font.weight = req->font_weight;
1066 screen_buffer->font.pitch_family = req->font_pitch_family;
1067 offset = req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE ? sizeof(screen_buffer->color_map) : 0;
1068 if (get_req_data_size() > offset)
1070 font_name_len = (get_req_data_size() - offset) / sizeof(WCHAR) * sizeof(WCHAR);
1071 font_name = mem_alloc( font_name_len );
1072 if (font_name)
1074 memcpy( font_name, (char *)get_req_data() + offset, font_name_len );
1075 free( screen_buffer->font.face_name );
1076 screen_buffer->font.face_name = font_name;
1077 screen_buffer->font.face_len = font_name_len;
1082 return 1;
1085 /* appends a new line to history (history is a fixed size array) */
1086 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1088 struct history_line *ptr;
1090 if (!console || !console->history_size)
1092 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1093 return;
1096 len = (len / sizeof(WCHAR)) * sizeof(WCHAR);
1097 if (console->history_mode && console->history_index &&
1098 console->history[console->history_index - 1]->len == len &&
1099 !memcmp( console->history[console->history_index - 1]->text, buf, len ))
1101 /* don't duplicate entry */
1102 set_error( STATUS_ALIAS_EXISTS );
1103 return;
1105 if (!(ptr = mem_alloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
1106 ptr->len = len;
1107 memcpy( ptr->text, buf, len );
1109 if (console->history_index < console->history_size)
1111 console->history[console->history_index++] = ptr;
1113 else
1115 free( console->history[0]) ;
1116 memmove( &console->history[0], &console->history[1],
1117 (console->history_size - 1) * sizeof(*console->history) );
1118 console->history[console->history_size - 1] = ptr;
1122 /* returns a line from the cache */
1123 static data_size_t console_input_get_hist( struct console_input *console, int index )
1125 data_size_t ret = 0;
1127 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1128 else
1130 ret = console->history[index]->len;
1131 set_reply_data( console->history[index]->text, min( ret, get_reply_max_size() ));
1133 return ret;
1136 /* dumb dump */
1137 static void console_input_dump( struct object *obj, int verbose )
1139 struct console_input *console = (struct console_input *)obj;
1140 assert( obj->ops == &console_input_ops );
1141 fprintf( stderr, "Console input active=%p evt=%p\n",
1142 console->active, console->evt );
1145 static void console_input_destroy( struct object *obj )
1147 struct console_input* console_in = (struct console_input *)obj;
1148 struct screen_buffer* curr;
1149 int i;
1151 assert( obj->ops == &console_input_ops );
1152 free( console_in->title );
1153 free( console_in->records );
1155 if (console_in->active) release_object( console_in->active );
1156 console_in->active = NULL;
1158 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1160 if (curr->input == console_in) curr->input = NULL;
1163 if (console_in->evt)
1165 release_object( console_in->evt );
1166 console_in->evt = NULL;
1168 if (console_in->event)
1169 release_object( console_in->event );
1170 if (console_in->fd)
1171 release_object( console_in->fd );
1173 for (i = 0; i < console_in->history_size; i++)
1174 free( console_in->history[i] );
1175 free( console_in->history );
1178 static void screen_buffer_dump( struct object *obj, int verbose )
1180 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1181 assert( obj->ops == &screen_buffer_ops );
1183 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1186 static void screen_buffer_destroy( struct object *obj )
1188 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1190 assert( obj->ops == &screen_buffer_ops );
1192 list_remove( &screen_buffer->entry );
1194 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1196 struct screen_buffer *sb;
1198 screen_buffer->input->active = NULL;
1199 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1201 if (sb->input == screen_buffer->input)
1203 sb->input->active = sb;
1204 break;
1208 if (screen_buffer->fd) release_object( screen_buffer->fd );
1209 free( screen_buffer->data );
1210 free( screen_buffer->font.face_name );
1213 static struct fd *screen_buffer_get_fd( struct object *obj )
1215 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1216 assert( obj->ops == &screen_buffer_ops );
1217 if (screen_buffer->fd)
1218 return (struct fd*)grab_object( screen_buffer->fd );
1219 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1220 return NULL;
1223 /* write data into a screen buffer */
1224 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1225 const void* data, enum char_info_mode mode,
1226 int x, int y, int wrap )
1228 unsigned int i;
1229 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1231 if (y >= screen_buffer->height) return 0;
1233 if (wrap)
1234 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1235 else
1236 end = screen_buffer->data + (y+1) * screen_buffer->width;
1238 switch(mode)
1240 case CHAR_INFO_MODE_TEXT:
1242 const WCHAR *ptr = data;
1243 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1245 break;
1246 case CHAR_INFO_MODE_ATTR:
1248 const unsigned short *ptr = data;
1249 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1251 break;
1252 case CHAR_INFO_MODE_TEXTATTR:
1254 const char_info_t *ptr = data;
1255 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1257 break;
1258 case CHAR_INFO_MODE_TEXTSTDATTR:
1260 const WCHAR *ptr = data;
1261 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1263 dest->ch = ptr[i];
1264 dest->attr = screen_buffer->attr;
1267 break;
1268 default:
1269 set_error( STATUS_INVALID_PARAMETER );
1270 return 0;
1273 if (i && screen_buffer == screen_buffer->input->active)
1275 struct console_renderer_event evt;
1276 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1277 memset(&evt.u, 0, sizeof(evt.u));
1278 evt.u.update.top = y + x / screen_buffer->width;
1279 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1280 console_input_events_append( screen_buffer->input, &evt );
1282 return i;
1285 /* fill a screen buffer with uniform data */
1286 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1287 enum char_info_mode mode, int x, int y, int count, int wrap )
1289 int i;
1290 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1292 if (y >= screen_buffer->height) return 0;
1294 if (wrap)
1295 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1296 else
1297 end = screen_buffer->data + (y+1) * screen_buffer->width;
1299 if (count > end - dest) count = end - dest;
1301 switch(mode)
1303 case CHAR_INFO_MODE_TEXT:
1304 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1305 break;
1306 case CHAR_INFO_MODE_ATTR:
1307 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1308 break;
1309 case CHAR_INFO_MODE_TEXTATTR:
1310 for (i = 0; i < count; i++) dest[i] = data;
1311 break;
1312 case CHAR_INFO_MODE_TEXTSTDATTR:
1313 for (i = 0; i < count; i++)
1315 dest[i].ch = data.ch;
1316 dest[i].attr = screen_buffer->attr;
1318 break;
1319 default:
1320 set_error( STATUS_INVALID_PARAMETER );
1321 return 0;
1324 if (count && screen_buffer == screen_buffer->input->active)
1326 struct console_renderer_event evt;
1327 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1328 memset(&evt.u, 0, sizeof(evt.u));
1329 evt.u.update.top = y;
1330 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1331 console_input_events_append( screen_buffer->input, &evt );
1333 return i;
1336 /* read data from a screen buffer */
1337 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1338 enum char_info_mode mode, int wrap )
1340 int i;
1341 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1343 if (y >= screen_buffer->height) return;
1345 if (wrap)
1346 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1347 else
1348 end = screen_buffer->data + (y+1) * screen_buffer->width;
1350 switch(mode)
1352 case CHAR_INFO_MODE_TEXT:
1354 WCHAR *data;
1355 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1356 if ((data = set_reply_data_size( count * sizeof(*data) )))
1358 for (i = 0; i < count; i++) data[i] = src[i].ch;
1361 break;
1362 case CHAR_INFO_MODE_ATTR:
1364 unsigned short *data;
1365 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1366 if ((data = set_reply_data_size( count * sizeof(*data) )))
1368 for (i = 0; i < count; i++) data[i] = src[i].attr;
1371 break;
1372 case CHAR_INFO_MODE_TEXTATTR:
1374 char_info_t *data;
1375 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1376 if ((data = set_reply_data_size( count * sizeof(*data) )))
1378 for (i = 0; i < count; i++) data[i] = src[i];
1381 break;
1382 default:
1383 set_error( STATUS_INVALID_PARAMETER );
1384 break;
1388 /* scroll parts of a screen buffer */
1389 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1390 int w, int h )
1392 int j;
1393 char_info_t *psrc, *pdst;
1394 struct console_renderer_event evt;
1396 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1397 xsrc + w > screen_buffer->width ||
1398 xdst + w > screen_buffer->width ||
1399 ysrc + h > screen_buffer->height ||
1400 ydst + h > screen_buffer->height ||
1401 w == 0 || h == 0)
1403 set_error( STATUS_INVALID_PARAMETER );
1404 return;
1407 if (ysrc < ydst)
1409 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1410 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1412 for (j = h; j > 0; j--)
1414 memcpy(pdst, psrc, w * sizeof(*pdst) );
1415 pdst -= screen_buffer->width;
1416 psrc -= screen_buffer->width;
1419 else
1421 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1422 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1424 for (j = 0; j < h; j++)
1426 /* we use memmove here because when psrc and pdst are the same,
1427 * copies are done on the same row, so the dst and src blocks
1428 * can overlap */
1429 memmove( pdst, psrc, w * sizeof(*pdst) );
1430 pdst += screen_buffer->width;
1431 psrc += screen_buffer->width;
1435 /* FIXME: this could be enhanced, by signalling scroll */
1436 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1437 memset(&evt.u, 0, sizeof(evt.u));
1438 evt.u.update.top = min(ysrc, ydst);
1439 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1440 console_input_events_append( screen_buffer->input, &evt );
1443 /* allocate a console for the renderer */
1444 DECL_HANDLER(alloc_console)
1446 obj_handle_t in = 0;
1447 obj_handle_t evt = 0;
1448 struct process *process;
1449 struct thread *renderer;
1450 struct console_input *console;
1451 int fd;
1452 int attach = 0;
1454 if (req->input_fd != -1)
1456 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1458 set_error( STATUS_INVALID_PARAMETER );
1459 return;
1462 else fd = -1;
1464 switch (req->pid)
1466 case 0:
1467 /* renderer is current, console to be attached to parent process */
1468 renderer = current;
1469 if (!(process = get_process_from_id( current->process->parent_id )))
1471 if (fd != -1) close( fd );
1472 set_error( STATUS_ACCESS_DENIED );
1473 return;
1475 attach = 1;
1476 break;
1477 case 0xffffffff:
1478 /* no renderer, console to be attached to current process */
1479 renderer = NULL;
1480 process = current->process;
1481 grab_object( process );
1482 attach = 1;
1483 break;
1484 default:
1485 /* renderer is current, console to be attached to req->pid */
1486 renderer = current;
1487 if (!(process = get_process_from_id( req->pid )))
1489 if (fd != -1) close( fd );
1490 return;
1494 if (attach && process->console)
1496 if (fd != -1) close( fd );
1497 set_error( STATUS_ACCESS_DENIED );
1498 goto the_end;
1501 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1503 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1505 if (!console->evt ||
1506 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1508 if (attach)
1510 process->console = (struct console_input*)grab_object( console );
1511 console->num_proc++;
1513 reply->handle_in = in;
1514 reply->event = evt;
1515 release_object( console );
1516 goto the_end;
1518 close_handle( current->process, in );
1520 release_object( console );
1522 the_end:
1523 release_object( process );
1526 /* free the console of the current process */
1527 DECL_HANDLER(free_console)
1529 free_console( current->process );
1532 /* let the renderer peek the events it's waiting on */
1533 DECL_HANDLER(get_console_renderer_events)
1535 struct console_input_events *evt;
1537 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1538 FILE_READ_PROPERTIES, &console_input_events_ops );
1539 if (!evt) return;
1540 console_input_events_get( evt );
1541 release_object( evt );
1544 /* open a handle to the process console */
1545 DECL_HANDLER(open_console)
1547 struct object *obj = NULL;
1549 reply->handle = 0;
1550 if (!req->from)
1552 if (current->process->console)
1553 obj = grab_object( (struct object*)current->process->console );
1555 else if (req->from == (obj_handle_t)1)
1557 if (current->process->console && current->process->console->active)
1558 obj = grab_object( (struct object*)current->process->console->active );
1560 else if ((obj = get_handle_obj( current->process, req->from,
1561 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1563 struct console_input *console = (struct console_input *)obj;
1564 obj = (console->active) ? grab_object( console->active ) : NULL;
1565 release_object( console );
1568 /* FIXME: req->share is not used (as in screen buffer creation) */
1569 if (obj)
1571 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1572 release_object( obj );
1574 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1577 /* attach to a other process's console */
1578 DECL_HANDLER(attach_console)
1580 struct process *process;
1582 if (current->process->console)
1584 set_error( STATUS_ACCESS_DENIED );
1585 return;
1588 process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
1589 ? current->process->parent_id : req->pid );
1590 if (!process) return;
1592 if (process->console && process->console->active)
1594 reply->std_in = alloc_handle( current->process, process->console, GENERIC_READ, 0 );
1595 if (!reply->std_in) goto error;
1597 reply->std_out = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
1598 if (!reply->std_out) goto error;
1600 reply->std_err = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
1601 if (!reply->std_err) goto error;
1603 current->process->console = (struct console_input *)grab_object( process->console );
1604 current->process->console->num_proc++;
1606 else
1608 set_error( STATUS_INVALID_HANDLE );
1611 release_object( process );
1612 return;
1614 error:
1615 if (reply->std_in) close_handle( current->process, reply->std_in );
1616 if (reply->std_out) close_handle( current->process, reply->std_out );
1617 release_object( process );
1620 /* set info about a console input */
1621 DECL_HANDLER(set_console_input_info)
1623 set_console_input_info( req, get_req_data(), get_req_data_size() );
1626 /* get info about a console (output only) */
1627 DECL_HANDLER(get_console_input_info)
1629 struct console_input *console;
1631 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1632 if (console->title) set_reply_data( console->title, min( console->title_len, get_reply_max_size() ));
1633 reply->history_mode = console->history_mode;
1634 reply->history_size = console->history_size;
1635 reply->history_index = console->history_index;
1636 reply->edition_mode = console->edition_mode;
1637 reply->input_cp = console->input_cp;
1638 reply->output_cp = console->output_cp;
1639 reply->win = console->win;
1641 release_object( console );
1644 /* get a console mode (input or output) */
1645 DECL_HANDLER(get_console_mode)
1647 reply->mode = get_console_mode( req->handle );
1650 /* set a console mode (input or output) */
1651 DECL_HANDLER(set_console_mode)
1653 set_console_mode( req->handle, req->mode );
1656 /* add input records to a console input queue */
1657 DECL_HANDLER(write_console_input)
1659 struct console_input *console;
1661 reply->written = 0;
1662 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1663 FILE_WRITE_PROPERTIES, &console_input_ops )))
1664 return;
1665 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1666 get_req_data() );
1667 release_object( console );
1670 /* fetch input records from a console input queue */
1671 DECL_HANDLER(read_console_input)
1673 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1674 reply->read = read_console_input( req->handle, count, req->flush );
1677 /* appends a string to console's history */
1678 DECL_HANDLER(append_console_input_history)
1680 struct console_input *console;
1682 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1683 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1684 release_object( console );
1687 /* appends a string to console's history */
1688 DECL_HANDLER(get_console_input_history)
1690 struct console_input *console;
1692 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1693 reply->total = console_input_get_hist( console, req->index );
1694 release_object( console );
1697 /* creates a screen buffer */
1698 DECL_HANDLER(create_console_output)
1700 struct console_input* console;
1701 struct screen_buffer* screen_buffer;
1702 int fd;
1704 if (req->fd != -1)
1706 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1708 set_error( STATUS_INVALID_HANDLE );
1709 return;
1712 else fd = -1;
1713 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1715 if (fd != -1) close( fd );
1716 return;
1718 if (console_input_is_bare( console ) ^ (fd != -1))
1720 if (fd != -1) close( fd );
1721 release_object( console );
1722 set_error( STATUS_INVALID_HANDLE );
1723 return;
1726 screen_buffer = create_console_output( console, fd );
1727 if (screen_buffer)
1729 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1730 * see file.c on how this could be done */
1731 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1732 release_object( screen_buffer );
1734 release_object( console );
1737 /* set info about a console screen buffer */
1738 DECL_HANDLER(set_console_output_info)
1740 struct screen_buffer *screen_buffer;
1742 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1743 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1745 set_console_output_info( screen_buffer, req );
1746 release_object( screen_buffer );
1750 /* get info about a console screen buffer */
1751 DECL_HANDLER(get_console_output_info)
1753 struct screen_buffer *screen_buffer;
1754 void *data;
1755 data_size_t total;
1757 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1758 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1760 reply->cursor_size = screen_buffer->cursor_size;
1761 reply->cursor_visible = screen_buffer->cursor_visible;
1762 reply->cursor_x = screen_buffer->cursor_x;
1763 reply->cursor_y = screen_buffer->cursor_y;
1764 reply->width = screen_buffer->width;
1765 reply->height = screen_buffer->height;
1766 reply->attr = screen_buffer->attr;
1767 reply->popup_attr = screen_buffer->popup_attr;
1768 reply->win_left = screen_buffer->win.left;
1769 reply->win_top = screen_buffer->win.top;
1770 reply->win_right = screen_buffer->win.right;
1771 reply->win_bottom = screen_buffer->win.bottom;
1772 reply->max_width = screen_buffer->max_width;
1773 reply->max_height = screen_buffer->max_height;
1774 reply->font_width = screen_buffer->font.width;
1775 reply->font_height = screen_buffer->font.height;
1776 reply->font_weight = screen_buffer->font.weight;
1777 reply->font_pitch_family = screen_buffer->font.pitch_family;
1778 total = min( sizeof(screen_buffer->color_map) + screen_buffer->font.face_len, get_reply_max_size() );
1779 if (total)
1781 data = set_reply_data_size( total );
1782 memcpy( data, screen_buffer->color_map, min( total, sizeof(screen_buffer->color_map) ));
1783 if (screen_buffer->font.face_len && total > sizeof(screen_buffer->color_map))
1785 memcpy( (char *)data + sizeof(screen_buffer->color_map), screen_buffer->font.face_name,
1786 min( total - sizeof(screen_buffer->color_map), screen_buffer->font.face_len ));
1789 release_object( screen_buffer );
1793 /* read data (chars & attrs) from a screen buffer */
1794 DECL_HANDLER(read_console_output)
1796 struct screen_buffer *screen_buffer;
1798 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1799 FILE_READ_DATA, &screen_buffer_ops )))
1801 if (console_input_is_bare( screen_buffer->input ))
1803 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1804 release_object( screen_buffer );
1805 return;
1807 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1808 reply->width = screen_buffer->width;
1809 reply->height = screen_buffer->height;
1810 release_object( screen_buffer );
1814 /* write data (char and/or attrs) to a screen buffer */
1815 DECL_HANDLER(write_console_output)
1817 struct screen_buffer *screen_buffer;
1819 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1820 FILE_WRITE_DATA, &screen_buffer_ops)))
1822 if (console_input_is_bare( screen_buffer->input ))
1824 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1825 release_object( screen_buffer );
1826 return;
1828 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1829 req->mode, req->x, req->y, req->wrap );
1830 reply->width = screen_buffer->width;
1831 reply->height = screen_buffer->height;
1832 release_object( screen_buffer );
1836 /* fill a screen buffer with constant data (chars and/or attributes) */
1837 DECL_HANDLER(fill_console_output)
1839 struct screen_buffer *screen_buffer;
1841 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1842 FILE_WRITE_DATA, &screen_buffer_ops)))
1844 if (console_input_is_bare( screen_buffer->input ))
1846 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1847 release_object( screen_buffer );
1848 return;
1850 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1851 req->x, req->y, req->count, req->wrap );
1852 release_object( screen_buffer );
1856 /* move a rect of data in a screen buffer */
1857 DECL_HANDLER(move_console_output)
1859 struct screen_buffer *screen_buffer;
1861 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1862 FILE_WRITE_DATA, &screen_buffer_ops)))
1864 if (console_input_is_bare( screen_buffer->input ))
1866 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1867 release_object( screen_buffer );
1868 return;
1870 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1871 req->w, req->h );
1872 release_object( screen_buffer );
1876 /* sends a signal to a console (process, group...) */
1877 DECL_HANDLER(send_console_signal)
1879 process_id_t group;
1881 group = req->group_id ? req->group_id : current->process->group_id;
1883 if (!group)
1884 set_error( STATUS_INVALID_PARAMETER );
1885 else
1886 propagate_console_signal( current->process->console, req->signal, group );
1889 /* get console which renderer is 'current' */
1890 static int cgwe_enum( struct process* process, void* user)
1892 if (process->console && process->console->renderer == current)
1894 *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1895 return 1;
1897 return 0;
1900 DECL_HANDLER(get_console_wait_event)
1902 struct console_input* console = NULL;
1903 struct object *obj;
1905 if (req->handle)
1907 if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
1908 if (obj->ops == &console_input_ops)
1909 console = (struct console_input *)grab_object( obj );
1910 else if (obj->ops == &screen_buffer_ops)
1911 console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
1912 else
1913 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1914 release_object( obj );
1916 else if (current->process->console)
1917 console = (struct console_input *)grab_object( current->process->console );
1918 else enum_processes(cgwe_enum, &console);
1920 if (console)
1922 reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1923 release_object( console );
1925 else set_error( STATUS_INVALID_PARAMETER );