blob: 6616bd2d3b05a7557cda1eb68166e2deaab376b8 [file] [log] [blame]
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +00001/*
2 * Server-side console management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Eric Pouech0b83d4c2001-11-23 23:04:58 +00005 * 2001 Eric Pouech
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +00006 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00007 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000021 */
22
Patrik Stridvall96336321999-10-24 22:13:47 +000023#include "config.h"
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000024#include "wine/port.h"
Patrik Stridvall96336321999-10-24 22:13:47 +000025
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000026#include <assert.h>
Alexandre Julliard05625391999-01-03 11:55:56 +000027#include <string.h>
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000028#include <stdio.h>
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000029#include <unistd.h>
Eric Pouech93bfa0d2002-06-02 21:22:22 +000030#include <signal.h>
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000031
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010032#include "ntstatus.h"
33#define WIN32_NO_STATUS
Alexandre Julliard43c190e1999-05-15 10:48:19 +000034#include "handle.h"
35#include "process.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000036#include "request.h"
Eric Pouech465f3d12010-08-28 13:14:43 +020037#include "file.h"
Eric Pouech0b83d4c2001-11-23 23:04:58 +000038#include "unicode.h"
Alexandre Julliard627ca402007-05-11 12:46:32 +020039#include "wincon.h"
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010040#include "winternl.h"
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000041
Alexandre Julliard627ca402007-05-11 12:46:32 +020042struct screen_buffer;
43struct console_input_events;
44
45struct console_input
46{
47 struct object obj; /* object header */
48 int num_proc; /* number of processes attached to this console */
49 struct thread *renderer; /* console renderer thread */
50 int mode; /* input mode */
51 struct screen_buffer *active; /* active screen buffer */
52 int recnum; /* number of input records */
53 INPUT_RECORD *records; /* input records */
54 struct console_input_events *evt; /* synchronization event with renderer */
55 WCHAR *title; /* console title */
56 WCHAR **history; /* lines history */
57 int history_size; /* number of entries in history array */
58 int history_index; /* number of used entries in history array */
59 int history_mode; /* mode of history (non zero means remove doubled strings */
60 int edition_mode; /* index to edition mode flavors */
61 int input_cp; /* console input codepage */
62 int output_cp; /* console output codepage */
Kirill K. Smirnovf3e13172007-08-23 19:17:17 +040063 user_handle_t win; /* window handle if backend supports it */
Alexandre Julliard627ca402007-05-11 12:46:32 +020064 struct event *event; /* event to wait on for input queue */
Eric Pouechb39a8d92010-08-30 22:18:44 +020065 struct fd *fd; /* for bare console, attached input fd */
Alexandre Julliard627ca402007-05-11 12:46:32 +020066};
67
Eric Pouech0b83d4c2001-11-23 23:04:58 +000068static void console_input_dump( struct object *obj, int verbose );
69static void console_input_destroy( struct object *obj );
Eric Pouechb39a8d92010-08-30 22:18:44 +020070static struct fd *console_input_get_fd( struct object *obj );
Eric Pouech0b83d4c2001-11-23 23:04:58 +000071
Eric Pouech0b83d4c2001-11-23 23:04:58 +000072static const struct object_ops console_input_ops =
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000073{
Eric Pouech0b83d4c2001-11-23 23:04:58 +000074 sizeof(struct console_input), /* size */
75 console_input_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +010076 no_get_type, /* get_type */
Eric Pouech412d37f2003-06-21 02:07:10 +000077 no_add_queue, /* add_queue */
78 NULL, /* remove_queue */
79 NULL, /* signaled */
Eric Pouech0b83d4c2001-11-23 23:04:58 +000080 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000081 no_signal, /* signal */
Eric Pouechb39a8d92010-08-30 22:18:44 +020082 console_input_get_fd, /* get_fd */
Eric Pouech465f3d12010-08-28 13:14:43 +020083 default_fd_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +010084 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000086 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +010087 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000088 no_close_handle, /* close_handle */
Eric Pouech0b83d4c2001-11-23 23:04:58 +000089 console_input_destroy /* destroy */
90};
91
92static void console_input_events_dump( struct object *obj, int verbose );
93static void console_input_events_destroy( struct object *obj );
94static int console_input_events_signaled( struct object *obj, struct thread *thread );
95
Vincent BĂ©ron9a624912002-05-31 23:06:46 +000096struct console_input_events
Eric Pouech0b83d4c2001-11-23 23:04:58 +000097{
98 struct object obj; /* object header */
99 int num_alloc; /* number of allocated events */
100 int num_used; /* number of actually used events */
101 struct console_renderer_event* events;
102};
103
104static const struct object_ops console_input_events_ops =
105{
106 sizeof(struct console_input_events), /* size */
107 console_input_events_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100108 no_get_type, /* get_type */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 console_input_events_signaled, /* signaled */
112 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000113 no_signal, /* signal */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000114 no_get_fd, /* get_fd */
Eric Pouech465f3d12010-08-28 13:14:43 +0200115 default_fd_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000118 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100119 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000120 no_close_handle, /* close_handle */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000121 console_input_events_destroy /* destroy */
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000122};
123
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000124struct screen_buffer
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000125{
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000126 struct object obj; /* object header */
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +0000127 struct list entry; /* entry in list of all screen buffers */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000128 struct console_input *input; /* associated console input */
129 int mode; /* output mode */
130 int cursor_size; /* size of cursor (percentage filled) */
131 int cursor_visible;/* cursor visibility flag */
132 int cursor_x; /* position of cursor */
133 int cursor_y; /* position of cursor */
134 int width; /* size (w-h) of the screen buffer */
135 int height;
136 int max_width; /* size (w-h) of the window given font size */
137 int max_height;
138 char_info_t *data; /* the data for each cell - a width x height matrix */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000139 unsigned short attr; /* default attribute for screen buffer */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000140 rectangle_t win; /* current visible window on the screen buffer *
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000141 * as seen in wineconsole */
Eric Pouech6f326442010-08-30 22:18:52 +0200142 struct fd *fd; /* for bare console, attached output fd */
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000143};
144
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000145static void screen_buffer_dump( struct object *obj, int verbose );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000146static void screen_buffer_destroy( struct object *obj );
Eric Pouech6f326442010-08-30 22:18:52 +0200147static struct fd *screen_buffer_get_fd( struct object *obj );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000148
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000149static const struct object_ops screen_buffer_ops =
150{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000151 sizeof(struct screen_buffer), /* size */
152 screen_buffer_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100153 no_get_type, /* get_type */
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000154 no_add_queue, /* add_queue */
155 NULL, /* remove_queue */
156 NULL, /* signaled */
157 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000158 no_signal, /* signal */
Eric Pouech6f326442010-08-30 22:18:52 +0200159 screen_buffer_get_fd, /* get_fd */
Eric Pouech465f3d12010-08-28 13:14:43 +0200160 default_fd_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000163 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100164 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000165 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000166 screen_buffer_destroy /* destroy */
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000167};
168
Eric Pouechb39a8d92010-08-30 22:18:44 +0200169static enum server_fd_type console_get_fd_type( struct fd *fd );
170
171static const struct fd_ops console_fd_ops =
172{
173 default_fd_get_poll_events, /* get_poll_events */
174 default_poll_event, /* poll_event */
175 no_flush, /* flush */
176 console_get_fd_type, /* get_fd_type */
177 default_fd_ioctl, /* ioctl */
178 default_fd_queue_async, /* queue_async */
179 default_fd_reselect_async, /* reselect_async */
180 default_fd_cancel_async /* cancel_async */
181};
182
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +0000183static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000184
Eric Pouecha9087562002-01-02 21:45:47 +0000185static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000186
Eric Pouech4fdcdef2010-08-30 22:19:00 +0200187static int console_input_is_bare( struct console_input* cin )
188{
189 return cin->evt == NULL;
190}
191
Eric Pouechb39a8d92010-08-30 22:18:44 +0200192static struct fd *console_input_get_fd( struct object* obj )
193{
194 struct console_input *console_input = (struct console_input*)obj;
195 assert( obj->ops == &console_input_ops );
196 return console_input->fd ? (struct fd*)grab_object( console_input->fd ) : NULL;
197}
198
199static enum server_fd_type console_get_fd_type( struct fd *fd )
200{
201 return FD_TYPE_CHAR;
202}
203
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000204/* dumps the renderer events of a console */
205static void console_input_events_dump( struct object *obj, int verbose )
206{
207 struct console_input_events *evts = (struct console_input_events *)obj;
208 assert( obj->ops == &console_input_events_ops );
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000209 fprintf( stderr, "Console input events: %d/%d events\n",
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000210 evts->num_used, evts->num_alloc );
211}
212
213/* destroys the renderer events of a console */
214static void console_input_events_destroy( struct object *obj )
215{
216 struct console_input_events *evts = (struct console_input_events *)obj;
217 assert( obj->ops == &console_input_events_ops );
218 free( evts->events );
219}
220
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000221/* the renderer events list is signaled when it's not empty */
222static int console_input_events_signaled( struct object *obj, struct thread *thread )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000223{
224 struct console_input_events *evts = (struct console_input_events *)obj;
225 assert( obj->ops == &console_input_events_ops );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000226 return (evts->num_used != 0);
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000227}
228
229/* add an event to the console's renderer events list */
Eric Pouech2f7af562010-08-30 22:19:06 +0200230static void console_input_events_append( struct console_input* console,
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000231 struct console_renderer_event* evt)
232{
Eric Pouech2f7af562010-08-30 22:19:06 +0200233 struct console_input_events* evts;
Eric Pouechf4b298b2003-01-05 01:07:34 +0000234 int collapsed = FALSE;
235
Eric Pouech2f7af562010-08-30 22:19:06 +0200236 if (!(evts = console->evt)) return;
Eric Pouechb39a8d92010-08-30 22:18:44 +0200237 /* to be done even when evt has been generated by the renderer ? */
Eric Pouechf4b298b2003-01-05 01:07:34 +0000238
239 /* try to collapse evt into current queue's events */
240 if (evts->num_used)
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000241 {
Eric Pouechf4b298b2003-01-05 01:07:34 +0000242 struct console_renderer_event* last = &evts->events[evts->num_used - 1];
243
244 if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
245 evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
246 {
247 /* if two update events overlap, collapse them into a single one */
248 if (last->u.update.bottom + 1 >= evt->u.update.top &&
249 evt->u.update.bottom + 1 >= last->u.update.top)
250 {
251 last->u.update.top = min(last->u.update.top, evt->u.update.top);
252 last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
253 collapsed = TRUE;
254 }
255 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000256 }
Eric Pouechf4b298b2003-01-05 01:07:34 +0000257 if (!collapsed)
258 {
259 if (evts->num_used == evts->num_alloc)
260 {
261 evts->num_alloc += 16;
262 evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
263 assert(evts->events);
264 }
265 evts->events[evts->num_used++] = *evt;
266 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000267 wake_up( &evts->obj, 0 );
268}
269
270/* retrieves events from the console's renderer events list */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000271static void console_input_events_get( struct console_input_events* evts )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000272{
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200273 data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000274
275 if (num > evts->num_used) num = evts->num_used;
276 set_reply_data( evts->events, num * sizeof(evts->events[0]) );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000277 if (num < evts->num_used)
278 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000279 memmove( &evts->events[0], &evts->events[num],
280 (evts->num_used - num) * sizeof(evts->events[0]) );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000281 }
282 evts->num_used -= num;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000283}
284
285static struct console_input_events *create_console_input_events(void)
286{
287 struct console_input_events* evt;
288
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000289 if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000290 evt->num_alloc = evt->num_used = 0;
291 evt->events = NULL;
292 return evt;
293}
294
Eric Pouechb39a8d92010-08-30 22:18:44 +0200295static struct object *create_console_input( struct thread* renderer, int fd )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000296{
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000297 struct console_input *console_input;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000298
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000299 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000300 console_input->renderer = renderer;
301 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
302 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
303 console_input->num_proc = 0;
304 console_input->active = NULL;
305 console_input->recnum = 0;
306 console_input->records = NULL;
Eric Pouechb39a8d92010-08-30 22:18:44 +0200307 console_input->evt = renderer ? create_console_input_events() : NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000308 console_input->title = NULL;
309 console_input->history_size = 50;
310 console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
311 console_input->history_index = 0;
312 console_input->history_mode = 0;
Eric Pouechfa8b85a2003-01-09 06:01:32 +0000313 console_input->edition_mode = 0;
Kirill K. Smirnov9d746612007-05-10 21:19:47 +0400314 console_input->input_cp = 0;
315 console_input->output_cp = 0;
Kirill K. Smirnovf3e13172007-08-23 19:17:17 +0400316 console_input->win = 0;
Rob Shearmandd9e3922007-10-24 16:04:42 +0100317 console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
Eric Pouechb39a8d92010-08-30 22:18:44 +0200318 console_input->fd = NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000319
Eric Pouechb39a8d92010-08-30 22:18:44 +0200320 if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000321 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000322 release_object( console_input );
323 return NULL;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000324 }
Eric Pouechb39a8d92010-08-30 22:18:44 +0200325 if (fd != -1) /* bare console */
326 {
Eric Pouechb39a8d92010-08-30 22:18:44 +0200327 if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
328 FILE_SYNCHRONOUS_IO_NONALERT )))
329 {
330 release_object( console_input );
331 return NULL;
332 }
Eric Pouechb39a8d92010-08-30 22:18:44 +0200333 allow_fd_caching( console_input->fd );
334 }
335
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000336 return &console_input->obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000337}
338
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300339static void generate_sb_initial_events( struct console_input *console_input )
340{
341 struct screen_buffer *screen_buffer = console_input->active;
342 struct console_renderer_event evt;
343
344 evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
345 memset(&evt.u, 0, sizeof(evt.u));
Eric Pouech2f7af562010-08-30 22:19:06 +0200346 console_input_events_append( console_input, &evt );
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300347
348 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
349 evt.u.resize.width = screen_buffer->width;
350 evt.u.resize.height = screen_buffer->height;
Eric Pouech2f7af562010-08-30 22:19:06 +0200351 console_input_events_append( console_input, &evt );
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300352
353 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
354 evt.u.display.left = screen_buffer->win.left;
355 evt.u.display.top = screen_buffer->win.top;
356 evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
357 evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
Eric Pouech2f7af562010-08-30 22:19:06 +0200358 console_input_events_append( console_input, &evt );
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300359
360 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
361 evt.u.update.top = 0;
362 evt.u.update.bottom = screen_buffer->height - 1;
Eric Pouech2f7af562010-08-30 22:19:06 +0200363 console_input_events_append( console_input, &evt );
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300364
365 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
366 evt.u.cursor_geom.size = screen_buffer->cursor_size;
367 evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
Eric Pouech2f7af562010-08-30 22:19:06 +0200368 console_input_events_append( console_input, &evt );
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300369
370 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
371 evt.u.cursor_pos.x = screen_buffer->cursor_x;
372 evt.u.cursor_pos.y = screen_buffer->cursor_y;
Eric Pouech2f7af562010-08-30 22:19:06 +0200373 console_input_events_append( console_input, &evt );
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300374}
375
Eric Pouech6f326442010-08-30 22:18:52 +0200376static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000377{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000378 struct screen_buffer *screen_buffer;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000379 int i;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000380
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000381 if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000382 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
383 screen_buffer->input = console_input;
384 screen_buffer->cursor_size = 100;
385 screen_buffer->cursor_visible = 1;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000386 screen_buffer->width = 80;
387 screen_buffer->height = 150;
388 screen_buffer->max_width = 80;
389 screen_buffer->max_height = 25;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000390 screen_buffer->cursor_x = 0;
391 screen_buffer->cursor_y = 0;
Eric Pouecha9087562002-01-02 21:45:47 +0000392 screen_buffer->attr = 0x0F;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000393 screen_buffer->win.left = 0;
394 screen_buffer->win.right = screen_buffer->max_width - 1;
395 screen_buffer->win.top = 0;
396 screen_buffer->win.bottom = screen_buffer->max_height - 1;
Eric Pouech6f326442010-08-30 22:18:52 +0200397 if (fd == -1)
398 screen_buffer->fd = NULL;
399 else
400 {
401 if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
402 FILE_SYNCHRONOUS_IO_NONALERT )))
403 {
404 release_object( screen_buffer );
405 return NULL;
406 }
407 allow_fd_caching(screen_buffer->fd);
408 }
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000409
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +0000410 list_add_head( &screen_buffer_list, &screen_buffer->entry );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000411
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000412 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
413 sizeof(*screen_buffer->data) )))
414 {
415 release_object( screen_buffer );
416 return NULL;
417 }
418 /* clear the first row */
419 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
420 /* and copy it to all other rows */
421 for (i = 1; i < screen_buffer->height; i++)
422 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
423 screen_buffer->width * sizeof(char_info_t) );
424
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000425 if (!console_input->active)
Alexandre Julliard039aa421999-06-11 18:31:22 +0000426 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000427 console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300428 generate_sb_initial_events( console_input );
Alexandre Julliard039aa421999-06-11 18:31:22 +0000429 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000430 return screen_buffer;
Alexandre Julliard039aa421999-06-11 18:31:22 +0000431}
432
433/* free the console for this process */
434int free_console( struct process *process )
435{
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000436 struct console_input* console = process->console;
437
Eric Pouechb39a8d92010-08-30 22:18:44 +0200438 if (!console) return 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000439
440 process->console = NULL;
Eric Pouechb39a8d92010-08-30 22:18:44 +0200441 if (--console->num_proc == 0 && console->renderer)
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000442 {
443 /* all processes have terminated... tell the renderer to terminate too */
444 struct console_renderer_event evt;
445 evt.event = CONSOLE_RENDERER_EXIT_EVENT;
Eric Pouech2e0b5332006-01-27 16:17:51 +0100446 memset(&evt.u, 0, sizeof(evt.u));
Eric Pouech2f7af562010-08-30 22:19:06 +0200447 console_input_events_append( console, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000448 }
449 release_object( console );
450
Alexandre Julliard039aa421999-06-11 18:31:22 +0000451 return 1;
452}
453
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000454/* let process inherit the console from parent... this handle two cases :
455 * 1/ generic console inheritance
456 * 2/ parent is a renderer which launches process, and process should attach to the console
457 * renderered by parent
458 */
Alexandre Julliard51885742002-05-30 20:12:58 +0000459void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000460{
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000461 int done = 0;
Eric Pouech3940d8a2001-12-04 20:17:43 +0000462 struct process* parent = parent_thread->process;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000463
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000464 /* if parent is a renderer, then attach current process to its console
465 * a bit hacky....
466 */
467 if (hconin)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000468 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000469 struct console_input* console;
470
Eric Pouech449d10f2003-08-19 01:05:17 +0000471 /* FIXME: should we check some access rights ? */
472 if ((console = (struct console_input*)get_handle_obj( parent, hconin,
473 0, &console_input_ops )))
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000474 {
Eric Pouech449d10f2003-08-19 01:05:17 +0000475 if (console->renderer == parent_thread)
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000476 {
477 process->console = (struct console_input*)grab_object( console );
478 process->console->num_proc++;
479 done = 1;
480 }
481 release_object( console );
482 }
Alexandre Julliard01caa5e2005-07-12 20:27:09 +0000483 else clear_error(); /* ignore error */
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000484 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000485 /* otherwise, if parent has a console, attach child to this console */
486 if (!done && parent->console)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000487 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000488 process->console = (struct console_input*)grab_object( parent->console );
489 process->console->num_proc++;
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000490 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000491}
492
Alexandre Julliard627ca402007-05-11 12:46:32 +0200493struct thread *console_get_renderer( struct console_input *console )
494{
495 return console->renderer;
496}
497
Alexandre Julliard51885742002-05-30 20:12:58 +0000498static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000499{
Robert Shearmanc5165712005-05-25 18:41:09 +0000500 struct console_input* console = NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000501
502 if (handle)
503 console = (struct console_input *)get_handle_obj( current->process, handle,
504 access, &console_input_ops );
505 else if (current->process->console)
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000506 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000507 console = (struct console_input *)grab_object( current->process->console );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000508 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000509
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000510 if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
511 return console;
512}
Alexandre Julliard57e11311999-05-16 16:59:38 +0000513
Eric Pouech440ad8c2004-09-08 01:25:05 +0000514struct console_signal_info
515{
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000516 struct console_input *console;
Alexandre Julliard54f22872002-10-03 19:54:57 +0000517 process_id_t group;
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000518 int signal;
519};
520
521static int propagate_console_signal_cb(struct process *process, void *user)
522{
523 struct console_signal_info* csi = (struct console_signal_info*)user;
524
525 if (process->console == csi->console && process->running_threads &&
Alexandre Julliard54f22872002-10-03 19:54:57 +0000526 (!csi->group || process->group_id == csi->group))
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000527 {
Eric Pouech440ad8c2004-09-08 01:25:05 +0000528 /* find a suitable thread to signal */
529 struct thread *thread;
Alexandre Julliard05026382005-03-01 10:56:18 +0000530 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000531 {
Eric Pouech440ad8c2004-09-08 01:25:05 +0000532 if (send_thread_signal( thread, csi->signal )) break;
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000533 }
534 }
535 return FALSE;
536}
537
Andreas Mohr17a2fe02002-07-01 23:22:48 +0000538static void propagate_console_signal( struct console_input *console,
Alexandre Julliard54f22872002-10-03 19:54:57 +0000539 int sig, process_id_t group_id )
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000540{
541 struct console_signal_info csi;
542
543 if (!console)
544 {
545 set_error( STATUS_INVALID_PARAMETER );
546 return;
547 }
548 /* FIXME: should support the other events (like CTRL_BREAK) */
549 if (sig != CTRL_C_EVENT)
550 {
551 set_error( STATUS_NOT_IMPLEMENTED );
552 return;
553 }
554 csi.console = console;
555 csi.signal = SIGINT;
556 csi.group = group_id;
557
558 enum_processes(propagate_console_signal_cb, &csi);
559}
560
Eric Pouech4fdcdef2010-08-30 22:19:00 +0200561static int get_console_mode( obj_handle_t handle, int *bare )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000562{
563 struct object *obj;
564 int ret = 0;
565
Eric Pouech465f3d12010-08-28 13:14:43 +0200566 if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000567 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000568 if (obj->ops == &console_input_ops)
Eric Pouech4fdcdef2010-08-30 22:19:00 +0200569 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000570 ret = ((struct console_input *)obj)->mode;
Eric Pouech4fdcdef2010-08-30 22:19:00 +0200571 *bare = console_input_is_bare((struct console_input *)obj);
572 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000573 else if (obj->ops == &screen_buffer_ops)
Eric Pouech4fdcdef2010-08-30 22:19:00 +0200574 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000575 ret = ((struct screen_buffer *)obj)->mode;
Eric Pouech4fdcdef2010-08-30 22:19:00 +0200576 *bare = console_input_is_bare(((struct screen_buffer *)obj)->input);
577 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000578 else
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000579 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000580 release_object( obj );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000581 }
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000582 return ret;
583}
584
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000585/* changes the mode of either a console input or a screen buffer */
Alexandre Julliard51885742002-05-30 20:12:58 +0000586static int set_console_mode( obj_handle_t handle, int mode )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000587{
588 struct object *obj;
589 int ret = 0;
590
Eric Pouech465f3d12010-08-28 13:14:43 +0200591 if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000592 return 0;
593 if (obj->ops == &console_input_ops)
594 {
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000595 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000596 ((struct console_input *)obj)->mode = mode;
597 ret = 1;
598 }
599 else if (obj->ops == &screen_buffer_ops)
600 {
601 ((struct screen_buffer *)obj)->mode = mode;
602 ret = 1;
603 }
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000604 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000605 release_object( obj );
606 return ret;
607}
608
Alexandre Julliard4b461121999-01-31 19:04:30 +0000609/* add input events to a console input queue */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000610static int write_console_input( struct console_input* console, int count,
611 const INPUT_RECORD *records )
Alexandre Julliard4b461121999-01-31 19:04:30 +0000612{
613 INPUT_RECORD *new_rec;
Alexandre Julliard4b461121999-01-31 19:04:30 +0000614
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000615 if (!count) return 0;
Alexandre Julliard4b461121999-01-31 19:04:30 +0000616 if (!(new_rec = realloc( console->records,
617 (console->recnum + count) * sizeof(INPUT_RECORD) )))
618 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000619 set_error( STATUS_NO_MEMORY );
Alexandre Julliard4b461121999-01-31 19:04:30 +0000620 release_object( console );
621 return -1;
622 }
623 console->records = new_rec;
624 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000625
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000626 if (console->mode & ENABLE_PROCESSED_INPUT)
627 {
628 int i = 0;
629 while (i < count)
630 {
Andreas Mohr17a2fe02002-07-01 23:22:48 +0000631 if (records[i].EventType == KEY_EVENT &&
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000632 records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
633 !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
634 {
635 if (i != count - 1)
Andreas Mohr17a2fe02002-07-01 23:22:48 +0000636 memcpy( &console->records[console->recnum + i],
637 &console->records[console->recnum + i + 1],
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000638 (count - i - 1) * sizeof(INPUT_RECORD) );
639 count--;
640 if (records[i].Event.KeyEvent.bKeyDown)
641 {
642 /* send SIGINT to all processes attached to this console */
Alexandre Julliard54f22872002-10-03 19:54:57 +0000643 propagate_console_signal( console, CTRL_C_EVENT, 0 );
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000644 }
645 }
646 else i++;
647 }
648 }
Eric Pouecha86a2892003-06-23 03:37:14 +0000649 if (!console->recnum && count) set_event( console->event );
Eric Pouech93bfa0d2002-06-02 21:22:22 +0000650 console->recnum += count;
Alexandre Julliard4b461121999-01-31 19:04:30 +0000651 return count;
652}
653
654/* retrieve a pointer to the console input records */
Alexandre Julliard51885742002-05-30 20:12:58 +0000655static int read_console_input( obj_handle_t handle, int count, int flush )
Alexandre Julliard4b461121999-01-31 19:04:30 +0000656{
657 struct console_input *console;
658
659 if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
Eric Pouech465f3d12010-08-28 13:14:43 +0200660 FILE_READ_DATA, &console_input_ops )))
Alexandre Julliard4b461121999-01-31 19:04:30 +0000661 return -1;
Alexandre Julliard92643002000-08-31 01:59:51 +0000662
663 if (!count)
664 {
665 /* special case: do not retrieve anything, but return
666 * the total number of records available */
667 count = console->recnum;
668 }
669 else
670 {
671 if (count > console->recnum) count = console->recnum;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000672 set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
Alexandre Julliard92643002000-08-31 01:59:51 +0000673 }
Alexandre Julliard4b461121999-01-31 19:04:30 +0000674 if (flush)
675 {
676 int i;
677 for (i = count; i < console->recnum; i++)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000678 console->records[i-count] = console->records[i];
Alexandre Julliard4b461121999-01-31 19:04:30 +0000679 if ((console->recnum -= count) > 0)
680 {
681 INPUT_RECORD *new_rec = realloc( console->records,
682 console->recnum * sizeof(INPUT_RECORD) );
683 if (new_rec) console->records = new_rec;
684 }
685 else
686 {
687 free( console->records );
688 console->records = NULL;
Eric Pouecha86a2892003-06-23 03:37:14 +0000689 reset_event( console->event );
Alexandre Julliard4b461121999-01-31 19:04:30 +0000690 }
691 }
692 release_object( console );
693 return count;
694}
695
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000696/* set misc console input information */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000697static int set_console_input_info( const struct set_console_input_info_request *req,
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200698 const WCHAR *title, data_size_t len )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000699{
700 struct console_input *console;
701 struct console_renderer_event evt;
702
Eric Pouech465f3d12010-08-28 13:14:43 +0200703 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
Eric Pouechdaaee9e2010-08-30 22:19:12 +0200704 if (console_input_is_bare(console) &&
705 (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
706 SET_CONSOLE_INPUT_INFO_WIN)))
707 {
708 set_error( STATUS_UNSUCCESSFUL );
709 goto error;
710 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000711
Eric Pouech2e0b5332006-01-27 16:17:51 +0100712 memset(&evt.u, 0, sizeof(evt.u));
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000713 if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
714 {
715 struct screen_buffer *screen_buffer;
716
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000717 screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
Eric Pouech465f3d12010-08-28 13:14:43 +0200718 FILE_WRITE_PROPERTIES, &screen_buffer_ops );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000719 if (!screen_buffer || screen_buffer->input != console)
720 {
Kirill K. Smirnov545d7f12007-05-09 20:56:29 +0400721 set_error( STATUS_INVALID_HANDLE );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000722 if (screen_buffer) release_object( screen_buffer );
723 goto error;
724 }
725
726 if (screen_buffer != console->active)
727 {
728 if (console->active) release_object( console->active );
729 console->active = screen_buffer;
Kirill K. Smirnov6e76cc72007-12-18 03:03:11 +0300730 generate_sb_initial_events( console );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000731 }
732 else
733 release_object( screen_buffer );
734 }
735 if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
736 {
737 WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
738 if (new_title)
739 {
Eric Pouech58711742003-02-19 23:27:49 +0000740 memcpy( new_title, title, len );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000741 new_title[len / sizeof(WCHAR)] = 0;
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200742 free( console->title );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000743 console->title = new_title;
Eric Pouech47205b82002-05-11 22:53:56 +0000744 evt.event = CONSOLE_RENDERER_TITLE_EVENT;
Eric Pouech2f7af562010-08-30 22:19:06 +0200745 console_input_events_append( console, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000746 }
747 }
748 if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
749 {
750 console->history_mode = req->history_mode;
751 }
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000752 if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000753 console->history_size != req->history_size)
754 {
755 WCHAR** mem = NULL;
756 int i;
757 int delta;
758
759 if (req->history_size)
760 {
761 mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
762 if (!mem) goto error;
763 memset( mem, 0, req->history_size * sizeof(WCHAR*) );
764 }
765
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000766 delta = (console->history_index > req->history_size) ?
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000767 (console->history_index - req->history_size) : 0;
768
769 for (i = delta; i < console->history_index; i++)
770 {
771 mem[i - delta] = console->history[i];
772 console->history[i] = NULL;
773 }
774 console->history_index -= delta;
775
776 for (i = 0; i < console->history_size; i++)
Lionel Debroux2f89fe72007-10-20 09:35:13 +0200777 free( console->history[i] );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000778 free( console->history );
779 console->history = mem;
780 console->history_size = req->history_size;
781 }
Eric Pouechfa8b85a2003-01-09 06:01:32 +0000782 if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
783 {
784 console->edition_mode = req->edition_mode;
785 }
Kirill K. Smirnov9d746612007-05-10 21:19:47 +0400786 if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
787 {
788 console->input_cp = req->input_cp;
789 }
790 if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
791 {
792 console->output_cp = req->output_cp;
793 }
Kirill K. Smirnovf3e13172007-08-23 19:17:17 +0400794 if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
795 {
796 console->win = req->win;
797 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000798 release_object( console );
799 return 1;
800 error:
801 if (console) release_object( console );
802 return 0;
803}
804
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000805/* resize a screen buffer */
806static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
807 int new_width, int new_height )
808{
809 int i, old_width, old_height, copy_width, copy_height;
810 char_info_t *new_data;
811
812 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
813 {
814 set_error( STATUS_NO_MEMORY );
815 return 0;
816 }
817 old_width = screen_buffer->width;
818 old_height = screen_buffer->height;
819 copy_width = min( old_width, new_width );
820 copy_height = min( old_height, new_height );
821
822 /* copy all the rows */
823 for (i = 0; i < copy_height; i++)
824 {
825 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
826 copy_width * sizeof(char_info_t) );
827 }
828
829 /* clear the end of each row */
830 if (new_width > old_width)
831 {
832 /* fill first row */
833 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
834 /* and blast it to the other rows */
835 for (i = 1; i < copy_height; i++)
836 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
837 (new_width - old_width) * sizeof(char_info_t) );
838 }
839
840 /* clear remaining rows */
841 if (new_height > old_height)
842 {
843 /* fill first row */
844 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
845 /* and blast it to the other rows */
846 for (i = old_height+1; i < new_height; i++)
847 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
848 new_width * sizeof(char_info_t) );
849 }
850 free( screen_buffer->data );
851 screen_buffer->data = new_data;
852 screen_buffer->width = new_width;
853 screen_buffer->height = new_height;
854 return 1;
855}
856
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000857/* set misc screen buffer information */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000858static int set_console_output_info( struct screen_buffer *screen_buffer,
859 const struct set_console_output_info_request *req )
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000860{
861 struct console_renderer_event evt;
862
Eric Pouech2e0b5332006-01-27 16:17:51 +0100863 memset(&evt.u, 0, sizeof(evt.u));
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000864 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
865 {
866 if (req->cursor_size < 1 || req->cursor_size > 100)
867 {
868 set_error( STATUS_INVALID_PARAMETER );
869 return 0;
870 }
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000871 if (screen_buffer->cursor_size != req->cursor_size ||
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000872 screen_buffer->cursor_visible != req->cursor_visible)
873 {
874 screen_buffer->cursor_size = req->cursor_size;
875 screen_buffer->cursor_visible = req->cursor_visible;
876 evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
877 evt.u.cursor_geom.size = req->cursor_size;
878 evt.u.cursor_geom.visible = req->cursor_visible;
Eric Pouech2f7af562010-08-30 22:19:06 +0200879 console_input_events_append( screen_buffer->input, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000880 }
881 }
882 if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
883 {
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000884 if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000885 req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
886 {
887 set_error( STATUS_INVALID_PARAMETER );
888 return 0;
889 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000890 if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000891 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000892 screen_buffer->cursor_x = req->cursor_x;
893 screen_buffer->cursor_y = req->cursor_y;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000894 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;
Eric Pouech2f7af562010-08-30 22:19:06 +0200897 console_input_events_append( screen_buffer->input, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000898 }
899 }
900 if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
901 {
Eric Pouechf4b298b2003-01-05 01:07:34 +0000902 unsigned cc;
903
904 /* new screen-buffer cannot be smaller than actual window */
905 if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
906 req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
907 {
908 set_error( STATUS_INVALID_PARAMETER );
909 return 0;
910 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000911 /* FIXME: there are also some basic minimum and max size to deal with */
912 if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000913
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000914 evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
915 evt.u.resize.width = req->width;
916 evt.u.resize.height = req->height;
Eric Pouech2f7af562010-08-30 22:19:06 +0200917 console_input_events_append( screen_buffer->input, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000918
Eric Pouechf4b298b2003-01-05 01:07:34 +0000919 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
920 evt.u.update.top = 0;
921 evt.u.update.bottom = screen_buffer->height - 1;
Eric Pouech2f7af562010-08-30 22:19:06 +0200922 console_input_events_append( screen_buffer->input, &evt );
Eric Pouechf4b298b2003-01-05 01:07:34 +0000923
924 /* scroll window to display sb */
925 if (screen_buffer->win.right >= req->width)
926 {
927 screen_buffer->win.right -= screen_buffer->win.left;
928 screen_buffer->win.left = 0;
929 }
930 if (screen_buffer->win.bottom >= req->height)
931 {
932 screen_buffer->win.bottom -= screen_buffer->win.top;
933 screen_buffer->win.top = 0;
934 }
935 /* reset cursor if needed (normally, if cursor was outside of new sb, the
936 * window has been shifted so that the new position of the cursor will be
937 * visible */
938 cc = 0;
939 if (screen_buffer->cursor_x >= req->width)
940 {
941 screen_buffer->cursor_x = req->width - 1;
942 cc++;
943 }
944 if (screen_buffer->cursor_y >= req->height)
945 {
946 screen_buffer->cursor_y = req->height - 1;
947 cc++;
948 }
949 if (cc)
950 {
951 evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
952 evt.u.cursor_pos.x = req->cursor_x;
953 evt.u.cursor_pos.y = req->cursor_y;
Eric Pouech2f7af562010-08-30 22:19:06 +0200954 console_input_events_append( screen_buffer->input, &evt );
Eric Pouechf4b298b2003-01-05 01:07:34 +0000955 }
956
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000957 if (screen_buffer == screen_buffer->input->active &&
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000958 screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
959 {
960 INPUT_RECORD ir;
961 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
962 ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
963 ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
964 write_console_input( screen_buffer->input, 1, &ir );
965 }
966 }
967 if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
968 {
969 screen_buffer->attr = req->attr;
970 }
971 if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
972 {
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000973 if (req->win_left < 0 || req->win_left > req->win_right ||
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000974 req->win_right >= screen_buffer->width ||
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000975 req->win_top < 0 || req->win_top > req->win_bottom ||
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000976 req->win_bottom >= screen_buffer->height)
977 {
978 set_error( STATUS_INVALID_PARAMETER );
979 return 0;
980 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000981 if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
982 screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000983 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000984 screen_buffer->win.left = req->win_left;
985 screen_buffer->win.top = req->win_top;
986 screen_buffer->win.right = req->win_right;
987 screen_buffer->win.bottom = req->win_bottom;
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000988 evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
989 evt.u.display.left = req->win_left;
990 evt.u.display.top = req->win_top;
991 evt.u.display.width = req->win_right - req->win_left + 1;
992 evt.u.display.height = req->win_bottom - req->win_top + 1;
Eric Pouech2f7af562010-08-30 22:19:06 +0200993 console_input_events_append( screen_buffer->input, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +0000994 }
995 }
996 if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
997 {
998 /* can only be done by renderer */
999 if (current->process->console != screen_buffer->input)
1000 {
1001 set_error( STATUS_INVALID_PARAMETER );
1002 return 0;
1003 }
1004
1005 screen_buffer->max_width = req->max_width;
1006 screen_buffer->max_height = req->max_height;
1007 }
1008
1009 return 1;
1010}
1011
1012/* appends a new line to history (history is a fixed size array) */
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001013static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001014{
1015 WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
1016
1017 if (!ptr)
Robert Shearman7a3df4d2006-04-07 11:17:12 +01001018 return;
1019
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001020 if (!console || !console->history_size)
1021 {
1022 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
Robert Shearman7a3df4d2006-04-07 11:17:12 +01001023 free( ptr );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001024 return;
1025 }
1026
1027 memcpy( ptr, buf, len * sizeof(WCHAR) );
1028 ptr[len] = 0;
1029
1030 if (console->history_mode && console->history_index &&
1031 strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
1032 {
1033 /* ok, mode ask us to not use twice the same string...
1034 * so just free mem and returns
1035 */
1036 set_error( STATUS_ALIAS_EXISTS );
1037 free(ptr);
1038 return;
1039 }
1040
1041 if (console->history_index < console->history_size)
1042 {
1043 console->history[console->history_index++] = ptr;
1044 }
1045 else
1046 {
1047 free( console->history[0]) ;
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00001048 memmove( &console->history[0], &console->history[1],
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001049 (console->history_size - 1) * sizeof(WCHAR*) );
1050 console->history[console->history_size - 1] = ptr;
1051 }
1052}
1053
Eric Pouechf4b298b2003-01-05 01:07:34 +00001054/* returns a line from the cache */
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001055static data_size_t console_input_get_hist( struct console_input *console, int index )
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001056{
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001057 data_size_t ret = 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001058
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001059 if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
1060 else
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001061 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001062 ret = strlenW( console->history[index] ) * sizeof(WCHAR);
1063 set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001064 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001065 return ret;
1066}
1067
1068/* dumb dump */
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001069static void console_input_dump( struct object *obj, int verbose )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +00001070{
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001071 struct console_input *console = (struct console_input *)obj;
1072 assert( obj->ops == &console_input_ops );
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00001073 fprintf( stderr, "Console input active=%p evt=%p\n",
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001074 console->active, console->evt );
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001075}
1076
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001077static void console_input_destroy( struct object *obj )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +00001078{
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001079 struct console_input* console_in = (struct console_input *)obj;
1080 struct screen_buffer* curr;
1081 int i;
1082
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001083 assert( obj->ops == &console_input_ops );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +02001084 free( console_in->title );
1085 free( console_in->records );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001086
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +00001087 if (console_in->active) release_object( console_in->active );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001088 console_in->active = NULL;
1089
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +00001090 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001091 {
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +00001092 if (curr->input == console_in) curr->input = NULL;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001093 }
1094
Eric Pouechb39a8d92010-08-30 22:18:44 +02001095 if (console_in->evt)
1096 {
1097 release_object( console_in->evt );
1098 console_in->evt = NULL;
1099 }
Eric Pouecha86a2892003-06-23 03:37:14 +00001100 release_object( console_in->event );
Eric Pouechb39a8d92010-08-30 22:18:44 +02001101 if (console_in->fd)
Eric Pouechb39a8d92010-08-30 22:18:44 +02001102 release_object( console_in->fd );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001103
1104 for (i = 0; i < console_in->history_size; i++)
Lionel Debroux2f89fe72007-10-20 09:35:13 +02001105 free( console_in->history[i] );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +02001106 free( console_in->history );
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001107}
1108
1109static void screen_buffer_dump( struct object *obj, int verbose )
1110{
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001111 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001112 assert( obj->ops == &screen_buffer_ops );
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001113
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001114 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001115}
1116
1117static void screen_buffer_destroy( struct object *obj )
1118{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001119 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001120
Alexandre Julliard62a8b431999-01-19 17:48:23 +00001121 assert( obj->ops == &screen_buffer_ops );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001122
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +00001123 list_remove( &screen_buffer->entry );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001124
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001125 if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
1126 {
Alexandre Julliardb2eff1c2005-02-25 21:06:11 +00001127 struct screen_buffer *sb;
1128
1129 screen_buffer->input->active = NULL;
1130 LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
1131 {
1132 if (sb->input == screen_buffer->input)
1133 {
1134 sb->input->active = sb;
1135 break;
1136 }
1137 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001138 }
Eric Pouech6f326442010-08-30 22:18:52 +02001139 if (screen_buffer->fd) release_object( screen_buffer->fd );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +02001140 free( screen_buffer->data );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +00001141}
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001142
Eric Pouech6f326442010-08-30 22:18:52 +02001143static struct fd *screen_buffer_get_fd( struct object *obj )
1144{
1145 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
1146 assert( obj->ops == &screen_buffer_ops );
1147 return screen_buffer->fd ? (struct fd*)grab_object( screen_buffer->fd ) : NULL;
1148}
1149
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001150/* write data into a screen buffer */
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001151static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001152 const void* data, enum char_info_mode mode,
1153 int x, int y, int wrap )
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001154{
Michael Stefaniuca6249772006-07-25 10:30:04 +02001155 unsigned int i;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001156 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001157
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001158 if (y >= screen_buffer->height) return 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001159
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001160 if (wrap)
1161 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1162 else
1163 end = screen_buffer->data + (y+1) * screen_buffer->width;
1164
1165 switch(mode)
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001166 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001167 case CHAR_INFO_MODE_TEXT:
1168 {
1169 const WCHAR *ptr = data;
1170 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
1171 }
1172 break;
1173 case CHAR_INFO_MODE_ATTR:
1174 {
1175 const unsigned short *ptr = data;
1176 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
1177 }
1178 break;
1179 case CHAR_INFO_MODE_TEXTATTR:
1180 {
1181 const char_info_t *ptr = data;
1182 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
1183 }
1184 break;
1185 case CHAR_INFO_MODE_TEXTSTDATTR:
1186 {
1187 const WCHAR *ptr = data;
1188 for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
1189 {
1190 dest->ch = ptr[i];
1191 dest->attr = screen_buffer->attr;
1192 }
1193 }
1194 break;
1195 default:
1196 set_error( STATUS_INVALID_PARAMETER );
1197 return 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001198 }
1199
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001200 if (i && screen_buffer == screen_buffer->input->active)
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001201 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001202 struct console_renderer_event evt;
1203 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
Eric Pouech2e0b5332006-01-27 16:17:51 +01001204 memset(&evt.u, 0, sizeof(evt.u));
Eric Pouechf4b298b2003-01-05 01:07:34 +00001205 evt.u.update.top = y + x / screen_buffer->width;
1206 evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
Eric Pouech2f7af562010-08-30 22:19:06 +02001207 console_input_events_append( screen_buffer->input, &evt );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001208 }
1209 return i;
1210}
1211
1212/* fill a screen buffer with uniform data */
1213static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
1214 enum char_info_mode mode, int x, int y, int count, int wrap )
1215{
1216 int i;
1217 char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1218
1219 if (y >= screen_buffer->height) return 0;
1220
1221 if (wrap)
1222 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1223 else
1224 end = screen_buffer->data + (y+1) * screen_buffer->width;
1225
1226 if (count > end - dest) count = end - dest;
1227
1228 switch(mode)
1229 {
1230 case CHAR_INFO_MODE_TEXT:
1231 for (i = 0; i < count; i++) dest[i].ch = data.ch;
1232 break;
1233 case CHAR_INFO_MODE_ATTR:
1234 for (i = 0; i < count; i++) dest[i].attr = data.attr;
1235 break;
1236 case CHAR_INFO_MODE_TEXTATTR:
1237 for (i = 0; i < count; i++) dest[i] = data;
1238 break;
1239 case CHAR_INFO_MODE_TEXTSTDATTR:
1240 for (i = 0; i < count; i++)
1241 {
1242 dest[i].ch = data.ch;
1243 dest[i].attr = screen_buffer->attr;
1244 }
1245 break;
1246 default:
1247 set_error( STATUS_INVALID_PARAMETER );
1248 return 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001249 }
1250
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001251 if (count && screen_buffer == screen_buffer->input->active)
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001252 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001253 struct console_renderer_event evt;
1254 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
Eric Pouech2e0b5332006-01-27 16:17:51 +01001255 memset(&evt.u, 0, sizeof(evt.u));
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001256 evt.u.update.top = y;
1257 evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
Eric Pouech2f7af562010-08-30 22:19:06 +02001258 console_input_events_append( screen_buffer->input, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001259 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001260 return i;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001261}
1262
1263/* read data from a screen buffer */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001264static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
1265 enum char_info_mode mode, int wrap )
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001266{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001267 int i;
1268 char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001269
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001270 if (y >= screen_buffer->height) return;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001271
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001272 if (wrap)
1273 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
1274 else
1275 end = screen_buffer->data + (y+1) * screen_buffer->width;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001276
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001277 switch(mode)
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001278 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001279 case CHAR_INFO_MODE_TEXT:
1280 {
1281 WCHAR *data;
1282 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1283 if ((data = set_reply_data_size( count * sizeof(*data) )))
1284 {
1285 for (i = 0; i < count; i++) data[i] = src[i].ch;
1286 }
1287 }
1288 break;
1289 case CHAR_INFO_MODE_ATTR:
1290 {
1291 unsigned short *data;
1292 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1293 if ((data = set_reply_data_size( count * sizeof(*data) )))
1294 {
1295 for (i = 0; i < count; i++) data[i] = src[i].attr;
1296 }
1297 }
1298 break;
1299 case CHAR_INFO_MODE_TEXTATTR:
1300 {
1301 char_info_t *data;
1302 int count = min( end - src, get_reply_max_size() / sizeof(*data) );
1303 if ((data = set_reply_data_size( count * sizeof(*data) )))
1304 {
1305 for (i = 0; i < count; i++) data[i] = src[i];
1306 }
1307 }
1308 break;
1309 default:
1310 set_error( STATUS_INVALID_PARAMETER );
1311 break;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001312 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001313}
1314
1315/* scroll parts of a screen buffer */
Eric Pouechdaaee9e2010-08-30 22:19:12 +02001316static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001317 int w, int h )
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001318{
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001319 int j;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001320 char_info_t *psrc, *pdst;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001321 struct console_renderer_event evt;
1322
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001323 if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
1324 xsrc + w > screen_buffer->width ||
1325 xdst + w > screen_buffer->width ||
1326 ysrc + h > screen_buffer->height ||
1327 ydst + h > screen_buffer->height ||
1328 w == 0 || h == 0)
1329 {
1330 set_error( STATUS_INVALID_PARAMETER );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001331 return;
1332 }
1333
1334 if (ysrc < ydst)
1335 {
1336 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
1337 pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
1338
1339 for (j = h; j > 0; j--)
1340 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001341 memcpy(pdst, psrc, w * sizeof(*pdst) );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001342 pdst -= screen_buffer->width;
1343 psrc -= screen_buffer->width;
1344 }
1345 }
1346 else
1347 {
1348 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
1349 pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
1350
1351 for (j = 0; j < h; j++)
1352 {
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00001353 /* we use memmove here because when psrc and pdst are the same,
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001354 * copies are done on the same row, so the dst and src blocks
1355 * can overlap */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001356 memmove( pdst, psrc, w * sizeof(*pdst) );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001357 pdst += screen_buffer->width;
1358 psrc += screen_buffer->width;
1359 }
1360 }
1361
1362 /* FIXME: this could be enhanced, by signalling scroll */
1363 evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
Eric Pouech2e0b5332006-01-27 16:17:51 +01001364 memset(&evt.u, 0, sizeof(evt.u));
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001365 evt.u.update.top = min(ysrc, ydst);
1366 evt.u.update.bottom = max(ysrc, ydst) + h - 1;
Eric Pouech2f7af562010-08-30 22:19:06 +02001367 console_input_events_append( screen_buffer->input, &evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001368}
1369
1370/* allocate a console for the renderer */
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001371DECL_HANDLER(alloc_console)
1372{
Alexandre Julliard51885742002-05-30 20:12:58 +00001373 obj_handle_t in = 0;
1374 obj_handle_t evt = 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001375 struct process *process;
Eric Pouechb39a8d92010-08-30 22:18:44 +02001376 struct thread *renderer;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001377 struct console_input *console;
Eric Pouechb39a8d92010-08-30 22:18:44 +02001378 int fd;
Alexandre Julliard039aa421999-06-11 18:31:22 +00001379
Eric Pouechb39a8d92010-08-30 22:18:44 +02001380 switch (req->pid)
Alexandre Julliard31b02bb2007-01-04 09:41:14 +01001381 {
Eric Pouechb39a8d92010-08-30 22:18:44 +02001382 case 0:
1383 /* renderer is current, console to be attached to parent process */
1384 renderer = current;
Eric Pouech1f255342010-08-28 13:14:55 +02001385 if (!(process = current->process->parent))
Alexandre Julliard31b02bb2007-01-04 09:41:14 +01001386 {
1387 set_error( STATUS_ACCESS_DENIED );
1388 return;
1389 }
1390 grab_object( process );
Eric Pouechb39a8d92010-08-30 22:18:44 +02001391 break;
1392 case 0xffffffff:
1393 /* no renderer, console to be attached to current process */
1394 renderer = NULL;
1395 process = current->process;
1396 grab_object( process );
1397 break;
1398 default:
1399 /* renderer is current, console to be attached to req->pid */
1400 renderer = current;
1401 if (!(process = get_process_from_id( req->pid ))) return;
Alexandre Julliard31b02bb2007-01-04 09:41:14 +01001402 }
Alexandre Julliard039aa421999-06-11 18:31:22 +00001403
Eric Pouech1f255342010-08-28 13:14:55 +02001404 if (process != current->process && process->console)
Eric Pouechb39a8d92010-08-30 22:18:44 +02001405 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001406 set_error( STATUS_ACCESS_DENIED );
Eric Pouechb39a8d92010-08-30 22:18:44 +02001407 goto the_end;
1408 }
1409 if (req->input_fd != -1)
1410 {
1411 if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
1412 {
1413 set_error( STATUS_INVALID_PARAMETER );
1414 goto the_end;
1415 }
1416 }
1417 else fd = -1;
1418
1419 if ((console = (struct console_input*)create_console_input( renderer, fd )))
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001420 {
Eric Pouech1f255342010-08-28 13:14:55 +02001421 if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001422 {
Eric Pouechb39a8d92010-08-30 22:18:44 +02001423 if (!console->evt ||
1424 (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001425 {
Eric Pouechb39a8d92010-08-30 22:18:44 +02001426 process->console = (struct console_input*)grab_object( console );
1427 console->num_proc++;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001428 reply->handle_in = in;
1429 reply->event = evt;
1430 release_object( console );
1431 goto the_end;
1432 }
Eric Pouech1f255342010-08-28 13:14:55 +02001433 close_handle( current->process, in );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001434 }
1435 free_console( process );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001436 }
1437 the_end:
1438 release_object( process );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001439}
1440
1441/* free the console of the current process */
1442DECL_HANDLER(free_console)
1443{
1444 free_console( current->process );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001445}
1446
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001447/* let the renderer peek the events it's waiting on */
1448DECL_HANDLER(get_console_renderer_events)
1449{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001450 struct console_input_events *evt;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001451
1452 evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
Eric Pouech465f3d12010-08-28 13:14:43 +02001453 FILE_READ_PROPERTIES, &console_input_events_ops );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001454 if (!evt) return;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001455 console_input_events_get( evt );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001456 release_object( evt );
1457}
1458
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001459/* open a handle to the process console */
1460DECL_HANDLER(open_console)
1461{
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001462 struct object *obj = NULL;
Alexandre Julliard039aa421999-06-11 18:31:22 +00001463
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001464 reply->handle = 0;
Francois Gougetd6abd382008-12-01 11:10:06 +01001465 if (!req->from)
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001466 {
Eric Pouechb39a8d92010-08-30 22:18:44 +02001467 if (current->process->console)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001468 obj = grab_object( (struct object*)current->process->console );
Mike McCormackfac494c2006-06-07 18:33:46 +09001469 }
1470 else if (req->from == (obj_handle_t)1)
1471 {
Eric Pouech6f326442010-08-30 22:18:52 +02001472 if (current->process->console && current->process->console->active)
1473 obj = grab_object( (struct object*)current->process->console->active );
Mike McCormackfac494c2006-06-07 18:33:46 +09001474 }
1475 else if ((obj = get_handle_obj( current->process, req->from,
Eric Pouech465f3d12010-08-28 13:14:43 +02001476 FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
Mike McCormackfac494c2006-06-07 18:33:46 +09001477 {
1478 struct console_input *console = (struct console_input *)obj;
1479 obj = (console->active) ? grab_object( console->active ) : NULL;
1480 release_object( console );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001481 }
1482
1483 /* FIXME: req->share is not used (as in screen buffer creation) */
1484 if (obj)
1485 {
Alexandre Julliard24560e72005-12-09 13:58:25 +01001486 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001487 release_object( obj );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001488 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001489 else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001490}
1491
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001492/* set info about a console input */
1493DECL_HANDLER(set_console_input_info)
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001494{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001495 set_console_input_info( req, get_req_data(), get_req_data_size() );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001496}
1497
1498/* get info about a console (output only) */
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001499DECL_HANDLER(get_console_input_info)
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001500{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001501 struct console_input *console;
Alexandre Julliard92643002000-08-31 01:59:51 +00001502
Eric Pouech465f3d12010-08-28 13:14:43 +02001503 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001504 if (console->title)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +00001505 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001506 data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001507 if (len > get_reply_max_size()) len = get_reply_max_size();
1508 set_reply_data( console->title, len );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +00001509 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001510 reply->history_mode = console->history_mode;
1511 reply->history_size = console->history_size;
1512 reply->history_index = console->history_index;
Eric Pouechfa8b85a2003-01-09 06:01:32 +00001513 reply->edition_mode = console->edition_mode;
Kirill K. Smirnov9d746612007-05-10 21:19:47 +04001514 reply->input_cp = console->input_cp;
1515 reply->output_cp = console->output_cp;
Kirill K. Smirnovf3e13172007-08-23 19:17:17 +04001516 reply->win = console->win;
Eric Pouechfa8b85a2003-01-09 06:01:32 +00001517
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001518 release_object( console );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001519}
1520
1521/* get a console mode (input or output) */
1522DECL_HANDLER(get_console_mode)
1523{
Eric Pouech4fdcdef2010-08-30 22:19:00 +02001524 reply->mode = get_console_mode( req->handle, &reply->is_bare );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001525}
1526
1527/* set a console mode (input or output) */
1528DECL_HANDLER(set_console_mode)
1529{
1530 set_console_mode( req->handle, req->mode );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001531}
1532
1533/* add input records to a console input queue */
1534DECL_HANDLER(write_console_input)
1535{
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001536 struct console_input *console;
1537
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001538 reply->written = 0;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001539 if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
Eric Pouech465f3d12010-08-28 13:14:43 +02001540 FILE_WRITE_PROPERTIES, &console_input_ops )))
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001541 return;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001542 reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
1543 get_req_data() );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001544 release_object( console );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001545}
1546
1547/* fetch input records from a console input queue */
1548DECL_HANDLER(read_console_input)
1549{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001550 int count = get_reply_max_size() / sizeof(INPUT_RECORD);
1551 reply->read = read_console_input( req->handle, count, req->flush );
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001552}
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001553
1554/* appends a string to console's history */
1555DECL_HANDLER(append_console_input_history)
1556{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001557 struct console_input *console;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001558
Eric Pouech465f3d12010-08-28 13:14:43 +02001559 if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001560 console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001561 release_object( console );
1562}
1563
1564/* appends a string to console's history */
1565DECL_HANDLER(get_console_input_history)
1566{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001567 struct console_input *console;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001568
Eric Pouech465f3d12010-08-28 13:14:43 +02001569 if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001570 reply->total = console_input_get_hist( console, req->index );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001571 release_object( console );
1572}
1573
1574/* creates a screen buffer */
1575DECL_HANDLER(create_console_output)
1576{
1577 struct console_input* console;
1578 struct screen_buffer* screen_buffer;
Eric Pouech6f326442010-08-30 22:18:52 +02001579 int fd;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001580
Eric Pouech6f326442010-08-30 22:18:52 +02001581 if (req->fd != -1)
1582 {
1583 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
1584 {
Eric Pouechdaaee9e2010-08-30 22:19:12 +02001585 set_error( STATUS_INVALID_HANDLE );
Eric Pouech6f326442010-08-30 22:18:52 +02001586 return;
1587 }
1588 }
1589 else fd = -1;
1590 if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
1591 {
1592 close(fd);
1593 return;
1594 }
Eric Pouechdaaee9e2010-08-30 22:19:12 +02001595 if (console_input_is_bare( console ) ^ (fd != -1))
1596 {
1597 close( fd );
1598 set_error( STATUS_INVALID_HANDLE );
1599 return;
1600 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001601
Eric Pouech6f326442010-08-30 22:18:52 +02001602 screen_buffer = create_console_output( console, fd );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001603 if (screen_buffer)
1604 {
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00001605 /* FIXME: should store sharing and test it when opening the CONOUT$ device
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001606 * see file.c on how this could be done */
Alexandre Julliard24560e72005-12-09 13:58:25 +01001607 reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001608 release_object( screen_buffer );
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001609 }
1610 release_object( console );
1611}
1612
1613/* set info about a console screen buffer */
1614DECL_HANDLER(set_console_output_info)
1615{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001616 struct screen_buffer *screen_buffer;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001617
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001618 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
Eric Pouech465f3d12010-08-28 13:14:43 +02001619 FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001620 {
1621 set_console_output_info( screen_buffer, req );
1622 release_object( screen_buffer );
1623 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001624}
1625
1626/* get info about a console screen buffer */
1627DECL_HANDLER(get_console_output_info)
1628{
1629 struct screen_buffer *screen_buffer;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001630
1631 if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
Eric Pouech465f3d12010-08-28 13:14:43 +02001632 FILE_READ_PROPERTIES, &screen_buffer_ops)))
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001633 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001634 reply->cursor_size = screen_buffer->cursor_size;
1635 reply->cursor_visible = screen_buffer->cursor_visible;
1636 reply->cursor_x = screen_buffer->cursor_x;
1637 reply->cursor_y = screen_buffer->cursor_y;
1638 reply->width = screen_buffer->width;
1639 reply->height = screen_buffer->height;
1640 reply->attr = screen_buffer->attr;
1641 reply->win_left = screen_buffer->win.left;
1642 reply->win_top = screen_buffer->win.top;
1643 reply->win_right = screen_buffer->win.right;
1644 reply->win_bottom = screen_buffer->win.bottom;
1645 reply->max_width = screen_buffer->max_width;
1646 reply->max_height = screen_buffer->max_height;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001647 release_object( screen_buffer );
1648 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001649}
1650
1651/* read data (chars & attrs) from a screen buffer */
1652DECL_HANDLER(read_console_output)
1653{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001654 struct screen_buffer *screen_buffer;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001655
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001656 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
Eric Pouech465f3d12010-08-28 13:14:43 +02001657 FILE_READ_DATA, &screen_buffer_ops )))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001658 {
Eric Pouechdaaee9e2010-08-30 22:19:12 +02001659 if (console_input_is_bare( screen_buffer->input ))
1660 {
1661 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1662 release_object( screen_buffer );
1663 return;
1664 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001665 read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
1666 reply->width = screen_buffer->width;
1667 reply->height = screen_buffer->height;
1668 release_object( screen_buffer );
1669 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001670}
1671
1672/* write data (char and/or attrs) to a screen buffer */
1673DECL_HANDLER(write_console_output)
1674{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001675 struct screen_buffer *screen_buffer;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001676
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001677 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
Eric Pouech465f3d12010-08-28 13:14:43 +02001678 FILE_WRITE_DATA, &screen_buffer_ops)))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001679 {
Eric Pouechdaaee9e2010-08-30 22:19:12 +02001680 if (console_input_is_bare( screen_buffer->input ))
1681 {
1682 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1683 release_object( screen_buffer );
1684 return;
1685 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001686 reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
1687 req->mode, req->x, req->y, req->wrap );
1688 reply->width = screen_buffer->width;
1689 reply->height = screen_buffer->height;
1690 release_object( screen_buffer );
1691 }
1692}
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001693
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001694/* fill a screen buffer with constant data (chars and/or attributes) */
1695DECL_HANDLER(fill_console_output)
1696{
1697 struct screen_buffer *screen_buffer;
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001698
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001699 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
Eric Pouech465f3d12010-08-28 13:14:43 +02001700 FILE_WRITE_DATA, &screen_buffer_ops)))
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001701 {
Eric Pouechdaaee9e2010-08-30 22:19:12 +02001702 if (console_input_is_bare( screen_buffer->input ))
1703 {
1704 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1705 release_object( screen_buffer );
1706 return;
1707 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001708 reply->written = fill_console_output( screen_buffer, req->data, req->mode,
1709 req->x, req->y, req->count, req->wrap );
1710 release_object( screen_buffer );
1711 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001712}
1713
1714/* move a rect of data in a screen buffer */
1715DECL_HANDLER(move_console_output)
1716{
Eric Pouechdaaee9e2010-08-30 22:19:12 +02001717 struct screen_buffer *screen_buffer;
1718
1719 if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1720 FILE_WRITE_DATA, &screen_buffer_ops)))
1721 {
1722 if (console_input_is_bare( screen_buffer->input ))
1723 {
1724 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1725 release_object( screen_buffer );
1726 return;
1727 }
1728 scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
1729 req->w, req->h );
1730 release_object( screen_buffer );
1731 }
Eric Pouech0b83d4c2001-11-23 23:04:58 +00001732}
Eric Pouech93bfa0d2002-06-02 21:22:22 +00001733
1734/* sends a signal to a console (process, group...) */
1735DECL_HANDLER(send_console_signal)
1736{
Alexandre Julliard54f22872002-10-03 19:54:57 +00001737 process_id_t group;
Eric Pouech93bfa0d2002-06-02 21:22:22 +00001738
1739 group = req->group_id ? req->group_id : current->process->group_id;
1740
1741 if (!group)
Eric Pouech412d37f2003-06-21 02:07:10 +00001742 set_error( STATUS_INVALID_PARAMETER );
Eric Pouech93bfa0d2002-06-02 21:22:22 +00001743 else
1744 propagate_console_signal( current->process->console, req->signal, group );
1745}
Eric Pouech412d37f2003-06-21 02:07:10 +00001746
1747/* get console which renderer is 'current' */
1748static int cgwe_enum( struct process* process, void* user)
1749{
1750 if (process->console && process->console->renderer == current)
1751 {
1752 *(struct console_input**)user = process->console;
1753 return 1;
1754 }
1755 return 0;
1756}
1757
1758DECL_HANDLER(get_console_wait_event)
1759{
1760 struct console_input* console = NULL;
1761
Eric Pouechb39a8d92010-08-30 22:18:44 +02001762 if (current->process->console)
Eric Pouech412d37f2003-06-21 02:07:10 +00001763 console = (struct console_input*)grab_object( (struct object*)current->process->console );
1764 else enum_processes(cgwe_enum, &console);
1765
1766 if (console)
1767 {
Alexandre Julliard24560e72005-12-09 13:58:25 +01001768 reply->handle = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
Eric Pouech412d37f2003-06-21 02:07:10 +00001769 release_object( console );
1770 }
1771 else set_error( STATUS_INVALID_PARAMETER );
1772}