1 /*
2 * Server-side console management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
5 * 2001 Eric Pouech
6 *
7 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
31
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "handle.h"
35 #include "process.h"
36 #include "request.h"
37 #include "unicode.h"
38 #include "wincon.h"
39 #include "winternl.h"
40
41 /* specific access rights (FIXME: should use finer-grained access rights) */
42 #define CONSOLE_READ 0x01
43 #define CONSOLE_WRITE 0x02
44
45 struct screen_buffer;
46 struct console_input_events;
47
48 struct console_input
49 {
50 struct object obj; /* object header */
51 int num_proc; /* number of processes attached to this console */
52 struct thread *renderer; /* console renderer thread */
53 int mode; /* input mode */
54 struct screen_buffer *active; /* active screen buffer */
55 int recnum; /* number of input records */
56 INPUT_RECORD *records; /* input records */
57 struct console_input_events *evt; /* synchronization event with renderer */
58 WCHAR *title; /* console title */
59 WCHAR **history; /* lines history */
60 int history_size; /* number of entries in history array */
61 int history_index; /* number of used entries in history array */
62 int history_mode; /* mode of history (non zero means remove doubled strings */
63 int edition_mode; /* index to edition mode flavors */
64 int input_cp; /* console input codepage */
65 int output_cp; /* console output codepage */
66 user_handle_t win; /* window handle if backend supports it */
67 struct event *event; /* event to wait on for input queue */
68 };
69
70 static unsigned int console_map_access( struct object *obj, unsigned int access );
71
72 static void console_input_dump( struct object *obj, int verbose );
73 static void console_input_destroy( struct object *obj );
74
75 static const struct object_ops console_input_ops =
76 {
77 sizeof(struct console_input), /* size */
78 console_input_dump, /* dump */
79 no_get_type, /* get_type */
80 no_add_queue, /* add_queue */
81 NULL, /* remove_queue */
82 NULL, /* signaled */
83 no_satisfied, /* satisfied */
84 no_signal, /* signal */
85 no_get_fd, /* get_fd */
86 console_map_access, /* map_access */
87 default_get_sd, /* get_sd */
88 default_set_sd, /* set_sd */
89 no_lookup_name, /* lookup_name */
90 no_open_file, /* open_file */
91 no_close_handle, /* close_handle */
92 console_input_destroy /* destroy */
93 };
94
95 static void console_input_events_dump( struct object *obj, int verbose );
96 static void console_input_events_destroy( struct object *obj );
97 static int console_input_events_signaled( struct object *obj, struct thread *thread );
98
99 struct console_input_events
100 {
101 struct object obj; /* object header */
102 int num_alloc; /* number of allocated events */
103 int num_used; /* number of actually used events */
104 struct console_renderer_event* events;
105 };
106
107 static const struct object_ops console_input_events_ops =
108 {
109 sizeof(struct console_input_events), /* size */
110 console_input_events_dump, /* dump */
111 no_get_type, /* get_type */
112 add_queue, /* add_queue */
113 remove_queue, /* remove_queue */
114 console_input_events_signaled, /* signaled */
115 no_satisfied, /* satisfied */
116 no_signal, /* signal */
117 no_get_fd, /* get_fd */
118 console_map_access, /* map_access */
119 default_get_sd, /* get_sd */
120 default_set_sd, /* set_sd */
121 no_lookup_name, /* lookup_name */
122 no_open_file, /* open_file */
123 no_close_handle, /* close_handle */
124 console_input_events_destroy /* destroy */
125 };
126
127 struct screen_buffer
128 {
129 struct object obj; /* object header */
130 struct list entry; /* entry in list of all screen buffers */
131 struct console_input *input; /* associated console input */
132 int mode; /* output mode */
133 int cursor_size; /* size of cursor (percentage filled) */
134 int cursor_visible;/* cursor visibility flag */
135 int cursor_x; /* position of cursor */
136 int cursor_y; /* position of cursor */
137 int width; /* size (w-h) of the screen buffer */
138 int height;
139 int max_width; /* size (w-h) of the window given font size */
140 int max_height;
141 char_info_t *data; /* the data for each cell - a width x height matrix */
142 unsigned short attr; /* default attribute for screen buffer */
143 rectangle_t win; /* current visible window on the screen buffer *
144 * as seen in wineconsole */
145 };
146
147 static void screen_buffer_dump( struct object *obj, int verbose );
148 static void screen_buffer_destroy( struct object *obj );
149
150 static const struct object_ops screen_buffer_ops =
151 {
152 sizeof(struct screen_buffer), /* size */
153 screen_buffer_dump, /* dump */
154 no_get_type, /* get_type */
155 no_add_queue, /* add_queue */
156 NULL, /* remove_queue */
157 NULL, /* signaled */
158 NULL, /* satisfied */
159 no_signal, /* signal */
160 no_get_fd, /* get_fd */
161 console_map_access, /* map_access */
162 default_get_sd, /* get_sd */
163 default_set_sd, /* set_sd */
164 no_lookup_name, /* lookup_name */
165 no_open_file, /* open_file */
166 no_close_handle, /* close_handle */
167 screen_buffer_destroy /* destroy */
168 };
169
170 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
171
172 static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
173
174 /* access mapping for all console objects */
175 static unsigned int console_map_access( struct object *obj, unsigned int access )
176 {
177 if (access & GENERIC_READ) access |= SYNCHRONIZE | STANDARD_RIGHTS_READ | CONSOLE_READ;
178 if (access & GENERIC_WRITE) access |= SYNCHRONIZE | STANDARD_RIGHTS_WRITE | CONSOLE_WRITE;
179 if (access & GENERIC_EXECUTE) access |= SYNCHRONIZE | STANDARD_RIGHTS_EXECUTE;
180 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | CONSOLE_READ | CONSOLE_WRITE;
181 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
182 }
183
184 /* dumps the renderer events of a console */
185 static void console_input_events_dump( struct object *obj, int verbose )
186 {
187 struct console_input_events *evts = (struct console_input_events *)obj;
188 assert( obj->ops == &console_input_events_ops );
189 fprintf( stderr, "Console input events: %d/%d events\n",
190 evts->num_used, evts->num_alloc );
191 }
192
193 /* destroys the renderer events of a console */
194 static void console_input_events_destroy( struct object *obj )
195 {
196 struct console_input_events *evts = (struct console_input_events *)obj;
197 assert( obj->ops == &console_input_events_ops );
198 free( evts->events );
199 }
200
201 /* the renderer events list is signaled when it's not empty */
202 static int console_input_events_signaled( struct object *obj, struct thread *thread )
203 {
204 struct console_input_events *evts = (struct console_input_events *)obj;
205 assert( obj->ops == &console_input_events_ops );
206 return (evts->num_used != 0);
207 }
208
209 /* add an event to the console's renderer events list */
210 static void console_input_events_append( struct console_input_events* evts,
211 struct console_renderer_event* evt)
212 {
213 int collapsed = FALSE;
214
215 /* to be done even when evt has been generated by the rendere ? */
216
217 /* try to collapse evt into current queue's events */
218 if (evts->num_used)
219 {
220 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
221
222 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
223 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
224 {
225 /* if two update events overlap, collapse them into a single one */
226 if (last->u.update.bottom + 1 >= evt->u.update.top &&
227 evt->u.update.bottom + 1 >= last->u.update.top)
228 {
229 last->u.update.top = min(last->u.update.top, evt->u.update.top);
230 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
231 collapsed = TRUE;
232 }
233 }
234 }
235 if (!collapsed)
236 {
237 if (evts->num_used == evts->num_alloc)
238 {
239 evts->num_alloc += 16;
240 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
241 assert(evts->events);
242 }
243 evts->events[evts->num_used++] = *evt;
244 }
245 wake_up( &evts->obj, 0 );
246 }
247
248 /* retrieves events from the console's renderer events list */
249 static void console_input_events_get( struct console_input_events* evts )
250 {
251 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
252
253 if (num > evts->num_used) num = evts->num_used;
254 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
255 if (num < evts->num_used)
256 {
257 memmove( &evts->events[0], &evts->events[num],
258 (evts->num_used - num) * sizeof(evts->events[0]) );
259 }
260 evts->num_used -= num;
261 }
262
263 static struct console_input_events *create_console_input_events(void)
264 {
265 struct console_input_events* evt;
266
267 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
268 evt->num_alloc = evt->num_used = 0;
269 evt->events = NULL;
270 return evt;
271 }
272
273 static struct object *create_console_input( struct thread* renderer )
274 {
275 struct console_input *console_input;
276
277 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
278 console_input->renderer = renderer;
279 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
280 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
281 console_input->num_proc = 0;
282 console_input->active = NULL;
283 console_input->recnum = 0;
284 console_input->records = NULL;
285 console_input->evt = create_console_input_events();
286 console_input->title = NULL;
287 console_input->history_size = 50;
288 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
289 console_input->history_index = 0;
290 console_input->history_mode = 0;
291 console_input->edition_mode = 0;
292 console_input->input_cp = 0;
293 console_input->output_cp = 0;
294 console_input->win = 0;
295 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
296
297 if (!console_input->history || !console_input->evt)
298 {
299 release_object( console_input );
300 return NULL;
301 }
302 return &console_input->obj;
303 }
304
305 static void generate_sb_initial_events( struct console_input *console_input )
306 {
307 struct screen_buffer *screen_buffer = console_input->active;
308 struct console_renderer_event evt;
309
310 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
311 memset(&evt.u, 0, sizeof(evt.u));
312 console_input_events_append( console_input->evt, &evt );
313
314 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
315 evt.u.resize.width = screen_buffer->width;
316 evt.u.resize.height = screen_buffer->height;
317 console_input_events_append( console_input->evt, &evt );
318
319 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
320 evt.u.display.left = screen_buffer->win.left;
321 evt.u.display.top = screen_buffer->win.top;
322 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
323 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
324 console_input_events_append( console_input->evt, &evt );
325
326 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
327 evt.u.update.top = 0;
328 evt.u.update.bottom = screen_buffer->height - 1;
329 console_input_events_append( console_input->evt, &evt );
330
331 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
332 evt.u.cursor_geom.size = screen_buffer->cursor_size;
333 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
334 console_input_events_append( console_input->evt, &evt );
335
336 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
337 evt.u.cursor_pos.x = screen_buffer->cursor_x;
338 evt.u.cursor_pos.y = screen_buffer->cursor_y;
339 console_input_events_append( console_input->evt, &evt );
340 }
341
342 static struct screen_buffer *create_console_output( struct console_input *console_input )
343 {
344 struct screen_buffer *screen_buffer;
345 int i;
346
347 if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
348 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
349 screen_buffer->input = console_input;
350 screen_buffer->cursor_size = 100;
351 screen_buffer->cursor_visible = 1;
352 screen_buffer->width = 80;
353 screen_buffer->height = 150;
354 screen_buffer->max_width = 80;
355 screen_buffer->max_height = 25;
356 screen_buffer->cursor_x = 0;
357 screen_buffer->cursor_y = 0;
358 screen_buffer->attr = 0x0F;
359 screen_buffer->win.left = 0;
360 screen_buffer->win.right = screen_buffer->max_width - 1;
361 screen_buffer->win.top = 0;
362 screen_buffer->win.bottom = screen_buffer->max_height - 1;
363
364 list_add_head( &screen_buffer_list, &screen_buffer->entry );
365
366 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
367 sizeof(*screen_buffer->data) )))
368 {
369 release_object( screen_buffer );
370 return NULL;
371 }
372 /* clear the first row */
373 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
374 /* and copy it to all other rows */
375 for (i = 1; i < screen_buffer->height; i++)
376 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
377 screen_buffer->width * sizeof(char_info_t) );
378
379 if (!console_input->active)
380 {
381 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
382 generate_sb_initial_events( console_input );
383 }
384 return screen_buffer;
385 }
386
387 /* free the console for this process */
388 int free_console( struct process *process )
389 {
390 struct console_input* console = process->console;
391
392 if (!console || !console->renderer) return 0;
393
394 process->console = NULL;
395 if (--console->num_proc == 0)
396 {
397 /* all processes have terminated... tell the renderer to terminate too */
398 struct console_renderer_event evt;
399 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
400 memset(&evt.u, 0, sizeof(evt.u));
401 console_input_events_append( console->evt, &evt );
402 }
403 release_object( console );
404
405 return 1;
406 }
407
408 /* let process inherit the console from parent... this handle two cases :
409 * 1/ generic console inheritance
410 * 2/ parent is a renderer which launches process, and process should attach to the console
411 * renderered by parent
412 */
413 void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
414 {
415 int done = 0;
416 struct process* parent = parent_thread->process;
417
418 /* if parent is a renderer, then attach current process to its console
419 * a bit hacky....
420 */
421 if (hconin)
422 {
423 struct console_input* console;
424
425 /* FIXME: should we check some access rights ? */
426 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
427 0, &console_input_ops )))
428 {
429 if (console->renderer == parent_thread)
430 {
431 process->console = (struct console_input*)grab_object( console );
432 process->console->num_proc++;
433 done = 1;
434 }
435 release_object( console );
436 }
437 else clear_error(); /* ignore error */
438 }
439 /* otherwise, if parent has a console, attach child to this console */
440 if (!done && parent->console)
441 {
442 assert(parent->console->renderer);
443 process->console = (struct console_input*)grab_object( parent->console );
444 process->console->num_proc++;
445 }
446 }
447
448 struct thread *console_get_renderer( struct console_input *console )
449 {
450 return console->renderer;
451 }
452
453 static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
454 {
455 struct console_input* console = NULL;
456
457 if (handle)
458 console = (struct console_input *)get_handle_obj( current->process, handle,
459 access, &console_input_ops );
460 else if (current->process->console)
461 {
462 assert( current->process->console->renderer );
463 console = (struct console_input *)grab_object( current->process->console );
464 }
465
466 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
467 return console;
468 }
469
470 struct console_signal_info
471 {
472 struct console_input *console;
473 process_id_t group;
474 int signal;
475 };
476
477 static int propagate_console_signal_cb(struct process *process, void *user)
478 {
479 struct console_signal_info* csi = (struct console_signal_info*)user;
480
481 if (process->console == csi->console && process->running_threads &&
482 (!csi->group || process->group_id == csi->group))
483 {
484 /* find a suitable thread to signal */
485 struct thread *thread;
486 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
487 {
488 if (send_thread_signal( thread, csi->signal )) break;
489 }
490 }
491 return FALSE;
492 }
493
494 static void propagate_console_signal( struct console_input *console,
495 int sig, process_id_t group_id )
496 {
497 struct console_signal_info csi;
498
499 if (!console)
500 {
501 set_error( STATUS_INVALID_PARAMETER );
502 return;
503 }
504 /* FIXME: should support the other events (like CTRL_BREAK) */
505 if (sig != CTRL_C_EVENT)
506 {
507 set_error( STATUS_NOT_IMPLEMENTED );
508 return;
509 }
510 csi.console = console;
511 csi.signal = SIGINT;
512 csi.group = group_id;
513
514 enum_processes(propagate_console_signal_cb, &csi);
515 }
516
517 static int get_console_mode( obj_handle_t handle )
518 {
519 struct object *obj;
520 int ret = 0;
521
522 if ((obj = get_handle_obj( current->process, handle, CONSOLE_READ, NULL )))
523 {
524 if (obj->ops == &console_input_ops)
525 ret = ((struct console_input *)obj)->mode;
526 else if (obj->ops == &screen_buffer_ops)
527 ret = ((struct screen_buffer *)obj)->mode;
528 else
529 set_error( STATUS_OBJECT_TYPE_MISMATCH );
530 release_object( obj );
531 }
532 return ret;
533 }
534
535 /* changes the mode of either a console input or a screen buffer */
536 static int set_console_mode( obj_handle_t handle, int mode )
537 {
538 struct object *obj;
539 int ret = 0;
540
541 if (!(obj = get_handle_obj( current->process, handle, CONSOLE_WRITE, NULL )))
542 return 0;
543 if (obj->ops == &console_input_ops)
544 {
545 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
546 ((struct console_input *)obj)->mode = mode;
547 ret = 1;
548 }
549 else if (obj->ops == &screen_buffer_ops)
550 {
551 ((struct screen_buffer *)obj)->mode = mode;
552 ret = 1;
553 }
554 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
555 release_object( obj );
556 return ret;
557 }
558
559 /* add input events to a console input queue */
560 static int write_console_input( struct console_input* console, int count,
561 const INPUT_RECORD *records )
562 {
563 INPUT_RECORD *new_rec;
564
565 if (!count) return 0;
566 if (!(new_rec = realloc( console->records,
567 (console->recnum + count) * sizeof(INPUT_RECORD) )))
568 {
569 set_error( STATUS_NO_MEMORY );
570 release_object( console );
571 return -1;
572 }
573 console->records = new_rec;
574 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
575
576 if (console->mode & ENABLE_PROCESSED_INPUT)
577 {
578 int i = 0;
579 while (i < count)
580 {
581 if (records[i].EventType == KEY_EVENT &&
582 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
583 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
584 {
585 if (i != count - 1)
586 memcpy( &console->records[console->recnum + i],
587 &console->records[console->recnum + i + 1],
588 (count - i - 1) * sizeof(INPUT_RECORD) );
589 count--;
590 if (records[i].Event.KeyEvent.bKeyDown)
591 {
592 /* send SIGINT to all processes attached to this console */
593 propagate_console_signal( console, CTRL_C_EVENT, 0 );
594 }
595 }
596 else i++;
597 }
598 }
599 if (!console->recnum && count) set_event( console->event );
600 console->recnum += count;
601 return count;
602 }
603
604 /* retrieve a pointer to the console input records */
605 static int read_console_input( obj_handle_t handle, int count, int flush )
606 {
607 struct console_input *console;
608
609 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
610 CONSOLE_READ, &console_input_ops )))
611 return -1;
612
613 if (!count)
614 {
615 /* special case: do not retrieve anything, but return
616 * the total number of records available */
617 count = console->recnum;
618 }
619 else
620 {
621 if (count > console->recnum) count = console->recnum;
622 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
623 }
624 if (flush)
625 {
626 int i;
627 for (i = count; i < console->recnum; i++)
628 console->records[i-count] = console->records[i];
629 if ((console->recnum -= count) > 0)
630 {
631 INPUT_RECORD *new_rec = realloc( console->records,
632 console->recnum * sizeof(INPUT_RECORD) );
633 if (new_rec) console->records = new_rec;
634 }
635 else
636 {
637 free( console->records );
638 console->records = NULL;
639 reset_event( console->event );
640 }
641 }
642 release_object( console );
643 return count;
644 }
645
646 /* set misc console input information */
647 static int set_console_input_info( const struct set_console_input_info_request *req,
648 const WCHAR *title, data_size_t len )
649 {
650 struct console_input *console;
651 struct console_renderer_event evt;
652
653 if (!(console = console_input_get( req->handle, CONSOLE_WRITE ))) goto error;
654
655 memset(&evt.u, 0, sizeof(evt.u));
656 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
657 {
658 struct screen_buffer *screen_buffer;
659
660 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
661 CONSOLE_WRITE, &screen_buffer_ops );
662 if (!screen_buffer || screen_buffer->input != console)
663 {
664 set_error( STATUS_INVALID_HANDLE );
665 if (screen_buffer) release_object( screen_buffer );
666 goto error;
667 }
668
669 if (screen_buffer != console->active)
670 {
671 if (console->active) release_object( console->active );
672 console->active = screen_buffer;
673 generate_sb_initial_events( console );
674 }
675 else
676 release_object( screen_buffer );
677 }
678 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
679 {
680 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
681 if (new_title)
682 {
683 memcpy( new_title, title, len );
684 new_title[len / sizeof(WCHAR)] = 0;
685 free( console->title );
686 console->title = new_title;
687 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
688 console_input_events_append( console->evt, &evt );
689 }
690 }
691 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
692 {
693 console->history_mode = req->history_mode;
694 }
695 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
696 console->history_size != req->history_size)
697 {
698 WCHAR** mem = NULL;
699 int i;
700 int delta;
701
702 if (req->history_size)
703 {
704 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
705 if (!mem) goto error;
706 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
707 }
708
709 delta = (console->history_index > req->history_size) ?
710 (console->history_index - req->history_size) : 0;
711
712 for (i = delta; i < console->history_index; i++)
713 {
714 mem[i - delta] = console->history[i];
715 console->history[i] = NULL;
716 }
717 console->history_index -= delta;
718
719 for (i = 0; i < console->history_size; i++)
720 free( console->history[i] );
721 free( console->history );
722 console->history = mem;
723 console->history_size = req->history_size;
724 }
725 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
726 {
727 console->edition_mode = req->edition_mode;
728 }
729 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
730 {
731 console->input_cp = req->input_cp;
732 }
733 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
734 {
735 console->output_cp = req->output_cp;
736 }
737 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
738 {
739 console->win = req->win;
740 }
741 release_object( console );
742 return 1;
743 error:
744 if (console) release_object( console );
745 return 0;
746 }
747
748 /* resize a screen buffer */
749 static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
750 int new_width, int new_height )
751 {
752 int i, old_width, old_height, copy_width, copy_height;
753 char_info_t *new_data;
754
755 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
756 {
757 set_error( STATUS_NO_MEMORY );
758 return 0;
759 }
760 old_width = screen_buffer->width;
761 old_height = screen_buffer->height;
762 copy_width = min( old_width, new_width );
763 copy_height = min( old_height, new_height );
764
765 /* copy all the rows */
766 for (i = 0; i < copy_height; i++)
767 {
768 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
769 copy_width * sizeof(char_info_t) );
770 }
771
772 /* clear the end of each row */
773 if (new_width > old_width)
774 {
775 /* fill first row */
776 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
777 /* and blast it to the other rows */
778 for (i = 1; i < copy_height; i++)
779 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
780 (new_width - old_width) * sizeof(char_info_t) );
781 }
782
783 /* clear remaining rows */
784 if (new_height > old_height)
785 {
786 /* fill first row */
787 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
788 /* and blast it to the other rows */
789 for (i = old_height+1; i < new_height; i++)
790 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
791 new_width * sizeof(char_info_t) );
792 }
793 free( screen_buffer->data );
794 screen_buffer->data = new_data;
795 screen_buffer->width = new_width;
796 screen_buffer->height = new_height;
797 return 1;
798 }
799
800 /* set misc screen buffer information */
801 static int set_console_output_info( struct screen_buffer *screen_buffer,
802 const struct set_console_output_info_request *req )
803 {
804 struct console_renderer_event evt;
805
806 memset(&evt.u, 0, sizeof(evt.u));
807 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
808 {
809 if (req->cursor_size < 1 || req->cursor_size > 100)
810 {
811 set_error( STATUS_INVALID_PARAMETER );
812 return 0;
813 }
814 if (screen_buffer->cursor_size != req->cursor_size ||
815 screen_buffer->cursor_visible != req->cursor_visible)
816 {
817 screen_buffer->cursor_size = req->cursor_size;
818 screen_buffer->cursor_visible = req->cursor_visible;
819 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
820 evt.u.cursor_geom.size = req->cursor_size;
821 evt.u.cursor_geom.visible = req->cursor_visible;
822 console_input_events_append( screen_buffer->input->evt, &evt );
823 }
824 }
825 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
826 {
827 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
828 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
829 {
830 set_error( STATUS_INVALID_PARAMETER );
831 return 0;
832 }
833 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
834 {
835 screen_buffer->cursor_x = req->cursor_x;
836 screen_buffer->cursor_y = req->cursor_y;
837 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
838 evt.u.cursor_pos.x = req->cursor_x;
839 evt.u.cursor_pos.y = req->cursor_y;
840 console_input_events_append( screen_buffer->input->evt, &evt );
841 }
842 }
843 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
844 {
845 unsigned cc;
846
847 /* new screen-buffer cannot be smaller than actual window */
848 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
849 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
850 {
851 set_error( STATUS_INVALID_PARAMETER );
852 return 0;
853 }
854 /* FIXME: there are also some basic minimum and max size to deal with */
855 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
856
857 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
858 evt.u.resize.width = req->width;
859 evt.u.resize.height = req->height;
860 console_input_events_append( screen_buffer->input->evt, &evt );
861
862 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
863 evt.u.update.top = 0;
864 evt.u.update.bottom = screen_buffer->height - 1;
865 console_input_events_append( screen_buffer->input->evt, &evt );
866
867 /* scroll window to display sb */
868 if (screen_buffer->win.right >= req->width)
869 {
870 screen_buffer->win.right -= screen_buffer->win.left;
871 screen_buffer->win.left = 0;
872 }
873 if (screen_buffer->win.bottom >= req->height)
874 {
875 screen_buffer->win.bottom -= screen_buffer->win.top;
876 screen_buffer->win.top = 0;
877 }
878 /* reset cursor if needed (normally, if cursor was outside of new sb, the
879 * window has been shifted so that the new position of the cursor will be
880 * visible */
881 cc = 0;
882 if (screen_buffer->cursor_x >= req->width)
883 {
884 screen_buffer->cursor_x = req->width - 1;
885 cc++;
886 }
887 if (screen_buffer->cursor_y >= req->height)
888 {
889 screen_buffer->cursor_y = req->height - 1;
890 cc++;
891 }
892 if (cc)
893 {
894 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
895 evt.u.cursor_pos.x = req->cursor_x;
896 evt.u.cursor_pos.y = req->cursor_y;
897 console_input_events_append( screen_buffer->input->evt, &evt );
898 }
899
900 if (screen_buffer == screen_buffer->input->active &&
901 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
902 {
903 INPUT_RECORD ir;
904 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
905 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
906 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;