vbscript: Fix memory leak in owned safearray iterator.
[wine.git] / programs / conhost / conhost.c
blob1bed7b8a03c56f409a30ab8bbdef3b0a9608a6d2
1 /*
2 * Copyright 1998 Alexandre Julliard
3 * Copyright 2001 Eric Pouech
4 * Copyright 2012 Detlef Riekenberg
5 * Copyright 2020 Jacek Caban
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <assert.h>
23 #include <limits.h>
25 #include "conhost.h"
27 #include "wine/server.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(console);
32 static const char_info_t empty_char_info = { ' ', 0x0007 }; /* white on black space */
34 static CRITICAL_SECTION console_section;
35 static CRITICAL_SECTION_DEBUG critsect_debug =
37 0, 0, &console_section,
38 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
39 0, 0, { (DWORD_PTR)(__FILE__ ": console_section") }
41 static CRITICAL_SECTION console_section = { &critsect_debug, -1, 0, 0, 0, 0 };
43 static void *ioctl_buffer;
44 static size_t ioctl_buffer_size;
46 static void *alloc_ioctl_buffer( size_t size )
48 if (size > ioctl_buffer_size)
50 void *new_buffer;
51 if (!(new_buffer = realloc( ioctl_buffer, size ))) return NULL;
52 ioctl_buffer = new_buffer;
53 ioctl_buffer_size = size;
55 return ioctl_buffer;
58 static int screen_buffer_compare_id( const void *key, const struct wine_rb_entry *entry )
60 struct screen_buffer *screen_buffer = WINE_RB_ENTRY_VALUE( entry, struct screen_buffer, entry );
61 return PtrToLong(key) - screen_buffer->id;
64 static struct wine_rb_tree screen_buffer_map = { screen_buffer_compare_id };
66 static void destroy_screen_buffer( struct screen_buffer *screen_buffer )
68 if (screen_buffer->console->active == screen_buffer)
69 screen_buffer->console->active = NULL;
70 wine_rb_remove( &screen_buffer_map, &screen_buffer->entry );
71 free( screen_buffer->font.face_name );
72 free( screen_buffer->data );
73 free( screen_buffer );
76 static struct screen_buffer *create_screen_buffer( struct console *console, int id, int width, int height )
78 struct screen_buffer *screen_buffer;
79 unsigned int i;
81 if (!(screen_buffer = calloc( 1, sizeof(*screen_buffer) ))) return NULL;
82 screen_buffer->console = console;
83 screen_buffer->id = id;
84 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
85 screen_buffer->cursor_size = 25;
86 screen_buffer->cursor_visible = 1;
87 screen_buffer->width = width;
88 screen_buffer->height = height;
90 if (console->active)
92 screen_buffer->max_width = console->active->max_width;
93 screen_buffer->max_height = console->active->max_height;
94 screen_buffer->win.right = console->active->win.right - console->active->win.left;
95 screen_buffer->win.bottom = console->active->win.bottom - console->active->win.top;
96 screen_buffer->attr = console->active->attr;
97 screen_buffer->popup_attr = console->active->attr;
98 screen_buffer->font = console->active->font;
99 memcpy( screen_buffer->color_map, console->active->color_map, sizeof(console->active->color_map) );
101 if (screen_buffer->font.face_len)
103 screen_buffer->font.face_name = malloc( screen_buffer->font.face_len * sizeof(WCHAR) );
104 if (!screen_buffer->font.face_name)
106 free( screen_buffer );
107 return NULL;
110 memcpy( screen_buffer->font.face_name, console->active->font.face_name,
111 screen_buffer->font.face_len * sizeof(WCHAR) );
114 else
116 screen_buffer->max_width = width;
117 screen_buffer->max_height = height;
118 screen_buffer->win.right = width - 1;
119 screen_buffer->win.bottom = height - 1;
120 screen_buffer->attr = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED;
121 screen_buffer->popup_attr = 0xf5;
122 screen_buffer->font.weight = FW_NORMAL;
123 screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
126 if (wine_rb_put( &screen_buffer_map, LongToPtr(id), &screen_buffer->entry ))
128 free( screen_buffer );
129 ERR( "id %x already exists\n", id );
130 return NULL;
133 if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
134 sizeof(*screen_buffer->data) )))
136 destroy_screen_buffer( screen_buffer );
137 return NULL;
140 /* clear the first row */
141 for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
142 /* and copy it to all other rows */
143 for (i = 1; i < screen_buffer->height; i++)
144 memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
145 screen_buffer->width * sizeof(char_info_t) );
147 return screen_buffer;
150 static BOOL is_active( struct screen_buffer *screen_buffer )
152 return screen_buffer == screen_buffer->console->active;
155 static unsigned int get_tty_cp( struct console *console )
157 return console->is_unix ? CP_UNIXCP : CP_UTF8;
160 static void tty_flush( struct console *console )
162 if (!console->tty_output || !console->tty_buffer_count) return;
163 TRACE("%s\n", debugstr_an(console->tty_buffer, console->tty_buffer_count));
164 if (!WriteFile( console->tty_output, console->tty_buffer, console->tty_buffer_count,
165 NULL, NULL ))
166 WARN( "write failed: %lu\n", GetLastError() );
167 console->tty_buffer_count = 0;
170 static void tty_write( struct console *console, const char *buffer, size_t size )
172 if (!size || !console->tty_output) return;
173 if (console->tty_buffer_count + size > sizeof(console->tty_buffer))
174 tty_flush( console );
175 if (console->tty_buffer_count + size <= sizeof(console->tty_buffer))
177 memcpy( console->tty_buffer + console->tty_buffer_count, buffer, size );
178 console->tty_buffer_count += size;
180 else
182 assert( !console->tty_buffer_count );
183 if (!WriteFile( console->tty_output, buffer, size, NULL, NULL ))
184 WARN( "write failed: %lu\n", GetLastError() );
188 static void *tty_alloc_buffer( struct console *console, size_t size )
190 void *ret;
191 if (console->tty_buffer_count + size > sizeof(console->tty_buffer)) return NULL;
192 ret = console->tty_buffer + console->tty_buffer_count;
193 console->tty_buffer_count += size;
194 return ret;
197 static void hide_tty_cursor( struct console *console )
199 if (console->tty_cursor_visible)
201 tty_write( console, "\x1b[?25l", 6 );
202 console->tty_cursor_visible = FALSE;
206 static void set_tty_cursor( struct console *console, unsigned int x, unsigned int y )
208 char buf[64];
210 if (console->tty_cursor_x == x && console->tty_cursor_y == y) return;
212 if (!x && y == console->tty_cursor_y + 1) strcpy( buf, "\r\n" );
213 else if (!x && y == console->tty_cursor_y) strcpy( buf, "\r" );
214 else if (y == console->tty_cursor_y)
216 if (console->tty_cursor_x >= console->active->width)
218 if (console->is_unix)
220 /* Unix will usually have the cursor at width-1 in this case. instead of depending
221 * on the exact behaviour, move the cursor to the first column and move forward
222 * from there. */
223 tty_write( console, "\r", 1 );
224 console->tty_cursor_x = 0;
226 else if (console->active->mode & ENABLE_WRAP_AT_EOL_OUTPUT)
228 console->tty_cursor_x--;
230 if (console->tty_cursor_x == x) return;
232 if (x + 1 == console->tty_cursor_x) strcpy( buf, "\b" );
233 else if (x > console->tty_cursor_x) sprintf( buf, "\x1b[%uC", x - console->tty_cursor_x );
234 else sprintf( buf, "\x1b[%uD", console->tty_cursor_x - x );
236 else if (x || y)
238 hide_tty_cursor( console );
239 sprintf( buf, "\x1b[%u;%uH", y + 1, x + 1);
241 else strcpy( buf, "\x1b[H" );
242 console->tty_cursor_x = x;
243 console->tty_cursor_y = y;
244 tty_write( console, buf, strlen(buf) );
247 static void set_tty_cursor_relative( struct console *console, unsigned int x, unsigned int y )
249 if (y < console->tty_cursor_y)
251 char buf[64];
252 sprintf( buf, "\x1b[%uA", console->tty_cursor_y - y );
253 tty_write( console, buf, strlen(buf) );
254 console->tty_cursor_y = y;
256 else
258 while (console->tty_cursor_y < y)
260 console->tty_cursor_x = 0;
261 console->tty_cursor_y++;
262 tty_write( console, "\r\n", 2 );
265 set_tty_cursor( console, x, y );
268 static void set_tty_attr( struct console *console, unsigned int attr )
270 char buf[8];
272 if ((attr & 0x0f) != (console->tty_attr & 0x0f))
274 if ((attr & 0x0f) != 7)
276 unsigned int n = 30;
277 if (attr & FOREGROUND_BLUE) n += 4;
278 if (attr & FOREGROUND_GREEN) n += 2;
279 if (attr & FOREGROUND_RED) n += 1;
280 if (attr & FOREGROUND_INTENSITY) n += 60;
281 sprintf(buf, "\x1b[%um", n);
282 tty_write( console, buf, strlen(buf) );
284 else tty_write( console, "\x1b[m", 3 );
287 if ((attr & 0xf0) != (console->tty_attr & 0xf0) && attr != 7)
289 unsigned int n = 40;
290 if (attr & BACKGROUND_BLUE) n += 4;
291 if (attr & BACKGROUND_GREEN) n += 2;
292 if (attr & BACKGROUND_RED) n += 1;
293 if (attr & BACKGROUND_INTENSITY) n += 60;
294 sprintf(buf, "\x1b[%um", n);
295 tty_write( console, buf, strlen(buf) );
298 console->tty_attr = attr;
301 static void tty_sync( struct console *console )
303 if (!console->tty_output) return;
305 if (console->active->cursor_visible)
307 set_tty_cursor( console, get_bounded_cursor_x( console->active ), console->active->cursor_y );
308 if (!console->tty_cursor_visible)
310 tty_write( console, "\x1b[?25h", 6 ); /* show cursor */
311 console->tty_cursor_visible = TRUE;
314 else if (console->tty_cursor_visible)
315 hide_tty_cursor( console );
316 tty_flush( console );
319 static void init_tty_output( struct console *console )
321 if (!console->is_unix)
323 /* initialize tty output, but don't flush */
324 tty_write( console, "\x1b[2J", 4 ); /* clear screen */
325 set_tty_attr( console, console->active->attr );
326 tty_write( console, "\x1b[H", 3 ); /* move cursor to (0,0) */
328 else console->tty_attr = empty_char_info.attr;
329 console->tty_cursor_visible = TRUE;
332 /* no longer use relative cursor positioning (legacy API have been used) */
333 static void enter_absolute_mode( struct console *console )
335 console->use_relative_cursor = 0;
338 static void scroll_to_cursor( struct screen_buffer *screen_buffer )
340 unsigned int cursor_x = get_bounded_cursor_x( screen_buffer );
341 int w = screen_buffer->win.right - screen_buffer->win.left + 1;
342 int h = screen_buffer->win.bottom - screen_buffer->win.top + 1;
344 if (cursor_x < screen_buffer->win.left)
345 screen_buffer->win.left = min( cursor_x, screen_buffer->width - w );
346 else if (cursor_x > screen_buffer->win.right)
347 screen_buffer->win.left = max( cursor_x, w ) - w + 1;
348 screen_buffer->win.right = screen_buffer->win.left + w - 1;
350 if (screen_buffer->cursor_y < screen_buffer->win.top)
351 screen_buffer->win.top = min( screen_buffer->cursor_y, screen_buffer->height - h );
352 else if (screen_buffer->cursor_y > screen_buffer->win.bottom)
353 screen_buffer->win.top = max( screen_buffer->cursor_y, h ) - h + 1;
354 screen_buffer->win.bottom = screen_buffer->win.top + h - 1;
357 static void update_output( struct screen_buffer *screen_buffer, RECT *rect )
359 int x, y, size, trailing_spaces;
360 char_info_t *ch;
361 char buf[8];
362 WCHAR wch;
363 const unsigned int mask = (1u << '\0') | (1u << '\b') | (1u << '\t') | (1u << '\n') | (1u << '\a') | (1u << '\r');
365 if (!is_active( screen_buffer ) || rect->top > rect->bottom || rect->right < rect->left)
366 return;
368 TRACE( "%s\n", wine_dbgstr_rect( rect ));
370 if (screen_buffer->console->window)
372 update_window_region( screen_buffer->console, rect );
373 return;
375 if (!screen_buffer->console->tty_output) return;
377 hide_tty_cursor( screen_buffer->console );
379 for (y = rect->top; y <= rect->bottom; y++)
381 for (trailing_spaces = 0; trailing_spaces < screen_buffer->width; trailing_spaces++)
383 ch = &screen_buffer->data[(y + 1) * screen_buffer->width - trailing_spaces - 1];
384 if (ch->ch != ' ' || ch->attr != 7) break;
386 if (trailing_spaces < 4) trailing_spaces = 0;
388 for (x = rect->left; x <= rect->right; x++)
390 ch = &screen_buffer->data[y * screen_buffer->width + x];
391 set_tty_attr( screen_buffer->console, ch->attr );
392 set_tty_cursor( screen_buffer->console, x, y );
394 if (x + trailing_spaces >= screen_buffer->width)
396 tty_write( screen_buffer->console, "\x1b[K", 3 );
397 break;
399 wch = ch->ch;
400 if (screen_buffer->console->is_unix && wch < L' ' && mask & (1u << wch))
401 wch = L'?';
402 size = WideCharToMultiByte( get_tty_cp( screen_buffer->console ), 0,
403 &wch, 1, buf, sizeof(buf), NULL, NULL );
404 tty_write( screen_buffer->console, buf, size );
405 screen_buffer->console->tty_cursor_x++;
409 empty_update_rect( screen_buffer, rect );
412 static void new_line( struct screen_buffer *screen_buffer, RECT *update_rect )
414 unsigned int i;
416 assert( screen_buffer->cursor_y >= screen_buffer->height );
417 screen_buffer->cursor_y = screen_buffer->height - 1;
419 if (screen_buffer->console->tty_output)
420 update_output( screen_buffer, update_rect );
421 else
422 SetRect( update_rect, 0, 0, screen_buffer->width - 1, screen_buffer->height - 1 );
424 memmove( screen_buffer->data, screen_buffer->data + screen_buffer->width,
425 screen_buffer->width * (screen_buffer->height - 1) * sizeof(*screen_buffer->data) );
426 for (i = 0; i < screen_buffer->width; i++)
427 screen_buffer->data[screen_buffer->width * (screen_buffer->height - 1) + i] = empty_char_info;
428 if (is_active( screen_buffer ))
430 screen_buffer->console->tty_cursor_y--;
431 if (screen_buffer->console->tty_cursor_y != screen_buffer->height - 2)
432 set_tty_cursor( screen_buffer->console, 0, screen_buffer->height - 2 );
433 set_tty_cursor( screen_buffer->console, 0, screen_buffer->height - 1 );
437 static void write_char( struct screen_buffer *screen_buffer, WCHAR ch, RECT *update_rect, unsigned int *home_y )
439 if (screen_buffer->cursor_x == screen_buffer->width)
441 screen_buffer->cursor_x = 0;
442 screen_buffer->cursor_y++;
444 if (screen_buffer->cursor_y == screen_buffer->height)
446 if (home_y)
448 if (!*home_y) return;
449 (*home_y)--;
451 new_line( screen_buffer, update_rect );
454 screen_buffer->data[screen_buffer->cursor_y * screen_buffer->width + screen_buffer->cursor_x].ch = ch;
455 screen_buffer->data[screen_buffer->cursor_y * screen_buffer->width + screen_buffer->cursor_x].attr = screen_buffer->attr;
456 update_rect->left = min( update_rect->left, screen_buffer->cursor_x );
457 update_rect->top = min( update_rect->top, screen_buffer->cursor_y );
458 update_rect->right = max( update_rect->right, screen_buffer->cursor_x );
459 update_rect->bottom = max( update_rect->bottom, screen_buffer->cursor_y );
460 screen_buffer->cursor_x++;
463 static NTSTATUS read_complete( struct console *console, NTSTATUS status, const void *buf, size_t size, int signal )
465 SERVER_START_REQ( get_next_console_request )
467 req->handle = wine_server_obj_handle( console->server );
468 req->signal = signal;
469 req->read = 1;
470 req->status = status;
471 if (console->read_ioctl == IOCTL_CONDRV_READ_CONSOLE_CONTROL)
472 wine_server_add_data( req, &console->key_state, sizeof(console->key_state) );
473 wine_server_add_data( req, buf, size );
474 status = wine_server_call( req );
476 SERVER_END_REQ;
477 if (status && (console->read_ioctl || status != STATUS_INVALID_HANDLE)) ERR( "failed: %#lx\n", status );
478 console->signaled = signal;
479 console->read_ioctl = 0;
480 console->pending_read = 0;
481 return status;
484 static NTSTATUS read_console_input( struct console *console, size_t out_size )
486 size_t count = min( out_size / sizeof(INPUT_RECORD), console->record_count );
488 TRACE("count %Iu\n", count);
490 read_complete( console, STATUS_SUCCESS, console->records, count * sizeof(*console->records),
491 console->record_count > count );
493 if (count < console->record_count)
494 memmove( console->records, console->records + count,
495 (console->record_count - count) * sizeof(*console->records) );
496 console->record_count -= count;
497 return STATUS_SUCCESS;
500 static void read_from_buffer( struct console *console, size_t out_size )
502 size_t len, read_len = 0;
503 char *buf = NULL;
505 switch( console->read_ioctl )
507 case IOCTL_CONDRV_READ_CONSOLE:
508 case IOCTL_CONDRV_READ_CONSOLE_CONTROL:
509 out_size = min( out_size, console->read_buffer_count * sizeof(WCHAR) );
510 read_complete( console, STATUS_SUCCESS, console->read_buffer, out_size, console->record_count != 0 );
511 read_len = out_size / sizeof(WCHAR);
512 break;
513 case IOCTL_CONDRV_READ_FILE:
514 read_len = len = 0;
515 while (read_len < console->read_buffer_count && len < out_size)
517 len += WideCharToMultiByte( console->input_cp, 0, console->read_buffer + read_len, 1, NULL, 0, NULL, NULL );
518 read_len++;
520 if (len)
522 if (!(buf = malloc( len )))
524 read_complete( console, STATUS_NO_MEMORY, NULL, 0, console->record_count != 0 );
525 return;
527 WideCharToMultiByte( console->input_cp, 0, console->read_buffer, read_len, buf, len, NULL, NULL );
529 len = min( out_size, len );
530 read_complete( console, STATUS_SUCCESS, buf, len, console->record_count != 0 );
531 free( buf );
532 break;
535 if (read_len < console->read_buffer_count)
537 memmove( console->read_buffer, console->read_buffer + read_len,
538 (console->read_buffer_count - read_len) * sizeof(WCHAR) );
540 if (!(console->read_buffer_count -= read_len))
541 free( console->read_buffer );
544 static void append_input_history( struct console *console, const WCHAR *str, size_t len )
546 struct history_line *ptr;
548 if (!console->history_size) return;
550 /* don't duplicate entry */
551 if (console->history_mode && console->history_index &&
552 console->history[console->history_index - 1]->len == len &&
553 !memcmp( console->history[console->history_index - 1]->text, str, len ))
554 return;
556 if (!(ptr = malloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
557 ptr->len = len;
558 memcpy( ptr->text, str, len );
560 if (console->history_index < console->history_size)
562 console->history[console->history_index++] = ptr;
564 else
566 free( console->history[0]) ;
567 memmove( &console->history[0], &console->history[1],
568 (console->history_size - 1) * sizeof(*console->history) );
569 console->history[console->history_size - 1] = ptr;
573 static void edit_line_update( struct console *console, unsigned int begin, unsigned int length )
575 struct edit_line *ctx = &console->edit_line;
576 if (!length) return;
577 ctx->update_begin = min( ctx->update_begin, begin );
578 ctx->update_end = max( ctx->update_end, begin + length - 1 );
581 static BOOL edit_line_grow( struct console *console, size_t length )
583 struct edit_line *ctx = &console->edit_line;
584 WCHAR *new_buf;
585 size_t new_size;
587 if (ctx->len + length < ctx->size) return TRUE;
589 /* round up size to 32 byte-WCHAR boundary */
590 new_size = (ctx->len + length + 32) & ~31;
591 if (!(new_buf = realloc( ctx->buf, sizeof(WCHAR) * new_size )))
593 ctx->status = STATUS_NO_MEMORY;
594 return FALSE;
596 ctx->buf = new_buf;
597 ctx->size = new_size;
598 return TRUE;
601 static void edit_line_delete( struct console *console, int begin, int end )
603 struct edit_line *ctx = &console->edit_line;
604 unsigned int len = end - begin;
606 edit_line_update( console, begin, ctx->len - begin );
607 if (end < ctx->len)
608 memmove( &ctx->buf[begin], &ctx->buf[end], (ctx->len - end) * sizeof(WCHAR));
609 ctx->len -= len;
610 edit_line_update( console, 0, ctx->len );
611 ctx->buf[ctx->len] = 0;
614 static void edit_line_insert( struct console *console, const WCHAR *str, unsigned int len )
616 struct edit_line *ctx = &console->edit_line;
617 unsigned int update_len;
619 if (!len) return;
620 if (ctx->insert_mode)
622 if (!edit_line_grow( console, len )) return;
623 if (ctx->len > ctx->cursor)
624 memmove( &ctx->buf[ctx->cursor + len], &ctx->buf[ctx->cursor],
625 (ctx->len - ctx->cursor) * sizeof(WCHAR) );
626 ctx->len += len;
627 update_len = ctx->len - ctx->cursor;
629 else
631 if (ctx->cursor + len > ctx->len)
633 if (!edit_line_grow( console, (ctx->cursor + len) - ctx->len) )
634 return;
635 ctx->len = ctx->cursor + len;
637 update_len = len;
639 memcpy( &ctx->buf[ctx->cursor], str, len * sizeof(WCHAR) );
640 ctx->buf[ctx->len] = 0;
641 edit_line_update( console, ctx->cursor, update_len );
642 ctx->cursor += len;
645 static void edit_line_save_yank( struct console *console, unsigned int begin, unsigned int end )
647 struct edit_line *ctx = &console->edit_line;
648 unsigned int len = end - begin;
649 if (len <= 0) return;
651 free(ctx->yanked);
652 ctx->yanked = malloc( (len + 1) * sizeof(WCHAR) );
653 if (!ctx->yanked)
655 ctx->status = STATUS_NO_MEMORY;
656 return;
658 memcpy( ctx->yanked, &ctx->buf[begin], len * sizeof(WCHAR) );
659 ctx->yanked[len] = 0;
662 static int edit_line_left_word_transition( struct console *console, int offset )
664 offset--;
665 while (offset >= 0 && !iswalnum( console->edit_line.buf[offset] )) offset--;
666 while (offset >= 0 && iswalnum( console->edit_line.buf[offset] )) offset--;
667 if (offset >= 0) offset++;
668 return max( offset, 0 );
671 static int edit_line_right_word_transition( struct console *console, int offset )
673 offset++;
674 while (offset <= console->edit_line.len && iswalnum( console->edit_line.buf[offset] ))
675 offset++;
676 while (offset <= console->edit_line.len && !iswalnum( console->edit_line.buf[offset] ))
677 offset++;
678 return min(offset, console->edit_line.len);
681 static WCHAR *edit_line_history( struct console *console, unsigned int index )
683 WCHAR *ptr = NULL;
685 if (index < console->history_index)
687 if ((ptr = malloc( console->history[index]->len + sizeof(WCHAR) )))
689 memcpy( ptr, console->history[index]->text, console->history[index]->len );
690 ptr[console->history[index]->len / sizeof(WCHAR)] = 0;
693 else if(console->edit_line.current_history)
695 if ((ptr = malloc( (lstrlenW(console->edit_line.current_history) + 1) * sizeof(WCHAR) )))
696 lstrcpyW( ptr, console->edit_line.current_history );
698 return ptr;
701 static void edit_line_move_to_history( struct console *console, int index )
703 struct edit_line *ctx = &console->edit_line;
704 WCHAR *line = edit_line_history(console, index);
705 size_t len = line ? lstrlenW(line) : 0;
707 /* save current line edition for recall when needed */
708 if (ctx->history_index == console->history_index)
710 free( ctx->current_history );
711 ctx->current_history = malloc( (ctx->len + 1) * sizeof(WCHAR) );
712 if (ctx->current_history)
714 memcpy( ctx->current_history, ctx->buf, (ctx->len + 1) * sizeof(WCHAR) );
716 else
718 ctx->status = STATUS_NO_MEMORY;
719 return;
723 /* need to clean also the screen if new string is shorter than old one */
724 edit_line_delete(console, 0, ctx->len);
725 ctx->cursor = 0;
726 /* insert new string */
727 if (edit_line_grow(console, len + 1))
729 edit_line_insert( console, line, len );
730 ctx->history_index = index;
732 free(line);
735 static void edit_line_find_in_history( struct console *console )
737 struct edit_line *ctx = &console->edit_line;
738 int start_pos = ctx->history_index;
739 unsigned int len, oldoffset;
740 WCHAR *line;
742 if (!console->history_index) return;
743 if (ctx->history_index && ctx->history_index == console->history_index)
745 start_pos--;
746 ctx->history_index--;
751 line = edit_line_history(console, ctx->history_index);
753 if (ctx->history_index) ctx->history_index--;
754 else ctx->history_index = console->history_index - 1;
756 len = lstrlenW(line) + 1;
757 if (len >= ctx->cursor && !memcmp( ctx->buf, line, ctx->cursor * sizeof(WCHAR) ))
759 /* need to clean also the screen if new string is shorter than old one */
760 edit_line_delete(console, 0, ctx->len);
762 if (edit_line_grow(console, len))
764 oldoffset = ctx->cursor;
765 ctx->cursor = 0;
766 edit_line_insert( console, line, len - 1 );
767 ctx->cursor = oldoffset;
768 free(line);
769 return;
772 free(line);
774 while (ctx->history_index != start_pos);
777 static void edit_line_move_left( struct console *console )
779 if (console->edit_line.cursor > 0) console->edit_line.cursor--;
782 static void edit_line_move_right( struct console *console )
784 struct edit_line *ctx = &console->edit_line;
785 if (ctx->cursor < ctx->len) ctx->cursor++;
788 static void edit_line_move_left_word( struct console *console )
790 console->edit_line.cursor = edit_line_left_word_transition( console, console->edit_line.cursor );
793 static void edit_line_move_right_word( struct console *console )
795 console->edit_line.cursor = edit_line_right_word_transition( console, console->edit_line.cursor );
798 static void edit_line_move_home( struct console *console )
800 console->edit_line.cursor = 0;
803 static void edit_line_move_end( struct console *console )
805 console->edit_line.cursor = console->edit_line.len;
808 static void edit_line_set_mark( struct console *console )
810 console->edit_line.mark = console->edit_line.cursor;
813 static void edit_line_exchange_mark( struct console *console )
815 struct edit_line *ctx = &console->edit_line;
816 unsigned int cursor;
818 if (ctx->mark > ctx->len) return;
819 cursor = ctx->cursor;
820 ctx->cursor = ctx->mark;
821 ctx->mark = cursor;
824 static void edit_line_copy_marked_zone( struct console *console )
826 struct edit_line *ctx = &console->edit_line;
827 unsigned int begin, end;
829 if (ctx->mark > ctx->len || ctx->mark == ctx->cursor) return;
830 if (ctx->mark > ctx->cursor)
832 begin = ctx->cursor;
833 end = ctx->mark;
835 else
837 begin = ctx->mark;
838 end = ctx->cursor;
840 edit_line_save_yank( console, begin, end );
843 static void edit_line_transpose_char( struct console *console )
845 struct edit_line *ctx = &console->edit_line;
846 WCHAR c;
848 if (!ctx->cursor || ctx->cursor == ctx->len) return;
850 c = ctx->buf[ctx->cursor];
851 ctx->buf[ctx->cursor] = ctx->buf[ctx->cursor - 1];
852 ctx->buf[ctx->cursor - 1] = c;
854 edit_line_update( console, ctx->cursor - 1, 2 );
855 ctx->cursor++;
858 static void edit_line_transpose_words( struct console *console )
860 struct edit_line *ctx = &console->edit_line;
861 unsigned int left_offset = edit_line_left_word_transition( console, ctx->cursor );
862 unsigned int right_offset = edit_line_right_word_transition( console, ctx->cursor );
863 if (left_offset < ctx->cursor && right_offset > ctx->cursor)
865 unsigned int len_r = right_offset - ctx->cursor;
866 unsigned int len_l = ctx->cursor - left_offset;
867 char *tmp = malloc( len_r * sizeof(WCHAR) );
868 if (!tmp)
870 ctx->status = STATUS_NO_MEMORY;
871 return;
874 memcpy( tmp, &ctx->buf[ctx->cursor], len_r * sizeof(WCHAR) );
875 memmove( &ctx->buf[left_offset + len_r], &ctx->buf[left_offset],
876 len_l * sizeof(WCHAR) );
877 memcpy( &ctx->buf[left_offset], tmp, len_r * sizeof(WCHAR) );
878 free(tmp);
880 edit_line_update( console, left_offset, len_l + len_r );
881 ctx->cursor = right_offset;
885 static void edit_line_lower_case_word( struct console *console )
887 struct edit_line *ctx = &console->edit_line;
888 unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
889 if (new_offset != ctx->cursor)
891 CharLowerBuffW( ctx->buf + ctx->cursor, new_offset - ctx->cursor + 1 );
892 edit_line_update( console, ctx->cursor, new_offset - ctx->cursor + 1 );
893 ctx->cursor = new_offset;
897 static void edit_line_upper_case_word( struct console *console )
899 struct edit_line *ctx = &console->edit_line;
900 unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
901 if (new_offset != ctx->cursor)
903 CharUpperBuffW( ctx->buf + ctx->cursor, new_offset - ctx->cursor + 1 );
904 edit_line_update( console, ctx->cursor, new_offset - ctx->cursor + 1 );
905 ctx->cursor = new_offset;
909 static void edit_line_capitalize_word( struct console *console )
911 struct edit_line *ctx = &console->edit_line;
912 unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
913 if (new_offset != ctx->cursor)
915 CharUpperBuffW( ctx->buf + ctx->cursor, 1 );
916 CharLowerBuffW( ctx->buf + ctx->cursor + 1, new_offset - ctx->cursor );
917 edit_line_update( console, ctx->cursor, new_offset - ctx->cursor + 1 );
918 ctx->cursor = new_offset;
922 static void edit_line_yank( struct console *console )
924 struct edit_line *ctx = &console->edit_line;
925 if (ctx->yanked) edit_line_insert( console, ctx->yanked, wcslen(ctx->yanked) );
928 static void edit_line_kill_suffix( struct console *console )
930 struct edit_line *ctx = &console->edit_line;
931 edit_line_save_yank( console, ctx->cursor, ctx->len );
932 edit_line_delete( console, ctx->cursor, ctx->len );
935 static void edit_line_kill_prefix( struct console *console )
937 struct edit_line *ctx = &console->edit_line;
938 if (ctx->cursor)
940 edit_line_save_yank( console, 0, ctx->cursor );
941 edit_line_delete( console, 0, ctx->cursor );
942 ctx->cursor = 0;
946 static void edit_line_kill_marked_zone( struct console *console )
948 struct edit_line *ctx = &console->edit_line;
949 unsigned int begin, end;
951 if (ctx->mark > ctx->len || ctx->mark == ctx->cursor)
952 return;
953 if (ctx->mark > ctx->cursor)
955 begin = ctx->cursor;
956 end = ctx->mark;
958 else
960 begin = ctx->mark;
961 end = ctx->cursor;
963 edit_line_save_yank( console, begin, end );
964 edit_line_delete( console, begin, end );
965 ctx->cursor = begin;
968 static void edit_line_delete_prev( struct console *console )
970 struct edit_line *ctx = &console->edit_line;
971 if (ctx->cursor)
973 edit_line_delete( console, ctx->cursor - 1, ctx->cursor );
974 ctx->cursor--;
978 static void edit_line_delete_char( struct console *console )
980 struct edit_line *ctx = &console->edit_line;
981 if (ctx->cursor < ctx->len)
982 edit_line_delete( console, ctx->cursor, ctx->cursor + 1 );
985 static void edit_line_delete_left_word( struct console *console )
987 struct edit_line *ctx = &console->edit_line;
988 unsigned int new_offset = edit_line_left_word_transition( console, ctx->cursor );
989 if (new_offset != ctx->cursor)
991 edit_line_delete( console, new_offset, ctx->cursor );
992 ctx->cursor = new_offset;
996 static void edit_line_delete_right_word( struct console *console )
998 struct edit_line *ctx = &console->edit_line;
999 unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
1000 if (new_offset != ctx->cursor)
1002 edit_line_delete( console, ctx->cursor, new_offset );
1006 static void edit_line_move_to_prev_hist( struct console *console )
1008 if (console->edit_line.history_index)
1009 edit_line_move_to_history( console, console->edit_line.history_index - 1 );
1012 static void edit_line_move_to_next_hist( struct console *console )
1014 if (console->edit_line.history_index < console->history_index)
1015 edit_line_move_to_history( console, console->edit_line.history_index + 1 );
1018 static void edit_line_move_to_first_hist( struct console *console )
1020 if (console->edit_line.history_index)
1021 edit_line_move_to_history( console, 0 );
1024 static void edit_line_move_to_last_hist( struct console *console )
1026 if (console->edit_line.history_index != console->history_index)
1027 edit_line_move_to_history( console, console->history_index );
1030 static void edit_line_redraw( struct console *console )
1032 if (console->mode & ENABLE_ECHO_INPUT)
1033 edit_line_update( console, 0, console->edit_line.len );
1036 static void edit_line_toggle_insert( struct console *console )
1038 struct edit_line *ctx = &console->edit_line;
1039 ctx->insert_key = !ctx->insert_key;
1040 console->active->cursor_size = ctx->insert_key ? 100 : 25;
1043 static void edit_line_done( struct console *console )
1045 console->edit_line.status = STATUS_SUCCESS;
1048 struct edit_line_key_entry
1050 WCHAR val; /* vk or unicode char */
1051 void (*func)( struct console *console );
1054 struct edit_line_key_map
1056 DWORD key_state; /* keyState (from INPUT_RECORD) to match */
1057 BOOL is_char; /* check vk or char */
1058 const struct edit_line_key_entry *entries;
1061 #define CTRL(x) ((x) - '@')
1062 static const struct edit_line_key_entry std_key_map[] =
1064 { VK_BACK, edit_line_delete_prev },
1065 { VK_RETURN, edit_line_done },
1066 { VK_DELETE, edit_line_delete_char },
1067 { 0 }
1070 static const struct edit_line_key_entry emacs_key_map_ctrl[] =
1072 { CTRL('@'), edit_line_set_mark },
1073 { CTRL('A'), edit_line_move_home },
1074 { CTRL('B'), edit_line_move_left },
1075 { CTRL('D'), edit_line_delete_char },
1076 { CTRL('E'), edit_line_move_end },
1077 { CTRL('F'), edit_line_move_right },
1078 { CTRL('H'), edit_line_delete_prev },
1079 { CTRL('J'), edit_line_done },
1080 { CTRL('K'), edit_line_kill_suffix },
1081 { CTRL('L'), edit_line_redraw },
1082 { CTRL('M'), edit_line_done },
1083 { CTRL('N'), edit_line_move_to_next_hist },
1084 { CTRL('P'), edit_line_move_to_prev_hist },
1085 { CTRL('T'), edit_line_transpose_char },
1086 { CTRL('W'), edit_line_kill_marked_zone },
1087 { CTRL('X'), edit_line_exchange_mark },
1088 { CTRL('Y'), edit_line_yank },
1089 { 0 }
1092 static const struct edit_line_key_entry emacs_key_map_alt[] =
1094 { 0x7f, edit_line_delete_left_word },
1095 { '<', edit_line_move_to_first_hist },
1096 { '>', edit_line_move_to_last_hist },
1097 { 'b', edit_line_move_left_word },
1098 { 'c', edit_line_capitalize_word },
1099 { 'd', edit_line_delete_right_word },
1100 { 'f', edit_line_move_right_word },
1101 { 'l', edit_line_lower_case_word },
1102 { 't', edit_line_transpose_words },
1103 { 'u', edit_line_upper_case_word },
1104 { 'w', edit_line_copy_marked_zone },
1105 { 0 }
1108 static const struct edit_line_key_entry emacs_std_key_map[] =
1110 { VK_PRIOR, edit_line_move_to_prev_hist },
1111 { VK_NEXT, edit_line_move_to_next_hist },
1112 { VK_END, edit_line_move_end },
1113 { VK_HOME, edit_line_move_home },
1114 { VK_RIGHT, edit_line_move_right },
1115 { VK_LEFT, edit_line_move_left },
1116 { VK_INSERT, edit_line_toggle_insert },
1117 { 0 }
1120 static const struct edit_line_key_map emacs_key_map[] =
1122 { 0, 0, std_key_map },
1123 { 0, 0, emacs_std_key_map },
1124 { RIGHT_ALT_PRESSED, 1, emacs_key_map_alt },
1125 { LEFT_ALT_PRESSED, 1, emacs_key_map_alt },
1126 { RIGHT_CTRL_PRESSED, 1, emacs_key_map_ctrl },
1127 { LEFT_CTRL_PRESSED, 1, emacs_key_map_ctrl },
1128 { 0 }
1131 static const struct edit_line_key_entry win32_std_key_map[] =
1133 { VK_LEFT, edit_line_move_left },
1134 { VK_RIGHT, edit_line_move_right },
1135 { VK_HOME, edit_line_move_home },
1136 { VK_END, edit_line_move_end },
1137 { VK_UP, edit_line_move_to_prev_hist },
1138 { VK_DOWN, edit_line_move_to_next_hist },
1139 { VK_INSERT, edit_line_toggle_insert },
1140 { VK_F8, edit_line_find_in_history },
1141 { 0 }
1144 static const struct edit_line_key_entry win32_key_map_ctrl[] =
1146 { VK_LEFT, edit_line_move_left_word },
1147 { VK_RIGHT, edit_line_move_right_word },
1148 { VK_END, edit_line_kill_suffix },
1149 { VK_HOME, edit_line_kill_prefix },
1150 { 'M', edit_line_done },
1151 { 0 }
1154 static const struct edit_line_key_map win32_key_map[] =
1156 { 0, 0, std_key_map },
1157 { SHIFT_PRESSED, 0, std_key_map },
1158 { 0, 0, win32_std_key_map },
1159 { RIGHT_CTRL_PRESSED, 0, win32_key_map_ctrl },
1160 { LEFT_CTRL_PRESSED, 0, win32_key_map_ctrl },
1161 { 0 }
1163 #undef CTRL
1165 static unsigned int edit_line_string_width( const WCHAR *str, unsigned int len)
1167 unsigned int i, offset = 0;
1168 for (i = 0; i < len; i++) offset += str[i] < ' ' ? 2 : 1;
1169 return offset;
1172 static void update_read_output( struct console *console, BOOL newline )
1174 struct screen_buffer *screen_buffer = console->active;
1175 struct edit_line *ctx = &console->edit_line;
1176 int offset = 0, j, end_offset;
1177 RECT update_rect;
1179 empty_update_rect( screen_buffer, &update_rect );
1181 if (ctx->update_end >= ctx->update_begin)
1183 TRACE( "update %d-%d %s\n", ctx->update_begin, ctx->update_end,
1184 debugstr_wn( ctx->buf + ctx->update_begin, ctx->update_end - ctx->update_begin + 1 ));
1186 hide_tty_cursor( screen_buffer->console );
1188 offset = edit_line_string_width( ctx->buf, ctx->update_begin );
1189 screen_buffer->cursor_x = (ctx->home_x + offset) % screen_buffer->width;
1190 screen_buffer->cursor_y = ctx->home_y + (ctx->home_x + offset) / screen_buffer->width;
1191 for (j = ctx->update_begin; j <= ctx->update_end; j++)
1193 if (screen_buffer->cursor_y >= screen_buffer->height && !ctx->home_y) break;
1194 if (j >= ctx->len) break;
1195 if (ctx->buf[j] < ' ')
1197 write_char( screen_buffer, '^', &update_rect, &ctx->home_y );
1198 write_char( screen_buffer, '@' + ctx->buf[j], &update_rect, &ctx->home_y );
1199 offset += 2;
1201 else
1203 write_char( screen_buffer, ctx->buf[j], &update_rect, &ctx->home_y );
1204 offset++;
1207 end_offset = ctx->end_offset;
1208 ctx->end_offset = offset;
1209 if (j >= ctx->len)
1211 /* clear trailing characters if buffer was shortened */
1212 while (offset < end_offset && screen_buffer->cursor_y < screen_buffer->height)
1214 write_char( screen_buffer, ' ', &update_rect, &ctx->home_y );
1215 offset++;
1220 if (newline)
1222 offset = edit_line_string_width( ctx->buf, ctx->len );
1223 screen_buffer->cursor_x = 0;
1224 screen_buffer->cursor_y = ctx->home_y + (ctx->home_x + offset) / screen_buffer->width;
1225 if (++screen_buffer->cursor_y >= screen_buffer->height)
1226 new_line( screen_buffer, &update_rect );
1228 else
1230 offset = edit_line_string_width( ctx->buf, ctx->cursor );
1231 screen_buffer->cursor_y = ctx->home_y + (ctx->home_x + offset) / screen_buffer->width;
1232 if (screen_buffer->cursor_y < screen_buffer->height)
1234 screen_buffer->cursor_x = (ctx->home_x + offset) % screen_buffer->width;
1236 else
1238 screen_buffer->cursor_x = screen_buffer->width - 1;
1239 screen_buffer->cursor_y = screen_buffer->height - 1;
1243 /* always try to use relative cursor positions in UNIX mode so that it works even if cursor
1244 * position is out of sync */
1245 if (update_rect.left <= update_rect.right && update_rect.top <= update_rect.bottom)
1247 if (console->is_unix)
1248 set_tty_cursor_relative( screen_buffer->console, update_rect.left, update_rect.top );
1249 update_output( screen_buffer, &update_rect );
1250 scroll_to_cursor( screen_buffer );
1252 if (console->is_unix)
1253 set_tty_cursor_relative( screen_buffer->console, screen_buffer->cursor_x, screen_buffer->cursor_y );
1254 tty_sync( screen_buffer->console );
1255 update_window_config( screen_buffer->console, TRUE );
1258 /* can end on any ctrl-character: from 0x00 up to 0x1F) */
1259 #define FIRST_NON_CONTROL_CHAR (L' ')
1261 static NTSTATUS process_console_input( struct console *console )
1263 struct edit_line *ctx = &console->edit_line;
1264 unsigned int i;
1265 WCHAR ctrl_value = FIRST_NON_CONTROL_CHAR;
1266 unsigned int ctrl_keyvalue = 0;
1268 switch (console->read_ioctl)
1270 case IOCTL_CONDRV_READ_INPUT:
1271 if (console->record_count) read_console_input( console, console->pending_read );
1272 return STATUS_SUCCESS;
1273 case IOCTL_CONDRV_READ_CONSOLE:
1274 case IOCTL_CONDRV_READ_CONSOLE_CONTROL:
1275 case IOCTL_CONDRV_READ_FILE:
1276 break;
1277 default:
1278 assert( !console->read_ioctl );
1279 if (console->record_count && !console->signaled)
1280 read_complete( console, STATUS_PENDING, NULL, 0, TRUE ); /* signal server */
1281 return STATUS_SUCCESS;
1284 ctx->update_begin = ctx->len + 1;
1285 ctx->update_end = 0;
1287 for (i = 0; i < console->record_count && ctx->status == STATUS_PENDING; i++)
1289 void (*func)( struct console *console ) = NULL;
1290 INPUT_RECORD ir = console->records[i];
1292 if (ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown) continue;
1294 TRACE( "key code=%02x scan=%02x char=%02x state=%08lx\n",
1295 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
1296 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState );
1298 if (console->mode & ENABLE_LINE_INPUT)
1300 const struct edit_line_key_entry *entry;
1301 const struct edit_line_key_map *map;
1302 unsigned int state;
1304 /* mask out some bits which don't interest us */
1305 state = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
1307 if (ctx->ctrl_mask &&
1308 ir.Event.KeyEvent.uChar.UnicodeChar &&
1309 ir.Event.KeyEvent.uChar.UnicodeChar < FIRST_NON_CONTROL_CHAR)
1311 if (ctx->ctrl_mask & (1u << ir.Event.KeyEvent.uChar.UnicodeChar))
1313 ctrl_value = ir.Event.KeyEvent.uChar.UnicodeChar;
1314 ctrl_keyvalue = ir.Event.KeyEvent.dwControlKeyState;
1315 ctx->status = STATUS_SUCCESS;
1316 TRACE("Found ctrl char in mask: ^%lc %x\n", ir.Event.KeyEvent.uChar.UnicodeChar + '@', ctx->ctrl_mask);
1317 continue;
1319 if (ir.Event.KeyEvent.uChar.UnicodeChar == 10) continue;
1321 func = NULL;
1322 for (map = console->edition_mode ? emacs_key_map : win32_key_map; map->entries != NULL; map++)
1324 if (map->key_state != state)
1325 continue;
1326 if (map->is_char)
1328 for (entry = &map->entries[0]; entry->func != 0; entry++)
1329 if (entry->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
1331 else
1333 for (entry = &map->entries[0]; entry->func != 0; entry++)
1334 if (entry->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
1337 if (entry->func)
1339 func = entry->func;
1340 break;
1345 ctx->insert_mode = ((console->mode & (ENABLE_INSERT_MODE | ENABLE_EXTENDED_FLAGS)) ==
1346 (ENABLE_INSERT_MODE | ENABLE_EXTENDED_FLAGS))
1347 ^ ctx->insert_key;
1349 if (func) func( console );
1350 else if (ir.Event.KeyEvent.uChar.UnicodeChar)
1351 edit_line_insert( console, &ir.Event.KeyEvent.uChar.UnicodeChar, 1 );
1353 if (!(console->mode & ENABLE_LINE_INPUT) && ctx->status == STATUS_PENDING)
1355 if (console->read_ioctl == IOCTL_CONDRV_READ_FILE)
1357 if (WideCharToMultiByte(console->input_cp, 0, ctx->buf, ctx->len, NULL, 0, NULL, NULL)
1358 >= console->pending_read)
1359 ctx->status = STATUS_SUCCESS;
1361 else if (ctx->len >= console->pending_read / sizeof(WCHAR))
1362 ctx->status = STATUS_SUCCESS;
1366 if (console->record_count > i) memmove( console->records, console->records + i,
1367 (console->record_count - i) * sizeof(*console->records) );
1368 console->record_count -= i;
1370 if (ctx->status == STATUS_PENDING && !(console->mode & ENABLE_LINE_INPUT) && ctx->len)
1371 ctx->status = STATUS_SUCCESS;
1373 if (console->mode & ENABLE_ECHO_INPUT) update_read_output( console, !ctx->status && ctrl_value == FIRST_NON_CONTROL_CHAR );
1374 if (ctx->status == STATUS_PENDING) return STATUS_SUCCESS;
1376 if (!ctx->status && (console->mode & ENABLE_LINE_INPUT))
1378 if (ctrl_value < FIRST_NON_CONTROL_CHAR)
1380 edit_line_insert( console, &ctrl_value, 1 );
1381 console->key_state = ctrl_keyvalue;
1383 else
1385 if (ctx->len) append_input_history( console, ctx->buf, ctx->len * sizeof(WCHAR) );
1386 if (edit_line_grow(console, 2))
1388 ctx->buf[ctx->len++] = '\r';
1389 ctx->buf[ctx->len++] = '\n';
1390 ctx->buf[ctx->len] = 0;
1393 TRACE( "return %s\n", debugstr_wn( ctx->buf, ctx->len ));
1396 console->read_buffer = ctx->buf;
1397 console->read_buffer_count = ctx->len;
1398 console->read_buffer_size = ctx->size;
1400 if (ctx->status) read_complete( console, ctx->status, NULL, 0, console->record_count );
1401 else read_from_buffer( console, console->pending_read );
1403 /* reset context */
1404 free( ctx->yanked );
1405 free( ctx->current_history );
1406 memset( &console->edit_line, 0, sizeof(console->edit_line) );
1407 return STATUS_SUCCESS;
1410 static NTSTATUS read_console( struct console *console, unsigned int ioctl, size_t out_size,
1411 const WCHAR *initial, unsigned int initial_len, unsigned int ctrl_mask )
1413 struct edit_line *ctx = &console->edit_line;
1414 TRACE("\n");
1416 if (out_size > INT_MAX)
1418 read_complete( console, STATUS_NO_MEMORY, NULL, 0, console->record_count );
1419 return STATUS_NO_MEMORY;
1422 console->read_ioctl = ioctl;
1423 console->key_state = 0;
1424 if (!out_size || console->read_buffer_count)
1426 read_from_buffer( console, out_size );
1427 return STATUS_SUCCESS;
1430 ctx->history_index = console->history_index;
1431 ctx->home_x = console->active->cursor_x;
1432 ctx->home_y = console->active->cursor_y;
1433 ctx->status = STATUS_PENDING;
1434 if (initial_len && edit_line_grow( console, initial_len + 1 ))
1436 unsigned offset = edit_line_string_width( initial, initial_len );
1437 if (offset > ctx->home_x)
1439 int deltay;
1440 offset -= ctx->home_x + 1;
1441 deltay = offset / console->active->width + 1;
1442 if (ctx->home_y >= deltay)
1443 ctx->home_y -= deltay;
1444 else
1446 ctx->home_y = 0;
1447 FIXME("Support for negative ordinates is missing\n");
1449 ctx->home_x = console->active->width - 1 - (offset % console->active->width);
1451 else
1452 ctx->home_x -= offset;
1453 ctx->cursor = initial_len;
1454 memcpy( ctx->buf, initial, initial_len * sizeof(WCHAR) );
1455 ctx->buf[initial_len] = 0;
1456 ctx->len = initial_len;
1457 ctx->end_offset = initial_len;
1459 else if (edit_line_grow( console, 1 )) ctx->buf[0] = 0;
1460 ctx->ctrl_mask = ctrl_mask;
1462 console->pending_read = out_size;
1463 return process_console_input( console );
1466 static BOOL map_to_ctrlevent( struct console *console, const INPUT_RECORD *record,
1467 unsigned int* event)
1469 if (record->EventType == KEY_EVENT)
1471 if (record->Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
1472 !(record->Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1474 *event = CTRL_C_EVENT;
1475 return TRUE;
1477 /* we want to get ctrl-pause/break, but it's already translated by user32 into VK_CANCEL */
1478 if (record->Event.KeyEvent.uChar.UnicodeChar == 0 &&
1479 record->Event.KeyEvent.wVirtualKeyCode == VK_CANCEL &&
1480 record->Event.KeyEvent.dwControlKeyState == LEFT_CTRL_PRESSED)
1482 *event = CTRL_BREAK_EVENT;
1483 return TRUE;
1486 return FALSE;
1489 /* add input events to a console input queue */
1490 NTSTATUS write_console_input( struct console *console, const INPUT_RECORD *records,
1491 unsigned int count, BOOL flush )
1493 TRACE( "%u\n", count );
1495 if (!count) return STATUS_SUCCESS;
1496 if (console->record_count + count > console->record_size)
1498 INPUT_RECORD *new_rec;
1499 if (!(new_rec = realloc( console->records, (console->record_size * 2 + count) * sizeof(INPUT_RECORD) )))
1500 return STATUS_NO_MEMORY;
1501 console->records = new_rec;
1502 console->record_size = console->record_size * 2 + count;
1504 memcpy( console->records + console->record_count, records, count * sizeof(INPUT_RECORD) );
1506 if (console->mode & ENABLE_PROCESSED_INPUT)
1508 unsigned int i = 0;
1509 while (i < count)
1511 unsigned int event;
1513 if (map_to_ctrlevent(console, &records[i], &event))
1515 if (i != count - 1)
1516 memcpy( &console->records[console->record_count + i],
1517 &console->records[console->record_count + i + 1],
1518 (count - i - 1) * sizeof(INPUT_RECORD) );
1519 count--;
1520 if (records[i].Event.KeyEvent.bKeyDown)
1522 struct condrv_ctrl_event ctrl_event;
1523 IO_STATUS_BLOCK io;
1525 ctrl_event.event = event;
1526 ctrl_event.group_id = 0;
1527 NtDeviceIoControlFile( console->server, NULL, NULL, NULL, &io, IOCTL_CONDRV_CTRL_EVENT,
1528 &ctrl_event, sizeof(ctrl_event), NULL, 0 );
1532 else i++;
1535 console->record_count += count;
1536 return flush ? process_console_input( console ) : STATUS_SUCCESS;
1539 static void set_key_input_record( INPUT_RECORD *record, WCHAR ch, unsigned int vk, BOOL is_down, unsigned int ctrl_state )
1541 record->EventType = KEY_EVENT;
1542 record->Event.KeyEvent.bKeyDown = is_down;
1543 record->Event.KeyEvent.wRepeatCount = 1;
1544 record->Event.KeyEvent.uChar.UnicodeChar = ch;
1545 record->Event.KeyEvent.wVirtualKeyCode = vk;
1546 record->Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW( vk, MAPVK_VK_TO_VSC );
1547 record->Event.KeyEvent.dwControlKeyState = ctrl_state;
1550 static NTSTATUS key_press( struct console *console, WCHAR ch, unsigned int vk, unsigned int ctrl_state )
1552 INPUT_RECORD records[8];
1553 unsigned int count = 0, ctrl = 0;
1555 if (ctrl_state & SHIFT_PRESSED)
1557 ctrl |= SHIFT_PRESSED;
1558 set_key_input_record( &records[count++], 0, VK_SHIFT, TRUE, ctrl );
1560 if (ctrl_state & LEFT_ALT_PRESSED)
1562 ctrl |= LEFT_ALT_PRESSED;
1563 set_key_input_record( &records[count++], 0, VK_MENU, TRUE, ctrl );
1565 if (ctrl_state & LEFT_CTRL_PRESSED)
1567 ctrl |= LEFT_CTRL_PRESSED;
1568 set_key_input_record( &records[count++], 0, VK_CONTROL, TRUE, ctrl );
1571 set_key_input_record( &records[count++], ch, vk, TRUE, ctrl );
1572 set_key_input_record( &records[count++], ch, vk, FALSE, ctrl );
1574 if (ctrl & LEFT_CTRL_PRESSED)
1576 ctrl &= ~LEFT_CTRL_PRESSED;
1577 set_key_input_record( &records[count++], 0, VK_CONTROL, FALSE, ctrl );
1579 if (ctrl & LEFT_ALT_PRESSED)
1581 ctrl &= ~LEFT_ALT_PRESSED;
1582 set_key_input_record( &records[count++], 0, VK_MENU, FALSE, ctrl );
1584 if (ctrl & SHIFT_PRESSED)
1586 ctrl &= ~SHIFT_PRESSED;
1587 set_key_input_record( &records[count++], 0, VK_SHIFT, FALSE, ctrl );
1590 return write_console_input( console, records, count, FALSE );
1593 static void char_key_press( struct console *console, WCHAR ch, unsigned int ctrl )
1595 unsigned int vk = VkKeyScanW( ch );
1596 if (vk == ~0) vk = 0;
1597 if (vk & 0x0100) ctrl |= SHIFT_PRESSED;
1598 if (vk & 0x0200) ctrl |= LEFT_CTRL_PRESSED;
1599 if (vk & 0x0400) ctrl |= LEFT_ALT_PRESSED;
1600 vk &= 0xff;
1601 key_press( console, ch, vk, ctrl );
1604 static unsigned int escape_char_to_vk( WCHAR ch, unsigned int *ctrl, WCHAR *outuch )
1606 if (ctrl) *ctrl = 0;
1607 if (outuch) *outuch = '\0';
1609 switch (ch)
1611 case 'A': return VK_UP;
1612 case 'B': return VK_DOWN;
1613 case 'C': return VK_RIGHT;
1614 case 'D': return VK_LEFT;
1615 case 'H': return VK_HOME;
1616 case 'F': return VK_END;
1617 case 'P': return VK_F1;
1618 case 'Q': return VK_F2;
1619 case 'R': return VK_F3;
1620 case 'S': return VK_F4;
1621 case 'Z': if (ctrl && outuch) {*ctrl = SHIFT_PRESSED; *outuch = '\t'; return VK_TAB;}
1622 return 0;
1623 default: return 0;
1627 static unsigned int escape_number_to_vk( unsigned int n )
1629 switch(n)
1631 case 2: return VK_INSERT;
1632 case 3: return VK_DELETE;
1633 case 5: return VK_PRIOR;
1634 case 6: return VK_NEXT;
1635 case 15: return VK_F5;
1636 case 17: return VK_F6;
1637 case 18: return VK_F7;
1638 case 19: return VK_F8;
1639 case 20: return VK_F9;
1640 case 21: return VK_F10;
1641 case 23: return VK_F11;
1642 case 24: return VK_F12;
1643 default: return 0;
1647 static unsigned int convert_modifiers( unsigned int n )
1649 unsigned int ctrl = 0;
1650 if (!n || n > 16) return 0;
1651 n--;
1652 if (n & 1) ctrl |= SHIFT_PRESSED;
1653 if (n & 2) ctrl |= LEFT_ALT_PRESSED;
1654 if (n & 4) ctrl |= LEFT_CTRL_PRESSED;
1655 return ctrl;
1658 static unsigned int process_csi_sequence( struct console *console, const WCHAR *buf, size_t size )
1660 unsigned int n, count = 0, params[8], params_cnt = 0, vk, ctrl;
1661 WCHAR outuch;
1663 for (;;)
1665 n = 0;
1666 while (count < size && '0' <= buf[count] && buf[count] <= '9')
1667 n = n * 10 + buf[count++] - '0';
1668 if (params_cnt < ARRAY_SIZE(params)) params[params_cnt++] = n;
1669 else FIXME( "too many params, skipping %u\n", n );
1670 if (count == size) return 0;
1671 if (buf[count] != ';') break;
1672 if (++count == size) return 0;
1675 if ((vk = escape_char_to_vk( buf[count], &ctrl, &outuch )))
1677 key_press( console, outuch, vk, params_cnt >= 2 ? convert_modifiers( params[1] ) : ctrl );
1678 return count + 1;
1681 switch (buf[count])
1683 case '~':
1684 vk = escape_number_to_vk( params[0] );
1685 key_press( console, 0, vk, params_cnt == 2 ? convert_modifiers( params[1] ) : 0 );
1686 return count + 1;
1688 default:
1689 FIXME( "unhandled sequence %s\n", debugstr_wn( buf, size ));
1690 return 0;
1694 static unsigned int process_input_escape( struct console *console, const WCHAR *buf, size_t size )
1696 unsigned int vk = 0, count = 0, nlen;
1698 if (!size)
1700 key_press( console, 0, VK_ESCAPE, 0 );
1701 return 0;
1704 switch(buf[0])
1706 case '[':
1707 if (++count == size) break;
1708 if ((nlen = process_csi_sequence( console, buf + 1, size - 1 ))) return count + nlen;
1709 break;
1711 case 'O':
1712 if (++count == size) break;
1713 vk = escape_char_to_vk( buf[1], NULL, NULL );
1714 if (vk)
1716 key_press( console, 0, vk, 0 );
1717 return count + 1;
1721 char_key_press( console, buf[0], LEFT_ALT_PRESSED );
1722 return 1;
1725 static DWORD WINAPI tty_input( void *param )
1727 struct console *console = param;
1728 IO_STATUS_BLOCK io;
1729 HANDLE event;
1730 char read_buf[4096];
1731 WCHAR buf[4096];
1732 DWORD count, i;
1733 BOOL signaled;
1734 NTSTATUS status;
1736 if (console->is_unix)
1738 unsigned int h = condrv_handle( console->tty_input );
1739 status = NtDeviceIoControlFile( console->server, NULL, NULL, NULL, &io, IOCTL_CONDRV_SETUP_INPUT,
1740 &h, sizeof(h), NULL, 0 );
1741 if (status) ERR( "input setup failed: %#lx\n", status );
1744 event = CreateEventW( NULL, TRUE, FALSE, NULL );
1746 for (;;)
1748 status = NtReadFile( console->tty_input, event, NULL, NULL, &io, read_buf, sizeof(read_buf), NULL, NULL );
1749 if (status == STATUS_PENDING)
1751 if ((status = NtWaitForSingleObject( event, FALSE, NULL ))) break;
1752 status = io.Status;
1754 if (status) break;
1756 EnterCriticalSection( &console_section );
1757 signaled = console->record_count != 0;
1759 /* FIXME: Handle partial char read */
1760 count = MultiByteToWideChar( get_tty_cp( console ), 0, read_buf, io.Information, buf, ARRAY_SIZE(buf) );
1762 TRACE( "%s\n", debugstr_wn(buf, count) );
1764 for (i = 0; i < count; i++)
1766 WCHAR ch = buf[i];
1767 switch (ch)
1769 case 3: /* end of text */
1770 LeaveCriticalSection( &console_section );
1771 goto done;
1772 case '\n':
1773 key_press( console, '\n', VK_RETURN, LEFT_CTRL_PRESSED );
1774 break;
1775 case '\b':
1776 key_press( console, ch, 'H', LEFT_CTRL_PRESSED );
1777 break;
1778 case 0x1b:
1779 i += process_input_escape( console, buf + i + 1, count - i - 1 );
1780 break;
1781 case 0x1c: /* map ctrl-\ unix-ism into ctrl-break/pause windows-ism for unix consoles */
1782 if (console->is_unix)
1783 key_press( console, 0, VK_CANCEL, LEFT_CTRL_PRESSED );
1784 else
1785 char_key_press( console, ch, 0 );
1786 break;
1787 case 0x7f:
1788 key_press( console, '\b', VK_BACK, 0 );
1789 break;
1790 default:
1791 char_key_press( console, ch, 0 );
1795 process_console_input( console );
1796 if (!signaled && console->record_count)
1798 assert( !console->read_ioctl );
1799 read_complete( console, STATUS_SUCCESS, NULL, 0, TRUE ); /* signal console */
1801 LeaveCriticalSection( &console_section );
1804 TRACE( "NtReadFile failed: %#lx\n", status );
1806 done:
1807 EnterCriticalSection( &console_section );
1808 if (console->read_ioctl) read_complete( console, status, NULL, 0, FALSE );
1809 if (console->is_unix)
1811 unsigned int h = 0;
1812 status = NtDeviceIoControlFile( console->server, NULL, NULL, NULL, &io, IOCTL_CONDRV_SETUP_INPUT,
1813 &h, sizeof(h), NULL, 0 );
1814 if (status) ERR( "input restore failed: %#lx\n", status );
1816 CloseHandle( console->input_thread );
1817 console->input_thread = NULL;
1818 LeaveCriticalSection( &console_section );
1820 return 0;
1823 static BOOL ensure_tty_input_thread( struct console *console )
1825 if (!console->tty_input) return TRUE;
1826 if (!console->input_thread)
1827 console->input_thread = CreateThread( NULL, 0, tty_input, console, 0, NULL );
1828 return console->input_thread != NULL;
1831 static NTSTATUS screen_buffer_activate( struct screen_buffer *screen_buffer )
1833 RECT update_rect;
1834 TRACE( "%p\n", screen_buffer );
1835 screen_buffer->console->active = screen_buffer;
1836 SetRect( &update_rect, 0, 0, screen_buffer->width - 1, screen_buffer->height - 1 );
1837 update_output( screen_buffer, &update_rect );
1838 tty_sync( screen_buffer->console );
1839 update_window_config( screen_buffer->console, FALSE );
1840 return STATUS_SUCCESS;
1843 static NTSTATUS get_output_info( struct screen_buffer *screen_buffer, size_t *out_size )
1845 struct condrv_output_info *info;
1847 *out_size = min( *out_size, sizeof(*info) + screen_buffer->font.face_len * sizeof(WCHAR) );
1848 if (!(info = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
1850 info->cursor_size = screen_buffer->cursor_size;
1851 info->cursor_visible = screen_buffer->cursor_visible;
1852 info->cursor_x = get_bounded_cursor_x( screen_buffer );
1853 info->cursor_y = screen_buffer->cursor_y;
1854 info->width = screen_buffer->width;
1855 info->height = screen_buffer->height;
1856 info->attr = screen_buffer->attr;
1857 info->popup_attr = screen_buffer->popup_attr;
1858 info->win_left = screen_buffer->win.left;
1859 info->win_top = screen_buffer->win.top;
1860 info->win_right = screen_buffer->win.right;
1861 info->win_bottom = screen_buffer->win.bottom;
1862 info->max_width = screen_buffer->max_width;
1863 info->max_height = screen_buffer->max_height;
1864 info->font_width = screen_buffer->font.width;
1865 info->font_height = screen_buffer->font.height;
1866 info->font_weight = screen_buffer->font.weight;
1867 info->font_pitch_family = screen_buffer->font.pitch_family;
1868 memcpy( info->color_map, screen_buffer->color_map, sizeof(info->color_map) );
1869 if (*out_size > sizeof(*info)) memcpy( info + 1, screen_buffer->font.face_name, *out_size - sizeof(*info) );
1871 TRACE( "%p cursor_size=%u cursor_visible=%x cursor=(%u,%u) width=%u height=%u win=%s attr=%x popup_attr=%x"
1872 " font_width=%u font_height=%u %s\n", screen_buffer, info->cursor_size, info->cursor_visible,
1873 info->cursor_x, info->cursor_y, info->width, info->height, wine_dbgstr_rect(&screen_buffer->win),
1874 info->attr, info->popup_attr, info->font_width, info->font_height,
1875 debugstr_wn( (const WCHAR *)(info + 1), (*out_size - sizeof(*info)) / sizeof(WCHAR) ) );
1876 return STATUS_SUCCESS;
1879 void notify_screen_buffer_size( struct screen_buffer *screen_buffer )
1881 if (is_active( screen_buffer ) && screen_buffer->console->mode & ENABLE_WINDOW_INPUT)
1883 INPUT_RECORD ir;
1884 ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
1885 ir.Event.WindowBufferSizeEvent.dwSize.X = screen_buffer->width;
1886 ir.Event.WindowBufferSizeEvent.dwSize.Y = screen_buffer->height;
1887 write_console_input( screen_buffer->console, &ir, 1, TRUE );
1891 NTSTATUS change_screen_buffer_size( struct screen_buffer *screen_buffer, int new_width, int new_height )
1893 int i, old_width, old_height, copy_width, copy_height;
1894 char_info_t *new_data;
1896 if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) ))) return STATUS_NO_MEMORY;
1898 old_width = screen_buffer->width;
1899 old_height = screen_buffer->height;
1900 copy_width = min( old_width, new_width );
1901 copy_height = min( old_height, new_height );
1903 /* copy all the rows */
1904 for (i = 0; i < copy_height; i++)
1906 memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
1907 copy_width * sizeof(char_info_t) );
1910 /* clear the end of each row */
1911 if (new_width > old_width)
1913 /* fill first row */
1914 for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
1915 /* and blast it to the other rows */
1916 for (i = 1; i < copy_height; i++)
1917 memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
1918 (new_width - old_width) * sizeof(char_info_t) );
1921 /* clear remaining rows */
1922 if (new_height > old_height)
1924 /* fill first row */
1925 for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
1926 /* and blast it to the other rows */
1927 for (i = old_height+1; i < new_height; i++)
1928 memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
1929 new_width * sizeof(char_info_t) );
1931 free( screen_buffer->data );
1932 screen_buffer->data = new_data;
1933 screen_buffer->width = new_width;
1934 screen_buffer->height = new_height;
1935 return STATUS_SUCCESS;
1938 static NTSTATUS set_output_info( struct screen_buffer *screen_buffer,
1939 const struct condrv_output_info_params *params, size_t in_size )
1941 const struct condrv_output_info *info = &params->info;
1942 NTSTATUS status;
1944 TRACE( "%p\n", screen_buffer );
1946 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
1948 if (info->cursor_size < 1 || info->cursor_size > 100) return STATUS_INVALID_PARAMETER;
1950 screen_buffer->cursor_size = info->cursor_size;
1951 screen_buffer->cursor_visible = !!info->cursor_visible;
1953 if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
1955 if (info->cursor_x < 0 || info->cursor_x >= screen_buffer->width ||
1956 info->cursor_y < 0 || info->cursor_y >= screen_buffer->height)
1958 return STATUS_INVALID_PARAMETER;
1961 if (screen_buffer->cursor_x != info->cursor_x || screen_buffer->cursor_y != info->cursor_y)
1963 struct console *console = screen_buffer->console;
1964 screen_buffer->cursor_x = info->cursor_x;
1965 screen_buffer->cursor_y = info->cursor_y;
1966 if (console->use_relative_cursor)
1967 set_tty_cursor_relative( console, screen_buffer->cursor_x, screen_buffer->cursor_y );
1968 scroll_to_cursor( screen_buffer );
1971 if (params->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
1973 enter_absolute_mode( screen_buffer->console );
1974 /* new screen-buffer cannot be smaller than actual window */
1975 if (info->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
1976 info->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
1978 return STATUS_INVALID_PARAMETER;
1980 /* FIXME: there are also some basic minimum and max size to deal with */
1981 if ((status = change_screen_buffer_size( screen_buffer, info->width, info->height ))) return status;
1983 /* scroll window to display sb */
1984 if (screen_buffer->win.right >= info->width)
1986 screen_buffer->win.right -= screen_buffer->win.left;
1987 screen_buffer->win.left = 0;
1989 if (screen_buffer->win.bottom >= info->height)
1991 screen_buffer->win.bottom -= screen_buffer->win.top;
1992 screen_buffer->win.top = 0;
1994 if (screen_buffer->cursor_x >= info->width) screen_buffer->cursor_x = info->width - 1;
1995 if (screen_buffer->cursor_y >= info->height) screen_buffer->cursor_y = info->height - 1;
1997 notify_screen_buffer_size( screen_buffer );
1999 if (params->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
2001 screen_buffer->attr = info->attr;
2003 if (params->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
2005 screen_buffer->popup_attr = info->popup_attr;
2007 if (params->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
2009 enter_absolute_mode( screen_buffer->console );
2010 if (info->win_left < 0 || info->win_left > info->win_right ||
2011 info->win_right >= screen_buffer->width ||
2012 info->win_top < 0 || info->win_top > info->win_bottom ||
2013 info->win_bottom >= screen_buffer->height)
2015 return STATUS_INVALID_PARAMETER;
2017 if (screen_buffer->win.left != info->win_left || screen_buffer->win.top != info->win_top ||
2018 screen_buffer->win.right != info->win_right || screen_buffer->win.bottom != info->win_bottom)
2020 screen_buffer->win.left = info->win_left;
2021 screen_buffer->win.top = info->win_top;
2022 screen_buffer->win.right = info->win_right;
2023 screen_buffer->win.bottom = info->win_bottom;
2026 if (params->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
2028 enter_absolute_mode( screen_buffer->console );
2029 screen_buffer->max_width = info->max_width;
2030 screen_buffer->max_height = info->max_height;
2032 if (params->mask & SET_CONSOLE_OUTPUT_INFO_FONT)
2034 WCHAR *face_name = (WCHAR *)(params + 1);
2035 size_t face_name_size = in_size - sizeof(*params);
2036 unsigned int height = info->font_height;
2037 unsigned int weight = FW_NORMAL;
2039 if (!face_name_size)
2041 face_name = screen_buffer->font.face_name;
2042 face_name_size = screen_buffer->font.face_len * sizeof(WCHAR);
2045 if (!height) height = 12;
2046 if (info->font_weight >= FW_SEMIBOLD) weight = FW_BOLD;
2048 update_console_font( screen_buffer->console, face_name, face_name_size, height, weight );
2051 if (is_active( screen_buffer ))
2053 tty_sync( screen_buffer->console );
2054 update_window_config( screen_buffer->console, FALSE );
2056 return STATUS_SUCCESS;
2059 static NTSTATUS write_console( struct screen_buffer *screen_buffer, const WCHAR *buffer, size_t len )
2061 RECT update_rect;
2062 size_t i, j;
2064 TRACE( "%s\n", debugstr_wn(buffer, len) );
2066 empty_update_rect( screen_buffer, &update_rect );
2068 for (i = 0; i < len; i++)
2070 if (screen_buffer->mode & ENABLE_PROCESSED_OUTPUT)
2072 switch (buffer[i])
2074 case '\b':
2075 screen_buffer->cursor_x = get_bounded_cursor_x( screen_buffer );
2076 if (screen_buffer->cursor_x) screen_buffer->cursor_x--;
2077 continue;
2078 case '\t':
2079 j = min( screen_buffer->width - screen_buffer->cursor_x, 8 - (screen_buffer->cursor_x % 8) );
2080 if (!j) j = 8;
2081 while (j--) write_char( screen_buffer, ' ', &update_rect, NULL );
2082 continue;
2083 case '\n':
2084 screen_buffer->cursor_x = 0;
2085 if (++screen_buffer->cursor_y == screen_buffer->height)
2086 new_line( screen_buffer, &update_rect );
2087 else if (screen_buffer->mode & ENABLE_WRAP_AT_EOL_OUTPUT)
2089 update_output( screen_buffer, &update_rect );
2090 set_tty_cursor( screen_buffer->console, screen_buffer->cursor_x, screen_buffer->cursor_y );
2092 continue;
2093 case '\a':
2094 FIXME( "beep\n" );
2095 continue;
2096 case '\r':
2097 screen_buffer->cursor_x = 0;
2098 continue;
2101 if (screen_buffer->cursor_x == screen_buffer->width && !(screen_buffer->mode & ENABLE_WRAP_AT_EOL_OUTPUT))
2102 screen_buffer->cursor_x = update_rect.left;
2103 write_char( screen_buffer, buffer[i], &update_rect, NULL );
2106 if (screen_buffer->cursor_x == screen_buffer->width)
2108 if (screen_buffer->mode & ENABLE_WRAP_AT_EOL_OUTPUT)
2110 if (!(screen_buffer->mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
2112 screen_buffer->cursor_x = 0;
2113 if (++screen_buffer->cursor_y == screen_buffer->height)
2114 new_line( screen_buffer, &update_rect );
2117 else screen_buffer->cursor_x = update_rect.left;
2120 scroll_to_cursor( screen_buffer );
2121 update_output( screen_buffer, &update_rect );
2122 tty_sync( screen_buffer->console );
2123 update_window_config( screen_buffer->console, TRUE );
2124 return STATUS_SUCCESS;
2127 static NTSTATUS write_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
2128 size_t in_size, size_t *out_size )
2130 unsigned int i, entry_size, entry_cnt, x, y;
2131 char_info_t *dest;
2132 char *src;
2134 enter_absolute_mode( screen_buffer->console );
2135 if (*out_size == sizeof(SMALL_RECT) && !params->width) return STATUS_INVALID_PARAMETER;
2137 entry_size = params->mode == CHAR_INFO_MODE_TEXTATTR ? sizeof(char_info_t) : sizeof(WCHAR);
2138 entry_cnt = (in_size - sizeof(*params)) / entry_size;
2140 TRACE( "(%u,%u) cnt %u\n", params->x, params->y, entry_cnt );
2142 if (params->x >= screen_buffer->width)
2144 *out_size = 0;
2145 return STATUS_SUCCESS;
2148 for (i = 0, src = (char *)(params + 1); i < entry_cnt; i++, src += entry_size)
2150 if (params->width)
2152 x = params->x + i % params->width;
2153 y = params->y + i / params->width;
2154 if (x >= screen_buffer->width) continue;
2156 else
2158 x = (params->x + i) % screen_buffer->width;
2159 y = params->y + (params->x + i) / screen_buffer->width;
2161 if (y >= screen_buffer->height) break;
2163 dest = &screen_buffer->data[y * screen_buffer->width + x];
2164 switch(params->mode)
2166 case CHAR_INFO_MODE_TEXT:
2167 dest->ch = *(const WCHAR *)src;
2168 break;
2169 case CHAR_INFO_MODE_ATTR:
2170 dest->attr = *(const unsigned short *)src;
2171 break;
2172 case CHAR_INFO_MODE_TEXTATTR:
2173 *dest = *(const char_info_t *)src;
2174 break;
2175 default:
2176 return STATUS_INVALID_PARAMETER;
2180 if (i && is_active( screen_buffer ))
2182 RECT update_rect;
2184 update_rect.left = params->x;
2185 update_rect.top = params->y;
2186 if (params->width)
2188 update_rect.bottom = min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1;
2189 update_rect.right = min( params->x + params->width, screen_buffer->width ) - 1;
2191 else
2193 update_rect.bottom = params->y + (params->x + i - 1) / screen_buffer->width;
2194 if (update_rect.bottom != params->y)
2196 update_rect.left = 0;
2197 update_rect.right = screen_buffer->width - 1;
2199 else
2201 update_rect.right = params->x + i - 1;
2204 update_output( screen_buffer, &update_rect );
2205 tty_sync( screen_buffer->console );
2208 if (*out_size == sizeof(SMALL_RECT))
2210 SMALL_RECT *region;
2211 unsigned int width = params->width;
2212 x = params->x;
2213 y = params->y;
2214 if (!(region = alloc_ioctl_buffer( sizeof(*region )))) return STATUS_NO_MEMORY;
2215 region->Left = x;
2216 region->Top = y;
2217 region->Right = min( x + width, screen_buffer->width ) - 1;
2218 region->Bottom = min( y + entry_cnt / width, screen_buffer->height ) - 1;
2220 else
2222 DWORD *result;
2223 if (!(result = alloc_ioctl_buffer( sizeof(*result )))) return STATUS_NO_MEMORY;
2224 *result = i;
2227 return STATUS_SUCCESS;
2230 static NTSTATUS read_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
2231 size_t *out_size )
2233 enum char_info_mode mode;
2234 unsigned int x, y, width;
2235 unsigned int i, count;
2237 enter_absolute_mode( screen_buffer->console );
2238 x = params->x;
2239 y = params->y;
2240 mode = params->mode;
2241 width = params->width;
2242 TRACE( "(%u %u) mode %u width %u\n", x, y, mode, width );
2244 switch(mode)
2246 case CHAR_INFO_MODE_TEXT:
2248 WCHAR *data;
2249 char_info_t *src;
2250 if (x >= screen_buffer->width || y >= screen_buffer->height)
2252 *out_size = 0;
2253 return STATUS_SUCCESS;
2255 src = screen_buffer->data + y * screen_buffer->width + x;
2256 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
2257 *out_size / sizeof(*data) );
2258 *out_size = count * sizeof(*data);
2259 if (!(data = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
2260 for (i = 0; i < count; i++) data[i] = src[i].ch;
2262 break;
2263 case CHAR_INFO_MODE_ATTR:
2265 unsigned short *data;
2266 char_info_t *src;
2267 if (x >= screen_buffer->width || y >= screen_buffer->height)
2269 *out_size = 0;
2270 return STATUS_SUCCESS;
2272 src = screen_buffer->data + y * screen_buffer->width + x;
2273 count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
2274 *out_size / sizeof(*data) );
2275 *out_size = count * sizeof(*data);
2276 if (!(data = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
2277 for (i = 0; i < count; i++) data[i] = src[i].attr;
2279 break;
2280 case CHAR_INFO_MODE_TEXTATTR:
2282 SMALL_RECT *region;
2283 char_info_t *data;
2284 if (!width || *out_size < sizeof(*region) || x >= screen_buffer->width || y >= screen_buffer->height)
2285 return STATUS_INVALID_PARAMETER;
2286 count = min( (*out_size - sizeof(*region)) / (width * sizeof(*data)), screen_buffer->height - y );
2287 width = min( width, screen_buffer->width - x );
2288 *out_size = sizeof(*region) + width * count * sizeof(*data);
2289 if (!(region = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
2290 region->Left = x;
2291 region->Top = y;
2292 region->Right = x + width - 1;
2293 region->Bottom = y + count - 1;
2294 data = (char_info_t *)(region + 1);
2295 for (i = 0; i < count; i++)
2297 memcpy( &data[i * width], &screen_buffer->data[(y + i) * screen_buffer->width + x],
2298 width * sizeof(*data) );
2301 break;
2302 default:
2303 return STATUS_INVALID_PARAMETER;
2306 return STATUS_SUCCESS;
2309 static NTSTATUS fill_output( struct screen_buffer *screen_buffer, const struct condrv_fill_output_params *params )
2311 char_info_t *end, *dest;
2312 DWORD i, count, *result;
2314 TRACE( "(%u %u) mode %u\n", params->x, params->y, params->mode );
2316 enter_absolute_mode( screen_buffer->console );
2317 if (params->y >= screen_buffer->height) return STATUS_SUCCESS;
2318 dest = screen_buffer->data + min( params->y * screen_buffer->width + params->x,
2319 screen_buffer->height * screen_buffer->width );
2321 end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
2323 count = params->count;
2324 if (count > end - dest) count = end - dest;
2326 switch(params->mode)
2328 case CHAR_INFO_MODE_TEXT:
2329 for (i = 0; i < count; i++) dest[i].ch = params->ch;
2330 break;
2331 case CHAR_INFO_MODE_ATTR:
2332 for (i = 0; i < count; i++) dest[i].attr = params->attr;
2333 break;
2334 case CHAR_INFO_MODE_TEXTATTR:
2335 for (i = 0; i < count; i++)
2337 dest[i].ch = params->ch;
2338 dest[i].attr = params->attr;
2340 break;
2341 default:
2342 return STATUS_INVALID_PARAMETER;
2345 if (count && is_active(screen_buffer))
2347 RECT update_rect;
2348 SetRect( &update_rect,
2349 params->x % screen_buffer->width,
2350 params->y + params->x / screen_buffer->width,
2351 (params->x + i - 1) % screen_buffer->width,
2352 params->y + (params->x + i - 1) / screen_buffer->width );
2353 update_output( screen_buffer, &update_rect );
2354 tty_sync( screen_buffer->console );
2357 if (!(result = alloc_ioctl_buffer( sizeof(*result) ))) return STATUS_NO_MEMORY;
2358 *result = count;
2359 return STATUS_SUCCESS;
2362 static NTSTATUS scroll_output( struct screen_buffer *screen_buffer, const struct condrv_scroll_params *params )
2364 int x, y, xsrc, ysrc, w, h;
2365 char_info_t *psrc, *pdst;
2366 SMALL_RECT src, dst;
2367 RECT update_rect;
2368 SMALL_RECT clip;
2370 enter_absolute_mode( screen_buffer->console );
2371 xsrc = params->scroll.Left;
2372 ysrc = params->scroll.Top;
2373 w = params->scroll.Right - params->scroll.Left + 1;
2374 h = params->scroll.Bottom - params->scroll.Top + 1;
2376 TRACE( "(%d %d) -> (%u %u) w %u h %u\n", xsrc, ysrc, params->origin.X, params->origin.Y, w, h );
2378 clip.Left = max( params->clip.Left, 0 );
2379 clip.Top = max( params->clip.Top, 0 );
2380 clip.Right = min( params->clip.Right, screen_buffer->width - 1 );
2381 clip.Bottom = min( params->clip.Bottom, screen_buffer->height - 1 );
2382 if (clip.Left > clip.Right || clip.Top > clip.Bottom || params->scroll.Left < 0 || params->scroll.Top < 0 ||
2383 params->scroll.Right >= screen_buffer->width || params->scroll.Bottom >= screen_buffer->height ||
2384 params->scroll.Right < params->scroll.Left || params->scroll.Top > params->scroll.Bottom ||
2385 params->origin.X < 0 || params->origin.X >= screen_buffer->width || params->origin.Y < 0 ||
2386 params->origin.Y >= screen_buffer->height)
2387 return STATUS_INVALID_PARAMETER;
2389 src.Left = max( xsrc, clip.Left );
2390 src.Top = max( ysrc, clip.Top );
2391 src.Right = min( xsrc + w - 1, clip.Right );
2392 src.Bottom = min( ysrc + h - 1, clip.Bottom );
2394 dst.Left = params->origin.X;
2395 dst.Top = params->origin.Y;
2396 dst.Right = params->origin.X + w - 1;
2397 dst.Bottom = params->origin.Y + h - 1;
2399 if (dst.Left < clip.Left)
2401 xsrc += clip.Left - dst.Left;
2402 w -= clip.Left - dst.Left;
2403 dst.Left = clip.Left;
2405 if (dst.Top < clip.Top)
2407 ysrc += clip.Top - dst.Top;
2408 h -= clip.Top - dst.Top;
2409 dst.Top = clip.Top;
2411 if (dst.Right > clip.Right) w -= dst.Right - clip.Right;
2412 if (dst.Bottom > clip.Bottom) h -= dst.Bottom - clip.Bottom;
2414 if (w > 0 && h > 0)
2416 if (ysrc < dst.Top)
2418 psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
2419 pdst = &screen_buffer->data[(dst.Top + h - 1) * screen_buffer->width + dst.Left];
2421 for (y = h; y > 0; y--)
2423 memcpy( pdst, psrc, w * sizeof(*pdst) );
2424 pdst -= screen_buffer->width;
2425 psrc -= screen_buffer->width;
2428 else
2430 psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
2431 pdst = &screen_buffer->data[dst.Top * screen_buffer->width + dst.Left];
2433 for (y = 0; y < h; y++)
2435 /* we use memmove here because when psrc and pdst are the same,
2436 * copies are done on the same row, so the dst and src blocks
2437 * can overlap */
2438 memmove( pdst, psrc, w * sizeof(*pdst) );
2439 pdst += screen_buffer->width;
2440 psrc += screen_buffer->width;
2445 for (y = src.Top; y <= src.Bottom; y++)
2447 int left = src.Left;
2448 int right = src.Right;
2449 if (dst.Top <= y && y <= dst.Bottom)
2451 if (dst.Left <= src.Left) left = max( left, dst.Right + 1 );
2452 if (dst.Left >= src.Left) right = min( right, dst.Left - 1 );
2454 for (x = left; x <= right; x++) screen_buffer->data[y * screen_buffer->width + x] = params->fill;
2457 SetRect( &update_rect, min( src.Left, dst.Left ), min( src.Top, dst.Top ),
2458 max( src.Right, dst.Right ), max( src.Bottom, dst.Bottom ));
2459 update_output( screen_buffer, &update_rect );
2460 tty_sync( screen_buffer->console );
2461 return STATUS_SUCCESS;
2464 static NTSTATUS set_console_title( struct console *console, const WCHAR *in_title, size_t size )
2466 WCHAR *title = NULL;
2468 TRACE( "%s\n", debugstr_wn(in_title, size / sizeof(WCHAR)) );
2470 if (size)
2472 if (!(title = malloc( size + sizeof(WCHAR) ))) return STATUS_NO_MEMORY;
2473 memcpy( title, in_title, size );
2474 title[size / sizeof(WCHAR)] = 0;
2476 free( console->title );
2477 console->title = title;
2479 if (console->tty_output)
2481 size_t len;
2482 char *vt;
2484 tty_write( console, "\x1b]0;", 4 );
2485 len = WideCharToMultiByte( get_tty_cp( console ), 0, console->title, size / sizeof(WCHAR),
2486 NULL, 0, NULL, NULL);
2487 if ((vt = tty_alloc_buffer( console, len )))
2488 WideCharToMultiByte( get_tty_cp( console ), 0, console->title, size / sizeof(WCHAR),
2489 vt, len, NULL, NULL );
2490 tty_write( console, "\x07", 1 );
2491 tty_sync( console );
2493 if (console->win)
2494 SetWindowTextW( console->win, console->title );
2495 return STATUS_SUCCESS;
2498 static NTSTATUS screen_buffer_ioctl( struct screen_buffer *screen_buffer, unsigned int code,
2499 const void *in_data, size_t in_size, size_t *out_size )
2501 switch (code)
2503 case IOCTL_CONDRV_CLOSE_OUTPUT:
2504 if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
2505 destroy_screen_buffer( screen_buffer );
2506 return STATUS_SUCCESS;
2508 case IOCTL_CONDRV_ACTIVATE:
2509 if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
2510 return screen_buffer_activate( screen_buffer );
2512 case IOCTL_CONDRV_GET_MODE:
2514 DWORD *mode;
2515 TRACE( "returning mode %x\n", screen_buffer->mode );
2516 if (in_size || *out_size != sizeof(*mode)) return STATUS_INVALID_PARAMETER;
2517 if (!(mode = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
2518 *mode = screen_buffer->mode;
2519 return STATUS_SUCCESS;
2522 case IOCTL_CONDRV_SET_MODE:
2523 if (in_size != sizeof(unsigned int) || *out_size) return STATUS_INVALID_PARAMETER;
2524 screen_buffer->mode = *(unsigned int *)in_data;
2525 TRACE( "set %x mode\n", screen_buffer->mode );
2526 return STATUS_SUCCESS;
2528 case IOCTL_CONDRV_IS_UNIX:
2529 return screen_buffer->console->is_unix ? STATUS_SUCCESS : STATUS_NOT_SUPPORTED;
2531 case IOCTL_CONDRV_WRITE_CONSOLE:
2532 if (in_size % sizeof(WCHAR) || *out_size) return STATUS_INVALID_PARAMETER;
2533 return write_console( screen_buffer, in_data, in_size / sizeof(WCHAR) );
2535 case IOCTL_CONDRV_WRITE_FILE:
2537 unsigned int len;
2538 WCHAR *buf;
2539 NTSTATUS status;
2541 len = MultiByteToWideChar( screen_buffer->console->output_cp, 0, in_data, in_size,
2542 NULL, 0 );
2543 if (!len) return STATUS_SUCCESS;
2544 if (!(buf = malloc( len * sizeof(WCHAR) ))) return STATUS_NO_MEMORY;
2545 MultiByteToWideChar( screen_buffer->console->output_cp, 0, in_data, in_size, buf, len );
2546 status = write_console( screen_buffer, buf, len );
2547 free( buf );
2548 return status;
2551 case IOCTL_CONDRV_WRITE_OUTPUT:
2552 if ((*out_size != sizeof(DWORD) && *out_size != sizeof(SMALL_RECT)) ||
2553 in_size < sizeof(struct condrv_output_params))
2554 return STATUS_INVALID_PARAMETER;
2555 return write_output( screen_buffer, in_data, in_size, out_size );
2557 case IOCTL_CONDRV_READ_OUTPUT:
2558 if (in_size != sizeof(struct condrv_output_params)) return STATUS_INVALID_PARAMETER;
2559 return read_output( screen_buffer, in_data, out_size );
2561 case IOCTL_CONDRV_GET_OUTPUT_INFO:
2562 if (in_size || *out_size < sizeof(struct condrv_output_info)) return STATUS_INVALID_PARAMETER;
2563 return get_output_info( screen_buffer, out_size );
2565 case IOCTL_CONDRV_SET_OUTPUT_INFO:
2566 if (in_size < sizeof(struct condrv_output_info_params) || *out_size)
2567 return STATUS_INVALID_PARAMETER;
2568 return set_output_info( screen_buffer, in_data, in_size );
2570 case IOCTL_CONDRV_FILL_OUTPUT:
2571 if (in_size != sizeof(struct condrv_fill_output_params) || *out_size != sizeof(DWORD))
2572 return STATUS_INVALID_PARAMETER;
2573 return fill_output( screen_buffer, in_data );
2575 case IOCTL_CONDRV_SCROLL:
2576 if (in_size != sizeof(struct condrv_scroll_params) || *out_size)
2577 return STATUS_INVALID_PARAMETER;
2578 return scroll_output( screen_buffer, in_data );
2580 default:
2581 WARN( "invalid ioctl %x\n", code );
2582 return STATUS_INVALID_HANDLE;
2586 static NTSTATUS console_input_ioctl( struct console *console, unsigned int code, const void *in_data,
2587 size_t in_size, size_t *out_size )
2589 NTSTATUS status;
2591 switch (code)
2593 case IOCTL_CONDRV_GET_MODE:
2595 DWORD *mode;
2596 TRACE( "returning mode %x\n", console->mode );
2597 if (in_size || *out_size != sizeof(*mode)) return STATUS_INVALID_PARAMETER;
2598 if (!(mode = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
2599 *mode = console->mode;
2600 return STATUS_SUCCESS;
2603 case IOCTL_CONDRV_SET_MODE:
2604 if (in_size != sizeof(unsigned int) || *out_size) return STATUS_INVALID_PARAMETER;
2605 console->mode = *(unsigned int *)in_data;
2606 TRACE( "set %x mode\n", console->mode );
2607 return STATUS_SUCCESS;
2609 case IOCTL_CONDRV_IS_UNIX:
2610 return console->is_unix ? STATUS_SUCCESS : STATUS_NOT_SUPPORTED;
2612 case IOCTL_CONDRV_READ_CONSOLE:
2613 if (in_size || *out_size % sizeof(WCHAR)) return STATUS_INVALID_PARAMETER;
2614 ensure_tty_input_thread( console );
2615 status = read_console( console, code, *out_size, NULL, 0, 0 );
2616 *out_size = 0;
2617 return status;
2619 case IOCTL_CONDRV_READ_CONSOLE_CONTROL:
2620 if ((in_size < sizeof(DWORD)) || ((in_size - sizeof(DWORD)) % sizeof(WCHAR)) ||
2621 (*out_size < sizeof(DWORD)) || ((*out_size - sizeof(DWORD)) % sizeof(WCHAR)))
2622 return STATUS_INVALID_PARAMETER;
2623 ensure_tty_input_thread( console );
2624 status = read_console( console, code, *out_size - sizeof(DWORD),
2625 (const WCHAR*)((const char*)in_data + sizeof(DWORD)),
2626 (in_size - sizeof(DWORD)) / sizeof(WCHAR),
2627 *(DWORD*)in_data );
2628 *out_size = 0;
2629 return status;
2631 case IOCTL_CONDRV_READ_FILE:
2632 ensure_tty_input_thread( console );
2633 status = read_console( console, code, *out_size, NULL, 0, 0 );
2634 *out_size = 0;
2635 return status;
2637 case IOCTL_CONDRV_READ_INPUT:
2639 if (in_size) return STATUS_INVALID_PARAMETER;
2640 ensure_tty_input_thread( console );
2641 if (!console->record_count && *out_size)
2643 TRACE( "pending read\n" );
2644 console->read_ioctl = IOCTL_CONDRV_READ_INPUT;
2645 console->pending_read = *out_size;
2646 return STATUS_PENDING;
2648 status = read_console_input( console, *out_size );
2649 *out_size = 0;
2650 return status;
2653 case IOCTL_CONDRV_WRITE_INPUT:
2654 if (in_size % sizeof(INPUT_RECORD) || *out_size) return STATUS_INVALID_PARAMETER;
2655 return write_console_input( console, in_data, in_size / sizeof(INPUT_RECORD), TRUE );
2657 case IOCTL_CONDRV_PEEK:
2659 void *result;
2660 TRACE( "peek\n" );
2661 if (in_size) return STATUS_INVALID_PARAMETER;
2662 ensure_tty_input_thread( console );
2663 *out_size = min( *out_size, console->record_count * sizeof(INPUT_RECORD) );
2664 if (!(result = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
2665 if (*out_size) memcpy( result, console->records, *out_size );
2666 return STATUS_SUCCESS;
2669 case IOCTL_CONDRV_GET_INPUT_INFO:
2671 struct condrv_input_info *info;
2672 TRACE( "get info\n" );
2673 if (in_size || *out_size != sizeof(*info)) return STATUS_INVALID_PARAMETER;
2674 if (!(info = alloc_ioctl_buffer( sizeof(*info )))) return STATUS_NO_MEMORY;
2675 info->input_cp = console->input_cp;
2676 info->output_cp = console->output_cp;
2677 info->input_count = console->record_count;
2678 return STATUS_SUCCESS;
2681 case IOCTL_CONDRV_GET_WINDOW:
2683 condrv_handle_t *result;
2684 TRACE( "get window\n" );
2685 if (in_size || *out_size != sizeof(*result)) return STATUS_INVALID_PARAMETER;
2686 if (!(result = alloc_ioctl_buffer( sizeof(*result )))) return STATUS_NO_MEMORY;
2687 if (!console->win && !console->no_window) init_message_window( console );
2688 *result = condrv_handle( console->win );
2689 return STATUS_SUCCESS;
2692 case IOCTL_CONDRV_SET_INPUT_INFO:
2694 const struct condrv_input_info_params *params = in_data;
2695 TRACE( "set info\n" );
2696 if (in_size != sizeof(*params) || *out_size) return STATUS_INVALID_PARAMETER;
2697 if (params->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
2699 if (!IsValidCodePage( params->info.input_cp )) return STATUS_INVALID_PARAMETER;
2700 console->input_cp = params->info.input_cp;
2702 if (params->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
2704 if (!IsValidCodePage( params->info.output_cp )) return STATUS_INVALID_PARAMETER;
2705 console->output_cp = params->info.output_cp;
2707 return STATUS_SUCCESS;
2710 case IOCTL_CONDRV_GET_TITLE:
2712 size_t title_len, str_size;
2713 struct condrv_title_params *params;
2714 if (in_size) return STATUS_INVALID_PARAMETER;
2715 title_len = console->title ? wcslen( console->title ) : 0;
2716 str_size = min( *out_size - sizeof(*params), title_len * sizeof(WCHAR) );
2717 *out_size = sizeof(*params) + str_size;
2718 if (!(params = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
2719 TRACE( "returning title %s\n", debugstr_w(console->title) );
2720 if (str_size) memcpy( params->buffer, console->title, str_size );
2721 params->title_len = title_len;
2722 return STATUS_SUCCESS;
2725 case IOCTL_CONDRV_SET_TITLE:
2726 if (in_size % sizeof(WCHAR) || *out_size) return STATUS_INVALID_PARAMETER;
2727 return set_console_title( console, in_data, in_size );
2729 case IOCTL_CONDRV_BEEP:
2730 if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
2731 if (console->is_unix)
2733 tty_write( console, "\a", 1 );
2734 tty_sync( console );
2736 return STATUS_SUCCESS;
2738 case IOCTL_CONDRV_FLUSH:
2739 if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
2740 TRACE( "flush\n" );
2741 console->record_count = 0;
2742 return STATUS_SUCCESS;
2744 default:
2745 WARN( "unsupported ioctl %x\n", code );
2746 return STATUS_INVALID_HANDLE;
2750 static NTSTATUS process_console_ioctls( struct console *console )
2752 size_t out_size = 0, in_size;
2753 unsigned int code;
2754 int output;
2755 NTSTATUS status = STATUS_SUCCESS;
2757 for (;;)
2759 if (status) out_size = 0;
2761 console->signaled = console->record_count != 0;
2762 SERVER_START_REQ( get_next_console_request )
2764 req->handle = wine_server_obj_handle( console->server );
2765 req->status = status;
2766 req->signal = console->signaled;
2767 wine_server_add_data( req, ioctl_buffer, out_size );
2768 wine_server_set_reply( req, ioctl_buffer, ioctl_buffer_size );
2769 status = wine_server_call( req );
2770 code = reply->code;
2771 output = reply->output;
2772 out_size = reply->out_size;
2773 in_size = wine_server_reply_size( reply );
2775 SERVER_END_REQ;
2777 if (status == STATUS_PENDING) return STATUS_SUCCESS;
2778 if (status == STATUS_BUFFER_OVERFLOW)
2780 if (!alloc_ioctl_buffer( out_size )) return STATUS_NO_MEMORY;
2781 status = STATUS_SUCCESS;
2782 continue;
2784 if (status)
2786 TRACE( "failed to get next request: %#lx\n", status );
2787 return status;
2790 if (code == IOCTL_CONDRV_INIT_OUTPUT)
2792 TRACE( "initializing output %x\n", output );
2793 enter_absolute_mode( console );
2794 if (console->active)
2795 create_screen_buffer( console, output, console->active->width, console->active->height );
2796 else
2797 create_screen_buffer( console, output, 80, 150 );
2799 else if (!output)
2801 status = console_input_ioctl( console, code, ioctl_buffer, in_size, &out_size );
2803 else
2805 struct wine_rb_entry *entry;
2806 if (!(entry = wine_rb_get( &screen_buffer_map, LongToPtr(output) )))
2808 ERR( "invalid screen buffer id %x\n", output );
2809 status = STATUS_INVALID_HANDLE;
2811 else
2813 status = screen_buffer_ioctl( WINE_RB_ENTRY_VALUE( entry, struct screen_buffer, entry ), code,
2814 ioctl_buffer, in_size, &out_size );
2820 static int main_loop( struct console *console, HANDLE signal )
2822 HANDLE signal_event = NULL;
2823 HANDLE wait_handles[3];
2824 unsigned int wait_cnt = 0;
2825 unsigned short signal_id;
2826 IO_STATUS_BLOCK signal_io;
2827 NTSTATUS status;
2828 DWORD res;
2830 if (signal)
2832 if (!(signal_event = CreateEventW( NULL, TRUE, FALSE, NULL ))) return 1;
2833 status = NtReadFile( signal, signal_event, NULL, NULL, &signal_io, &signal_id,
2834 sizeof(signal_id), NULL, NULL );
2835 if (status && status != STATUS_PENDING) return 1;
2838 if (!alloc_ioctl_buffer( 4096 )) return 1;
2840 wait_handles[wait_cnt++] = console->server;
2841 if (signal) wait_handles[wait_cnt++] = signal_event;
2842 if (console->input_thread) wait_handles[wait_cnt++] = console->input_thread;
2844 for (;;)
2846 if (console->win)
2847 res = MsgWaitForMultipleObjects( wait_cnt, wait_handles, FALSE, INFINITE, QS_ALLINPUT );
2848 else
2849 res = WaitForMultipleObjects( wait_cnt, wait_handles, FALSE, INFINITE );
2851 if (res == WAIT_OBJECT_0 + wait_cnt)
2853 MSG msg;
2854 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
2856 if (msg.message == WM_QUIT) return 0;
2857 DispatchMessageW(&msg);
2859 continue;
2862 switch (res)
2864 case WAIT_OBJECT_0:
2865 EnterCriticalSection( &console_section );
2866 status = process_console_ioctls( console );
2867 LeaveCriticalSection( &console_section );
2868 if (status) return 0;
2869 break;
2871 case WAIT_OBJECT_0 + 1:
2872 if (signal_io.Status || signal_io.Information != sizeof(signal_id))
2874 TRACE( "signaled quit\n" );
2875 return 0;
2877 FIXME( "unimplemented signal %x\n", signal_id );
2878 status = NtReadFile( signal, signal_event, NULL, NULL, &signal_io, &signal_id,
2879 sizeof(signal_id), NULL, NULL );
2880 if (status && status != STATUS_PENDING) return 1;
2881 break;
2883 default:
2884 TRACE( "wait failed, quit\n");
2885 return 0;
2889 return 0;
2892 static void teardown( struct console *console )
2894 if (console->is_unix)
2896 set_tty_attr( console, empty_char_info.attr );
2897 tty_flush( console );
2901 int __cdecl wmain(int argc, WCHAR *argv[])
2903 int headless = 0, i, width = 0, height = 0, ret;
2904 HANDLE signal = NULL;
2905 WCHAR *end;
2907 static struct console console;
2909 for (i = 0; i < argc; i++) TRACE("%s ", wine_dbgstr_w(argv[i]));
2910 TRACE("\n");
2912 console.mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
2913 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
2914 ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_AUTO_POSITION;
2915 console.input_cp = console.output_cp = GetOEMCP();
2916 console.history_size = 50;
2917 if (!(console.history = calloc( console.history_size, sizeof(*console.history) ))) return 1;
2919 for (i = 1; i < argc; i++)
2921 if (!wcscmp( argv[i], L"--headless"))
2923 headless = 1;
2924 continue;
2926 if (!wcscmp( argv[i], L"--unix"))
2928 console.is_unix = 1;
2929 console.use_relative_cursor = 1;
2930 headless = 1;
2931 continue;
2933 if (!wcscmp( argv[i], L"--width" ))
2935 if (++i == argc) return 1;
2936 width = wcstol( argv[i], &end, 0 );
2937 if ((!width && !console.is_unix) || width > 0xffff || *end) return 1;
2938 continue;
2940 if (!wcscmp( argv[i], L"--height" ))
2942 if (++i == argc) return 1;
2943 height = wcstol( argv[i], &end, 0 );
2944 if ((!height && !console.is_unix) || height > 0xffff || *end) return 1;
2945 continue;
2947 if (!wcscmp( argv[i], L"--signal" ))
2949 if (++i == argc) return 1;
2950 signal = ULongToHandle( wcstol( argv[i], &end, 0 ));
2951 if (*end) return 1;
2952 continue;
2954 if (!wcscmp( argv[i], L"--server" ))
2956 if (++i == argc) return 1;
2957 console.server = ULongToHandle( wcstol( argv[i], &end, 0 ));
2958 if (*end) return 1;
2959 continue;
2961 FIXME( "unknown option %s\n", debugstr_w(argv[i]) );
2962 return 1;
2965 if (!console.server)
2967 ERR( "no server handle\n" );
2968 return 1;
2971 if (!width) width = 80;
2972 if (!height) height = 150;
2974 if (!(console.active = create_screen_buffer( &console, 1, width, height ))) return 1;
2975 if (headless)
2977 console.tty_input = GetStdHandle( STD_INPUT_HANDLE );
2978 console.tty_output = GetStdHandle( STD_OUTPUT_HANDLE );
2980 if (console.tty_input || console.tty_output)
2982 init_tty_output( &console );
2983 if (!console.is_unix && !ensure_tty_input_thread( &console )) return 1;
2985 else console.no_window = TRUE;
2987 else
2989 STARTUPINFOW si;
2990 if (!init_window( &console )) return 1;
2991 GetStartupInfoW( &si );
2992 set_console_title( &console, si.lpTitle, wcslen( si.lpTitle ) * sizeof(WCHAR) );
2993 ShowWindow( console.win, (si.dwFlags & STARTF_USESHOWWINDOW) ? si.wShowWindow : SW_SHOW );
2996 ret = main_loop( &console, signal );
2997 teardown( &console );
2999 return ret;