webservices: Implement WsCreateXmlBuffer.
[wine.git] / server / console.c
blob10b135017a7904c79c10198b49fcb6e2cf374f20
1 /*
2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
5 * 2001 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "handle.h"
35 #include "process.h"
36 #include "request.h"
37 #include "file.h"
38 #include "unicode.h"
39 #include "wincon.h"
40 #include "winternl.h"
42 struct screen_buffer;
43 struct console_input_events;
45 struct console_input
47 struct object obj; /* object header */
48 int num_proc; /* number of processes attached to this console */
49 struct thread *renderer; /* console renderer thread */
50 int mode; /* input mode */
51 struct screen_buffer *active; /* active screen buffer */
52 int recnum; /* number of input records */
53 INPUT_RECORD *records; /* input records */
54 struct console_input_events *evt; /* synchronization event with renderer */
55 WCHAR *title; /* console title */
56 WCHAR **history; /* lines history */
57 int history_size; /* number of entries in history array */
58 int history_index; /* number of used entries in history array */
59 int history_mode; /* mode of history (non zero means remove doubled strings */
60 int edition_mode; /* index to edition mode flavors */
61 int input_cp; /* console input codepage */
62 int output_cp; /* console output codepage */
63 user_handle_t win; /* window handle if backend supports it */
64 struct event *event; /* event to wait on for input queue */
65 struct fd *fd; /* for bare console, attached input fd */
68 static void console_input_dump( struct object *obj, int verbose );
69 static void console_input_destroy( struct object *obj );
70 static struct fd *console_input_get_fd( struct object *obj );
72 static const struct object_ops console_input_ops =
74 sizeof(struct console_input), /* size */
75 console_input_dump, /* dump */
76 no_get_type, /* get_type */
77 no_add_queue, /* add_queue */
78 NULL, /* remove_queue */
79 NULL, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 console_input_get_fd, /* get_fd */
83 default_fd_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_open_file, /* open_file */
88 no_close_handle, /* close_handle */
89 console_input_destroy /* destroy */
92 static void console_input_events_dump( struct object *obj, int verbose );
93 static void console_input_events_destroy( struct object *obj );
94 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
96 struct console_input_events
98 struct object obj; /* object header */
99 int num_alloc; /* number of allocated events */
100 int num_used; /* number of actually used events */
101 struct console_renderer_event* events;
104 static const struct object_ops console_input_events_ops =
106 sizeof(struct console_input_events), /* size */
107 console_input_events_dump, /* dump */
108 no_get_type, /* get_type */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 console_input_events_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 default_fd_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 no_close_handle, /* close_handle */
121 console_input_events_destroy /* destroy */
124 struct screen_buffer
126 struct object obj; /* object header */
127 struct list entry; /* entry in list of all screen buffers */
128 struct console_input *input; /* associated console input */
129 int mode; /* output mode */
130 int cursor_size; /* size of cursor (percentage filled) */
131 int cursor_visible;/* cursor visibility flag */
132 int cursor_x; /* position of cursor */
133 int cursor_y; /* position of cursor */
134 int width; /* size (w-h) of the screen buffer */
135 int height;
136 int max_width; /* size (w-h) of the window given font size */
137 int max_height;
138 char_info_t *data; /* the data for each cell - a width x height matrix */
139 unsigned short attr; /* default attribute for screen buffer */
140 rectangle_t win; /* current visible window on the screen buffer *
141 * as seen in wineconsole */
142 struct fd *fd; /* for bare console, attached output fd */
145 static void screen_buffer_dump( struct object *obj, int verbose );
146 static void screen_buffer_destroy( struct object *obj );
147 static struct fd *screen_buffer_get_fd( struct object *obj );
149 static const struct object_ops screen_buffer_ops =
151 sizeof(struct screen_buffer), /* size */
152 screen_buffer_dump, /* dump */
153 no_get_type, /* get_type */
154 no_add_queue, /* add_queue */
155 NULL, /* remove_queue */
156 NULL, /* signaled */
157 NULL, /* satisfied */
158 no_signal, /* signal */
159 screen_buffer_get_fd, /* get_fd */
160 default_fd_map_access, /* map_access */
161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
163 no_lookup_name, /* lookup_name */
164 no_open_file, /* open_file */
165 no_close_handle, /* close_handle */
166 screen_buffer_destroy /* destroy */
169 static enum server_fd_type console_get_fd_type( struct fd *fd );
171 static const struct fd_ops console_fd_ops =
173 default_fd_get_poll_events, /* get_poll_events */
174 default_poll_event, /* poll_event */
175 console_get_fd_type, /* get_fd_type */
176 no_fd_read, /* read */
177 no_fd_write, /* write */
178 no_fd_flush, /* flush */
179 default_fd_ioctl, /* ioctl */
180 default_fd_queue_async, /* queue_async */
181 default_fd_reselect_async, /* reselect_async */
182 default_fd_cancel_async /* cancel_async */
185 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
187 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
189 static int console_input_is_bare( struct console_input* cin )
191 return cin->evt == NULL;
194 static struct fd *console_input_get_fd( struct object* obj )
196 struct console_input *console_input = (struct console_input*)obj;
197 assert( obj->ops == &console_input_ops );
198 if (console_input->fd)
199 return (struct fd*)grab_object( console_input->fd );
200 set_error( STATUS_OBJECT_TYPE_MISMATCH );
201 return NULL;
204 static enum server_fd_type console_get_fd_type( struct fd *fd )
206 return FD_TYPE_CHAR;
209 /* dumps the renderer events of a console */
210 static void console_input_events_dump( struct object *obj, int verbose )
212 struct console_input_events *evts = (struct console_input_events *)obj;
213 assert( obj->ops == &console_input_events_ops );
214 fprintf( stderr, "Console input events: %d/%d events\n",
215 evts->num_used, evts->num_alloc );
218 /* destroys the renderer events of a console */
219 static void console_input_events_destroy( struct object *obj )
221 struct console_input_events *evts = (struct console_input_events *)obj;
222 assert( obj->ops == &console_input_events_ops );
223 free( evts->events );
226 /* the renderer events list is signaled when it's not empty */
227 static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
229 struct console_input_events *evts = (struct console_input_events *)obj;
230 assert( obj->ops == &console_input_events_ops );
231 return (evts->num_used != 0);
234 /* add an event to the console's renderer events list */
235 static void console_input_events_append( struct console_input* console,
236 struct console_renderer_event* evt)
238 struct console_input_events* evts;
239 int collapsed = FALSE;
241 if (!(evts = console->evt)) return;
242 /* to be done even when evt has been generated by the renderer ? */
244 /* try to collapse evt into current queue's events */
245 if (evts->num_used)
247 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
249 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
250 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
252 /* if two update events overlap, collapse them into a single one */
253 if (last->u.update.bottom + 1 >= evt->u.update.top &&
254 evt->u.update.bottom + 1 >= last->u.update.top)
256 last->u.update.top = min(last->u.update.top, evt->u.update.top);
257 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
258 collapsed = TRUE;
262 if (!collapsed)
264 if (evts->num_used == evts->num_alloc)
266 evts->num_alloc += 16;
267 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
268 assert(evts->events);
270 evts->events[evts->num_used++] = *evt;
272 wake_up( &evts->obj, 0 );
275 /* retrieves events from the console's renderer events list */
276 static void console_input_events_get( struct console_input_events* evts )
278 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
280 if (num > evts->num_used) num = evts->num_used;
281 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
282 if (num < evts->num_used)
284 memmove( &evts->events[0], &evts->events[num],
285 (evts->num_used - num) * sizeof(evts->events[0]) );
287 evts->num_used -= num;
290 static struct console_input_events *create_console_input_events(void)
292 struct console_input_events* evt;
294 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
295 evt->num_alloc = evt->num_used = 0;
296 evt->events = NULL;
297 return evt;
300 static struct object *create_console_input( struct thread* renderer, int fd )
302 struct console_input *console_input;
304 if (!(console_input = alloc_object( &console_input_ops )))
306 if (fd != -1) close( fd );
307 return NULL;
309 console_input->renderer = renderer;
310 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
311 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
312 ENABLE_EXTENDED_FLAGS;
313 console_input->num_proc = 0;
314 console_input->active = NULL;
315 console_input->recnum = 0;
316 console_input->records = NULL;
317 console_input->evt = renderer ? create_console_input_events() : NULL;
318 console_input->title = NULL;
319 console_input->history_size = 50;
320 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
321 console_input->history_index = 0;
322 console_input->history_mode = 0;
323 console_input->edition_mode = 0;
324 console_input->input_cp = 0;
325 console_input->output_cp = 0;
326 console_input->win = 0;
327 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
328 console_input->fd = NULL;
330 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
332 if (fd != -1) close( fd );
333 console_input->history_size = 0;
334 release_object( console_input );
335 return NULL;
337 if (fd != -1) /* bare console */
339 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
340 FILE_SYNCHRONOUS_IO_NONALERT )))
342 release_object( console_input );
343 return NULL;
345 allow_fd_caching( console_input->fd );
348 return &console_input->obj;
351 static void generate_sb_initial_events( struct console_input *console_input )
353 struct screen_buffer *screen_buffer = console_input->active;
354 struct console_renderer_event evt;
356 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
357 memset(&evt.u, 0, sizeof(evt.u));
358 console_input_events_append( console_input, &evt );
360 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
361 evt.u.resize.width = screen_buffer->width;
362 evt.u.resize.height = screen_buffer->height;
363 console_input_events_append( console_input, &evt );
365 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
366 evt.u.display.left = screen_buffer->win.left;
367 evt.u.display.top = screen_buffer->win.top;
368 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
369 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
370 console_input_events_append( console_input, &evt );
372 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
373 evt.u.update.top = 0;
374 evt.u.update.bottom = screen_buffer->height - 1;
375 console_input_events_append( console_input, &evt );
377 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
378 evt.u.cursor_geom.size = screen_buffer->cursor_size;
379 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
380 console_input_events_append( console_input, &evt );
382 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
383 evt.u.cursor_pos.x = screen_buffer->cursor_x;
384 evt.u.cursor_pos.y = screen_buffer->cursor_y;
385 console_input_events_append( console_input, &evt );
388 static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
390 struct screen_buffer *screen_buffer;
391 int i;
393 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
395 if (fd != -1) close( fd );
396 return NULL;
398 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
399 screen_buffer->input = console_input;
400 screen_buffer->cursor_size = 100;
401 screen_buffer->cursor_visible = 1;
402 screen_buffer->width = 80;
403 screen_buffer->height = 150;
404 screen_buffer->max_width = 80;
405 screen_buffer->max_height = 25;
406 screen_buffer->cursor_x = 0;
407 screen_buffer->cursor_y = 0;
408 screen_buffer->attr = 0x0F;
409 screen_buffer->win.left = 0;
410 screen_buffer->win.right = screen_buffer->max_width - 1;
411 screen_buffer->win.top = 0;
412 screen_buffer->win.bottom = screen_buffer->max_height - 1;
413 screen_buffer->data = NULL;
414 list_add_head( &screen_buffer_list, &screen_buffer->entry );
416 if (fd == -1)
417 screen_buffer->fd = NULL;
418 else
420 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
421 FILE_SYNCHRONOUS_IO_NONALERT )))
423 release_object( screen_buffer );
424 return NULL;
426 allow_fd_caching(screen_buffer->fd);
429 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
430 sizeof(*screen_buffer->data) )))
432 release_object( screen_buffer );
433 return NULL;
435 /* clear the first row */
436 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
437 /* and copy it to all other rows */
438 for (i = 1; i < screen_buffer->height; i++)
439 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
440 screen_buffer->width * sizeof(char_info_t) );
442 if (!console_input->active)
444 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
445 generate_sb_initial_events( console_input );
447 return screen_buffer;
450 /* free the console for this process */
451 int free_console( struct process *process )
453 struct console_input* console = process->console;
455 if (!console) return 0;
457 process->console = NULL;
458 if (--console->num_proc == 0 && console->renderer)
460 /* all processes have terminated... tell the renderer to terminate too */
461 struct console_renderer_event evt;
462 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
463 memset(&evt.u, 0, sizeof(evt.u));
464 console_input_events_append( console, &evt );
466 release_object( console );
468 return 1;
471 /* let process inherit the console from parent... this handle two cases :
472 * 1/ generic console inheritance
473 * 2/ parent is a renderer which launches process, and process should attach to the console
474 * renderered by parent
476 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
478 int done = 0;
479 struct process* parent = parent_thread->process;
481 /* if parent is a renderer, then attach current process to its console
482 * a bit hacky....
484 if (hconin)
486 struct console_input* console;
488 /* FIXME: should we check some access rights ? */
489 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
490 0, &console_input_ops )))
492 if (console->renderer == parent_thread)
494 process->console = (struct console_input*)grab_object( console );
495 process->console->num_proc++;
496 done = 1;
498 release_object( console );
500 else clear_error(); /* ignore error */
502 /* otherwise, if parent has a console, attach child to this console */
503 if (!done && parent->console)
505 process->console = (struct console_input*)grab_object( parent->console );
506 process->console->num_proc++;
510 struct thread *console_get_renderer( struct console_input *console )
512 return console->renderer;
515 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
517 struct console_input* console = NULL;
519 if (handle)
520 console = (struct console_input *)get_handle_obj( current->process, handle,
521 access, &console_input_ops );
522 else if (current->process->console)
524 console = (struct console_input *)grab_object( current->process->console );
527 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
528 return console;
531 struct console_signal_info
533 struct console_input *console;
534 process_id_t group;
535 int signal;
538 static int propagate_console_signal_cb(struct process *process, void *user)
540 struct console_signal_info* csi = (struct console_signal_info*)user;
542 if (process->console == csi->console && process->running_threads &&
543 (!csi->group || process->group_id == csi->group))
545 /* find a suitable thread to signal */
546 struct thread *thread;
547 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
549 if (send_thread_signal( thread, csi->signal )) break;
552 return FALSE;
555 static void propagate_console_signal( struct console_input *console,
556 int sig, process_id_t group_id )
558 struct console_signal_info csi;
560 if (!console)
562 set_error( STATUS_INVALID_PARAMETER );
563 return;
565 /* FIXME: should support the other events (like CTRL_BREAK) */
566 if (sig != CTRL_C_EVENT)
568 set_error( STATUS_NOT_IMPLEMENTED );
569 return;
571 csi.console = console;
572 csi.signal = SIGINT;
573 csi.group = group_id;
575 enum_processes(propagate_console_signal_cb, &csi);
578 static int get_console_mode( obj_handle_t handle )
580 struct object *obj;
581 int ret = 0;
583 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
585 if (obj->ops == &console_input_ops)
587 ret = ((struct console_input *)obj)->mode;
589 else if (obj->ops == &screen_buffer_ops)
591 ret = ((struct screen_buffer *)obj)->mode;
593 else
594 set_error( STATUS_OBJECT_TYPE_MISMATCH );
595 release_object( obj );
597 return ret;
600 /* changes the mode of either a console input or a screen buffer */
601 static int set_console_mode( obj_handle_t handle, int mode )
603 struct object *obj;
604 int ret = 0;
606 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
607 return 0;
608 if (obj->ops == &console_input_ops)
610 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
611 ((struct console_input *)obj)->mode = mode;
612 ret = 1;
614 else if (obj->ops == &screen_buffer_ops)
616 ((struct screen_buffer *)obj)->mode = mode;
617 ret = 1;
619 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
620 release_object( obj );
621 return ret;
624 /* add input events to a console input queue */
625 static int write_console_input( struct console_input* console, int count,
626 const INPUT_RECORD *records )
628 INPUT_RECORD *new_rec;
630 if (!count) return 0;
631 if (!(new_rec = realloc( console->records,
632 (console->recnum + count) * sizeof(INPUT_RECORD) )))
634 set_error( STATUS_NO_MEMORY );
635 release_object( console );
636 return -1;
638 console->records = new_rec;
639 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
641 if (console->mode & ENABLE_PROCESSED_INPUT)
643 int i = 0;
644 while (i < count)
646 if (records[i].EventType == KEY_EVENT &&
647 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
648 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
650 if (i != count - 1)
651 memcpy( &console->records[console->recnum + i],
652 &console->records[console->recnum + i + 1],
653 (count - i - 1) * sizeof(INPUT_RECORD) );
654 count--;
655 if (records[i].Event.KeyEvent.bKeyDown)
657 /* send SIGINT to all processes attached to this console */
658 propagate_console_signal( console, CTRL_C_EVENT, 0 );
661 else i++;
664 if (!console->recnum && count) set_event( console->event );
665 console->recnum += count;
666 return count;
669 /* retrieve a pointer to the console input records */
670 static int read_console_input( obj_handle_t handle, int count, int flush )
672 struct console_input *console;
674 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
675 FILE_READ_DATA, &console_input_ops )))
676 return -1;
678 if (!count)
680 /* special case: do not retrieve anything, but return
681 * the total number of records available */
682 count = console->recnum;
684 else
686 if (count > console->recnum) count = console->recnum;
687 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
689 if (flush)
691 int i;
692 for (i = count; i < console->recnum; i++)
693 console->records[i-count] = console->records[i];
694 if ((console->recnum -= count) > 0)
696 INPUT_RECORD *new_rec = realloc( console->records,
697 console->recnum * sizeof(INPUT_RECORD) );
698 if (new_rec) console->records = new_rec;
700 else
702 free( console->records );
703 console->records = NULL;
704 reset_event( console->event );
707 release_object( console );
708 return count;
711 /* set misc console input information */
712 static int set_console_input_info( const struct set_console_input_info_request *req,
713 const WCHAR *title, data_size_t len )
715 struct console_input *console;
716 struct console_renderer_event evt;
718 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
719 if (console_input_is_bare(console) &&
720 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
721 SET_CONSOLE_INPUT_INFO_WIN)))
723 set_error( STATUS_UNSUCCESSFUL );
724 goto error;
727 memset(&evt.u, 0, sizeof(evt.u));
728 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
730 struct screen_buffer *screen_buffer;
732 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
733 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
734 if (!screen_buffer || screen_buffer->input != console)
736 set_error( STATUS_INVALID_HANDLE );
737 if (screen_buffer) release_object( screen_buffer );
738 goto error;
741 if (screen_buffer != console->active)
743 if (console->active) release_object( console->active );
744 console->active = screen_buffer;
745 generate_sb_initial_events( console );
747 else
748 release_object( screen_buffer );
750 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
752 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
753 if (new_title)
755 memcpy( new_title, title, len );
756 new_title[len / sizeof(WCHAR)] = 0;
757 free( console->title );
758 console->title = new_title;
759 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
760 console_input_events_append( console, &evt );
763 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
765 console->history_mode = req->history_mode;
767 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
768 console->history_size != req->history_size)
770 WCHAR** mem = NULL;
771 int i;
772 int delta;
774 if (req->history_size)
776 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
777 if (!mem) goto error;
778 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
781 delta = (console->history_index > req->history_size) ?
782 (console->history_index - req->history_size) : 0;
784 for (i = delta; i < console->history_index; i++)
786 mem[i - delta] = console->history[i];
787 console->history[i] = NULL;
789 console->history_index -= delta;
791 for (i = 0; i < console->history_size; i++)
792 free( console->history[i] );
793 free( console->history );
794 console->history = mem;
795 console->history_size = req->history_size;
797 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
799 console->edition_mode = req->edition_mode;
801 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
803 console->input_cp = req->input_cp;
805 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
807 console->output_cp = req->output_cp;
809 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
811 console->win = req->win;
813 release_object( console );
814 return 1;
815 error:
816 if (console) release_object( console );
817 return 0;
820 /* resize a screen buffer */
821 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
822 int new_width, int new_height )
824 int i, old_width, old_height, copy_width, copy_height;
825 char_info_t *new_data;
827 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
829 set_error( STATUS_NO_MEMORY );
830 return 0;
832 old_width = screen_buffer->width;
833 old_height = screen_buffer->height;
834 copy_width = min( old_width, new_width );
835 copy_height = min( old_height, new_height );
837 /* copy all the rows */
838 for (i = 0; i < copy_height; i++)
840 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
841 copy_width * sizeof(char_info_t) );
844 /* clear the end of each row */
845 if (new_width > old_width)
847 /* fill first row */
848 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
849 /* and blast it to the other rows */
850 for (i = 1; i < copy_height; i++)
851 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
852 (new_width - old_width) * sizeof(char_info_t) );
855 /* clear remaining rows */
856 if (new_height > old_height)
858 /* fill first row */
859 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
860 /* and blast it to the other rows */
861 for (i = old_height+1; i < new_height; i++)
862 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
863 new_width * sizeof(char_info_t) );
865 free( screen_buffer->data );
866 screen_buffer->data = new_data;
867 screen_buffer->width = new_width;
868 screen_buffer->height = new_height;
869 return 1;
872 /* set misc screen buffer information */
873 static int set_console_output_info( struct screen_buffer *screen_buffer,
874 const struct set_console_output_info_request *req )
876 struct console_renderer_event evt;
878 memset(&evt.u, 0, sizeof(evt.u));
879 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
881 if (req->cursor_size < 1 || req->cursor_size > 100)
883 set_error( STATUS_INVALID_PARAMETER );
884 return 0;
886 if (screen_buffer->cursor_size != req->cursor_size ||
887 screen_buffer->cursor_visible != req->cursor_visible)
889 screen_buffer->cursor_size = req->cursor_size;
890 screen_buffer->cursor_visible = req->cursor_visible;
891 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
892 evt.u.cursor_geom.size = req->cursor_size;
893 evt.u.cursor_geom.visible = req->cursor_visible;
894 console_input_events_append( screen_buffer->input, &evt );
897 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
899 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
900 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
902 set_error( STATUS_INVALID_PARAMETER );
903 return 0;
905 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
907 screen_buffer->cursor_x = req->cursor_x;
908 screen_buffer->cursor_y = req->cursor_y;
909 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
910 evt.u.cursor_pos.x = req->cursor_x;
911 evt.u.cursor_pos.y = req->cursor_y;
912 console_input_events_append( screen_buffer->input, &evt );
915 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
917 unsigned cc;
919 /* new screen-buffer cannot be smaller than actual window */
920 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
921 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
923 set_error( STATUS_INVALID_PARAMETER );
924 return 0;
926 /* FIXME: there are also some basic minimum and max size to deal with */
927 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
929 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
930 evt.u.resize.width = req->width;
931 evt.u.resize.height = req->height;
932 console_input_events_append( screen_buffer->input, &evt );
934 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
935 evt.u.update.top = 0;
936 evt.u.update.bottom = screen_buffer->height - 1;
937 console_input_events_append( screen_buffer->input, &evt );
939 /* scroll window to display sb */
940 if (screen_buffer->win.right >= req->width)
942 screen_buffer->win.right -= screen_buffer->win.left;
943 screen_buffer->win.left = 0;
945 if (screen_buffer->win.bottom >= req->height)
947 screen_buffer->win.bottom -= screen_buffer->win.top;
948 screen_buffer->win.top = 0;
950 /* reset cursor if needed (normally, if cursor was outside of new sb, the
951 * window has been shifted so that the new position of the cursor will be
952 * visible */
953 cc = 0;
954 if (screen_buffer->cursor_x >= req->width)
956 screen_buffer->cursor_x = req->width - 1;
957 cc++;
959 if (screen_buffer->cursor_y >= req->height)
961 screen_buffer->cursor_y = req->height - 1;
962 cc++;
964 if (cc)
966 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
967 evt.u.cursor_pos.x = req->cursor_x;
968 evt.u.cursor_pos.y = req->cursor_y;
969 console_input_events_append( screen_buffer->input, &evt );
972 if (screen_buffer == screen_buffer->input->active &&
973 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
975 INPUT_RECORD ir;
976 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
977 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
978 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
979 write_console_input( screen_buffer->input, 1, &ir );
982 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
984 screen_buffer->attr = req->attr;
986 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
988 if (req->win_left < 0 || req->win_left > req->win_right ||
989 req->win_right >= screen_buffer->width ||
990 req->win_top < 0 || req->win_top > req->win_bottom ||
991 req->win_bottom >= screen_buffer->height)
993 set_error( STATUS_INVALID_PARAMETER );
994 return 0;
996 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
997 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
999 screen_buffer->win.left = req->win_left;
1000 screen_buffer->win.top = req->win_top;
1001 screen_buffer->win.right = req->win_right;
1002 screen_buffer->win.bottom = req->win_bottom;
1003 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
1004 evt.u.display.left = req->win_left;
1005 evt.u.display.top = req->win_top;
1006 evt.u.display.width = req->win_right - req->win_left + 1;
1007 evt.u.display.height = req->win_bottom - req->win_top + 1;
1008 console_input_events_append( screen_buffer->input, &evt );
1011 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
1013 /* can only be done by renderer */
1014 if (current->process->console != screen_buffer->input)
1016 set_error( STATUS_INVALID_PARAMETER );
1017 return 0;
1020 screen_buffer->max_width = req->max_width;
1021 screen_buffer->max_height = req->max_height;
1024 return 1;
1027 /* appends a new line to history (history is a fixed size array) */
1028 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1030 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1032 if (!ptr)
1033 return;
1035 if (!console || !console->history_size)
1037 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1038 free( ptr );
1039 return;
1042 memcpy( ptr, buf, len * sizeof(WCHAR) );
1043 ptr[len] = 0;
1045 if (console->history_mode && console->history_index &&
1046 strncmpW( console->history[console->history_index - 1], ptr, len ) == 0)
1048 /* ok, mode ask us to not use twice the same string...
1049 * so just free mem and returns
1051 set_error( STATUS_ALIAS_EXISTS );
1052 free(ptr);
1053 return;
1056 if (console->history_index < console->history_size)
1058 console->history[console->history_index++] = ptr;
1060 else
1062 free( console->history[0]) ;
1063 memmove( &console->history[0], &console->history[1],
1064 (console->history_size - 1) * sizeof(WCHAR*) );
1065 console->history[console->history_size - 1] = ptr;
1069 /* returns a line from the cache */
1070 static data_size_t console_input_get_hist( struct console_input *console, int index )
1072 data_size_t ret = 0;
1074 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1075 else
1077 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1078 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1080 return ret;
1083 /* dumb dump */
1084 static void console_input_dump( struct object *obj, int verbose )
1086 struct console_input *console = (struct console_input *)obj;
1087 assert( obj->ops == &console_input_ops );
1088 fprintf( stderr, "Console input active=%p evt=%p\n",
1089 console->active, console->evt );
1092 static void console_input_destroy( struct object *obj )
1094 struct console_input* console_in = (struct console_input *)obj;
1095 struct screen_buffer* curr;
1096 int i;
1098 assert( obj->ops == &console_input_ops );
1099 free( console_in->title );
1100 free( console_in->records );
1102 if (console_in->active) release_object( console_in->active );
1103 console_in->active = NULL;
1105 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1107 if (curr->input == console_in) curr->input = NULL;
1110 if (console_in->evt)
1112 release_object( console_in->evt );
1113 console_in->evt = NULL;
1115 if (console_in->event)
1116 release_object( console_in->event );
1117 if (console_in->fd)
1118 release_object( console_in->fd );
1120 for (i = 0; i < console_in->history_size; i++)
1121 free( console_in->history[i] );
1122 free( console_in->history );
1125 static void screen_buffer_dump( struct object *obj, int verbose )
1127 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1128 assert( obj->ops == &screen_buffer_ops );
1130 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1133 static void screen_buffer_destroy( struct object *obj )
1135 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1137 assert( obj->ops == &screen_buffer_ops );
1139 list_remove( &screen_buffer->entry );
1141 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1143 struct screen_buffer *sb;
1145 screen_buffer->input->active = NULL;
1146 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1148 if (sb->input == screen_buffer->input)
1150 sb->input->active = sb;
1151 break;
1155 if (screen_buffer->fd) release_object( screen_buffer->fd );
1156 free( screen_buffer->data );
1159 static struct fd *screen_buffer_get_fd( struct object *obj )
1161 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1162 assert( obj->ops == &screen_buffer_ops );
1163 if (screen_buffer->fd)
1164 return (struct fd*)grab_object( screen_buffer->fd );
1165 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1166 return NULL;
1169 /* write data into a screen buffer */
1170 static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1171 const void* data, enum char_info_mode mode,
1172 int x, int y, int wrap )
1174 unsigned int i;
1175 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1177 if (y >= screen_buffer->height) return 0;
1179 if (wrap)
1180 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1181 else
1182 end = screen_buffer->data + (y+1) * screen_buffer->width;
1184 switch(mode)
1186 case CHAR_INFO_MODE_TEXT:
1188 const WCHAR *ptr = data;
1189 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1191 break;
1192 case CHAR_INFO_MODE_ATTR:
1194 const unsigned short *ptr = data;
1195 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1197 break;
1198 case CHAR_INFO_MODE_TEXTATTR:
1200 const char_info_t *ptr = data;
1201 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1203 break;
1204 case CHAR_INFO_MODE_TEXTSTDATTR:
1206 const WCHAR *ptr = data;
1207 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1209 dest->ch = ptr[i];
1210 dest->attr = screen_buffer->attr;
1213 break;
1214 default:
1215 set_error( STATUS_INVALID_PARAMETER );
1216 return 0;
1219 if (i && screen_buffer == screen_buffer->input->active)
1221 struct console_renderer_event evt;
1222 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1223 memset(&evt.u, 0, sizeof(evt.u));
1224 evt.u.update.top = y + x / screen_buffer->width;
1225 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1226 console_input_events_append( screen_buffer->input, &evt );
1228 return i;
1231 /* fill a screen buffer with uniform data */
1232 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1233 enum char_info_mode mode, int x, int y, int count, int wrap )
1235 int i;
1236 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1238 if (y >= screen_buffer->height) return 0;
1240 if (wrap)
1241 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1242 else
1243 end = screen_buffer->data + (y+1) * screen_buffer->width;
1245 if (count > end - dest) count = end - dest;
1247 switch(mode)
1249 case CHAR_INFO_MODE_TEXT:
1250 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1251 break;
1252 case CHAR_INFO_MODE_ATTR:
1253 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1254 break;
1255 case CHAR_INFO_MODE_TEXTATTR:
1256 for (i = 0; i < count; i++) dest[i] = data;
1257 break;
1258 case CHAR_INFO_MODE_TEXTSTDATTR:
1259 for (i = 0; i < count; i++)
1261 dest[i].ch = data.ch;
1262 dest[i].attr = screen_buffer->attr;
1264 break;
1265 default:
1266 set_error( STATUS_INVALID_PARAMETER );
1267 return 0;
1270 if (count && screen_buffer == screen_buffer->input->active)
1272 struct console_renderer_event evt;
1273 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1274 memset(&evt.u, 0, sizeof(evt.u));
1275 evt.u.update.top = y;
1276 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1277 console_input_events_append( screen_buffer->input, &evt );
1279 return i;
1282 /* read data from a screen buffer */
1283 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1284 enum char_info_mode mode, int wrap )
1286 int i;
1287 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1289 if (y >= screen_buffer->height) return;
1291 if (wrap)
1292 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1293 else
1294 end = screen_buffer->data + (y+1) * screen_buffer->width;
1296 switch(mode)
1298 case CHAR_INFO_MODE_TEXT:
1300 WCHAR *data;
1301 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1302 if ((data = set_reply_data_size( count * sizeof(*data) )))
1304 for (i = 0; i < count; i++) data[i] = src[i].ch;
1307 break;
1308 case CHAR_INFO_MODE_ATTR:
1310 unsigned short *data;
1311 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1312 if ((data = set_reply_data_size( count * sizeof(*data) )))
1314 for (i = 0; i < count; i++) data[i] = src[i].attr;
1317 break;
1318 case CHAR_INFO_MODE_TEXTATTR:
1320 char_info_t *data;
1321 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1322 if ((data = set_reply_data_size( count * sizeof(*data) )))
1324 for (i = 0; i < count; i++) data[i] = src[i];
1327 break;
1328 default:
1329 set_error( STATUS_INVALID_PARAMETER );
1330 break;
1334 /* scroll parts of a screen buffer */
1335 static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1336 int w, int h )
1338 int j;
1339 char_info_t *psrc, *pdst;
1340 struct console_renderer_event evt;
1342 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1343 xsrc + w > screen_buffer->width ||
1344 xdst + w > screen_buffer->width ||
1345 ysrc + h > screen_buffer->height ||
1346 ydst + h > screen_buffer->height ||
1347 w == 0 || h == 0)
1349 set_error( STATUS_INVALID_PARAMETER );
1350 return;
1353 if (ysrc < ydst)
1355 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1356 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1358 for (j = h; j > 0; j--)
1360 memcpy(pdst, psrc, w * sizeof(*pdst) );
1361 pdst -= screen_buffer->width;
1362 psrc -= screen_buffer->width;
1365 else
1367 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1368 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1370 for (j = 0; j < h; j++)
1372 /* we use memmove here because when psrc and pdst are the same,
1373 * copies are done on the same row, so the dst and src blocks
1374 * can overlap */
1375 memmove( pdst, psrc, w * sizeof(*pdst) );
1376 pdst += screen_buffer->width;
1377 psrc += screen_buffer->width;
1381 /* FIXME: this could be enhanced, by signalling scroll */
1382 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1383 memset(&evt.u, 0, sizeof(evt.u));
1384 evt.u.update.top = min(ysrc, ydst);
1385 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1386 console_input_events_append( screen_buffer->input, &evt );
1389 /* allocate a console for the renderer */
1390 DECL_HANDLER(alloc_console)
1392 obj_handle_t in = 0;
1393 obj_handle_t evt = 0;
1394 struct process *process;
1395 struct thread *renderer;
1396 struct console_input *console;
1397 int fd;
1398 int attach = 0;
1400 if (req->input_fd != -1)
1402 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1404 set_error( STATUS_INVALID_PARAMETER );
1405 return;
1408 else fd = -1;
1410 switch (req->pid)
1412 case 0:
1413 /* renderer is current, console to be attached to parent process */
1414 renderer = current;
1415 if (!(process = current->process->parent))
1417 if (fd != -1) close( fd );
1418 set_error( STATUS_ACCESS_DENIED );
1419 return;
1421 grab_object( process );
1422 attach = 1;
1423 break;
1424 case 0xffffffff:
1425 /* no renderer, console to be attached to current process */
1426 renderer = NULL;
1427 process = current->process;
1428 grab_object( process );
1429 attach = 1;
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 )))
1436 if (fd != -1) close( fd );
1437 return;
1441 if (attach && process->console)
1443 if (fd != -1) close( fd );
1444 set_error( STATUS_ACCESS_DENIED );
1445 goto the_end;
1448 if ((console = (struct console_input*)create_console_input( renderer, fd )))
1450 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1452 if (!console->evt ||
1453 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1455 if (attach)
1457 process->console = (struct console_input*)grab_object( console );
1458 console->num_proc++;
1460 reply->handle_in = in;
1461 reply->event = evt;
1462 release_object( console );
1463 goto the_end;
1465 close_handle( current->process, in );
1467 release_object( console );
1469 the_end:
1470 release_object( process );
1473 /* free the console of the current process */
1474 DECL_HANDLER(free_console)
1476 free_console( current->process );
1479 /* let the renderer peek the events it's waiting on */
1480 DECL_HANDLER(get_console_renderer_events)
1482 struct console_input_events *evt;
1484 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1485 FILE_READ_PROPERTIES, &console_input_events_ops );
1486 if (!evt) return;
1487 console_input_events_get( evt );
1488 release_object( evt );
1491 /* open a handle to the process console */
1492 DECL_HANDLER(open_console)
1494 struct object *obj = NULL;
1496 reply->handle = 0;
1497 if (!req->from)
1499 if (current->process->console)
1500 obj = grab_object( (struct object*)current->process->console );
1502 else if (req->from == (obj_handle_t)1)
1504 if (current->process->console && current->process->console->active)
1505 obj = grab_object( (struct object*)current->process->console->active );
1507 else if ((obj = get_handle_obj( current->process, req->from,
1508 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1510 struct console_input *console = (struct console_input *)obj;
1511 obj = (console->active) ? grab_object( console->active ) : NULL;
1512 release_object( console );
1515 /* FIXME: req->share is not used (as in screen buffer creation) */
1516 if (obj)
1518 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1519 release_object( obj );
1521 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1524 /* set info about a console input */
1525 DECL_HANDLER(set_console_input_info)
1527 set_console_input_info( req, get_req_data(), get_req_data_size() );
1530 /* get info about a console (output only) */
1531 DECL_HANDLER(get_console_input_info)
1533 struct console_input *console;
1535 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1536 if (console->title)
1538 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1539 if (len > get_reply_max_size()) len = get_reply_max_size();
1540 set_reply_data( console->title, len );
1542 reply->history_mode = console->history_mode;
1543 reply->history_size = console->history_size;
1544 reply->history_index = console->history_index;
1545 reply->edition_mode = console->edition_mode;
1546 reply->input_cp = console->input_cp;
1547 reply->output_cp = console->output_cp;
1548 reply->win = console->win;
1550 release_object( console );
1553 /* get a console mode (input or output) */
1554 DECL_HANDLER(get_console_mode)
1556 reply->mode = get_console_mode( req->handle );
1559 /* set a console mode (input or output) */
1560 DECL_HANDLER(set_console_mode)
1562 set_console_mode( req->handle, req->mode );
1565 /* add input records to a console input queue */
1566 DECL_HANDLER(write_console_input)
1568 struct console_input *console;
1570 reply->written = 0;
1571 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1572 FILE_WRITE_PROPERTIES, &console_input_ops )))
1573 return;
1574 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1575 get_req_data() );
1576 release_object( console );
1579 /* fetch input records from a console input queue */
1580 DECL_HANDLER(read_console_input)
1582 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1583 reply->read = read_console_input( req->handle, count, req->flush );
1586 /* appends a string to console's history */
1587 DECL_HANDLER(append_console_input_history)
1589 struct console_input *console;
1591 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1592 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1593 release_object( console );
1596 /* appends a string to console's history */
1597 DECL_HANDLER(get_console_input_history)
1599 struct console_input *console;
1601 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1602 reply->total = console_input_get_hist( console, req->index );
1603 release_object( console );
1606 /* creates a screen buffer */
1607 DECL_HANDLER(create_console_output)
1609 struct console_input* console;
1610 struct screen_buffer* screen_buffer;
1611 int fd;
1613 if (req->fd != -1)
1615 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1617 set_error( STATUS_INVALID_HANDLE );
1618 return;
1621 else fd = -1;
1622 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1624 if (fd != -1) close( fd );
1625 return;
1627 if (console_input_is_bare( console ) ^ (fd != -1))
1629 if (fd != -1) close( fd );
1630 release_object( console );
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 );