Fix handling of unix absolute paths in DOSFS_GetFullName and
[wine/multimedia.git] / server / console.c
blob5d94d7371dee3bdc39ecfd40d147c37e16c0df6c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
31 #include "handle.h"
32 #include "process.h"
33 #include "request.h"
34 #include "unicode.h"
35 #include "console.h"
38 static void console_input_dump( struct object *obj, int verbose );
39 static void console_input_destroy( struct object *obj );
40 static int console_input_signaled( struct object *obj, struct thread *thread );
42 /* common routine */
43 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
45 static const struct object_ops console_input_ops =
47 sizeof(struct console_input), /* size */
48 console_input_dump, /* dump */
49 add_queue, /* add_queue */
50 remove_queue, /* remove_queue */
51 console_input_signaled, /* signaled */
52 no_satisfied, /* satisfied */
53 NULL, /* get_poll_events */
54 NULL, /* poll_event */
55 no_get_fd, /* get_fd */
56 no_flush, /* flush */
57 console_get_file_info, /* get_file_info */
58 NULL, /* queue_async */
59 console_input_destroy /* destroy */
62 static void console_input_events_dump( struct object *obj, int verbose );
63 static void console_input_events_destroy( struct object *obj );
64 static int console_input_events_signaled( struct object *obj, struct thread *thread );
66 struct console_input_events
68 struct object obj; /* object header */
69 int num_alloc; /* number of allocated events */
70 int num_used; /* number of actually used events */
71 struct console_renderer_event* events;
74 static const struct object_ops console_input_events_ops =
76 sizeof(struct console_input_events), /* size */
77 console_input_events_dump, /* dump */
78 add_queue, /* add_queue */
79 remove_queue, /* remove_queue */
80 console_input_events_signaled, /* signaled */
81 no_satisfied, /* satisfied */
82 NULL, /* get_poll_events */
83 NULL, /* poll_event */
84 no_get_fd, /* get_fd */
85 no_flush, /* flush */
86 no_get_file_info, /* get_file_info */
87 NULL, /* queue_async */
88 console_input_events_destroy /* destroy */
91 struct screen_buffer
93 struct object obj; /* object header */
94 struct screen_buffer *next; /* linked list of all screen buffers */
95 struct screen_buffer *prev;
96 struct console_input *input; /* associated console input */
97 int mode; /* output mode */
98 int cursor_size; /* size of cursor (percentage filled) */
99 int cursor_visible;/* cursor visibility flag */
100 int cursor_x; /* position of cursor */
101 int cursor_y; /* position of cursor */
102 int width; /* size (w-h) of the screen buffer */
103 int height;
104 int max_width; /* size (w-h) of the window given font size */
105 int max_height;
106 char_info_t *data; /* the data for each cell - a width x height matrix */
107 unsigned short attr; /* default attribute for screen buffer */
108 rectangle_t win; /* current visible window on the screen buffer *
109 * as seen in wineconsole */
112 static void screen_buffer_dump( struct object *obj, int verbose );
113 static void screen_buffer_destroy( struct object *obj );
115 static const struct object_ops screen_buffer_ops =
117 sizeof(struct screen_buffer), /* size */
118 screen_buffer_dump, /* dump */
119 no_add_queue, /* add_queue */
120 NULL, /* remove_queue */
121 NULL, /* signaled */
122 NULL, /* satisfied */
123 NULL, /* get_poll_events */
124 NULL, /* poll_event */
125 no_get_fd, /* get_fd */
126 no_flush, /* flush */
127 console_get_file_info, /* get_file_info */
128 NULL, /* queue_async */
129 screen_buffer_destroy /* destroy */
132 static struct screen_buffer *screen_buffer_list;
134 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
136 /* dumps the renderer events of a console */
137 static void console_input_events_dump( struct object *obj, int verbose )
139 struct console_input_events *evts = (struct console_input_events *)obj;
140 assert( obj->ops == &console_input_events_ops );
141 fprintf( stderr, "Console input events: %d/%d events\n",
142 evts->num_used, evts->num_alloc );
145 /* destroys the renderer events of a console */
146 static void console_input_events_destroy( struct object *obj )
148 struct console_input_events *evts = (struct console_input_events *)obj;
149 assert( obj->ops == &console_input_events_ops );
150 free( evts->events );
153 /* the renderer events list is signaled when it's not empty */
154 static int console_input_events_signaled( struct object *obj, struct thread *thread )
156 struct console_input_events *evts = (struct console_input_events *)obj;
157 assert( obj->ops == &console_input_events_ops );
158 return (evts->num_used != 0);
161 /* add an event to the console's renderer events list */
162 static void console_input_events_append( struct console_input_events* evts,
163 struct console_renderer_event* evt)
165 /* to be done even when the renderer generates the events ? */
166 if (evts->num_used == evts->num_alloc)
168 evts->num_alloc += 16;
169 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
170 assert(evts->events);
172 evts->events[evts->num_used++] = *evt;
173 wake_up( &evts->obj, 0 );
176 /* retrieves events from the console's renderer events list */
177 static void console_input_events_get( struct console_input_events* evts )
179 size_t num = get_reply_max_size() / sizeof(evts->events[0]);
181 if (num > evts->num_used) num = evts->num_used;
182 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
183 if (num < evts->num_used)
185 memmove( &evts->events[0], &evts->events[num],
186 (evts->num_used - num) * sizeof(evts->events[0]) );
188 evts->num_used -= num;
191 static struct console_input_events *create_console_input_events(void)
193 struct console_input_events* evt;
195 if (!(evt = alloc_object( &console_input_events_ops, -1 ))) return NULL;
196 evt->num_alloc = evt->num_used = 0;
197 evt->events = NULL;
198 return evt;
201 static struct object *create_console_input( struct thread* renderer )
203 struct console_input *console_input;
205 if (!(console_input = alloc_object( &console_input_ops, -1 ))) return NULL;
206 console_input->renderer = renderer;
207 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
208 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
209 console_input->num_proc = 0;
210 console_input->active = NULL;
211 console_input->recnum = 0;
212 console_input->records = NULL;
213 console_input->evt = create_console_input_events();
214 console_input->title = NULL;
215 console_input->history_size = 50;
216 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
217 console_input->history_index = 0;
218 console_input->history_mode = 0;
220 if (!console_input->history || !console_input->evt)
222 release_object( console_input );
223 return NULL;
225 return &console_input->obj;
228 static struct screen_buffer *create_console_output( struct console_input *console_input )
230 struct screen_buffer *screen_buffer;
231 struct console_renderer_event evt;
232 int i;
234 if (!(screen_buffer = alloc_object( &screen_buffer_ops, -1 ))) return NULL;
235 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
236 screen_buffer->input = console_input;
237 screen_buffer->cursor_size = 100;
238 screen_buffer->cursor_visible = 1;
239 screen_buffer->width = 80;
240 screen_buffer->height = 150;
241 screen_buffer->max_width = 80;
242 screen_buffer->max_height = 25;
243 screen_buffer->cursor_x = 0;
244 screen_buffer->cursor_y = 0;
245 screen_buffer->attr = 0x0F;
246 screen_buffer->win.left = 0;
247 screen_buffer->win.right = screen_buffer->max_width - 1;
248 screen_buffer->win.top = 0;
249 screen_buffer->win.bottom = screen_buffer->max_height - 1;
251 if ((screen_buffer->next = screen_buffer_list)) screen_buffer->next->prev = screen_buffer;
252 screen_buffer->prev = NULL;
253 screen_buffer_list = screen_buffer;
255 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
256 sizeof(*screen_buffer->data) )))
258 release_object( screen_buffer );
259 return NULL;
261 /* clear the first row */
262 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
263 /* and copy it to all other rows */
264 for (i = 1; i < screen_buffer->height; i++)
265 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
266 screen_buffer->width * sizeof(char_info_t) );
268 if (!console_input->active)
270 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
272 /* generate the fist events */
273 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
274 console_input_events_append( console_input->evt, &evt );
276 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
277 evt.u.resize.width = screen_buffer->width;
278 evt.u.resize.height = screen_buffer->height;
279 console_input_events_append( console_input->evt, &evt );
281 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
282 evt.u.display.left = screen_buffer->win.left;
283 evt.u.display.top = screen_buffer->win.top;
284 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
285 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
286 console_input_events_append( console_input->evt, &evt );
288 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
289 evt.u.update.top = 0;
290 evt.u.update.bottom = screen_buffer->height - 1;
291 console_input_events_append( console_input->evt, &evt );
293 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
294 evt.u.cursor_geom.size = screen_buffer->cursor_size;
295 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
296 console_input_events_append( console_input->evt, &evt );
298 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
299 evt.u.cursor_pos.x = screen_buffer->cursor_x;
300 evt.u.cursor_pos.y = screen_buffer->cursor_y;
301 console_input_events_append( console_input->evt, &evt );
303 return screen_buffer;
306 /* free the console for this process */
307 int free_console( struct process *process )
309 struct console_input* console = process->console;
311 if (!console || !console->renderer) return 0;
313 process->console = NULL;
314 if (--console->num_proc == 0)
316 /* all processes have terminated... tell the renderer to terminate too */
317 struct console_renderer_event evt;
318 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
319 console_input_events_append( console->evt, &evt );
321 release_object( console );
323 return 1;
326 /* let process inherit the console from parent... this handle two cases :
327 * 1/ generic console inheritance
328 * 2/ parent is a renderer which launches process, and process should attach to the console
329 * renderered by parent
331 void inherit_console(struct thread *parent_thread, struct process *process, handle_t hconin)
333 int done = 0;
334 struct process* parent = parent_thread->process;
336 /* if parent is a renderer, then attach current process to its console
337 * a bit hacky....
339 if (hconin)
341 struct console_input* console;
343 if ((console = (struct console_input*)get_handle_obj( parent, hconin, 0, NULL )))
345 if (console->renderer == parent_thread)
347 process->console = (struct console_input*)grab_object( console );
348 process->console->num_proc++;
349 done = 1;
351 release_object( console );
354 /* otherwise, if parent has a console, attach child to this console */
355 if (!done && parent->console)
357 assert(parent->console->renderer);
358 process->console = (struct console_input*)grab_object( parent->console );
359 process->console->num_proc++;
363 static struct console_input* console_input_get( handle_t handle, unsigned access )
365 struct console_input* console = 0;
367 if (handle)
368 console = (struct console_input *)get_handle_obj( current->process, handle,
369 access, &console_input_ops );
370 else if (current->process->console)
372 assert( current->process->console->renderer );
373 console = (struct console_input *)grab_object( current->process->console );
376 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
377 return console;
380 /* check if a console input is signaled: yes if non read input records */
381 static int console_input_signaled( struct object *obj, struct thread *thread )
383 struct console_input *console = (struct console_input *)obj;
384 assert( obj->ops == &console_input_ops );
385 return console->recnum ? 1 : 0;
388 static int get_console_mode( handle_t handle )
390 struct object *obj;
391 int ret = 0;
393 if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
395 if (obj->ops == &console_input_ops)
396 ret = ((struct console_input *)obj)->mode;
397 else if (obj->ops == &screen_buffer_ops)
398 ret = ((struct screen_buffer *)obj)->mode;
399 else
400 set_error( STATUS_OBJECT_TYPE_MISMATCH );
401 release_object( obj );
403 return ret;
406 /* changes the mode of either a console input or a screen buffer */
407 static int set_console_mode( handle_t handle, int mode )
409 struct object *obj;
410 int ret = 0;
412 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
413 return 0;
414 if (obj->ops == &console_input_ops)
416 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
417 ((struct console_input *)obj)->mode = mode;
418 ret = 1;
420 else if (obj->ops == &screen_buffer_ops)
422 ((struct screen_buffer *)obj)->mode = mode;
423 ret = 1;
425 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
426 release_object( obj );
427 return ret;
430 /* add input events to a console input queue */
431 static int write_console_input( struct console_input* console, int count,
432 const INPUT_RECORD *records )
434 INPUT_RECORD *new_rec;
436 if (!count) return 0;
437 if (!(new_rec = realloc( console->records,
438 (console->recnum + count) * sizeof(INPUT_RECORD) )))
440 set_error( STATUS_NO_MEMORY );
441 release_object( console );
442 return -1;
444 console->records = new_rec;
445 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
446 console->recnum += count;
448 /* wake up all waiters */
449 wake_up( &console->obj, 0 );
450 return count;
453 /* retrieve a pointer to the console input records */
454 static int read_console_input( handle_t handle, int count, int flush )
456 struct console_input *console;
458 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
459 GENERIC_READ, &console_input_ops )))
460 return -1;
462 if (!count)
464 /* special case: do not retrieve anything, but return
465 * the total number of records available */
466 count = console->recnum;
468 else
470 if (count > console->recnum) count = console->recnum;
471 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
473 if (flush)
475 int i;
476 for (i = count; i < console->recnum; i++)
477 console->records[i-count] = console->records[i];
478 if ((console->recnum -= count) > 0)
480 INPUT_RECORD *new_rec = realloc( console->records,
481 console->recnum * sizeof(INPUT_RECORD) );
482 if (new_rec) console->records = new_rec;
484 else
486 free( console->records );
487 console->records = NULL;
490 release_object( console );
491 return count;
494 /* set misc console input information */
495 static int set_console_input_info( const struct set_console_input_info_request *req,
496 const WCHAR *title, size_t len )
498 struct console_input *console;
499 struct console_renderer_event evt;
501 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) goto error;
503 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
505 struct screen_buffer *screen_buffer;
507 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
508 GENERIC_READ, &screen_buffer_ops );
509 if (!screen_buffer || screen_buffer->input != console)
511 set_error( STATUS_INVALID_PARAMETER );
512 if (screen_buffer) release_object( screen_buffer );
513 goto error;
516 if (screen_buffer != console->active)
518 if (console->active) release_object( console->active );
519 console->active = screen_buffer;
520 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
521 console_input_events_append( console->evt, &evt );
523 else
524 release_object( screen_buffer );
526 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
528 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
529 if (new_title)
531 memcpy( new_title, title, len + sizeof(WCHAR) );
532 new_title[len / sizeof(WCHAR)] = 0;
533 if (console->title) free( console->title );
534 console->title = new_title;
537 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
539 console->history_mode = req->history_mode;
541 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
542 console->history_size != req->history_size)
544 WCHAR** mem = NULL;
545 int i;
546 int delta;
548 if (req->history_size)
550 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
551 if (!mem) goto error;
552 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
555 delta = (console->history_index > req->history_size) ?
556 (console->history_index - req->history_size) : 0;
558 for (i = delta; i < console->history_index; i++)
560 mem[i - delta] = console->history[i];
561 console->history[i] = NULL;
563 console->history_index -= delta;
565 for (i = 0; i < console->history_size; i++)
566 if (console->history[i]) free( console->history[i] );
567 free( console->history );
568 console->history = mem;
569 console->history_size = req->history_size;
571 release_object( console );
572 return 1;
573 error:
574 if (console) release_object( console );
575 return 0;
578 /* resize a screen buffer */
579 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
580 int new_width, int new_height )
582 int i, old_width, old_height, copy_width, copy_height;
583 char_info_t *new_data;
585 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
587 set_error( STATUS_NO_MEMORY );
588 return 0;
590 old_width = screen_buffer->width;
591 old_height = screen_buffer->height;
592 copy_width = min( old_width, new_width );
593 copy_height = min( old_height, new_height );
595 /* copy all the rows */
596 for (i = 0; i < copy_height; i++)
598 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
599 copy_width * sizeof(char_info_t) );
602 /* clear the end of each row */
603 if (new_width > old_width)
605 /* fill first row */
606 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
607 /* and blast it to the other rows */
608 for (i = 1; i < copy_height; i++)
609 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
610 (new_width - old_width) * sizeof(char_info_t) );
613 /* clear remaining rows */
614 if (new_height > old_height)
616 /* fill first row */
617 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
618 /* and blast it to the other rows */
619 for (i = old_height+1; i < new_height; i++)
620 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
621 new_width * sizeof(char_info_t) );
623 free( screen_buffer->data );
624 screen_buffer->data = new_data;
625 screen_buffer->width = new_width;
626 screen_buffer->height = new_height;
627 return 1;
630 /* set misc screen buffer information */
631 static int set_console_output_info( struct screen_buffer *screen_buffer,
632 const struct set_console_output_info_request *req )
634 struct console_renderer_event evt;
636 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
638 if (req->cursor_size < 1 || req->cursor_size > 100)
640 set_error( STATUS_INVALID_PARAMETER );
641 return 0;
643 if (screen_buffer->cursor_size != req->cursor_size ||
644 screen_buffer->cursor_visible != req->cursor_visible)
646 screen_buffer->cursor_size = req->cursor_size;
647 screen_buffer->cursor_visible = req->cursor_visible;
648 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
649 evt.u.cursor_geom.size = req->cursor_size;
650 evt.u.cursor_geom.visible = req->cursor_visible;
651 console_input_events_append( screen_buffer->input->evt, &evt );
654 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
656 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
657 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
659 set_error( STATUS_INVALID_PARAMETER );
660 return 0;
662 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
664 screen_buffer->cursor_x = req->cursor_x;
665 screen_buffer->cursor_y = req->cursor_y;
666 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
667 evt.u.cursor_pos.x = req->cursor_x;
668 evt.u.cursor_pos.y = req->cursor_y;
669 console_input_events_append( screen_buffer->input->evt, &evt );
672 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
674 /* FIXME: there are also some basic minimum and max size to deal with */
675 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
677 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
678 evt.u.resize.width = req->width;
679 evt.u.resize.height = req->height;
680 console_input_events_append( screen_buffer->input->evt, &evt );
682 if (screen_buffer == screen_buffer->input->active &&
683 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
685 INPUT_RECORD ir;
686 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
687 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
688 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
689 write_console_input( screen_buffer->input, 1, &ir );
692 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
694 screen_buffer->attr = req->attr;
696 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
698 if (req->win_left < 0 || req->win_left > req->win_right ||
699 req->win_right >= screen_buffer->width ||
700 req->win_top < 0 || req->win_top > req->win_bottom ||
701 req->win_bottom >= screen_buffer->height)
703 set_error( STATUS_INVALID_PARAMETER );
704 return 0;
706 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
707 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
709 screen_buffer->win.left = req->win_left;
710 screen_buffer->win.top = req->win_top;
711 screen_buffer->win.right = req->win_right;
712 screen_buffer->win.bottom = req->win_bottom;
713 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
714 evt.u.display.left = req->win_left;
715 evt.u.display.top = req->win_top;
716 evt.u.display.width = req->win_right - req->win_left + 1;
717 evt.u.display.height = req->win_bottom - req->win_top + 1;
718 console_input_events_append( screen_buffer->input->evt, &evt );
721 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
723 /* can only be done by renderer */
724 if (current->process->console != screen_buffer->input)
726 set_error( STATUS_INVALID_PARAMETER );
727 return 0;
730 screen_buffer->max_width = req->max_width;
731 screen_buffer->max_height = req->max_height;
734 return 1;
737 /* appends a new line to history (history is a fixed size array) */
738 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
740 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
742 if (!ptr)
744 set_error( STATUS_NO_MEMORY );
745 return;
747 if (!console || !console->history_size)
749 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
750 return;
753 memcpy( ptr, buf, len * sizeof(WCHAR) );
754 ptr[len] = 0;
756 if (console->history_mode && console->history_index &&
757 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
759 /* ok, mode ask us to not use twice the same string...
760 * so just free mem and returns
762 set_error( STATUS_ALIAS_EXISTS );
763 free(ptr);
764 return;
767 if (console->history_index < console->history_size)
769 console->history[console->history_index++] = ptr;
771 else
773 free( console->history[0]) ;
774 memmove( &console->history[0], &console->history[1],
775 (console->history_size - 1) * sizeof(WCHAR*) );
776 console->history[console->history_size - 1] = ptr;
780 /* returns a line from the cachde */
781 static size_t console_input_get_hist( struct console_input *console, int index )
783 size_t ret = 0;
785 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
786 else
788 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
789 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
791 return ret;
794 /* dumb dump */
795 static void console_input_dump( struct object *obj, int verbose )
797 struct console_input *console = (struct console_input *)obj;
798 assert( obj->ops == &console_input_ops );
799 fprintf( stderr, "Console input active=%p evt=%p\n",
800 console->active, console->evt );
803 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
805 if (reply)
807 reply->type = FILE_TYPE_CHAR;
808 reply->attr = 0;
809 reply->access_time = 0;
810 reply->write_time = 0;
811 reply->size_high = 0;
812 reply->size_low = 0;
813 reply->links = 0;
814 reply->index_high = 0;
815 reply->index_low = 0;
816 reply->serial = 0;
818 *flags = 0;
819 return FD_TYPE_CONSOLE;
822 static void console_input_destroy( struct object *obj )
824 struct console_input* console_in = (struct console_input *)obj;
825 struct screen_buffer* curr;
826 int i;
828 assert( obj->ops == &console_input_ops );
829 if (console_in->title) free( console_in->title );
830 if (console_in->records) free( console_in->records );
832 if (console_in->active) release_object( console_in->active );
833 console_in->active = NULL;
835 for (curr = screen_buffer_list; curr; curr = curr->next)
837 if (curr->input == console_in) curr->input = NULL;
840 release_object( console_in->evt );
841 console_in->evt = NULL;
843 for (i = 0; i < console_in->history_size; i++)
844 if (console_in->history[i]) free( console_in->history[i] );
845 if (console_in->history) free( console_in->history );
848 static void screen_buffer_dump( struct object *obj, int verbose )
850 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
851 assert( obj->ops == &screen_buffer_ops );
853 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
856 static void screen_buffer_destroy( struct object *obj )
858 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
860 assert( obj->ops == &screen_buffer_ops );
862 if (screen_buffer->next) screen_buffer->next->prev = screen_buffer->prev;
863 if (screen_buffer->prev) screen_buffer->prev->next = screen_buffer->next;
864 else screen_buffer_list = screen_buffer->next;
866 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
868 struct screen_buffer* sb;
869 for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
870 screen_buffer->input->active = sb;
872 if (screen_buffer->data) free( screen_buffer->data );
875 /* write data into a screen buffer */
876 static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
877 const void* data, enum char_info_mode mode,
878 int x, int y, int wrap )
880 int i;
881 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
883 if (y >= screen_buffer->height) return 0;
885 if (wrap)
886 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
887 else
888 end = screen_buffer->data + (y+1) * screen_buffer->width;
890 switch(mode)
892 case CHAR_INFO_MODE_TEXT:
894 const WCHAR *ptr = data;
895 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
897 break;
898 case CHAR_INFO_MODE_ATTR:
900 const unsigned short *ptr = data;
901 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
903 break;
904 case CHAR_INFO_MODE_TEXTATTR:
906 const char_info_t *ptr = data;
907 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
909 break;
910 case CHAR_INFO_MODE_TEXTSTDATTR:
912 const WCHAR *ptr = data;
913 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
915 dest->ch = ptr[i];
916 dest->attr = screen_buffer->attr;
919 break;
920 default:
921 set_error( STATUS_INVALID_PARAMETER );
922 return 0;
925 if (i && screen_buffer == screen_buffer->input->active)
927 struct console_renderer_event evt;
928 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
929 evt.u.update.top = y;
930 evt.u.update.bottom = (y * screen_buffer->width + x + i - 1) / screen_buffer->width;
931 console_input_events_append( screen_buffer->input->evt, &evt );
933 return i;
936 /* fill a screen buffer with uniform data */
937 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
938 enum char_info_mode mode, int x, int y, int count, int wrap )
940 int i;
941 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
943 if (y >= screen_buffer->height) return 0;
945 if (wrap)
946 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
947 else
948 end = screen_buffer->data + (y+1) * screen_buffer->width;
950 if (count > end - dest) count = end - dest;
952 switch(mode)
954 case CHAR_INFO_MODE_TEXT:
955 for (i = 0; i < count; i++) dest[i].ch = data.ch;
956 break;
957 case CHAR_INFO_MODE_ATTR:
958 for (i = 0; i < count; i++) dest[i].attr = data.attr;
959 break;
960 case CHAR_INFO_MODE_TEXTATTR:
961 for (i = 0; i < count; i++) dest[i] = data;
962 break;
963 case CHAR_INFO_MODE_TEXTSTDATTR:
964 for (i = 0; i < count; i++)
966 dest[i].ch = data.ch;
967 dest[i].attr = screen_buffer->attr;
969 break;
970 default:
971 set_error( STATUS_INVALID_PARAMETER );
972 return 0;
975 if (count && screen_buffer == screen_buffer->input->active)
977 struct console_renderer_event evt;
978 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
979 evt.u.update.top = y;
980 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
981 console_input_events_append( screen_buffer->input->evt, &evt );
983 return i;
986 /* read data from a screen buffer */
987 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
988 enum char_info_mode mode, int wrap )
990 int i;
991 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
993 if (y >= screen_buffer->height) return;
995 if (wrap)
996 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
997 else
998 end = screen_buffer->data + (y+1) * screen_buffer->width;
1000 switch(mode)
1002 case CHAR_INFO_MODE_TEXT:
1004 WCHAR *data;
1005 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1006 if ((data = set_reply_data_size( count * sizeof(*data) )))
1008 for (i = 0; i < count; i++) data[i] = src[i].ch;
1011 break;
1012 case CHAR_INFO_MODE_ATTR:
1014 unsigned short *data;
1015 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1016 if ((data = set_reply_data_size( count * sizeof(*data) )))
1018 for (i = 0; i < count; i++) data[i] = src[i].attr;
1021 break;
1022 case CHAR_INFO_MODE_TEXTATTR:
1024 char_info_t *data;
1025 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1026 if ((data = set_reply_data_size( count * sizeof(*data) )))
1028 for (i = 0; i < count; i++) data[i] = src[i];
1031 break;
1032 default:
1033 set_error( STATUS_INVALID_PARAMETER );
1034 break;
1038 /* scroll parts of a screen buffer */
1039 static void scroll_console_output( handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1040 int w, int h )
1042 struct screen_buffer *screen_buffer;
1043 int j;
1044 char_info_t *psrc, *pdst;
1045 struct console_renderer_event evt;
1047 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1048 GENERIC_READ, &screen_buffer_ops )))
1049 return;
1050 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1051 xsrc + w > screen_buffer->width ||
1052 xdst + w > screen_buffer->width ||
1053 ysrc + h > screen_buffer->height ||
1054 ydst + h > screen_buffer->height ||
1055 w == 0 || h == 0)
1057 set_error( STATUS_INVALID_PARAMETER );
1058 release_object( screen_buffer );
1059 return;
1062 if (ysrc < ydst)
1064 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1065 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1067 for (j = h; j > 0; j--)
1069 memcpy(pdst, psrc, w * sizeof(*pdst) );
1070 pdst -= screen_buffer->width;
1071 psrc -= screen_buffer->width;
1074 else
1076 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1077 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1079 for (j = 0; j < h; j++)
1081 /* we use memmove here because when psrc and pdst are the same,
1082 * copies are done on the same row, so the dst and src blocks
1083 * can overlap */
1084 memmove( pdst, psrc, w * sizeof(*pdst) );
1085 pdst += screen_buffer->width;
1086 psrc += screen_buffer->width;
1090 /* FIXME: this could be enhanced, by signalling scroll */
1091 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1092 evt.u.update.top = min(ysrc, ydst);
1093 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1094 console_input_events_append( screen_buffer->input->evt, &evt );
1096 release_object( screen_buffer );
1099 /* allocate a console for the renderer */
1100 DECL_HANDLER(alloc_console)
1102 handle_t in = 0;
1103 handle_t evt = 0;
1104 struct process *process;
1105 struct process *renderer = current->process;
1106 struct console_input *console;
1108 process = (req->pid) ? get_process_from_id( req->pid ) :
1109 (struct process *)grab_object( renderer->parent );
1111 reply->handle_in = 0;
1112 reply->event = 0;
1113 if (!process) return;
1114 if (process != renderer && process->console)
1116 set_error( STATUS_ACCESS_DENIED );
1117 goto the_end;
1120 if ((console = (struct console_input*)create_console_input( current )))
1122 if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
1124 if ((evt = alloc_handle( renderer, console->evt,
1125 SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
1127 if (process != renderer)
1129 process->console = (struct console_input*)grab_object( console );
1130 console->num_proc++;
1132 reply->handle_in = in;
1133 reply->event = evt;
1134 release_object( console );
1135 goto the_end;
1137 close_handle( renderer, in, NULL );
1139 free_console( process );
1141 the_end:
1142 release_object( process );
1145 /* free the console of the current process */
1146 DECL_HANDLER(free_console)
1148 free_console( current->process );
1151 /* let the renderer peek the events it's waiting on */
1152 DECL_HANDLER(get_console_renderer_events)
1154 struct console_input_events *evt;
1156 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1157 GENERIC_WRITE, &console_input_events_ops );
1158 if (!evt) return;
1159 console_input_events_get( evt );
1160 release_object( evt );
1163 /* open a handle to the process console */
1164 DECL_HANDLER(open_console)
1166 struct object *obj = NULL;
1168 reply->handle = 0;
1169 switch (req->from)
1171 case 0:
1172 if (current->process->console && current->process->console->renderer)
1173 obj = grab_object( (struct object*)current->process->console );
1174 break;
1175 case 1:
1176 if (current->process->console && current->process->console->renderer &&
1177 current->process->console->active)
1178 obj = grab_object( (struct object*)current->process->console->active );
1179 break;
1180 default:
1181 if ((obj = get_handle_obj( current->process, (handle_t)req->from,
1182 GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1184 struct console_input* console = (struct console_input*)obj;
1185 obj = (console->active) ? grab_object( console->active ) : NULL;
1186 release_object( console );
1188 break;
1191 /* FIXME: req->share is not used (as in screen buffer creation) */
1192 if (obj)
1194 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1195 release_object( obj );
1197 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1200 /* set info about a console input */
1201 DECL_HANDLER(set_console_input_info)
1203 set_console_input_info( req, get_req_data(), get_req_data_size() );
1206 /* get info about a console (output only) */
1207 DECL_HANDLER(get_console_input_info)
1209 struct console_input *console;
1211 if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1212 if (console->title)
1214 size_t len = strlenW( console->title ) * sizeof(WCHAR);
1215 if (len > get_reply_max_size()) len = get_reply_max_size();
1216 set_reply_data( console->title, len );
1218 reply->history_mode = console->history_mode;
1219 reply->history_size = console->history_size;
1220 reply->history_index = console->history_index;
1221 release_object( console );
1224 /* get a console mode (input or output) */
1225 DECL_HANDLER(get_console_mode)
1227 reply->mode = get_console_mode( req->handle );
1230 /* set a console mode (input or output) */
1231 DECL_HANDLER(set_console_mode)
1233 set_console_mode( req->handle, req->mode );
1236 /* add input records to a console input queue */
1237 DECL_HANDLER(write_console_input)
1239 struct console_input *console;
1241 reply->written = 0;
1242 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1243 GENERIC_WRITE, &console_input_ops )))
1244 return;
1245 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1246 get_req_data() );
1247 release_object( console );
1250 /* fetch input records from a console input queue */
1251 DECL_HANDLER(read_console_input)
1253 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1254 reply->read = read_console_input( req->handle, count, req->flush );
1257 /* appends a string to console's history */
1258 DECL_HANDLER(append_console_input_history)
1260 struct console_input *console;
1262 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1263 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1264 release_object( console );
1267 /* appends a string to console's history */
1268 DECL_HANDLER(get_console_input_history)
1270 struct console_input *console;
1272 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1273 reply->total = console_input_get_hist( console, req->index );
1274 release_object( console );
1277 /* creates a screen buffer */
1278 DECL_HANDLER(create_console_output)
1280 struct console_input* console;
1281 struct screen_buffer* screen_buffer;
1283 if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1285 screen_buffer = create_console_output( console );
1286 if (screen_buffer)
1288 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1289 * see file.c on how this could be done */
1290 reply->handle_out = alloc_handle( current->process, screen_buffer,
1291 req->access, req->inherit );
1292 release_object( screen_buffer );
1294 release_object( console );
1297 /* set info about a console screen buffer */
1298 DECL_HANDLER(set_console_output_info)
1300 struct screen_buffer *screen_buffer;
1302 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1303 GENERIC_WRITE, &screen_buffer_ops)))
1305 set_console_output_info( screen_buffer, req );
1306 release_object( screen_buffer );
1310 /* get info about a console screen buffer */
1311 DECL_HANDLER(get_console_output_info)
1313 struct screen_buffer *screen_buffer;
1315 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1316 GENERIC_READ, &screen_buffer_ops)))
1318 reply->cursor_size = screen_buffer->cursor_size;
1319 reply->cursor_visible = screen_buffer->cursor_visible;
1320 reply->cursor_x = screen_buffer->cursor_x;
1321 reply->cursor_y = screen_buffer->cursor_y;
1322 reply->width = screen_buffer->width;
1323 reply->height = screen_buffer->height;
1324 reply->attr = screen_buffer->attr;
1325 reply->win_left = screen_buffer->win.left;
1326 reply->win_top = screen_buffer->win.top;
1327 reply->win_right = screen_buffer->win.right;
1328 reply->win_bottom = screen_buffer->win.bottom;
1329 reply->max_width = screen_buffer->max_width;
1330 reply->max_height = screen_buffer->max_height;
1331 release_object( screen_buffer );
1335 /* read data (chars & attrs) from a screen buffer */
1336 DECL_HANDLER(read_console_output)
1338 struct screen_buffer *screen_buffer;
1340 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1341 GENERIC_READ, &screen_buffer_ops )))
1343 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1344 reply->width = screen_buffer->width;
1345 reply->height = screen_buffer->height;
1346 release_object( screen_buffer );
1350 /* write data (char and/or attrs) to a screen buffer */
1351 DECL_HANDLER(write_console_output)
1353 struct screen_buffer *screen_buffer;
1355 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1356 GENERIC_WRITE, &screen_buffer_ops)))
1358 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1359 req->mode, req->x, req->y, req->wrap );
1360 reply->width = screen_buffer->width;
1361 reply->height = screen_buffer->height;
1362 release_object( screen_buffer );
1366 /* fill a screen buffer with constant data (chars and/or attributes) */
1367 DECL_HANDLER(fill_console_output)
1369 struct screen_buffer *screen_buffer;
1371 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1372 GENERIC_WRITE, &screen_buffer_ops)))
1374 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1375 req->x, req->y, req->count, req->wrap );
1376 release_object( screen_buffer );
1380 /* move a rect of data in a screen buffer */
1381 DECL_HANDLER(move_console_output)
1383 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1384 req->w, req->h );