d3d8/tests: Add window proc tests for switching between fullscreen and windowed.
[wine.git] / server / console.c
blob444116309882ea4c7d4e2c4b44b487fc901eee10
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 thread *thread );
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 no_flush, /* flush */
176 console_get_fd_type, /* get_fd_type */
177 default_fd_ioctl, /* ioctl */
178 default_fd_queue_async, /* queue_async */
179 default_fd_reselect_async, /* reselect_async */
180 default_fd_cancel_async /* cancel_async */
183 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
185 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
187 static int console_input_is_bare( struct console_input* cin )
189 return cin->evt == NULL;
192 static struct fd *console_input_get_fd( struct object* obj )
194 struct console_input *console_input = (struct console_input*)obj;
195 assert( obj->ops == &console_input_ops );
196 if (console_input->fd)
197 return (struct fd*)grab_object( console_input->fd );
198 set_error( STATUS_OBJECT_TYPE_MISMATCH );
199 return NULL;
202 static enum server_fd_type console_get_fd_type( struct fd *fd )
204 return FD_TYPE_CHAR;
207 /* dumps the renderer events of a console */
208 static void console_input_events_dump( struct object *obj, int verbose )
210 struct console_input_events *evts = (struct console_input_events *)obj;
211 assert( obj->ops == &console_input_events_ops );
212 fprintf( stderr, "Console input events: %d/%d events\n",
213 evts->num_used, evts->num_alloc );
216 /* destroys the renderer events of a console */
217 static void console_input_events_destroy( struct object *obj )
219 struct console_input_events *evts = (struct console_input_events *)obj;
220 assert( obj->ops == &console_input_events_ops );
221 free( evts->events );
224 /* the renderer events list is signaled when it's not empty */
225 static int console_input_events_signaled( struct object *obj, struct thread *thread )
227 struct console_input_events *evts = (struct console_input_events *)obj;
228 assert( obj->ops == &console_input_events_ops );
229 return (evts->num_used != 0);
232 /* add an event to the console's renderer events list */
233 static void console_input_events_append( struct console_input* console,
234 struct console_renderer_event* evt)
236 struct console_input_events* evts;
237 int collapsed = FALSE;
239 if (!(evts = console->evt)) return;
240 /* to be done even when evt has been generated by the renderer ? */
242 /* try to collapse evt into current queue's events */
243 if (evts->num_used)
245 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
247 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
248 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
250 /* if two update events overlap, collapse them into a single one */
251 if (last->u.update.bottom + 1 >= evt->u.update.top &&
252 evt->u.update.bottom + 1 >= last->u.update.top)
254 last->u.update.top = min(last->u.update.top, evt->u.update.top);
255 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
256 collapsed = TRUE;
260 if (!collapsed)
262 if (evts->num_used == evts->num_alloc)
264 evts->num_alloc += 16;
265 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
266 assert(evts->events);
268 evts->events[evts->num_used++] = *evt;
270 wake_up( &evts->obj, 0 );
273 /* retrieves events from the console's renderer events list */
274 static void console_input_events_get( struct console_input_events* evts )
276 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
278 if (num > evts->num_used) num = evts->num_used;
279 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
280 if (num < evts->num_used)
282 memmove( &evts->events[0], &evts->events[num],
283 (evts->num_used - num) * sizeof(evts->events[0]) );
285 evts->num_used -= num;
288 static struct console_input_events *create_console_input_events(void)
290 struct console_input_events* evt;
292 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
293 evt->num_alloc = evt->num_used = 0;
294 evt->events = NULL;
295 return evt;
298 static struct object *create_console_input( struct thread* renderer, int fd )
300 struct console_input *console_input;
302 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
303 console_input->renderer = renderer;
304 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
305 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
306 console_input->num_proc = 0;
307 console_input->active = NULL;
308 console_input->recnum = 0;
309 console_input->records = NULL;
310 console_input->evt = renderer ? create_console_input_events() : NULL;
311 console_input->title = NULL;
312 console_input->history_size = 50;
313 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
314 console_input->history_index = 0;
315 console_input->history_mode = 0;
316 console_input->edition_mode = 0;
317 console_input->input_cp = 0;
318 console_input->output_cp = 0;
319 console_input->win = 0;
320 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
321 console_input->fd = NULL;
323 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
325 release_object( console_input );
326 return NULL;
328 if (fd != -1) /* bare console */
330 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
331 FILE_SYNCHRONOUS_IO_NONALERT )))
333 release_object( console_input );
334 return NULL;
336 allow_fd_caching( console_input->fd );
339 return &console_input->obj;
342 static void generate_sb_initial_events( struct console_input *console_input )
344 struct screen_buffer *screen_buffer = console_input->active;
345 struct console_renderer_event evt;
347 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
348 memset(&evt.u, 0, sizeof(evt.u));
349 console_input_events_append( console_input, &evt );
351 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
352 evt.u.resize.width = screen_buffer->width;
353 evt.u.resize.height = screen_buffer->height;
354 console_input_events_append( console_input, &evt );
356 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
357 evt.u.display.left = screen_buffer->win.left;
358 evt.u.display.top = screen_buffer->win.top;
359 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
360 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
361 console_input_events_append( console_input, &evt );
363 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
364 evt.u.update.top = 0;
365 evt.u.update.bottom = screen_buffer->height - 1;
366 console_input_events_append( console_input, &evt );
368 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
369 evt.u.cursor_geom.size = screen_buffer->cursor_size;
370 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
371 console_input_events_append( console_input, &evt );
373 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
374 evt.u.cursor_pos.x = screen_buffer->cursor_x;
375 evt.u.cursor_pos.y = screen_buffer->cursor_y;
376 console_input_events_append( console_input, &evt );
379 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
381 struct screen_buffer *screen_buffer;
382 int i;
384 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
386 if (fd != -1) close( fd );
387 return NULL;
389 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
390 screen_buffer->input = console_input;
391 screen_buffer->cursor_size = 100;
392 screen_buffer->cursor_visible = 1;
393 screen_buffer->width = 80;
394 screen_buffer->height = 150;
395 screen_buffer->max_width = 80;
396 screen_buffer->max_height = 25;
397 screen_buffer->cursor_x = 0;
398 screen_buffer->cursor_y = 0;
399 screen_buffer->attr = 0x0F;
400 screen_buffer->win.left = 0;
401 screen_buffer->win.right = screen_buffer->max_width - 1;
402 screen_buffer->win.top = 0;
403 screen_buffer->win.bottom = screen_buffer->max_height - 1;
404 if (fd == -1)
405 screen_buffer->fd = NULL;
406 else
408 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
409 FILE_SYNCHRONOUS_IO_NONALERT )))
411 release_object( screen_buffer );
412 return NULL;
414 allow_fd_caching(screen_buffer->fd);
417 list_add_head( &screen_buffer_list, &screen_buffer->entry );
419 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
420 sizeof(*screen_buffer->data) )))
422 release_object( screen_buffer );
423 return NULL;
425 /* clear the first row */
426 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
427 /* and copy it to all other rows */
428 for (i = 1; i < screen_buffer->height; i++)
429 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
430 screen_buffer->width * sizeof(char_info_t) );
432 if (!console_input->active)
434 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
435 generate_sb_initial_events( console_input );
437 return screen_buffer;
440 /* free the console for this process */
441 int free_console( struct process *process )
443 struct console_input* console = process->console;
445 if (!console) return 0;
447 process->console = NULL;
448 if (--console->num_proc == 0 && console->renderer)
450 /* all processes have terminated... tell the renderer to terminate too */
451 struct console_renderer_event evt;
452 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
453 memset(&evt.u, 0, sizeof(evt.u));
454 console_input_events_append( console, &evt );
456 release_object( console );
458 return 1;
461 /* let process inherit the console from parent... this handle two cases :
462 * 1/ generic console inheritance
463 * 2/ parent is a renderer which launches process, and process should attach to the console
464 * renderered by parent
466 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
468 int done = 0;
469 struct process* parent = parent_thread->process;
471 /* if parent is a renderer, then attach current process to its console
472 * a bit hacky....
474 if (hconin)
476 struct console_input* console;
478 /* FIXME: should we check some access rights ? */
479 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
480 0, &console_input_ops )))
482 if (console->renderer == parent_thread)
484 process->console = (struct console_input*)grab_object( console );
485 process->console->num_proc++;
486 done = 1;
488 release_object( console );
490 else clear_error(); /* ignore error */
492 /* otherwise, if parent has a console, attach child to this console */
493 if (!done && parent->console)
495 process->console = (struct console_input*)grab_object( parent->console );
496 process->console->num_proc++;
500 struct thread *console_get_renderer( struct console_input *console )
502 return console->renderer;
505 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
507 struct console_input* console = NULL;
509 if (handle)
510 console = (struct console_input *)get_handle_obj( current->process, handle,
511 access, &console_input_ops );
512 else if (current->process->console)
514 console = (struct console_input *)grab_object( current->process->console );
517 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
518 return console;
521 struct console_signal_info
523 struct console_input *console;
524 process_id_t group;
525 int signal;
528 static int propagate_console_signal_cb(struct process *process, void *user)
530 struct console_signal_info* csi = (struct console_signal_info*)user;
532 if (process->console == csi->console && process->running_threads &&
533 (!csi->group || process->group_id == csi->group))
535 /* find a suitable thread to signal */
536 struct thread *thread;
537 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
539 if (send_thread_signal( thread, csi->signal )) break;
542 return FALSE;
545 static void propagate_console_signal( struct console_input *console,
546 int sig, process_id_t group_id )
548 struct console_signal_info csi;
550 if (!console)
552 set_error( STATUS_INVALID_PARAMETER );
553 return;
555 /* FIXME: should support the other events (like CTRL_BREAK) */
556 if (sig != CTRL_C_EVENT)
558 set_error( STATUS_NOT_IMPLEMENTED );
559 return;
561 csi.console = console;
562 csi.signal = SIGINT;
563 csi.group = group_id;
565 enum_processes(propagate_console_signal_cb, &csi);
568 static int get_console_mode( obj_handle_t handle, int *bare )
570 struct object *obj;
571 int ret = 0;
573 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
575 if (obj->ops == &console_input_ops)
577 ret = ((struct console_input *)obj)->mode;
578 *bare = console_input_is_bare((struct console_input *)obj);
580 else if (obj->ops == &screen_buffer_ops)
582 ret = ((struct screen_buffer *)obj)->mode;
583 *bare = console_input_is_bare(((struct screen_buffer *)obj)->input);
585 else
586 set_error( STATUS_OBJECT_TYPE_MISMATCH );
587 release_object( obj );
589 return ret;
592 /* changes the mode of either a console input or a screen buffer */
593 static int set_console_mode( obj_handle_t handle, int mode )
595 struct object *obj;
596 int ret = 0;
598 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
599 return 0;
600 if (obj->ops == &console_input_ops)
602 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
603 ((struct console_input *)obj)->mode = mode;
604 ret = 1;
606 else if (obj->ops == &screen_buffer_ops)
608 ((struct screen_buffer *)obj)->mode = mode;
609 ret = 1;
611 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
612 release_object( obj );
613 return ret;
616 /* add input events to a console input queue */
617 static int write_console_input( struct console_input* console, int count,
618 const INPUT_RECORD *records )
620 INPUT_RECORD *new_rec;
622 if (!count) return 0;
623 if (!(new_rec = realloc( console->records,
624 (console->recnum + count) * sizeof(INPUT_RECORD) )))
626 set_error( STATUS_NO_MEMORY );
627 release_object( console );
628 return -1;
630 console->records = new_rec;
631 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
633 if (console->mode & ENABLE_PROCESSED_INPUT)
635 int i = 0;
636 while (i < count)
638 if (records[i].EventType == KEY_EVENT &&
639 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
640 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
642 if (i != count - 1)
643 memcpy( &console->records[console->recnum + i],
644 &console->records[console->recnum + i + 1],
645 (count - i - 1) * sizeof(INPUT_RECORD) );
646 count--;
647 if (records[i].Event.KeyEvent.bKeyDown)
649 /* send SIGINT to all processes attached to this console */
650 propagate_console_signal( console, CTRL_C_EVENT, 0 );
653 else i++;
656 if (!console->recnum && count) set_event( console->event );
657 console->recnum += count;
658 return count;
661 /* retrieve a pointer to the console input records */
662 static int read_console_input( obj_handle_t handle, int count, int flush )
664 struct console_input *console;
666 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
667 FILE_READ_DATA, &console_input_ops )))
668 return -1;
670 if (!count)
672 /* special case: do not retrieve anything, but return
673 * the total number of records available */
674 count = console->recnum;
676 else
678 if (count > console->recnum) count = console->recnum;
679 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
681 if (flush)
683 int i;
684 for (i = count; i < console->recnum; i++)
685 console->records[i-count] = console->records[i];
686 if ((console->recnum -= count) > 0)
688 INPUT_RECORD *new_rec = realloc( console->records,
689 console->recnum * sizeof(INPUT_RECORD) );
690 if (new_rec) console->records = new_rec;
692 else
694 free( console->records );
695 console->records = NULL;
696 reset_event( console->event );
699 release_object( console );
700 return count;
703 /* set misc console input information */
704 static int set_console_input_info( const struct set_console_input_info_request *req,
705 const WCHAR *title, data_size_t len )
707 struct console_input *console;
708 struct console_renderer_event evt;
710 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
711 if (console_input_is_bare(console) &&
712 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
713 SET_CONSOLE_INPUT_INFO_WIN)))
715 set_error( STATUS_UNSUCCESSFUL );
716 goto error;
719 memset(&evt.u, 0, sizeof(evt.u));
720 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
722 struct screen_buffer *screen_buffer;
724 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
725 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
726 if (!screen_buffer || screen_buffer->input != console)
728 set_error( STATUS_INVALID_HANDLE );
729 if (screen_buffer) release_object( screen_buffer );
730 goto error;
733 if (screen_buffer != console->active)
735 if (console->active) release_object( console->active );
736 console->active = screen_buffer;
737 generate_sb_initial_events( console );
739 else
740 release_object( screen_buffer );
742 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
744 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
745 if (new_title)
747 memcpy( new_title, title, len );
748 new_title[len / sizeof(WCHAR)] = 0;
749 free( console->title );
750 console->title = new_title;
751 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
752 console_input_events_append( console, &evt );
755 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
757 console->history_mode = req->history_mode;
759 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
760 console->history_size != req->history_size)
762 WCHAR** mem = NULL;
763 int i;
764 int delta;
766 if (req->history_size)
768 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
769 if (!mem) goto error;
770 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
773 delta = (console->history_index > req->history_size) ?
774 (console->history_index - req->history_size) : 0;
776 for (i = delta; i < console->history_index; i++)
778 mem[i - delta] = console->history[i];
779 console->history[i] = NULL;
781 console->history_index -= delta;
783 for (i = 0; i < console->history_size; i++)
784 free( console->history[i] );
785 free( console->history );
786 console->history = mem;
787 console->history_size = req->history_size;
789 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
791 console->edition_mode = req->edition_mode;
793 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
795 console->input_cp = req->input_cp;
797 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
799 console->output_cp = req->output_cp;
801 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
803 console->win = req->win;
805 release_object( console );
806 return 1;
807 error:
808 if (console) release_object( console );
809 return 0;
812 /* resize a screen buffer */
813 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
814 int new_width, int new_height )
816 int i, old_width, old_height, copy_width, copy_height;
817 char_info_t *new_data;
819 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
821 set_error( STATUS_NO_MEMORY );
822 return 0;
824 old_width = screen_buffer->width;
825 old_height = screen_buffer->height;
826 copy_width = min( old_width, new_width );
827 copy_height = min( old_height, new_height );
829 /* copy all the rows */
830 for (i = 0; i < copy_height; i++)
832 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
833 copy_width * sizeof(char_info_t) );
836 /* clear the end of each row */
837 if (new_width > old_width)
839 /* fill first row */
840 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
841 /* and blast it to the other rows */
842 for (i = 1; i < copy_height; i++)
843 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
844 (new_width - old_width) * sizeof(char_info_t) );
847 /* clear remaining rows */
848 if (new_height > old_height)
850 /* fill first row */
851 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
852 /* and blast it to the other rows */
853 for (i = old_height+1; i < new_height; i++)
854 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
855 new_width * sizeof(char_info_t) );
857 free( screen_buffer->data );
858 screen_buffer->data = new_data;
859 screen_buffer->width = new_width;
860 screen_buffer->height = new_height;
861 return 1;
864 /* set misc screen buffer information */
865 static int set_console_output_info( struct screen_buffer *screen_buffer,
866 const struct set_console_output_info_request *req )
868 struct console_renderer_event evt;
870 memset(&evt.u, 0, sizeof(evt.u));
871 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
873 if (req->cursor_size < 1 || req->cursor_size > 100)
875 set_error( STATUS_INVALID_PARAMETER );
876 return 0;
878 if (screen_buffer->cursor_size != req->cursor_size ||
879 screen_buffer->cursor_visible != req->cursor_visible)
881 screen_buffer->cursor_size = req->cursor_size;
882 screen_buffer->cursor_visible = req->cursor_visible;
883 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
884 evt.u.cursor_geom.size = req->cursor_size;
885 evt.u.cursor_geom.visible = req->cursor_visible;
886 console_input_events_append( screen_buffer->input, &evt );
889 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
891 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
892 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
894 set_error( STATUS_INVALID_PARAMETER );
895 return 0;
897 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
899 screen_buffer->cursor_x = req->cursor_x;
900 screen_buffer->cursor_y = req->cursor_y;
901 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
902 evt.u.cursor_pos.x = req->cursor_x;
903 evt.u.cursor_pos.y = req->cursor_y;
904 console_input_events_append( screen_buffer->input, &evt );
907 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
909 unsigned cc;
911 /* new screen-buffer cannot be smaller than actual window */
912 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
913 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
915 set_error( STATUS_INVALID_PARAMETER );
916 return 0;
918 /* FIXME: there are also some basic minimum and max size to deal with */
919 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
921 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
922 evt.u.resize.width = req->width;
923 evt.u.resize.height = req->height;
924 console_input_events_append( screen_buffer->input, &evt );
926 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
927 evt.u.update.top = 0;
928 evt.u.update.bottom = screen_buffer->height - 1;
929 console_input_events_append( screen_buffer->input, &evt );
931 /* scroll window to display sb */
932 if (screen_buffer->win.right >= req->width)
934 screen_buffer->win.right -= screen_buffer->win.left;
935 screen_buffer->win.left = 0;
937 if (screen_buffer->win.bottom >= req->height)
939 screen_buffer->win.bottom -= screen_buffer->win.top;
940 screen_buffer->win.top = 0;
942 /* reset cursor if needed (normally, if cursor was outside of new sb, the
943 * window has been shifted so that the new position of the cursor will be
944 * visible */
945 cc = 0;
946 if (screen_buffer->cursor_x >= req->width)
948 screen_buffer->cursor_x = req->width - 1;
949 cc++;
951 if (screen_buffer->cursor_y >= req->height)
953 screen_buffer->cursor_y = req->height - 1;
954 cc++;
956 if (cc)
958 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
959 evt.u.cursor_pos.x = req->cursor_x;
960 evt.u.cursor_pos.y = req->cursor_y;
961 console_input_events_append( screen_buffer->input, &evt );
964 if (screen_buffer == screen_buffer->input->active &&
965 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
967 INPUT_RECORD ir;
968 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
969 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
970 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
971 write_console_input( screen_buffer->input, 1, &ir );
974 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
976 screen_buffer->attr = req->attr;
978 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
980 if (req->win_left < 0 || req->win_left > req->win_right ||
981 req->win_right >= screen_buffer->width ||
982 req->win_top < 0 || req->win_top > req->win_bottom ||
983 req->win_bottom >= screen_buffer->height)
985 set_error( STATUS_INVALID_PARAMETER );
986 return 0;
988 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
989 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
991 screen_buffer->win.left = req->win_left;
992 screen_buffer->win.top = req->win_top;
993 screen_buffer->win.right = req->win_right;
994 screen_buffer->win.bottom = req->win_bottom;
995 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
996 evt.u.display.left = req->win_left;
997 evt.u.display.top = req->win_top;
998 evt.u.display.width = req->win_right - req->win_left + 1;
999 evt.u.display.height = req->win_bottom - req->win_top + 1;
1000 console_input_events_append( screen_buffer->input, &evt );
1003 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1005 /* can only be done by renderer */
1006 if (current->process->console != screen_buffer->input)
1008 set_error( STATUS_INVALID_PARAMETER );
1009 return 0;
1012 screen_buffer->max_width = req->max_width;
1013 screen_buffer->max_height = req->max_height;
1016 return 1;
1019 /* appends a new line to history (history is a fixed size array) */
1020 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1022 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1024 if (!ptr)
1025 return;
1027 if (!console || !console->history_size)
1029 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1030 free( ptr );
1031 return;
1034 memcpy( ptr, buf, len * sizeof(WCHAR) );
1035 ptr[len] = 0;
1037 if (console->history_mode && console->history_index &&
1038 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
1040 /* ok, mode ask us to not use twice the same string...
1041 * so just free mem and returns
1043 set_error( STATUS_ALIAS_EXISTS );
1044 free(ptr);
1045 return;
1048 if (console->history_index < console->history_size)
1050 console->history[console->history_index++] = ptr;
1052 else
1054 free( console->history[0]) ;
1055 memmove( &console->history[0], &console->history[1],
1056 (console->history_size - 1) * sizeof(WCHAR*) );
1057 console->history[console->history_size - 1] = ptr;
1061 /* returns a line from the cache */
1062 static data_size_t console_input_get_hist( struct console_input *console, int index )
1064 data_size_t ret = 0;
1066 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1067 else
1069 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1070 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1072 return ret;
1075 /* dumb dump */
1076 static void console_input_dump( struct object *obj, int verbose )
1078 struct console_input *console = (struct console_input *)obj;
1079 assert( obj->ops == &console_input_ops );
1080 fprintf( stderr, "Console input active=%p evt=%p\n",
1081 console->active, console->evt );
1084 static void console_input_destroy( struct object *obj )
1086 struct console_input* console_in = (struct console_input *)obj;
1087 struct screen_buffer* curr;
1088 int i;
1090 assert( obj->ops == &console_input_ops );
1091 free( console_in->title );
1092 free( console_in->records );
1094 if (console_in->active) release_object( console_in->active );
1095 console_in->active = NULL;
1097 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1099 if (curr->input == console_in) curr->input = NULL;
1102 if (console_in->evt)
1104 release_object( console_in->evt );
1105 console_in->evt = NULL;
1107 release_object( console_in->event );
1108 if (console_in->fd)
1109 release_object( console_in->fd );
1111 for (i = 0; i < console_in->history_size; i++)
1112 free( console_in->history[i] );
1113 free( console_in->history );
1116 static void screen_buffer_dump( struct object *obj, int verbose )
1118 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1119 assert( obj->ops == &screen_buffer_ops );
1121 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1124 static void screen_buffer_destroy( struct object *obj )
1126 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1128 assert( obj->ops == &screen_buffer_ops );
1130 list_remove( &screen_buffer->entry );
1132 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1134 struct screen_buffer *sb;
1136 screen_buffer->input->active = NULL;
1137 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1139 if (sb->input == screen_buffer->input)
1141 sb->input->active = sb;
1142 break;
1146 if (screen_buffer->fd) release_object( screen_buffer->fd );
1147 free( screen_buffer->data );
1150 static struct fd *screen_buffer_get_fd( struct object *obj )
1152 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1153 assert( obj->ops == &screen_buffer_ops );
1154 if (screen_buffer->fd)
1155 return (struct fd*)grab_object( screen_buffer->fd );
1156 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1157 return NULL;
1160 /* write data into a screen buffer */
1161 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1162 const void* data, enum char_info_mode mode,
1163 int x, int y, int wrap )
1165 unsigned int i;
1166 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1168 if (y >= screen_buffer->height) return 0;
1170 if (wrap)
1171 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1172 else
1173 end = screen_buffer->data + (y+1) * screen_buffer->width;
1175 switch(mode)
1177 case CHAR_INFO_MODE_TEXT:
1179 const WCHAR *ptr = data;
1180 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1182 break;
1183 case CHAR_INFO_MODE_ATTR:
1185 const unsigned short *ptr = data;
1186 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1188 break;
1189 case CHAR_INFO_MODE_TEXTATTR:
1191 const char_info_t *ptr = data;
1192 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1194 break;
1195 case CHAR_INFO_MODE_TEXTSTDATTR:
1197 const WCHAR *ptr = data;
1198 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1200 dest->ch = ptr[i];
1201 dest->attr = screen_buffer->attr;
1204 break;
1205 default:
1206 set_error( STATUS_INVALID_PARAMETER );
1207 return 0;
1210 if (i && screen_buffer == screen_buffer->input->active)
1212 struct console_renderer_event evt;
1213 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1214 memset(&evt.u, 0, sizeof(evt.u));
1215 evt.u.update.top = y + x / screen_buffer->width;
1216 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1217 console_input_events_append( screen_buffer->input, &evt );
1219 return i;
1222 /* fill a screen buffer with uniform data */
1223 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1224 enum char_info_mode mode, int x, int y, int count, int wrap )
1226 int i;
1227 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1229 if (y >= screen_buffer->height) return 0;
1231 if (wrap)
1232 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1233 else
1234 end = screen_buffer->data + (y+1) * screen_buffer->width;
1236 if (count > end - dest) count = end - dest;
1238 switch(mode)
1240 case CHAR_INFO_MODE_TEXT:
1241 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1242 break;
1243 case CHAR_INFO_MODE_ATTR:
1244 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1245 break;
1246 case CHAR_INFO_MODE_TEXTATTR:
1247 for (i = 0; i < count; i++) dest[i] = data;
1248 break;
1249 case CHAR_INFO_MODE_TEXTSTDATTR:
1250 for (i = 0; i < count; i++)
1252 dest[i].ch = data.ch;
1253 dest[i].attr = screen_buffer->attr;
1255 break;
1256 default:
1257 set_error( STATUS_INVALID_PARAMETER );
1258 return 0;
1261 if (count && screen_buffer == screen_buffer->input->active)
1263 struct console_renderer_event evt;
1264 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1265 memset(&evt.u, 0, sizeof(evt.u));
1266 evt.u.update.top = y;
1267 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1268 console_input_events_append( screen_buffer->input, &evt );
1270 return i;
1273 /* read data from a screen buffer */
1274 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1275 enum char_info_mode mode, int wrap )
1277 int i;
1278 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1280 if (y >= screen_buffer->height) return;
1282 if (wrap)
1283 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1284 else
1285 end = screen_buffer->data + (y+1) * screen_buffer->width;
1287 switch(mode)
1289 case CHAR_INFO_MODE_TEXT:
1291 WCHAR *data;
1292 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1293 if ((data = set_reply_data_size( count * sizeof(*data) )))
1295 for (i = 0; i < count; i++) data[i] = src[i].ch;
1298 break;
1299 case CHAR_INFO_MODE_ATTR:
1301 unsigned short *data;
1302 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1303 if ((data = set_reply_data_size( count * sizeof(*data) )))
1305 for (i = 0; i < count; i++) data[i] = src[i].attr;
1308 break;
1309 case CHAR_INFO_MODE_TEXTATTR:
1311 char_info_t *data;
1312 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1313 if ((data = set_reply_data_size( count * sizeof(*data) )))
1315 for (i = 0; i < count; i++) data[i] = src[i];
1318 break;
1319 default:
1320 set_error( STATUS_INVALID_PARAMETER );
1321 break;
1325 /* scroll parts of a screen buffer */
1326 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1327 int w, int h )
1329 int j;
1330 char_info_t *psrc, *pdst;
1331 struct console_renderer_event evt;
1333 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1334 xsrc + w > screen_buffer->width ||
1335 xdst + w > screen_buffer->width ||
1336 ysrc + h > screen_buffer->height ||
1337 ydst + h > screen_buffer->height ||
1338 w == 0 || h == 0)
1340 set_error( STATUS_INVALID_PARAMETER );
1341 return;
1344 if (ysrc < ydst)
1346 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1347 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1349 for (j = h; j > 0; j--)
1351 memcpy(pdst, psrc, w * sizeof(*pdst) );
1352 pdst -= screen_buffer->width;
1353 psrc -= screen_buffer->width;
1356 else
1358 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1359 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1361 for (j = 0; j < h; j++)
1363 /* we use memmove here because when psrc and pdst are the same,
1364 * copies are done on the same row, so the dst and src blocks
1365 * can overlap */
1366 memmove( pdst, psrc, w * sizeof(*pdst) );
1367 pdst += screen_buffer->width;
1368 psrc += screen_buffer->width;
1372 /* FIXME: this could be enhanced, by signalling scroll */
1373 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1374 memset(&evt.u, 0, sizeof(evt.u));
1375 evt.u.update.top = min(ysrc, ydst);
1376 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1377 console_input_events_append( screen_buffer->input, &evt );
1380 /* allocate a console for the renderer */
1381 DECL_HANDLER(alloc_console)
1383 obj_handle_t in = 0;
1384 obj_handle_t evt = 0;
1385 struct process *process;
1386 struct thread *renderer;
1387 struct console_input *console;
1388 int fd;
1389 int attach = 0;
1391 switch (req->pid)
1393 case 0:
1394 /* renderer is current, console to be attached to parent process */
1395 renderer = current;
1396 if (!(process = current->process->parent))
1398 set_error( STATUS_ACCESS_DENIED );
1399 return;
1401 grab_object( process );
1402 attach = 1;
1403 break;
1404 case 0xffffffff:
1405 /* no renderer, console to be attached to current process */
1406 renderer = NULL;
1407 process = current->process;
1408 grab_object( process );
1409 attach = 1;
1410 break;
1411 default:
1412 /* renderer is current, console to be attached to req->pid */
1413 renderer = current;
1414 if (!(process = get_process_from_id( req->pid ))) return;
1417 if (attach && process->console)
1419 set_error( STATUS_ACCESS_DENIED );
1420 goto the_end;
1422 if (req->input_fd != -1)
1424 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1426 set_error( STATUS_INVALID_PARAMETER );
1427 goto the_end;
1430 else fd = -1;
1432 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1434 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1436 if (!console->evt ||
1437 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1439 if (attach)
1441 process->console = (struct console_input*)grab_object( console );
1442 console->num_proc++;
1444 reply->handle_in = in;
1445 reply->event = evt;
1446 release_object( console );
1447 goto the_end;
1449 close_handle( current->process, in );
1451 release_object( console );
1453 the_end:
1454 release_object( process );
1457 /* free the console of the current process */
1458 DECL_HANDLER(free_console)
1460 free_console( current->process );
1463 /* let the renderer peek the events it's waiting on */
1464 DECL_HANDLER(get_console_renderer_events)
1466 struct console_input_events *evt;
1468 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1469 FILE_READ_PROPERTIES, &console_input_events_ops );
1470 if (!evt) return;
1471 console_input_events_get( evt );
1472 release_object( evt );
1475 /* open a handle to the process console */
1476 DECL_HANDLER(open_console)
1478 struct object *obj = NULL;
1480 reply->handle = 0;
1481 if (!req->from)
1483 if (current->process->console)
1484 obj = grab_object( (struct object*)current->process->console );
1486 else if (req->from == (obj_handle_t)1)
1488 if (current->process->console && current->process->console->active)
1489 obj = grab_object( (struct object*)current->process->console->active );
1491 else if ((obj = get_handle_obj( current->process, req->from,
1492 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1494 struct console_input *console = (struct console_input *)obj;
1495 obj = (console->active) ? grab_object( console->active ) : NULL;
1496 release_object( console );
1499 /* FIXME: req->share is not used (as in screen buffer creation) */
1500 if (obj)
1502 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1503 release_object( obj );
1505 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1508 /* set info about a console input */
1509 DECL_HANDLER(set_console_input_info)
1511 set_console_input_info( req, get_req_data(), get_req_data_size() );
1514 /* get info about a console (output only) */
1515 DECL_HANDLER(get_console_input_info)
1517 struct console_input *console;
1519 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1520 if (console->title)
1522 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1523 if (len > get_reply_max_size()) len = get_reply_max_size();
1524 set_reply_data( console->title, len );
1526 reply->history_mode = console->history_mode;
1527 reply->history_size = console->history_size;
1528 reply->history_index = console->history_index;
1529 reply->edition_mode = console->edition_mode;
1530 reply->input_cp = console->input_cp;
1531 reply->output_cp = console->output_cp;
1532 reply->win = console->win;
1534 release_object( console );
1537 /* get a console mode (input or output) */
1538 DECL_HANDLER(get_console_mode)
1540 reply->mode = get_console_mode( req->handle, &reply->is_bare );
1543 /* set a console mode (input or output) */
1544 DECL_HANDLER(set_console_mode)
1546 set_console_mode( req->handle, req->mode );
1549 /* add input records to a console input queue */
1550 DECL_HANDLER(write_console_input)
1552 struct console_input *console;
1554 reply->written = 0;
1555 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1556 FILE_WRITE_PROPERTIES, &console_input_ops )))
1557 return;
1558 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1559 get_req_data() );
1560 release_object( console );
1563 /* fetch input records from a console input queue */
1564 DECL_HANDLER(read_console_input)
1566 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1567 reply->read = read_console_input( req->handle, count, req->flush );
1570 /* appends a string to console's history */
1571 DECL_HANDLER(append_console_input_history)
1573 struct console_input *console;
1575 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1576 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1577 release_object( console );
1580 /* appends a string to console's history */
1581 DECL_HANDLER(get_console_input_history)
1583 struct console_input *console;
1585 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1586 reply->total = console_input_get_hist( console, req->index );
1587 release_object( console );
1590 /* creates a screen buffer */
1591 DECL_HANDLER(create_console_output)
1593 struct console_input* console;
1594 struct screen_buffer* screen_buffer;
1595 int fd;
1597 if (req->fd != -1)
1599 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1601 set_error( STATUS_INVALID_HANDLE );
1602 return;
1605 else fd = -1;
1606 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1608 close(fd);
1609 return;
1611 if (console_input_is_bare( console ) ^ (fd != -1))
1613 close( fd );
1614 release_object( console );
1615 set_error( STATUS_INVALID_HANDLE );
1616 return;
1619 screen_buffer = create_console_output( console, fd );
1620 if (screen_buffer)
1622 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1623 * see file.c on how this could be done */
1624 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1625 release_object( screen_buffer );
1627 release_object( console );
1630 /* set info about a console screen buffer */
1631 DECL_HANDLER(set_console_output_info)
1633 struct screen_buffer *screen_buffer;
1635 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1636 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1638 set_console_output_info( screen_buffer, req );
1639 release_object( screen_buffer );
1643 /* get info about a console screen buffer */
1644 DECL_HANDLER(get_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_READ_PROPERTIES, &screen_buffer_ops)))
1651 reply->cursor_size = screen_buffer->cursor_size;
1652 reply->cursor_visible = screen_buffer->cursor_visible;
1653 reply->cursor_x = screen_buffer->cursor_x;
1654 reply->cursor_y = screen_buffer->cursor_y;
1655 reply->width = screen_buffer->width;
1656 reply->height = screen_buffer->height;
1657 reply->attr = screen_buffer->attr;
1658 reply->win_left = screen_buffer->win.left;
1659 reply->win_top = screen_buffer->win.top;
1660 reply->win_right = screen_buffer->win.right;
1661 reply->win_bottom = screen_buffer->win.bottom;
1662 reply->max_width = screen_buffer->max_width;
1663 reply->max_height = screen_buffer->max_height;
1664 release_object( screen_buffer );
1668 /* read data (chars & attrs) from a screen buffer */
1669 DECL_HANDLER(read_console_output)
1671 struct screen_buffer *screen_buffer;
1673 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1674 FILE_READ_DATA, &screen_buffer_ops )))
1676 if (console_input_is_bare( screen_buffer->input ))
1678 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1679 release_object( screen_buffer );
1680 return;
1682 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1683 reply->width = screen_buffer->width;
1684 reply->height = screen_buffer->height;
1685 release_object( screen_buffer );
1689 /* write data (char and/or attrs) to a screen buffer */
1690 DECL_HANDLER(write_console_output)
1692 struct screen_buffer *screen_buffer;
1694 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1695 FILE_WRITE_DATA, &screen_buffer_ops)))
1697 if (console_input_is_bare( screen_buffer->input ))
1699 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1700 release_object( screen_buffer );
1701 return;
1703 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1704 req->mode, req->x, req->y, req->wrap );
1705 reply->width = screen_buffer->width;
1706 reply->height = screen_buffer->height;
1707 release_object( screen_buffer );
1711 /* fill a screen buffer with constant data (chars and/or attributes) */
1712 DECL_HANDLER(fill_console_output)
1714 struct screen_buffer *screen_buffer;
1716 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1717 FILE_WRITE_DATA, &screen_buffer_ops)))
1719 if (console_input_is_bare( screen_buffer->input ))
1721 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1722 release_object( screen_buffer );
1723 return;
1725 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1726 req->x, req->y, req->count, req->wrap );
1727 release_object( screen_buffer );
1731 /* move a rect of data in a screen buffer */
1732 DECL_HANDLER(move_console_output)
1734 struct screen_buffer *screen_buffer;
1736 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1737 FILE_WRITE_DATA, &screen_buffer_ops)))
1739 if (console_input_is_bare( screen_buffer->input ))
1741 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1742 release_object( screen_buffer );
1743 return;
1745 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1746 req->w, req->h );
1747 release_object( screen_buffer );
1751 /* sends a signal to a console (process, group...) */
1752 DECL_HANDLER(send_console_signal)
1754 process_id_t group;
1756 group = req->group_id ? req->group_id : current->process->group_id;
1758 if (!group)
1759 set_error( STATUS_INVALID_PARAMETER );
1760 else
1761 propagate_console_signal( current->process->console, req->signal, group );
1764 /* get console which renderer is 'current' */
1765 static int cgwe_enum( struct process* process, void* user)
1767 if (process->console && process->console->renderer == current)
1769 *(struct console_input**)user = process->console;
1770 return 1;
1772 return 0;
1775 DECL_HANDLER(get_console_wait_event)
1777 struct console_input* console = NULL;
1779 if (current->process->console)
1780 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1781 else enum_processes(cgwe_enum, &console);
1783 if (console)
1785 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1786 release_object( console );
1788 else set_error( STATUS_INVALID_PARAMETER );