wined3d: Add AMD NT6x drivers.
[wine.git] / server / console.c
blob2ca6bd4191948e1e7cafdeebb99a26a06604d6cf
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>
31 #ifdef HAVE_TERMIOS_H
32 #include <termios.h>
33 #endif
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "handle.h"
38 #include "process.h"
39 #include "request.h"
40 #include "file.h"
41 #include "unicode.h"
42 #include "wincon.h"
43 #include "winternl.h"
45 struct screen_buffer;
46 struct console_input_events;
48 struct console_input
50 struct object obj; /* object header */
51 int num_proc; /* number of processes attached to this console */
52 struct thread *renderer; /* console renderer thread */
53 int mode; /* input mode */
54 struct screen_buffer *active; /* active screen buffer */
55 int recnum; /* number of input records */
56 INPUT_RECORD *records; /* input records */
57 struct console_input_events *evt; /* synchronization event with renderer */
58 WCHAR *title; /* console title */
59 WCHAR **history; /* lines history */
60 int history_size; /* number of entries in history array */
61 int history_index; /* number of used entries in history array */
62 int history_mode; /* mode of history (non zero means remove doubled strings */
63 int edition_mode; /* index to edition mode flavors */
64 int input_cp; /* console input codepage */
65 int output_cp; /* console output codepage */
66 user_handle_t win; /* window handle if backend supports it */
67 struct event *event; /* event to wait on for input queue */
68 struct fd *fd; /* for bare console, attached input fd */
69 struct termios termios; /* for bare console, saved termio info */
72 static void console_input_dump( struct object *obj, int verbose );
73 static void console_input_destroy( struct object *obj );
74 static struct fd *console_input_get_fd( struct object *obj );
76 static const struct object_ops console_input_ops =
78 sizeof(struct console_input), /* size */
79 console_input_dump, /* dump */
80 no_get_type, /* get_type */
81 no_add_queue, /* add_queue */
82 NULL, /* remove_queue */
83 NULL, /* signaled */
84 no_satisfied, /* satisfied */
85 no_signal, /* signal */
86 console_input_get_fd, /* get_fd */
87 default_fd_map_access, /* map_access */
88 default_get_sd, /* get_sd */
89 default_set_sd, /* set_sd */
90 no_lookup_name, /* lookup_name */
91 no_open_file, /* open_file */
92 no_close_handle, /* close_handle */
93 console_input_destroy /* destroy */
96 static void console_input_events_dump( struct object *obj, int verbose );
97 static void console_input_events_destroy( struct object *obj );
98 static int console_input_events_signaled( struct object *obj, struct thread *thread );
100 struct console_input_events
102 struct object obj; /* object header */
103 int num_alloc; /* number of allocated events */
104 int num_used; /* number of actually used events */
105 struct console_renderer_event* events;
108 static const struct object_ops console_input_events_ops =
110 sizeof(struct console_input_events), /* size */
111 console_input_events_dump, /* dump */
112 no_get_type, /* get_type */
113 add_queue, /* add_queue */
114 remove_queue, /* remove_queue */
115 console_input_events_signaled, /* signaled */
116 no_satisfied, /* satisfied */
117 no_signal, /* signal */
118 no_get_fd, /* get_fd */
119 default_fd_map_access, /* map_access */
120 default_get_sd, /* get_sd */
121 default_set_sd, /* set_sd */
122 no_lookup_name, /* lookup_name */
123 no_open_file, /* open_file */
124 no_close_handle, /* close_handle */
125 console_input_events_destroy /* destroy */
128 struct screen_buffer
130 struct object obj; /* object header */
131 struct list entry; /* entry in list of all screen buffers */
132 struct console_input *input; /* associated console input */
133 int mode; /* output mode */
134 int cursor_size; /* size of cursor (percentage filled) */
135 int cursor_visible;/* cursor visibility flag */
136 int cursor_x; /* position of cursor */
137 int cursor_y; /* position of cursor */
138 int width; /* size (w-h) of the screen buffer */
139 int height;
140 int max_width; /* size (w-h) of the window given font size */
141 int max_height;
142 char_info_t *data; /* the data for each cell - a width x height matrix */
143 unsigned short attr; /* default attribute for screen buffer */
144 rectangle_t win; /* current visible window on the screen buffer *
145 * as seen in wineconsole */
146 struct fd *fd; /* for bare console, attached output fd */
149 static void screen_buffer_dump( struct object *obj, int verbose );
150 static void screen_buffer_destroy( struct object *obj );
151 static struct fd *screen_buffer_get_fd( struct object *obj );
153 static const struct object_ops screen_buffer_ops =
155 sizeof(struct screen_buffer), /* size */
156 screen_buffer_dump, /* dump */
157 no_get_type, /* get_type */
158 no_add_queue, /* add_queue */
159 NULL, /* remove_queue */
160 NULL, /* signaled */
161 NULL, /* satisfied */
162 no_signal, /* signal */
163 screen_buffer_get_fd, /* get_fd */
164 default_fd_map_access, /* map_access */
165 default_get_sd, /* get_sd */
166 default_set_sd, /* set_sd */
167 no_lookup_name, /* lookup_name */
168 no_open_file, /* open_file */
169 no_close_handle, /* close_handle */
170 screen_buffer_destroy /* destroy */
173 static enum server_fd_type console_get_fd_type( struct fd *fd );
175 static const struct fd_ops console_fd_ops =
177 default_fd_get_poll_events, /* get_poll_events */
178 default_poll_event, /* poll_event */
179 no_flush, /* flush */
180 console_get_fd_type, /* get_fd_type */
181 default_fd_ioctl, /* ioctl */
182 default_fd_queue_async, /* queue_async */
183 default_fd_reselect_async, /* reselect_async */
184 default_fd_cancel_async /* cancel_async */
187 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
189 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
191 static int console_input_is_bare( struct console_input* cin )
193 return cin->evt == NULL;
196 static struct fd *console_input_get_fd( struct object* obj )
198 struct console_input *console_input = (struct console_input*)obj;
199 assert( obj->ops == &console_input_ops );
200 return console_input->fd ? (struct fd*)grab_object( console_input->fd ) : NULL;
203 static enum server_fd_type console_get_fd_type( struct fd *fd )
205 return FD_TYPE_CHAR;
208 /* dumps the renderer events of a console */
209 static void console_input_events_dump( struct object *obj, int verbose )
211 struct console_input_events *evts = (struct console_input_events *)obj;
212 assert( obj->ops == &console_input_events_ops );
213 fprintf( stderr, "Console input events: %d/%d events\n",
214 evts->num_used, evts->num_alloc );
217 /* destroys the renderer events of a console */
218 static void console_input_events_destroy( struct object *obj )
220 struct console_input_events *evts = (struct console_input_events *)obj;
221 assert( obj->ops == &console_input_events_ops );
222 free( evts->events );
225 /* the renderer events list is signaled when it's not empty */
226 static int console_input_events_signaled( struct object *obj, struct thread *thread )
228 struct console_input_events *evts = (struct console_input_events *)obj;
229 assert( obj->ops == &console_input_events_ops );
230 return (evts->num_used != 0);
233 /* add an event to the console's renderer events list */
234 static void console_input_events_append( struct console_input* console,
235 struct console_renderer_event* evt)
237 struct console_input_events* evts;
238 int collapsed = FALSE;
240 if (!(evts = console->evt)) return;
241 /* to be done even when evt has been generated by the renderer ? */
243 /* try to collapse evt into current queue's events */
244 if (evts->num_used)
246 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
248 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
249 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
251 /* if two update events overlap, collapse them into a single one */
252 if (last->u.update.bottom + 1 >= evt->u.update.top &&
253 evt->u.update.bottom + 1 >= last->u.update.top)
255 last->u.update.top = min(last->u.update.top, evt->u.update.top);
256 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
257 collapsed = TRUE;
261 if (!collapsed)
263 if (evts->num_used == evts->num_alloc)
265 evts->num_alloc += 16;
266 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
267 assert(evts->events);
269 evts->events[evts->num_used++] = *evt;
271 wake_up( &evts->obj, 0 );
274 /* retrieves events from the console's renderer events list */
275 static void console_input_events_get( struct console_input_events* evts )
277 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
279 if (num > evts->num_used) num = evts->num_used;
280 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
281 if (num < evts->num_used)
283 memmove( &evts->events[0], &evts->events[num],
284 (evts->num_used - num) * sizeof(evts->events[0]) );
286 evts->num_used -= num;
289 static struct console_input_events *create_console_input_events(void)
291 struct console_input_events* evt;
293 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
294 evt->num_alloc = evt->num_used = 0;
295 evt->events = NULL;
296 return evt;
299 static struct object *create_console_input( struct thread* renderer, int fd )
301 struct console_input *console_input;
303 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
304 console_input->renderer = renderer;
305 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
306 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
307 console_input->num_proc = 0;
308 console_input->active = NULL;
309 console_input->recnum = 0;
310 console_input->records = NULL;
311 console_input->evt = renderer ? create_console_input_events() : NULL;
312 console_input->title = NULL;
313 console_input->history_size = 50;
314 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
315 console_input->history_index = 0;
316 console_input->history_mode = 0;
317 console_input->edition_mode = 0;
318 console_input->input_cp = 0;
319 console_input->output_cp = 0;
320 console_input->win = 0;
321 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
322 console_input->fd = NULL;
324 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
326 release_object( console_input );
327 return NULL;
329 if (fd != -1) /* bare console */
331 struct termios term;
333 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
334 FILE_SYNCHRONOUS_IO_NONALERT )))
336 release_object( console_input );
337 return NULL;
339 if (tcgetattr(fd, &term) < 0)
341 release_object( console_input );
342 set_error( STATUS_INVALID_HANDLE );
343 return NULL;
345 console_input->termios = term;
346 term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
347 term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
348 term.c_cflag &= ~(CSIZE | PARENB);
349 term.c_cflag |= CS8;
350 /* FIXME: we should actually disable output processing here
351 * and let kernel32/console.c do the job (with support of enable/disable of
352 * processed output)
354 /* term.c_oflag &= ~(OPOST); */
355 term.c_cc[VMIN] = 1;
356 term.c_cc[VTIME] = 0;
357 if (tcsetattr(fd, TCSANOW, &term) < 0)
359 release_object( console_input );
360 set_error( STATUS_INVALID_HANDLE );
361 return NULL;
363 allow_fd_caching( console_input->fd );
366 return &console_input->obj;
369 static void generate_sb_initial_events( struct console_input *console_input )
371 struct screen_buffer *screen_buffer = console_input->active;
372 struct console_renderer_event evt;
374 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
375 memset(&evt.u, 0, sizeof(evt.u));
376 console_input_events_append( console_input, &evt );
378 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
379 evt.u.resize.width = screen_buffer->width;
380 evt.u.resize.height = screen_buffer->height;
381 console_input_events_append( console_input, &evt );
383 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
384 evt.u.display.left = screen_buffer->win.left;
385 evt.u.display.top = screen_buffer->win.top;
386 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
387 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
388 console_input_events_append( console_input, &evt );
390 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
391 evt.u.update.top = 0;
392 evt.u.update.bottom = screen_buffer->height - 1;
393 console_input_events_append( console_input, &evt );
395 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
396 evt.u.cursor_geom.size = screen_buffer->cursor_size;
397 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
398 console_input_events_append( console_input, &evt );
400 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
401 evt.u.cursor_pos.x = screen_buffer->cursor_x;
402 evt.u.cursor_pos.y = screen_buffer->cursor_y;
403 console_input_events_append( console_input, &evt );
406 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
408 struct screen_buffer *screen_buffer;
409 int i;
411 if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
412 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
413 screen_buffer->input = console_input;
414 screen_buffer->cursor_size = 100;
415 screen_buffer->cursor_visible = 1;
416 screen_buffer->width = 80;
417 screen_buffer->height = 150;
418 screen_buffer->max_width = 80;
419 screen_buffer->max_height = 25;
420 screen_buffer->cursor_x = 0;
421 screen_buffer->cursor_y = 0;
422 screen_buffer->attr = 0x0F;
423 screen_buffer->win.left = 0;
424 screen_buffer->win.right = screen_buffer->max_width - 1;
425 screen_buffer->win.top = 0;
426 screen_buffer->win.bottom = screen_buffer->max_height - 1;
427 if (fd == -1)
428 screen_buffer->fd = NULL;
429 else
431 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
432 FILE_SYNCHRONOUS_IO_NONALERT )))
434 release_object( screen_buffer );
435 return NULL;
437 allow_fd_caching(screen_buffer->fd);
440 list_add_head( &screen_buffer_list, &screen_buffer->entry );
442 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
443 sizeof(*screen_buffer->data) )))
445 release_object( screen_buffer );
446 return NULL;
448 /* clear the first row */
449 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
450 /* and copy it to all other rows */
451 for (i = 1; i < screen_buffer->height; i++)
452 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
453 screen_buffer->width * sizeof(char_info_t) );
455 if (!console_input->active)
457 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
458 generate_sb_initial_events( console_input );
460 return screen_buffer;
463 /* free the console for this process */
464 int free_console( struct process *process )
466 struct console_input* console = process->console;
468 if (!console) return 0;
470 process->console = NULL;
471 if (--console->num_proc == 0 && console->renderer)
473 /* all processes have terminated... tell the renderer to terminate too */
474 struct console_renderer_event evt;
475 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
476 memset(&evt.u, 0, sizeof(evt.u));
477 console_input_events_append( console, &evt );
479 release_object( console );
481 return 1;
484 /* let process inherit the console from parent... this handle two cases :
485 * 1/ generic console inheritance
486 * 2/ parent is a renderer which launches process, and process should attach to the console
487 * renderered by parent
489 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
491 int done = 0;
492 struct process* parent = parent_thread->process;
494 /* if parent is a renderer, then attach current process to its console
495 * a bit hacky....
497 if (hconin)
499 struct console_input* console;
501 /* FIXME: should we check some access rights ? */
502 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
503 0, &console_input_ops )))
505 if (console->renderer == parent_thread)
507 process->console = (struct console_input*)grab_object( console );
508 process->console->num_proc++;
509 done = 1;
511 release_object( console );
513 else clear_error(); /* ignore error */
515 /* otherwise, if parent has a console, attach child to this console */
516 if (!done && parent->console)
518 process->console = (struct console_input*)grab_object( parent->console );
519 process->console->num_proc++;
523 struct thread *console_get_renderer( struct console_input *console )
525 return console->renderer;
528 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
530 struct console_input* console = NULL;
532 if (handle)
533 console = (struct console_input *)get_handle_obj( current->process, handle,
534 access, &console_input_ops );
535 else if (current->process->console)
537 console = (struct console_input *)grab_object( current->process->console );
540 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
541 return console;
544 struct console_signal_info
546 struct console_input *console;
547 process_id_t group;
548 int signal;
551 static int propagate_console_signal_cb(struct process *process, void *user)
553 struct console_signal_info* csi = (struct console_signal_info*)user;
555 if (process->console == csi->console && process->running_threads &&
556 (!csi->group || process->group_id == csi->group))
558 /* find a suitable thread to signal */
559 struct thread *thread;
560 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
562 if (send_thread_signal( thread, csi->signal )) break;
565 return FALSE;
568 static void propagate_console_signal( struct console_input *console,
569 int sig, process_id_t group_id )
571 struct console_signal_info csi;
573 if (!console)
575 set_error( STATUS_INVALID_PARAMETER );
576 return;
578 /* FIXME: should support the other events (like CTRL_BREAK) */
579 if (sig != CTRL_C_EVENT)
581 set_error( STATUS_NOT_IMPLEMENTED );
582 return;
584 csi.console = console;
585 csi.signal = SIGINT;
586 csi.group = group_id;
588 enum_processes(propagate_console_signal_cb, &csi);
591 static int get_console_mode( obj_handle_t handle, int *bare )
593 struct object *obj;
594 int ret = 0;
596 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
598 if (obj->ops == &console_input_ops)
600 ret = ((struct console_input *)obj)->mode;
601 *bare = console_input_is_bare((struct console_input *)obj);
603 else if (obj->ops == &screen_buffer_ops)
605 ret = ((struct screen_buffer *)obj)->mode;
606 *bare = console_input_is_bare(((struct screen_buffer *)obj)->input);
608 else
609 set_error( STATUS_OBJECT_TYPE_MISMATCH );
610 release_object( obj );
612 return ret;
615 /* changes the mode of either a console input or a screen buffer */
616 static int set_console_mode( obj_handle_t handle, int mode )
618 struct object *obj;
619 int ret = 0;
621 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
622 return 0;
623 if (obj->ops == &console_input_ops)
625 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
626 ((struct console_input *)obj)->mode = mode;
627 ret = 1;
629 else if (obj->ops == &screen_buffer_ops)
631 ((struct screen_buffer *)obj)->mode = mode;
632 ret = 1;
634 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
635 release_object( obj );
636 return ret;
639 /* add input events to a console input queue */
640 static int write_console_input( struct console_input* console, int count,
641 const INPUT_RECORD *records )
643 INPUT_RECORD *new_rec;
645 if (!count) return 0;
646 if (!(new_rec = realloc( console->records,
647 (console->recnum + count) * sizeof(INPUT_RECORD) )))
649 set_error( STATUS_NO_MEMORY );
650 release_object( console );
651 return -1;
653 console->records = new_rec;
654 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
656 if (console->mode & ENABLE_PROCESSED_INPUT)
658 int i = 0;
659 while (i < count)
661 if (records[i].EventType == KEY_EVENT &&
662 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
663 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
665 if (i != count - 1)
666 memcpy( &console->records[console->recnum + i],
667 &console->records[console->recnum + i + 1],
668 (count - i - 1) * sizeof(INPUT_RECORD) );
669 count--;
670 if (records[i].Event.KeyEvent.bKeyDown)
672 /* send SIGINT to all processes attached to this console */
673 propagate_console_signal( console, CTRL_C_EVENT, 0 );
676 else i++;
679 if (!console->recnum && count) set_event( console->event );
680 console->recnum += count;
681 return count;
684 /* retrieve a pointer to the console input records */
685 static int read_console_input( obj_handle_t handle, int count, int flush )
687 struct console_input *console;
689 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
690 FILE_READ_DATA, &console_input_ops )))
691 return -1;
693 if (!count)
695 /* special case: do not retrieve anything, but return
696 * the total number of records available */
697 count = console->recnum;
699 else
701 if (count > console->recnum) count = console->recnum;
702 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
704 if (flush)
706 int i;
707 for (i = count; i < console->recnum; i++)
708 console->records[i-count] = console->records[i];
709 if ((console->recnum -= count) > 0)
711 INPUT_RECORD *new_rec = realloc( console->records,
712 console->recnum * sizeof(INPUT_RECORD) );
713 if (new_rec) console->records = new_rec;
715 else
717 free( console->records );
718 console->records = NULL;
719 reset_event( console->event );
722 release_object( console );
723 return count;
726 /* set misc console input information */
727 static int set_console_input_info( const struct set_console_input_info_request *req,
728 const WCHAR *title, data_size_t len )
730 struct console_input *console;
731 struct console_renderer_event evt;
733 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
734 if (console_input_is_bare(console) &&
735 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
736 SET_CONSOLE_INPUT_INFO_WIN)))
738 set_error( STATUS_UNSUCCESSFUL );
739 goto error;
742 memset(&evt.u, 0, sizeof(evt.u));
743 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
745 struct screen_buffer *screen_buffer;
747 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
748 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
749 if (!screen_buffer || screen_buffer->input != console)
751 set_error( STATUS_INVALID_HANDLE );
752 if (screen_buffer) release_object( screen_buffer );
753 goto error;
756 if (screen_buffer != console->active)
758 if (console->active) release_object( console->active );
759 console->active = screen_buffer;
760 generate_sb_initial_events( console );
762 else
763 release_object( screen_buffer );
765 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
767 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
768 if (new_title)
770 memcpy( new_title, title, len );
771 new_title[len / sizeof(WCHAR)] = 0;
772 free( console->title );
773 console->title = new_title;
774 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
775 console_input_events_append( console, &evt );
778 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
780 console->history_mode = req->history_mode;
782 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
783 console->history_size != req->history_size)
785 WCHAR** mem = NULL;
786 int i;
787 int delta;
789 if (req->history_size)
791 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
792 if (!mem) goto error;
793 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
796 delta = (console->history_index > req->history_size) ?
797 (console->history_index - req->history_size) : 0;
799 for (i = delta; i < console->history_index; i++)
801 mem[i - delta] = console->history[i];
802 console->history[i] = NULL;
804 console->history_index -= delta;
806 for (i = 0; i < console->history_size; i++)
807 free( console->history[i] );
808 free( console->history );
809 console->history = mem;
810 console->history_size = req->history_size;
812 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
814 console->edition_mode = req->edition_mode;
816 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
818 console->input_cp = req->input_cp;
820 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
822 console->output_cp = req->output_cp;
824 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
826 console->win = req->win;
828 release_object( console );
829 return 1;
830 error:
831 if (console) release_object( console );
832 return 0;
835 /* resize a screen buffer */
836 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
837 int new_width, int new_height )
839 int i, old_width, old_height, copy_width, copy_height;
840 char_info_t *new_data;
842 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
844 set_error( STATUS_NO_MEMORY );
845 return 0;
847 old_width = screen_buffer->width;
848 old_height = screen_buffer->height;
849 copy_width = min( old_width, new_width );
850 copy_height = min( old_height, new_height );
852 /* copy all the rows */
853 for (i = 0; i < copy_height; i++)
855 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
856 copy_width * sizeof(char_info_t) );
859 /* clear the end of each row */
860 if (new_width > old_width)
862 /* fill first row */
863 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
864 /* and blast it to the other rows */
865 for (i = 1; i < copy_height; i++)
866 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
867 (new_width - old_width) * sizeof(char_info_t) );
870 /* clear remaining rows */
871 if (new_height > old_height)
873 /* fill first row */
874 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
875 /* and blast it to the other rows */
876 for (i = old_height+1; i < new_height; i++)
877 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
878 new_width * sizeof(char_info_t) );
880 free( screen_buffer->data );
881 screen_buffer->data = new_data;
882 screen_buffer->width = new_width;
883 screen_buffer->height = new_height;
884 return 1;
887 /* set misc screen buffer information */
888 static int set_console_output_info( struct screen_buffer *screen_buffer,
889 const struct set_console_output_info_request *req )
891 struct console_renderer_event evt;
893 memset(&evt.u, 0, sizeof(evt.u));
894 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
896 if (req->cursor_size < 1 || req->cursor_size > 100)
898 set_error( STATUS_INVALID_PARAMETER );
899 return 0;
901 if (screen_buffer->cursor_size != req->cursor_size ||
902 screen_buffer->cursor_visible != req->cursor_visible)
904 screen_buffer->cursor_size = req->cursor_size;
905 screen_buffer->cursor_visible = req->cursor_visible;
906 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
907 evt.u.cursor_geom.size = req->cursor_size;
908 evt.u.cursor_geom.visible = req->cursor_visible;
909 console_input_events_append( screen_buffer->input, &evt );
912 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
914 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
915 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
917 set_error( STATUS_INVALID_PARAMETER );
918 return 0;
920 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
922 screen_buffer->cursor_x = req->cursor_x;
923 screen_buffer->cursor_y = req->cursor_y;
924 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
925 evt.u.cursor_pos.x = req->cursor_x;
926 evt.u.cursor_pos.y = req->cursor_y;
927 console_input_events_append( screen_buffer->input, &evt );
930 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
932 unsigned cc;
934 /* new screen-buffer cannot be smaller than actual window */
935 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
936 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
938 set_error( STATUS_INVALID_PARAMETER );
939 return 0;
941 /* FIXME: there are also some basic minimum and max size to deal with */
942 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
944 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
945 evt.u.resize.width = req->width;
946 evt.u.resize.height = req->height;
947 console_input_events_append( screen_buffer->input, &evt );
949 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
950 evt.u.update.top = 0;
951 evt.u.update.bottom = screen_buffer->height - 1;
952 console_input_events_append( screen_buffer->input, &evt );
954 /* scroll window to display sb */
955 if (screen_buffer->win.right >= req->width)
957 screen_buffer->win.right -= screen_buffer->win.left;
958 screen_buffer->win.left = 0;
960 if (screen_buffer->win.bottom >= req->height)
962 screen_buffer->win.bottom -= screen_buffer->win.top;
963 screen_buffer->win.top = 0;
965 /* reset cursor if needed (normally, if cursor was outside of new sb, the
966 * window has been shifted so that the new position of the cursor will be
967 * visible */
968 cc = 0;
969 if (screen_buffer->cursor_x >= req->width)
971 screen_buffer->cursor_x = req->width - 1;
972 cc++;
974 if (screen_buffer->cursor_y >= req->height)
976 screen_buffer->cursor_y = req->height - 1;
977 cc++;
979 if (cc)
981 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
982 evt.u.cursor_pos.x = req->cursor_x;
983 evt.u.cursor_pos.y = req->cursor_y;
984 console_input_events_append( screen_buffer->input, &evt );
987 if (screen_buffer == screen_buffer->input->active &&
988 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
990 INPUT_RECORD ir;
991 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
992 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
993 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
994 write_console_input( screen_buffer->input, 1, &ir );
997 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
999 screen_buffer->attr = req->attr;
1001 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
1003 if (req->win_left < 0 || req->win_left > req->win_right ||
1004 req->win_right >= screen_buffer->width ||
1005 req->win_top < 0 || req->win_top > req->win_bottom ||
1006 req->win_bottom >= screen_buffer->height)
1008 set_error( STATUS_INVALID_PARAMETER );
1009 return 0;
1011 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
1012 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1014 screen_buffer->win.left = req->win_left;
1015 screen_buffer->win.top = req->win_top;
1016 screen_buffer->win.right = req->win_right;
1017 screen_buffer->win.bottom = req->win_bottom;
1018 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1019 evt.u.display.left = req->win_left;
1020 evt.u.display.top = req->win_top;
1021 evt.u.display.width = req->win_right - req->win_left + 1;
1022 evt.u.display.height = req->win_bottom - req->win_top + 1;
1023 console_input_events_append( screen_buffer->input, &evt );
1026 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1028 /* can only be done by renderer */
1029 if (current->process->console != screen_buffer->input)
1031 set_error( STATUS_INVALID_PARAMETER );
1032 return 0;
1035 screen_buffer->max_width = req->max_width;
1036 screen_buffer->max_height = req->max_height;
1039 return 1;
1042 /* appends a new line to history (history is a fixed size array) */
1043 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1045 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1047 if (!ptr)
1048 return;
1050 if (!console || !console->history_size)
1052 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1053 free( ptr );
1054 return;
1057 memcpy( ptr, buf, len * sizeof(WCHAR) );
1058 ptr[len] = 0;
1060 if (console->history_mode && console->history_index &&
1061 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
1063 /* ok, mode ask us to not use twice the same string...
1064 * so just free mem and returns
1066 set_error( STATUS_ALIAS_EXISTS );
1067 free(ptr);
1068 return;
1071 if (console->history_index < console->history_size)
1073 console->history[console->history_index++] = ptr;
1075 else
1077 free( console->history[0]) ;
1078 memmove( &console->history[0], &console->history[1],
1079 (console->history_size - 1) * sizeof(WCHAR*) );
1080 console->history[console->history_size - 1] = ptr;
1084 /* returns a line from the cache */
1085 static data_size_t console_input_get_hist( struct console_input *console, int index )
1087 data_size_t ret = 0;
1089 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1090 else
1092 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1093 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1095 return ret;
1098 /* dumb dump */
1099 static void console_input_dump( struct object *obj, int verbose )
1101 struct console_input *console = (struct console_input *)obj;
1102 assert( obj->ops == &console_input_ops );
1103 fprintf( stderr, "Console input active=%p evt=%p\n",
1104 console->active, console->evt );
1107 static void console_input_destroy( struct object *obj )
1109 struct console_input* console_in = (struct console_input *)obj;
1110 struct screen_buffer* curr;
1111 int i;
1113 assert( obj->ops == &console_input_ops );
1114 free( console_in->title );
1115 free( console_in->records );
1117 if (console_in->active) release_object( console_in->active );
1118 console_in->active = NULL;
1120 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1122 if (curr->input == console_in) curr->input = NULL;
1125 if (console_in->evt)
1127 release_object( console_in->evt );
1128 console_in->evt = NULL;
1130 release_object( console_in->event );
1131 if (console_in->fd)
1133 tcsetattr(get_unix_fd(console_in->fd), TCSANOW, &console_in->termios);
1134 release_object( console_in->fd );
1137 for (i = 0; i < console_in->history_size; i++)
1138 free( console_in->history[i] );
1139 free( console_in->history );
1142 static void screen_buffer_dump( struct object *obj, int verbose )
1144 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1145 assert( obj->ops == &screen_buffer_ops );
1147 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1150 static void screen_buffer_destroy( struct object *obj )
1152 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1154 assert( obj->ops == &screen_buffer_ops );
1156 list_remove( &screen_buffer->entry );
1158 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1160 struct screen_buffer *sb;
1162 screen_buffer->input->active = NULL;
1163 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1165 if (sb->input == screen_buffer->input)
1167 sb->input->active = sb;
1168 break;
1172 if (screen_buffer->fd) release_object( screen_buffer->fd );
1173 free( screen_buffer->data );
1176 static struct fd *screen_buffer_get_fd( struct object *obj )
1178 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1179 assert( obj->ops == &screen_buffer_ops );
1180 return screen_buffer->fd ? (struct fd*)grab_object( screen_buffer->fd ) : NULL;
1183 /* write data into a screen buffer */
1184 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1185 const void* data, enum char_info_mode mode,
1186 int x, int y, int wrap )
1188 unsigned int i;
1189 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1191 if (y >= screen_buffer->height) return 0;
1193 if (wrap)
1194 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1195 else
1196 end = screen_buffer->data + (y+1) * screen_buffer->width;
1198 switch(mode)
1200 case CHAR_INFO_MODE_TEXT:
1202 const WCHAR *ptr = data;
1203 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1205 break;
1206 case CHAR_INFO_MODE_ATTR:
1208 const unsigned short *ptr = data;
1209 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1211 break;
1212 case CHAR_INFO_MODE_TEXTATTR:
1214 const char_info_t *ptr = data;
1215 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1217 break;
1218 case CHAR_INFO_MODE_TEXTSTDATTR:
1220 const WCHAR *ptr = data;
1221 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1223 dest->ch = ptr[i];
1224 dest->attr = screen_buffer->attr;
1227 break;
1228 default:
1229 set_error( STATUS_INVALID_PARAMETER );
1230 return 0;
1233 if (i && screen_buffer == screen_buffer->input->active)
1235 struct console_renderer_event evt;
1236 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1237 memset(&evt.u, 0, sizeof(evt.u));
1238 evt.u.update.top = y + x / screen_buffer->width;
1239 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1240 console_input_events_append( screen_buffer->input, &evt );
1242 return i;
1245 /* fill a screen buffer with uniform data */
1246 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1247 enum char_info_mode mode, int x, int y, int count, int wrap )
1249 int i;
1250 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1252 if (y >= screen_buffer->height) return 0;
1254 if (wrap)
1255 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1256 else
1257 end = screen_buffer->data + (y+1) * screen_buffer->width;
1259 if (count > end - dest) count = end - dest;
1261 switch(mode)
1263 case CHAR_INFO_MODE_TEXT:
1264 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1265 break;
1266 case CHAR_INFO_MODE_ATTR:
1267 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1268 break;
1269 case CHAR_INFO_MODE_TEXTATTR:
1270 for (i = 0; i < count; i++) dest[i] = data;
1271 break;
1272 case CHAR_INFO_MODE_TEXTSTDATTR:
1273 for (i = 0; i < count; i++)
1275 dest[i].ch = data.ch;
1276 dest[i].attr = screen_buffer->attr;
1278 break;
1279 default:
1280 set_error( STATUS_INVALID_PARAMETER );
1281 return 0;
1284 if (count && screen_buffer == screen_buffer->input->active)
1286 struct console_renderer_event evt;
1287 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1288 memset(&evt.u, 0, sizeof(evt.u));
1289 evt.u.update.top = y;
1290 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1291 console_input_events_append( screen_buffer->input, &evt );
1293 return i;
1296 /* read data from a screen buffer */
1297 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1298 enum char_info_mode mode, int wrap )
1300 int i;
1301 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1303 if (y >= screen_buffer->height) return;
1305 if (wrap)
1306 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1307 else
1308 end = screen_buffer->data + (y+1) * screen_buffer->width;
1310 switch(mode)
1312 case CHAR_INFO_MODE_TEXT:
1314 WCHAR *data;
1315 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1316 if ((data = set_reply_data_size( count * sizeof(*data) )))
1318 for (i = 0; i < count; i++) data[i] = src[i].ch;
1321 break;
1322 case CHAR_INFO_MODE_ATTR:
1324 unsigned short *data;
1325 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1326 if ((data = set_reply_data_size( count * sizeof(*data) )))
1328 for (i = 0; i < count; i++) data[i] = src[i].attr;
1331 break;
1332 case CHAR_INFO_MODE_TEXTATTR:
1334 char_info_t *data;
1335 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1336 if ((data = set_reply_data_size( count * sizeof(*data) )))
1338 for (i = 0; i < count; i++) data[i] = src[i];
1341 break;
1342 default:
1343 set_error( STATUS_INVALID_PARAMETER );
1344 break;
1348 /* scroll parts of a screen buffer */
1349 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1350 int w, int h )
1352 int j;
1353 char_info_t *psrc, *pdst;
1354 struct console_renderer_event evt;
1356 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1357 xsrc + w > screen_buffer->width ||
1358 xdst + w > screen_buffer->width ||
1359 ysrc + h > screen_buffer->height ||
1360 ydst + h > screen_buffer->height ||
1361 w == 0 || h == 0)
1363 set_error( STATUS_INVALID_PARAMETER );
1364 return;
1367 if (ysrc < ydst)
1369 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1370 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1372 for (j = h; j > 0; j--)
1374 memcpy(pdst, psrc, w * sizeof(*pdst) );
1375 pdst -= screen_buffer->width;
1376 psrc -= screen_buffer->width;
1379 else
1381 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1382 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1384 for (j = 0; j < h; j++)
1386 /* we use memmove here because when psrc and pdst are the same,
1387 * copies are done on the same row, so the dst and src blocks
1388 * can overlap */
1389 memmove( pdst, psrc, w * sizeof(*pdst) );
1390 pdst += screen_buffer->width;
1391 psrc += screen_buffer->width;
1395 /* FIXME: this could be enhanced, by signalling scroll */
1396 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1397 memset(&evt.u, 0, sizeof(evt.u));
1398 evt.u.update.top = min(ysrc, ydst);
1399 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1400 console_input_events_append( screen_buffer->input, &evt );
1403 /* allocate a console for the renderer */
1404 DECL_HANDLER(alloc_console)
1406 obj_handle_t in = 0;
1407 obj_handle_t evt = 0;
1408 struct process *process;
1409 struct thread *renderer;
1410 struct console_input *console;
1411 int fd;
1413 switch (req->pid)
1415 case 0:
1416 /* renderer is current, console to be attached to parent process */
1417 renderer = current;
1418 if (!(process = current->process->parent))
1420 set_error( STATUS_ACCESS_DENIED );
1421 return;
1423 grab_object( process );
1424 break;
1425 case 0xffffffff:
1426 /* no renderer, console to be attached to current process */
1427 renderer = NULL;
1428 process = current->process;
1429 grab_object( process );
1430 break;
1431 default:
1432 /* renderer is current, console to be attached to req->pid */
1433 renderer = current;
1434 if (!(process = get_process_from_id( req->pid ))) return;
1437 if (process != current->process && process->console)
1439 set_error( STATUS_ACCESS_DENIED );
1440 goto the_end;
1442 if (req->input_fd != -1)
1444 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1446 set_error( STATUS_INVALID_PARAMETER );
1447 goto the_end;
1450 else fd = -1;
1452 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1454 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1456 if (!console->evt ||
1457 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1459 process->console = (struct console_input*)grab_object( console );
1460 console->num_proc++;
1461 reply->handle_in = in;
1462 reply->event = evt;
1463 release_object( console );
1464 goto the_end;
1466 close_handle( current->process, in );
1468 free_console( process );
1470 the_end:
1471 release_object( process );
1474 /* free the console of the current process */
1475 DECL_HANDLER(free_console)
1477 free_console( current->process );
1480 /* let the renderer peek the events it's waiting on */
1481 DECL_HANDLER(get_console_renderer_events)
1483 struct console_input_events *evt;
1485 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1486 FILE_READ_PROPERTIES, &console_input_events_ops );
1487 if (!evt) return;
1488 console_input_events_get( evt );
1489 release_object( evt );
1492 /* open a handle to the process console */
1493 DECL_HANDLER(open_console)
1495 struct object *obj = NULL;
1497 reply->handle = 0;
1498 if (!req->from)
1500 if (current->process->console)
1501 obj = grab_object( (struct object*)current->process->console );
1503 else if (req->from == (obj_handle_t)1)
1505 if (current->process->console && current->process->console->active)
1506 obj = grab_object( (struct object*)current->process->console->active );
1508 else if ((obj = get_handle_obj( current->process, req->from,
1509 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1511 struct console_input *console = (struct console_input *)obj;
1512 obj = (console->active) ? grab_object( console->active ) : NULL;
1513 release_object( console );
1516 /* FIXME: req->share is not used (as in screen buffer creation) */
1517 if (obj)
1519 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1520 release_object( obj );
1522 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1525 /* set info about a console input */
1526 DECL_HANDLER(set_console_input_info)
1528 set_console_input_info( req, get_req_data(), get_req_data_size() );
1531 /* get info about a console (output only) */
1532 DECL_HANDLER(get_console_input_info)
1534 struct console_input *console;
1536 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1537 if (console->title)
1539 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1540 if (len > get_reply_max_size()) len = get_reply_max_size();
1541 set_reply_data( console->title, len );
1543 reply->history_mode = console->history_mode;
1544 reply->history_size = console->history_size;
1545 reply->history_index = console->history_index;
1546 reply->edition_mode = console->edition_mode;
1547 reply->input_cp = console->input_cp;
1548 reply->output_cp = console->output_cp;
1549 reply->win = console->win;
1551 release_object( console );
1554 /* get a console mode (input or output) */
1555 DECL_HANDLER(get_console_mode)
1557 reply->mode = get_console_mode( req->handle, &reply->is_bare );
1560 /* set a console mode (input or output) */
1561 DECL_HANDLER(set_console_mode)
1563 set_console_mode( req->handle, req->mode );
1566 /* add input records to a console input queue */
1567 DECL_HANDLER(write_console_input)
1569 struct console_input *console;
1571 reply->written = 0;
1572 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1573 FILE_WRITE_PROPERTIES, &console_input_ops )))
1574 return;
1575 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1576 get_req_data() );
1577 release_object( console );
1580 /* fetch input records from a console input queue */
1581 DECL_HANDLER(read_console_input)
1583 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1584 reply->read = read_console_input( req->handle, count, req->flush );
1587 /* appends a string to console's history */
1588 DECL_HANDLER(append_console_input_history)
1590 struct console_input *console;
1592 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1593 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1594 release_object( console );
1597 /* appends a string to console's history */
1598 DECL_HANDLER(get_console_input_history)
1600 struct console_input *console;
1602 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1603 reply->total = console_input_get_hist( console, req->index );
1604 release_object( console );
1607 /* creates a screen buffer */
1608 DECL_HANDLER(create_console_output)
1610 struct console_input* console;
1611 struct screen_buffer* screen_buffer;
1612 int fd;
1614 if (req->fd != -1)
1616 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1618 set_error( STATUS_INVALID_HANDLE );
1619 return;
1622 else fd = -1;
1623 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1625 close(fd);
1626 return;
1628 if (console_input_is_bare( console ) ^ (fd != -1))
1630 close( fd );
1631 set_error( STATUS_INVALID_HANDLE );
1632 return;
1635 screen_buffer = create_console_output( console, fd );
1636 if (screen_buffer)
1638 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1639 * see file.c on how this could be done */
1640 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1641 release_object( screen_buffer );
1643 release_object( console );
1646 /* set info about a console screen buffer */
1647 DECL_HANDLER(set_console_output_info)
1649 struct screen_buffer *screen_buffer;
1651 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1652 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1654 set_console_output_info( screen_buffer, req );
1655 release_object( screen_buffer );
1659 /* get info about a console screen buffer */
1660 DECL_HANDLER(get_console_output_info)
1662 struct screen_buffer *screen_buffer;
1664 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1665 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1667 reply->cursor_size = screen_buffer->cursor_size;
1668 reply->cursor_visible = screen_buffer->cursor_visible;
1669 reply->cursor_x = screen_buffer->cursor_x;
1670 reply->cursor_y = screen_buffer->cursor_y;
1671 reply->width = screen_buffer->width;
1672 reply->height = screen_buffer->height;
1673 reply->attr = screen_buffer->attr;
1674 reply->win_left = screen_buffer->win.left;
1675 reply->win_top = screen_buffer->win.top;
1676 reply->win_right = screen_buffer->win.right;
1677 reply->win_bottom = screen_buffer->win.bottom;
1678 reply->max_width = screen_buffer->max_width;
1679 reply->max_height = screen_buffer->max_height;
1680 release_object( screen_buffer );
1684 /* read data (chars & attrs) from a screen buffer */
1685 DECL_HANDLER(read_console_output)
1687 struct screen_buffer *screen_buffer;
1689 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1690 FILE_READ_DATA, &screen_buffer_ops )))
1692 if (console_input_is_bare( screen_buffer->input ))
1694 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1695 release_object( screen_buffer );
1696 return;
1698 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1699 reply->width = screen_buffer->width;
1700 reply->height = screen_buffer->height;
1701 release_object( screen_buffer );
1705 /* write data (char and/or attrs) to a screen buffer */
1706 DECL_HANDLER(write_console_output)
1708 struct screen_buffer *screen_buffer;
1710 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1711 FILE_WRITE_DATA, &screen_buffer_ops)))
1713 if (console_input_is_bare( screen_buffer->input ))
1715 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1716 release_object( screen_buffer );
1717 return;
1719 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1720 req->mode, req->x, req->y, req->wrap );
1721 reply->width = screen_buffer->width;
1722 reply->height = screen_buffer->height;
1723 release_object( screen_buffer );
1727 /* fill a screen buffer with constant data (chars and/or attributes) */
1728 DECL_HANDLER(fill_console_output)
1730 struct screen_buffer *screen_buffer;
1732 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1733 FILE_WRITE_DATA, &screen_buffer_ops)))
1735 if (console_input_is_bare( screen_buffer->input ))
1737 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1738 release_object( screen_buffer );
1739 return;
1741 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1742 req->x, req->y, req->count, req->wrap );
1743 release_object( screen_buffer );
1747 /* move a rect of data in a screen buffer */
1748 DECL_HANDLER(move_console_output)
1750 struct screen_buffer *screen_buffer;
1752 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1753 FILE_WRITE_DATA, &screen_buffer_ops)))
1755 if (console_input_is_bare( screen_buffer->input ))
1757 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1758 release_object( screen_buffer );
1759 return;
1761 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1762 req->w, req->h );
1763 release_object( screen_buffer );
1767 /* sends a signal to a console (process, group...) */
1768 DECL_HANDLER(send_console_signal)
1770 process_id_t group;
1772 group = req->group_id ? req->group_id : current->process->group_id;
1774 if (!group)
1775 set_error( STATUS_INVALID_PARAMETER );
1776 else
1777 propagate_console_signal( current->process->console, req->signal, group );
1780 /* get console which renderer is 'current' */
1781 static int cgwe_enum( struct process* process, void* user)
1783 if (process->console && process->console->renderer == current)
1785 *(struct console_input**)user = process->console;
1786 return 1;
1788 return 0;
1791 DECL_HANDLER(get_console_wait_event)
1793 struct console_input* console = NULL;
1795 if (current->process->console)
1796 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1797 else enum_processes(cgwe_enum, &console);
1799 if (console)
1801 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1802 release_object( console );
1804 else set_error( STATUS_INVALID_PARAMETER );