2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
27 static void console_input_dump( struct object *obj, int verbose );
28 static void console_input_destroy( struct object *obj );
29 static int console_input_signaled( struct object *obj, struct thread *thread );
32 static int console_get_file_info( struct object *obj, struct get_file_info_request *req );
34 static const struct object_ops console_input_ops =
36 sizeof(struct console_input), /* size */
37 console_input_dump, /* dump */
38 add_queue, /* add_queue */
39 remove_queue, /* remove_queue */
40 console_input_signaled, /* signaled */
41 no_satisfied, /* satisfied */
42 NULL, /* get_poll_events */
43 NULL, /* poll_event */
44 no_get_fd, /* get_fd */
46 console_get_file_info, /* get_file_info */
47 console_input_destroy /* destroy */
50 static void console_input_events_dump( struct object *obj, int verbose );
51 static void console_input_events_destroy( struct object *obj );
52 static int console_input_events_signaled( struct object *obj, struct thread *thread );
54 struct console_input_events
56 struct object obj; /* object header */
57 int num_alloc; /* number of allocated events */
58 int num_used; /* number of actually used events */
59 struct console_renderer_event* events;
62 static const struct object_ops console_input_events_ops =
64 sizeof(struct console_input_events), /* size */
65 console_input_events_dump, /* dump */
66 add_queue, /* add_queue */
67 remove_queue, /* remove_queue */
68 console_input_events_signaled, /* signaled */
69 no_satisfied, /* satisfied */
70 NULL, /* get_poll_events */
71 NULL, /* poll_event */
72 no_get_fd, /* get_fd */
74 no_get_file_info, /* get_file_info */
75 console_input_events_destroy /* destroy */
80 struct object obj; /* object header */
81 int mode; /* output mode */
82 struct console_input *input; /* associated console input */
83 struct screen_buffer *next; /* linked list of all screen buffers */
84 short int cursor_size; /* size of cursor (percentage filled) */
85 short int cursor_visible;/* cursor visibility flag */
86 COORD cursor; /* position of cursor */
87 short int width; /* size (w-h) of the screen buffer */
89 short int max_width; /* size (w-h) of the window given font size */
91 unsigned *data; /* the data for each cell - a width x height matrix */
92 unsigned short attr; /* default attribute for screen buffer */
93 SMALL_RECT win; /* current visible window on the screen buffer *
94 * as seen in wineconsole */
97 static void screen_buffer_dump( struct object *obj, int verbose );
98 static void screen_buffer_destroy( struct object *obj );
100 static const struct object_ops screen_buffer_ops =
102 sizeof(struct screen_buffer), /* size */
103 screen_buffer_dump, /* dump */
104 no_add_queue, /* add_queue */
105 NULL, /* remove_queue */
107 NULL, /* satisfied */
108 NULL, /* get_poll_events */
109 NULL, /* poll_event */
110 no_get_fd, /* get_fd */
111 no_flush, /* flush */
112 console_get_file_info, /* get_file_info */
113 screen_buffer_destroy /* destroy */
116 static struct screen_buffer *screen_buffer_list;
118 /* dumps the renderer events of a console */
119 static void console_input_events_dump( struct object *obj, int verbose )
121 struct console_input_events *evts = (struct console_input_events *)obj;
122 assert( obj->ops == &console_input_events_ops );
123 fprintf( stderr, "Console input events: %d/%d events\n",
124 evts->num_used, evts->num_alloc );
127 /* destroys the renderer events of a console */
128 static void console_input_events_destroy( struct object *obj )
130 struct console_input_events *evts = (struct console_input_events *)obj;
131 assert( obj->ops == &console_input_events_ops );
132 free( evts->events );
135 /* the rendere events list is signaled when it's not empty */
136 static int console_input_events_signaled( struct object *obj, struct thread *thread )
138 struct console_input_events *evts = (struct console_input_events *)obj;
139 assert( obj->ops == &console_input_events_ops );
140 return evts->num_used ? 1 : 0;
143 /* add an event to the console's renderer events list */
144 static void console_input_events_append( struct console_input_events* evts,
145 struct console_renderer_event* evt)
149 /* to be done even when the renderer generates the events ? */
150 if (evts->num_used == evts->num_alloc)
152 evts->num_alloc += 16;
153 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
154 assert(evts->events);
156 evts->events[evts->num_used++] = *evt;
157 wake_up( &evts->obj, 0 );
160 /* retrieves events from the console's renderer events list */
161 static size_t console_input_events_get( struct console_input_events* evts,
162 struct console_renderer_event* evt, size_t num )
164 if (num % sizeof(*evt) != 0)
166 set_error( STATUS_INVALID_PARAMETER );
170 if (num > evts->num_used)
171 num = evts->num_used;
172 memcpy( evt, evts->events, num * sizeof(*evt) );
173 if (num < evts->num_used)
175 memmove( &evts->events[0], &evts->events[num],
176 (evts->num_used - num) * sizeof(*evt) );
178 evts->num_used -= num;
179 return num * sizeof(struct console_renderer_event);
182 static struct console_input_events *create_console_input_events(void)
184 struct console_input_events* evt;
186 if (!(evt = alloc_object( &console_input_events_ops, -1 ))) return NULL;
187 evt->num_alloc = evt->num_used = 0;
192 static struct object *create_console_input( struct process* renderer )
194 struct console_input *console_input;
196 if (!(console_input = alloc_object( &console_input_ops, -1 ))) return NULL;
197 console_input->renderer = renderer;
198 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
199 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
200 console_input->num_proc = 0;
201 console_input->active = NULL;
202 console_input->recnum = 0;
203 console_input->records = NULL;
204 console_input->evt = create_console_input_events();
205 console_input->title = NULL;
206 console_input->history_size = 50;
207 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
208 console_input->history_index = 0;
209 console_input->history_mode = 0;
211 if (!console_input->history || !console_input->evt)
213 release_object( console_input );
216 return &console_input->obj;
219 static struct object *create_console_output( struct console_input *console_input )
221 struct screen_buffer *screen_buffer;
222 struct console_renderer_event evt;
225 if (!(screen_buffer = alloc_object( &screen_buffer_ops, -1 ))) return NULL;
226 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
227 screen_buffer->input = console_input;
228 screen_buffer->cursor_size = 100;
229 screen_buffer->cursor_visible = 1;
230 screen_buffer->width = 80;
231 screen_buffer->height = 150;
232 screen_buffer->max_width = 80;
233 screen_buffer->max_height = 25;
234 screen_buffer->data = malloc( 4 * screen_buffer->width * screen_buffer->height );
235 /* fill the buffer with white on black spaces */
236 for (i = 0; i < screen_buffer->width * screen_buffer->height; i++)
238 screen_buffer->data[i] = 0x00F00020;
240 screen_buffer->cursor.X = 0;
241 screen_buffer->cursor.Y = 0;
242 screen_buffer->attr = 0xF0;
243 screen_buffer->win.Left = 0;
244 screen_buffer->win.Right = screen_buffer->max_width - 1;
245 screen_buffer->win.Top = 0;
246 screen_buffer->win.Bottom = screen_buffer->max_height - 1;
248 screen_buffer->next = screen_buffer_list;
249 screen_buffer_list = screen_buffer;
251 if (!console_input->active)
253 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
255 /* generate the fist events */
256 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
257 console_input_events_append( console_input->evt, &evt );
259 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
260 evt.u.resize.width = screen_buffer->width;
261 evt.u.resize.height = screen_buffer->height;
262 console_input_events_append( console_input->evt, &evt );
264 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
265 evt.u.display.left = screen_buffer->win.Left;
266 evt.u.display.top = screen_buffer->win.Top;
267 evt.u.display.width = screen_buffer->win.Right - screen_buffer->win.Left + 1;
268 evt.u.display.height = screen_buffer->win.Bottom - screen_buffer->win.Top + 1;
269 console_input_events_append( console_input->evt, &evt );
271 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
272 evt.u.update.top = 0;
273 evt.u.update.bottom = screen_buffer->height - 1;
274 console_input_events_append( console_input->evt, &evt );
276 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
277 evt.u.cursor_geom.size = screen_buffer->cursor_size;
278 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
279 console_input_events_append( console_input->evt, &evt );
281 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
282 evt.u.cursor_pos.x = screen_buffer->cursor.X;
283 evt.u.cursor_pos.y = screen_buffer->cursor.Y;
284 console_input_events_append( console_input->evt, &evt );
287 return &screen_buffer->obj;
290 /* free the console for this process */
291 int free_console( struct process *process )
293 struct console_input* console = process->console;
295 if (!console || !console->renderer) return 0;
297 process->console = NULL;
298 if (--console->num_proc == 0)
300 /* all processes have terminated... tell the renderer to terminate too */
301 struct console_renderer_event evt;
302 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
303 console_input_events_append( console->evt, &evt );
305 release_object( console );
310 /* let process inherit the console from parent... this handle two cases :
311 * 1/ generic console inheritance
312 * 2/ parent is a renderer which launches process, and process should attach to the console
313 * renderered by parent
315 void inherit_console(struct process *parent, struct process *process, handle_t hconin)
319 /* if parent is a renderer, then attach current process to its console
324 struct console_input* console;
326 if ((console = (struct console_input*)get_handle_obj( parent, hconin, 0, NULL )))
328 if (console->renderer == parent)
330 process->console = (struct console_input*)grab_object( console );
331 process->console->num_proc++;
334 release_object( console );
337 /* otherwise, if parent has a console, attach child to this console */
338 if (!done && parent->console)
340 assert(parent->console->renderer);
341 process->console = (struct console_input*)grab_object( parent->console );
342 process->console->num_proc++;
346 static struct console_input* console_input_get( handle_t handle, unsigned access )
348 struct console_input* console = 0;
351 console = (struct console_input *)get_handle_obj( current->process, handle,
352 access, &console_input_ops );
353 else if (current->process->console)
355 assert( current->process->console->renderer );
356 console = (struct console_input *)grab_object( current->process->console );
359 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
363 /* check if a console input is signaled: yes if non read input records */
364 static int console_input_signaled( struct object *obj, struct thread *thread )
366 struct console_input *console = (struct console_input *)obj;
367 assert( obj->ops == &console_input_ops );
368 return console->recnum ? 1 : 0;
371 static int get_console_mode( handle_t handle )
376 if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
378 if (obj->ops == &console_input_ops)
379 ret = ((struct console_input *)obj)->mode;
380 else if (obj->ops == &screen_buffer_ops)
381 ret = ((struct screen_buffer *)obj)->mode;
383 set_error( STATUS_OBJECT_TYPE_MISMATCH );
384 release_object( obj );
389 /* changes the mode of either a console input or a screen buffer */
390 static int set_console_mode( handle_t handle, int mode )
395 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
397 if (obj->ops == &console_input_ops)
399 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
400 ((struct console_input *)obj)->mode = mode;
403 else if (obj->ops == &screen_buffer_ops)
405 ((struct screen_buffer *)obj)->mode = mode;
408 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
409 release_object( obj );
413 /* add input events to a console input queue */
414 static int write_console_input( struct console_input* console, int count, INPUT_RECORD *records )
416 INPUT_RECORD *new_rec;
419 if (!(new_rec = realloc( console->records,
420 (console->recnum + count) * sizeof(INPUT_RECORD) )))
422 set_error( STATUS_NO_MEMORY );
423 release_object( console );
426 console->records = new_rec;
427 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
428 console->recnum += count;
430 /* wake up all waiters */
431 wake_up( &console->obj, 0 );
435 /* retrieve a pointer to the console input records */
436 static int read_console_input( handle_t handle, int count, INPUT_RECORD *rec, int flush )
438 struct console_input *console;
440 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
441 GENERIC_READ, &console_input_ops )))
446 /* special case: do not retrieve anything, but return
447 * the total number of records available */
448 count = console->recnum;
452 if (count > console->recnum) count = console->recnum;
453 memcpy( rec, console->records, count * sizeof(INPUT_RECORD) );
458 for (i = count; i < console->recnum; i++)
459 ((INPUT_RECORD*)console->records)[i-count] = ((INPUT_RECORD*)console->records)[i];
460 if ((console->recnum -= count) > 0)
462 INPUT_RECORD *new_rec = realloc( console->records,
463 console->recnum * sizeof(INPUT_RECORD) );
464 if (new_rec) console->records = new_rec;
468 free( console->records );
469 console->records = NULL;
472 release_object( console );
476 /* set misc console input information */
477 static int set_console_input_info( struct set_console_input_info_request *req,
478 const WCHAR *title, size_t len )
480 struct console_input *console;
481 struct console_renderer_event evt;
483 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) goto error;
485 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
487 struct screen_buffer *screen_buffer;
489 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
490 GENERIC_READ, &screen_buffer_ops );
491 if (!screen_buffer || screen_buffer->input != console)
493 set_error( STATUS_INVALID_PARAMETER );
494 if (screen_buffer) release_object( screen_buffer );
498 if (screen_buffer != console->active)
500 if (console->active) release_object( console->active );
501 console->active = screen_buffer;
502 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
503 console_input_events_append( console->evt, &evt );
506 release_object( screen_buffer );
508 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
510 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
513 memcpy( new_title, title, len + sizeof(WCHAR) );
514 new_title[len / sizeof(WCHAR)] = 0;
515 if (console->title) free( console->title );
516 console->title = new_title;
519 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
521 console->history_mode = req->history_mode;
523 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
524 console->history_size != req->history_size)
530 if (req->history_size)
532 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
533 if (!mem) goto error;
534 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
537 delta = (console->history_index > req->history_size) ?
538 (console->history_index - req->history_size) : 0;
540 for (i = delta; i < console->history_index; i++)
542 mem[i - delta] = console->history[i];
543 console->history[i] = NULL;
545 console->history_index -= delta;
547 for (i = 0; i < console->history_size; i++)
548 if (console->history[i]) free( console->history[i] );
549 free( console->history );
550 console->history = mem;
551 console->history_size = req->history_size;
553 release_object( console );
556 if (console) release_object( console );
560 /* set misc screen buffer information */
561 static int set_console_output_info( struct screen_buffer *screen_buffer,
562 struct set_console_output_info_request *req )
564 struct console_renderer_event evt;
566 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
568 if (req->cursor_size < 1 || req->cursor_size > 100)
570 set_error( STATUS_INVALID_PARAMETER );
573 if (screen_buffer->cursor_size != req->cursor_size ||
574 screen_buffer->cursor_visible != req->cursor_visible)
576 screen_buffer->cursor_size = req->cursor_size;
577 screen_buffer->cursor_visible = req->cursor_visible;
578 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
579 evt.u.cursor_geom.size = req->cursor_size;
580 evt.u.cursor_geom.visible = req->cursor_visible;
581 console_input_events_append( screen_buffer->input->evt, &evt );
584 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
586 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
587 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
589 set_error( STATUS_INVALID_PARAMETER );
592 if (screen_buffer->cursor.X != req->cursor_x || screen_buffer->cursor.Y != req->cursor_y)
594 screen_buffer->cursor.X = req->cursor_x;
595 screen_buffer->cursor.Y = req->cursor_y;
596 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
597 evt.u.cursor_pos.x = req->cursor_x;
598 evt.u.cursor_pos.y = req->cursor_y;
599 console_input_events_append( screen_buffer->input->evt, &evt );
602 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
605 /* FIXME: there are also some basic minimum and max size to deal with */
606 unsigned* new_data = mem_alloc( 4 * req->width * req->height );
608 if (!new_data) return 0;
610 /* fill the buffer with either the old buffer content or white on black spaces */
611 for (j = 0; j < req->height; j++)
613 for (i = 0; i < req->width; i++)
615 new_data[j * req->width + i] =
616 (i < screen_buffer->width && j < screen_buffer->height) ?
617 screen_buffer->data[j * screen_buffer->width + i] : 0x00F00020;
620 free( screen_buffer->data );
621 screen_buffer->data = new_data;
622 screen_buffer->width = req->width;
623 screen_buffer->height = req->height;
624 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
625 evt.u.resize.width = req->width;
626 evt.u.resize.height = req->height;
627 console_input_events_append( screen_buffer->input->evt, &evt );
629 if (screen_buffer == screen_buffer->input->active &&
630 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
633 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
634 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
635 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
636 write_console_input( screen_buffer->input, 1, &ir );
639 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
641 screen_buffer->attr = req->attr;
643 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
645 if (req->win_left < 0 || req->win_left > req->win_right ||
646 req->win_right >= screen_buffer->width ||
647 req->win_top < 0 || req->win_top > req->win_bottom ||
648 req->win_bottom >= screen_buffer->height)
650 set_error( STATUS_INVALID_PARAMETER );
653 if (screen_buffer->win.Left != req->win_left || screen_buffer->win.Top != req->win_top ||
654 screen_buffer->win.Right != req->win_right || screen_buffer->win.Bottom != req->win_bottom)
656 screen_buffer->win.Left = req->win_left;
657 screen_buffer->win.Top = req->win_top;
658 screen_buffer->win.Right = req->win_right;
659 screen_buffer->win.Bottom = req->win_bottom;
660 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
661 evt.u.display.left = req->win_left;
662 evt.u.display.top = req->win_top;
663 evt.u.display.width = req->win_right - req->win_left + 1;
664 evt.u.display.height = req->win_bottom - req->win_top + 1;
665 console_input_events_append( screen_buffer->input->evt, &evt );
668 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
670 /* can only be done by renderer */
671 if (current->process->console != screen_buffer->input)
673 set_error( STATUS_INVALID_PARAMETER );
677 screen_buffer->max_width = req->max_width;
678 screen_buffer->max_height = req->max_height;
684 /* appends a new line to history (history is a fixed size array) */
685 static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
687 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
691 set_error( STATUS_NO_MEMORY );
694 if (!console || !console->history_size)
696 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
700 memcpy( ptr, buf, len * sizeof(WCHAR) );
703 if (console->history_mode && console->history_index &&
704 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
706 /* ok, mode ask us to not use twice the same string...
707 * so just free mem and returns
709 set_error( STATUS_ALIAS_EXISTS );
714 if (console->history_index < console->history_size)
716 console->history[console->history_index++] = ptr;
720 free( console->history[0]) ;
721 memmove( &console->history[0], &console->history[1],
722 (console->history_size - 1) * sizeof(WCHAR*) );
723 console->history[console->history_size - 1] = ptr;
727 /* returns a line from the cachde */
728 static int console_input_get_hist( struct console_input* console, WCHAR* buf, size_t len, int index )
732 /* FIXME: don't use len yet */
733 if (!console || index >= console->history_index)
735 set_error( STATUS_INVALID_PARAMETER );
738 ret = strlenW(console->history[index]);
739 memcpy( buf, console->history[index], ret * sizeof(WCHAR) ); /* FIXME should use len */
744 static void console_input_dump( struct object *obj, int verbose )
746 struct console_input *console = (struct console_input *)obj;
747 assert( obj->ops == &console_input_ops );
748 fprintf( stderr, "Console input active=%p evt=%p\n",
749 console->active, console->evt );
752 static int console_get_file_info( struct object *obj, struct get_file_info_request *req )
756 req->type = FILE_TYPE_CHAR;
758 req->access_time = 0;
767 return FD_TYPE_CONSOLE;
770 static void console_input_destroy( struct object *obj )
772 struct console_input* console_in = (struct console_input *)obj;
773 struct screen_buffer* curr;
776 assert( obj->ops == &console_input_ops );
777 if (console_in->title) free( console_in->title );
778 if (console_in->records) free( console_in->records );
780 if (console_in->active) release_object( console_in->active );
781 console_in->active = NULL;
783 for (curr = screen_buffer_list; curr; curr = curr->next)
785 if (curr->input == console_in) curr->input = NULL;
788 release_object( console_in->evt );
789 console_in->evt = NULL;
791 for (i = 0; i < console_in->history_size; i++)
792 if (console_in->history[i]) free( console_in->history[i] );
793 if (console_in->history) free( console_in->history );
796 static void screen_buffer_dump( struct object *obj, int verbose )
798 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
799 assert( obj->ops == &screen_buffer_ops );
801 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
804 static void screen_buffer_destroy( struct object *obj )
806 struct screen_buffer* screen_buffer = (struct screen_buffer *)obj;
807 struct screen_buffer** psb;
809 assert( obj->ops == &screen_buffer_ops );
811 for (psb = &screen_buffer_list; *psb; *psb = (*psb)->next)
813 if (*psb == screen_buffer)
815 *psb = screen_buffer->next;
819 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
821 struct screen_buffer* sb;
822 for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
823 screen_buffer->input->active = sb;
827 /* write data into a screen buffer */
828 static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
829 const unsigned char* data, int mode, short int x, short int y )
831 int uniform = mode & WRITE_CONSOLE_MODE_UNIFORM;
836 mode &= ~WRITE_CONSOLE_MODE_UNIFORM;
838 if (mode < 0 || mode > 3)
840 set_error(STATUS_INVALID_PARAMETER);
844 /* set destination pointer and increment */
845 ptr = screen_buffer->data + (y * screen_buffer->width + x);
846 if (mode == WRITE_CONSOLE_MODE_ATTR) ptr = (unsigned*)((char*)ptr + 2);
847 inc = (mode == WRITE_CONSOLE_MODE_TEXTATTR) ? 4 : 2;
851 if (x + len > screen_buffer->width) len = screen_buffer->width - x;
853 for (i = 0; i < len; i++)
855 if (mode == WRITE_CONSOLE_MODE_TEXTSTDATTR)
857 memcpy( (char*)ptr + 2, &screen_buffer->attr, 2 );
859 memcpy( ptr++, data, inc );
860 if (!uniform) data += inc;
863 if (len && screen_buffer == screen_buffer->input->active)
866 struct console_renderer_event evt;
868 y2 = (y * screen_buffer->width + x + len - 1) / screen_buffer->width;
870 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
871 evt.u.update.top = y;
872 evt.u.update.bottom = y2;
873 console_input_events_append( screen_buffer->input->evt, &evt );
878 /* read data from a screen buffer */
879 static int read_console_output( struct screen_buffer *screen_buffer, size_t size, void* data,
880 short int x, short int y, short int w, short int h,
881 short int* eff_w, short int* eff_h )
885 if (size < w * h * 4 || x >= screen_buffer->width || y >= screen_buffer->height)
887 set_error(STATUS_INVALID_PARAMETER);
893 if (x + w > screen_buffer->width) *eff_w = screen_buffer->width - x;
894 if (y + h > screen_buffer->height) *eff_h = screen_buffer->height - y;
896 for (j = 0; j < *eff_h; j++)
898 memcpy( (char*)data + 4 * j * w, &screen_buffer->data[(y + j) * screen_buffer->width + x],
902 return *eff_w * *eff_h;
905 /* scroll parts of a screen buffer */
906 static void scroll_console_output( handle_t handle, short int xsrc, short int ysrc,
907 short int xdst, short int ydst, short int w, short int h )
909 struct screen_buffer *screen_buffer;
913 struct console_renderer_event evt;
915 if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
916 GENERIC_READ, &screen_buffer_ops )))
918 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
919 xsrc + w > screen_buffer->width ||
920 xdst + w > screen_buffer->width ||
921 ysrc + h > screen_buffer->height ||
922 ydst + h > screen_buffer->height ||
925 set_error( STATUS_INVALID_PARAMETER );
926 release_object( screen_buffer );
932 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
933 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
935 for (j = h; j > 0; j--)
937 memcpy(pdst, psrc, w * 4);
938 pdst -= screen_buffer->width;
939 psrc -= screen_buffer->width;
944 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
945 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
947 for (j = 0; j < h; j++)
949 /* we use memmove here because when psrc and pdst are the same,
950 * copies are done on the same row, so the dst and src blocks
952 memmove( pdst, psrc, w * 4 );
953 pdst += screen_buffer->width;
954 psrc += screen_buffer->width;
958 /* FIXME: this could be enhanced, by signalling scroll */
959 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
960 evt.u.update.top = min(ysrc, ydst);
961 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
962 console_input_events_append( screen_buffer->input->evt, &evt );
964 release_object( screen_buffer );
967 /* allocate a console for the renderer */
968 DECL_HANDLER(alloc_console)
972 struct process *process;
973 struct process *renderer = current->process;
974 struct console_input *console;
976 process = (req->pid) ? get_process_from_id( req->pid ) :
977 (struct process *)grab_object( renderer->parent );
981 if (!process) return;
982 if (process != renderer && process->console)
984 set_error( STATUS_ACCESS_DENIED );
988 if ((console = (struct console_input*)create_console_input( renderer )))
990 if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
992 if ((evt = alloc_handle( renderer, console->evt,
993 SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
995 if (process != renderer)
997 process->console = (struct console_input*)grab_object( console );
1000 req->handle_in = in;
1002 release_object( console );
1005 close_handle( renderer, in, NULL );
1007 free_console( process );
1010 release_object( process );
1013 /* free the console of the current process */
1014 DECL_HANDLER(free_console)
1016 free_console( current->process );
1019 /* let the renderer peek the events it's waiting on */
1020 DECL_HANDLER(get_console_renderer_events)
1022 struct console_input_events* evt;
1025 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1026 GENERIC_WRITE, &console_input_events_ops );
1028 len = console_input_events_get( evt, get_req_data(req), get_req_data_size(req) );
1029 set_req_data_size( req, len );
1030 release_object( evt );
1033 /* open a handle to the process console */
1034 DECL_HANDLER(open_console)
1036 struct object *obj = NULL;
1042 if (current->process->console && current->process->console->renderer)
1043 obj = grab_object( (struct object*)current->process->console );
1046 if (current->process->console && current->process->console->renderer &&
1047 current->process->console->active)
1048 obj = grab_object( (struct object*)current->process->console->active );
1051 if ((obj = get_handle_obj( current->process, (handle_t)req->from,
1052 GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
1054 struct console_input* console = (struct console_input*)obj;
1055 obj = (console->active) ? grab_object( console->active ) : NULL;
1056 release_object( console );
1061 /* FIXME: req->share is not used (as in screen buffer creation) */
1064 req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
1065 release_object( obj );
1067 if (!req->handle && !get_error()) set_error( STATUS_ACCESS_DENIED );
1070 /* set info about a console input */
1071 DECL_HANDLER(set_console_input_info)
1073 set_console_input_info( req, get_req_data(req), get_req_data_size(req) );
1076 /* get info about a console (output only) */
1077 DECL_HANDLER(get_console_input_info)
1079 struct console_input *console = 0;
1081 set_req_data_size( req, 0 );
1082 if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
1086 size_t len = strlenW( console->title ) * sizeof(WCHAR);
1087 if (len > get_req_data_size(req)) len = get_req_data_size(req);
1088 memcpy( get_req_data(req), console->title, len );
1089 set_req_data_size( req, len );
1091 req->history_mode = console->history_mode;
1092 req->history_size = console->history_size;
1093 req->history_index = console->history_index;
1095 release_object( console );
1098 /* get a console mode (input or output) */
1099 DECL_HANDLER(get_console_mode)
1101 req->mode = get_console_mode( req->handle );
1104 /* set a console mode (input or output) */
1105 DECL_HANDLER(set_console_mode)
1107 set_console_mode( req->handle, req->mode );
1110 /* add input records to a console input queue */
1111 DECL_HANDLER(write_console_input)
1113 struct console_input *console;
1116 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1117 GENERIC_WRITE, &console_input_ops )))
1120 req->written = write_console_input( console, get_req_data_size(req) / sizeof(INPUT_RECORD),
1121 get_req_data(req) );
1122 release_object( console );
1125 /* fetch input records from a console input queue */
1126 DECL_HANDLER(read_console_input)
1128 size_t size = get_req_data_size(req) / sizeof(INPUT_RECORD);
1129 int res = read_console_input( req->handle, size, get_req_data(req), req->flush );
1130 /* if size was 0 we didn't fetch anything */
1131 if (size) set_req_data_size( req, res * sizeof(INPUT_RECORD) );
1135 /* appends a string to console's history */
1136 DECL_HANDLER(append_console_input_history)
1138 struct console_input* console;
1140 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1141 console_input_append_hist( console, get_req_data(req),
1142 get_req_data_size(req) / sizeof(WCHAR) );
1143 release_object( console );
1146 /* appends a string to console's history */
1147 DECL_HANDLER(get_console_input_history)
1149 struct console_input* console;
1152 if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
1154 len = console_input_get_hist( console, get_req_data(req), 0 /* FIXME */, req->index );
1155 set_req_data_size( req, len * sizeof(WCHAR));
1156 release_object( console );
1159 /* creates a screen buffer */
1160 DECL_HANDLER(create_console_output)
1162 struct console_input* console;
1163 struct screen_buffer* screen_buffer;
1165 if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
1167 screen_buffer = (struct screen_buffer*)create_console_output( console );
1170 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1171 * see file.c on how this could be done
1173 req->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->inherit );
1174 release_object( screen_buffer );
1176 release_object( console );
1179 /* set info about a console screen buffer */
1180 DECL_HANDLER(set_console_output_info)
1182 struct screen_buffer *screen_buffer;
1184 if (!(screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1185 GENERIC_WRITE, &screen_buffer_ops )))
1188 set_console_output_info( screen_buffer, req );
1189 release_object( screen_buffer );
1192 /* get info about a console screen buffer */
1193 DECL_HANDLER(get_console_output_info)
1195 struct screen_buffer *screen_buffer;
1198 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1199 GENERIC_READ, &screen_buffer_ops )))
1201 req->cursor_size = screen_buffer->cursor_size;
1202 req->cursor_visible = screen_buffer->cursor_visible;
1203 req->cursor_x = screen_buffer->cursor.X;
1204 req->cursor_y = screen_buffer->cursor.Y;
1205 req->width = screen_buffer->width;
1206 req->height = screen_buffer->height;
1207 req->attr = screen_buffer->attr;
1208 req->win_left = screen_buffer->win.Left;
1209 req->win_top = screen_buffer->win.Top;
1210 req->win_right = screen_buffer->win.Right;
1211 req->win_bottom = screen_buffer->win.Bottom;
1212 req->max_width = screen_buffer->max_width;
1213 req->max_height = screen_buffer->max_height;
1215 release_object( screen_buffer );
1217 set_req_data_size( req, len );
1220 /* read data (chars & attrs) from a screen buffer */
1221 DECL_HANDLER(read_console_output)
1223 struct screen_buffer *screen_buffer;
1224 size_t size = get_req_data_size(req);
1227 if (!(screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1228 GENERIC_READ, &screen_buffer_ops )))
1231 res = read_console_output( screen_buffer, size, get_req_data(req),
1232 req->x, req->y, req->w, req->h, &req->eff_w, &req->eff_h);
1233 /* if size was 0 we didn't fetch anything */
1234 if (size) set_req_data_size( req, res * 4 );
1235 release_object( screen_buffer );
1238 /* write data (char and/or attrs) to a screen buffer */
1239 DECL_HANDLER(write_console_output)
1241 struct screen_buffer *screen_buffer;
1242 size_t size = get_req_data_size(req);
1245 if (!(screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1246 GENERIC_WRITE, &screen_buffer_ops )))
1249 res = write_console_output( screen_buffer, size, get_req_data(req), req->mode, req->x, req->y );
1251 /* if size was 0 we didn't fetch anything */
1252 if (size) set_req_data_size( req, res );
1254 release_object( screen_buffer );
1257 /* move a rect of data in a screen buffer */
1258 DECL_HANDLER(move_console_output)
1260 scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,