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
->data
);
72 free( screen_buffer
);
75 static struct screen_buffer
*create_screen_buffer( struct console
*console
, int id
, int width
, int height
)
77 struct screen_buffer
*screen_buffer
;
80 if (!(screen_buffer
= calloc( 1, sizeof(*screen_buffer
) ))) return NULL
;
81 screen_buffer
->console
= console
;
82 screen_buffer
->id
= id
;
83 screen_buffer
->mode
= ENABLE_PROCESSED_OUTPUT
| ENABLE_WRAP_AT_EOL_OUTPUT
;
84 screen_buffer
->cursor_size
= 25;
85 screen_buffer
->cursor_visible
= 1;
86 screen_buffer
->width
= width
;
87 screen_buffer
->height
= height
;
88 screen_buffer
->attr
= 0x07;
89 screen_buffer
->popup_attr
= 0xf5;
90 screen_buffer
->font
.weight
= FW_NORMAL
;
91 screen_buffer
->font
.pitch_family
= FIXED_PITCH
| FF_DONTCARE
;
95 screen_buffer
->max_width
= console
->active
->max_width
;
96 screen_buffer
->max_height
= console
->active
->max_height
;
97 screen_buffer
->win
.right
= console
->active
->win
.right
- console
->active
->win
.left
;
98 screen_buffer
->win
.bottom
= console
->active
->win
.bottom
- console
->active
->win
.top
;
102 screen_buffer
->max_width
= width
;
103 screen_buffer
->max_height
= height
;
104 screen_buffer
->win
.right
= width
- 1;
105 screen_buffer
->win
.bottom
= height
- 1;
108 if (wine_rb_put( &screen_buffer_map
, LongToPtr(id
), &screen_buffer
->entry
))
110 free( screen_buffer
);
111 ERR( "id %x already exists\n", id
);
115 if (!(screen_buffer
->data
= malloc( screen_buffer
->width
* screen_buffer
->height
*
116 sizeof(*screen_buffer
->data
) )))
118 destroy_screen_buffer( screen_buffer
);
122 /* clear the first row */
123 for (i
= 0; i
< screen_buffer
->width
; i
++) screen_buffer
->data
[i
] = empty_char_info
;
124 /* and copy it to all other rows */
125 for (i
= 1; i
< screen_buffer
->height
; i
++)
126 memcpy( &screen_buffer
->data
[i
* screen_buffer
->width
], screen_buffer
->data
,
127 screen_buffer
->width
* sizeof(char_info_t
) );
129 return screen_buffer
;
132 static BOOL
is_active( struct screen_buffer
*screen_buffer
)
134 return screen_buffer
== screen_buffer
->console
->active
;
137 static unsigned int get_tty_cp( struct console
*console
)
139 return console
->is_unix
? CP_UNIXCP
: CP_UTF8
;
142 static void tty_flush( struct console
*console
)
144 if (!console
->tty_output
|| !console
->tty_buffer_count
) return;
145 TRACE("%s\n", debugstr_an(console
->tty_buffer
, console
->tty_buffer_count
));
146 if (!WriteFile( console
->tty_output
, console
->tty_buffer
, console
->tty_buffer_count
,
148 WARN( "write failed: %u\n", GetLastError() );
149 console
->tty_buffer_count
= 0;
152 static void tty_write( struct console
*console
, const char *buffer
, size_t size
)
154 if (!size
|| !console
->tty_output
) return;
155 if (console
->tty_buffer_count
+ size
> sizeof(console
->tty_buffer
))
156 tty_flush( console
);
157 if (console
->tty_buffer_count
+ size
<= sizeof(console
->tty_buffer
))
159 memcpy( console
->tty_buffer
+ console
->tty_buffer_count
, buffer
, size
);
160 console
->tty_buffer_count
+= size
;
164 assert( !console
->tty_buffer_count
);
165 if (!WriteFile( console
->tty_output
, buffer
, size
, NULL
, NULL
))
166 WARN( "write failed: %u\n", GetLastError() );
170 static void *tty_alloc_buffer( struct console
*console
, size_t size
)
173 if (console
->tty_buffer_count
+ size
> sizeof(console
->tty_buffer
)) return NULL
;
174 ret
= console
->tty_buffer
+ console
->tty_buffer_count
;
175 console
->tty_buffer_count
+= size
;
179 static void hide_tty_cursor( struct console
*console
)
181 if (console
->tty_cursor_visible
)
183 tty_write( console
, "\x1b[?25l", 6 );
184 console
->tty_cursor_visible
= FALSE
;
188 static void set_tty_cursor( struct console
*console
, unsigned int x
, unsigned int y
)
192 if (console
->tty_cursor_x
== x
&& console
->tty_cursor_y
== y
) return;
194 if (!x
&& y
== console
->tty_cursor_y
+ 1) strcpy( buf
, "\r\n" );
195 else if (!x
&& y
== console
->tty_cursor_y
) strcpy( buf
, "\r" );
196 else if (y
== console
->tty_cursor_y
)
198 if (x
+ 1 == console
->tty_cursor_x
) strcpy( buf
, "\b" );
199 else if (x
> console
->tty_cursor_x
) sprintf( buf
, "\x1b[%uC", x
- console
->tty_cursor_x
);
200 else sprintf( buf
, "\x1b[%uD", console
->tty_cursor_x
- x
);
204 hide_tty_cursor( console
);
205 sprintf( buf
, "\x1b[%u;%uH", y
+ 1, x
+ 1);
207 else strcpy( buf
, "\x1b[H" );
208 console
->tty_cursor_x
= x
;
209 console
->tty_cursor_y
= y
;
210 tty_write( console
, buf
, strlen(buf
) );
213 static void set_tty_cursor_relative( struct console
*console
, unsigned int x
, unsigned int y
)
215 if (y
< console
->tty_cursor_y
)
218 sprintf( buf
, "\x1b[%uA", console
->tty_cursor_y
- y
);
219 tty_write( console
, buf
, strlen(buf
) );
220 console
->tty_cursor_y
= y
;
224 while (console
->tty_cursor_y
< y
)
226 console
->tty_cursor_x
= 0;
227 console
->tty_cursor_y
++;
228 tty_write( console
, "\r\n", 2 );
231 set_tty_cursor( console
, x
, y
);
234 static void set_tty_attr( struct console
*console
, unsigned int attr
)
238 if ((attr
& 0x0f) != (console
->tty_attr
& 0x0f))
240 if ((attr
& 0x0f) != 7)
243 if (attr
& FOREGROUND_BLUE
) n
+= 4;
244 if (attr
& FOREGROUND_GREEN
) n
+= 2;
245 if (attr
& FOREGROUND_RED
) n
+= 1;
246 if (attr
& FOREGROUND_INTENSITY
) n
+= 60;
247 sprintf(buf
, "\x1b[%um", n
);
248 tty_write( console
, buf
, strlen(buf
) );
250 else tty_write( console
, "\x1b[m", 3 );
253 if ((attr
& 0xf0) != (console
->tty_attr
& 0xf0) && attr
!= 7)
256 if (attr
& BACKGROUND_BLUE
) n
+= 4;
257 if (attr
& BACKGROUND_GREEN
) n
+= 2;
258 if (attr
& BACKGROUND_RED
) n
+= 1;
259 if (attr
& BACKGROUND_INTENSITY
) n
+= 60;
260 sprintf(buf
, "\x1b[%um", n
);
261 tty_write( console
, buf
, strlen(buf
) );
264 console
->tty_attr
= attr
;
267 static void tty_sync( struct console
*console
)
269 if (!console
->tty_output
) return;
271 if (console
->active
->cursor_visible
)
273 set_tty_cursor( console
, console
->active
->cursor_x
, console
->active
->cursor_y
);
274 if (!console
->tty_cursor_visible
)
276 tty_write( console
, "\x1b[?25h", 6 ); /* show cursor */
277 console
->tty_cursor_visible
= TRUE
;
280 else if (console
->tty_cursor_visible
)
281 hide_tty_cursor( console
);
282 tty_flush( console
);
285 static void init_tty_output( struct console
*console
)
287 if (!console
->is_unix
)
289 /* initialize tty output, but don't flush */
290 tty_write( console
, "\x1b[2J", 4 ); /* clear screen */
291 set_tty_attr( console
, console
->active
->attr
);
292 tty_write( console
, "\x1b[H", 3 ); /* move cursor to (0,0) */
294 else console
->tty_attr
= empty_char_info
.attr
;
295 console
->tty_cursor_visible
= TRUE
;
298 static void scroll_to_cursor( struct screen_buffer
*screen_buffer
)
300 int w
= screen_buffer
->win
.right
- screen_buffer
->win
.left
+ 1;
301 int h
= screen_buffer
->win
.bottom
- screen_buffer
->win
.top
+ 1;
303 if (screen_buffer
->cursor_x
< screen_buffer
->win
.left
)
304 screen_buffer
->win
.left
= min( screen_buffer
->cursor_x
, screen_buffer
->width
- w
);
305 else if (screen_buffer
->cursor_x
> screen_buffer
->win
.right
)
306 screen_buffer
->win
.left
= max( screen_buffer
->cursor_x
, w
) - w
+ 1;
307 screen_buffer
->win
.right
= screen_buffer
->win
.left
+ w
- 1;
309 if (screen_buffer
->cursor_y
< screen_buffer
->win
.top
)
310 screen_buffer
->win
.top
= min( screen_buffer
->cursor_y
, screen_buffer
->height
- h
);
311 else if (screen_buffer
->cursor_y
> screen_buffer
->win
.bottom
)
312 screen_buffer
->win
.top
= max( screen_buffer
->cursor_y
, h
) - h
+ 1;
313 screen_buffer
->win
.bottom
= screen_buffer
->win
.top
+ h
- 1;
316 static void update_output( struct screen_buffer
*screen_buffer
, RECT
*rect
)
318 int x
, y
, size
, trailing_spaces
;
322 if (!is_active( screen_buffer
) || rect
->top
> rect
->bottom
|| rect
->right
< rect
->left
)
325 TRACE( "%s\n", wine_dbgstr_rect( rect
));
327 if (screen_buffer
->console
->win
)
329 update_window_region( screen_buffer
->console
, rect
);
332 if (!screen_buffer
->console
->tty_output
) return;
334 hide_tty_cursor( screen_buffer
->console
);
336 for (y
= rect
->top
; y
<= rect
->bottom
; y
++)
338 for (trailing_spaces
= 0; trailing_spaces
< screen_buffer
->width
; trailing_spaces
++)
340 ch
= &screen_buffer
->data
[(y
+ 1) * screen_buffer
->width
- trailing_spaces
- 1];
341 if (ch
->ch
!= ' ' || ch
->attr
!= 7) break;
343 if (trailing_spaces
< 4) trailing_spaces
= 0;
345 for (x
= rect
->left
; x
<= rect
->right
; x
++)
347 ch
= &screen_buffer
->data
[y
* screen_buffer
->width
+ x
];
348 set_tty_attr( screen_buffer
->console
, ch
->attr
);
349 set_tty_cursor( screen_buffer
->console
, x
, y
);
351 if (x
+ trailing_spaces
>= screen_buffer
->width
)
353 tty_write( screen_buffer
->console
, "\x1b[K", 3 );
357 size
= WideCharToMultiByte( get_tty_cp( screen_buffer
->console
), 0,
358 &ch
->ch
, 1, buf
, sizeof(buf
), NULL
, NULL
);
359 tty_write( screen_buffer
->console
, buf
, size
);
360 screen_buffer
->console
->tty_cursor_x
++;
364 empty_update_rect( screen_buffer
, rect
);
367 static void new_line( struct screen_buffer
*screen_buffer
, RECT
*update_rect
)
371 assert( screen_buffer
->cursor_y
>= screen_buffer
->height
);
372 screen_buffer
->cursor_y
= screen_buffer
->height
- 1;
374 if (screen_buffer
->console
->tty_output
)
375 update_output( screen_buffer
, update_rect
);
377 SetRect( update_rect
, 0, 0, screen_buffer
->width
- 1, screen_buffer
->height
- 1 );
379 memmove( screen_buffer
->data
, screen_buffer
->data
+ screen_buffer
->width
,
380 screen_buffer
->width
* (screen_buffer
->height
- 1) * sizeof(*screen_buffer
->data
) );
381 for (i
= 0; i
< screen_buffer
->width
; i
++)
382 screen_buffer
->data
[screen_buffer
->width
* (screen_buffer
->height
- 1) + i
] = empty_char_info
;
383 if (is_active( screen_buffer
))
385 screen_buffer
->console
->tty_cursor_y
--;
386 if (screen_buffer
->console
->tty_cursor_y
!= screen_buffer
->height
- 2)
387 set_tty_cursor( screen_buffer
->console
, 0, screen_buffer
->height
- 2 );
388 set_tty_cursor( screen_buffer
->console
, 0, screen_buffer
->height
- 1 );
392 static void write_char( struct screen_buffer
*screen_buffer
, WCHAR ch
, RECT
*update_rect
, unsigned int *home_y
)
394 if (screen_buffer
->cursor_x
== screen_buffer
->width
)
396 screen_buffer
->cursor_x
= 0;
397 screen_buffer
->cursor_y
++;
399 if (screen_buffer
->cursor_y
== screen_buffer
->height
)
403 if (!*home_y
) return;
406 new_line( screen_buffer
, update_rect
);
409 screen_buffer
->data
[screen_buffer
->cursor_y
* screen_buffer
->width
+ screen_buffer
->cursor_x
].ch
= ch
;
410 screen_buffer
->data
[screen_buffer
->cursor_y
* screen_buffer
->width
+ screen_buffer
->cursor_x
].attr
= screen_buffer
->attr
;
411 update_rect
->left
= min( update_rect
->left
, screen_buffer
->cursor_x
);
412 update_rect
->top
= min( update_rect
->top
, screen_buffer
->cursor_y
);
413 update_rect
->right
= max( update_rect
->right
, screen_buffer
->cursor_x
);
414 update_rect
->bottom
= max( update_rect
->bottom
, screen_buffer
->cursor_y
);
415 screen_buffer
->cursor_x
++;
418 static NTSTATUS
read_complete( struct console
*console
, NTSTATUS status
, const void *buf
, size_t size
, int signal
)
420 SERVER_START_REQ( get_next_console_request
)
422 req
->handle
= wine_server_obj_handle( console
->server
);
423 req
->signal
= signal
;
425 req
->status
= status
;
426 wine_server_add_data( req
, buf
, size
);
427 status
= wine_server_call( req
);
430 if (status
&& (console
->read_ioctl
|| status
!= STATUS_INVALID_HANDLE
)) ERR( "failed: %#x\n", status
);
431 console
->signaled
= signal
;
432 console
->read_ioctl
= 0;
433 console
->pending_read
= 0;
437 static NTSTATUS
read_console_input( struct console
*console
, size_t out_size
)
439 size_t count
= min( out_size
/ sizeof(INPUT_RECORD
), console
->record_count
);
441 TRACE("count %u\n", count
);
443 read_complete( console
, STATUS_SUCCESS
, console
->records
, count
* sizeof(*console
->records
),
444 console
->record_count
> count
);
446 if (count
< console
->record_count
)
447 memmove( console
->records
, console
->records
+ count
,
448 (console
->record_count
- count
) * sizeof(*console
->records
) );
449 console
->record_count
-= count
;
450 return STATUS_SUCCESS
;
453 static void read_from_buffer( struct console
*console
, size_t out_size
)
455 size_t len
, read_len
= 0;
458 switch( console
->read_ioctl
)
460 case IOCTL_CONDRV_READ_CONSOLE
:
461 out_size
= min( out_size
, console
->read_buffer_count
* sizeof(WCHAR
) );
462 read_complete( console
, STATUS_SUCCESS
, console
->read_buffer
, out_size
, console
->record_count
!= 0 );
463 read_len
= out_size
/ sizeof(WCHAR
);
465 case IOCTL_CONDRV_READ_FILE
:
467 while (read_len
< console
->read_buffer_count
&& len
< out_size
)
469 len
+= WideCharToMultiByte( console
->input_cp
, 0, console
->read_buffer
+ read_len
, 1, NULL
, 0, NULL
, NULL
);
474 if (!(buf
= malloc( len
)))
476 read_complete( console
, STATUS_NO_MEMORY
, NULL
, 0, console
->record_count
!= 0 );
479 WideCharToMultiByte( console
->input_cp
, 0, console
->read_buffer
, read_len
, buf
, len
, NULL
, NULL
);
481 len
= min( out_size
, len
);
482 read_complete( console
, STATUS_SUCCESS
, buf
, len
, console
->record_count
!= 0 );
487 if (read_len
< console
->read_buffer_count
)
489 memmove( console
->read_buffer
, console
->read_buffer
+ read_len
,
490 (console
->read_buffer_count
- read_len
) * sizeof(WCHAR
) );
492 if (!(console
->read_buffer_count
-= read_len
))
493 free( console
->read_buffer
);
496 static void append_input_history( struct console
*console
, const WCHAR
*str
, size_t len
)
498 struct history_line
*ptr
;
500 if (!console
->history_size
) return;
502 /* don't duplicate entry */
503 if (console
->history_mode
&& console
->history_index
&&
504 console
->history
[console
->history_index
- 1]->len
== len
&&
505 !memcmp( console
->history
[console
->history_index
- 1]->text
, str
, len
))
508 if (!(ptr
= malloc( offsetof( struct history_line
, text
[len
/ sizeof(WCHAR
)] )))) return;
510 memcpy( ptr
->text
, str
, len
);
512 if (console
->history_index
< console
->history_size
)
514 console
->history
[console
->history_index
++] = ptr
;
518 free( console
->history
[0]) ;
519 memmove( &console
->history
[0], &console
->history
[1],
520 (console
->history_size
- 1) * sizeof(*console
->history
) );
521 console
->history
[console
->history_size
- 1] = ptr
;
525 static void edit_line_update( struct console
*console
, unsigned int begin
, unsigned int length
)
527 struct edit_line
*ctx
= &console
->edit_line
;
529 ctx
->update_begin
= min( ctx
->update_begin
, begin
);
530 ctx
->update_end
= max( ctx
->update_end
, begin
+ length
- 1 );
533 static BOOL
edit_line_grow( struct console
*console
, size_t length
)
535 struct edit_line
*ctx
= &console
->edit_line
;
539 if (ctx
->len
+ length
< ctx
->size
) return TRUE
;
541 /* round up size to 32 byte-WCHAR boundary */
542 new_size
= (ctx
->len
+ length
+ 32) & ~31;
543 if (!(new_buf
= realloc( ctx
->buf
, sizeof(WCHAR
) * new_size
)))
545 ctx
->status
= STATUS_NO_MEMORY
;
549 ctx
->size
= new_size
;
553 static void edit_line_delete( struct console
*console
, int begin
, int end
)
555 struct edit_line
*ctx
= &console
->edit_line
;
556 unsigned int len
= end
- begin
;
558 edit_line_update( console
, begin
, ctx
->len
- begin
);
560 memmove( &ctx
->buf
[begin
], &ctx
->buf
[end
], (ctx
->len
- end
) * sizeof(WCHAR
));
562 edit_line_update( console
, 0, ctx
->len
);
563 ctx
->buf
[ctx
->len
] = 0;
566 static void edit_line_insert( struct console
*console
, const WCHAR
*str
, unsigned int len
)
568 struct edit_line
*ctx
= &console
->edit_line
;
569 unsigned int update_len
;
572 if (ctx
->insert_mode
)
574 if (!edit_line_grow( console
, len
)) return;
575 if (ctx
->len
> ctx
->cursor
)
576 memmove( &ctx
->buf
[ctx
->cursor
+ len
], &ctx
->buf
[ctx
->cursor
],
577 (ctx
->len
- ctx
->cursor
) * sizeof(WCHAR
) );
579 update_len
= ctx
->len
- ctx
->cursor
;
583 if (ctx
->cursor
+ len
> ctx
->len
)
585 if (!edit_line_grow( console
, (ctx
->cursor
+ len
) - ctx
->len
) )
587 ctx
->len
= ctx
->cursor
+ len
;
591 memcpy( &ctx
->buf
[ctx
->cursor
], str
, len
* sizeof(WCHAR
) );
592 ctx
->buf
[ctx
->len
] = 0;
593 edit_line_update( console
, ctx
->cursor
, update_len
);
597 static void edit_line_save_yank( struct console
*console
, unsigned int begin
, unsigned int end
)
599 struct edit_line
*ctx
= &console
->edit_line
;
600 unsigned int len
= end
- begin
;
601 if (len
<= 0) return;
604 ctx
->yanked
= malloc( (len
+ 1) * sizeof(WCHAR
) );
607 ctx
->status
= STATUS_NO_MEMORY
;
610 memcpy( ctx
->yanked
, &ctx
->buf
[begin
], len
* sizeof(WCHAR
) );
611 ctx
->yanked
[len
] = 0;
614 static int edit_line_left_word_transition( struct console
*console
, int offset
)
617 while (offset
>= 0 && !iswalnum( console
->edit_line
.buf
[offset
] )) offset
--;
618 while (offset
>= 0 && iswalnum( console
->edit_line
.buf
[offset
] )) offset
--;
619 if (offset
>= 0) offset
++;
620 return max( offset
, 0 );
623 static int edit_line_right_word_transition( struct console
*console
, int offset
)
626 while (offset
<= console
->edit_line
.len
&& iswalnum( console
->edit_line
.buf
[offset
] ))
628 while (offset
<= console
->edit_line
.len
&& !iswalnum( console
->edit_line
.buf
[offset
] ))
630 return min(offset
, console
->edit_line
.len
);
633 static WCHAR
*edit_line_history( struct console
*console
, unsigned int index
)
637 if (index
< console
->history_index
)
639 if ((ptr
= malloc( console
->history
[index
]->len
+ sizeof(WCHAR
) )))
641 memcpy( ptr
, console
->history
[index
]->text
, console
->history
[index
]->len
);
642 ptr
[console
->history
[index
]->len
/ sizeof(WCHAR
)] = 0;
645 else if(console
->edit_line
.current_history
)
647 if ((ptr
= malloc( (lstrlenW(console
->edit_line
.current_history
) + 1) * sizeof(WCHAR
) )))
648 lstrcpyW( ptr
, console
->edit_line
.current_history
);
653 static void edit_line_move_to_history( struct console
*console
, int index
)
655 struct edit_line
*ctx
= &console
->edit_line
;
656 WCHAR
*line
= edit_line_history(console
, index
);
657 size_t len
= line
? lstrlenW(line
) : 0;
659 /* save current line edition for recall when needed */
660 if (ctx
->history_index
== console
->history_index
)
662 free( ctx
->current_history
);
663 ctx
->current_history
= malloc( (ctx
->len
+ 1) * sizeof(WCHAR
) );
664 if (ctx
->current_history
)
666 memcpy( ctx
->current_history
, ctx
->buf
, (ctx
->len
+ 1) * sizeof(WCHAR
) );
670 ctx
->status
= STATUS_NO_MEMORY
;
675 /* need to clean also the screen if new string is shorter than old one */
676 edit_line_delete(console
, 0, ctx
->len
);
678 /* insert new string */
679 if (edit_line_grow(console
, len
+ 1))
681 edit_line_insert( console
, line
, len
);
682 ctx
->history_index
= index
;
687 static void edit_line_find_in_history( struct console
*console
)
689 struct edit_line
*ctx
= &console
->edit_line
;
690 int start_pos
= ctx
->history_index
;
691 unsigned int len
, oldoffset
;
694 if (!console
->history_index
) return;
695 if (ctx
->history_index
&& ctx
->history_index
== console
->history_index
)
698 ctx
->history_index
--;
703 line
= edit_line_history(console
, ctx
->history_index
);
705 if (ctx
->history_index
) ctx
->history_index
--;
706 else ctx
->history_index
= console
->history_index
- 1;
708 len
= lstrlenW(line
) + 1;
709 if (len
>= ctx
->cursor
&& !memcmp( ctx
->buf
, line
, ctx
->cursor
* sizeof(WCHAR
) ))
711 /* need to clean also the screen if new string is shorter than old one */
712 edit_line_delete(console
, 0, ctx
->len
);
714 if (edit_line_grow(console
, len
))
716 oldoffset
= ctx
->cursor
;
718 edit_line_insert( console
, line
, len
- 1 );
719 ctx
->cursor
= oldoffset
;
726 while (ctx
->history_index
!= start_pos
);
729 static void edit_line_move_left( struct console
*console
)
731 if (console
->edit_line
.cursor
> 0) console
->edit_line
.cursor
--;
734 static void edit_line_move_right( struct console
*console
)
736 struct edit_line
*ctx
= &console
->edit_line
;
737 if (ctx
->cursor
< ctx
->len
) ctx
->cursor
++;
740 static void edit_line_move_left_word( struct console
*console
)
742 console
->edit_line
.cursor
= edit_line_left_word_transition( console
, console
->edit_line
.cursor
);
745 static void edit_line_move_right_word( struct console
*console
)
747 console
->edit_line
.cursor
= edit_line_right_word_transition( console
, console
->edit_line
.cursor
);
750 static void edit_line_move_home( struct console
*console
)
752 console
->edit_line
.cursor
= 0;
755 static void edit_line_move_end( struct console
*console
)
757 console
->edit_line
.cursor
= console
->edit_line
.len
;
760 static void edit_line_set_mark( struct console
*console
)
762 console
->edit_line
.mark
= console
->edit_line
.cursor
;
765 static void edit_line_exchange_mark( struct console
*console
)
767 struct edit_line
*ctx
= &console
->edit_line
;
770 if (ctx
->mark
> ctx
->len
) return;
771 cursor
= ctx
->cursor
;
772 ctx
->cursor
= ctx
->mark
;
776 static void edit_line_copy_marked_zone( struct console
*console
)
778 struct edit_line
*ctx
= &console
->edit_line
;
779 unsigned int begin
, end
;
781 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->cursor
) return;
782 if (ctx
->mark
> ctx
->cursor
)
792 edit_line_save_yank( console
, begin
, end
);
795 static void edit_line_transpose_char( struct console
*console
)
797 struct edit_line
*ctx
= &console
->edit_line
;
800 if (!ctx
->cursor
|| ctx
->cursor
== ctx
->len
) return;
802 c
= ctx
->buf
[ctx
->cursor
];
803 ctx
->buf
[ctx
->cursor
] = ctx
->buf
[ctx
->cursor
- 1];
804 ctx
->buf
[ctx
->cursor
- 1] = c
;
806 edit_line_update( console
, ctx
->cursor
- 1, 2 );
810 static void edit_line_transpose_words( struct console
*console
)
812 struct edit_line
*ctx
= &console
->edit_line
;
813 unsigned int left_offset
= edit_line_left_word_transition( console
, ctx
->cursor
);
814 unsigned int right_offset
= edit_line_right_word_transition( console
, ctx
->cursor
);
815 if (left_offset
< ctx
->cursor
&& right_offset
> ctx
->cursor
)
817 unsigned int len_r
= right_offset
- ctx
->cursor
;
818 unsigned int len_l
= ctx
->cursor
- left_offset
;
819 char *tmp
= malloc( len_r
* sizeof(WCHAR
) );
822 ctx
->status
= STATUS_NO_MEMORY
;
826 memcpy( tmp
, &ctx
->buf
[ctx
->cursor
], len_r
* sizeof(WCHAR
) );
827 memmove( &ctx
->buf
[left_offset
+ len_r
], &ctx
->buf
[left_offset
],
828 len_l
* sizeof(WCHAR
) );
829 memcpy( &ctx
->buf
[left_offset
], tmp
, len_r
* sizeof(WCHAR
) );
832 edit_line_update( console
, left_offset
, len_l
+ len_r
);
833 ctx
->cursor
= right_offset
;
837 static void edit_line_lower_case_word( struct console
*console
)
839 struct edit_line
*ctx
= &console
->edit_line
;
840 unsigned int new_offset
= edit_line_right_word_transition( console
, ctx
->cursor
);
841 if (new_offset
!= ctx
->cursor
)
843 CharLowerBuffW( ctx
->buf
+ ctx
->cursor
, new_offset
- ctx
->cursor
+ 1 );
844 edit_line_update( console
, ctx
->cursor
, new_offset
- ctx
->cursor
+ 1 );
845 ctx
->cursor
= new_offset
;
849 static void edit_line_upper_case_word( struct console
*console
)
851 struct edit_line
*ctx
= &console
->edit_line
;
852 unsigned int new_offset
= edit_line_right_word_transition( console
, ctx
->cursor
);
853 if (new_offset
!= ctx
->cursor
)
855 CharUpperBuffW( ctx
->buf
+ ctx
->cursor
, new_offset
- ctx
->cursor
+ 1 );
856 edit_line_update( console
, ctx
->cursor
, new_offset
- ctx
->cursor
+ 1 );
857 ctx
->cursor
= new_offset
;
861 static void edit_line_capitalize_word( struct console
*console
)
863 struct edit_line
*ctx
= &console
->edit_line
;
864 unsigned int new_offset
= edit_line_right_word_transition( console
, ctx
->cursor
);
865 if (new_offset
!= ctx
->cursor
)
867 CharUpperBuffW( ctx
->buf
+ ctx
->cursor
, 1 );
868 CharLowerBuffW( ctx
->buf
+ ctx
->cursor
+ 1, new_offset
- ctx
->cursor
);
869 edit_line_update( console
, ctx
->cursor
, new_offset
- ctx
->cursor
+ 1 );
870 ctx
->cursor
= new_offset
;
874 static void edit_line_yank( struct console
*console
)
876 struct edit_line
*ctx
= &console
->edit_line
;
877 if (ctx
->yanked
) edit_line_insert( console
, ctx
->yanked
, wcslen(ctx
->yanked
) );
880 static void edit_line_kill_suffix( struct console
*console
)
882 struct edit_line
*ctx
= &console
->edit_line
;
883 edit_line_save_yank( console
, ctx
->cursor
, ctx
->len
);
884 edit_line_delete( console
, ctx
->cursor
, ctx
->len
);
887 static void edit_line_kill_prefix( struct console
*console
)
889 struct edit_line
*ctx
= &console
->edit_line
;
892 edit_line_save_yank( console
, 0, ctx
->cursor
);
893 edit_line_delete( console
, 0, ctx
->cursor
);
898 static void edit_line_kill_marked_zone( struct console
*console
)
900 struct edit_line
*ctx
= &console
->edit_line
;
901 unsigned int begin
, end
;
903 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->cursor
)
905 if (ctx
->mark
> ctx
->cursor
)
915 edit_line_save_yank( console
, begin
, end
);
916 edit_line_delete( console
, begin
, end
);
920 static void edit_line_delete_prev( struct console
*console
)
922 struct edit_line
*ctx
= &console
->edit_line
;
925 edit_line_delete( console
, ctx
->cursor
- 1, ctx
->cursor
);
930 static void edit_line_delete_char( struct console
*console
)
932 struct edit_line
*ctx
= &console
->edit_line
;
933 if (ctx
->cursor
< ctx
->len
)
934 edit_line_delete( console
, ctx
->cursor
, ctx
->cursor
+ 1 );
937 static void edit_line_delete_left_word( struct console
*console
)
939 struct edit_line
*ctx
= &console
->edit_line
;
940 unsigned int new_offset
= edit_line_left_word_transition( console
, ctx
->cursor
);
941 if (new_offset
!= ctx
->cursor
)
943 edit_line_delete( console
, new_offset
, ctx
->cursor
);
944 ctx
->cursor
= new_offset
;
948 static void edit_line_delete_right_word( struct console
*console
)
950 struct edit_line
*ctx
= &console
->edit_line
;
951 unsigned int new_offset
= edit_line_right_word_transition( console
, ctx
->cursor
);
952 if (new_offset
!= ctx
->cursor
)
954 edit_line_delete( console
, ctx
->cursor
, new_offset
);
958 static void edit_line_move_to_prev_hist( struct console
*console
)
960 if (console
->edit_line
.history_index
)
961 edit_line_move_to_history( console
, console
->edit_line
.history_index
- 1 );
964 static void edit_line_move_to_next_hist( struct console
*console
)
966 if (console
->edit_line
.history_index
< console
->history_index
)
967 edit_line_move_to_history( console
, console
->edit_line
.history_index
+ 1 );
970 static void edit_line_move_to_first_hist( struct console
*console
)
972 if (console
->edit_line
.history_index
)
973 edit_line_move_to_history( console
, 0 );
976 static void edit_line_move_to_last_hist( struct console
*console
)
978 if (console
->edit_line
.history_index
!= console
->history_index
)
979 edit_line_move_to_history( console
, console
->history_index
);
982 static void edit_line_redraw( struct console
*console
)
984 if (console
->mode
& ENABLE_ECHO_INPUT
)
985 edit_line_update( console
, 0, console
->edit_line
.len
);
988 static void edit_line_toggle_insert( struct console
*console
)
990 struct edit_line
*ctx
= &console
->edit_line
;
991 ctx
->insert_key
= !ctx
->insert_key
;
992 console
->active
->cursor_size
= ctx
->insert_key
? 100 : 25;
995 static void edit_line_done( struct console
*console
)
997 console
->edit_line
.status
= STATUS_SUCCESS
;
1000 struct edit_line_key_entry
1002 WCHAR val
; /* vk or unicode char */
1003 void (*func
)( struct console
*console
);
1006 struct edit_line_key_map
1008 DWORD key_state
; /* keyState (from INPUT_RECORD) to match */
1009 BOOL is_char
; /* check vk or char */
1010 const struct edit_line_key_entry
*entries
;
1013 #define CTRL(x) ((x) - '@')
1014 static const struct edit_line_key_entry std_key_map
[] =
1016 { VK_BACK
, edit_line_delete_prev
},
1017 { VK_RETURN
, edit_line_done
},
1018 { VK_DELETE
, edit_line_delete_char
},
1022 static const struct edit_line_key_entry emacs_key_map_ctrl
[] =
1024 { CTRL('@'), edit_line_set_mark
},
1025 { CTRL('A'), edit_line_move_home
},
1026 { CTRL('B'), edit_line_move_left
},
1027 { CTRL('D'), edit_line_delete_char
},
1028 { CTRL('E'), edit_line_move_end
},
1029 { CTRL('F'), edit_line_move_right
},
1030 { CTRL('H'), edit_line_delete_prev
},
1031 { CTRL('J'), edit_line_done
},
1032 { CTRL('K'), edit_line_kill_suffix
},
1033 { CTRL('L'), edit_line_redraw
},
1034 { CTRL('M'), edit_line_done
},
1035 { CTRL('N'), edit_line_move_to_next_hist
},
1036 { CTRL('P'), edit_line_move_to_prev_hist
},
1037 { CTRL('T'), edit_line_transpose_char
},
1038 { CTRL('W'), edit_line_kill_marked_zone
},
1039 { CTRL('X'), edit_line_exchange_mark
},
1040 { CTRL('Y'), edit_line_yank
},
1044 static const struct edit_line_key_entry emacs_key_map_alt
[] =
1046 { 0x7f, edit_line_delete_left_word
},
1047 { '<', edit_line_move_to_first_hist
},
1048 { '>', edit_line_move_to_last_hist
},
1049 { 'b', edit_line_move_left_word
},
1050 { 'c', edit_line_capitalize_word
},
1051 { 'd', edit_line_delete_right_word
},
1052 { 'f', edit_line_move_right_word
},
1053 { 'l', edit_line_lower_case_word
},
1054 { 't', edit_line_transpose_words
},
1055 { 'u', edit_line_upper_case_word
},
1056 { 'w', edit_line_copy_marked_zone
},
1060 static const struct edit_line_key_entry emacs_std_key_map
[] =
1062 { VK_PRIOR
, edit_line_move_to_prev_hist
},
1063 { VK_NEXT
, edit_line_move_to_next_hist
},
1064 { VK_END
, edit_line_move_end
},
1065 { VK_HOME
, edit_line_move_home
},
1066 { VK_RIGHT
, edit_line_move_right
},
1067 { VK_LEFT
, edit_line_move_left
},
1068 { VK_INSERT
, edit_line_toggle_insert
},
1072 static const struct edit_line_key_map emacs_key_map
[] =
1074 { 0, 0, std_key_map
},
1075 { 0, 0, emacs_std_key_map
},
1076 { RIGHT_ALT_PRESSED
, 1, emacs_key_map_alt
},
1077 { LEFT_ALT_PRESSED
, 1, emacs_key_map_alt
},
1078 { RIGHT_CTRL_PRESSED
, 1, emacs_key_map_ctrl
},
1079 { LEFT_CTRL_PRESSED
, 1, emacs_key_map_ctrl
},
1083 static const struct edit_line_key_entry win32_std_key_map
[] =
1085 { VK_LEFT
, edit_line_move_left
},
1086 { VK_RIGHT
, edit_line_move_right
},
1087 { VK_HOME
, edit_line_move_home
},
1088 { VK_END
, edit_line_move_end
},
1089 { VK_UP
, edit_line_move_to_prev_hist
},
1090 { VK_DOWN
, edit_line_move_to_next_hist
},
1091 { VK_INSERT
, edit_line_toggle_insert
},
1092 { VK_F8
, edit_line_find_in_history
},
1096 static const struct edit_line_key_entry win32_key_map_ctrl
[] =
1098 { VK_LEFT
, edit_line_move_left_word
},
1099 { VK_RIGHT
, edit_line_move_right_word
},
1100 { VK_END
, edit_line_kill_suffix
},
1101 { VK_HOME
, edit_line_kill_prefix
},
1105 static const struct edit_line_key_map win32_key_map
[] =
1107 { 0, 0, std_key_map
},
1108 { SHIFT_PRESSED
, 0, std_key_map
},
1109 { 0, 0, win32_std_key_map
},
1110 { RIGHT_CTRL_PRESSED
, 0, win32_key_map_ctrl
},
1111 { LEFT_CTRL_PRESSED
, 0, win32_key_map_ctrl
},
1116 static unsigned int edit_line_string_width( const WCHAR
*str
, unsigned int len
)
1118 unsigned int i
, offset
= 0;
1119 for (i
= 0; i
< len
; i
++) offset
+= str
[i
] < ' ' ? 2 : 1;
1123 static void update_read_output( struct console
*console
)
1125 struct screen_buffer
*screen_buffer
= console
->active
;
1126 struct edit_line
*ctx
= &console
->edit_line
;
1127 int offset
= 0, j
, end_offset
;
1130 empty_update_rect( screen_buffer
, &update_rect
);
1132 if (ctx
->update_end
>= ctx
->update_begin
)
1134 TRACE( "update %d-%d %s\n", ctx
->update_begin
, ctx
->update_end
,
1135 debugstr_wn( ctx
->buf
+ ctx
->update_begin
, ctx
->update_end
- ctx
->update_begin
+ 1 ));
1137 hide_tty_cursor( screen_buffer
->console
);
1139 offset
= edit_line_string_width( ctx
->buf
, ctx
->update_begin
);
1140 screen_buffer
->cursor_x
= (ctx
->home_x
+ offset
) % screen_buffer
->width
;
1141 screen_buffer
->cursor_y
= ctx
->home_y
+ (ctx
->home_x
+ offset
) / screen_buffer
->width
;
1142 for (j
= ctx
->update_begin
; j
<= ctx
->update_end
; j
++)
1144 if (screen_buffer
->cursor_y
>= screen_buffer
->height
&& !ctx
->home_y
) break;
1145 if (j
>= ctx
->len
) break;
1146 if (ctx
->buf
[j
] < ' ')
1148 write_char( screen_buffer
, '^', &update_rect
, &ctx
->home_y
);
1149 write_char( screen_buffer
, '@' + ctx
->buf
[j
], &update_rect
, &ctx
->home_y
);
1154 write_char( screen_buffer
, ctx
->buf
[j
], &update_rect
, &ctx
->home_y
);
1158 end_offset
= ctx
->end_offset
;
1159 ctx
->end_offset
= offset
;
1162 /* clear trailing characters if buffer was shortened */
1163 while (offset
< end_offset
&& screen_buffer
->cursor_y
< screen_buffer
->height
)
1165 write_char( screen_buffer
, ' ', &update_rect
, &ctx
->home_y
);
1173 offset
= edit_line_string_width( ctx
->buf
, ctx
->len
);
1174 screen_buffer
->cursor_x
= 0;
1175 screen_buffer
->cursor_y
= ctx
->home_y
+ (ctx
->home_x
+ offset
) / screen_buffer
->width
;
1176 if (++screen_buffer
->cursor_y
>= screen_buffer
->height
)
1177 new_line( screen_buffer
, &update_rect
);
1181 offset
= edit_line_string_width( ctx
->buf
, ctx
->cursor
);
1182 screen_buffer
->cursor_y
= ctx
->home_y
+ (ctx
->home_x
+ offset
) / screen_buffer
->width
;
1183 if (screen_buffer
->cursor_y
< screen_buffer
->height
)
1185 screen_buffer
->cursor_x
= (ctx
->home_x
+ offset
) % screen_buffer
->width
;
1189 screen_buffer
->cursor_x
= screen_buffer
->width
- 1;
1190 screen_buffer
->cursor_y
= screen_buffer
->height
- 1;
1194 /* always try to use relative cursor positions in UNIX mode so that it works even if cursor
1195 * position is out of sync */
1196 if (update_rect
.left
<= update_rect
.right
&& update_rect
.top
<= update_rect
.bottom
)
1198 if (console
->is_unix
)
1199 set_tty_cursor_relative( screen_buffer
->console
, update_rect
.left
, update_rect
.top
);
1200 update_output( screen_buffer
, &update_rect
);
1201 scroll_to_cursor( screen_buffer
);
1203 if (console
->is_unix
)
1204 set_tty_cursor_relative( screen_buffer
->console
, screen_buffer
->cursor_x
, screen_buffer
->cursor_y
);
1205 tty_sync( screen_buffer
->console
);
1206 update_window_config( screen_buffer
->console
);
1209 static NTSTATUS
process_console_input( struct console
*console
)
1211 struct edit_line
*ctx
= &console
->edit_line
;
1214 switch (console
->read_ioctl
)
1216 case IOCTL_CONDRV_READ_INPUT
:
1217 if (console
->record_count
) read_console_input( console
, console
->pending_read
);
1218 return STATUS_SUCCESS
;
1219 case IOCTL_CONDRV_READ_CONSOLE
:
1220 case IOCTL_CONDRV_READ_FILE
:
1223 assert( !console
->read_ioctl
);
1224 if (console
->record_count
&& !console
->signaled
)
1225 read_complete( console
, STATUS_PENDING
, NULL
, 0, TRUE
); /* signal server */
1226 return STATUS_SUCCESS
;
1229 ctx
->update_begin
= ctx
->len
+ 1;
1230 ctx
->update_end
= 0;
1232 for (i
= 0; i
< console
->record_count
&& ctx
->status
== STATUS_PENDING
; i
++)
1234 void (*func
)( struct console
*console
) = NULL
;
1235 INPUT_RECORD ir
= console
->records
[i
];
1237 if (ir
.EventType
!= KEY_EVENT
|| !ir
.Event
.KeyEvent
.bKeyDown
) continue;
1239 TRACE( "key code=%02x scan=%02x char=%02x state=%08x\n",
1240 ir
.Event
.KeyEvent
.wVirtualKeyCode
, ir
.Event
.KeyEvent
.wVirtualScanCode
,
1241 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
, ir
.Event
.KeyEvent
.dwControlKeyState
);
1243 if (console
->mode
& ENABLE_LINE_INPUT
)
1245 const struct edit_line_key_entry
*entry
;
1246 const struct edit_line_key_map
*map
;
1249 /* mask out some bits which don't interest us */
1250 state
= ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
|ENHANCED_KEY
);
1253 for (map
= console
->edition_mode
? emacs_key_map
: win32_key_map
; map
->entries
!= NULL
; map
++)
1255 if (map
->key_state
!= state
)
1259 for (entry
= &map
->entries
[0]; entry
->func
!= 0; entry
++)
1260 if (entry
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
1264 for (entry
= &map
->entries
[0]; entry
->func
!= 0; entry
++)
1265 if (entry
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
1276 ctx
->insert_mode
= ((console
->mode
& (ENABLE_INSERT_MODE
| ENABLE_EXTENDED_FLAGS
)) ==
1277 (ENABLE_INSERT_MODE
| ENABLE_EXTENDED_FLAGS
))
1280 if (func
) func( console
);
1281 else if (ir
.Event
.KeyEvent
.uChar
.UnicodeChar
)
1282 edit_line_insert( console
, &ir
.Event
.KeyEvent
.uChar
.UnicodeChar
, 1 );
1284 if (!(console
->mode
& ENABLE_LINE_INPUT
) && ctx
->status
== STATUS_PENDING
)
1286 if (console
->read_ioctl
== IOCTL_CONDRV_READ_FILE
)
1288 if (WideCharToMultiByte(console
->input_cp
, 0, ctx
->buf
, ctx
->len
, NULL
, 0, NULL
, NULL
)
1289 >= console
->pending_read
)
1290 ctx
->status
= STATUS_SUCCESS
;
1292 else if (ctx
->len
>= console
->pending_read
/ sizeof(WCHAR
))
1293 ctx
->status
= STATUS_SUCCESS
;
1297 if (console
->record_count
> i
) memmove( console
->records
, console
->records
+ i
,
1298 (console
->record_count
- i
) * sizeof(*console
->records
) );
1299 console
->record_count
-= i
;
1301 if (ctx
->status
== STATUS_PENDING
&& !(console
->mode
& ENABLE_LINE_INPUT
) && ctx
->len
)
1302 ctx
->status
= STATUS_SUCCESS
;
1304 if (console
->mode
& ENABLE_ECHO_INPUT
) update_read_output( console
);
1305 if (ctx
->status
== STATUS_PENDING
) return STATUS_SUCCESS
;
1307 if (!ctx
->status
&& (console
->mode
& ENABLE_LINE_INPUT
))
1309 if (ctx
->len
) append_input_history( console
, ctx
->buf
, ctx
->len
* sizeof(WCHAR
) );
1310 if (edit_line_grow(console
, 2))
1312 ctx
->buf
[ctx
->len
++] = '\r';
1313 ctx
->buf
[ctx
->len
++] = '\n';
1314 ctx
->buf
[ctx
->len
] = 0;
1315 TRACE( "return %s\n", debugstr_wn( ctx
->buf
, ctx
->len
));
1319 console
->read_buffer
= ctx
->buf
;
1320 console
->read_buffer_count
= ctx
->len
;
1321 console
->read_buffer_size
= ctx
->size
;
1323 if (ctx
->status
) read_complete( console
, ctx
->status
, NULL
, 0, console
->record_count
);
1324 else read_from_buffer( console
, console
->pending_read
);
1327 free( ctx
->yanked
);
1328 free( ctx
->current_history
);
1329 memset( &console
->edit_line
, 0, sizeof(console
->edit_line
) );
1330 return STATUS_SUCCESS
;
1333 static NTSTATUS
read_console( struct console
*console
, unsigned int ioctl
, size_t out_size
)
1337 if (out_size
> INT_MAX
)
1339 read_complete( console
, STATUS_NO_MEMORY
, NULL
, 0, console
->record_count
);
1340 return STATUS_NO_MEMORY
;
1343 console
->read_ioctl
= ioctl
;
1344 if (!out_size
|| console
->read_buffer_count
)
1346 read_from_buffer( console
, out_size
);
1347 return STATUS_SUCCESS
;
1350 console
->edit_line
.history_index
= console
->history_index
;
1351 console
->edit_line
.home_x
= console
->active
->cursor_x
;
1352 console
->edit_line
.home_y
= console
->active
->cursor_y
;
1353 console
->edit_line
.status
= STATUS_PENDING
;
1354 if (edit_line_grow( console
, 1 )) console
->edit_line
.buf
[0] = 0;
1356 console
->pending_read
= out_size
;
1357 return process_console_input( console
);
1360 /* add input events to a console input queue */
1361 NTSTATUS
write_console_input( struct console
*console
, const INPUT_RECORD
*records
,
1362 unsigned int count
, BOOL flush
)
1364 TRACE( "%u\n", count
);
1366 if (!count
) return STATUS_SUCCESS
;
1367 if (console
->record_count
+ count
> console
->record_size
)
1369 INPUT_RECORD
*new_rec
;
1370 if (!(new_rec
= realloc( console
->records
, (console
->record_size
* 2 + count
) * sizeof(INPUT_RECORD
) )))
1371 return STATUS_NO_MEMORY
;
1372 console
->records
= new_rec
;
1373 console
->record_size
= console
->record_size
* 2 + count
;
1375 memcpy( console
->records
+ console
->record_count
, records
, count
* sizeof(INPUT_RECORD
) );
1377 if (console
->mode
& ENABLE_PROCESSED_INPUT
)
1382 if (records
[i
].EventType
== KEY_EVENT
&&
1383 records
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
== 'C' - 64 &&
1384 !(records
[i
].Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
1387 memcpy( &console
->records
[console
->record_count
+ i
],
1388 &console
->records
[console
->record_count
+ i
+ 1],
1389 (count
- i
- 1) * sizeof(INPUT_RECORD
) );
1391 if (records
[i
].Event
.KeyEvent
.bKeyDown
)
1393 struct condrv_ctrl_event ctrl_event
;
1396 ctrl_event
.event
= CTRL_C_EVENT
;
1397 ctrl_event
.group_id
= 0;
1398 NtDeviceIoControlFile( console
->server
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_CTRL_EVENT
,
1399 &ctrl_event
, sizeof(ctrl_event
), NULL
, 0 );
1406 console
->record_count
+= count
;
1407 return flush
? process_console_input( console
) : STATUS_SUCCESS
;
1410 static void set_key_input_record( INPUT_RECORD
*record
, WCHAR ch
, unsigned int vk
, BOOL is_down
, unsigned int ctrl_state
)
1412 record
->EventType
= KEY_EVENT
;
1413 record
->Event
.KeyEvent
.bKeyDown
= is_down
;
1414 record
->Event
.KeyEvent
.wRepeatCount
= 1;
1415 record
->Event
.KeyEvent
.uChar
.UnicodeChar
= ch
;
1416 record
->Event
.KeyEvent
.wVirtualKeyCode
= vk
;
1417 record
->Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKeyW( vk
, MAPVK_VK_TO_VSC
);
1418 record
->Event
.KeyEvent
.dwControlKeyState
= ctrl_state
;
1421 static NTSTATUS
key_press( struct console
*console
, WCHAR ch
, unsigned int vk
, unsigned int ctrl_state
)
1423 INPUT_RECORD records
[8];
1424 unsigned int count
= 0, ctrl
= 0;
1426 if (ctrl_state
& SHIFT_PRESSED
)
1428 ctrl
|= SHIFT_PRESSED
;
1429 set_key_input_record( &records
[count
++], 0, VK_SHIFT
, TRUE
, ctrl
);
1431 if (ctrl_state
& LEFT_ALT_PRESSED
)
1433 ctrl
|= LEFT_ALT_PRESSED
;
1434 set_key_input_record( &records
[count
++], 0, VK_MENU
, TRUE
, ctrl
);
1436 if (ctrl_state
& LEFT_CTRL_PRESSED
)
1438 ctrl
|= LEFT_CTRL_PRESSED
;
1439 set_key_input_record( &records
[count
++], 0, VK_CONTROL
, TRUE
, ctrl
);
1442 set_key_input_record( &records
[count
++], ch
, vk
, TRUE
, ctrl
);
1443 set_key_input_record( &records
[count
++], ch
, vk
, FALSE
, ctrl
);
1445 if (ctrl
& LEFT_CTRL_PRESSED
)
1447 ctrl
&= ~LEFT_CTRL_PRESSED
;
1448 set_key_input_record( &records
[count
++], 0, VK_CONTROL
, FALSE
, ctrl
);
1450 if (ctrl
& LEFT_ALT_PRESSED
)
1452 ctrl
&= ~LEFT_ALT_PRESSED
;
1453 set_key_input_record( &records
[count
++], 0, VK_MENU
, FALSE
, ctrl
);
1455 if (ctrl
& SHIFT_PRESSED
)
1457 ctrl
&= ~SHIFT_PRESSED
;
1458 set_key_input_record( &records
[count
++], 0, VK_SHIFT
, FALSE
, ctrl
);
1461 return write_console_input( console
, records
, count
, FALSE
);
1464 static void char_key_press( struct console
*console
, WCHAR ch
, unsigned int ctrl
)
1466 unsigned int vk
= VkKeyScanW( ch
);
1467 if (vk
== ~0) vk
= 0;
1468 if (vk
& 0x0100) ctrl
|= SHIFT_PRESSED
;
1469 if (vk
& 0x0200) ctrl
|= LEFT_CTRL_PRESSED
;
1470 if (vk
& 0x0400) ctrl
|= LEFT_ALT_PRESSED
;
1472 key_press( console
, ch
, vk
, ctrl
);
1475 static unsigned int escape_char_to_vk( WCHAR ch
)
1479 case 'A': return VK_UP
;
1480 case 'B': return VK_DOWN
;
1481 case 'C': return VK_RIGHT
;
1482 case 'D': return VK_LEFT
;
1483 case 'H': return VK_HOME
;
1484 case 'F': return VK_END
;
1485 case 'P': return VK_F1
;
1486 case 'Q': return VK_F2
;
1487 case 'R': return VK_F3
;
1488 case 'S': return VK_F4
;
1493 static unsigned int escape_number_to_vk( unsigned int n
)
1497 case 2: return VK_INSERT
;
1498 case 3: return VK_DELETE
;
1499 case 5: return VK_PRIOR
;
1500 case 6: return VK_NEXT
;
1501 case 15: return VK_F5
;
1502 case 17: return VK_F6
;
1503 case 18: return VK_F7
;
1504 case 19: return VK_F8
;
1505 case 20: return VK_F9
;
1506 case 21: return VK_F10
;
1507 case 23: return VK_F11
;
1508 case 24: return VK_F12
;
1513 static unsigned int convert_modifiers( unsigned int n
)
1515 unsigned int ctrl
= 0;
1516 if (!n
|| n
> 16) return 0;
1518 if (n
& 1) ctrl
|= SHIFT_PRESSED
;
1519 if (n
& 2) ctrl
|= LEFT_ALT_PRESSED
;
1520 if (n
& 4) ctrl
|= LEFT_CTRL_PRESSED
;
1524 static unsigned int process_csi_sequence( struct console
*console
, const WCHAR
*buf
, size_t size
)
1526 unsigned int n
, count
= 0, params
[8], params_cnt
= 0, vk
;
1531 while (count
< size
&& '0' <= buf
[count
] && buf
[count
] <= '9')
1532 n
= n
* 10 + buf
[count
++] - '0';
1533 if (params_cnt
< ARRAY_SIZE(params
)) params
[params_cnt
++] = n
;
1534 else FIXME( "too many params, skipping %u\n", n
);
1535 if (count
== size
) return 0;
1536 if (buf
[count
] != ';') break;
1537 if (++count
== size
) return 0;
1540 if ((vk
= escape_char_to_vk( buf
[count
] )))
1542 key_press( console
, 0, vk
, params_cnt
>= 2 ? convert_modifiers( params
[1] ) : 0 );
1549 vk
= escape_number_to_vk( params
[0] );
1550 key_press( console
, 0, vk
, params_cnt
== 2 ? convert_modifiers( params
[1] ) : 0 );
1554 FIXME( "unhandled sequence %s\n", debugstr_wn( buf
, size
));
1559 static unsigned int process_input_escape( struct console
*console
, const WCHAR
*buf
, size_t size
)
1561 unsigned int vk
= 0, count
= 0, nlen
;
1565 key_press( console
, 0, VK_ESCAPE
, 0 );
1572 if (++count
== size
) break;
1573 if ((nlen
= process_csi_sequence( console
, buf
+ 1, size
- 1 ))) return count
+ nlen
;
1577 if (++count
== size
) break;
1578 vk
= escape_char_to_vk( buf
[1] );
1581 key_press( console
, 0, vk
, 0 );
1586 char_key_press( console
, buf
[0], LEFT_ALT_PRESSED
);
1590 static DWORD WINAPI
tty_input( void *param
)
1592 struct console
*console
= param
;
1595 char read_buf
[4096];
1601 if (console
->is_unix
)
1603 unsigned int h
= condrv_handle( console
->tty_input
);
1604 status
= NtDeviceIoControlFile( console
->server
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_SETUP_INPUT
,
1605 &h
, sizeof(h
), NULL
, 0 );
1606 if (status
) ERR( "input setup failed: %#x\n", status
);
1609 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1613 status
= NtReadFile( console
->tty_input
, event
, NULL
, NULL
, &io
, read_buf
, sizeof(read_buf
), NULL
, NULL
);
1614 if (status
== STATUS_PENDING
)
1616 if ((status
= NtWaitForSingleObject( event
, FALSE
, NULL
))) break;
1621 EnterCriticalSection( &console_section
);
1622 signaled
= console
->record_count
!= 0;
1624 /* FIXME: Handle partial char read */
1625 count
= MultiByteToWideChar( get_tty_cp( console
), 0, read_buf
, io
.Information
, buf
, ARRAY_SIZE(buf
) );
1627 TRACE( "%s\n", debugstr_wn(buf
, count
) );
1629 for (i
= 0; i
< count
; i
++)
1634 case 3: /* end of text */
1635 LeaveCriticalSection( &console_section
);
1638 key_press( console
, '\n', VK_RETURN
, LEFT_CTRL_PRESSED
);
1641 key_press( console
, ch
, 'H', LEFT_CTRL_PRESSED
);
1644 i
+= process_input_escape( console
, buf
+ i
+ 1, count
- i
- 1 );
1647 key_press( console
, '\b', VK_BACK
, 0 );
1650 char_key_press( console
, ch
, 0 );
1654 process_console_input( console
);
1655 if (!signaled
&& console
->record_count
)
1657 assert( !console
->read_ioctl
);
1658 read_complete( console
, STATUS_SUCCESS
, NULL
, 0, TRUE
); /* signal console */
1660 LeaveCriticalSection( &console_section
);
1663 TRACE( "NtReadFile failed: %#x\n", status
);
1666 EnterCriticalSection( &console_section
);
1667 if (console
->read_ioctl
) read_complete( console
, status
, NULL
, 0, FALSE
);
1668 if (console
->is_unix
)
1671 status
= NtDeviceIoControlFile( console
->server
, NULL
, NULL
, NULL
, &io
, IOCTL_CONDRV_SETUP_INPUT
,
1672 &h
, sizeof(h
), NULL
, 0 );
1673 if (status
) ERR( "input restore failed: %#x\n", status
);
1675 CloseHandle( console
->input_thread
);
1676 console
->input_thread
= NULL
;
1677 LeaveCriticalSection( &console_section
);
1682 static BOOL
ensure_tty_input_thread( struct console
*console
)
1684 if (!console
->tty_input
) return TRUE
;
1685 if (!console
->input_thread
)
1686 console
->input_thread
= CreateThread( NULL
, 0, tty_input
, console
, 0, NULL
);
1687 return console
->input_thread
!= NULL
;
1690 static NTSTATUS
screen_buffer_activate( struct screen_buffer
*screen_buffer
)
1693 TRACE( "%p\n", screen_buffer
);
1694 screen_buffer
->console
->active
= screen_buffer
;
1695 SetRect( &update_rect
, 0, 0, screen_buffer
->width
- 1, screen_buffer
->height
- 1 );
1696 update_output( screen_buffer
, &update_rect
);
1697 tty_sync( screen_buffer
->console
);
1698 update_window_config( screen_buffer
->console
);
1699 return STATUS_SUCCESS
;
1702 static NTSTATUS
get_output_info( struct screen_buffer
*screen_buffer
, size_t *out_size
)
1704 struct condrv_output_info
*info
;
1706 *out_size
= min( *out_size
, sizeof(*info
) + screen_buffer
->font
.face_len
);
1707 if (!(info
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
1709 info
->cursor_size
= screen_buffer
->cursor_size
;
1710 info
->cursor_visible
= screen_buffer
->cursor_visible
;
1711 info
->cursor_x
= screen_buffer
->cursor_x
;
1712 info
->cursor_y
= screen_buffer
->cursor_y
;
1713 info
->width
= screen_buffer
->width
;
1714 info
->height
= screen_buffer
->height
;
1715 info
->attr
= screen_buffer
->attr
;
1716 info
->popup_attr
= screen_buffer
->popup_attr
;
1717 info
->win_left
= screen_buffer
->win
.left
;
1718 info
->win_top
= screen_buffer
->win
.top
;
1719 info
->win_right
= screen_buffer
->win
.right
;
1720 info
->win_bottom
= screen_buffer
->win
.bottom
;
1721 info
->max_width
= screen_buffer
->max_width
;
1722 info
->max_height
= screen_buffer
->max_height
;
1723 info
->font_width
= screen_buffer
->font
.width
;
1724 info
->font_height
= screen_buffer
->font
.height
;
1725 info
->font_weight
= screen_buffer
->font
.weight
;
1726 info
->font_pitch_family
= screen_buffer
->font
.pitch_family
;
1727 memcpy( info
->color_map
, screen_buffer
->color_map
, sizeof(info
->color_map
) );
1728 if (*out_size
> sizeof(*info
)) memcpy( info
+ 1, screen_buffer
->font
.face_name
, *out_size
- sizeof(*info
) );
1730 TRACE( "%p cursor_size=%u cursor_visible=%x cursor=(%u,%u) width=%u height=%u win=%s attr=%x popup_attr=%x"
1731 " font_width=%u font_height=%u %s\n", screen_buffer
, info
->cursor_size
, info
->cursor_visible
,
1732 info
->cursor_x
, info
->cursor_y
, info
->width
, info
->height
, wine_dbgstr_rect(&screen_buffer
->win
),
1733 info
->attr
, info
->popup_attr
, info
->font_width
, info
->font_height
,
1734 debugstr_wn( (const WCHAR
*)(info
+ 1), (*out_size
- sizeof(*info
)) / sizeof(WCHAR
) ) );
1735 return STATUS_SUCCESS
;
1738 NTSTATUS
change_screen_buffer_size( struct screen_buffer
*screen_buffer
, int new_width
, int new_height
)
1740 int i
, old_width
, old_height
, copy_width
, copy_height
;
1741 char_info_t
*new_data
;
1743 if (!(new_data
= malloc( new_width
* new_height
* sizeof(*new_data
) ))) return STATUS_NO_MEMORY
;
1745 old_width
= screen_buffer
->width
;
1746 old_height
= screen_buffer
->height
;
1747 copy_width
= min( old_width
, new_width
);
1748 copy_height
= min( old_height
, new_height
);
1750 /* copy all the rows */
1751 for (i
= 0; i
< copy_height
; i
++)
1753 memcpy( &new_data
[i
* new_width
], &screen_buffer
->data
[i
* old_width
],
1754 copy_width
* sizeof(char_info_t
) );
1757 /* clear the end of each row */
1758 if (new_width
> old_width
)
1760 /* fill first row */
1761 for (i
= old_width
; i
< new_width
; i
++) new_data
[i
] = empty_char_info
;
1762 /* and blast it to the other rows */
1763 for (i
= 1; i
< copy_height
; i
++)
1764 memcpy( &new_data
[i
* new_width
+ old_width
], &new_data
[old_width
],
1765 (new_width
- old_width
) * sizeof(char_info_t
) );
1768 /* clear remaining rows */
1769 if (new_height
> old_height
)
1771 /* fill first row */
1772 for (i
= 0; i
< new_width
; i
++) new_data
[old_height
* new_width
+ i
] = empty_char_info
;
1773 /* and blast it to the other rows */
1774 for (i
= old_height
+1; i
< new_height
; i
++)
1775 memcpy( &new_data
[i
* new_width
], &new_data
[old_height
* new_width
],
1776 new_width
* sizeof(char_info_t
) );
1778 free( screen_buffer
->data
);
1779 screen_buffer
->data
= new_data
;
1780 screen_buffer
->width
= new_width
;
1781 screen_buffer
->height
= new_height
;
1782 return STATUS_SUCCESS
;
1785 static NTSTATUS
set_output_info( struct screen_buffer
*screen_buffer
,
1786 const struct condrv_output_info_params
*params
, size_t extra_size
)
1788 const struct condrv_output_info
*info
= ¶ms
->info
;
1791 TRACE( "%p\n", screen_buffer
);
1793 extra_size
-= sizeof(*params
);
1795 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
)
1797 if (info
->cursor_size
< 1 || info
->cursor_size
> 100) return STATUS_INVALID_PARAMETER
;
1799 screen_buffer
->cursor_size
= info
->cursor_size
;
1800 screen_buffer
->cursor_visible
= !!info
->cursor_visible
;
1802 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
)
1804 if (info
->cursor_x
< 0 || info
->cursor_x
>= screen_buffer
->width
||
1805 info
->cursor_y
< 0 || info
->cursor_y
>= screen_buffer
->height
)
1807 return STATUS_INVALID_PARAMETER
;
1810 if (screen_buffer
->cursor_x
!= info
->cursor_x
|| screen_buffer
->cursor_y
!= info
->cursor_y
)
1812 screen_buffer
->cursor_x
= info
->cursor_x
;
1813 screen_buffer
->cursor_y
= info
->cursor_y
;
1814 scroll_to_cursor( screen_buffer
);
1817 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_SIZE
)
1819 /* new screen-buffer cannot be smaller than actual window */
1820 if (info
->width
< screen_buffer
->win
.right
- screen_buffer
->win
.left
+ 1 ||
1821 info
->height
< screen_buffer
->win
.bottom
- screen_buffer
->win
.top
+ 1)
1823 return STATUS_INVALID_PARAMETER
;
1825 /* FIXME: there are also some basic minimum and max size to deal with */
1826 if ((status
= change_screen_buffer_size( screen_buffer
, info
->width
, info
->height
))) return status
;
1828 /* scroll window to display sb */
1829 if (screen_buffer
->win
.right
>= info
->width
)
1831 screen_buffer
->win
.right
-= screen_buffer
->win
.left
;
1832 screen_buffer
->win
.left
= 0;
1834 if (screen_buffer
->win
.bottom
>= info
->height
)
1836 screen_buffer
->win
.bottom
-= screen_buffer
->win
.top
;
1837 screen_buffer
->win
.top
= 0;
1839 if (screen_buffer
->cursor_x
>= info
->width
) screen_buffer
->cursor_x
= info
->width
- 1;
1840 if (screen_buffer
->cursor_y
>= info
->height
) screen_buffer
->cursor_y
= info
->height
- 1;
1842 if (is_active( screen_buffer
) && screen_buffer
->console
->mode
& ENABLE_WINDOW_INPUT
)
1845 ir
.EventType
= WINDOW_BUFFER_SIZE_EVENT
;
1846 ir
.Event
.WindowBufferSizeEvent
.dwSize
.X
= info
->width
;
1847 ir
.Event
.WindowBufferSizeEvent
.dwSize
.Y
= info
->height
;
1848 write_console_input( screen_buffer
->console
, &ir
, 1, TRUE
);
1851 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_ATTR
)
1853 screen_buffer
->attr
= info
->attr
;
1855 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR
)
1857 screen_buffer
->popup_attr
= info
->popup_attr
;
1859 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
)
1861 if (info
->win_left
< 0 || info
->win_left
> info
->win_right
||
1862 info
->win_right
>= screen_buffer
->width
||
1863 info
->win_top
< 0 || info
->win_top
> info
->win_bottom
||
1864 info
->win_bottom
>= screen_buffer
->height
)
1866 return STATUS_INVALID_PARAMETER
;
1868 if (screen_buffer
->win
.left
!= info
->win_left
|| screen_buffer
->win
.top
!= info
->win_top
||
1869 screen_buffer
->win
.right
!= info
->win_right
|| screen_buffer
->win
.bottom
!= info
->win_bottom
)
1871 screen_buffer
->win
.left
= info
->win_left
;
1872 screen_buffer
->win
.top
= info
->win_top
;
1873 screen_buffer
->win
.right
= info
->win_right
;
1874 screen_buffer
->win
.bottom
= info
->win_bottom
;
1877 if (params
->mask
& SET_CONSOLE_OUTPUT_INFO_MAX_SIZE
)
1879 screen_buffer
->max_width
= info
->max_width
;
1880 screen_buffer
->max_height
= info
->max_height
;
1883 if (is_active( screen_buffer
))
1885 tty_sync( screen_buffer
->console
);
1886 update_window_config( screen_buffer
->console
);
1888 return STATUS_SUCCESS
;
1891 static NTSTATUS
write_console( struct screen_buffer
*screen_buffer
, const WCHAR
*buffer
, size_t len
)
1896 TRACE( "%s\n", debugstr_wn(buffer
, len
) );
1898 empty_update_rect( screen_buffer
, &update_rect
);
1900 for (i
= 0; i
< len
; i
++)
1902 if (screen_buffer
->mode
& ENABLE_PROCESSED_OUTPUT
)
1907 if (screen_buffer
->cursor_x
) screen_buffer
->cursor_x
--;
1910 j
= min( screen_buffer
->width
- screen_buffer
->cursor_x
, 8 - (screen_buffer
->cursor_x
% 8) );
1911 while (j
--) write_char( screen_buffer
, ' ', &update_rect
, NULL
);
1914 screen_buffer
->cursor_x
= 0;
1915 if (++screen_buffer
->cursor_y
== screen_buffer
->height
)
1916 new_line( screen_buffer
, &update_rect
);
1917 else if (screen_buffer
->mode
& ENABLE_WRAP_AT_EOL_OUTPUT
)
1919 update_output( screen_buffer
, &update_rect
);
1920 set_tty_cursor( screen_buffer
->console
, screen_buffer
->cursor_x
, screen_buffer
->cursor_y
);
1927 screen_buffer
->cursor_x
= 0;
1931 if (screen_buffer
->cursor_x
== screen_buffer
->width
&& !(screen_buffer
->mode
& ENABLE_WRAP_AT_EOL_OUTPUT
))
1932 screen_buffer
->cursor_x
= update_rect
.left
;
1933 write_char( screen_buffer
, buffer
[i
], &update_rect
, NULL
);
1936 if (screen_buffer
->cursor_x
== screen_buffer
->width
)
1938 if (screen_buffer
->mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) screen_buffer
->cursor_x
--;
1939 else screen_buffer
->cursor_x
= update_rect
.left
;
1942 scroll_to_cursor( screen_buffer
);
1943 update_output( screen_buffer
, &update_rect
);
1944 tty_sync( screen_buffer
->console
);
1945 update_window_config( screen_buffer
->console
);
1946 return STATUS_SUCCESS
;
1949 static NTSTATUS
write_output( struct screen_buffer
*screen_buffer
, const struct condrv_output_params
*params
,
1950 size_t in_size
, size_t *out_size
)
1952 unsigned int i
, entry_size
, entry_cnt
, x
, y
;
1956 if (*out_size
== sizeof(SMALL_RECT
) && !params
->width
) return STATUS_INVALID_PARAMETER
;
1958 entry_size
= params
->mode
== CHAR_INFO_MODE_TEXTATTR
? sizeof(char_info_t
) : sizeof(WCHAR
);
1959 entry_cnt
= (in_size
- sizeof(*params
)) / entry_size
;
1961 TRACE( "(%u,%u) cnt %u\n", params
->x
, params
->y
, entry_cnt
);
1963 if (params
->x
>= screen_buffer
->width
)
1966 return STATUS_SUCCESS
;
1969 for (i
= 0, src
= (char *)(params
+ 1); i
< entry_cnt
; i
++, src
+= entry_size
)
1973 x
= params
->x
+ i
% params
->width
;
1974 y
= params
->y
+ i
/ params
->width
;
1975 if (x
>= screen_buffer
->width
) continue;
1979 x
= (params
->x
+ i
) % screen_buffer
->width
;
1980 y
= params
->y
+ (params
->x
+ i
) / screen_buffer
->width
;
1982 if (y
>= screen_buffer
->height
) break;
1984 dest
= &screen_buffer
->data
[y
* screen_buffer
->width
+ x
];
1985 switch(params
->mode
)
1987 case CHAR_INFO_MODE_TEXT
:
1988 dest
->ch
= *(const WCHAR
*)src
;
1990 case CHAR_INFO_MODE_ATTR
:
1991 dest
->attr
= *(const unsigned short *)src
;
1993 case CHAR_INFO_MODE_TEXTATTR
:
1994 *dest
= *(const char_info_t
*)src
;
1997 return STATUS_INVALID_PARAMETER
;
2001 if (i
&& is_active( screen_buffer
))
2005 update_rect
.left
= params
->x
;
2006 update_rect
.top
= params
->y
;
2009 update_rect
.bottom
= min( params
->y
+ entry_cnt
/ params
->width
, screen_buffer
->height
) - 1;
2010 update_rect
.right
= min( params
->x
+ params
->width
, screen_buffer
->width
) - 1;
2014 update_rect
.bottom
= params
->y
+ (params
->x
+ i
- 1) / screen_buffer
->width
;
2015 if (update_rect
.bottom
!= params
->y
)
2017 update_rect
.left
= 0;
2018 update_rect
.right
= screen_buffer
->width
- 1;
2022 update_rect
.right
= params
->x
+ i
- 1;
2025 update_output( screen_buffer
, &update_rect
);
2026 tty_sync( screen_buffer
->console
);
2029 if (*out_size
== sizeof(SMALL_RECT
))
2032 unsigned int width
= params
->width
;
2035 if (!(region
= alloc_ioctl_buffer( sizeof(*region
)))) return STATUS_NO_MEMORY
;
2038 region
->Right
= min( x
+ width
, screen_buffer
->width
) - 1;
2039 region
->Bottom
= min( y
+ entry_cnt
/ width
, screen_buffer
->height
) - 1;
2044 if (!(result
= alloc_ioctl_buffer( sizeof(*result
)))) return STATUS_NO_MEMORY
;
2048 return STATUS_SUCCESS
;
2051 static NTSTATUS
read_output( struct screen_buffer
*screen_buffer
, const struct condrv_output_params
*params
,
2054 enum char_info_mode mode
;
2055 unsigned int x
, y
, width
;
2056 unsigned int i
, count
;
2060 mode
= params
->mode
;
2061 width
= params
->width
;
2062 TRACE( "(%u %u) mode %u width %u\n", x
, y
, mode
, width
);
2066 case CHAR_INFO_MODE_TEXT
:
2070 if (x
>= screen_buffer
->width
|| y
>= screen_buffer
->height
)
2073 return STATUS_SUCCESS
;
2075 src
= screen_buffer
->data
+ y
* screen_buffer
->width
+ x
;
2076 count
= min( screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
- src
,
2077 *out_size
/ sizeof(*data
) );
2078 *out_size
= count
* sizeof(*data
);
2079 if (!(data
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2080 for (i
= 0; i
< count
; i
++) data
[i
] = src
[i
].ch
;
2083 case CHAR_INFO_MODE_ATTR
:
2085 unsigned short *data
;
2087 if (x
>= screen_buffer
->width
|| y
>= screen_buffer
->height
)
2090 return STATUS_SUCCESS
;
2092 src
= screen_buffer
->data
+ y
* screen_buffer
->width
+ x
;
2093 count
= min( screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
- src
,
2094 *out_size
/ sizeof(*data
) );
2095 *out_size
= count
* sizeof(*data
);
2096 if (!(data
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2097 for (i
= 0; i
< count
; i
++) data
[i
] = src
[i
].attr
;
2100 case CHAR_INFO_MODE_TEXTATTR
:
2104 if (!width
|| *out_size
< sizeof(*region
) || x
>= screen_buffer
->width
|| y
>= screen_buffer
->height
)
2105 return STATUS_INVALID_PARAMETER
;
2106 count
= min( (*out_size
- sizeof(*region
)) / (width
* sizeof(*data
)), screen_buffer
->height
- y
);
2107 width
= min( width
, screen_buffer
->width
- x
);
2108 *out_size
= sizeof(*region
) + width
* count
* sizeof(*data
);
2109 if (!(region
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2112 region
->Right
= x
+ width
- 1;
2113 region
->Bottom
= y
+ count
- 1;
2114 data
= (char_info_t
*)(region
+ 1);
2115 for (i
= 0; i
< count
; i
++)
2117 memcpy( &data
[i
* width
], &screen_buffer
->data
[(y
+ i
) * screen_buffer
->width
+ x
],
2118 width
* sizeof(*data
) );
2123 return STATUS_INVALID_PARAMETER
;
2126 return STATUS_SUCCESS
;
2129 static NTSTATUS
fill_output( struct screen_buffer
*screen_buffer
, const struct condrv_fill_output_params
*params
)
2131 char_info_t
*end
, *dest
;
2132 DWORD i
, count
, *result
;
2134 TRACE( "(%u %u) mode %u\n", params
->x
, params
->y
, params
->mode
);
2136 if (params
->y
>= screen_buffer
->height
) return STATUS_SUCCESS
;
2137 dest
= screen_buffer
->data
+ min( params
->y
* screen_buffer
->width
+ params
->x
,
2138 screen_buffer
->height
* screen_buffer
->width
);
2140 end
= screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
;
2142 count
= params
->count
;
2143 if (count
> end
- dest
) count
= end
- dest
;
2145 switch(params
->mode
)
2147 case CHAR_INFO_MODE_TEXT
:
2148 for (i
= 0; i
< count
; i
++) dest
[i
].ch
= params
->ch
;
2150 case CHAR_INFO_MODE_ATTR
:
2151 for (i
= 0; i
< count
; i
++) dest
[i
].attr
= params
->attr
;
2153 case CHAR_INFO_MODE_TEXTATTR
:
2154 for (i
= 0; i
< count
; i
++)
2156 dest
[i
].ch
= params
->ch
;
2157 dest
[i
].attr
= params
->attr
;
2161 return STATUS_INVALID_PARAMETER
;
2164 if (count
&& is_active(screen_buffer
))
2167 SetRect( &update_rect
,
2168 params
->x
% screen_buffer
->width
,
2169 params
->y
+ params
->x
/ screen_buffer
->width
,
2170 (params
->x
+ i
- 1) % screen_buffer
->width
,
2171 params
->y
+ (params
->x
+ i
- 1) / screen_buffer
->width
);
2172 update_output( screen_buffer
, &update_rect
);
2173 tty_sync( screen_buffer
->console
);
2176 if (!(result
= alloc_ioctl_buffer( sizeof(*result
) ))) return STATUS_NO_MEMORY
;
2178 return STATUS_SUCCESS
;
2181 static NTSTATUS
scroll_output( struct screen_buffer
*screen_buffer
, const struct condrv_scroll_params
*params
)
2183 int x
, y
, xsrc
, ysrc
, w
, h
;
2184 char_info_t
*psrc
, *pdst
;
2185 SMALL_RECT src
, dst
;
2189 xsrc
= params
->scroll
.Left
;
2190 ysrc
= params
->scroll
.Top
;
2191 w
= params
->scroll
.Right
- params
->scroll
.Left
+ 1;
2192 h
= params
->scroll
.Bottom
- params
->scroll
.Top
+ 1;
2194 TRACE( "(%d %d) -> (%u %u) w %u h %u\n", xsrc
, ysrc
, params
->origin
.X
, params
->origin
.Y
, w
, h
);
2196 clip
.Left
= max( params
->clip
.Left
, 0 );
2197 clip
.Top
= max( params
->clip
.Top
, 0 );
2198 clip
.Right
= min( params
->clip
.Right
, screen_buffer
->width
- 1 );
2199 clip
.Bottom
= min( params
->clip
.Bottom
, screen_buffer
->height
- 1 );
2200 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
|| params
->scroll
.Left
< 0 || params
->scroll
.Top
< 0 ||
2201 params
->scroll
.Right
>= screen_buffer
->width
|| params
->scroll
.Bottom
>= screen_buffer
->height
||
2202 params
->scroll
.Right
< params
->scroll
.Left
|| params
->scroll
.Top
> params
->scroll
.Bottom
||
2203 params
->origin
.X
< 0 || params
->origin
.X
>= screen_buffer
->width
|| params
->origin
.Y
< 0 ||
2204 params
->origin
.Y
>= screen_buffer
->height
)
2205 return STATUS_INVALID_PARAMETER
;
2207 src
.Left
= max( xsrc
, clip
.Left
);
2208 src
.Top
= max( ysrc
, clip
.Top
);
2209 src
.Right
= min( xsrc
+ w
- 1, clip
.Right
);
2210 src
.Bottom
= min( ysrc
+ h
- 1, clip
.Bottom
);
2212 dst
.Left
= params
->origin
.X
;
2213 dst
.Top
= params
->origin
.Y
;
2214 dst
.Right
= params
->origin
.X
+ w
- 1;
2215 dst
.Bottom
= params
->origin
.Y
+ h
- 1;
2217 if (dst
.Left
< clip
.Left
)
2219 xsrc
+= clip
.Left
- dst
.Left
;
2220 w
-= clip
.Left
- dst
.Left
;
2221 dst
.Left
= clip
.Left
;
2223 if (dst
.Top
< clip
.Top
)
2225 ysrc
+= clip
.Top
- dst
.Top
;
2226 h
-= clip
.Top
- dst
.Top
;
2229 if (dst
.Right
> clip
.Right
) w
-= dst
.Right
- clip
.Right
;
2230 if (dst
.Bottom
> clip
.Bottom
) h
-= dst
.Bottom
- clip
.Bottom
;
2236 psrc
= &screen_buffer
->data
[(ysrc
+ h
- 1) * screen_buffer
->width
+ xsrc
];
2237 pdst
= &screen_buffer
->data
[(dst
.Top
+ h
- 1) * screen_buffer
->width
+ dst
.Left
];
2239 for (y
= h
; y
> 0; y
--)
2241 memcpy( pdst
, psrc
, w
* sizeof(*pdst
) );
2242 pdst
-= screen_buffer
->width
;
2243 psrc
-= screen_buffer
->width
;
2248 psrc
= &screen_buffer
->data
[ysrc
* screen_buffer
->width
+ xsrc
];
2249 pdst
= &screen_buffer
->data
[dst
.Top
* screen_buffer
->width
+ dst
.Left
];
2251 for (y
= 0; y
< h
; y
++)
2253 /* we use memmove here because when psrc and pdst are the same,
2254 * copies are done on the same row, so the dst and src blocks
2256 memmove( pdst
, psrc
, w
* sizeof(*pdst
) );
2257 pdst
+= screen_buffer
->width
;
2258 psrc
+= screen_buffer
->width
;
2263 for (y
= src
.Top
; y
<= src
.Bottom
; y
++)
2265 int left
= src
.Left
;
2266 int right
= src
.Right
;
2267 if (dst
.Top
<= y
&& y
<= dst
.Bottom
)
2269 if (dst
.Left
<= src
.Left
) left
= max( left
, dst
.Right
+ 1 );
2270 if (dst
.Left
>= src
.Left
) right
= min( right
, dst
.Left
- 1 );
2272 for (x
= left
; x
<= right
; x
++) screen_buffer
->data
[y
* screen_buffer
->width
+ x
] = params
->fill
;
2275 SetRect( &update_rect
, min( src
.Left
, dst
.Left
), min( src
.Top
, dst
.Top
),
2276 max( src
.Right
, dst
.Right
), max( src
.Bottom
, dst
.Bottom
));
2277 update_output( screen_buffer
, &update_rect
);
2278 tty_sync( screen_buffer
->console
);
2279 return STATUS_SUCCESS
;
2282 static NTSTATUS
set_console_title( struct console
*console
, const WCHAR
*in_title
, size_t size
)
2284 WCHAR
*title
= NULL
;
2286 TRACE( "%s\n", debugstr_wn(in_title
, size
/ sizeof(WCHAR
)) );
2290 if (!(title
= malloc( size
+ sizeof(WCHAR
) ))) return STATUS_NO_MEMORY
;
2291 memcpy( title
, in_title
, size
);
2292 title
[size
/ sizeof(WCHAR
)] = 0;
2294 free( console
->title
);
2295 console
->title
= title
;
2297 if (console
->tty_output
)
2302 tty_write( console
, "\x1b]0;", 4 );
2303 len
= WideCharToMultiByte( get_tty_cp( console
), 0, console
->title
, size
/ sizeof(WCHAR
),
2304 NULL
, 0, NULL
, NULL
);
2305 if ((vt
= tty_alloc_buffer( console
, len
)))
2306 WideCharToMultiByte( get_tty_cp( console
), 0, console
->title
, size
/ sizeof(WCHAR
),
2307 vt
, len
, NULL
, NULL
);
2308 tty_write( console
, "\x07", 1 );
2309 tty_sync( console
);
2312 SetWindowTextW( console
->win
, console
->title
);
2313 return STATUS_SUCCESS
;
2316 static NTSTATUS
screen_buffer_ioctl( struct screen_buffer
*screen_buffer
, unsigned int code
,
2317 const void *in_data
, size_t in_size
, size_t *out_size
)
2321 case IOCTL_CONDRV_CLOSE_OUTPUT
:
2322 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2323 destroy_screen_buffer( screen_buffer
);
2324 return STATUS_SUCCESS
;
2326 case IOCTL_CONDRV_ACTIVATE
:
2327 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2328 return screen_buffer_activate( screen_buffer
);
2330 case IOCTL_CONDRV_GET_MODE
:
2333 TRACE( "returning mode %x\n", screen_buffer
->mode
);
2334 if (in_size
|| *out_size
!= sizeof(*mode
)) return STATUS_INVALID_PARAMETER
;
2335 if (!(mode
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2336 *mode
= screen_buffer
->mode
;
2337 return STATUS_SUCCESS
;
2340 case IOCTL_CONDRV_SET_MODE
:
2341 if (in_size
!= sizeof(unsigned int) || *out_size
) return STATUS_INVALID_PARAMETER
;
2342 screen_buffer
->mode
= *(unsigned int *)in_data
;
2343 TRACE( "set %x mode\n", screen_buffer
->mode
);
2344 return STATUS_SUCCESS
;
2346 case IOCTL_CONDRV_WRITE_CONSOLE
:
2347 if (in_size
% sizeof(WCHAR
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2348 return write_console( screen_buffer
, in_data
, in_size
/ sizeof(WCHAR
) );
2350 case IOCTL_CONDRV_WRITE_FILE
:
2356 len
= MultiByteToWideChar( screen_buffer
->console
->output_cp
, 0, in_data
, in_size
,
2358 if (!len
) return STATUS_SUCCESS
;
2359 if (!(buf
= malloc( len
* sizeof(WCHAR
) ))) return STATUS_NO_MEMORY
;
2360 MultiByteToWideChar( screen_buffer
->console
->output_cp
, 0, in_data
, in_size
, buf
, len
);
2361 status
= write_console( screen_buffer
, buf
, len
);
2366 case IOCTL_CONDRV_WRITE_OUTPUT
:
2367 if ((*out_size
!= sizeof(DWORD
) && *out_size
!= sizeof(SMALL_RECT
)) ||
2368 in_size
< sizeof(struct condrv_output_params
))
2369 return STATUS_INVALID_PARAMETER
;
2370 return write_output( screen_buffer
, in_data
, in_size
, out_size
);
2372 case IOCTL_CONDRV_READ_OUTPUT
:
2373 if (in_size
!= sizeof(struct condrv_output_params
)) return STATUS_INVALID_PARAMETER
;
2374 return read_output( screen_buffer
, in_data
, out_size
);
2376 case IOCTL_CONDRV_GET_OUTPUT_INFO
:
2377 if (in_size
|| *out_size
< sizeof(struct condrv_output_info
)) return STATUS_INVALID_PARAMETER
;
2378 return get_output_info( screen_buffer
, out_size
);
2380 case IOCTL_CONDRV_SET_OUTPUT_INFO
:
2381 if (in_size
< sizeof(struct condrv_output_info
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2382 return set_output_info( screen_buffer
, in_data
, in_size
);
2384 case IOCTL_CONDRV_FILL_OUTPUT
:
2385 if (in_size
!= sizeof(struct condrv_fill_output_params
) || *out_size
!= sizeof(DWORD
))
2386 return STATUS_INVALID_PARAMETER
;
2387 return fill_output( screen_buffer
, in_data
);
2389 case IOCTL_CONDRV_SCROLL
:
2390 if (in_size
!= sizeof(struct condrv_scroll_params
) || *out_size
)
2391 return STATUS_INVALID_PARAMETER
;
2392 return scroll_output( screen_buffer
, in_data
);
2395 WARN( "invalid ioctl %x\n", code
);
2396 return STATUS_INVALID_HANDLE
;
2400 static NTSTATUS
console_input_ioctl( struct console
*console
, unsigned int code
, const void *in_data
,
2401 size_t in_size
, size_t *out_size
)
2407 case IOCTL_CONDRV_GET_MODE
:
2410 TRACE( "returning mode %x\n", console
->mode
);
2411 if (in_size
|| *out_size
!= sizeof(*mode
)) return STATUS_INVALID_PARAMETER
;
2412 if (!(mode
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2413 *mode
= console
->mode
;
2414 return STATUS_SUCCESS
;
2417 case IOCTL_CONDRV_SET_MODE
:
2418 if (in_size
!= sizeof(unsigned int) || *out_size
) return STATUS_INVALID_PARAMETER
;
2419 console
->mode
= *(unsigned int *)in_data
;
2420 TRACE( "set %x mode\n", console
->mode
);
2421 return STATUS_SUCCESS
;
2423 case IOCTL_CONDRV_READ_CONSOLE
:
2424 if (in_size
|| *out_size
% sizeof(WCHAR
)) return STATUS_INVALID_PARAMETER
;
2425 ensure_tty_input_thread( console
);
2426 status
= read_console( console
, code
, *out_size
);
2430 case IOCTL_CONDRV_READ_FILE
:
2431 ensure_tty_input_thread( console
);
2432 status
= read_console( console
, code
, *out_size
);
2436 case IOCTL_CONDRV_READ_INPUT
:
2438 if (in_size
) return STATUS_INVALID_PARAMETER
;
2439 ensure_tty_input_thread( console
);
2440 if (!console
->record_count
&& *out_size
)
2442 TRACE( "pending read\n" );
2443 console
->read_ioctl
= IOCTL_CONDRV_READ_INPUT
;
2444 console
->pending_read
= *out_size
;
2445 return STATUS_PENDING
;
2447 status
= read_console_input( console
, *out_size
);
2452 case IOCTL_CONDRV_WRITE_INPUT
:
2453 if (in_size
% sizeof(INPUT_RECORD
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2454 return write_console_input( console
, in_data
, in_size
/ sizeof(INPUT_RECORD
), TRUE
);
2456 case IOCTL_CONDRV_PEEK
:
2460 if (in_size
) return STATUS_INVALID_PARAMETER
;
2461 ensure_tty_input_thread( console
);
2462 *out_size
= min( *out_size
, console
->record_count
* sizeof(INPUT_RECORD
) );
2463 if (!(result
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2464 if (*out_size
) memcpy( result
, console
->records
, *out_size
);
2465 return STATUS_SUCCESS
;
2468 case IOCTL_CONDRV_GET_INPUT_INFO
:
2470 struct condrv_input_info
*info
;
2471 TRACE( "get info\n" );
2472 if (in_size
|| *out_size
!= sizeof(*info
)) return STATUS_INVALID_PARAMETER
;
2473 if (!(info
= alloc_ioctl_buffer( sizeof(*info
)))) return STATUS_NO_MEMORY
;
2474 info
->input_cp
= console
->input_cp
;
2475 info
->output_cp
= console
->output_cp
;
2476 info
->win
= condrv_handle( console
->win
);
2477 info
->input_count
= console
->record_count
;
2478 return STATUS_SUCCESS
;
2481 case IOCTL_CONDRV_SET_INPUT_INFO
:
2483 const struct condrv_input_info_params
*params
= in_data
;
2484 TRACE( "set info\n" );
2485 if (in_size
!= sizeof(*params
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2486 if (params
->mask
& SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE
)
2488 if (!IsValidCodePage( params
->info
.input_cp
)) return STATUS_INVALID_PARAMETER
;
2489 console
->input_cp
= params
->info
.input_cp
;
2491 if (params
->mask
& SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE
)
2493 if (!IsValidCodePage( params
->info
.output_cp
)) return STATUS_INVALID_PARAMETER
;
2494 console
->output_cp
= params
->info
.output_cp
;
2496 return STATUS_SUCCESS
;
2499 case IOCTL_CONDRV_GET_TITLE
:
2502 if (in_size
) return STATUS_INVALID_PARAMETER
;
2503 TRACE( "returning title %s\n", debugstr_w(console
->title
) );
2504 *out_size
= min( *out_size
, console
->title
? wcslen( console
->title
) * sizeof(WCHAR
) : 0 );
2505 if (!(result
= alloc_ioctl_buffer( *out_size
))) return STATUS_NO_MEMORY
;
2506 if (*out_size
) memcpy( result
, console
->title
, *out_size
);
2507 return STATUS_SUCCESS
;
2510 case IOCTL_CONDRV_SET_TITLE
:
2511 if (in_size
% sizeof(WCHAR
) || *out_size
) return STATUS_INVALID_PARAMETER
;
2512 return set_console_title( console
, in_data
, in_size
);
2514 case IOCTL_CONDRV_BEEP
:
2515 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2516 if (console
->is_unix
)
2518 tty_write( console
, "\a", 1 );
2519 tty_sync( console
);
2521 return STATUS_SUCCESS
;
2523 case IOCTL_CONDRV_FLUSH
:
2524 if (in_size
|| *out_size
) return STATUS_INVALID_PARAMETER
;
2526 console
->record_count
= 0;
2527 return STATUS_SUCCESS
;
2530 FIXME( "unsupported ioctl %x\n", code
);
2531 return STATUS_NOT_SUPPORTED
;
2535 static NTSTATUS
process_console_ioctls( struct console
*console
)
2537 size_t out_size
= 0, in_size
;
2540 NTSTATUS status
= STATUS_SUCCESS
;
2544 if (status
) out_size
= 0;
2546 console
->signaled
= console
->record_count
!= 0;
2547 SERVER_START_REQ( get_next_console_request
)
2549 req
->handle
= wine_server_obj_handle( console
->server
);
2550 req
->status
= status
;
2551 req
->signal
= console
->signaled
;
2552 wine_server_add_data( req
, ioctl_buffer
, out_size
);
2553 wine_server_set_reply( req
, ioctl_buffer
, ioctl_buffer_size
);
2554 status
= wine_server_call( req
);
2556 output
= reply
->output
;
2557 out_size
= reply
->out_size
;
2558 in_size
= wine_server_reply_size( reply
);
2562 if (status
== STATUS_PENDING
) return STATUS_SUCCESS
;
2563 if (status
== STATUS_BUFFER_OVERFLOW
)
2565 if (!alloc_ioctl_buffer( out_size
)) return STATUS_NO_MEMORY
;
2566 status
= STATUS_SUCCESS
;
2571 TRACE( "failed to get next request: %#x\n", status
);
2575 if (code
== IOCTL_CONDRV_INIT_OUTPUT
)
2577 TRACE( "initializing output %x\n", output
);
2578 if (console
->active
)
2579 create_screen_buffer( console
, output
, console
->active
->width
, console
->active
->height
);
2581 create_screen_buffer( console
, output
, 80, 150 );
2585 status
= console_input_ioctl( console
, code
, ioctl_buffer
, in_size
, &out_size
);
2589 struct wine_rb_entry
*entry
;
2590 if (!(entry
= wine_rb_get( &screen_buffer_map
, LongToPtr(output
) )))
2592 ERR( "invalid screen buffer id %x\n", output
);
2593 status
= STATUS_INVALID_HANDLE
;
2597 status
= screen_buffer_ioctl( WINE_RB_ENTRY_VALUE( entry
, struct screen_buffer
, entry
), code
,
2598 ioctl_buffer
, in_size
, &out_size
);
2604 static int main_loop( struct console
*console
, HANDLE signal
)
2606 HANDLE signal_event
= NULL
;
2607 HANDLE wait_handles
[3];
2608 unsigned int wait_cnt
= 0;
2609 unsigned short signal_id
;
2610 IO_STATUS_BLOCK signal_io
;
2617 if (!(signal_event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
))) return 1;
2618 status
= NtReadFile( signal
, signal_event
, NULL
, NULL
, &signal_io
, &signal_id
,
2619 sizeof(signal_id
), NULL
, NULL
);
2620 if (status
&& status
!= STATUS_PENDING
) return 1;
2623 if (!alloc_ioctl_buffer( 4096 )) return 1;
2625 wait_handles
[wait_cnt
++] = console
->server
;
2626 if (signal
) wait_handles
[wait_cnt
++] = signal_event
;
2627 if (console
->input_thread
) wait_handles
[wait_cnt
++] = console
->input_thread
;
2628 pump_msgs
= console
->win
!= NULL
;
2633 res
= MsgWaitForMultipleObjects( wait_cnt
, wait_handles
, FALSE
, INFINITE
, QS_ALLINPUT
);
2635 res
= WaitForMultipleObjects( wait_cnt
, wait_handles
, FALSE
, INFINITE
);
2637 if (res
== WAIT_OBJECT_0
+ wait_cnt
)
2640 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
2642 if (msg
.message
== WM_QUIT
) return 0;
2643 DispatchMessageW(&msg
);
2651 EnterCriticalSection( &console_section
);
2652 status
= process_console_ioctls( console
);
2653 LeaveCriticalSection( &console_section
);
2654 if (status
) return 0;
2657 case WAIT_OBJECT_0
+ 1:
2658 if (signal_io
.Status
|| signal_io
.Information
!= sizeof(signal_id
))
2660 TRACE( "signaled quit\n" );
2663 FIXME( "unimplemented signal %x\n", signal_id
);
2664 status
= NtReadFile( signal
, signal_event
, NULL
, NULL
, &signal_io
, &signal_id
,
2665 sizeof(signal_id
), NULL
, NULL
);
2666 if (status
&& status
!= STATUS_PENDING
) return 1;
2670 TRACE( "wait failed, quit\n");
2678 static LONG WINAPI
handle_ctrl_c( EXCEPTION_POINTERS
*eptr
)
2680 if (eptr
->ExceptionRecord
->ExceptionCode
!= CONTROL_C_EXIT
) return EXCEPTION_CONTINUE_SEARCH
;
2681 /* In Unix mode, ignore ctrl c exceptions. Signals are sent it to clients as well and we will
2682 * terminate the usual way if they don't handle it. */
2683 return EXCEPTION_CONTINUE_EXECUTION
;
2686 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
2688 int headless
= 0, i
, width
= 0, height
= 0;
2689 HANDLE signal
= NULL
;
2692 static struct console console
;
2694 for (i
= 0; i
< argc
; i
++) TRACE("%s ", wine_dbgstr_w(argv
[i
]));
2697 console
.mode
= ENABLE_PROCESSED_INPUT
| ENABLE_LINE_INPUT
|
2698 ENABLE_ECHO_INPUT
| ENABLE_MOUSE_INPUT
| ENABLE_INSERT_MODE
|
2699 ENABLE_QUICK_EDIT_MODE
| ENABLE_EXTENDED_FLAGS
| ENABLE_AUTO_POSITION
;
2700 console
.input_cp
= console
.output_cp
= GetOEMCP();
2701 console
.history_size
= 50;
2702 if (!(console
.history
= calloc( console
.history_size
, sizeof(*console
.history
) ))) return 1;
2704 for (i
= 1; i
< argc
; i
++)
2706 if (!wcscmp( argv
[i
], L
"--headless"))
2711 if (!wcscmp( argv
[i
], L
"--unix"))
2713 console
.is_unix
= 1;
2717 if (!wcscmp( argv
[i
], L
"--width" ))
2719 if (++i
== argc
) return 1;
2720 width
= wcstol( argv
[i
], &end
, 0 );
2721 if ((!width
&& !console
.is_unix
) || width
> 0xffff || *end
) return 1;
2724 if (!wcscmp( argv
[i
], L
"--height" ))
2726 if (++i
== argc
) return 1;
2727 height
= wcstol( argv
[i
], &end
, 0 );
2728 if ((!height
&& !console
.is_unix
) || height
> 0xffff || *end
) return 1;
2731 if (!wcscmp( argv
[i
], L
"--signal" ))
2733 if (++i
== argc
) return 1;
2734 signal
= ULongToHandle( wcstol( argv
[i
], &end
, 0 ));
2738 if (!wcscmp( argv
[i
], L
"--server" ))
2740 if (++i
== argc
) return 1;
2741 console
.server
= ULongToHandle( wcstol( argv
[i
], &end
, 0 ));
2745 FIXME( "unknown option %s\n", debugstr_w(argv
[i
]) );
2749 if (!console
.server
)
2751 ERR( "no server handle\n" );
2755 if (!width
) width
= 80;
2756 if (!height
) height
= 150;
2758 if (!(console
.active
= create_screen_buffer( &console
, 1, width
, height
))) return 1;
2761 console
.tty_input
= GetStdHandle( STD_INPUT_HANDLE
);
2762 console
.tty_output
= GetStdHandle( STD_OUTPUT_HANDLE
);
2763 init_tty_output( &console
);
2764 if (!console
.is_unix
&& !ensure_tty_input_thread( &console
)) return 1;
2769 if (!init_window( &console
)) return 1;
2770 GetStartupInfoW( &si
);
2771 set_console_title( &console
, si
.lpTitle
, wcslen( si
.lpTitle
) * sizeof(WCHAR
) );
2772 ShowWindow( console
.win
, (si
.dwFlags
& STARTF_USESHOWWINDOW
) ? si
.wShowWindow
: SW_SHOW
);
2775 if (console
.is_unix
) RtlAddVectoredExceptionHandler( FALSE
, handle_ctrl_c
);
2777 return main_loop( &console
, signal
);