Fixed buffer handling of some locale functions (based on a patch by
[wine/multimedia.git] / server / console.c
blobfac9960643b4f5d19e733f0d733418e388506de9
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;
535 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
536 console_input_events_append( console->evt, &evt );
539 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
541 console->history_mode = req->history_mode;
543 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
544 console->history_size != req->history_size)
546 WCHAR** mem = NULL;
547 int i;
548 int delta;
550 if (req->history_size)
552 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
553 if (!mem) goto error;
554 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
557 delta = (console->history_index > req->history_size) ?
558 (console->history_index - req->history_size) : 0;
560 for (i = delta; i < console->history_index; i++)
562 mem[i - delta] = console->history[i];
563 console->history[i] = NULL;
565 console->history_index -= delta;
567 for (i = 0; i < console->history_size; i++)
568 if (console->history[i]) free( console->history[i] );
569 free( console->history );
570 console->history = mem;
571 console->history_size = req->history_size;
573 release_object( console );
574 return 1;
575 error:
576 if (console) release_object( console );
577 return 0;
580 /* resize a screen buffer */
581 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
582 int new_width, int new_height )
584 int i, old_width, old_height, copy_width, copy_height;
585 char_info_t *new_data;
587 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
589 set_error( STATUS_NO_MEMORY );
590 return 0;
592 old_width = screen_buffer->width;
593 old_height = screen_buffer->height;
594 copy_width = min( old_width, new_width );
595 copy_height = min( old_height, new_height );
597 /* copy all the rows */
598 for (i = 0; i < copy_height; i++)
600 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
601 copy_width * sizeof(char_info_t) );
604 /* clear the end of each row */
605 if (new_width > old_width)
607 /* fill first row */
608 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
609 /* and blast it to the other rows */
610 for (i = 1; i < copy_height; i++)
611 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
612 (new_width - old_width) * sizeof(char_info_t) );
615 /* clear remaining rows */
616 if (new_height > old_height)
618 /* fill first row */
619 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
620 /* and blast it to the other rows */
621 for (i = old_height+1; i < new_height; i++)
622 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
623 new_width * sizeof(char_info_t) );
625 free( screen_buffer->data );
626 screen_buffer->data = new_data;
627 screen_buffer->width = new_width;
628 screen_buffer->height = new_height;
629 return 1;
632 /* set misc screen buffer information */
633 static int set_console_output_info( struct screen_buffer *screen_buffer,
634 const struct set_console_output_info_request *req )
636 struct console_renderer_event evt;
638 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
640 if (req->cursor_size < 1 || req->cursor_size > 100)
642 set_error( STATUS_INVALID_PARAMETER );
643 return 0;
645 if (screen_buffer->cursor_size != req->cursor_size ||
646 screen_buffer->cursor_visible != req->cursor_visible)
648 screen_buffer->cursor_size = req->cursor_size;
649 screen_buffer->cursor_visible = req->cursor_visible;
650 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
651 evt.u.cursor_geom.size = req->cursor_size;
652 evt.u.cursor_geom.visible = req->cursor_visible;
653 console_input_events_append( screen_buffer->input->evt, &evt );
656 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
658 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
659 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
661 set_error( STATUS_INVALID_PARAMETER );
662 return 0;
664 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
666 screen_buffer->cursor_x = req->cursor_x;
667 screen_buffer->cursor_y = req->cursor_y;
668 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
669 evt.u.cursor_pos.x = req->cursor_x;
670 evt.u.cursor_pos.y = req->cursor_y;
671 console_input_events_append( screen_buffer->input->evt, &evt );
674 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
676 /* FIXME: there are also some basic minimum and max size to deal with */
677 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
679 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
680 evt.u.resize.width = req->width;
681 evt.u.resize.height = req->height;
682 console_input_events_append( screen_buffer->input->evt, &evt );
684 if (screen_buffer == screen_buffer->input->active &&
685 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
687 INPUT_RECORD ir;
688 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
689 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
690 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
691 write_console_input( screen_buffer->input, 1, &ir );
694 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
696 screen_buffer->attr = req->attr;
698 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
700 if (req->win_left < 0 || req->win_left > req->win_right ||
701 req->win_right >= screen_buffer->width ||
702 req->win_top < 0 || req->win_top > req->win_bottom ||
703 req->win_bottom >= screen_buffer->height)
705 set_error( STATUS_INVALID_PARAMETER );
706 return 0;
708 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
709 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
711 screen_buffer->win.left = req->win_left;
712 screen_buffer->win.top = req->win_top;
713 screen_buffer->win.right = req->win_right;
714 screen_buffer->win.bottom = req->win_bottom;
715 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
716 evt.u.display.left = req->win_left;
717 evt.u.display.top = req->win_top;
718 evt.u.display.width = req->win_right - req->win_left + 1;
719 evt.u.display.height = req->win_bottom - req->win_top + 1;
720 console_input_events_append( screen_buffer->input->evt, &evt );
723 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
725 /* can only be done by renderer */
726 if (current->process->console != screen_buffer->input)
728 set_error( STATUS_INVALID_PARAMETER );
729 return 0;
732 screen_buffer->max_width = req->max_width;
733 screen_buffer->max_height = req->max_height;
736 return 1;
739 /* appends a new line to history (history is a fixed size array) */
740 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
742 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
744 if (!ptr)
746 set_error( STATUS_NO_MEMORY );
747 return;
749 if (!console || !console->history_size)
751 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
752 return;
755 memcpy( ptr, buf, len * sizeof(WCHAR) );
756 ptr[len] = 0;
758 if (console->history_mode && console->history_index &&
759 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
761 /* ok, mode ask us to not use twice the same string...
762 * so just free mem and returns
764 set_error( STATUS_ALIAS_EXISTS );
765 free(ptr);
766 return;
769 if (console->history_index < console->history_size)
771 console->history[console->history_index++] = ptr;
773 else
775 free( console->history[0]) ;
776 memmove( &console->history[0], &console->history[1],
777 (console->history_size - 1) * sizeof(WCHAR*) );
778 console->history[console->history_size - 1] = ptr;
782 /* returns a line from the cachde */
783 static size_t console_input_get_hist( struct console_input *console, int index )
785 size_t ret = 0;
787 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
788 else
790 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
791 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
793 return ret;
796 /* dumb dump */
797 static void console_input_dump( struct object *obj, int verbose )
799 struct console_input *console = (struct console_input *)obj;
800 assert( obj->ops == &console_input_ops );
801 fprintf( stderr, "Console input active=%p evt=%p\n",
802 console->active, console->evt );
805 static int console_get_file_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
807 if (reply)
809 reply->type = FILE_TYPE_CHAR;
810 reply->attr = 0;
811 reply->access_time = 0;
812 reply->write_time = 0;
813 reply->size_high = 0;
814 reply->size_low = 0;
815 reply->links = 0;
816 reply->index_high = 0;
817 reply->index_low = 0;
818 reply->serial = 0;
820 *flags = 0;
821 return FD_TYPE_CONSOLE;
824 static void console_input_destroy( struct object *obj )
826 struct console_input* console_in = (struct console_input *)obj;
827 struct screen_buffer* curr;
828 int i;
830 assert( obj->ops == &console_input_ops );
831 if (console_in->title) free( console_in->title );
832 if (console_in->records) free( console_in->records );
834 if (console_in->active) release_object( console_in->active );
835 console_in->active = NULL;
837 for (curr = screen_buffer_list; curr; curr = curr->next)
839 if (curr->input == console_in) curr->input = NULL;
842 release_object( console_in->evt );
843 console_in->evt = NULL;
845 for (i = 0; i < console_in->history_size; i++)
846 if (console_in->history[i]) free( console_in->history[i] );
847 if (console_in->history) free( console_in->history );
850 static void screen_buffer_dump( struct object *obj, int verbose )
852 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
853 assert( obj->ops == &screen_buffer_ops );
855 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
858 static void screen_buffer_destroy( struct object *obj )
860 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
862 assert( obj->ops == &screen_buffer_ops );
864 if (screen_buffer->next) screen_buffer->next->prev = screen_buffer->prev;
865 if (screen_buffer->prev) screen_buffer->prev->next = screen_buffer->next;
866 else screen_buffer_list = screen_buffer->next;
868 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
870 struct screen_buffer* sb;
871 for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
872 screen_buffer->input->active = sb;
874 if (screen_buffer->data) free( screen_buffer->data );
877 /* write data into a screen buffer */
878 static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
879 const void* data, enum char_info_mode mode,
880 int x, int y, int wrap )
882 int i;
883 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
885 if (y >= screen_buffer->height) return 0;
887 if (wrap)
888 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
889 else
890 end = screen_buffer->data + (y+1) * screen_buffer->width;
892 switch(mode)
894 case CHAR_INFO_MODE_TEXT:
896 const WCHAR *ptr = data;
897 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
899 break;
900 case CHAR_INFO_MODE_ATTR:
902 const unsigned short *ptr = data;
903 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
905 break;
906 case CHAR_INFO_MODE_TEXTATTR:
908 const char_info_t *ptr = data;
909 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
911 break;
912 case CHAR_INFO_MODE_TEXTSTDATTR:
914 const WCHAR *ptr = data;
915 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
917 dest->ch = ptr[i];
918 dest->attr = screen_buffer->attr;
921 break;
922 default:
923 set_error( STATUS_INVALID_PARAMETER );
924 return 0;
927 if (i && screen_buffer == screen_buffer->input->active)
929 struct console_renderer_event evt;
930 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
931 evt.u.update.top = y;
932 evt.u.update.bottom = (y * screen_buffer->width + x + i - 1) / screen_buffer->width;
933 console_input_events_append( screen_buffer->input->evt, &evt );
935 return i;
938 /* fill a screen buffer with uniform data */
939 static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
940 enum char_info_mode mode, int x, int y, int count, int wrap )
942 int i;
943 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
945 if (y >= screen_buffer->height) return 0;
947 if (wrap)
948 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
949 else
950 end = screen_buffer->data + (y+1) * screen_buffer->width;
952 if (count > end - dest) count = end - dest;
954 switch(mode)
956 case CHAR_INFO_MODE_TEXT:
957 for (i = 0; i < count; i++) dest[i].ch = data.ch;
958 break;
959 case CHAR_INFO_MODE_ATTR:
960 for (i = 0; i < count; i++) dest[i].attr = data.attr;
961 break;
962 case CHAR_INFO_MODE_TEXTATTR:
963 for (i = 0; i < count; i++) dest[i] = data;
964 break;
965 case CHAR_INFO_MODE_TEXTSTDATTR:
966 for (i = 0; i < count; i++)
968 dest[i].ch = data.ch;
969 dest[i].attr = screen_buffer->attr;
971 break;
972 default:
973 set_error( STATUS_INVALID_PARAMETER );
974 return 0;
977 if (count && screen_buffer == screen_buffer->input->active)
979 struct console_renderer_event evt;
980 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
981 evt.u.update.top = y;
982 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
983 console_input_events_append( screen_buffer->input->evt, &evt );
985 return i;
988 /* read data from a screen buffer */
989 static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
990 enum char_info_mode mode, int wrap )
992 int i;
993 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
995 if (y >= screen_buffer->height) return;
997 if (wrap)
998 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
999 else
1000 end = screen_buffer->data + (y+1) * screen_buffer->width;
1002 switch(mode)
1004 case CHAR_INFO_MODE_TEXT:
1006 WCHAR *data;
1007 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1008 if ((data = set_reply_data_size( count * sizeof(*data) )))
1010 for (i = 0; i < count; i++) data[i] = src[i].ch;
1013 break;
1014 case CHAR_INFO_MODE_ATTR:
1016 unsigned short *data;
1017 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1018 if ((data = set_reply_data_size( count * sizeof(*data) )))
1020 for (i = 0; i < count; i++) data[i] = src[i].attr;
1023 break;
1024 case CHAR_INFO_MODE_TEXTATTR:
1026 char_info_t *data;
1027 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1028 if ((data = set_reply_data_size( count * sizeof(*data) )))
1030 for (i = 0; i < count; i++) data[i] = src[i];
1033 break;
1034 default:
1035 set_error( STATUS_INVALID_PARAMETER );
1036 break;
1040 /* scroll parts of a screen buffer */
1041 static void scroll_console_output( handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
1042 int w, int h )
1044 struct screen_buffer *screen_buffer;
1045 int j;
1046 char_info_t *psrc, *pdst;
1047 struct console_renderer_event evt;
1049 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
1050 GENERIC_READ, &screen_buffer_ops )))
1051 return;
1052 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1053 xsrc + w > screen_buffer->width ||
1054 xdst + w > screen_buffer->width ||
1055 ysrc + h > screen_buffer->height ||
1056 ydst + h > screen_buffer->height ||
1057 w == 0 || h == 0)
1059 set_error( STATUS_INVALID_PARAMETER );
1060 release_object( screen_buffer );
1061 return;
1064 if (ysrc < ydst)
1066 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1067 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1069 for (j = h; j > 0; j--)
1071 memcpy(pdst, psrc, w * sizeof(*pdst) );
1072 pdst -= screen_buffer->width;
1073 psrc -= screen_buffer->width;
1076 else
1078 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1079 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1081 for (j = 0; j < h; j++)
1083 /* we use memmove here because when psrc and pdst are the same,
1084 * copies are done on the same row, so the dst and src blocks
1085 * can overlap */
1086 memmove( pdst, psrc, w * sizeof(*pdst) );
1087 pdst += screen_buffer->width;
1088 psrc += screen_buffer->width;
1092 /* FIXME: this could be enhanced, by signalling scroll */
1093 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1094 evt.u.update.top = min(ysrc, ydst);
1095 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1096 console_input_events_append( screen_buffer->input->evt, &evt );
1098 release_object( screen_buffer );
1101 /* allocate a console for the renderer */
1102 DECL_HANDLER(alloc_console)
1104 handle_t in = 0;
1105 handle_t evt = 0;
1106 struct process *process;
1107 struct process *renderer = current->process;
1108 struct console_input *console;
1110 process = (req->pid) ? get_process_from_id( req->pid ) :
1111 (struct process *)grab_object( renderer->parent );
1113 reply->handle_in = 0;
1114 reply->event = 0;
1115 if (!process) return;
1116 if (process != renderer && process->console)
1118 set_error( STATUS_ACCESS_DENIED );
1119 goto the_end;
1122 if ((console = (struct console_input*)create_console_input( current )))
1124 if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
1126 if ((evt = alloc_handle( renderer, console->evt,
1127 SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
1129 if (process != renderer)
1131 process->console = (struct console_input*)grab_object( console );
1132 console->num_proc++;
1134 reply->handle_in = in;
1135 reply->event = evt;
1136 release_object( console );
1137 goto the_end;
1139 close_handle( renderer, in, NULL );
1141 free_console( process );
1143 the_end:
1144 release_object( process );
1147 /* free the console of the current process */
1148 DECL_HANDLER(free_console)
1150 free_console( current->process );
1153 /* let the renderer peek the events it's waiting on */
1154 DECL_HANDLER(get_console_renderer_events)
1156 struct console_input_events *evt;
1158 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1159 GENERIC_WRITE, &console_input_events_ops );
1160 if (!evt) return;
1161 console_input_events_get( evt );
1162 release_object( evt );
1165 /* open a handle to the process console */
1166 DECL_HANDLER(open_console)
1168 struct object *obj = NULL;
1170 reply->handle = 0;
1171 switch (req->from)
1173 case 0:
1174 if (current->process->console && current->process->console->renderer)
1175 obj = grab_object( (struct object*)current->process->console );
1176 break;
1177 case 1:
1178 if (current->process->console && current->process->console->renderer &&
1179 current->process->console->active)
1180 obj = grab_object( (struct object*)current->process->console->active );
1181 break;
1182 default:
1183 if ((obj = get_handle_obj( current->process, (handle_t)req->from,
1184 GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1186 struct console_input* console = (struct console_input*)obj;
1187 obj = (console->active) ? grab_object( console->active ) : NULL;
1188 release_object( console );
1190 break;
1193 /* FIXME: req->share is not used (as in screen buffer creation) */
1194 if (obj)
1196 reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1197 release_object( obj );
1199 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1202 /* set info about a console input */
1203 DECL_HANDLER(set_console_input_info)
1205 set_console_input_info( req, get_req_data(), get_req_data_size() );
1208 /* get info about a console (output only) */
1209 DECL_HANDLER(get_console_input_info)
1211 struct console_input *console;
1213 if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1214 if (console->title)
1216 size_t len = strlenW( console->title ) * sizeof(WCHAR);
1217 if (len > get_reply_max_size()) len = get_reply_max_size();
1218 set_reply_data( console->title, len );
1220 reply->history_mode = console->history_mode;
1221 reply->history_size = console->history_size;
1222 reply->history_index = console->history_index;
1223 release_object( console );
1226 /* get a console mode (input or output) */
1227 DECL_HANDLER(get_console_mode)
1229 reply->mode = get_console_mode( req->handle );
1232 /* set a console mode (input or output) */
1233 DECL_HANDLER(set_console_mode)
1235 set_console_mode( req->handle, req->mode );
1238 /* add input records to a console input queue */
1239 DECL_HANDLER(write_console_input)
1241 struct console_input *console;
1243 reply->written = 0;
1244 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1245 GENERIC_WRITE, &console_input_ops )))
1246 return;
1247 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1248 get_req_data() );
1249 release_object( console );
1252 /* fetch input records from a console input queue */
1253 DECL_HANDLER(read_console_input)
1255 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1256 reply->read = read_console_input( req->handle, count, req->flush );
1259 /* appends a string to console's history */
1260 DECL_HANDLER(append_console_input_history)
1262 struct console_input *console;
1264 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1265 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1266 release_object( console );
1269 /* appends a string to console's history */
1270 DECL_HANDLER(get_console_input_history)
1272 struct console_input *console;
1274 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1275 reply->total = console_input_get_hist( console, req->index );
1276 release_object( console );
1279 /* creates a screen buffer */
1280 DECL_HANDLER(create_console_output)
1282 struct console_input* console;
1283 struct screen_buffer* screen_buffer;
1285 if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1287 screen_buffer = create_console_output( console );
1288 if (screen_buffer)
1290 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1291 * see file.c on how this could be done */
1292 reply->handle_out = alloc_handle( current->process, screen_buffer,
1293 req->access, req->inherit );
1294 release_object( screen_buffer );
1296 release_object( console );
1299 /* set info about a console screen buffer */
1300 DECL_HANDLER(set_console_output_info)
1302 struct screen_buffer *screen_buffer;
1304 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1305 GENERIC_WRITE, &screen_buffer_ops)))
1307 set_console_output_info( screen_buffer, req );
1308 release_object( screen_buffer );
1312 /* get info about a console screen buffer */
1313 DECL_HANDLER(get_console_output_info)
1315 struct screen_buffer *screen_buffer;
1317 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1318 GENERIC_READ, &screen_buffer_ops)))
1320 reply->cursor_size = screen_buffer->cursor_size;
1321 reply->cursor_visible = screen_buffer->cursor_visible;
1322 reply->cursor_x = screen_buffer->cursor_x;
1323 reply->cursor_y = screen_buffer->cursor_y;
1324 reply->width = screen_buffer->width;
1325 reply->height = screen_buffer->height;
1326 reply->attr = screen_buffer->attr;
1327 reply->win_left = screen_buffer->win.left;
1328 reply->win_top = screen_buffer->win.top;
1329 reply->win_right = screen_buffer->win.right;
1330 reply->win_bottom = screen_buffer->win.bottom;
1331 reply->max_width = screen_buffer->max_width;
1332 reply->max_height = screen_buffer->max_height;
1333 release_object( screen_buffer );
1337 /* read data (chars & attrs) from a screen buffer */
1338 DECL_HANDLER(read_console_output)
1340 struct screen_buffer *screen_buffer;
1342 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1343 GENERIC_READ, &screen_buffer_ops )))
1345 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1346 reply->width = screen_buffer->width;
1347 reply->height = screen_buffer->height;
1348 release_object( screen_buffer );
1352 /* write data (char and/or attrs) to a screen buffer */
1353 DECL_HANDLER(write_console_output)
1355 struct screen_buffer *screen_buffer;
1357 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1358 GENERIC_WRITE, &screen_buffer_ops)))
1360 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1361 req->mode, req->x, req->y, req->wrap );
1362 reply->width = screen_buffer->width;
1363 reply->height = screen_buffer->height;
1364 release_object( screen_buffer );
1368 /* fill a screen buffer with constant data (chars and/or attributes) */
1369 DECL_HANDLER(fill_console_output)
1371 struct screen_buffer *screen_buffer;
1373 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1374 GENERIC_WRITE, &screen_buffer_ops)))
1376 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1377 req->x, req->y, req->count, req->wrap );
1378 release_object( screen_buffer );
1382 /* move a rect of data in a screen buffer */
1383 DECL_HANDLER(move_console_output)
1385 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
1386 req->w, req->h );