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
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
)
51 if (!(new_buffer
= realloc( ioctl_buffer
, size
))) return NULL
;
52 ioctl_buffer
= new_buffer
;
53 ioctl_buffer_size
= size
;
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
;
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
;
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
);
110 memcpy( screen_buffer
->font
.face_name
, console
->active
->font
.face_name
,
111 screen_buffer
->font
.face_len
* sizeof(WCHAR
) );
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
);
133 if (!(screen_buffer
->data
= malloc( screen_buffer
->width
* screen_buffer
->height
*
134 sizeof(*screen_buffer
->data
) )))
136 destroy_screen_buffer( screen_buffer
);
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
,
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
;
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
)
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
;
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
)
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
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
);
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
)
252 sprintf( buf
, "\x1b[%uA", console
->tty_cursor_y
- y
);
253 tty_write( console
, buf
, strlen(buf
) );
254 console
->tty_cursor_y
= y
;
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
)
272 if ((attr
& 0x0f) != (console
->tty_attr
& 0x0f))
274 if ((attr
& 0x0f) != 7)
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)
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
;
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
)
368 TRACE( "%s\n", wine_dbgstr_rect( rect
));
370 if (screen_buffer
->console
->window
)
372 update_window_region( screen_buffer
->console
, rect
);
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 );
400 if (screen_buffer
->console
->is_unix
&& wch
< L
' ' && mask
& (1u << wch
))
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
)
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
);
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
)
448 if (!*home_y
) return;
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
;
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
);
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;
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;
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
);
513 case IOCTL_CONDRV_READ_FILE
:
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
);
522 if (!(buf
= malloc( len
)))
524 read_complete( console
, STATUS_NO_MEMORY
, NULL
, 0, console
->record_count
!= 0 );
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 );
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
))
556 if (!(ptr
= malloc( offsetof( struct history_line
, text
[len
/ sizeof(WCHAR
)] )))) return;
558 memcpy( ptr
->text
, str
, len
);
560 if (console
->history_index
< console
->history_size
)
562 console
->history
[console
->history_index
++] = ptr
;
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
;
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
;
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
;
597 ctx
->size
= new_size
;
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
);
608 memmove( &ctx
->buf
[begin
], &ctx
->buf
[end
], (ctx
->len
- end
) * sizeof(WCHAR
));
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
;
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
) );
627 update_len
= ctx
->len
- ctx
->cursor
;
631 if (ctx
->cursor
+ len
> ctx
->len
)
633 if (!edit_line_grow( console
, (ctx
->cursor
+ len
) - ctx
->len
) )
635 ctx
->len
= ctx
->cursor
+ 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
);
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;
652 ctx
->yanked
= malloc( (len
+ 1) * sizeof(WCHAR
) );
655 ctx
->status
= STATUS_NO_MEMORY
;
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
)
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
)
674 while (offset
<= console
->edit_line
.len
&& iswalnum( console
->edit_line
.buf
[offset
] ))
676 while (offset
<= console
->edit_line
.len
&& !iswalnum( console
->edit_line
.buf
[offset
] ))
678 return min(offset
, console
->edit_line
.len
);
681 static WCHAR
*edit_line_history( struct console
*console
, unsigned int index
)
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 ptr
= wcsdup( console
->edit_line
.current_history
);
700 static void edit_line_move_to_history( struct console
*console
, int index
)
702 struct edit_line
*ctx
= &console
->edit_line
;
703 WCHAR
*line
= edit_line_history(console
, index
);
704 size_t len
= line
? lstrlenW(line
) : 0;
706 /* save current line edition for recall when needed */
707 if (ctx
->history_index
== console
->history_index
)
709 free( ctx
->current_history
);
710 ctx
->current_history
= malloc( (ctx
->len
+ 1) * sizeof(WCHAR
) );
711 if (ctx
->current_history
)
713 memcpy( ctx
->current_history
, ctx
->buf
, (ctx
->len
+ 1) * sizeof(WCHAR
) );
718 ctx
->status
= STATUS_NO_MEMORY
;
723 /* need to clean also the screen if new string is shorter than old one */
724 edit_line_delete(console
, 0, ctx
->len
);
726 /* insert new string */
727 if (edit_line_grow(console
, len
+ 1))
729 edit_line_insert( console
, line
, len
);
730 ctx
->history_index
= index
;
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
;
742 if (!console
->history_index
) return;
743 if (ctx
->history_index
&& ctx
->history_index
== console
->history_index
)
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
;
766 edit_line_insert( console
, line
, len
- 1 );
767 ctx
->cursor
= oldoffset
;
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
;
818 if (ctx
->mark
> ctx
->len
) return;
819 cursor
= ctx
->cursor
;
820 ctx
->cursor
= ctx
->mark
;
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
)
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
;
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 );
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 WCHAR
*tmp
= malloc( len_r
* sizeof(WCHAR
) );
870 ctx
->status
= STATUS_NO_MEMORY
;
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
) );
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
;
940 edit_line_save_yank( console
, 0, ctx
->cursor
);
941 edit_line_delete( console
, 0, ctx
->cursor
);
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
)
953 if (ctx
->mark
> ctx
->cursor
)
963 edit_line_save_yank( console
, begin
, end
);
964 edit_line_delete( console
, begin
, end
);
968 static void edit_line_delete_prev( struct console
*console
)
970 struct edit_line
*ctx
= &console
->edit_line
;
973 edit_line_delete( console
, ctx
->cursor
- 1, 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
},
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
},
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
},
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
},
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
},
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
},
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
},
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
},
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;
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
;
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
);
1203 write_char( screen_buffer
, ctx
->buf
[j
], &update_rect
, &ctx
->home_y
);
1207 end_offset
= ctx
->end_offset
;
1208 ctx
->end_offset
= offset
;
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
);
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
);
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
;
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
;
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
:
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
;
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
);
1319 if (ir
.Event
.KeyEvent
.uChar
.UnicodeChar
== 10) continue;
1322 for (map
= console
->edition_mode
? emacs_key_map
: win32_key_map
; map
->entries
!= NULL
; map
++)
1324 if (map
->key_state
!= state
)
1328 for (entry
= &map
->entries
[0]; entry
->func
!= 0; entry
++)
1329 if (entry
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
1333 for (entry
= &map
->entries
[0]; entry
->func
!= 0; entry
++)
1334 if (entry
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
1345 ctx
->insert_mode
= ((console
->mode
& (ENABLE_INSERT_MODE
| ENABLE_EXTENDED_FLAGS
)) ==
1346 (ENABLE_INSERT_MODE
| ENABLE_EXTENDED_FLAGS
))
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
;
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
);
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
;
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
)
1440 offset
-= ctx
->home_x
+ 1;
1441 deltay
= offset
/ console
->active
->width
+ 1;
1442 if (ctx
->home_y
>= deltay
)
1443 ctx
->home_y
-= deltay
;
1447 FIXME("Support for negative ordinates is missing\n");
1449 ctx
->home_x
= console
->active
->width
- 1 - (offset
% console
->active
->width
);
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
;
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
;
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
;
1505 if (console
->mode
& ENABLE_PROCESSED_INPUT
)
1508 for (i
= 0; i
< count
; i
++)
1512 if (map_to_ctrlevent( console
, &records
[i
], &event
))
1514 if (records
[i
].Event
.KeyEvent
.bKeyDown
)
1516 struct condrv_ctrl_event ctrl_event
;
1519 ctrl_event
.event
= event
;
1520 ctrl_event
.group_id
= 0;
1521 NtDeviceIoControlFile( console
->server
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_CTRL_EVENT
,
1522 &ctrl_event
, sizeof(ctrl_event
), NULL
, 0 );
1526 console
->records
[console
->record_count
++] = records
[i
];
1531 memcpy( console
->records
+ console
->record_count
, records
, count
* sizeof(INPUT_RECORD
) );
1532 console
->record_count
+= count
;
1534 return flush
? process_console_input( console
) : STATUS_SUCCESS
;
1537 static void set_key_input_record( INPUT_RECORD
*record
, WCHAR ch
, unsigned int vk
, BOOL is_down
, unsigned int ctrl_state
)
1539 record
->EventType
= KEY_EVENT
;
1540 record
->Event
.KeyEvent
.bKeyDown
= is_down
;
1541 record
->Event
.KeyEvent
.wRepeatCount
= 1;
1542 record
->Event
.KeyEvent
.uChar
.UnicodeChar
= ch
;
1543 record
->Event
.KeyEvent
.wVirtualKeyCode
= vk
;
1544 record
->Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKeyW( vk
, MAPVK_VK_TO_VSC
);
1545 record
->Event
.KeyEvent
.dwControlKeyState
= ctrl_state
;
1548 static NTSTATUS
key_press( struct console
*console
, WCHAR ch
, unsigned int vk
, unsigned int ctrl_state
)
1550 INPUT_RECORD records
[8];
1551 unsigned int count
= 0, ctrl
= 0;
1553 if (ctrl_state
& SHIFT_PRESSED
)
1555 ctrl
|= SHIFT_PRESSED
;
1556 set_key_input_record( &records
[count
++], 0, VK_SHIFT
, TRUE
, ctrl
);
1558 if (ctrl_state
& LEFT_ALT_PRESSED
)
1560 ctrl
|= LEFT_ALT_PRESSED
;
1561 set_key_input_record( &records
[count
++], 0, VK_MENU
, TRUE
, ctrl
);
1563 if (ctrl_state
& LEFT_CTRL_PRESSED
)
1565 ctrl
|= LEFT_CTRL_PRESSED
;
1566 set_key_input_record( &records
[count
++], 0, VK_CONTROL
, TRUE
, ctrl
);
1569 set_key_input_record( &records
[count
++], ch
, vk
, TRUE
, ctrl
);
1570 set_key_input_record( &records
[count
++], ch
, vk
, FALSE
, ctrl
);
1572 if (ctrl
& LEFT_CTRL_PRESSED
)
1574 ctrl
&= ~LEFT_CTRL_PRESSED
;
1575 set_key_input_record( &records
[count
++], 0, VK_CONTROL
, FALSE
, ctrl
);
1577 if (ctrl
& LEFT_ALT_PRESSED
)
1579 ctrl
&= ~LEFT_ALT_PRESSED
;
1580 set_key_input_record( &records
[count
++], 0, VK_MENU
, FALSE
, ctrl
);
1582 if (ctrl
& SHIFT_PRESSED
)
1584 ctrl
&= ~SHIFT_PRESSED
;
1585 set_key_input_record( &records
[count
++], 0, VK_SHIFT
, FALSE
, ctrl
);
1588 return write_console_input( console
, records
, count
, FALSE
);
1591 static void char_key_press( struct console
*console
, WCHAR ch
, unsigned int ctrl
)
1593 unsigned int vk
= VkKeyScanW( ch
);
1594 if (vk
== ~0) vk
= 0;
1595 if (vk
& 0x0100) ctrl
|= SHIFT_PRESSED
;
1596 if (vk
& 0x0200) ctrl
|= LEFT_CTRL_PRESSED
;
1597 if (vk
& 0x0400) ctrl
|= LEFT_ALT_PRESSED
;
1599 key_press( console
, ch
, vk
, ctrl
);
1602 static unsigned int escape_char_to_vk( WCHAR ch
, unsigned int *ctrl
, WCHAR
*outuch
)
1604 if (ctrl
) *ctrl
= 0;
1605 if (outuch
) *outuch
= '\0';
1609 case 'A': return VK_UP
;
1610 case 'B': return VK_DOWN
;
1611 case 'C': return VK_RIGHT
;
1612 case 'D': return VK_LEFT
;
1613 case 'H': return VK_HOME
;
1614 case 'F': return VK_END
;
1615 case 'P': return VK_F1
;
1616 case 'Q': return VK_F2
;
1617 case 'R': return VK_F3
;
1618 case 'S': return VK_F4
;
1619 case 'Z': if (ctrl
&& outuch
) {*ctrl
= SHIFT_PRESSED
; *outuch
= '\t'; return VK_TAB
;}
1625 static unsigned int escape_number_to_vk( unsigned int n
)
1629 case 2: return VK_INSERT
;
1630 case 3: return VK_DELETE
;
1631 case 5: return VK_PRIOR
;
1632 case 6: return VK_NEXT
;
1633 case 15: return VK_F5
;
1634 case 17: return VK_F6
;
1635 case 18: return VK_F7
;
1636 case 19: return VK_F8
;
1637 case 20: return VK_F9
;
1638 case 21: return VK_F10
;
1639 case 23: return VK_F11
;
1640 case 24: return VK_F12
;
1645 static unsigned int convert_modifiers( unsigned int n
)
1647 unsigned int ctrl
= 0;
1648 if (!n
|| n
> 16) return 0;
1650 if (n
& 1) ctrl
|= SHIFT_PRESSED
;
1651 if (n
& 2) ctrl
|= LEFT_ALT_PRESSED
;
1652 if (n
& 4) ctrl
|= LEFT_CTRL_PRESSED
;
1656 static unsigned int process_csi_sequence( struct console
*console
, const WCHAR
*buf
, size_t size
)
1658 unsigned int n
, count
= 0, params
[8], params_cnt
= 0, vk
, ctrl
;
1664 while (count
< size
&& '0' <= buf
[count
] && buf
[count
] <= '9')
1665 n
= n
* 10 + buf
[count
++] - '0';
1666 if (params_cnt
< ARRAY_SIZE(params
)) params
[params_cnt
++] = n
;
1667 else FIXME( "too many params, skipping %u\n", n
);
1668 if (count
== size
) return 0;
1669 if (buf
[count
] != ';') break;
1670 if (++count
== size
) return 0;
1673 if ((vk
= escape_char_to_vk( buf
[count
], &ctrl
, &outuch
)))
1675 key_press( console
, outuch
, vk
, params_cnt
>= 2 ? convert_modifiers( params
[1] ) : ctrl
);
1682 vk
= escape_number_to_vk( params
[0] );
1683 key_press( console
, 0, vk
, params_cnt
== 2 ? convert_modifiers( params
[1] ) : 0 );
1687 FIXME( "unhandled sequence %s\n", debugstr_wn( buf
, size
));
1692 static unsigned int process_input_escape( struct console
*console
, const WCHAR
*buf
, size_t size
)
1694 unsigned int vk
= 0, count
= 0, nlen
;
1698 key_press( console
, 0, VK_ESCAPE
, 0 );
1705 if (++count
== size
) break;
1706 if ((nlen
= process_csi_sequence( console
, buf
+ 1, size
- 1 ))) return count
+ nlen
;
1710 if (++count
== size
) break;
1711 vk
= escape_char_to_vk( buf
[1], NULL
, NULL
);
1714 key_press( console
, 0, vk
, 0 );
1719 char_key_press( console
, buf
[0], LEFT_ALT_PRESSED
);
1723 static DWORD WINAPI
tty_input( void *param
)
1725 struct console
*console
= param
;
1728 char read_buf
[4096];
1734 if (console
->is_unix
)
1736 unsigned int h
= condrv_handle( console
->tty_input
);
1737 status
= NtDeviceIoControlFile( console
->server
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_SETUP_INPUT
,
1738 &h
, sizeof(h
), NULL
, 0 );
1739 if (status
) ERR( "input setup failed: %#lx\n", status
);
1742 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1746 status
= NtReadFile( console
->tty_input
, event
, NULL
, NULL
, &io
, read_buf
, sizeof(read_buf
), NULL
, NULL
);
1747 if (status
== STATUS_PENDING
)
1749 if ((status
= NtWaitForSingleObject( event
, FALSE
, NULL
))) break;
1754 EnterCriticalSection( &console_section
);
1755 signaled
= console
->record_count
!= 0;
1757 /* FIXME: Handle partial char read */
1758 count
= MultiByteToWideChar( get_tty_cp( console
), 0, read_buf
, io
.Information
, buf
, ARRAY_SIZE(buf
) );
1760 TRACE( "%s\n", debugstr_wn(buf
, count
) );
1762 for (i
= 0; i
< count
; i
++)
1767 case 3: /* end of text */
1768 if (console
->is_unix
&& (console
->mode
& ENABLE_PROCESSED_INPUT
))
1770 key_press( console
, ch
, 'C', LEFT_CTRL_PRESSED
);
1773 LeaveCriticalSection( &console_section
);
1776 key_press( console
, '\n', VK_RETURN
, LEFT_CTRL_PRESSED
);
1779 key_press( console
, ch
, 'H', LEFT_CTRL_PRESSED
);
1782 i
+= process_input_escape( console
, buf
+ i
+ 1, count
- i
- 1 );
1784 case 0x1c: /* map ctrl-\ unix-ism into ctrl-break/pause windows-ism for unix consoles */
1785 if (console
->is_unix
)
1786 key_press( console
, 0, VK_CANCEL
, LEFT_CTRL_PRESSED
);
1788 char_key_press( console
, ch
, 0 );
1791 key_press( console
, '\b', VK_BACK
, 0 );
1794 char_key_press( console
, ch
, 0 );
1798 process_console_input( console
);
1799 if (!signaled
&& console
->record_count
)
1801 assert( !console
->read_ioctl
);
1802 read_complete( console
, STATUS_SUCCESS
, NULL
, 0, TRUE
); /* signal console */
1804 LeaveCriticalSection( &console_section
);
1807 TRACE( "NtReadFile failed: %#lx\n", status
);
1810 EnterCriticalSection( &console_section
);
1811 if (console
->read_ioctl
) read_complete( console
, status
, NULL
, 0, FALSE
);
1812 if (console
->is_unix
)
1815 status
= NtDeviceIoControlFile( console
->server
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_SETUP_INPUT
,
1816 &h
, sizeof(h
), NULL
, 0 );
1817 if (status
) ERR( "input restore failed: %#lx\n", status
);
1819 CloseHandle( console
->input_thread
);
1820 console
->input_thread
= NULL
;
1821 LeaveCriticalSection( &console_section
);
1826 static BOOL
ensure_tty_input_thread( struct console
*console
)
1828 if (!console
->tty_input
) return TRUE
;
1829 if (!console
->input_thread
)
1830 console
->input_thread
= CreateThread( NULL
, 0, tty_input
, console
, 0, NULL
);
1831 return console
->input_thread
!= NULL
;
1834 static NTSTATUS
screen_buffer_activate( struct screen_buffer
*screen_buffer
)
1837 TRACE( "%p\n", screen_buffer
);
1838 screen_buffer
->console
->active
= screen_buffer
;
1839 SetRect( &update_rect
, 0, 0, screen_buffer
->width
- 1, screen_buffer
->height
- 1 );
1840 update_output( screen_buffer
, &update_rect
);
1841 tty_sync( screen_buffer
->console
);
1842 update_window_config( screen_buffer
->console
, FALSE
);
1843 return STATUS_SUCCESS
;
1846 static NTSTATUS
get_output_info( struct screen_buffer
*screen_buffer
, size_t *out_size
)
1848 struct condrv_output_info
*info
;
1850 *out_size
= min( *out_size
, sizeof(*info
) + screen_buffer
->font
.face_len
* sizeof(WCHAR
) );
1851 if (!(info
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
1853 info
->cursor_size
= screen_buffer
->cursor_size
;
1854 info
->cursor_visible
= screen_buffer
->cursor_visible
;
1855 info
->cursor_x
= get_bounded_cursor_x( screen_buffer
);
1856 info
->cursor_y
= screen_buffer
->cursor_y
;
1857 info
->width
= screen_buffer
->width
;
1858 info
->height
= screen_buffer
->height
;
1859 info
->attr
= screen_buffer
->attr
;
1860 info
->popup_attr
= screen_buffer
->popup_attr
;
1861 info
->win_left
= screen_buffer
->win
.left
;
1862 info
->win_top
= screen_buffer
->win
.top
;
1863 info
->win_right
= screen_buffer
->win
.right
;
1864 info
->win_bottom
= screen_buffer
->win
.bottom
;
1865 info
->max_width
= screen_buffer
->max_width
;
1866 info
->max_height
= screen_buffer
->max_height
;
1867 info
->font_width
= screen_buffer
->font
.width
;
1868 info
->font_height
= screen_buffer
->font
.height
;
1869 info
->font_weight
= screen_buffer
->font
.weight
;
1870 info
->font_pitch_family
= screen_buffer
->font
.pitch_family
;
1871 memcpy( info
->color_map
, screen_buffer
->color_map
, sizeof(info
->color_map
) );
1872 if (*out_size
> sizeof(*info
)) memcpy( info
+ 1, screen_buffer
->font
.face_name
, *out_size
- sizeof(*info
) );
1874 TRACE( "%p cursor_size=%u cursor_visible=%x cursor=(%u,%u) width=%u height=%u win=%s attr=%x popup_attr=%x"
1875 " font_width=%u font_height=%u %s\n", screen_buffer
, info
->cursor_size
, info
->cursor_visible
,
1876 info
->cursor_x
, info
->cursor_y
, info
->width
, info
->height
, wine_dbgstr_rect(&screen_buffer
->win
),
1877 info
->attr
, info
->popup_attr
, info
->font_width
, info
->font_height
,
1878 debugstr_wn( (const WCHAR
*)(info
+ 1), (*out_size
- sizeof(*info
)) / sizeof(WCHAR
) ) );
1879 return STATUS_SUCCESS
;
1882 void notify_screen_buffer_size( struct screen_buffer
*screen_buffer
)
1884 if (is_active( screen_buffer
) && screen_buffer
->console
->mode
& ENABLE_WINDOW_INPUT
)
1887 ir
.EventType
= WINDOW_BUFFER_SIZE_EVENT
;
1888 ir
.Event
.WindowBufferSizeEvent
.dwSize
.X
= screen_buffer
->width
;
1889 ir
.Event
.WindowBufferSizeEvent
.dwSize
.Y
= screen_buffer
->height
;
1890 write_console_input( screen_buffer
->console
, &ir
, 1, TRUE
);
1894 NTSTATUS
change_screen_buffer_size( struct screen_buffer
*screen_buffer
, int new_width
, int new_height
)
1896 int i
, old_width
, old_height
, copy_width
, copy_height
;
1897 char_info_t
*new_data
;
1899 if (!(new_data
= malloc( new_width
* new_height
* sizeof(*new_data
) ))) return STATUS_NO_MEMORY
;
1901 old_width
= screen_buffer
->width
;
1902 old_height
= screen_buffer
->height
;
1903 copy_width
= min( old_width
, new_width
);
1904 copy_height
= min( old_height
, new_height
);
1906 /* copy all the rows */
1907 for (i
= 0; i
< copy_height
; i
++)
1909 memcpy( &new_data
[i
* new_width
], &screen_buffer
->data
[i
* old_width
],
1910 copy_width
* sizeof(char_info_t
) );
1913 /* clear the end of each row */
1914 if (new_width
> old_width
)
1916 /* fill first row */
1917 for (i
= old_width
; i
< new_width
; i
++) new_data
[i
] = empty_char_info
;
1918 /* and blast it to the other rows */
1919 for (i
= 1; i
< copy_height
; i
++)
1920 memcpy( &new_data
[i
* new_width
+ old_width
], &new_data
[old_width
],
1921 (new_width
- old_width
) * sizeof(char_info_t
) );
1924 /* clear remaining rows */
1925 if (new_height
> old_height
)
1927 /* fill first row */
1928 for (i
= 0; i
< new_width
; i
++) new_data
[old_height
* new_width
+ i
] = empty_char_info
;
1929 /* and blast it to the other rows */
1930 for (i
= old_height
+1; i
< new_height
; i
++)
1931 memcpy( &new_data
[i
* new_width
], &new_data
[old_height
* new_width
],
1932 new_width
* sizeof(char_info_t
) );
1934 free( screen_buffer
->data
);
1935 screen_buffer
->data
= new_data
;
1936 screen_buffer
->width
= new_width
;
1937 screen_buffer
->height
= new_height
;
1938 return STATUS_SUCCESS
;
1941 static NTSTATUS
set_output_info( struct screen_buffer
*screen_buffer
,
1942 const struct condrv_output_info_params
*params
, size_t in_size
)
1944 const struct condrv_output_info
*info
= ¶ms
->info
;
1947 TRACE( "%p\n", screen_buffer
);
1949 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
)
1951 if (info
->cursor_size
< 1 || info
->cursor_size
> 100) return STATUS_INVALID_PARAMETER
;
1953 screen_buffer
->cursor_size
= info
->cursor_size
;
1954 screen_buffer
->cursor_visible
= !!info
->cursor_visible
;
1956 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
)
1958 if (info
->cursor_x
< 0 || info
->cursor_x
>= screen_buffer
->width
||
1959 info
->cursor_y
< 0 || info
->cursor_y
>= screen_buffer
->height
)
1961 return STATUS_INVALID_PARAMETER
;
1964 if (screen_buffer
->cursor_x
!= info
->cursor_x
|| screen_buffer
->cursor_y
!= info
->cursor_y
)
1966 struct console
*console
= screen_buffer
->console
;
1967 screen_buffer
->cursor_x
= info
->cursor_x
;
1968 screen_buffer
->cursor_y
= info
->cursor_y
;
1969 if (console
->use_relative_cursor
)
1970 set_tty_cursor_relative( console
, screen_buffer
->cursor_x
, screen_buffer
->cursor_y
);
1971 scroll_to_cursor( screen_buffer
);
1974 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_SIZE
)
1976 enter_absolute_mode( screen_buffer
->console
);
1977 /* new screen-buffer cannot be smaller than actual window */
1978 if (info
->width
< screen_buffer
->win
.right
- screen_buffer
->win
.left
+ 1 ||
1979 info
->height
< screen_buffer
->win
.bottom
- screen_buffer
->win
.top
+ 1)
1981 return STATUS_INVALID_PARAMETER
;
1983 /* FIXME: there are also some basic minimum and max size to deal with */
1984 if ((status
= change_screen_buffer_size( screen_buffer
, info
->width
, info
->height
))) return status
;
1986 /* scroll window to display sb */
1987 if (screen_buffer
->win
.right
>= info
->width
)
1989 screen_buffer
->win
.right
-= screen_buffer
->win
.left
;
1990 screen_buffer
->win
.left
= 0;
1992 if (screen_buffer
->win
.bottom
>= info
->height
)
1994 screen_buffer
->win
.bottom
-= screen_buffer
->win
.top
;
1995 screen_buffer
->win
.top
= 0;
1997 if (screen_buffer
->cursor_x
>= info
->width
) screen_buffer
->cursor_x
= info
->width
- 1;
1998 if (screen_buffer
->cursor_y
>= info
->height
) screen_buffer
->cursor_y
= info
->height
- 1;
2000 notify_screen_buffer_size( screen_buffer
);
2002 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_ATTR
)
2004 screen_buffer
->attr
= info
->attr
;
2006 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR
)
2008 screen_buffer
->popup_attr
= info
->popup_attr
;
2010 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
)
2012 enter_absolute_mode( screen_buffer
->console
);
2013 if (info
->win_left
< 0 || info
->win_left
> info
->win_right
||
2014 info
->win_right
>= screen_buffer
->width
||
2015 info
->win_top
< 0 || info
->win_top
> info
->win_bottom
||
2016 info
->win_bottom
>= screen_buffer
->height
)
2018 return STATUS_INVALID_PARAMETER
;
2020 if (screen_buffer
->win
.left
!= info
->win_left
|| screen_buffer
->win
.top
!= info
->win_top
||
2021 screen_buffer
->win
.right
!= info
->win_right
|| screen_buffer
->win
.bottom
!= info
->win_bottom
)
2023 screen_buffer
->win
.left
= info
->win_left
;
2024 screen_buffer
->win
.top
= info
->win_top
;
2025 screen_buffer
->win
.right
= info
->win_right
;
2026 screen_buffer
->win
.bottom
= info
->win_bottom
;
2029 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_MAX_SIZE
)
2031 enter_absolute_mode( screen_buffer
->console
);
2032 screen_buffer
->max_width
= info
->max_width
;
2033 screen_buffer
->max_height
= info
->max_height
;
2035 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_FONT
)
2037 WCHAR
*face_name
= (WCHAR
*)(params
+ 1);
2038 size_t face_name_size
= in_size
- sizeof(*params
);
2039 unsigned int height
= info
->font_height
;
2040 unsigned int weight
= FW_NORMAL
;
2042 if (!face_name_size
)
2044 face_name
= screen_buffer
->font
.face_name
;
2045 face_name_size
= screen_buffer
->font
.face_len
* sizeof(WCHAR
);
2048 if (!height
) height
= 12;
2049 if (info
->font_weight
>= FW_SEMIBOLD
) weight
= FW_BOLD
;
2051 update_console_font( screen_buffer
->console
, face_name
, face_name_size
, height
, weight
);
2054 if (is_active( screen_buffer
))
2056 tty_sync( screen_buffer
->console
);
2057 update_window_config( screen_buffer
->console
, FALSE
);
2059 return STATUS_SUCCESS
;
2062 static NTSTATUS
write_console( struct screen_buffer
*screen_buffer
, const WCHAR
*buffer
, size_t len
)
2067 TRACE( "%s\n", debugstr_wn(buffer
, len
) );
2069 empty_update_rect( screen_buffer
, &update_rect
);
2071 for (i
= 0; i
< len
; i
++)
2073 if (screen_buffer
->mode
& ENABLE_PROCESSED_OUTPUT
)
2078 screen_buffer
->cursor_x
= get_bounded_cursor_x( screen_buffer
);
2079 if (screen_buffer
->cursor_x
) screen_buffer
->cursor_x
--;
2082 j
= min( screen_buffer
->width
- screen_buffer
->cursor_x
, 8 - (screen_buffer
->cursor_x
% 8) );
2084 while (j
--) write_char( screen_buffer
, ' ', &update_rect
, NULL
);
2087 screen_buffer
->cursor_x
= 0;
2088 if (++screen_buffer
->cursor_y
== screen_buffer
->height
)
2089 new_line( screen_buffer
, &update_rect
);
2090 else if (screen_buffer
->mode
& ENABLE_WRAP_AT_EOL_OUTPUT
)
2092 update_output( screen_buffer
, &update_rect
);
2093 set_tty_cursor( screen_buffer
->console
, screen_buffer
->cursor_x
, screen_buffer
->cursor_y
);
2100 screen_buffer
->cursor_x
= 0;
2104 if (screen_buffer
->cursor_x
== screen_buffer
->width
&& !(screen_buffer
->mode
& ENABLE_WRAP_AT_EOL_OUTPUT
))
2105 screen_buffer
->cursor_x
= update_rect
.left
;
2106 write_char( screen_buffer
, buffer
[i
], &update_rect
, NULL
);
2109 if (screen_buffer
->cursor_x
== screen_buffer
->width
)
2111 if (screen_buffer
->mode
& ENABLE_WRAP_AT_EOL_OUTPUT
)
2113 if (!(screen_buffer
->mode
& ENABLE_VIRTUAL_TERMINAL_PROCESSING
))
2115 screen_buffer
->cursor_x
= 0;
2116 if (++screen_buffer
->cursor_y
== screen_buffer
->height
)
2117 new_line( screen_buffer
, &update_rect
);
2120 else screen_buffer
->cursor_x
= update_rect
.left
;
2123 scroll_to_cursor( screen_buffer
);
2124 update_output( screen_buffer
, &update_rect
);
2125 tty_sync( screen_buffer
->console
);
2126 update_window_config( screen_buffer
->console
, TRUE
);
2127 return STATUS_SUCCESS
;
2130 static NTSTATUS
write_output( struct screen_buffer
*screen_buffer
, const struct condrv_output_params
*params
,
2131 size_t in_size
, size_t *out_size
)
2133 unsigned int i
, entry_size
, entry_cnt
, x
, y
;
2137 enter_absolute_mode( screen_buffer
->console
);
2138 if (*out_size
== sizeof(SMALL_RECT
) && !params
->width
) return STATUS_INVALID_PARAMETER
;
2140 entry_size
= params
->mode
== CHAR_INFO_MODE_TEXTATTR
? sizeof(char_info_t
) : sizeof(WCHAR
);
2141 entry_cnt
= (in_size
- sizeof(*params
)) / entry_size
;
2143 TRACE( "(%u,%u) cnt %u\n", params
->x
, params
->y
, entry_cnt
);
2145 if (params
->x
>= screen_buffer
->width
)
2148 return STATUS_SUCCESS
;
2151 for (i
= 0, src
= (char *)(params
+ 1); i
< entry_cnt
; i
++, src
+= entry_size
)
2155 x
= params
->x
+ i
% params
->width
;
2156 y
= params
->y
+ i
/ params
->width
;
2157 if (x
>= screen_buffer
->width
) continue;
2161 x
= (params
->x
+ i
) % screen_buffer
->width
;
2162 y
= params
->y
+ (params
->x
+ i
) / screen_buffer
->width
;
2164 if (y
>= screen_buffer
->height
) break;
2166 dest
= &screen_buffer
->data
[y
* screen_buffer
->width
+ x
];
2167 switch(params
->mode
)
2169 case CHAR_INFO_MODE_TEXT
:
2170 dest
->ch
= *(const WCHAR
*)src
;
2172 case CHAR_INFO_MODE_ATTR
:
2173 dest
->attr
= *(const unsigned short *)src
;
2175 case CHAR_INFO_MODE_TEXTATTR
:
2176 *dest
= *(const char_info_t
*)src
;
2179 return STATUS_INVALID_PARAMETER
;
2183 if (i
&& is_active( screen_buffer
))
2187 update_rect
.left
= params
->x
;
2188 update_rect
.top
= params
->y
;
2191 update_rect
.bottom
= min( params
->y
+ entry_cnt
/ params
->width
, screen_buffer
->height
) - 1;
2192 update_rect
.right
= min( params
->x
+ params
->width
, screen_buffer
->width
) - 1;
2196 update_rect
.bottom
= params
->y
+ (params
->x
+ i
- 1) / screen_buffer
->width
;
2197 if (update_rect
.bottom
!= params
->y
)
2199 update_rect
.left
= 0;
2200 update_rect
.right
= screen_buffer
->width
- 1;
2204 update_rect
.right
= params
->x
+ i
- 1;
2207 update_output( screen_buffer
, &update_rect
);
2208 tty_sync( screen_buffer
->console
);
2211 if (*out_size
== sizeof(SMALL_RECT
))
2214 unsigned int width
= params
->width
;
2217 if (!(region
= alloc_ioctl_buffer( sizeof(*region
)))) return STATUS_NO_MEMORY
;
2220 region
->Right
= min( x
+ width
, screen_buffer
->width
) - 1;
2221 region
->Bottom
= min( y
+ entry_cnt
/ width
, screen_buffer
->height
) - 1;
2226 if (!(result
= alloc_ioctl_buffer( sizeof(*result
)))) return STATUS_NO_MEMORY
;
2230 return STATUS_SUCCESS
;
2233 static NTSTATUS
read_output( struct screen_buffer
*screen_buffer
, const struct condrv_output_params
*params
,
2236 enum char_info_mode mode
;
2237 unsigned int x
, y
, width
;
2238 unsigned int i
, count
;
2240 enter_absolute_mode( screen_buffer
->console
);
2243 mode
= params
->mode
;
2244 width
= params
->width
;
2245 TRACE( "(%u %u) mode %u width %u\n", x
, y
, mode
, width
);
2249 case CHAR_INFO_MODE_TEXT
:
2253 if (x
>= screen_buffer
->width
|| y
>= screen_buffer
->height
)
2256 return STATUS_SUCCESS
;
2258 src
= screen_buffer
->data
+ y
* screen_buffer
->width
+ x
;
2259 count
= min( screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
- src
,
2260 *out_size
/ sizeof(*data
) );
2261 *out_size
= count
* sizeof(*data
);
2262 if (!(data
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2263 for (i
= 0; i
< count
; i
++) data
[i
] = src
[i
].ch
;
2266 case CHAR_INFO_MODE_ATTR
:
2268 unsigned short *data
;
2270 if (x
>= screen_buffer
->width
|| y
>= screen_buffer
->height
)
2273 return STATUS_SUCCESS
;
2275 src
= screen_buffer
->data
+ y
* screen_buffer
->width
+ x
;
2276 count
= min( screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
- src
,
2277 *out_size
/ sizeof(*data
) );
2278 *out_size
= count
* sizeof(*data
);
2279 if (!(data
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2280 for (i
= 0; i
< count
; i
++) data
[i
] = src
[i
].attr
;
2283 case CHAR_INFO_MODE_TEXTATTR
:
2287 if (!width
|| *out_size
< sizeof(*region
) || x
>= screen_buffer
->width
|| y
>= screen_buffer
->height
)
2288 return STATUS_INVALID_PARAMETER
;
2289 count
= min( (*out_size
- sizeof(*region
)) / (width
* sizeof(*data
)), screen_buffer
->height
- y
);
2290 width
= min( width
, screen_buffer
->width
- x
);
2291 *out_size
= sizeof(*region
) + width
* count
* sizeof(*data
);
2292 if (!(region
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2295 region
->Right
= x
+ width
- 1;
2296 region
->Bottom
= y
+ count
- 1;
2297 data
= (char_info_t
*)(region
+ 1);
2298 for (i
= 0; i
< count
; i
++)
2300 memcpy( &data
[i
* width
], &screen_buffer
->data
[(y
+ i
) * screen_buffer
->width
+ x
],
2301 width
* sizeof(*data
) );
2306 return STATUS_INVALID_PARAMETER
;
2309 return STATUS_SUCCESS
;
2312 static NTSTATUS
fill_output( struct screen_buffer
*screen_buffer
, const struct condrv_fill_output_params
*params
)
2314 char_info_t
*end
, *dest
;
2315 DWORD i
, count
, *result
;
2317 TRACE( "(%u %u) mode %u\n", params
->x
, params
->y
, params
->mode
);
2319 enter_absolute_mode( screen_buffer
->console
);
2320 if (params
->y
>= screen_buffer
->height
) return STATUS_SUCCESS
;
2321 dest
= screen_buffer
->data
+ min( params
->y
* screen_buffer
->width
+ params
->x
,
2322 screen_buffer
->height
* screen_buffer
->width
);
2324 end
= screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
;
2326 count
= params
->count
;
2327 if (count
> end
- dest
) count
= end
- dest
;
2329 switch(params
->mode
)
2331 case CHAR_INFO_MODE_TEXT
:
2332 for (i
= 0; i
< count
; i
++) dest
[i
].ch
= params
->ch
;
2334 case CHAR_INFO_MODE_ATTR
:
2335 for (i
= 0; i
< count
; i
++) dest
[i
].attr
= params
->attr
;
2337 case CHAR_INFO_MODE_TEXTATTR
:
2338 for (i
= 0; i
< count
; i
++)
2340 dest
[i
].ch
= params
->ch
;
2341 dest
[i
].attr
= params
->attr
;
2345 return STATUS_INVALID_PARAMETER
;
2348 if (count
&& is_active(screen_buffer
))
2351 SetRect( &update_rect
,
2352 params
->x
% screen_buffer
->width
,
2353 params
->y
+ params
->x
/ screen_buffer
->width
,
2354 (params
->x
+ i
- 1) % screen_buffer
->width
,
2355 params
->y
+ (params
->x
+ i
- 1) / screen_buffer
->width
);
2356 update_output( screen_buffer
, &update_rect
);
2357 tty_sync( screen_buffer
->console
);
2360 if (!(result
= alloc_ioctl_buffer( sizeof(*result
) ))) return STATUS_NO_MEMORY
;
2362 return STATUS_SUCCESS
;
2365 static NTSTATUS
scroll_output( struct screen_buffer
*screen_buffer
, const struct condrv_scroll_params
*params
)
2367 int x
, y
, xsrc
, ysrc
, w
, h
;
2368 char_info_t
*psrc
, *pdst
;
2369 SMALL_RECT src
, dst
;
2373 enter_absolute_mode( screen_buffer
->console
);
2374 xsrc
= params
->scroll
.Left
;
2375 ysrc
= params
->scroll
.Top
;
2376 w
= params
->scroll
.Right
- params
->scroll
.Left
+ 1;
2377 h
= params
->scroll
.Bottom
- params
->scroll
.Top
+ 1;
2379 TRACE( "(%d %d) -> (%u %u) w %u h %u\n", xsrc
, ysrc
, params
->origin
.X
, params
->origin
.Y
, w
, h
);
2381 clip
.Left
= max( params
->clip
.Left
, 0 );
2382 clip
.Top
= max( params
->clip
.Top
, 0 );
2383 clip
.Right
= min( params
->clip
.Right
, screen_buffer
->width
- 1 );
2384 clip
.Bottom
= min( params
->clip
.Bottom
, screen_buffer
->height
- 1 );
2385 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
|| params
->scroll
.Left
< 0 || params
->scroll
.Top
< 0 ||
2386 params
->scroll
.Right
>= screen_buffer
->width
|| params
->scroll
.Bottom
>= screen_buffer
->height
||
2387 params
->scroll
.Right
< params
->scroll
.Left
|| params
->scroll
.Top
> params
->scroll
.Bottom
||
2388 params
->origin
.X
< 0 || params
->origin
.X
>= screen_buffer
->width
|| params
->origin
.Y
< 0 ||
2389 params
->origin
.Y
>= screen_buffer
->height
)
2390 return STATUS_INVALID_PARAMETER
;
2392 src
.Left
= max( xsrc
, clip
.Left
);
2393 src
.Top
= max( ysrc
, clip
.Top
);
2394 src
.Right
= min( xsrc
+ w
- 1, clip
.Right
);
2395 src
.Bottom
= min( ysrc
+ h
- 1, clip
.Bottom
);
2397 dst
.Left
= params
->origin
.X
;
2398 dst
.Top
= params
->origin
.Y
;
2399 dst
.Right
= params
->origin
.X
+ w
- 1;
2400 dst
.Bottom
= params
->origin
.Y
+ h
- 1;
2402 if (dst
.Left
< clip
.Left
)
2404 xsrc
+= clip
.Left
- dst
.Left
;
2405 w
-= clip
.Left
- dst
.Left
;
2406 dst
.Left
= clip
.Left
;
2408 if (dst
.Top
< clip
.Top
)
2410 ysrc
+= clip
.Top
- dst
.Top
;
2411 h
-= clip
.Top
- dst
.Top
;
2414 if (dst
.Right
> clip
.Right
) w
-= dst
.Right
- clip
.Right
;
2415 if (dst
.Bottom
> clip
.Bottom
) h
-= dst
.Bottom
- clip
.Bottom
;
2421 psrc
= &screen_buffer
->data
[(ysrc
+ h
- 1) * screen_buffer
->width
+ xsrc
];
2422 pdst
= &screen_buffer
->data
[(dst
.Top
+ h
- 1) * screen_buffer
->width
+ dst
.Left
];
2424 for (y
= h
; y
> 0; y
--)
2426 memcpy( pdst
, psrc
, w
* sizeof(*pdst
) );
2427 pdst
-= screen_buffer
->width
;
2428 psrc
-= screen_buffer
->width
;
2433 psrc
= &screen_buffer
->data
[ysrc
* screen_buffer
->width
+ xsrc
];
2434 pdst
= &screen_buffer
->data
[dst
.Top
* screen_buffer
->width
+ dst
.Left
];
2436 for (y
= 0; y
< h
; y
++)
2438 /* we use memmove here because when psrc and pdst are the same,
2439 * copies are done on the same row, so the dst and src blocks
2441 memmove( pdst
, psrc
, w
* sizeof(*pdst
) );
2442 pdst
+= screen_buffer
->width
;
2443 psrc
+= screen_buffer
->width
;
2448 for (y
= src
.Top
; y
<= src
.Bottom
; y
++)
2450 int left
= src
.Left
;
2451 int right
= src
.Right
;
2452 if (dst
.Top
<= y
&& y
<= dst
.Bottom
)
2454 if (dst
.Left
<= src
.Left
) left
= max( left
, dst
.Right
+ 1 );
2455 if (dst
.Left
>= src
.Left
) right
= min( right
, dst
.Left
- 1 );
2457 for (x
= left
; x
<= right
; x
++) screen_buffer
->data
[y
* screen_buffer
->width
+ x
] = params
->fill
;
2460 SetRect( &update_rect
, min( src
.Left
, dst
.Left
), min( src
.Top
, dst
.Top
),
2461 max( src
.Right
, dst
.Right
), max( src
.Bottom
, dst
.Bottom
));
2462 update_output( screen_buffer
, &update_rect
);
2463 tty_sync( screen_buffer
->console
);
2464 return STATUS_SUCCESS
;
2467 static WCHAR
*set_title( const WCHAR
*in_title
, size_t size
)
2469 WCHAR
*title
= NULL
;
2471 title
= malloc( size
+ sizeof(WCHAR
) );
2472 if (!title
) return NULL
;
2474 memcpy( title
, in_title
, size
);
2475 title
[ size
/ sizeof(WCHAR
) ] = 0;
2480 static NTSTATUS
set_console_title( struct console
*console
, const WCHAR
*in_title
, size_t size
)
2482 WCHAR
*title
= NULL
;
2484 TRACE( "%s\n", debugstr_wn(in_title
, size
/ sizeof(WCHAR
)) );
2486 if (!(title
= set_title( in_title
, size
)))
2487 return STATUS_NO_MEMORY
;
2489 free( console
->title
);
2490 console
->title
= title
;
2492 if (!console
->title_orig
&& !(console
->title_orig
= set_title( in_title
, size
)))
2494 free( console
->title
);
2495 console
->title
= NULL
;
2496 return STATUS_NO_MEMORY
;
2499 if (console
->tty_output
)
2504 tty_write( console
, "\x1b]0;", 4 );
2505 len
= WideCharToMultiByte( get_tty_cp( console
), 0, console
->title
, size
/ sizeof(WCHAR
),
2506 NULL
, 0, NULL
, NULL
);
2507 if ((vt
= tty_alloc_buffer( console
, len
)))
2508 WideCharToMultiByte( get_tty_cp( console
), 0, console
->title
, size
/ sizeof(WCHAR
),
2509 vt
, len
, NULL
, NULL
);
2510 tty_write( console
, "\x07", 1 );
2511 tty_sync( console
);
2514 SetWindowTextW( console
->win
, console
->title
);
2515 return STATUS_SUCCESS
;
2518 static NTSTATUS
screen_buffer_ioctl( struct screen_buffer
*screen_buffer
, unsigned int code
,
2519 const void *in_data
, size_t in_size
, size_t *out_size
)
2523 case IOCTL_CONDRV_CLOSE_OUTPUT
:
2524 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2525 destroy_screen_buffer( screen_buffer
);
2526 return STATUS_SUCCESS
;
2528 case IOCTL_CONDRV_ACTIVATE
:
2529 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2530 return screen_buffer_activate( screen_buffer
);
2532 case IOCTL_CONDRV_GET_MODE
:
2535 TRACE( "returning mode %x\n", screen_buffer
->mode
);
2536 if (in_size
|| *out_size
!= sizeof(*mode
)) return STATUS_INVALID_PARAMETER
;
2537 if (!(mode
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2538 *mode
= screen_buffer
->mode
;
2539 return STATUS_SUCCESS
;
2542 case IOCTL_CONDRV_SET_MODE
:
2543 if (in_size
!= sizeof(unsigned int) || *out_size
) return STATUS_INVALID_PARAMETER
;
2544 screen_buffer
->mode
= *(unsigned int *)in_data
;
2545 TRACE( "set %x mode\n", screen_buffer
->mode
);
2546 return STATUS_SUCCESS
;
2548 case IOCTL_CONDRV_IS_UNIX
:
2549 return screen_buffer
->console
->is_unix
? STATUS_SUCCESS
: STATUS_NOT_SUPPORTED
;
2551 case IOCTL_CONDRV_WRITE_CONSOLE
:
2552 if (in_size
% sizeof(WCHAR
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2553 return write_console( screen_buffer
, in_data
, in_size
/ sizeof(WCHAR
) );
2555 case IOCTL_CONDRV_WRITE_FILE
:
2561 len
= MultiByteToWideChar( screen_buffer
->console
->output_cp
, 0, in_data
, in_size
,
2563 if (!len
) return STATUS_SUCCESS
;
2564 if (!(buf
= malloc( len
* sizeof(WCHAR
) ))) return STATUS_NO_MEMORY
;
2565 MultiByteToWideChar( screen_buffer
->console
->output_cp
, 0, in_data
, in_size
, buf
, len
);
2566 status
= write_console( screen_buffer
, buf
, len
);
2571 case IOCTL_CONDRV_WRITE_OUTPUT
:
2572 if ((*out_size
!= sizeof(DWORD
) && *out_size
!= sizeof(SMALL_RECT
)) ||
2573 in_size
< sizeof(struct condrv_output_params
))
2574 return STATUS_INVALID_PARAMETER
;
2575 return write_output( screen_buffer
, in_data
, in_size
, out_size
);
2577 case IOCTL_CONDRV_READ_OUTPUT
:
2578 if (in_size
!= sizeof(struct condrv_output_params
)) return STATUS_INVALID_PARAMETER
;
2579 return read_output( screen_buffer
, in_data
, out_size
);
2581 case IOCTL_CONDRV_GET_OUTPUT_INFO
:
2582 if (in_size
|| *out_size
< sizeof(struct condrv_output_info
)) return STATUS_INVALID_PARAMETER
;
2583 return get_output_info( screen_buffer
, out_size
);
2585 case IOCTL_CONDRV_SET_OUTPUT_INFO
:
2586 if (in_size
< sizeof(struct condrv_output_info_params
) || *out_size
)
2587 return STATUS_INVALID_PARAMETER
;
2588 return set_output_info( screen_buffer
, in_data
, in_size
);
2590 case IOCTL_CONDRV_FILL_OUTPUT
:
2591 if (in_size
!= sizeof(struct condrv_fill_output_params
) || *out_size
!= sizeof(DWORD
))
2592 return STATUS_INVALID_PARAMETER
;
2593 return fill_output( screen_buffer
, in_data
);
2595 case IOCTL_CONDRV_SCROLL
:
2596 if (in_size
!= sizeof(struct condrv_scroll_params
) || *out_size
)
2597 return STATUS_INVALID_PARAMETER
;
2598 return scroll_output( screen_buffer
, in_data
);
2601 WARN( "invalid ioctl %x\n", code
);
2602 return STATUS_INVALID_HANDLE
;
2606 static NTSTATUS
console_input_ioctl( struct console
*console
, unsigned int code
, const void *in_data
,
2607 size_t in_size
, size_t *out_size
)
2613 case IOCTL_CONDRV_GET_MODE
:
2616 TRACE( "returning mode %x\n", console
->mode
);
2617 if (in_size
|| *out_size
!= sizeof(*mode
)) return STATUS_INVALID_PARAMETER
;
2618 if (!(mode
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2619 *mode
= console
->mode
;
2620 return STATUS_SUCCESS
;
2623 case IOCTL_CONDRV_SET_MODE
:
2624 if (in_size
!= sizeof(unsigned int) || *out_size
) return STATUS_INVALID_PARAMETER
;
2625 console
->mode
= *(unsigned int *)in_data
;
2626 TRACE( "set %x mode\n", console
->mode
);
2627 return STATUS_SUCCESS
;
2629 case IOCTL_CONDRV_IS_UNIX
:
2630 return console
->is_unix
? STATUS_SUCCESS
: STATUS_NOT_SUPPORTED
;
2632 case IOCTL_CONDRV_READ_CONSOLE
:
2633 if (in_size
|| *out_size
% sizeof(WCHAR
)) return STATUS_INVALID_PARAMETER
;
2634 ensure_tty_input_thread( console
);
2635 status
= read_console( console
, code
, *out_size
, NULL
, 0, 0 );
2639 case IOCTL_CONDRV_READ_CONSOLE_CONTROL
:
2640 if ((in_size
< sizeof(DWORD
)) || ((in_size
- sizeof(DWORD
)) % sizeof(WCHAR
)) ||
2641 (*out_size
< sizeof(DWORD
)) || ((*out_size
- sizeof(DWORD
)) % sizeof(WCHAR
)))
2642 return STATUS_INVALID_PARAMETER
;
2643 ensure_tty_input_thread( console
);
2644 status
= read_console( console
, code
, *out_size
- sizeof(DWORD
),
2645 (const WCHAR
*)((const char*)in_data
+ sizeof(DWORD
)),
2646 (in_size
- sizeof(DWORD
)) / sizeof(WCHAR
),
2651 case IOCTL_CONDRV_READ_FILE
:
2652 ensure_tty_input_thread( console
);
2653 status
= read_console( console
, code
, *out_size
, NULL
, 0, 0 );
2657 case IOCTL_CONDRV_READ_INPUT
:
2659 if (in_size
) return STATUS_INVALID_PARAMETER
;
2660 ensure_tty_input_thread( console
);
2661 if (!console
->record_count
&& *out_size
)
2663 TRACE( "pending read\n" );
2664 console
->read_ioctl
= IOCTL_CONDRV_READ_INPUT
;
2665 console
->pending_read
= *out_size
;
2666 return STATUS_PENDING
;
2668 status
= read_console_input( console
, *out_size
);
2673 case IOCTL_CONDRV_WRITE_INPUT
:
2674 if (in_size
% sizeof(INPUT_RECORD
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2675 return write_console_input( console
, in_data
, in_size
/ sizeof(INPUT_RECORD
), TRUE
);
2677 case IOCTL_CONDRV_PEEK
:
2681 if (in_size
) return STATUS_INVALID_PARAMETER
;
2682 ensure_tty_input_thread( console
);
2683 *out_size
= min( *out_size
, console
->record_count
* sizeof(INPUT_RECORD
) );
2684 if (!(result
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2685 if (*out_size
) memcpy( result
, console
->records
, *out_size
);
2686 return STATUS_SUCCESS
;
2689 case IOCTL_CONDRV_GET_INPUT_INFO
:
2691 struct condrv_input_info
*info
;
2692 TRACE( "get info\n" );
2693 if (in_size
|| *out_size
!= sizeof(*info
)) return STATUS_INVALID_PARAMETER
;
2694 if (!(info
= alloc_ioctl_buffer( sizeof(*info
)))) return STATUS_NO_MEMORY
;
2695 info
->input_cp
= console
->input_cp
;
2696 info
->output_cp
= console
->output_cp
;
2697 info
->input_count
= console
->record_count
;
2698 return STATUS_SUCCESS
;
2701 case IOCTL_CONDRV_GET_WINDOW
:
2703 condrv_handle_t
*result
;
2704 TRACE( "get window\n" );
2705 if (in_size
|| *out_size
!= sizeof(*result
)) return STATUS_INVALID_PARAMETER
;
2706 if (!(result
= alloc_ioctl_buffer( sizeof(*result
)))) return STATUS_NO_MEMORY
;
2707 if (!console
->win
&& !console
->no_window
) init_message_window( console
);
2708 *result
= condrv_handle( console
->win
);
2709 return STATUS_SUCCESS
;
2712 case IOCTL_CONDRV_SET_INPUT_INFO
:
2714 const struct condrv_input_info_params
*params
= in_data
;
2715 TRACE( "set info\n" );
2716 if (in_size
!= sizeof(*params
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2717 if (params
->mask
& SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE
)
2719 if (!IsValidCodePage( params
->info
.input_cp
)) return STATUS_INVALID_PARAMETER
;
2720 console
->input_cp
= params
->info
.input_cp
;
2722 if (params
->mask
& SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE
)
2724 if (!IsValidCodePage( params
->info
.output_cp
)) return STATUS_INVALID_PARAMETER
;
2725 console
->output_cp
= params
->info
.output_cp
;
2727 return STATUS_SUCCESS
;
2730 case IOCTL_CONDRV_GET_TITLE
:
2734 size_t title_len
, str_size
;
2735 struct condrv_title_params
*params
;
2736 if (in_size
!= sizeof(BOOL
)) return STATUS_INVALID_PARAMETER
;
2737 current_title
= *(BOOL
*)in_data
;
2738 title
= current_title
? console
->title
: console
->title_orig
;
2739 title_len
= title
? wcslen( title
) : 0;
2740 str_size
= min( *out_size
- sizeof(*params
), title_len
* sizeof(WCHAR
) );
2741 *out_size
= sizeof(*params
) + str_size
;
2742 if (!(params
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2743 TRACE( "returning %s %s\n", current_title
? "title" : "original title", debugstr_w(title
) );
2744 if (str_size
) memcpy( params
->buffer
, title
, str_size
);
2745 params
->title_len
= title_len
;
2746 return STATUS_SUCCESS
;
2749 case IOCTL_CONDRV_SET_TITLE
:
2750 if (in_size
% sizeof(WCHAR
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2751 return set_console_title( console
, in_data
, in_size
);
2753 case IOCTL_CONDRV_BEEP
:
2754 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2755 if (console
->is_unix
)
2757 tty_write( console
, "\a", 1 );
2758 tty_sync( console
);
2760 return STATUS_SUCCESS
;
2762 case IOCTL_CONDRV_FLUSH
:
2763 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2765 console
->record_count
= 0;
2766 return STATUS_SUCCESS
;
2769 WARN( "unsupported ioctl %x\n", code
);
2770 return STATUS_INVALID_HANDLE
;
2774 static NTSTATUS
process_console_ioctls( struct console
*console
)
2776 size_t out_size
= 0, in_size
;
2779 NTSTATUS status
= STATUS_SUCCESS
;
2783 if (status
) out_size
= 0;
2785 console
->signaled
= console
->record_count
!= 0;
2786 SERVER_START_REQ( get_next_console_request
)
2788 req
->handle
= wine_server_obj_handle( console
->server
);
2789 req
->status
= status
;
2790 req
->signal
= console
->signaled
;
2791 wine_server_add_data( req
, ioctl_buffer
, out_size
);
2792 wine_server_set_reply( req
, ioctl_buffer
, ioctl_buffer_size
);
2793 status
= wine_server_call( req
);
2795 output
= reply
->output
;
2796 out_size
= reply
->out_size
;
2797 in_size
= wine_server_reply_size( reply
);
2801 if (status
== STATUS_PENDING
) return STATUS_SUCCESS
;
2802 if (status
== STATUS_BUFFER_OVERFLOW
)
2804 if (!alloc_ioctl_buffer( out_size
)) return STATUS_NO_MEMORY
;
2805 status
= STATUS_SUCCESS
;
2810 TRACE( "failed to get next request: %#lx\n", status
);
2814 if (code
== IOCTL_CONDRV_INIT_OUTPUT
)
2816 TRACE( "initializing output %x\n", output
);
2817 enter_absolute_mode( console
);
2818 if (console
->active
)
2819 create_screen_buffer( console
, output
, console
->active
->width
, console
->active
->height
);
2821 create_screen_buffer( console
, output
, 80, 150 );
2825 status
= console_input_ioctl( console
, code
, ioctl_buffer
, in_size
, &out_size
);
2829 struct wine_rb_entry
*entry
;
2830 if (!(entry
= wine_rb_get( &screen_buffer_map
, LongToPtr(output
) )))
2832 ERR( "invalid screen buffer id %x\n", output
);
2833 status
= STATUS_INVALID_HANDLE
;
2837 status
= screen_buffer_ioctl( WINE_RB_ENTRY_VALUE( entry
, struct screen_buffer
, entry
), code
,
2838 ioctl_buffer
, in_size
, &out_size
);
2844 static int main_loop( struct console
*console
, HANDLE signal
)
2846 HANDLE signal_event
= NULL
;
2847 HANDLE wait_handles
[3];
2848 unsigned int wait_cnt
= 0;
2849 unsigned short signal_id
;
2850 IO_STATUS_BLOCK signal_io
;
2856 if (!(signal_event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
))) return 1;
2857 status
= NtReadFile( signal
, signal_event
, NULL
, NULL
, &signal_io
, &signal_id
,
2858 sizeof(signal_id
), NULL
, NULL
);
2859 if (status
&& status
!= STATUS_PENDING
) return 1;
2862 if (!alloc_ioctl_buffer( 4096 )) return 1;
2864 wait_handles
[wait_cnt
++] = console
->server
;
2865 if (signal
) wait_handles
[wait_cnt
++] = signal_event
;
2866 if (console
->input_thread
) wait_handles
[wait_cnt
++] = console
->input_thread
;
2871 res
= MsgWaitForMultipleObjects( wait_cnt
, wait_handles
, FALSE
, INFINITE
, QS_ALLINPUT
);
2873 res
= WaitForMultipleObjects( wait_cnt
, wait_handles
, FALSE
, INFINITE
);
2875 if (res
== WAIT_OBJECT_0
+ wait_cnt
)
2878 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
2880 if (msg
.message
== WM_QUIT
) return 0;
2881 DispatchMessageW(&msg
);
2889 EnterCriticalSection( &console_section
);
2890 status
= process_console_ioctls( console
);
2891 LeaveCriticalSection( &console_section
);
2892 if (status
) return 0;
2895 case WAIT_OBJECT_0
+ 1:
2896 if (signal_io
.Status
|| signal_io
.Information
!= sizeof(signal_id
))
2898 TRACE( "signaled quit\n" );
2901 FIXME( "unimplemented signal %x\n", signal_id
);
2902 status
= NtReadFile( signal
, signal_event
, NULL
, NULL
, &signal_io
, &signal_id
,
2903 sizeof(signal_id
), NULL
, NULL
);
2904 if (status
&& status
!= STATUS_PENDING
) return 1;
2908 TRACE( "wait failed, quit\n");
2916 static void teardown( struct console
*console
)
2918 if (console
->is_unix
)
2920 set_tty_attr( console
, empty_char_info
.attr
);
2921 tty_flush( console
);
2925 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
2927 int headless
= 0, i
, width
= 0, height
= 0, ret
;
2928 HANDLE signal
= NULL
;
2931 static struct console console
;
2933 for (i
= 0; i
< argc
; i
++) TRACE("%s ", wine_dbgstr_w(argv
[i
]));
2936 console
.mode
= ENABLE_PROCESSED_INPUT
| ENABLE_LINE_INPUT
|
2937 ENABLE_ECHO_INPUT
| ENABLE_MOUSE_INPUT
| ENABLE_INSERT_MODE
|
2938 ENABLE_QUICK_EDIT_MODE
| ENABLE_EXTENDED_FLAGS
| ENABLE_AUTO_POSITION
;
2939 console
.input_cp
= console
.output_cp
= GetOEMCP();
2940 console
.history_size
= 50;
2941 if (!(console
.history
= calloc( console
.history_size
, sizeof(*console
.history
) ))) return 1;
2943 for (i
= 1; i
< argc
; i
++)
2945 if (!wcscmp( argv
[i
], L
"--headless"))
2950 if (!wcscmp( argv
[i
], L
"--unix"))
2952 console
.is_unix
= 1;
2953 console
.use_relative_cursor
= 1;
2957 if (!wcscmp( argv
[i
], L
"--width" ))
2959 if (++i
== argc
) return 1;
2960 width
= wcstol( argv
[i
], &end
, 0 );
2961 if ((!width
&& !console
.is_unix
) || width
> 0xffff || *end
) return 1;
2964 if (!wcscmp( argv
[i
], L
"--height" ))
2966 if (++i
== argc
) return 1;
2967 height
= wcstol( argv
[i
], &end
, 0 );
2968 if ((!height
&& !console
.is_unix
) || height
> 0xffff || *end
) return 1;
2971 if (!wcscmp( argv
[i
], L
"--signal" ))
2973 if (++i
== argc
) return 1;
2974 signal
= ULongToHandle( wcstol( argv
[i
], &end
, 0 ));
2978 if (!wcscmp( argv
[i
], L
"--server" ))
2980 if (++i
== argc
) return 1;
2981 console
.server
= ULongToHandle( wcstol( argv
[i
], &end
, 0 ));
2985 FIXME( "unknown option %s\n", debugstr_w(argv
[i
]) );
2989 if (!console
.server
)
2991 ERR( "no server handle\n" );
2995 if (!width
) width
= 80;
2996 if (!height
) height
= 150;
2998 if (!(console
.active
= create_screen_buffer( &console
, 1, width
, height
))) return 1;
3001 console
.tty_input
= GetStdHandle( STD_INPUT_HANDLE
);
3002 console
.tty_output
= GetStdHandle( STD_OUTPUT_HANDLE
);
3004 if (console
.tty_input
|| console
.tty_output
)
3006 init_tty_output( &console
);
3007 if (!console
.is_unix
&& !ensure_tty_input_thread( &console
)) return 1;
3009 else console
.no_window
= TRUE
;
3014 if (!init_window( &console
)) return 1;
3015 GetStartupInfoW( &si
);
3016 set_console_title( &console
, si
.lpTitle
, wcslen( si
.lpTitle
) * sizeof(WCHAR
) );
3017 ShowWindow( console
.win
, (si
.dwFlags
& STARTF_USESHOWWINDOW
) ? si
.wShowWindow
: SW_SHOW
);
3020 ret
= main_loop( &console
, signal
);
3021 teardown( &console
);