console.c 64.7 KB
Newer Older
1 2 3 4
/*
 * Server-side console management
 *
 * Copyright (C) 1998 Alexandre Julliard
5
 *               2001 Eric Pouech
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 29
#include <stdio.h>
#include <unistd.h>
30
#include <signal.h>
31

32 33
#include "ntstatus.h"
#define WIN32_NO_STATUS
34 35
#include "handle.h"
#include "process.h"
36
#include "request.h"
37
#include "file.h"
38
#include "unicode.h"
39
#include "wincon.h"
40
#include "winternl.h"
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
struct screen_buffer;
struct console_input_events;

struct console_input
{
    struct object                obj;           /* object header */
    int                          num_proc;      /* number of processes attached to this console */
    struct thread               *renderer;      /* console renderer thread */
    int                          mode;          /* input mode */
    struct screen_buffer        *active;        /* active screen buffer */
    int                          recnum;        /* number of input records */
    INPUT_RECORD                *records;       /* input records */
    struct console_input_events *evt;           /* synchronization event with renderer */
    WCHAR                       *title;         /* console title */
    WCHAR                      **history;       /* lines history */
    int                          history_size;  /* number of entries in history array */
    int                          history_index; /* number of used entries in history array */
    int                          history_mode;  /* mode of history (non zero means remove doubled strings */
    int                          edition_mode;  /* index to edition mode flavors */
    int                          input_cp;      /* console input codepage */
    int                          output_cp;     /* console output codepage */
63
    user_handle_t                win;           /* window handle if backend supports it */
64
    struct event                *event;         /* event to wait on for input queue */
65
    struct fd                   *fd;            /* for bare console, attached input fd */
66 67
};

68 69
static void console_input_dump( struct object *obj, int verbose );
static void console_input_destroy( struct object *obj );
70
static struct fd *console_input_get_fd( struct object *obj );
71 72

static const struct object_ops console_input_ops =
73
{
74 75
    sizeof(struct console_input),     /* size */
    console_input_dump,               /* dump */
76
    no_get_type,                      /* get_type */
77 78 79
    no_add_queue,                     /* add_queue */
    NULL,                             /* remove_queue */
    NULL,                             /* signaled */
80
    no_satisfied,                     /* satisfied */
81
    no_signal,                        /* signal */
82
    console_input_get_fd,             /* get_fd */
83
    default_fd_map_access,            /* map_access */
84 85
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
86
    no_lookup_name,                   /* lookup_name */
87 88
    no_link_name,                     /* link_name */
    NULL,                             /* unlink_name */
89
    no_open_file,                     /* open_file */
90
    no_kernel_obj_list,               /* get_kernel_obj_list */
91
    no_close_handle,                  /* close_handle */
92
    console_input_destroy             /* destroy */
93 94
};

95 96
static void console_input_events_dump( struct object *obj, int verbose );
static void console_input_events_destroy( struct object *obj );
97
static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry );
98

99
struct console_input_events
100 101 102 103 104 105 106 107 108 109 110
{
    struct object         obj;         /* object header */
    int			  num_alloc;   /* number of allocated events */
    int 		  num_used;    /* number of actually used events */
    struct console_renderer_event*	events;
};

static const struct object_ops console_input_events_ops =
{
    sizeof(struct console_input_events), /* size */
    console_input_events_dump,        /* dump */
111
    no_get_type,                      /* get_type */
112 113 114 115
    add_queue,                        /* add_queue */
    remove_queue,                     /* remove_queue */
    console_input_events_signaled,    /* signaled */
    no_satisfied,                     /* satisfied */
116
    no_signal,                        /* signal */
117
    no_get_fd,                        /* get_fd */
118
    default_fd_map_access,            /* map_access */
119 120
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
121
    no_lookup_name,                   /* lookup_name */
122 123
    no_link_name,                     /* link_name */
    NULL,                             /* unlink_name */
124
    no_open_file,                     /* open_file */
125
    no_kernel_obj_list,               /* get_kernel_obj_list */
126
    no_close_handle,                  /* close_handle */
127 128 129
    console_input_events_destroy      /* destroy */
};

130 131 132 133
struct font_info
{
    short int width;
    short int height;
134 135 136 137
    short int weight;
    short int pitch_family;
    WCHAR    *face_name;
    data_size_t face_len;
138 139
};

140 141 142
struct screen_buffer
{
    struct object         obj;           /* object header */
143
    struct list           entry;         /* entry in list of all screen buffers */
144 145 146 147 148 149 150 151 152 153 154
    struct console_input *input;         /* associated console input */
    int                   mode;          /* output mode */
    int                   cursor_size;   /* size of cursor (percentage filled) */
    int                   cursor_visible;/* cursor visibility flag */
    int                   cursor_x;      /* position of cursor */
    int                   cursor_y;      /* position of cursor */
    int                   width;         /* size (w-h) of the screen buffer */
    int                   height;
    int                   max_width;     /* size (w-h) of the window given font size */
    int                   max_height;
    char_info_t          *data;          /* the data for each cell - a width x height matrix */
155 156
    unsigned short        attr;          /* default fill attributes (screen colors) */
    unsigned short        popup_attr;    /* pop-up color attributes */
157
    unsigned int          color_map[16]; /* color table */
158
    rectangle_t           win;           /* current visible window on the screen buffer *
159
					  * as seen in wineconsole */
160
    struct font_info      font;          /* console font information */
161
    struct fd            *fd;            /* for bare console, attached output fd */
162 163 164 165
};

static void screen_buffer_dump( struct object *obj, int verbose );
static void screen_buffer_destroy( struct object *obj );
166
static struct fd *screen_buffer_get_fd( struct object *obj );
167

168 169
static const struct object_ops screen_buffer_ops =
{
170 171
    sizeof(struct screen_buffer),     /* size */
    screen_buffer_dump,               /* dump */
172
    no_get_type,                      /* get_type */
173 174 175 176
    no_add_queue,                     /* add_queue */
    NULL,                             /* remove_queue */
    NULL,                             /* signaled */
    NULL,                             /* satisfied */
177
    no_signal,                        /* signal */
178
    screen_buffer_get_fd,             /* get_fd */
179
    default_fd_map_access,            /* map_access */
180 181
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
182
    no_lookup_name,                   /* lookup_name */
183 184
    no_link_name,                     /* link_name */
    NULL,                             /* unlink_name */
185
    no_open_file,                     /* open_file */
186
    no_kernel_obj_list,               /* get_kernel_obj_list */
187
    no_close_handle,                  /* close_handle */
188
    screen_buffer_destroy             /* destroy */
189 190
};

191 192 193 194 195 196 197
static enum server_fd_type console_get_fd_type( struct fd *fd );

static const struct fd_ops console_fd_ops =
{
    default_fd_get_poll_events,   /* get_poll_events */
    default_poll_event,           /* poll_event */
    console_get_fd_type,          /* get_fd_type */
198 199 200
    no_fd_read,                   /* read */
    no_fd_write,                  /* write */
    no_fd_flush,                  /* flush */
201
    no_fd_get_file_info,          /* get_file_info */
202
    no_fd_get_volume_info,        /* get_volume_info */
203 204
    default_fd_ioctl,             /* ioctl */
    default_fd_queue_async,       /* queue_async */
205
    default_fd_reselect_async     /* reselect_async */
206 207
};

208
static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
209

210
static const char_info_t empty_char_info = { ' ', 0x000f };  /* white on black space */
211

212 213 214 215 216
static int console_input_is_bare( struct console_input* cin )
{
    return cin->evt == NULL;
}

217 218 219 220
static struct fd *console_input_get_fd( struct object* obj )
{
    struct console_input *console_input = (struct console_input*)obj;
    assert( obj->ops == &console_input_ops );
221 222 223 224
    if (console_input->fd)
        return (struct fd*)grab_object( console_input->fd );
    set_error( STATUS_OBJECT_TYPE_MISMATCH );
    return NULL;
225 226 227 228 229 230 231
}

static enum server_fd_type console_get_fd_type( struct fd *fd )
{
    return FD_TYPE_CHAR;
}

232 233 234 235 236
/* dumps the renderer events of a console */
static void console_input_events_dump( struct object *obj, int verbose )
{
    struct console_input_events *evts = (struct console_input_events *)obj;
    assert( obj->ops == &console_input_events_ops );
237
    fprintf( stderr, "Console input events: %d/%d events\n",
238 239 240 241 242 243 244 245 246 247
	     evts->num_used, evts->num_alloc );
}

/* destroys the renderer events of a console */
static void console_input_events_destroy( struct object *obj )
{
    struct console_input_events *evts = (struct console_input_events *)obj;
    assert( obj->ops == &console_input_events_ops );
    free( evts->events );
}
248

249
/* the renderer events list is signaled when it's not empty */
250
static int console_input_events_signaled( struct object *obj, struct wait_queue_entry *entry )
251 252 253
{
    struct console_input_events *evts = (struct console_input_events *)obj;
    assert( obj->ops == &console_input_events_ops );
254
    return (evts->num_used != 0);
255 256 257
}

/* add an event to the console's renderer events list */
258
static void console_input_events_append( struct console_input* console,
259 260
					 struct console_renderer_event* evt)
{
261
    struct console_input_events* evts;
262 263
    int collapsed = FALSE;

264
    if (!(evts = console->evt)) return;
265
    /* to be done even when evt has been generated by the renderer ? */
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

    /* try to collapse evt into current queue's events */
    if (evts->num_used)
    {
        struct console_renderer_event* last = &evts->events[evts->num_used - 1];

        if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
            evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
        {
            /* if two update events overlap, collapse them into a single one */
            if (last->u.update.bottom + 1 >= evt->u.update.top &&
                evt->u.update.bottom + 1 >= last->u.update.top)
            {
                last->u.update.top = min(last->u.update.top, evt->u.update.top);
                last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
                collapsed = TRUE;
            }
        }
    }
    if (!collapsed)
286
    {
287 288 289 290 291 292 293
        if (evts->num_used == evts->num_alloc)
        {
            evts->num_alloc += 16;
            evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
            assert(evts->events);
        }
        evts->events[evts->num_used++] = *evt;
294 295 296 297 298
    }
    wake_up( &evts->obj, 0 );
}

/* retrieves events from the console's renderer events list */
299
static void console_input_events_get( struct console_input_events* evts )
300
{
301
    data_size_t num = get_reply_max_size() / sizeof(evts->events[0]);
302 303 304

    if (num > evts->num_used) num = evts->num_used;
    set_reply_data( evts->events, num * sizeof(evts->events[0]) );
305 306
    if (num < evts->num_used)
    {
307 308
        memmove( &evts->events[0], &evts->events[num],
                 (evts->num_used - num) * sizeof(evts->events[0]) );
309 310 311 312 313 314 315 316
    }
    evts->num_used -= num;
}

static struct console_input_events *create_console_input_events(void)
{
    struct console_input_events*	evt;

317
    if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
318 319 320 321 322
    evt->num_alloc = evt->num_used = 0;
    evt->events = NULL;
    return evt;
}

323
static struct object *create_console_input( struct thread* renderer, int fd )
324
{
325
    struct console_input *console_input;
326

327 328 329 330 331
    if (!(console_input = alloc_object( &console_input_ops )))
    {
        if (fd != -1) close( fd );
        return NULL;
    }
332 333
    console_input->renderer      = renderer;
    console_input->mode          = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
334 335
                                   ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
                                   ENABLE_EXTENDED_FLAGS;
336 337 338 339
    console_input->num_proc      = 0;
    console_input->active        = NULL;
    console_input->recnum        = 0;
    console_input->records       = NULL;
340
    console_input->evt           = renderer ? create_console_input_events() : NULL;
341 342 343 344 345
    console_input->title         = NULL;
    console_input->history_size  = 50;
    console_input->history       = calloc( console_input->history_size, sizeof(WCHAR*) );
    console_input->history_index = 0;
    console_input->history_mode  = 0;
346
    console_input->edition_mode  = 0;
347 348
    console_input->input_cp      = 0;
    console_input->output_cp     = 0;
349
    console_input->win           = 0;
350
    console_input->event         = create_event( NULL, NULL, 0, 1, 0, NULL );
351
    console_input->fd            = NULL;
352

353
    if (!console_input->history || (renderer && !console_input->evt) || !console_input->event)
354
    {
355
        if (fd != -1) close( fd );
356
        console_input->history_size = 0;
357 358
        release_object( console_input );
        return NULL;
359
    }
360 361 362 363 364 365 366 367 368 369 370
    if (fd != -1) /* bare console */
    {
        if (!(console_input->fd = create_anonymous_fd( &console_fd_ops, fd, &console_input->obj,
                                                       FILE_SYNCHRONOUS_IO_NONALERT )))
        {
            release_object( console_input );
            return NULL;
        }
        allow_fd_caching( console_input->fd );
    }

371
    return &console_input->obj;
372 373
}

374 375 376 377 378 379 380
static void generate_sb_initial_events( struct console_input *console_input )
{
    struct screen_buffer *screen_buffer = console_input->active;
    struct console_renderer_event evt;

    evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
    memset(&evt.u, 0, sizeof(evt.u));
381
    console_input_events_append( console_input, &evt );
382 383 384 385

    evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
    evt.u.resize.width  = screen_buffer->width;
    evt.u.resize.height = screen_buffer->height;
386
    console_input_events_append( console_input, &evt );
387 388 389 390 391 392

    evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
    evt.u.display.left   = screen_buffer->win.left;
    evt.u.display.top    = screen_buffer->win.top;
    evt.u.display.width  = screen_buffer->win.right - screen_buffer->win.left + 1;
    evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
393
    console_input_events_append( console_input, &evt );
394 395 396 397

    evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
    evt.u.update.top    = 0;
    evt.u.update.bottom = screen_buffer->height - 1;
398
    console_input_events_append( console_input, &evt );
399 400 401 402

    evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
    evt.u.cursor_geom.size    = screen_buffer->cursor_size;
    evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
403
    console_input_events_append( console_input, &evt );
404 405 406 407

    evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
    evt.u.cursor_pos.x = screen_buffer->cursor_x;
    evt.u.cursor_pos.y = screen_buffer->cursor_y;
408
    console_input_events_append( console_input, &evt );
409 410
}

411
static struct screen_buffer *create_console_output( struct console_input *console_input, int fd )
412 413
{
    struct screen_buffer *screen_buffer;
414
    int	i;
415

416 417 418 419 420
    if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
    {
        if (fd != -1) close( fd );
        return NULL;
    }
421 422 423 424
    screen_buffer->mode           = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
    screen_buffer->input          = console_input;
    screen_buffer->cursor_size    = 100;
    screen_buffer->cursor_visible = 1;
425 426 427 428
    screen_buffer->width          = 80;
    screen_buffer->height         = 150;
    screen_buffer->max_width      = 80;
    screen_buffer->max_height     = 25;
429 430
    screen_buffer->cursor_x       = 0;
    screen_buffer->cursor_y       = 0;
431
    screen_buffer->attr           = 0x0F;
432
    screen_buffer->popup_attr     = 0xF5;
433 434 435 436
    screen_buffer->win.left       = 0;
    screen_buffer->win.right      = screen_buffer->max_width - 1;
    screen_buffer->win.top        = 0;
    screen_buffer->win.bottom     = screen_buffer->max_height - 1;
437
    screen_buffer->data           = NULL;
438 439
    screen_buffer->font.width     = 0;
    screen_buffer->font.height    = 0;
440 441 442 443
    screen_buffer->font.weight    = FW_NORMAL;
    screen_buffer->font.pitch_family  = FIXED_PITCH | FF_DONTCARE;
    screen_buffer->font.face_name = NULL;
    screen_buffer->font.face_len  = 0;
444
    memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
445 446
    list_add_head( &screen_buffer_list, &screen_buffer->entry );

447 448 449 450 451 452 453 454 455 456 457 458
    if (fd == -1)
        screen_buffer->fd = NULL;
    else
    {
        if (!(screen_buffer->fd = create_anonymous_fd( &console_fd_ops, fd, &screen_buffer->obj,
                                                       FILE_SYNCHRONOUS_IO_NONALERT )))
        {
            release_object( screen_buffer );
            return NULL;
        }
        allow_fd_caching(screen_buffer->fd);
    }
459

460 461 462 463 464 465 466 467 468 469 470 471 472
    if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
                                        sizeof(*screen_buffer->data) )))
    {
        release_object( screen_buffer );
        return NULL;
    }
    /* clear the first row */
    for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
    /* and copy it to all other rows */
    for (i = 1; i < screen_buffer->height; i++)
        memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
                screen_buffer->width * sizeof(char_info_t) );

473
    if (!console_input->active)
474
    {
475
	console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
476
        generate_sb_initial_events( console_input );
477
    }
478
    return screen_buffer;
479 480 481 482 483
}

/* free the console for this process */
int free_console( struct process *process )
{
484 485
    struct console_input* console = process->console;

486
    if (!console) return 0;
487 488

    process->console = NULL;
489
    if (--console->num_proc == 0 && console->renderer)
490 491 492 493
    {
	/* all processes have terminated... tell the renderer to terminate too */
	struct console_renderer_event evt;
	evt.event = CONSOLE_RENDERER_EXIT_EVENT;
494
        memset(&evt.u, 0, sizeof(evt.u));
495
	console_input_events_append( console, &evt );
496 497 498
    }
    release_object( console );

499 500 501
    return 1;
}

502 503 504
/* let process inherit the console from parent... this handle two cases :
 *	1/ generic console inheritance
 *	2/ parent is a renderer which launches process, and process should attach to the console
505
 *	   rendered by parent
506
 */
507 508
void inherit_console( struct thread *parent_thread, struct process *parent, struct process *process,
                      obj_handle_t hconin )
509
{
510
    int done = 0;
511

512 513 514
    /* if parent is a renderer, then attach current process to its console
     * a bit hacky....
     */
515
    if (hconin && parent_thread)
516
    {
517
        struct console_input *console;
518

519
        /* FIXME: should we check some access rights ? */
520 521 522
        if ((console = (struct console_input *)get_handle_obj( parent, hconin,
                                                               0, &console_input_ops )))
        {
523
            if (console->renderer == parent_thread)
524 525 526 527 528 529 530
            {
                process->console = (struct console_input *)grab_object( console );
                process->console->num_proc++;
                done = 1;
            }
            release_object( console );
        }
531
        else clear_error();  /* ignore error */
532
    }
533 534
    /* otherwise, if parent has a console, attach child to this console */
    if (!done && parent->console)
535
    {
536 537
        process->console = (struct console_input *)grab_object( parent->console );
        process->console->num_proc++;
538
    }
539 540
}

541 542 543 544 545
struct thread *console_get_renderer( struct console_input *console )
{
    return console->renderer;
}

546
static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
547
{
548
    struct console_input*	console = NULL;
549 550 551 552 553

    if (handle)
	console = (struct console_input *)get_handle_obj( current->process, handle,
							  access, &console_input_ops );
    else if (current->process->console)
554
    {
555
	console = (struct console_input *)grab_object( current->process->console );
556
    }
557

558 559 560
    if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
    return console;
}
561

562 563
struct console_signal_info
{
564
    struct console_input        *console;
565
    process_id_t                 group;
566 567 568 569 570 571 572 573
    int                          signal;
};

static int propagate_console_signal_cb(struct process *process, void *user)
{
    struct console_signal_info* csi = (struct console_signal_info*)user;

    if (process->console == csi->console && process->running_threads &&
574
        (!csi->group || process->group_id == csi->group))
575
    {
576 577
        /* find a suitable thread to signal */
        struct thread *thread;
578
        LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
579
        {
580
            if (send_thread_signal( thread, csi->signal )) break;
581 582 583 584 585
        }
    }
    return FALSE;
}

586
static void propagate_console_signal( struct console_input *console,
587
                                      int sig, process_id_t group_id )
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
{
    struct console_signal_info csi;

    if (!console)
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
    /* FIXME: should support the other events (like CTRL_BREAK) */
    if (sig != CTRL_C_EVENT)
    {
        set_error( STATUS_NOT_IMPLEMENTED );
        return;
    }
    csi.console = console;
    csi.signal  = SIGINT;
    csi.group   = group_id;

    enum_processes(propagate_console_signal_cb, &csi);
}

609
static int get_console_mode( obj_handle_t handle )
610 611 612 613
{
    struct object *obj;
    int ret = 0;

614
    if ((obj = get_handle_obj( current->process, handle, FILE_READ_PROPERTIES, NULL )))
615
    {
616
        if (obj->ops == &console_input_ops)
617
        {
618
            ret = ((struct console_input *)obj)->mode;
619
        }
620
        else if (obj->ops == &screen_buffer_ops)
621
        {
622
            ret = ((struct screen_buffer *)obj)->mode;
623
        }
624
        else
625
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
626
        release_object( obj );
627 628 629 630
    }
    return ret;
}

631
/* changes the mode of either a console input or a screen buffer */
632
static int set_console_mode( obj_handle_t handle, int mode )
633 634 635 636
{
    struct object *obj;
    int ret = 0;

637
    if (!(obj = get_handle_obj( current->process, handle, FILE_WRITE_PROPERTIES, NULL )))
638 639 640
        return 0;
    if (obj->ops == &console_input_ops)
    {
641
	/* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
642 643 644 645 646 647 648 649
        ((struct console_input *)obj)->mode = mode;
        ret = 1;
    }
    else if (obj->ops == &screen_buffer_ops)
    {
        ((struct screen_buffer *)obj)->mode = mode;
        ret = 1;
    }
650
    else set_error( STATUS_OBJECT_TYPE_MISMATCH );
651 652 653 654
    release_object( obj );
    return ret;
}

655
/* add input events to a console input queue */
656 657
static int write_console_input( struct console_input* console, int count,
                                const INPUT_RECORD *records )
658 659 660
{
    INPUT_RECORD *new_rec;

661
    if (!count) return 0;
662 663 664
    if (!(new_rec = realloc( console->records,
                             (console->recnum + count) * sizeof(INPUT_RECORD) )))
    {
665
        set_error( STATUS_NO_MEMORY );
666 667 668 669
        return -1;
    }
    console->records = new_rec;
    memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
670

671 672 673 674 675
    if (console->mode & ENABLE_PROCESSED_INPUT)
    {
        int i = 0;
        while (i < count)
        {
676
            if (records[i].EventType == KEY_EVENT &&
677 678 679 680
		records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
		!(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
            {
                if (i != count - 1)
681 682
                    memcpy( &console->records[console->recnum + i],
                            &console->records[console->recnum + i + 1],
683 684 685 686 687
                            (count - i - 1) * sizeof(INPUT_RECORD) );
                count--;
                if (records[i].Event.KeyEvent.bKeyDown)
                {
                    /* send SIGINT to all processes attached to this console */
688
                    propagate_console_signal( console, CTRL_C_EVENT, 0 );
689 690 691 692 693
                }
            }
            else i++;
        }
    }
694
    if (!console->recnum && count) set_event( console->event );
695
    console->recnum += count;
696 697 698 699
    return count;
}

/* retrieve a pointer to the console input records */
700
static int read_console_input( obj_handle_t handle, int count, int flush )
701 702 703 704
{
    struct console_input *console;

    if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
705
                                                            FILE_READ_DATA, &console_input_ops )))
706
        return -1;
707 708 709 710 711 712 713 714 715 716

    if (!count)
    {
        /* special case: do not retrieve anything, but return
         * the total number of records available */
        count = console->recnum;
    }
    else
    {
        if (count > console->recnum) count = console->recnum;
717
        set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
718
    }
719 720 721 722
    if (flush)
    {
        int i;
        for (i = count; i < console->recnum; i++)
723
            console->records[i-count] = console->records[i];
724 725 726 727 728 729 730 731 732 733
        if ((console->recnum -= count) > 0)
        {
            INPUT_RECORD *new_rec = realloc( console->records,
                                             console->recnum * sizeof(INPUT_RECORD) );
            if (new_rec) console->records = new_rec;
        }
        else
        {
            free( console->records );
            console->records = NULL;
734
            reset_event( console->event );
735 736 737 738 739 740
        }
    }
    release_object( console );
    return count;
}

741
/* set misc console input information */
742
static int set_console_input_info( const struct set_console_input_info_request *req,
743
				   const WCHAR *title, data_size_t len )
744
{
745 746 747
    struct console_input *console;
    struct console_renderer_event evt;

748
    if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) goto error;
749 750 751 752 753 754 755
    if (console_input_is_bare(console) &&
        (req->mask & (SET_CONSOLE_INPUT_INFO_ACTIVE_SB|
                      SET_CONSOLE_INPUT_INFO_WIN)))
    {
        set_error( STATUS_UNSUCCESSFUL );
        goto error;
    }
756

757
    memset(&evt.u, 0, sizeof(evt.u));
758 759 760 761
    if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
    {
	struct screen_buffer *screen_buffer;

762
	screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
763
								FILE_WRITE_PROPERTIES, &screen_buffer_ops );
764 765
	if (!screen_buffer || screen_buffer->input != console)
	{
766
	    set_error( STATUS_INVALID_HANDLE );
767 768 769 770 771 772 773 774
	    if (screen_buffer) release_object( screen_buffer );
	    goto error;
	}

	if (screen_buffer != console->active)
	{
	    if (console->active) release_object( console->active );
	    console->active = screen_buffer;
775
	    generate_sb_initial_events( console );
776 777 778 779 780 781 782 783 784
	}
	else
	    release_object( screen_buffer );
    }
    if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
    {
        WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
        if (new_title)
        {
785
            memcpy( new_title, title, len );
786
            new_title[len / sizeof(WCHAR)] = 0;
787
            free( console->title );
788
            console->title = new_title;
789
	    evt.event = CONSOLE_RENDERER_TITLE_EVENT;
790
	    console_input_events_append( console, &evt );
791 792 793 794 795 796
        }
    }
    if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
    {
	console->history_mode = req->history_mode;
    }
797
    if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
798 799 800 801 802 803 804 805 806 807 808 809 810
	console->history_size != req->history_size)
    {
	WCHAR**	mem = NULL;
	int	i;
	int	delta;

	if (req->history_size)
	{
	    mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
	    if (!mem) goto error;
	    memset( mem, 0, req->history_size * sizeof(WCHAR*) );
	}

811
	delta = (console->history_index > req->history_size) ?
812 813 814 815 816 817 818 819 820 821
	    (console->history_index - req->history_size) : 0;

	for (i = delta; i < console->history_index; i++)
	{
	    mem[i - delta] = console->history[i];
	    console->history[i] = NULL;
	}
	console->history_index -= delta;

	for (i = 0; i < console->history_size; i++)
822
	    free( console->history[i] );
823 824 825 826
	free( console->history );
	console->history = mem;
	console->history_size = req->history_size;
    }
827 828 829 830
    if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
    {
        console->edition_mode = req->edition_mode;
    }
831 832 833 834 835 836 837 838
    if (req->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
    {
        console->input_cp = req->input_cp;
    }
    if (req->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
    {
        console->output_cp = req->output_cp;
    }
839 840 841 842
    if (req->mask & SET_CONSOLE_INPUT_INFO_WIN)
    {
        console->win = req->win;
    }
843 844 845 846 847 848 849
    release_object( console );
    return 1;
 error:
    if (console) release_object( console );
    return 0;
}

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
/* resize a screen buffer */
static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
                                      int new_width, int new_height )
{
    int i, old_width, old_height, copy_width, copy_height;
    char_info_t *new_data;

    if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
    {
        set_error( STATUS_NO_MEMORY );
        return 0;
    }
    old_width = screen_buffer->width;
    old_height = screen_buffer->height;
    copy_width = min( old_width, new_width );
    copy_height = min( old_height, new_height );

    /* copy all the rows */
    for (i = 0; i < copy_height; i++)
    {
        memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
                copy_width * sizeof(char_info_t) );
    }

    /* clear the end of each row */
    if (new_width > old_width)
    {
        /* fill first row */
        for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
        /* and blast it to the other rows */
        for (i = 1; i < copy_height; i++)
            memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
                    (new_width - old_width) * sizeof(char_info_t) );
    }

    /* clear remaining rows */
    if (new_height > old_height)
    {
        /* fill first row */
        for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
        /* and blast it to the other rows */
        for (i = old_height+1; i < new_height; i++)
            memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
                    new_width * sizeof(char_info_t) );
    }
    free( screen_buffer->data );
    screen_buffer->data = new_data;
    screen_buffer->width = new_width;
    screen_buffer->height = new_height;
    return 1;
}

902
/* set misc screen buffer information */
903 904
static int set_console_output_info( struct screen_buffer *screen_buffer,
                                    const struct set_console_output_info_request *req )
905 906
{
    struct console_renderer_event evt;
907 908
    data_size_t font_name_len, offset;
    WCHAR *font_name;
909

910
    memset(&evt.u, 0, sizeof(evt.u));
911 912 913 914 915 916 917
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
    {
	if (req->cursor_size < 1 || req->cursor_size > 100)
	{
	    set_error( STATUS_INVALID_PARAMETER );
	    return 0;
	}
918
        if (screen_buffer->cursor_size != req->cursor_size ||
919 920 921 922 923 924 925
	    screen_buffer->cursor_visible != req->cursor_visible)
	{
	    screen_buffer->cursor_size    = req->cursor_size;
	    screen_buffer->cursor_visible = req->cursor_visible;
	    evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
	    evt.u.cursor_geom.size    = req->cursor_size;
	    evt.u.cursor_geom.visible = req->cursor_visible;
926
	    console_input_events_append( screen_buffer->input, &evt );
927 928 929 930
	}
    }
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
    {
931
	if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
932 933 934 935 936
	    req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
	{
	    set_error( STATUS_INVALID_PARAMETER );
	    return 0;
	}
937
	if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
938
	{
939 940
	    screen_buffer->cursor_x       = req->cursor_x;
	    screen_buffer->cursor_y       = req->cursor_y;
941 942 943
	    evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
	    evt.u.cursor_pos.x = req->cursor_x;
	    evt.u.cursor_pos.y = req->cursor_y;
944
	    console_input_events_append( screen_buffer->input, &evt );
945 946 947 948
	}
    }
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
    {
949 950 951 952 953 954 955 956 957
        unsigned cc;

        /* new screen-buffer cannot be smaller than actual window */
	if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
            req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
	{
	    set_error( STATUS_INVALID_PARAMETER );
	    return 0;
	}
958 959
        /* FIXME: there are also some basic minimum and max size to deal with */
        if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
960 961 962 963

	evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
	evt.u.resize.width  = req->width;
	evt.u.resize.height = req->height;
964
	console_input_events_append( screen_buffer->input, &evt );
965

966 967 968
	evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
	evt.u.update.top    = 0;
	evt.u.update.bottom = screen_buffer->height - 1;
969
	console_input_events_append( screen_buffer->input, &evt );
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000

        /* scroll window to display sb */
        if (screen_buffer->win.right >= req->width)
        {       
            screen_buffer->win.right -= screen_buffer->win.left;
            screen_buffer->win.left = 0;
        }
        if (screen_buffer->win.bottom >= req->height)
        {       
            screen_buffer->win.bottom -= screen_buffer->win.top;
            screen_buffer->win.top = 0;
        }
        /* reset cursor if needed (normally, if cursor was outside of new sb, the
         * window has been shifted so that the new position of the cursor will be 
         * visible */
        cc = 0;
        if (screen_buffer->cursor_x >= req->width)
        {
            screen_buffer->cursor_x = req->width - 1;
            cc++;
        }
        if (screen_buffer->cursor_y >= req->height)
        {
            screen_buffer->cursor_y = req->height - 1;
            cc++;
        }
        if (cc)
        {
            evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
            evt.u.cursor_pos.x = req->cursor_x;
            evt.u.cursor_pos.y = req->cursor_y;
1001
            console_input_events_append( screen_buffer->input, &evt );
1002 1003
        }

1004
	if (screen_buffer == screen_buffer->input->active &&
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	    screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
	{
	    INPUT_RECORD	ir;
	    ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
	    ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
	    ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
	    write_console_input( screen_buffer->input, 1, &ir );
	}
    }
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
    {
	screen_buffer->attr = req->attr;
    }
1018 1019 1020 1021
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
    {
        screen_buffer->popup_attr = req->popup_attr;
    }
1022 1023
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
    {
1024
	if (req->win_left < 0 || req->win_left > req->win_right ||
1025
	    req->win_right >= screen_buffer->width ||
1026
	    req->win_top < 0  || req->win_top > req->win_bottom ||
1027 1028 1029 1030 1031
	    req->win_bottom >= screen_buffer->height)
	{
	    set_error( STATUS_INVALID_PARAMETER );
	    return 0;
	}
1032 1033
	if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
	    screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
1034
	{
1035 1036 1037 1038
	    screen_buffer->win.left   = req->win_left;
	    screen_buffer->win.top    = req->win_top;
	    screen_buffer->win.right  = req->win_right;
	    screen_buffer->win.bottom = req->win_bottom;
1039 1040 1041 1042 1043
	    evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
	    evt.u.display.left   = req->win_left;
	    evt.u.display.top    = req->win_top;
	    evt.u.display.width  = req->win_right - req->win_left + 1;
	    evt.u.display.height = req->win_bottom - req->win_top + 1;
1044
	    console_input_events_append( screen_buffer->input, &evt );
1045 1046 1047 1048 1049 1050 1051
	}
    }
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
    {
	screen_buffer->max_width  = req->max_width;
	screen_buffer->max_height = req->max_height;
    }
1052 1053 1054 1055
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE)
    {
        memcpy( screen_buffer->color_map, get_req_data(), min( get_req_data_size(), sizeof(screen_buffer->color_map) ));
    }
1056 1057 1058 1059
    if (req->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
    {
        screen_buffer->font.width  = req->font_width;
        screen_buffer->font.height = req->font_height;
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
        screen_buffer->font.weight = req->font_weight;
        screen_buffer->font.pitch_family = req->font_pitch_family;
        offset = req->mask & SET_CONSOLE_OUTPUT_INFO_COLORTABLE ? sizeof(screen_buffer->color_map) : 0;
        if (get_req_data_size() > offset)
        {
            font_name_len = (get_req_data_size() - offset) / sizeof(WCHAR) * sizeof(WCHAR);
            font_name = mem_alloc( font_name_len );
            if (font_name)
            {
                memcpy( font_name, (char *)get_req_data() + offset, font_name_len );
                free( screen_buffer->font.face_name );
                screen_buffer->font.face_name = font_name;
                screen_buffer->font.face_len  = font_name_len;
            }
        }
1075
    }
1076 1077

    return 1;
1078 1079
}

1080
/* appends a new line to history (history is a fixed size array) */
1081
static void console_input_append_hist( struct console_input* console, const WCHAR* buf, data_size_t len )
1082
{
1083 1084 1085
    WCHAR*	ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );

    if (!ptr)
1086 1087
        return;

1088 1089 1090
    if (!console || !console->history_size)
    {
	set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
1091
	free( ptr );
1092 1093 1094 1095 1096 1097 1098
	return;
    }

    memcpy( ptr, buf, len * sizeof(WCHAR) );
    ptr[len] = 0;

    if (console->history_mode && console->history_index &&
1099
        !strcmpW( console->history[console->history_index - 1], ptr ))
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    {
	/* ok, mode ask us to not use twice the same string...
	 * so just free mem and returns
	 */
	set_error( STATUS_ALIAS_EXISTS );
	free(ptr);
	return;
    }

    if (console->history_index < console->history_size)
    {
	console->history[console->history_index++] = ptr;
    }
    else
    {
	free( console->history[0]) ;
1116
	memmove( &console->history[0], &console->history[1],
1117 1118 1119
		 (console->history_size - 1) * sizeof(WCHAR*) );
	console->history[console->history_size - 1] = ptr;
    }
1120 1121
}

1122
/* returns a line from the cache */
1123
static data_size_t console_input_get_hist( struct console_input *console, int index )
1124
{
1125
    data_size_t ret = 0;
1126

1127 1128
    if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
    else
1129
    {
1130 1131
        ret = strlenW( console->history[index] ) * sizeof(WCHAR);
        set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
1132 1133 1134 1135 1136 1137
    }
    return ret;
}

/* dumb dump */
static void console_input_dump( struct object *obj, int verbose )
1138
{
1139 1140
    struct console_input *console = (struct console_input *)obj;
    assert( obj->ops == &console_input_ops );
1141
    fprintf( stderr, "Console input active=%p evt=%p\n",
1142
	     console->active, console->evt );
1143 1144
}

1145 1146
static void console_input_destroy( struct object *obj )
{
1147 1148 1149 1150
    struct console_input*	console_in = (struct console_input *)obj;
    struct screen_buffer*	curr;
    int				i;

1151
    assert( obj->ops == &console_input_ops );
1152 1153
    free( console_in->title );
    free( console_in->records );
1154

1155
    if (console_in->active) release_object( console_in->active );
1156 1157
    console_in->active = NULL;

1158
    LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
1159
    {
1160
        if (curr->input == console_in) curr->input = NULL;
1161 1162
    }

1163 1164 1165 1166 1167
    if (console_in->evt)
    {
        release_object( console_in->evt );
        console_in->evt = NULL;
    }
1168 1169
    if (console_in->event)
        release_object( console_in->event );
1170 1171
    if (console_in->fd)
        release_object( console_in->fd );
1172 1173

    for (i = 0; i < console_in->history_size; i++)
1174
        free( console_in->history[i] );
1175
    free( console_in->history );
1176
}
1177

1178 1179
static void screen_buffer_dump( struct object *obj, int verbose )
{
1180
    struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1181
    assert( obj->ops == &screen_buffer_ops );
1182 1183

    fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
1184 1185
}

1186
static void screen_buffer_destroy( struct object *obj )
1187
{
1188
    struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
1189 1190 1191

    assert( obj->ops == &screen_buffer_ops );

1192
    list_remove( &screen_buffer->entry );
1193

1194 1195
    if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
    {
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
        struct screen_buffer *sb;

        screen_buffer->input->active = NULL;
        LIST_FOR_EACH_ENTRY( sb, &screen_buffer_list, struct screen_buffer, entry )
        {
            if (sb->input == screen_buffer->input)
            {
                sb->input->active = sb;
                break;
            }
        }
1207
    }
1208
    if (screen_buffer->fd) release_object( screen_buffer->fd );
1209
    free( screen_buffer->data );
1210
    free( screen_buffer->font.face_name );
1211 1212
}

1213 1214 1215 1216
static struct fd *screen_buffer_get_fd( struct object *obj )
{
    struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
    assert( obj->ops == &screen_buffer_ops );
1217 1218 1219 1220
    if (screen_buffer->fd)
        return (struct fd*)grab_object( screen_buffer->fd );
    set_error( STATUS_OBJECT_TYPE_MISMATCH );
    return NULL;
1221 1222
}

1223
/* write data into a screen buffer */
1224
static int write_console_output( struct screen_buffer *screen_buffer, data_size_t size,
1225 1226
                                 const void* data, enum char_info_mode mode,
                                 int x, int y, int wrap )
1227
{
1228
    unsigned int i;
1229
    char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
1230

1231 1232 1233 1234 1235 1236
    if (y >= screen_buffer->height) return 0;

    if (wrap)
        end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
    else
        end = screen_buffer->data + (y+1) * screen_buffer->width;
1237

1238
    switch(mode)
1239
    {
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    case CHAR_INFO_MODE_TEXT:
        {
            const WCHAR *ptr = data;
            for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
        }
        break;
    case CHAR_INFO_MODE_ATTR:
        {
            const unsigned short *ptr = data;
            for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
        }
        break;
    case CHAR_INFO_MODE_TEXTATTR:
        {
            const char_info_t *ptr = data;
            for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
        }
        break;
    case CHAR_INFO_MODE_TEXTSTDATTR:
        {
            const WCHAR *ptr = data;
            for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
            {
                dest->ch   = ptr[i];
                dest->attr = screen_buffer->attr;
            }
        }
        break;
    default:
        set_error( STATUS_INVALID_PARAMETER );
        return 0;
1271 1272
    }

1273 1274 1275 1276
    if (i && screen_buffer == screen_buffer->input->active)
    {
        struct console_renderer_event evt;
        evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1277
        memset(&evt.u, 0, sizeof(evt.u));
1278 1279
        evt.u.update.top    = y + x / screen_buffer->width;
        evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
1280
        console_input_events_append( screen_buffer->input, &evt );
1281 1282 1283
    }
    return i;
}
1284

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
/* fill a screen buffer with uniform data */
static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
                                enum char_info_mode mode, int x, int y, int count, int wrap )
{
    int i;
    char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;

    if (y >= screen_buffer->height) return 0;

    if (wrap)
        end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
    else
        end = screen_buffer->data + (y+1) * screen_buffer->width;
1298

1299 1300 1301
    if (count > end - dest) count = end - dest;

    switch(mode)
1302
    {
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
    case CHAR_INFO_MODE_TEXT:
        for (i = 0; i < count; i++) dest[i].ch = data.ch;
        break;
    case CHAR_INFO_MODE_ATTR:
        for (i = 0; i < count; i++) dest[i].attr = data.attr;
        break;
    case CHAR_INFO_MODE_TEXTATTR:
        for (i = 0; i < count; i++) dest[i] = data;
        break;
    case CHAR_INFO_MODE_TEXTSTDATTR:
        for (i = 0; i < count; i++)
        {
            dest[i].ch   = data.ch;
            dest[i].attr = screen_buffer->attr;
        }
        break;
    default:
        set_error( STATUS_INVALID_PARAMETER );
        return 0;
1322 1323
    }

1324
    if (count && screen_buffer == screen_buffer->input->active)
1325
    {
1326 1327
        struct console_renderer_event evt;
        evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1328
        memset(&evt.u, 0, sizeof(evt.u));
1329 1330
        evt.u.update.top    = y;
        evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
1331
        console_input_events_append( screen_buffer->input, &evt );
1332
    }
1333
    return i;
1334 1335
}

1336
/* read data from a screen buffer */
1337 1338
static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
                                 enum char_info_mode mode, int wrap )
1339
{
1340 1341
    int i;
    char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
1342

1343
    if (y >= screen_buffer->height) return;
1344

1345 1346 1347 1348
    if (wrap)
        end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
    else
        end = screen_buffer->data + (y+1) * screen_buffer->width;
1349

1350
    switch(mode)
1351
    {
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
    case CHAR_INFO_MODE_TEXT:
        {
            WCHAR *data;
            int count = min( end - src, get_reply_max_size() / sizeof(*data) );
            if ((data = set_reply_data_size( count * sizeof(*data) )))
            {
                for (i = 0; i < count; i++) data[i] = src[i].ch;
            }
        }
        break;
    case CHAR_INFO_MODE_ATTR:
        {
            unsigned short *data;
            int count = min( end - src, get_reply_max_size() / sizeof(*data) );
            if ((data = set_reply_data_size( count * sizeof(*data) )))
            {
                for (i = 0; i < count; i++) data[i] = src[i].attr;
            }
        }
        break;
    case CHAR_INFO_MODE_TEXTATTR:
        {
            char_info_t *data;
            int count = min( end - src, get_reply_max_size() / sizeof(*data) );
            if ((data = set_reply_data_size( count * sizeof(*data) )))
            {
                for (i = 0; i < count; i++) data[i] = src[i];
            }
        }
        break;
    default:
        set_error( STATUS_INVALID_PARAMETER );
        break;
1385
    }
1386
}
1387

1388
/* scroll parts of a screen buffer */
1389
static void scroll_console_output( struct screen_buffer *screen_buffer, int xsrc, int ysrc, int xdst, int ydst,
1390
                                   int w, int h )
1391
{
1392
    int				j;
1393
    char_info_t *psrc, *pdst;
1394
    struct console_renderer_event evt;
1395

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
    if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
	xsrc + w > screen_buffer->width  ||
	xdst + w > screen_buffer->width  ||
	ysrc + h > screen_buffer->height ||
	ydst + h > screen_buffer->height ||
	w == 0 || h == 0)
    {
	set_error( STATUS_INVALID_PARAMETER );
	return;
    }
1406

1407
    if (ysrc < ydst)
1408
    {
1409 1410 1411 1412 1413
	psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
	pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];

	for (j = h; j > 0; j--)
	{
1414
	    memcpy(pdst, psrc, w * sizeof(*pdst) );
1415 1416 1417
	    pdst -= screen_buffer->width;
	    psrc -= screen_buffer->width;
	}
1418
    }
1419 1420 1421 1422 1423 1424 1425
    else
    {
	psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
	pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];

	for (j = 0; j < h; j++)
	{
1426
	    /* we use memmove here because when psrc and pdst are the same,
1427 1428
	     * copies are done on the same row, so the dst and src blocks
	     * can overlap */
1429
	    memmove( pdst, psrc, w * sizeof(*pdst) );
1430 1431 1432 1433 1434 1435 1436
	    pdst += screen_buffer->width;
	    psrc += screen_buffer->width;
	}
    }

    /* FIXME: this could be enhanced, by signalling scroll */
    evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
1437
    memset(&evt.u, 0, sizeof(evt.u));
1438 1439
    evt.u.update.top    = min(ysrc, ydst);
    evt.u.update.bottom = max(ysrc, ydst) + h - 1;
1440
    console_input_events_append( screen_buffer->input, &evt );
1441 1442 1443 1444 1445
}

/* allocate a console for the renderer */
DECL_HANDLER(alloc_console)
{
1446 1447
    obj_handle_t in = 0;
    obj_handle_t evt = 0;
1448
    struct process *process;
1449
    struct thread *renderer;
1450
    struct console_input *console;
1451
    int fd;
1452
    int attach = 0;
1453

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    if (req->input_fd != -1)
    {
        if ((fd = thread_get_inflight_fd( current, req->input_fd )) == -1)
        {
            set_error( STATUS_INVALID_PARAMETER );
            return;
        }
    }
    else fd = -1;

1464
    switch (req->pid)
1465
    {
1466 1467 1468
    case 0:
        /* renderer is current, console to be attached to parent process */
        renderer = current;
1469
        if (!(process = get_process_from_id( current->process->parent_id )))
1470
        {
1471
            if (fd != -1) close( fd );
1472 1473 1474
            set_error( STATUS_ACCESS_DENIED );
            return;
        }
1475
        attach = 1;
1476 1477 1478 1479 1480 1481
        break;
    case 0xffffffff:
        /* no renderer, console to be attached to current process */
        renderer = NULL;
        process = current->process;
        grab_object( process );
1482
        attach = 1;
1483 1484 1485 1486
        break;
    default:
        /* renderer is current, console to be attached to req->pid */
        renderer = current;
1487 1488 1489 1490 1491
        if (!(process = get_process_from_id( req->pid )))
        {
            if (fd != -1) close( fd );
            return;
        }
1492
    }
1493

1494
    if (attach && process->console)
1495
    {
1496
        if (fd != -1) close( fd );
1497
        set_error( STATUS_ACCESS_DENIED );
1498 1499 1500 1501
        goto the_end;
    }

    if ((console = (struct console_input*)create_console_input( renderer, fd )))
1502
    {
1503
        if ((in = alloc_handle( current->process, console, req->access, req->attributes )))
1504
        {
1505 1506
            if (!console->evt ||
                (evt = alloc_handle( current->process, console->evt, SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, 0 )))
1507
            {
1508 1509 1510 1511 1512
                if (attach)
                {
                    process->console = (struct console_input*)grab_object( console );
                    console->num_proc++;
                }
1513 1514 1515 1516 1517
                reply->handle_in = in;
                reply->event = evt;
                release_object( console );
                goto the_end;
            }
1518
            close_handle( current->process, in );
1519
        }
1520
        release_object( console );
1521 1522 1523
    }
 the_end:
    release_object( process );
1524 1525 1526 1527 1528 1529 1530 1531
}

/* free the console of the current process */
DECL_HANDLER(free_console)
{
    free_console( current->process );
}

1532 1533 1534
/* let the renderer peek the events it's waiting on */
DECL_HANDLER(get_console_renderer_events)
{
1535
    struct console_input_events *evt;
1536 1537

    evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
1538
                                                         FILE_READ_PROPERTIES, &console_input_events_ops );
1539
    if (!evt) return;
1540
    console_input_events_get( evt );
1541 1542 1543
    release_object( evt );
}

1544 1545 1546
/* open a handle to the process console */
DECL_HANDLER(open_console)
{
1547
    struct object      *obj = NULL;
1548

1549
    reply->handle = 0;
1550
    if (!req->from)
1551
    {
1552
        if (current->process->console)
1553
            obj = grab_object( (struct object*)current->process->console );
1554 1555 1556
    }
    else if (req->from == (obj_handle_t)1)
    {
1557 1558
        if (current->process->console && current->process->console->active)
            obj = grab_object( (struct object*)current->process->console->active );
1559 1560
    }
    else if ((obj = get_handle_obj( current->process, req->from,
1561
                                    FILE_READ_PROPERTIES|FILE_WRITE_PROPERTIES, &console_input_ops )))
1562 1563 1564 1565
    {
        struct console_input *console = (struct console_input *)obj;
        obj = (console->active) ? grab_object( console->active ) : NULL;
        release_object( console );
1566 1567 1568 1569 1570
    }

    /* FIXME: req->share is not used (as in screen buffer creation)  */
    if (obj)
    {
1571
        reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1572
        release_object( obj );
1573
    }
1574
    else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
1575 1576
}

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
/* attach to a other process's console */
DECL_HANDLER(attach_console)
{
    struct process *process;

    if (current->process->console)
    {
        set_error( STATUS_ACCESS_DENIED );
        return;
    }

    process = get_process_from_id( req->pid == ATTACH_PARENT_PROCESS
                                   ? current->process->parent_id : req->pid );
    if (!process) return;

    if (process->console && process->console->active)
    {
        reply->std_in = alloc_handle( current->process, process->console, GENERIC_READ, 0 );
        if (!reply->std_in) goto error;

        reply->std_out = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
        if (!reply->std_out) goto error;

        reply->std_err = alloc_handle( current->process, process->console->active, GENERIC_WRITE, 0 );
        if (!reply->std_err) goto error;

        current->process->console = (struct console_input *)grab_object( process->console );
        current->process->console->num_proc++;
    }
    else
    {
        set_error( STATUS_INVALID_HANDLE );
    }

    release_object( process );
    return;

error:
    if (reply->std_in) close_handle( current->process, reply->std_in );
    if (reply->std_out) close_handle( current->process, reply->std_out );
    release_object( process );
}

1620 1621
/* set info about a console input */
DECL_HANDLER(set_console_input_info)
1622
{
1623
    set_console_input_info( req, get_req_data(), get_req_data_size() );
1624 1625 1626
}

/* get info about a console (output only) */
1627
DECL_HANDLER(get_console_input_info)
1628
{
1629
    struct console_input *console;
1630

1631
    if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1632
    if (console->title)
1633
    {
1634
        data_size_t len = strlenW( console->title ) * sizeof(WCHAR);
1635 1636
        if (len > get_reply_max_size()) len = get_reply_max_size();
        set_reply_data( console->title, len );
1637
    }
1638 1639 1640
    reply->history_mode  = console->history_mode;
    reply->history_size  = console->history_size;
    reply->history_index = console->history_index;
1641
    reply->edition_mode  = console->edition_mode;
1642 1643
    reply->input_cp      = console->input_cp;
    reply->output_cp     = console->output_cp;
1644
    reply->win           = console->win;
1645

1646
    release_object( console );
1647 1648 1649 1650 1651
}

/* get a console mode (input or output) */
DECL_HANDLER(get_console_mode)
{
1652
    reply->mode = get_console_mode( req->handle );
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
}

/* set a console mode (input or output) */
DECL_HANDLER(set_console_mode)
{
    set_console_mode( req->handle, req->mode );
}

/* add input records to a console input queue */
DECL_HANDLER(write_console_input)
{
1664 1665
    struct console_input *console;

1666
    reply->written = 0;
1667
    if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
1668
                                                            FILE_WRITE_PROPERTIES, &console_input_ops )))
1669
        return;
1670 1671
    reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
                                          get_req_data() );
1672
    release_object( console );
1673 1674 1675 1676 1677
}

/* fetch input records from a console input queue */
DECL_HANDLER(read_console_input)
{
1678 1679
    int count = get_reply_max_size() / sizeof(INPUT_RECORD);
    reply->read = read_console_input( req->handle, count, req->flush );
1680
}
1681 1682 1683 1684

/* appends a string to console's history */
DECL_HANDLER(append_console_input_history)
{
1685
    struct console_input *console;
1686

1687
    if (!(console = console_input_get( req->handle, FILE_WRITE_PROPERTIES ))) return;
1688
    console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1689 1690 1691 1692 1693 1694
    release_object( console );
}

/* appends a string to console's history */
DECL_HANDLER(get_console_input_history)
{
1695
    struct console_input *console;
1696

1697
    if (!(console = console_input_get( req->handle, FILE_READ_PROPERTIES ))) return;
1698
    reply->total = console_input_get_hist( console, req->index );
1699 1700 1701 1702 1703 1704 1705 1706
    release_object( console );
}

/* creates a screen buffer */
DECL_HANDLER(create_console_output)
{
    struct console_input*	console;
    struct screen_buffer*	screen_buffer;
1707
    int                         fd;
1708

1709 1710 1711 1712
    if (req->fd != -1)
    {
        if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
        {
1713
            set_error( STATUS_INVALID_HANDLE );
1714 1715 1716 1717 1718 1719
            return;
        }
    }
    else fd = -1;
    if (!(console = console_input_get( req->handle_in, FILE_WRITE_PROPERTIES )))
    {
1720
        if (fd != -1) close( fd );
1721 1722
        return;
    }
1723 1724
    if (console_input_is_bare( console ) ^ (fd != -1))
    {
1725
        if (fd != -1) close( fd );
1726
        release_object( console );
1727 1728 1729
        set_error( STATUS_INVALID_HANDLE );
        return;
    }
1730

1731
    screen_buffer = create_console_output( console, fd );
1732 1733
    if (screen_buffer)
    {
1734
        /* FIXME: should store sharing and test it when opening the CONOUT$ device
1735
         * see file.c on how this could be done */
1736
        reply->handle_out = alloc_handle( current->process, screen_buffer, req->access, req->attributes );
1737
        release_object( screen_buffer );
1738 1739 1740 1741 1742 1743 1744
    }
    release_object( console );
}

/* set info about a console screen buffer */
DECL_HANDLER(set_console_output_info)
{
1745
    struct screen_buffer *screen_buffer;
1746

1747
    if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1748
                                                                FILE_WRITE_PROPERTIES, &screen_buffer_ops)))
1749 1750 1751 1752
    {
        set_console_output_info( screen_buffer, req );
        release_object( screen_buffer );
    }
1753 1754 1755 1756 1757 1758
}

/* get info about a console screen buffer */
DECL_HANDLER(get_console_output_info)
{
    struct screen_buffer *screen_buffer;
1759 1760
    void *data;
    data_size_t total;
1761 1762

    if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
1763
                                                                 FILE_READ_PROPERTIES, &screen_buffer_ops)))
1764
    {
1765 1766 1767 1768 1769 1770 1771
        reply->cursor_size    = screen_buffer->cursor_size;
        reply->cursor_visible = screen_buffer->cursor_visible;
        reply->cursor_x       = screen_buffer->cursor_x;
        reply->cursor_y       = screen_buffer->cursor_y;
        reply->width          = screen_buffer->width;
        reply->height         = screen_buffer->height;
        reply->attr           = screen_buffer->attr;
1772
        reply->popup_attr     = screen_buffer->popup_attr;
1773 1774 1775 1776 1777 1778
        reply->win_left       = screen_buffer->win.left;
        reply->win_top        = screen_buffer->win.top;
        reply->win_right      = screen_buffer->win.right;
        reply->win_bottom     = screen_buffer->win.bottom;
        reply->max_width      = screen_buffer->max_width;
        reply->max_height     = screen_buffer->max_height;
1779 1780
        reply->font_width     = screen_buffer->font.width;
        reply->font_height    = screen_buffer->font.height;
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
        reply->font_weight    = screen_buffer->font.weight;
        reply->font_pitch_family = screen_buffer->font.pitch_family;
        total = min( sizeof(screen_buffer->color_map) + screen_buffer->font.face_len, get_reply_max_size() );
        if (total)
        {
            data = set_reply_data_size( total );
            memcpy( data, screen_buffer->color_map, min( total, sizeof(screen_buffer->color_map) ));
            if (screen_buffer->font.face_len && total > sizeof(screen_buffer->color_map))
            {
                memcpy( (char *)data + sizeof(screen_buffer->color_map), screen_buffer->font.face_name,
                        min( total - sizeof(screen_buffer->color_map), screen_buffer->font.face_len ));
            }
        }
1794 1795 1796 1797 1798 1799 1800
        release_object( screen_buffer );
    }
}

/* read data (chars & attrs) from a screen buffer */
DECL_HANDLER(read_console_output)
{
1801
    struct screen_buffer *screen_buffer;
1802

1803
    if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1804
                                                                FILE_READ_DATA, &screen_buffer_ops )))
1805
    {
1806 1807 1808 1809 1810 1811
        if (console_input_is_bare( screen_buffer->input ))
        {
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
            release_object( screen_buffer );
            return;
        }
1812 1813 1814 1815 1816
        read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
        reply->width  = screen_buffer->width;
        reply->height = screen_buffer->height;
        release_object( screen_buffer );
    }
1817 1818 1819 1820 1821
}

/* write data (char and/or attrs) to a screen buffer */
DECL_HANDLER(write_console_output)
{
1822
    struct screen_buffer *screen_buffer;
1823

1824
    if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1825
                                                                FILE_WRITE_DATA, &screen_buffer_ops)))
1826
    {
1827 1828 1829 1830 1831 1832
        if (console_input_is_bare( screen_buffer->input ))
        {
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
            release_object( screen_buffer );
            return;
        }
1833 1834 1835 1836 1837 1838 1839
        reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
                                               req->mode, req->x, req->y, req->wrap );
        reply->width  = screen_buffer->width;
        reply->height = screen_buffer->height;
        release_object( screen_buffer );
    }
}
1840

1841 1842 1843 1844
/* fill a screen buffer with constant data (chars and/or attributes) */
DECL_HANDLER(fill_console_output)
{
    struct screen_buffer *screen_buffer;
1845

1846
    if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
1847
                                                                FILE_WRITE_DATA, &screen_buffer_ops)))
1848
    {
1849 1850 1851 1852 1853 1854
        if (console_input_is_bare( screen_buffer->input ))
        {
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
            release_object( screen_buffer );
            return;
        }
1855 1856 1857 1858
        reply->written = fill_console_output( screen_buffer, req->data, req->mode,
                                              req->x, req->y, req->count, req->wrap );
        release_object( screen_buffer );
    }
1859 1860 1861 1862 1863
}

/* move a rect of data in a screen buffer */
DECL_HANDLER(move_console_output)
{
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
    struct screen_buffer *screen_buffer;

    if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
                                                                FILE_WRITE_DATA, &screen_buffer_ops)))
    {
        if (console_input_is_bare( screen_buffer->input ))
        {
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
            release_object( screen_buffer );
            return;
        }
        scroll_console_output( screen_buffer, req->x_src, req->y_src, req->x_dst, req->y_dst,
                               req->w, req->h );
        release_object( screen_buffer );
    }
1879
}
1880 1881 1882 1883

/* sends a signal to a console (process, group...) */
DECL_HANDLER(send_console_signal)
{
1884
    process_id_t group;
1885 1886 1887 1888

    group = req->group_id ? req->group_id : current->process->group_id;

    if (!group)
1889
        set_error( STATUS_INVALID_PARAMETER );
1890 1891 1892
    else
        propagate_console_signal( current->process->console, req->signal, group );
}
1893 1894 1895 1896 1897 1898

/* get console which renderer is 'current' */
static int cgwe_enum( struct process* process, void* user)
{
    if (process->console && process->console->renderer == current)
    {
1899
        *(struct console_input**)user = (struct console_input *)grab_object( process->console );
1900 1901 1902 1903 1904 1905 1906 1907
        return 1;
    }
    return 0;
}

DECL_HANDLER(get_console_wait_event)
{
    struct console_input* console = NULL;
1908
    struct object *obj;
1909

1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
    if (req->handle)
    {
        if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
        if (obj->ops == &console_input_ops)
            console = (struct console_input *)grab_object( obj );
        else if (obj->ops == &screen_buffer_ops)
            console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
        else
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
        release_object( obj );
    }
    else if (current->process->console)
        console = (struct console_input *)grab_object( current->process->console );
1923 1924 1925 1926
    else enum_processes(cgwe_enum, &console);

    if (console)
    {
1927
        reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
1928 1929 1930 1931
        release_object( console );
    }
    else set_error( STATUS_INVALID_PARAMETER );
}