user32: Simplify GCLP_HICON and GCLP_HICONSM boolean logic.
[wine.git] / server / console.c
blob218831b6cacc434fb30a1c0200639297828bbba3
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_open_file, /* open_file */
88 no_close_handle, /* close_handle */
89 console_input_destroy /* destroy */
92 static void console_input_events_dump( struct object *obj, int verbose );
93 static void console_input_events_destroy( struct object *obj );
94 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
96 struct console_input_events
98 struct object obj; /* object header */
99 int num_alloc; /* number of allocated events */
100 int num_used; /* number of actually used events */
101 struct console_renderer_event* events;
104 static const struct object_ops console_input_events_ops =
106 sizeof(struct console_input_events), /* size */
107 console_input_events_dump, /* dump */
108 no_get_type, /* get_type */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 console_input_events_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 default_fd_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 no_close_handle, /* close_handle */
121 console_input_events_destroy /* destroy */
124 struct screen_buffer
126 struct object obj; /* object header */
127 struct list entry; /* entry in list of all screen buffers */
128 struct console_input *input; /* associated console input */
129 int mode; /* output mode */
130 int cursor_size; /* size of cursor (percentage filled) */
131 int cursor_visible;/* cursor visibility flag */
132 int cursor_x; /* position of cursor */
133 int cursor_y; /* position of cursor */
134 int width; /* size (w-h) of the screen buffer */
135 int height;
136 int max_width; /* size (w-h) of the window given font size */
137 int max_height;
138 char_info_t *data; /* the data for each cell - a width x height matrix */
139 unsigned short attr; /* default attribute for screen buffer */
140 rectangle_t win; /* current visible window on the screen buffer *
141 * as seen in wineconsole */
142 struct fd *fd; /* for bare console, attached output fd */
145 static void screen_buffer_dump( struct object *obj, int verbose );
146 static void screen_buffer_destroy( struct object *obj );
147 static struct fd *screen_buffer_get_fd( struct object *obj );
149 static const struct object_ops screen_buffer_ops =
151 sizeof(struct screen_buffer), /* size */
152 screen_buffer_dump, /* dump */
153 no_get_type, /* get_type */
154 no_add_queue, /* add_queue */
155 NULL, /* remove_queue */
156 NULL, /* signaled */
157 NULL, /* satisfied */
158 no_signal, /* signal */
159 screen_buffer_get_fd, /* get_fd */
160 default_fd_map_access, /* map_access */
161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
163 no_lookup_name, /* lookup_name */
164 no_open_file, /* open_file */
165 no_close_handle, /* close_handle */
166 screen_buffer_destroy /* destroy */
169 static enum server_fd_type console_get_fd_type( struct fd *fd );
171 static const struct fd_ops console_fd_ops =
173 default_fd_get_poll_events, /* get_poll_events */
174 default_poll_event, /* poll_event */
175 console_get_fd_type, /* get_fd_type */
176 no_fd_read, /* read */
177 no_fd_write, /* write */
178 no_fd_flush, /* flush */
179 default_fd_ioctl, /* ioctl */
180 default_fd_queue_async, /* queue_async */
181 default_fd_reselect_async, /* reselect_async */
182 default_fd_cancel_async /* cancel_async */
185 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
187 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
189 static int console_input_is_bare( struct console_input* cin )
191 return cin->evt == NULL;
194 static struct fd *console_input_get_fd( struct object* obj )
196 struct console_input *console_input = (struct console_input*)obj;
197 assert( obj->ops == &console_input_ops );
198 if (console_input->fd)
199 return (struct fd*)grab_object( console_input->fd );
200 set_error( STATUS_OBJECT_TYPE_MISMATCH );
201 return NULL;
204 static enum server_fd_type console_get_fd_type( struct fd *fd )
206 return FD_TYPE_CHAR;
209 /* dumps the renderer events of a console */
210 static void console_input_events_dump( struct object *obj, int verbose )
212 struct console_input_events *evts = (struct console_input_events *)obj;
213 assert( obj->ops == &console_input_events_ops );
214 fprintf( stderr, "Console input events: %d/%d events\n",
215 evts->num_used, evts->num_alloc );
218 /* destroys the renderer events of a console */
219 static void console_input_events_destroy( struct object *obj )
221 struct console_input_events *evts = (struct console_input_events *)obj;
222 assert( obj->ops == &console_input_events_ops );
223 free( evts->events );
226 /* the renderer events list is signaled when it's not empty */
227 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
229 struct console_input_events *evts = (struct console_input_events *)obj;
230 assert( obj->ops == &console_input_events_ops );
231 return (evts->num_used != 0);
234 /* add an event to the console's renderer events list */
235 static void console_input_events_append( struct console_input* console,
236 struct console_renderer_event* evt)
238 struct console_input_events* evts;
239 int collapsed = FALSE;
241 if (!(evts = console->evt)) return;
242 /* to be done even when evt has been generated by the renderer ? */
244 /* try to collapse evt into current queue's events */
245 if (evts->num_used)
247 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
249 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
250 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
252 /* if two update events overlap, collapse them into a single one */
253 if (last->u.update.bottom + 1 >= evt->u.update.top &&
254 evt->u.update.bottom + 1 >= last->u.update.top)
256 last->u.update.top = min(last->u.update.top, evt->u.update.top);
257 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
258 collapsed = TRUE;
262 if (!collapsed)
264 if (evts->num_used == evts->num_alloc)
266 evts->num_alloc += 16;
267 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
268 assert(evts->events);
270 evts->events[evts->num_used++] = *evt;
272 wake_up( &evts->obj, 0 );
275 /* retrieves events from the console's renderer events list */
276 static void console_input_events_get( struct console_input_events* evts )
278 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
280 if (num > evts->num_used) num = evts->num_used;
281 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
282 if (num < evts->num_used)
284 memmove( &evts->events[0], &evts->events[num],
285 (evts->num_used - num) * sizeof(evts->events[0]) );
287 evts->num_used -= num;
290 static struct console_input_events *create_console_input_events(void)
292 struct console_input_events* evt;
294 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
295 evt->num_alloc = evt->num_used = 0;
296 evt->events = NULL;
297 return evt;
300 static struct object *create_console_input( struct thread* renderer, int fd )
302 struct console_input *console_input;
304 if (!(console_input = alloc_object( &console_input_ops )))
306 if (fd != -1) close( fd );
307 return NULL;
309 console_input->renderer = renderer;
310 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
311 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
312 ENABLE_EXTENDED_FLAGS;
313 console_input->num_proc = 0;
314 console_input->active = NULL;
315 console_input->recnum = 0;
316 console_input->records = NULL;
317 console_input->evt = renderer ? create_console_input_events() : NULL;
318 console_input->title = NULL;
319 console_input->history_size = 50;
320 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
321 console_input->history_index = 0;
322 console_input->history_mode = 0;
323 console_input->edition_mode = 0;
324 console_input->input_cp = 0;
325 console_input->output_cp = 0;
326 console_input->win = 0;
327 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
328 console_input->fd = NULL;
330 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
332 if (fd != -1) close( fd );
333 release_object( console_input );
334 return NULL;
336 if (fd != -1) /* bare console */
338 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
339 FILE_SYNCHRONOUS_IO_NONALERT )))
341 release_object( console_input );
342 return NULL;
344 allow_fd_caching( console_input->fd );
347 return &console_input->obj;
350 static void generate_sb_initial_events( struct console_input *console_input )
352 struct screen_buffer *screen_buffer = console_input->active;
353 struct console_renderer_event evt;
355 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
356 memset(&evt.u, 0, sizeof(evt.u));
357 console_input_events_append( console_input, &evt );
359 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
360 evt.u.resize.width = screen_buffer->width;
361 evt.u.resize.height = screen_buffer->height;
362 console_input_events_append( console_input, &evt );
364 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
365 evt.u.display.left = screen_buffer->win.left;
366 evt.u.display.top = screen_buffer->win.top;
367 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
368 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
369 console_input_events_append( console_input, &evt );
371 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
372 evt.u.update.top = 0;
373 evt.u.update.bottom = screen_buffer->height - 1;
374 console_input_events_append( console_input, &evt );
376 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
377 evt.u.cursor_geom.size = screen_buffer->cursor_size;
378 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
379 console_input_events_append( console_input, &evt );
381 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
382 evt.u.cursor_pos.x = screen_buffer->cursor_x;
383 evt.u.cursor_pos.y = screen_buffer->cursor_y;
384 console_input_events_append( console_input, &evt );
387 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
389 struct screen_buffer *screen_buffer;
390 int i;
392 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
394 if (fd != -1) close( fd );
395 return NULL;
397 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
398 screen_buffer->input = console_input;
399 screen_buffer->cursor_size = 100;
400 screen_buffer->cursor_visible = 1;
401 screen_buffer->width = 80;
402 screen_buffer->height = 150;
403 screen_buffer->max_width = 80;
404 screen_buffer->max_height = 25;
405 screen_buffer->cursor_x = 0;
406 screen_buffer->cursor_y = 0;
407 screen_buffer->attr = 0x0F;
408 screen_buffer->win.left = 0;
409 screen_buffer->win.right = screen_buffer->max_width - 1;
410 screen_buffer->win.top = 0;
411 screen_buffer->win.bottom = screen_buffer->max_height - 1;
412 if (fd == -1)
413 screen_buffer->fd = NULL;
414 else
416 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
417 FILE_SYNCHRONOUS_IO_NONALERT )))
419 release_object( screen_buffer );
420 return NULL;
422 allow_fd_caching(screen_buffer->fd);
425 list_add_head( &screen_buffer_list, &screen_buffer->entry );
427 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
428 sizeof(*screen_buffer->data) )))
430 release_object( screen_buffer );
431 return NULL;
433 /* clear the first row */
434 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
435 /* and copy it to all other rows */
436 for (i = 1; i < screen_buffer->height; i++)
437 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
438 screen_buffer->width * sizeof(char_info_t) );
440 if (!console_input->active)
442 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
443 generate_sb_initial_events( console_input );
445 return screen_buffer;
448 /* free the console for this process */
449 int free_console( struct process *process )
451 struct console_input* console = process->console;
453 if (!console) return 0;
455 process->console = NULL;
456 if (--console->num_proc == 0 && console->renderer)
458 /* all processes have terminated... tell the renderer to terminate too */
459 struct console_renderer_event evt;
460 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
461 memset(&evt.u, 0, sizeof(evt.u));
462 console_input_events_append( console, &evt );
464 release_object( console );
466 return 1;
469 /* let process inherit the console from parent... this handle two cases :
470 * 1/ generic console inheritance
471 * 2/ parent is a renderer which launches process, and process should attach to the console
472 * renderered by parent
474 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
476 int done = 0;
477 struct process* parent = parent_thread->process;
479 /* if parent is a renderer, then attach current process to its console
480 * a bit hacky....
482 if (hconin)
484 struct console_input* console;
486 /* FIXME: should we check some access rights ? */
487 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
488 0, &console_input_ops )))
490 if (console->renderer == parent_thread)
492 process->console = (struct console_input*)grab_object( console );
493 process->console->num_proc++;
494 done = 1;
496 release_object( console );
498 else clear_error(); /* ignore error */
500 /* otherwise, if parent has a console, attach child to this console */
501 if (!done && parent->console)
503 process->console = (struct console_input*)grab_object( parent->console );
504 process->console->num_proc++;
508 struct thread *console_get_renderer( struct console_input *console )
510 return console->renderer;
513 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
515 struct console_input* console = NULL;
517 if (handle)
518 console = (struct console_input *)get_handle_obj( current->process, handle,
519 access, &console_input_ops );
520 else if (current->process->console)
522 console = (struct console_input *)grab_object( current->process->console );
525 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
526 return console;
529 struct console_signal_info
531 struct console_input *console;
532 process_id_t group;
533 int signal;
536 static int propagate_console_signal_cb(struct process *process, void *user)
538 struct console_signal_info* csi = (struct console_signal_info*)user;
540 if (process->console == csi->console && process->running_threads &&
541 (!csi->group || process->group_id == csi->group))
543 /* find a suitable thread to signal */
544 struct thread *thread;
545 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
547 if (send_thread_signal( thread, csi->signal )) break;
550 return FALSE;
553 static void propagate_console_signal( struct console_input *console,
554 int sig, process_id_t group_id )
556 struct console_signal_info csi;
558 if (!console)
560 set_error( STATUS_INVALID_PARAMETER );
561 return;
563 /* FIXME: should support the other events (like CTRL_BREAK) */
564 if (sig != CTRL_C_EVENT)
566 set_error( STATUS_NOT_IMPLEMENTED );
567 return;
569 csi.console = console;
570 csi.signal = SIGINT;
571 csi.group = group_id;
573 enum_processes(propagate_console_signal_cb, &csi);
576 static int get_console_mode( obj_handle_t handle )
578 struct object *obj;
579 int ret = 0;
581 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
583 if (obj->ops == &console_input_ops)
585 ret = ((struct console_input *)obj)->mode;
587 else if (obj->ops == &screen_buffer_ops)
589 ret = ((struct screen_buffer *)obj)->mode;
591 else
592 set_error( STATUS_OBJECT_TYPE_MISMATCH );
593 release_object( obj );
595 return ret;
598 /* changes the mode of either a console input or a screen buffer */
599 static int set_console_mode( obj_handle_t handle, int mode )
601 struct object *obj;
602 int ret = 0;
604 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
605 return 0;
606 if (obj->ops == &console_input_ops)
608 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
609 ((struct console_input *)obj)->mode = mode;
610 ret = 1;
612 else if (obj->ops == &screen_buffer_ops)
614 ((struct screen_buffer *)obj)->mode = mode;
615 ret = 1;
617 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
618 release_object( obj );
619 return ret;
622 /* add input events to a console input queue */
623 static int write_console_input( struct console_input* console, int count,
624 const INPUT_RECORD *records )
626 INPUT_RECORD *new_rec;
628 if (!count) return 0;
629 if (!(new_rec = realloc( console->records,
630 (console->recnum + count) * sizeof(INPUT_RECORD) )))
632 set_error( STATUS_NO_MEMORY );
633 release_object( console );
634 return -1;
636 console->records = new_rec;
637 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
639 if (console->mode & ENABLE_PROCESSED_INPUT)
641 int i = 0;
642 while (i < count)
644 if (records[i].EventType == KEY_EVENT &&
645 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
646 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
648 if (i != count - 1)
649 memcpy( &console->records[console->recnum + i],
650 &console->records[console->recnum + i + 1],
651 (count - i - 1) * sizeof(INPUT_RECORD) );
652 count--;
653 if (records[i].Event.KeyEvent.bKeyDown)
655 /* send SIGINT to all processes attached to this console */
656 propagate_console_signal( console, CTRL_C_EVENT, 0 );
659 else i++;
662 if (!console->recnum && count) set_event( console->event );
663 console->recnum += count;
664 return count;
667 /* retrieve a pointer to the console input records */
668 static int read_console_input( obj_handle_t handle, int count, int flush )
670 struct console_input *console;
672 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
673 FILE_READ_DATA, &console_input_ops )))
674 return -1;
676 if (!count)
678 /* special case: do not retrieve anything, but return
679 * the total number of records available */
680 count = console->recnum;
682 else
684 if (count > console->recnum) count = console->recnum;
685 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
687 if (flush)
689 int i;
690 for (i = count; i < console->recnum; i++)
691 console->records[i-count] = console->records[i];
692 if ((console->recnum -= count) > 0)
694 INPUT_RECORD *new_rec = realloc( console->records,
695 console->recnum * sizeof(INPUT_RECORD) );
696 if (new_rec) console->records = new_rec;
698 else
700 free( console->records );
701 console->records = NULL;
702 reset_event( console->event );
705 release_object( console );
706 return count;
709 /* set misc console input information */
710 static int set_console_input_info( const struct set_console_input_info_request *req,
711 const WCHAR *title, data_size_t len )
713 struct console_input *console;
714 struct console_renderer_event evt;
716 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
717 if (console_input_is_bare(console) &&
718 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
719 SET_CONSOLE_INPUT_INFO_WIN)))
721 set_error( STATUS_UNSUCCESSFUL );
722 goto error;
725 memset(&evt.u, 0, sizeof(evt.u));
726 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
728 struct screen_buffer *screen_buffer;
730 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
731 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
732 if (!screen_buffer || screen_buffer->input != console)
734 set_error( STATUS_INVALID_HANDLE );
735 if (screen_buffer) release_object( screen_buffer );
736 goto error;
739 if (screen_buffer != console->active)
741 if (console->active) release_object( console->active );
742 console->active = screen_buffer;
743 generate_sb_initial_events( console );
745 else
746 release_object( screen_buffer );
748 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
750 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
751 if (new_title)
753 memcpy( new_title, title, len );
754 new_title[len / sizeof(WCHAR)] = 0;
755 free( console->title );
756 console->title = new_title;
757 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
758 console_input_events_append( console, &evt );
761 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
763 console->history_mode = req->history_mode;
765 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
766 console->history_size != req->history_size)
768 WCHAR** mem = NULL;
769 int i;
770 int delta;
772 if (req->history_size)
774 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
775 if (!mem) goto error;
776 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
779 delta = (console->history_index > req->history_size) ?
780 (console->history_index - req->history_size) : 0;
782 for (i = delta; i < console->history_index; i++)
784 mem[i - delta] = console->history[i];
785 console->history[i] = NULL;
787 console->history_index -= delta;
789 for (i = 0; i < console->history_size; i++)
790 free( console->history[i] );
791 free( console->history );
792 console->history = mem;
793 console->history_size = req->history_size;
795 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
797 console->edition_mode = req->edition_mode;
799 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
801 console->input_cp = req->input_cp;
803 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
805 console->output_cp = req->output_cp;
807 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
809 console->win = req->win;
811 release_object( console );
812 return 1;
813 error:
814 if (console) release_object( console );
815 return 0;
818 /* resize a screen buffer */
819 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
820 int new_width, int new_height )
822 int i, old_width, old_height, copy_width, copy_height;
823 char_info_t *new_data;
825 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
827 set_error( STATUS_NO_MEMORY );
828 return 0;
830 old_width = screen_buffer->width;
831 old_height = screen_buffer->height;
832 copy_width = min( old_width, new_width );
833 copy_height = min( old_height, new_height );
835 /* copy all the rows */
836 for (i = 0; i < copy_height; i++)
838 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
839 copy_width * sizeof(char_info_t) );
842 /* clear the end of each row */
843 if (new_width > old_width)
845 /* fill first row */
846 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
847 /* and blast it to the other rows */
848 for (i = 1; i < copy_height; i++)
849 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
850 (new_width - old_width) * sizeof(char_info_t) );
853 /* clear remaining rows */
854 if (new_height > old_height)
856 /* fill first row */
857 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
858 /* and blast it to the other rows */
859 for (i = old_height+1; i < new_height; i++)
860 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
861 new_width * sizeof(char_info_t) );
863 free( screen_buffer->data );
864 screen_buffer->data = new_data;
865 screen_buffer->width = new_width;
866 screen_buffer->height = new_height;
867 return 1;
870 /* set misc screen buffer information */
871 static int set_console_output_info( struct screen_buffer *screen_buffer,
872 const struct set_console_output_info_request *req )
874 struct console_renderer_event evt;
876 memset(&evt.u, 0, sizeof(evt.u));
877 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
879 if (req->cursor_size < 1 || req->cursor_size > 100)
881 set_error( STATUS_INVALID_PARAMETER );
882 return 0;
884 if (screen_buffer->cursor_size != req->cursor_size ||
885 screen_buffer->cursor_visible != req->cursor_visible)
887 screen_buffer->cursor_size = req->cursor_size;
888 screen_buffer->cursor_visible = req->cursor_visible;
889 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
890 evt.u.cursor_geom.size = req->cursor_size;
891 evt.u.cursor_geom.visible = req->cursor_visible;
892 console_input_events_append( screen_buffer->input, &evt );
895 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
897 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
898 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
900 set_error( STATUS_INVALID_PARAMETER );
901 return 0;
903 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
905 screen_buffer->cursor_x = req->cursor_x;
906 screen_buffer->cursor_y = req->cursor_y;
907 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
908 evt.u.cursor_pos.x = req->cursor_x;
909 evt.u.cursor_pos.y = req->cursor_y;
910 console_input_events_append( screen_buffer->input, &evt );
913 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
915 unsigned cc;
917 /* new screen-buffer cannot be smaller than actual window */
918 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
919 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
921 set_error( STATUS_INVALID_PARAMETER );
922 return 0;
924 /* FIXME: there are also some basic minimum and max size to deal with */
925 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
927 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
928 evt.u.resize.width = req->width;
929 evt.u.resize.height = req->height;
930 console_input_events_append( screen_buffer->input, &evt );
932 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
933 evt.u.update.top = 0;
934 evt.u.update.bottom = screen_buffer->height - 1;
935 console_input_events_append( screen_buffer->input, &evt );
937 /* scroll window to display sb */
938 if (screen_buffer->win.right >= req->width)
940 screen_buffer->win.right -= screen_buffer->win.left;
941 screen_buffer->win.left = 0;
943 if (screen_buffer->win.bottom >= req->height)
945 screen_buffer->win.bottom -= screen_buffer->win.top;
946 screen_buffer->win.top = 0;
948 /* reset cursor if needed (normally, if cursor was outside of new sb, the
949 * window has been shifted so that the new position of the cursor will be
950 * visible */
951 cc = 0;
952 if (screen_buffer->cursor_x >= req->width)
954 screen_buffer->cursor_x = req->width - 1;
955 cc++;
957 if (screen_buffer->cursor_y >= req->height)
959 screen_buffer->cursor_y = req->height - 1;
960 cc++;
962 if (cc)
964 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
965 evt.u.cursor_pos.x = req->cursor_x;
966 evt.u.cursor_pos.y = req->cursor_y;
967 console_input_events_append( screen_buffer->input, &evt );
970 if (screen_buffer == screen_buffer->input->active &&
971 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
973 INPUT_RECORD ir;
974 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
975 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
976 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
977 write_console_input( screen_buffer->input, 1, &ir );
980 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
982 screen_buffer->attr = req->attr;
984 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
986 if (req->win_left < 0 || req->win_left > req->win_right ||
987 req->win_right >= screen_buffer->width ||
988 req->win_top < 0 || req->win_top > req->win_bottom ||
989 req->win_bottom >= screen_buffer->height)
991 set_error( STATUS_INVALID_PARAMETER );
992 return 0;
994 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
995 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
997 screen_buffer->win.left = req->win_left;
998 screen_buffer->win.top = req->win_top;
999 screen_buffer->win.right = req->win_right;
1000 screen_buffer->win.bottom = req->win_bottom;
1001 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1002 evt.u.display.left = req->win_left;
1003 evt.u.display.top = req->win_top;
1004 evt.u.display.width = req->win_right - req->win_left + 1;
1005 evt.u.display.height = req->win_bottom - req->win_top + 1;
1006 console_input_events_append( screen_buffer->input, &evt );
1009 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1011 /* can only be done by renderer */
1012 if (current->process->console != screen_buffer->input)
1014 set_error( STATUS_INVALID_PARAMETER );
1015 return 0;
1018 screen_buffer->max_width = req->max_width;
1019 screen_buffer->max_height = req->max_height;
1022 return 1;
1025 /* appends a new line to history (history is a fixed size array) */
1026 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1028 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1030 if (!ptr)
1031 return;
1033 if (!console || !console->history_size)
1035 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1036 free( ptr );
1037 return;
1040 memcpy( ptr, buf, len * sizeof(WCHAR) );
1041 ptr[len] = 0;
1043 if (console->history_mode && console->history_index &&
1044 strncmpW( console->history[console->history_index - 1], ptr, len ) == 0)
1046 /* ok, mode ask us to not use twice the same string...
1047 * so just free mem and returns
1049 set_error( STATUS_ALIAS_EXISTS );
1050 free(ptr);
1051 return;
1054 if (console->history_index < console->history_size)
1056 console->history[console->history_index++] = ptr;
1058 else
1060 free( console->history[0]) ;
1061 memmove( &console->history[0], &console->history[1],
1062 (console->history_size - 1) * sizeof(WCHAR*) );
1063 console->history[console->history_size - 1] = ptr;
1067 /* returns a line from the cache */
1068 static data_size_t console_input_get_hist( struct console_input *console, int index )
1070 data_size_t ret = 0;
1072 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1073 else
1075 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1076 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1078 return ret;
1081 /* dumb dump */
1082 static void console_input_dump( struct object *obj, int verbose )
1084 struct console_input *console = (struct console_input *)obj;
1085 assert( obj->ops == &console_input_ops );
1086 fprintf( stderr, "Console input active=%p evt=%p\n",
1087 console->active, console->evt );
1090 static void console_input_destroy( struct object *obj )
1092 struct console_input* console_in = (struct console_input *)obj;
1093 struct screen_buffer* curr;
1094 int i;
1096 assert( obj->ops == &console_input_ops );
1097 free( console_in->title );
1098 free( console_in->records );
1100 if (console_in->active) release_object( console_in->active );
1101 console_in->active = NULL;
1103 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1105 if (curr->input == console_in) curr->input = NULL;
1108 if (console_in->evt)
1110 release_object( console_in->evt );
1111 console_in->evt = NULL;
1113 release_object( console_in->event );
1114 if (console_in->fd)
1115 release_object( console_in->fd );
1117 for (i = 0; i < console_in->history_size; i++)
1118 free( console_in->history[i] );
1119 free( console_in->history );
1122 static void screen_buffer_dump( struct object *obj, int verbose )
1124 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1125 assert( obj->ops == &screen_buffer_ops );
1127 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1130 static void screen_buffer_destroy( struct object *obj )
1132 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1134 assert( obj->ops == &screen_buffer_ops );
1136 list_remove( &screen_buffer->entry );
1138 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1140 struct screen_buffer *sb;
1142 screen_buffer->input->active = NULL;
1143 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1145 if (sb->input == screen_buffer->input)
1147 sb->input->active = sb;
1148 break;
1152 if (screen_buffer->fd) release_object( screen_buffer->fd );
1153 free( screen_buffer->data );
1156 static struct fd *screen_buffer_get_fd( struct object *obj )
1158 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1159 assert( obj->ops == &screen_buffer_ops );
1160 if (screen_buffer->fd)
1161 return (struct fd*)grab_object( screen_buffer->fd );
1162 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1163 return NULL;
1166 /* write data into a screen buffer */
1167 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1168 const void* data, enum char_info_mode mode,
1169 int x, int y, int wrap )
1171 unsigned int i;
1172 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1174 if (y >= screen_buffer->height) return 0;
1176 if (wrap)
1177 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1178 else
1179 end = screen_buffer->data + (y+1) * screen_buffer->width;
1181 switch(mode)
1183 case CHAR_INFO_MODE_TEXT:
1185 const WCHAR *ptr = data;
1186 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1188 break;
1189 case CHAR_INFO_MODE_ATTR:
1191 const unsigned short *ptr = data;
1192 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1194 break;
1195 case CHAR_INFO_MODE_TEXTATTR:
1197 const char_info_t *ptr = data;
1198 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1200 break;
1201 case CHAR_INFO_MODE_TEXTSTDATTR:
1203 const WCHAR *ptr = data;
1204 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1206 dest->ch = ptr[i];
1207 dest->attr = screen_buffer->attr;
1210 break;
1211 default:
1212 set_error( STATUS_INVALID_PARAMETER );
1213 return 0;
1216 if (i && screen_buffer == screen_buffer->input->active)
1218 struct console_renderer_event evt;
1219 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1220 memset(&evt.u, 0, sizeof(evt.u));
1221 evt.u.update.top = y + x / screen_buffer->width;
1222 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1223 console_input_events_append( screen_buffer->input, &evt );
1225 return i;
1228 /* fill a screen buffer with uniform data */
1229 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1230 enum char_info_mode mode, int x, int y, int count, int wrap )
1232 int i;
1233 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1235 if (y >= screen_buffer->height) return 0;
1237 if (wrap)
1238 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1239 else
1240 end = screen_buffer->data + (y+1) * screen_buffer->width;
1242 if (count > end - dest) count = end - dest;
1244 switch(mode)
1246 case CHAR_INFO_MODE_TEXT:
1247 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1248 break;
1249 case CHAR_INFO_MODE_ATTR:
1250 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1251 break;
1252 case CHAR_INFO_MODE_TEXTATTR:
1253 for (i = 0; i < count; i++) dest[i] = data;
1254 break;
1255 case CHAR_INFO_MODE_TEXTSTDATTR:
1256 for (i = 0; i < count; i++)
1258 dest[i].ch = data.ch;
1259 dest[i].attr = screen_buffer->attr;
1261 break;
1262 default:
1263 set_error( STATUS_INVALID_PARAMETER );
1264 return 0;
1267 if (count && screen_buffer == screen_buffer->input->active)
1269 struct console_renderer_event evt;
1270 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1271 memset(&evt.u, 0, sizeof(evt.u));
1272 evt.u.update.top = y;
1273 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1274 console_input_events_append( screen_buffer->input, &evt );
1276 return i;
1279 /* read data from a screen buffer */
1280 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1281 enum char_info_mode mode, int wrap )
1283 int i;
1284 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1286 if (y >= screen_buffer->height) return;
1288 if (wrap)
1289 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1290 else
1291 end = screen_buffer->data + (y+1) * screen_buffer->width;
1293 switch(mode)
1295 case CHAR_INFO_MODE_TEXT:
1297 WCHAR *data;
1298 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1299 if ((data = set_reply_data_size( count * sizeof(*data) )))
1301 for (i = 0; i < count; i++) data[i] = src[i].ch;
1304 break;
1305 case CHAR_INFO_MODE_ATTR:
1307 unsigned short *data;
1308 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1309 if ((data = set_reply_data_size( count * sizeof(*data) )))
1311 for (i = 0; i < count; i++) data[i] = src[i].attr;
1314 break;
1315 case CHAR_INFO_MODE_TEXTATTR:
1317 char_info_t *data;
1318 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1319 if ((data = set_reply_data_size( count * sizeof(*data) )))
1321 for (i = 0; i < count; i++) data[i] = src[i];
1324 break;
1325 default:
1326 set_error( STATUS_INVALID_PARAMETER );
1327 break;
1331 /* scroll parts of a screen buffer */
1332 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1333 int w, int h )
1335 int j;
1336 char_info_t *psrc, *pdst;
1337 struct console_renderer_event evt;
1339 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1340 xsrc + w > screen_buffer->width ||
1341 xdst + w > screen_buffer->width ||
1342 ysrc + h > screen_buffer->height ||
1343 ydst + h > screen_buffer->height ||
1344 w == 0 || h == 0)
1346 set_error( STATUS_INVALID_PARAMETER );
1347 return;
1350 if (ysrc < ydst)
1352 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1353 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1355 for (j = h; j > 0; j--)
1357 memcpy(pdst, psrc, w * sizeof(*pdst) );
1358 pdst -= screen_buffer->width;
1359 psrc -= screen_buffer->width;
1362 else
1364 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1365 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1367 for (j = 0; j < h; j++)
1369 /* we use memmove here because when psrc and pdst are the same,
1370 * copies are done on the same row, so the dst and src blocks
1371 * can overlap */
1372 memmove( pdst, psrc, w * sizeof(*pdst) );
1373 pdst += screen_buffer->width;
1374 psrc += screen_buffer->width;
1378 /* FIXME: this could be enhanced, by signalling scroll */
1379 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1380 memset(&evt.u, 0, sizeof(evt.u));
1381 evt.u.update.top = min(ysrc, ydst);
1382 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1383 console_input_events_append( screen_buffer->input, &evt );
1386 /* allocate a console for the renderer */
1387 DECL_HANDLER(alloc_console)
1389 obj_handle_t in = 0;
1390 obj_handle_t evt = 0;
1391 struct process *process;
1392 struct thread *renderer;
1393 struct console_input *console;
1394 int fd;
1395 int attach = 0;
1397 if (req->input_fd != -1)
1399 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1401 set_error( STATUS_INVALID_PARAMETER );
1402 return;
1405 else fd = -1;
1407 switch (req->pid)
1409 case 0:
1410 /* renderer is current, console to be attached to parent process */
1411 renderer = current;
1412 if (!(process = current->process->parent))
1414 if (fd != -1) close( fd );
1415 set_error( STATUS_ACCESS_DENIED );
1416 return;
1418 grab_object( process );
1419 attach = 1;
1420 break;
1421 case 0xffffffff:
1422 /* no renderer, console to be attached to current process */
1423 renderer = NULL;
1424 process = current->process;
1425 grab_object( process );
1426 attach = 1;
1427 break;
1428 default:
1429 /* renderer is current, console to be attached to req->pid */
1430 renderer = current;
1431 if (!(process = get_process_from_id( req->pid )))
1433 if (fd != -1) close( fd );
1434 return;
1438 if (attach && process->console)
1440 if (fd != -1) close( fd );
1441 set_error( STATUS_ACCESS_DENIED );
1442 goto the_end;
1445 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1447 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1449 if (!console->evt ||
1450 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1452 if (attach)
1454 process->console = (struct console_input*)grab_object( console );
1455 console->num_proc++;
1457 reply->handle_in = in;
1458 reply->event = evt;
1459 release_object( console );
1460 goto the_end;
1462 close_handle( current->process, in );
1464 release_object( console );
1466 the_end:
1467 release_object( process );
1470 /* free the console of the current process */
1471 DECL_HANDLER(free_console)
1473 free_console( current->process );
1476 /* let the renderer peek the events it's waiting on */
1477 DECL_HANDLER(get_console_renderer_events)
1479 struct console_input_events *evt;
1481 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1482 FILE_READ_PROPERTIES, &console_input_events_ops );
1483 if (!evt) return;
1484 console_input_events_get( evt );
1485 release_object( evt );
1488 /* open a handle to the process console */
1489 DECL_HANDLER(open_console)
1491 struct object *obj = NULL;
1493 reply->handle = 0;
1494 if (!req->from)
1496 if (current->process->console)
1497 obj = grab_object( (struct object*)current->process->console );
1499 else if (req->from == (obj_handle_t)1)
1501 if (current->process->console && current->process->console->active)
1502 obj = grab_object( (struct object*)current->process->console->active );
1504 else if ((obj = get_handle_obj( current->process, req->from,
1505 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1507 struct console_input *console = (struct console_input *)obj;
1508 obj = (console->active) ? grab_object( console->active ) : NULL;
1509 release_object( console );
1512 /* FIXME: req->share is not used (as in screen buffer creation) */
1513 if (obj)
1515 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1516 release_object( obj );
1518 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1521 /* set info about a console input */
1522 DECL_HANDLER(set_console_input_info)
1524 set_console_input_info( req, get_req_data(), get_req_data_size() );
1527 /* get info about a console (output only) */
1528 DECL_HANDLER(get_console_input_info)
1530 struct console_input *console;
1532 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1533 if (console->title)
1535 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1536 if (len > get_reply_max_size()) len = get_reply_max_size();
1537 set_reply_data( console->title, len );
1539 reply->history_mode = console->history_mode;
1540 reply->history_size = console->history_size;
1541 reply->history_index = console->history_index;
1542 reply->edition_mode = console->edition_mode;
1543 reply->input_cp = console->input_cp;
1544 reply->output_cp = console->output_cp;
1545 reply->win = console->win;
1547 release_object( console );
1550 /* get a console mode (input or output) */
1551 DECL_HANDLER(get_console_mode)
1553 reply->mode = get_console_mode( req->handle );
1556 /* set a console mode (input or output) */
1557 DECL_HANDLER(set_console_mode)
1559 set_console_mode( req->handle, req->mode );
1562 /* add input records to a console input queue */
1563 DECL_HANDLER(write_console_input)
1565 struct console_input *console;
1567 reply->written = 0;
1568 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1569 FILE_WRITE_PROPERTIES, &console_input_ops )))
1570 return;
1571 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1572 get_req_data() );
1573 release_object( console );
1576 /* fetch input records from a console input queue */
1577 DECL_HANDLER(read_console_input)
1579 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1580 reply->read = read_console_input( req->handle, count, req->flush );
1583 /* appends a string to console's history */
1584 DECL_HANDLER(append_console_input_history)
1586 struct console_input *console;
1588 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1589 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1590 release_object( console );
1593 /* appends a string to console's history */
1594 DECL_HANDLER(get_console_input_history)
1596 struct console_input *console;
1598 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1599 reply->total = console_input_get_hist( console, req->index );
1600 release_object( console );
1603 /* creates a screen buffer */
1604 DECL_HANDLER(create_console_output)
1606 struct console_input* console;
1607 struct screen_buffer* screen_buffer;
1608 int fd;
1610 if (req->fd != -1)
1612 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1614 set_error( STATUS_INVALID_HANDLE );
1615 return;
1618 else fd = -1;
1619 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1621 if (fd != -1) close( fd );
1622 return;
1624 if (console_input_is_bare( console ) ^ (fd != -1))
1626 if (fd != -1) close( fd );
1627 release_object( console );
1628 set_error( STATUS_INVALID_HANDLE );
1629 return;
1632 screen_buffer = create_console_output( console, fd );
1633 if (screen_buffer)
1635 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1636 * see file.c on how this could be done */
1637 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1638 release_object( screen_buffer );
1640 release_object( console );
1643 /* set info about a console screen buffer */
1644 DECL_HANDLER(set_console_output_info)
1646 struct screen_buffer *screen_buffer;
1648 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1649 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1651 set_console_output_info( screen_buffer, req );
1652 release_object( screen_buffer );
1656 /* get info about a console screen buffer */
1657 DECL_HANDLER(get_console_output_info)
1659 struct screen_buffer *screen_buffer;
1661 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1662 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1664 reply->cursor_size = screen_buffer->cursor_size;
1665 reply->cursor_visible = screen_buffer->cursor_visible;
1666 reply->cursor_x = screen_buffer->cursor_x;
1667 reply->cursor_y = screen_buffer->cursor_y;
1668 reply->width = screen_buffer->width;
1669 reply->height = screen_buffer->height;
1670 reply->attr = screen_buffer->attr;
1671 reply->win_left = screen_buffer->win.left;
1672 reply->win_top = screen_buffer->win.top;
1673 reply->win_right = screen_buffer->win.right;
1674 reply->win_bottom = screen_buffer->win.bottom;
1675 reply->max_width = screen_buffer->max_width;
1676 reply->max_height = screen_buffer->max_height;
1677 release_object( screen_buffer );
1681 /* read data (chars & attrs) from a screen buffer */
1682 DECL_HANDLER(read_console_output)
1684 struct screen_buffer *screen_buffer;
1686 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1687 FILE_READ_DATA, &screen_buffer_ops )))
1689 if (console_input_is_bare( screen_buffer->input ))
1691 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1692 release_object( screen_buffer );
1693 return;
1695 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1696 reply->width = screen_buffer->width;
1697 reply->height = screen_buffer->height;
1698 release_object( screen_buffer );
1702 /* write data (char and/or attrs) to a screen buffer */
1703 DECL_HANDLER(write_console_output)
1705 struct screen_buffer *screen_buffer;
1707 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1708 FILE_WRITE_DATA, &screen_buffer_ops)))
1710 if (console_input_is_bare( screen_buffer->input ))
1712 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1713 release_object( screen_buffer );
1714 return;
1716 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1717 req->mode, req->x, req->y, req->wrap );
1718 reply->width = screen_buffer->width;
1719 reply->height = screen_buffer->height;
1720 release_object( screen_buffer );
1724 /* fill a screen buffer with constant data (chars and/or attributes) */
1725 DECL_HANDLER(fill_console_output)
1727 struct screen_buffer *screen_buffer;
1729 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1730 FILE_WRITE_DATA, &screen_buffer_ops)))
1732 if (console_input_is_bare( screen_buffer->input ))
1734 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1735 release_object( screen_buffer );
1736 return;
1738 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1739 req->x, req->y, req->count, req->wrap );
1740 release_object( screen_buffer );
1744 /* move a rect of data in a screen buffer */
1745 DECL_HANDLER(move_console_output)
1747 struct screen_buffer *screen_buffer;
1749 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1750 FILE_WRITE_DATA, &screen_buffer_ops)))
1752 if (console_input_is_bare( screen_buffer->input ))
1754 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1755 release_object( screen_buffer );
1756 return;
1758 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1759 req->w, req->h );
1760 release_object( screen_buffer );
1764 /* sends a signal to a console (process, group...) */
1765 DECL_HANDLER(send_console_signal)
1767 process_id_t group;
1769 group = req->group_id ? req->group_id : current->process->group_id;
1771 if (!group)
1772 set_error( STATUS_INVALID_PARAMETER );
1773 else
1774 propagate_console_signal( current->process->console, req->signal, group );
1777 /* get console which renderer is 'current' */
1778 static int cgwe_enum( struct process* process, void* user)
1780 if (process->console && process->console->renderer == current)
1782 *(struct console_input**)user = process->console;
1783 return 1;
1785 return 0;
1788 DECL_HANDLER(get_console_wait_event)
1790 struct console_input* console = NULL;
1792 if (current->process->console)
1793 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1794 else enum_processes(cgwe_enum, &console);
1796 if (console)
1798 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1799 release_object( console );
1801 else set_error( STATUS_INVALID_PARAMETER );