2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
33 #define WIN32_NO_STATUS
43 struct console_input_events
;
47 struct object obj
; /* object header */
48 int num_proc
; /* number of processes attached to this console */
49 struct thread
*renderer
; /* console renderer thread */
50 int mode
; /* input mode */
51 struct screen_buffer
*active
; /* active screen buffer */
52 int recnum
; /* number of input records */
53 INPUT_RECORD
*records
; /* input records */
54 struct console_input_events
*evt
; /* synchronization event with renderer */
55 WCHAR
*title
; /* console title */
56 WCHAR
**history
; /* lines history */
57 int history_size
; /* number of entries in history array */
58 int history_index
; /* number of used entries in history array */
59 int history_mode
; /* mode of history (non zero means remove doubled strings */
60 int edition_mode
; /* index to edition mode flavors */
61 int input_cp
; /* console input codepage */
62 int output_cp
; /* console output codepage */
63 user_handle_t win
; /* window handle if backend supports it */
64 struct event
*event
; /* event to wait on for input queue */
65 struct fd
*fd
; /* for bare console, attached input fd */
68 static void console_input_dump( struct object
*obj
, int verbose
);
69 static void console_input_destroy( struct object
*obj
);
70 static struct fd
*console_input_get_fd( struct object
*obj
);
72 static const struct object_ops console_input_ops
=
74 sizeof(struct console_input
), /* size */
75 console_input_dump
, /* dump */
76 no_get_type
, /* get_type */
77 no_add_queue
, /* add_queue */
78 NULL
, /* remove_queue */
80 no_satisfied
, /* satisfied */
81 no_signal
, /* signal */
82 console_input_get_fd
, /* get_fd */
83 default_fd_map_access
, /* map_access */
84 default_get_sd
, /* get_sd */
85 default_set_sd
, /* set_sd */
86 no_lookup_name
, /* lookup_name */
87 no_open_file
, /* open_file */
88 no_close_handle
, /* close_handle */
89 console_input_destroy
/* destroy */
92 static void console_input_events_dump( struct object
*obj
, int verbose
);
93 static void console_input_events_destroy( struct object
*obj
);
94 static int console_input_events_signaled( struct object
*obj
, struct thread
*thread
);
96 struct console_input_events
98 struct object obj
; /* object header */
99 int num_alloc
; /* number of allocated events */
100 int num_used
; /* number of actually used events */
101 struct console_renderer_event
* events
;
104 static const struct object_ops console_input_events_ops
=
106 sizeof(struct console_input_events
), /* size */
107 console_input_events_dump
, /* dump */
108 no_get_type
, /* get_type */
109 add_queue
, /* add_queue */
110 remove_queue
, /* remove_queue */
111 console_input_events_signaled
, /* signaled */
112 no_satisfied
, /* satisfied */
113 no_signal
, /* signal */
114 no_get_fd
, /* get_fd */
115 default_fd_map_access
, /* map_access */
116 default_get_sd
, /* get_sd */
117 default_set_sd
, /* set_sd */
118 no_lookup_name
, /* lookup_name */
119 no_open_file
, /* open_file */
120 no_close_handle
, /* close_handle */
121 console_input_events_destroy
/* destroy */
126 struct object obj
; /* object header */
127 struct list entry
; /* entry in list of all screen buffers */
128 struct console_input
*input
; /* associated console input */
129 int mode
; /* output mode */
130 int cursor_size
; /* size of cursor (percentage filled) */
131 int cursor_visible
;/* cursor visibility flag */
132 int cursor_x
; /* position of cursor */
133 int cursor_y
; /* position of cursor */
134 int width
; /* size (w-h) of the screen buffer */
136 int max_width
; /* size (w-h) of the window given font size */
138 char_info_t
*data
; /* the data for each cell - a width x height matrix */
139 unsigned short attr
; /* default attribute for screen buffer */
140 rectangle_t win
; /* current visible window on the screen buffer *
141 * as seen in wineconsole */
142 struct fd
*fd
; /* for bare console, attached output fd */
145 static void screen_buffer_dump( struct object
*obj
, int verbose
);
146 static void screen_buffer_destroy( struct object
*obj
);
147 static struct fd
*screen_buffer_get_fd( struct object
*obj
);
149 static const struct object_ops screen_buffer_ops
=
151 sizeof(struct screen_buffer
), /* size */
152 screen_buffer_dump
, /* dump */
153 no_get_type
, /* get_type */
154 no_add_queue
, /* add_queue */
155 NULL
, /* remove_queue */
157 NULL
, /* satisfied */
158 no_signal
, /* signal */
159 screen_buffer_get_fd
, /* get_fd */
160 default_fd_map_access
, /* map_access */
161 default_get_sd
, /* get_sd */
162 default_set_sd
, /* set_sd */
163 no_lookup_name
, /* lookup_name */
164 no_open_file
, /* open_file */
165 no_close_handle
, /* close_handle */
166 screen_buffer_destroy
/* destroy */
169 static enum server_fd_type
console_get_fd_type( struct fd
*fd
);
171 static const struct fd_ops console_fd_ops
=
173 default_fd_get_poll_events
, /* get_poll_events */
174 default_poll_event
, /* poll_event */
175 no_flush
, /* flush */
176 console_get_fd_type
, /* get_fd_type */
177 default_fd_ioctl
, /* ioctl */
178 default_fd_queue_async
, /* queue_async */
179 default_fd_reselect_async
, /* reselect_async */
180 default_fd_cancel_async
/* cancel_async */
183 static struct list screen_buffer_list
= LIST_INIT(screen_buffer_list
);
185 static const char_info_t empty_char_info
= { ' ', 0x000f }; /* white on black space */
187 static int console_input_is_bare( struct console_input
* cin
)
189 return cin
->evt
== NULL
;
192 static struct fd
*console_input_get_fd( struct object
* obj
)
194 struct console_input
*console_input
= (struct console_input
*)obj
;
195 assert( obj
->ops
== &console_input_ops
);
196 if (console_input
->fd
)
197 return (struct fd
*)grab_object( console_input
->fd
);
198 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
202 static enum server_fd_type
console_get_fd_type( struct fd
*fd
)
207 /* dumps the renderer events of a console */
208 static void console_input_events_dump( struct object
*obj
, int verbose
)
210 struct console_input_events
*evts
= (struct console_input_events
*)obj
;
211 assert( obj
->ops
== &console_input_events_ops
);
212 fprintf( stderr
, "Console input events: %d/%d events\n",
213 evts
->num_used
, evts
->num_alloc
);
216 /* destroys the renderer events of a console */
217 static void console_input_events_destroy( struct object
*obj
)
219 struct console_input_events
*evts
= (struct console_input_events
*)obj
;
220 assert( obj
->ops
== &console_input_events_ops
);
221 free( evts
->events
);
224 /* the renderer events list is signaled when it's not empty */
225 static int console_input_events_signaled( struct object
*obj
, struct thread
*thread
)
227 struct console_input_events
*evts
= (struct console_input_events
*)obj
;
228 assert( obj
->ops
== &console_input_events_ops
);
229 return (evts
->num_used
!= 0);
232 /* add an event to the console's renderer events list */
233 static void console_input_events_append( struct console_input
* console
,
234 struct console_renderer_event
* evt
)
236 struct console_input_events
* evts
;
237 int collapsed
= FALSE
;
239 if (!(evts
= console
->evt
)) return;
240 /* to be done even when evt has been generated by the renderer ? */
242 /* try to collapse evt into current queue's events */
245 struct console_renderer_event
* last
= &evts
->events
[evts
->num_used
- 1];
247 if (last
->event
== CONSOLE_RENDERER_UPDATE_EVENT
&&
248 evt
->event
== CONSOLE_RENDERER_UPDATE_EVENT
)
250 /* if two update events overlap, collapse them into a single one */
251 if (last
->u
.update
.bottom
+ 1 >= evt
->u
.update
.top
&&
252 evt
->u
.update
.bottom
+ 1 >= last
->u
.update
.top
)
254 last
->u
.update
.top
= min(last
->u
.update
.top
, evt
->u
.update
.top
);
255 last
->u
.update
.bottom
= max(last
->u
.update
.bottom
, evt
->u
.update
.bottom
);
262 if (evts
->num_used
== evts
->num_alloc
)
264 evts
->num_alloc
+= 16;
265 evts
->events
= realloc( evts
->events
, evts
->num_alloc
* sizeof(*evt
) );
266 assert(evts
->events
);
268 evts
->events
[evts
->num_used
++] = *evt
;
270 wake_up( &evts
->obj
, 0 );
273 /* retrieves events from the console's renderer events list */
274 static void console_input_events_get( struct console_input_events
* evts
)
276 data_size_t num
= get_reply_max_size() / sizeof(evts
->events
[0]);
278 if (num
> evts
->num_used
) num
= evts
->num_used
;
279 set_reply_data( evts
->events
, num
* sizeof(evts
->events
[0]) );
280 if (num
< evts
->num_used
)
282 memmove( &evts
->events
[0], &evts
->events
[num
],
283 (evts
->num_used
- num
) * sizeof(evts
->events
[0]) );
285 evts
->num_used
-= num
;
288 static struct console_input_events
*create_console_input_events(void)
290 struct console_input_events
* evt
;
292 if (!(evt
= alloc_object( &console_input_events_ops
))) return NULL
;
293 evt
->num_alloc
= evt
->num_used
= 0;
298 static struct object
*create_console_input( struct thread
* renderer
, int fd
)
300 struct console_input
*console_input
;
302 if (!(console_input
= alloc_object( &console_input_ops
))) return NULL
;
303 console_input
->renderer
= renderer
;
304 console_input
->mode
= ENABLE_PROCESSED_INPUT
| ENABLE_LINE_INPUT
|
305 ENABLE_ECHO_INPUT
| ENABLE_MOUSE_INPUT
;
306 console_input
->num_proc
= 0;
307 console_input
->active
= NULL
;
308 console_input
->recnum
= 0;
309 console_input
->records
= NULL
;
310 console_input
->evt
= renderer
? create_console_input_events() : NULL
;
311 console_input
->title
= NULL
;
312 console_input
->history_size
= 50;
313 console_input
->history
= calloc( console_input
->history_size
, sizeof(WCHAR
*) );
314 console_input
->history_index
= 0;
315 console_input
->history_mode
= 0;
316 console_input
->edition_mode
= 0;
317 console_input
->input_cp
= 0;
318 console_input
->output_cp
= 0;
319 console_input
->win
= 0;
320 console_input
->event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
321 console_input
->fd
= NULL
;
323 if (!console_input
->history
|| (renderer
&& !console_input
->evt
) || !console_input
->event
)
325 release_object( console_input
);
328 if (fd
!= -1) /* bare console */
330 if (!(console_input
->fd
= create_anonymous_fd( &console_fd_ops
, fd
, &console_input
->obj
,
331 FILE_SYNCHRONOUS_IO_NONALERT
)))
333 release_object( console_input
);
336 allow_fd_caching( console_input
->fd
);
339 return &console_input
->obj
;
342 static void generate_sb_initial_events( struct console_input
*console_input
)
344 struct screen_buffer
*screen_buffer
= console_input
->active
;
345 struct console_renderer_event evt
;
347 evt
.event
= CONSOLE_RENDERER_ACTIVE_SB_EVENT
;
348 memset(&evt
.u
, 0, sizeof(evt
.u
));
349 console_input_events_append( console_input
, &evt
);
351 evt
.event
= CONSOLE_RENDERER_SB_RESIZE_EVENT
;
352 evt
.u
.resize
.width
= screen_buffer
->width
;
353 evt
.u
.resize
.height
= screen_buffer
->height
;
354 console_input_events_append( console_input
, &evt
);
356 evt
.event
= CONSOLE_RENDERER_DISPLAY_EVENT
;
357 evt
.u
.display
.left
= screen_buffer
->win
.left
;
358 evt
.u
.display
.top
= screen_buffer
->win
.top
;
359 evt
.u
.display
.width
= screen_buffer
->win
.right
- screen_buffer
->win
.left
+ 1;
360 evt
.u
.display
.height
= screen_buffer
->win
.bottom
- screen_buffer
->win
.top
+ 1;
361 console_input_events_append( console_input
, &evt
);
363 evt
.event
= CONSOLE_RENDERER_UPDATE_EVENT
;
364 evt
.u
.update
.top
= 0;
365 evt
.u
.update
.bottom
= screen_buffer
->height
- 1;
366 console_input_events_append( console_input
, &evt
);
368 evt
.event
= CONSOLE_RENDERER_CURSOR_GEOM_EVENT
;
369 evt
.u
.cursor_geom
.size
= screen_buffer
->cursor_size
;
370 evt
.u
.cursor_geom
.visible
= screen_buffer
->cursor_visible
;
371 console_input_events_append( console_input
, &evt
);
373 evt
.event
= CONSOLE_RENDERER_CURSOR_POS_EVENT
;
374 evt
.u
.cursor_pos
.x
= screen_buffer
->cursor_x
;
375 evt
.u
.cursor_pos
.y
= screen_buffer
->cursor_y
;
376 console_input_events_append( console_input
, &evt
);
379 static struct screen_buffer
*create_console_output( struct console_input
*console_input
, int fd
)
381 struct screen_buffer
*screen_buffer
;
384 if (!(screen_buffer
= alloc_object( &screen_buffer_ops
)))
386 if (fd
!= -1) close( fd
);
389 screen_buffer
->mode
= ENABLE_PROCESSED_OUTPUT
| ENABLE_WRAP_AT_EOL_OUTPUT
;
390 screen_buffer
->input
= console_input
;
391 screen_buffer
->cursor_size
= 100;
392 screen_buffer
->cursor_visible
= 1;
393 screen_buffer
->width
= 80;
394 screen_buffer
->height
= 150;
395 screen_buffer
->max_width
= 80;
396 screen_buffer
->max_height
= 25;
397 screen_buffer
->cursor_x
= 0;
398 screen_buffer
->cursor_y
= 0;
399 screen_buffer
->attr
= 0x0F;
400 screen_buffer
->win
.left
= 0;
401 screen_buffer
->win
.right
= screen_buffer
->max_width
- 1;
402 screen_buffer
->win
.top
= 0;
403 screen_buffer
->win
.bottom
= screen_buffer
->max_height
- 1;
405 screen_buffer
->fd
= NULL
;
408 if (!(screen_buffer
->fd
= create_anonymous_fd( &console_fd_ops
, fd
, &screen_buffer
->obj
,
409 FILE_SYNCHRONOUS_IO_NONALERT
)))
411 release_object( screen_buffer
);
414 allow_fd_caching(screen_buffer
->fd
);
417 list_add_head( &screen_buffer_list
, &screen_buffer
->entry
);
419 if (!(screen_buffer
->data
= malloc( screen_buffer
->width
* screen_buffer
->height
*
420 sizeof(*screen_buffer
->data
) )))
422 release_object( screen_buffer
);
425 /* clear the first row */
426 for (i
= 0; i
< screen_buffer
->width
; i
++) screen_buffer
->data
[i
] = empty_char_info
;
427 /* and copy it to all other rows */
428 for (i
= 1; i
< screen_buffer
->height
; i
++)
429 memcpy( &screen_buffer
->data
[i
* screen_buffer
->width
], screen_buffer
->data
,
430 screen_buffer
->width
* sizeof(char_info_t
) );
432 if (!console_input
->active
)
434 console_input
->active
= (struct screen_buffer
*)grab_object( screen_buffer
);
435 generate_sb_initial_events( console_input
);
437 return screen_buffer
;
440 /* free the console for this process */
441 int free_console( struct process
*process
)
443 struct console_input
* console
= process
->console
;
445 if (!console
) return 0;
447 process
->console
= NULL
;
448 if (--console
->num_proc
== 0 && console
->renderer
)
450 /* all processes have terminated... tell the renderer to terminate too */
451 struct console_renderer_event evt
;
452 evt
.event
= CONSOLE_RENDERER_EXIT_EVENT
;
453 memset(&evt
.u
, 0, sizeof(evt
.u
));
454 console_input_events_append( console
, &evt
);
456 release_object( console
);
461 /* let process inherit the console from parent... this handle two cases :
462 * 1/ generic console inheritance
463 * 2/ parent is a renderer which launches process, and process should attach to the console
464 * renderered by parent
466 void inherit_console(struct thread
*parent_thread
, struct process
*process
, obj_handle_t hconin
)
469 struct process
* parent
= parent_thread
->process
;
471 /* if parent is a renderer, then attach current process to its console
476 struct console_input
* console
;
478 /* FIXME: should we check some access rights ? */
479 if ((console
= (struct console_input
*)get_handle_obj( parent
, hconin
,
480 0, &console_input_ops
)))
482 if (console
->renderer
== parent_thread
)
484 process
->console
= (struct console_input
*)grab_object( console
);
485 process
->console
->num_proc
++;
488 release_object( console
);
490 else clear_error(); /* ignore error */
492 /* otherwise, if parent has a console, attach child to this console */
493 if (!done
&& parent
->console
)
495 process
->console
= (struct console_input
*)grab_object( parent
->console
);
496 process
->console
->num_proc
++;
500 struct thread
*console_get_renderer( struct console_input
*console
)
502 return console
->renderer
;
505 static struct console_input
* console_input_get( obj_handle_t handle
, unsigned access
)
507 struct console_input
* console
= NULL
;
510 console
= (struct console_input
*)get_handle_obj( current
->process
, handle
,
511 access
, &console_input_ops
);
512 else if (current
->process
->console
)
514 console
= (struct console_input
*)grab_object( current
->process
->console
);
517 if (!console
&& !get_error()) set_error(STATUS_INVALID_PARAMETER
);
521 struct console_signal_info
523 struct console_input
*console
;
528 static int propagate_console_signal_cb(struct process
*process
, void *user
)
530 struct console_signal_info
* csi
= (struct console_signal_info
*)user
;
532 if (process
->console
== csi
->console
&& process
->running_threads
&&
533 (!csi
->group
|| process
->group_id
== csi
->group
))
535 /* find a suitable thread to signal */
536 struct thread
*thread
;
537 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
539 if (send_thread_signal( thread
, csi
->signal
)) break;
545 static void propagate_console_signal( struct console_input
*console
,
546 int sig
, process_id_t group_id
)
548 struct console_signal_info csi
;
552 set_error( STATUS_INVALID_PARAMETER
);
555 /* FIXME: should support the other events (like CTRL_BREAK) */
556 if (sig
!= CTRL_C_EVENT
)
558 set_error( STATUS_NOT_IMPLEMENTED
);
561 csi
.console
= console
;
563 csi
.group
= group_id
;
565 enum_processes(propagate_console_signal_cb
, &csi
);
568 static int get_console_mode( obj_handle_t handle
)
573 if ((obj
= get_handle_obj( current
->process
, handle
, FILE_READ_PROPERTIES
, NULL
)))
575 if (obj
->ops
== &console_input_ops
)
577 ret
= ((struct console_input
*)obj
)->mode
;
579 else if (obj
->ops
== &screen_buffer_ops
)
581 ret
= ((struct screen_buffer
*)obj
)->mode
;
584 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
585 release_object( obj
);
590 /* changes the mode of either a console input or a screen buffer */
591 static int set_console_mode( obj_handle_t handle
, int mode
)
596 if (!(obj
= get_handle_obj( current
->process
, handle
, FILE_WRITE_PROPERTIES
, NULL
)))
598 if (obj
->ops
== &console_input_ops
)
600 /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
601 ((struct console_input
*)obj
)->mode
= mode
;
604 else if (obj
->ops
== &screen_buffer_ops
)
606 ((struct screen_buffer
*)obj
)->mode
= mode
;
609 else set_error( STATUS_OBJECT_TYPE_MISMATCH
);
610 release_object( obj
);
614 /* add input events to a console input queue */
615 static int write_console_input( struct console_input
* console
, int count
,
616 const INPUT_RECORD
*records
)
618 INPUT_RECORD
*new_rec
;
620 if (!count
) return 0;
621 if (!(new_rec
= realloc( console
->records
,
622 (console
->recnum
+ count
) * sizeof(INPUT_RECORD
) )))
624 set_error( STATUS_NO_MEMORY
);
625 release_object( console
);
628 console
->records
= new_rec
;
629 memcpy( new_rec
+ console
->recnum
, records
, count
* sizeof(INPUT_RECORD
) );
631 if (console
->mode
& ENABLE_PROCESSED_INPUT
)
636 if (records
[i
].EventType
== KEY_EVENT
&&
637 records
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
== 'C' - 64 &&
638 !(records
[i
].Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
641 memcpy( &console
->records
[console
->recnum
+ i
],
642 &console
->records
[console
->recnum
+ i
+ 1],
643 (count
- i
- 1) * sizeof(INPUT_RECORD
) );
645 if (records
[i
].Event
.KeyEvent
.bKeyDown
)
647 /* send SIGINT to all processes attached to this console */
648 propagate_console_signal( console
, CTRL_C_EVENT
, 0 );
654 if (!console
->recnum
&& count
) set_event( console
->event
);
655 console
->recnum
+= count
;
659 /* retrieve a pointer to the console input records */
660 static int read_console_input( obj_handle_t handle
, int count
, int flush
)
662 struct console_input
*console
;
664 if (!(console
= (struct console_input
*)get_handle_obj( current
->process
, handle
,
665 FILE_READ_DATA
, &console_input_ops
)))
670 /* special case: do not retrieve anything, but return
671 * the total number of records available */
672 count
= console
->recnum
;
676 if (count
> console
->recnum
) count
= console
->recnum
;
677 set_reply_data( console
->records
, count
* sizeof(INPUT_RECORD
) );
682 for (i
= count
; i
< console
->recnum
; i
++)
683 console
->records
[i
-count
] = console
->records
[i
];
684 if ((console
->recnum
-= count
) > 0)
686 INPUT_RECORD
*new_rec
= realloc( console
->records
,
687 console
->recnum
* sizeof(INPUT_RECORD
) );
688 if (new_rec
) console
->records
= new_rec
;
692 free( console
->records
);
693 console
->records
= NULL
;
694 reset_event( console
->event
);
697 release_object( console
);
701 /* set misc console input information */
702 static int set_console_input_info( const struct set_console_input_info_request
*req
,
703 const WCHAR
*title
, data_size_t len
)
705 struct console_input
*console
;
706 struct console_renderer_event evt
;
708 if (!(console
= console_input_get( req
->handle
, FILE_WRITE_PROPERTIES
))) goto error
;
709 if (console_input_is_bare(console
) &&
710 (req
->mask
& (SET_CONSOLE_INPUT_INFO_ACTIVE_SB
|
711 SET_CONSOLE_INPUT_INFO_WIN
)))
713 set_error( STATUS_UNSUCCESSFUL
);
717 memset(&evt
.u
, 0, sizeof(evt
.u
));
718 if (req
->mask
& SET_CONSOLE_INPUT_INFO_ACTIVE_SB
)
720 struct screen_buffer
*screen_buffer
;
722 screen_buffer
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->active_sb
,
723 FILE_WRITE_PROPERTIES
, &screen_buffer_ops
);
724 if (!screen_buffer
|| screen_buffer
->input
!= console
)
726 set_error( STATUS_INVALID_HANDLE
);
727 if (screen_buffer
) release_object( screen_buffer
);
731 if (screen_buffer
!= console
->active
)
733 if (console
->active
) release_object( console
->active
);
734 console
->active
= screen_buffer
;
735 generate_sb_initial_events( console
);
738 release_object( screen_buffer
);
740 if (req
->mask
& SET_CONSOLE_INPUT_INFO_TITLE
)
742 WCHAR
*new_title
= mem_alloc( len
+ sizeof(WCHAR
) );
745 memcpy( new_title
, title
, len
);
746 new_title
[len
/ sizeof(WCHAR
)] = 0;
747 free( console
->title
);
748 console
->title
= new_title
;
749 evt
.event
= CONSOLE_RENDERER_TITLE_EVENT
;
750 console_input_events_append( console
, &evt
);
753 if (req
->mask
& SET_CONSOLE_INPUT_INFO_HISTORY_MODE
)
755 console
->history_mode
= req
->history_mode
;
757 if ((req
->mask
& SET_CONSOLE_INPUT_INFO_HISTORY_SIZE
) &&
758 console
->history_size
!= req
->history_size
)
764 if (req
->history_size
)
766 mem
= mem_alloc( req
->history_size
* sizeof(WCHAR
*) );
767 if (!mem
) goto error
;
768 memset( mem
, 0, req
->history_size
* sizeof(WCHAR
*) );
771 delta
= (console
->history_index
> req
->history_size
) ?
772 (console
->history_index
- req
->history_size
) : 0;
774 for (i
= delta
; i
< console
->history_index
; i
++)
776 mem
[i
- delta
] = console
->history
[i
];
777 console
->history
[i
] = NULL
;
779 console
->history_index
-= delta
;
781 for (i
= 0; i
< console
->history_size
; i
++)
782 free( console
->history
[i
] );
783 free( console
->history
);
784 console
->history
= mem
;
785 console
->history_size
= req
->history_size
;
787 if (req
->mask
& SET_CONSOLE_INPUT_INFO_EDITION_MODE
)
789 console
->edition_mode
= req
->edition_mode
;
791 if (req
->mask
& SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE
)
793 console
->input_cp
= req
->input_cp
;
795 if (req
->mask
& SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE
)
797 console
->output_cp
= req
->output_cp
;
799 if (req
->mask
& SET_CONSOLE_INPUT_INFO_WIN
)
801 console
->win
= req
->win
;
803 release_object( console
);
806 if (console
) release_object( console
);
810 /* resize a screen buffer */
811 static int change_screen_buffer_size( struct screen_buffer
*screen_buffer
,
812 int new_width
, int new_height
)
814 int i
, old_width
, old_height
, copy_width
, copy_height
;
815 char_info_t
*new_data
;
817 if (!(new_data
= malloc( new_width
* new_height
* sizeof(*new_data
) )))
819 set_error( STATUS_NO_MEMORY
);
822 old_width
= screen_buffer
->width
;
823 old_height
= screen_buffer
->height
;
824 copy_width
= min( old_width
, new_width
);
825 copy_height
= min( old_height
, new_height
);
827 /* copy all the rows */
828 for (i
= 0; i
< copy_height
; i
++)
830 memcpy( &new_data
[i
* new_width
], &screen_buffer
->data
[i
* old_width
],
831 copy_width
* sizeof(char_info_t
) );
834 /* clear the end of each row */
835 if (new_width
> old_width
)
838 for (i
= old_width
; i
< new_width
; i
++) new_data
[i
] = empty_char_info
;
839 /* and blast it to the other rows */
840 for (i
= 1; i
< copy_height
; i
++)
841 memcpy( &new_data
[i
* new_width
+ old_width
], &new_data
[old_width
],
842 (new_width
- old_width
) * sizeof(char_info_t
) );
845 /* clear remaining rows */
846 if (new_height
> old_height
)
849 for (i
= 0; i
< new_width
; i
++) new_data
[old_height
* new_width
+ i
] = empty_char_info
;
850 /* and blast it to the other rows */
851 for (i
= old_height
+1; i
< new_height
; i
++)
852 memcpy( &new_data
[i
* new_width
], &new_data
[old_height
* new_width
],
853 new_width
* sizeof(char_info_t
) );
855 free( screen_buffer
->data
);
856 screen_buffer
->data
= new_data
;
857 screen_buffer
->width
= new_width
;
858 screen_buffer
->height
= new_height
;
862 /* set misc screen buffer information */
863 static int set_console_output_info( struct screen_buffer
*screen_buffer
,
864 const struct set_console_output_info_request
*req
)
866 struct console_renderer_event evt
;
868 memset(&evt
.u
, 0, sizeof(evt
.u
));
869 if (req
->mask
& SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
)
871 if (req
->cursor_size
< 1 || req
->cursor_size
> 100)
873 set_error( STATUS_INVALID_PARAMETER
);
876 if (screen_buffer
->cursor_size
!= req
->cursor_size
||
877 screen_buffer
->cursor_visible
!= req
->cursor_visible
)
879 screen_buffer
->cursor_size
= req
->cursor_size
;
880 screen_buffer
->cursor_visible
= req
->cursor_visible
;
881 evt
.event
= CONSOLE_RENDERER_CURSOR_GEOM_EVENT
;
882 evt
.u
.cursor_geom
.size
= req
->cursor_size
;
883 evt
.u
.cursor_geom
.visible
= req
->cursor_visible
;
884 console_input_events_append( screen_buffer
->input
, &evt
);
887 if (req
->mask
& SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
)
889 if (req
->cursor_x
< 0 || req
->cursor_x
>= screen_buffer
->width
||
890 req
->cursor_y
< 0 || req
->cursor_y
>= screen_buffer
->height
)
892 set_error( STATUS_INVALID_PARAMETER
);
895 if (screen_buffer
->cursor_x
!= req
->cursor_x
|| screen_buffer
->cursor_y
!= req
->cursor_y
)
897 screen_buffer
->cursor_x
= req
->cursor_x
;
898 screen_buffer
->cursor_y
= req
->cursor_y
;
899 evt
.event
= CONSOLE_RENDERER_CURSOR_POS_EVENT
;
900 evt
.u
.cursor_pos
.x
= req
->cursor_x
;
901 evt
.u
.cursor_pos
.y
= req
->cursor_y
;
902 console_input_events_append( screen_buffer
->input
, &evt
);
905 if (req
->mask
& SET_CONSOLE_OUTPUT_INFO_SIZE
)
909 /* new screen-buffer cannot be smaller than actual window */
910 if (req
->width
< screen_buffer
->win
.right
- screen_buffer
->win
.left
+ 1 ||
911 req
->height
< screen_buffer
->win
.bottom
- screen_buffer
->win
.top
+ 1)
913 set_error( STATUS_INVALID_PARAMETER
);
916 /* FIXME: there are also some basic minimum and max size to deal with */
917 if (!change_screen_buffer_size( screen_buffer
, req
->width
, req
->height
)) return 0;
919 evt
.event
= CONSOLE_RENDERER_SB_RESIZE_EVENT
;
920 evt
.u
.resize
.width
= req
->width
;
921 evt
.u
.resize
.height
= req
->height
;
922 console_input_events_append( screen_buffer
->input
, &evt
);
924 evt
.event
= CONSOLE_RENDERER_UPDATE_EVENT
;
925 evt
.u
.update
.top
= 0;
926 evt
.u
.update
.bottom
= screen_buffer
->height
- 1;
927 console_input_events_append( screen_buffer
->input
, &evt
);
929 /* scroll window to display sb */
930 if (screen_buffer
->win
.right
>= req
->width
)
932 screen_buffer
->win
.right
-= screen_buffer
->win
.left
;
933 screen_buffer
->win
.left
= 0;
935 if (screen_buffer
->win
.bottom
>= req
->height
)
937 screen_buffer
->win
.bottom
-= screen_buffer
->win
.top
;
938 screen_buffer
->win
.top
= 0;
940 /* reset cursor if needed (normally, if cursor was outside of new sb, the
941 * window has been shifted so that the new position of the cursor will be
944 if (screen_buffer
->cursor_x
>= req
->width
)
946 screen_buffer
->cursor_x
= req
->width
- 1;
949 if (screen_buffer
->cursor_y
>= req
->height
)
951 screen_buffer
->cursor_y
= req
->height
- 1;
956 evt
.event
= CONSOLE_RENDERER_CURSOR_POS_EVENT
;
957 evt
.u
.cursor_pos
.x
= req
->cursor_x
;
958 evt
.u
.cursor_pos
.y
= req
->cursor_y
;
959 console_input_events_append( screen_buffer
->input
, &evt
);
962 if (screen_buffer
== screen_buffer
->input
->active
&&
963 screen_buffer
->input
->mode
& ENABLE_WINDOW_INPUT
)
966 ir
.EventType
= WINDOW_BUFFER_SIZE_EVENT
;
967 ir
.Event
.WindowBufferSizeEvent
.dwSize
.X
= req
->width
;
968 ir
.Event
.WindowBufferSizeEvent
.dwSize
.Y
= req
->height
;
969 write_console_input( screen_buffer
->input
, 1, &ir
);
972 if (req
->mask
& SET_CONSOLE_OUTPUT_INFO_ATTR
)
974 screen_buffer
->attr
= req
->attr
;
976 if (req
->mask
& SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
)
978 if (req
->win_left
< 0 || req
->win_left
> req
->win_right
||
979 req
->win_right
>= screen_buffer
->width
||
980 req
->win_top
< 0 || req
->win_top
> req
->win_bottom
||
981 req
->win_bottom
>= screen_buffer
->height
)
983 set_error( STATUS_INVALID_PARAMETER
);
986 if (screen_buffer
->win
.left
!= req
->win_left
|| screen_buffer
->win
.top
!= req
->win_top
||
987 screen_buffer
->win
.right
!= req
->win_right
|| screen_buffer
->win
.bottom
!= req
->win_bottom
)
989 screen_buffer
->win
.left
= req
->win_left
;
990 screen_buffer
->win
.top
= req
->win_top
;
991 screen_buffer
->win
.right
= req
->win_right
;
992 screen_buffer
->win
.bottom
= req
->win_bottom
;
993 evt
.event
= CONSOLE_RENDERER_DISPLAY_EVENT
;
994 evt
.u
.display
.left
= req
->win_left
;
995 evt
.u
.display
.top
= req
->win_top
;
996 evt
.u
.display
.width
= req
->win_right
- req
->win_left
+ 1;
997 evt
.u
.display
.height
= req
->win_bottom
- req
->win_top
+ 1;
998 console_input_events_append( screen_buffer
->input
, &evt
);
1001 if (req
->mask
& SET_CONSOLE_OUTPUT_INFO_MAX_SIZE
)
1003 /* can only be done by renderer */
1004 if (current
->process
->console
!= screen_buffer
->input
)
1006 set_error( STATUS_INVALID_PARAMETER
);
1010 screen_buffer
->max_width
= req
->max_width
;
1011 screen_buffer
->max_height
= req
->max_height
;
1017 /* appends a new line to history (history is a fixed size array) */
1018 static void console_input_append_hist( struct console_input
* console
, const WCHAR
* buf
, data_size_t len
)
1020 WCHAR
* ptr
= mem_alloc( (len
+ 1) * sizeof(WCHAR
) );
1025 if (!console
|| !console
->history_size
)
1027 set_error( STATUS_INVALID_PARAMETER
); /* FIXME */
1032 memcpy( ptr
, buf
, len
* sizeof(WCHAR
) );
1035 if (console
->history_mode
&& console
->history_index
&&
1036 strncmpW( console
->history
[console
->history_index
- 1], ptr
, len
* sizeof(WCHAR
) ) == 0)
1038 /* ok, mode ask us to not use twice the same string...
1039 * so just free mem and returns
1041 set_error( STATUS_ALIAS_EXISTS
);
1046 if (console
->history_index
< console
->history_size
)
1048 console
->history
[console
->history_index
++] = ptr
;
1052 free( console
->history
[0]) ;
1053 memmove( &console
->history
[0], &console
->history
[1],
1054 (console
->history_size
- 1) * sizeof(WCHAR
*) );
1055 console
->history
[console
->history_size
- 1] = ptr
;
1059 /* returns a line from the cache */
1060 static data_size_t
console_input_get_hist( struct console_input
*console
, int index
)
1062 data_size_t ret
= 0;
1064 if (index
>= console
->history_index
) set_error( STATUS_INVALID_PARAMETER
);
1067 ret
= strlenW( console
->history
[index
] ) * sizeof(WCHAR
);
1068 set_reply_data( console
->history
[index
], min( ret
, get_reply_max_size() ));
1074 static void console_input_dump( struct object
*obj
, int verbose
)
1076 struct console_input
*console
= (struct console_input
*)obj
;
1077 assert( obj
->ops
== &console_input_ops
);
1078 fprintf( stderr
, "Console input active=%p evt=%p\n",
1079 console
->active
, console
->evt
);
1082 static void console_input_destroy( struct object
*obj
)
1084 struct console_input
* console_in
= (struct console_input
*)obj
;
1085 struct screen_buffer
* curr
;
1088 assert( obj
->ops
== &console_input_ops
);
1089 free( console_in
->title
);
1090 free( console_in
->records
);
1092 if (console_in
->active
) release_object( console_in
->active
);
1093 console_in
->active
= NULL
;
1095 LIST_FOR_EACH_ENTRY( curr
, &screen_buffer_list
, struct screen_buffer
, entry
)
1097 if (curr
->input
== console_in
) curr
->input
= NULL
;
1100 if (console_in
->evt
)
1102 release_object( console_in
->evt
);
1103 console_in
->evt
= NULL
;
1105 release_object( console_in
->event
);
1107 release_object( console_in
->fd
);
1109 for (i
= 0; i
< console_in
->history_size
; i
++)
1110 free( console_in
->history
[i
] );
1111 free( console_in
->history
);
1114 static void screen_buffer_dump( struct object
*obj
, int verbose
)
1116 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
1117 assert( obj
->ops
== &screen_buffer_ops
);
1119 fprintf(stderr
, "Console screen buffer input=%p\n", screen_buffer
->input
);
1122 static void screen_buffer_destroy( struct object
*obj
)
1124 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
1126 assert( obj
->ops
== &screen_buffer_ops
);
1128 list_remove( &screen_buffer
->entry
);
1130 if (screen_buffer
->input
&& screen_buffer
->input
->active
== screen_buffer
)
1132 struct screen_buffer
*sb
;
1134 screen_buffer
->input
->active
= NULL
;
1135 LIST_FOR_EACH_ENTRY( sb
, &screen_buffer_list
, struct screen_buffer
, entry
)
1137 if (sb
->input
== screen_buffer
->input
)
1139 sb
->input
->active
= sb
;
1144 if (screen_buffer
->fd
) release_object( screen_buffer
->fd
);
1145 free( screen_buffer
->data
);
1148 static struct fd
*screen_buffer_get_fd( struct object
*obj
)
1150 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
1151 assert( obj
->ops
== &screen_buffer_ops
);
1152 if (screen_buffer
->fd
)
1153 return (struct fd
*)grab_object( screen_buffer
->fd
);
1154 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1158 /* write data into a screen buffer */
1159 static int write_console_output( struct screen_buffer
*screen_buffer
, data_size_t size
,
1160 const void* data
, enum char_info_mode mode
,
1161 int x
, int y
, int wrap
)
1164 char_info_t
*end
, *dest
= screen_buffer
->data
+ y
* screen_buffer
->width
+ x
;
1166 if (y
>= screen_buffer
->height
) return 0;
1169 end
= screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
;
1171 end
= screen_buffer
->data
+ (y
+1) * screen_buffer
->width
;
1175 case CHAR_INFO_MODE_TEXT
:
1177 const WCHAR
*ptr
= data
;
1178 for (i
= 0; i
< size
/sizeof(*ptr
) && dest
< end
; dest
++, i
++) dest
->ch
= ptr
[i
];
1181 case CHAR_INFO_MODE_ATTR
:
1183 const unsigned short *ptr
= data
;
1184 for (i
= 0; i
< size
/sizeof(*ptr
) && dest
< end
; dest
++, i
++) dest
->attr
= ptr
[i
];
1187 case CHAR_INFO_MODE_TEXTATTR
:
1189 const char_info_t
*ptr
= data
;
1190 for (i
= 0; i
< size
/sizeof(*ptr
) && dest
< end
; dest
++, i
++) *dest
= ptr
[i
];
1193 case CHAR_INFO_MODE_TEXTSTDATTR
:
1195 const WCHAR
*ptr
= data
;
1196 for (i
= 0; i
< size
/sizeof(*ptr
) && dest
< end
; dest
++, i
++)
1199 dest
->attr
= screen_buffer
->attr
;
1204 set_error( STATUS_INVALID_PARAMETER
);
1208 if (i
&& screen_buffer
== screen_buffer
->input
->active
)
1210 struct console_renderer_event evt
;
1211 evt
.event
= CONSOLE_RENDERER_UPDATE_EVENT
;
1212 memset(&evt
.u
, 0, sizeof(evt
.u
));
1213 evt
.u
.update
.top
= y
+ x
/ screen_buffer
->width
;
1214 evt
.u
.update
.bottom
= y
+ (x
+ i
- 1) / screen_buffer
->width
;
1215 console_input_events_append( screen_buffer
->input
, &evt
);
1220 /* fill a screen buffer with uniform data */
1221 static int fill_console_output( struct screen_buffer
*screen_buffer
, char_info_t data
,
1222 enum char_info_mode mode
, int x
, int y
, int count
, int wrap
)
1225 char_info_t
*end
, *dest
= screen_buffer
->data
+ y
* screen_buffer
->width
+ x
;
1227 if (y
>= screen_buffer
->height
) return 0;
1230 end
= screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
;
1232 end
= screen_buffer
->data
+ (y
+1) * screen_buffer
->width
;
1234 if (count
> end
- dest
) count
= end
- dest
;
1238 case CHAR_INFO_MODE_TEXT
:
1239 for (i
= 0; i
< count
; i
++) dest
[i
].ch
= data
.ch
;
1241 case CHAR_INFO_MODE_ATTR
:
1242 for (i
= 0; i
< count
; i
++) dest
[i
].attr
= data
.attr
;
1244 case CHAR_INFO_MODE_TEXTATTR
:
1245 for (i
= 0; i
< count
; i
++) dest
[i
] = data
;
1247 case CHAR_INFO_MODE_TEXTSTDATTR
:
1248 for (i
= 0; i
< count
; i
++)
1250 dest
[i
].ch
= data
.ch
;
1251 dest
[i
].attr
= screen_buffer
->attr
;
1255 set_error( STATUS_INVALID_PARAMETER
);
1259 if (count
&& screen_buffer
== screen_buffer
->input
->active
)
1261 struct console_renderer_event evt
;
1262 evt
.event
= CONSOLE_RENDERER_UPDATE_EVENT
;
1263 memset(&evt
.u
, 0, sizeof(evt
.u
));
1264 evt
.u
.update
.top
= y
;
1265 evt
.u
.update
.bottom
= (y
* screen_buffer
->width
+ x
+ count
- 1) / screen_buffer
->width
;
1266 console_input_events_append( screen_buffer
->input
, &evt
);
1271 /* read data from a screen buffer */
1272 static void read_console_output( struct screen_buffer
*screen_buffer
, int x
, int y
,
1273 enum char_info_mode mode
, int wrap
)
1276 char_info_t
*end
, *src
= screen_buffer
->data
+ y
* screen_buffer
->width
+ x
;
1278 if (y
>= screen_buffer
->height
) return;
1281 end
= screen_buffer
->data
+ screen_buffer
->height
* screen_buffer
->width
;
1283 end
= screen_buffer
->data
+ (y
+1) * screen_buffer
->width
;
1287 case CHAR_INFO_MODE_TEXT
:
1290 int count
= min( end
- src
, get_reply_max_size() / sizeof(*data
) );
1291 if ((data
= set_reply_data_size( count
* sizeof(*data
) )))
1293 for (i
= 0; i
< count
; i
++) data
[i
] = src
[i
].ch
;
1297 case CHAR_INFO_MODE_ATTR
:
1299 unsigned short *data
;
1300 int count
= min( end
- src
, get_reply_max_size() / sizeof(*data
) );
1301 if ((data
= set_reply_data_size( count
* sizeof(*data
) )))
1303 for (i
= 0; i
< count
; i
++) data
[i
] = src
[i
].attr
;
1307 case CHAR_INFO_MODE_TEXTATTR
:
1310 int count
= min( end
- src
, get_reply_max_size() / sizeof(*data
) );
1311 if ((data
= set_reply_data_size( count
* sizeof(*data
) )))
1313 for (i
= 0; i
< count
; i
++) data
[i
] = src
[i
];
1318 set_error( STATUS_INVALID_PARAMETER
);
1323 /* scroll parts of a screen buffer */
1324 static void scroll_console_output( struct screen_buffer
*screen_buffer
, int xsrc
, int ysrc
, int xdst
, int ydst
,
1328 char_info_t
*psrc
, *pdst
;
1329 struct console_renderer_event evt
;
1331 if (xsrc
< 0 || ysrc
< 0 || xdst
< 0 || ydst
< 0 ||
1332 xsrc
+ w
> screen_buffer
->width
||
1333 xdst
+ w
> screen_buffer
->width
||
1334 ysrc
+ h
> screen_buffer
->height
||
1335 ydst
+ h
> screen_buffer
->height
||
1338 set_error( STATUS_INVALID_PARAMETER
);
1344 psrc
= &screen_buffer
->data
[(ysrc
+ h
- 1) * screen_buffer
->width
+ xsrc
];
1345 pdst
= &screen_buffer
->data
[(ydst
+ h
- 1) * screen_buffer
->width
+ xdst
];
1347 for (j
= h
; j
> 0; j
--)
1349 memcpy(pdst
, psrc
, w
* sizeof(*pdst
) );
1350 pdst
-= screen_buffer
->width
;
1351 psrc
-= screen_buffer
->width
;
1356 psrc
= &screen_buffer
->data
[ysrc
* screen_buffer
->width
+ xsrc
];
1357 pdst
= &screen_buffer
->data
[ydst
* screen_buffer
->width
+ xdst
];
1359 for (j
= 0; j
< h
; j
++)
1361 /* we use memmove here because when psrc and pdst are the same,
1362 * copies are done on the same row, so the dst and src blocks
1364 memmove( pdst
, psrc
, w
* sizeof(*pdst
) );
1365 pdst
+= screen_buffer
->width
;
1366 psrc
+= screen_buffer
->width
;
1370 /* FIXME: this could be enhanced, by signalling scroll */
1371 evt
.event
= CONSOLE_RENDERER_UPDATE_EVENT
;
1372 memset(&evt
.u
, 0, sizeof(evt
.u
));
1373 evt
.u
.update
.top
= min(ysrc
, ydst
);
1374 evt
.u
.update
.bottom
= max(ysrc
, ydst
) + h
- 1;
1375 console_input_events_append( screen_buffer
->input
, &evt
);
1378 /* allocate a console for the renderer */
1379 DECL_HANDLER(alloc_console
)
1381 obj_handle_t in
= 0;
1382 obj_handle_t evt
= 0;
1383 struct process
*process
;
1384 struct thread
*renderer
;
1385 struct console_input
*console
;
1392 /* renderer is current, console to be attached to parent process */
1394 if (!(process
= current
->process
->parent
))
1396 set_error( STATUS_ACCESS_DENIED
);
1399 grab_object( process
);
1403 /* no renderer, console to be attached to current process */
1405 process
= current
->process
;
1406 grab_object( process
);
1410 /* renderer is current, console to be attached to req->pid */
1412 if (!(process
= get_process_from_id( req
->pid
))) return;
1415 if (attach
&& process
->console
)
1417 set_error( STATUS_ACCESS_DENIED
);
1420 if (req
->input_fd
!= -1)
1422 if ((fd
= thread_get_inflight_fd( current
, req
->input_fd
)) == -1)
1424 set_error( STATUS_INVALID_PARAMETER
);
1430 if ((console
= (struct console_input
*)create_console_input( renderer
, fd
)))
1432 if ((in
= alloc_handle( current
->process
, console
, req
->access
, req
->attributes
)))
1434 if (!console
->evt
||
1435 (evt
= alloc_handle( current
->process
, console
->evt
, SYNCHRONIZE
|GENERIC_READ
|GENERIC_WRITE
, 0 )))
1439 process
->console
= (struct console_input
*)grab_object( console
);
1440 console
->num_proc
++;
1442 reply
->handle_in
= in
;
1444 release_object( console
);
1447 close_handle( current
->process
, in
);
1449 release_object( console
);
1452 release_object( process
);
1455 /* free the console of the current process */
1456 DECL_HANDLER(free_console
)
1458 free_console( current
->process
);
1461 /* let the renderer peek the events it's waiting on */
1462 DECL_HANDLER(get_console_renderer_events
)
1464 struct console_input_events
*evt
;
1466 evt
= (struct console_input_events
*)get_handle_obj( current
->process
, req
->handle
,
1467 FILE_READ_PROPERTIES
, &console_input_events_ops
);
1469 console_input_events_get( evt
);
1470 release_object( evt
);
1473 /* open a handle to the process console */
1474 DECL_HANDLER(open_console
)
1476 struct object
*obj
= NULL
;
1481 if (current
->process
->console
)
1482 obj
= grab_object( (struct object
*)current
->process
->console
);
1484 else if (req
->from
== (obj_handle_t
)1)
1486 if (current
->process
->console
&& current
->process
->console
->active
)
1487 obj
= grab_object( (struct object
*)current
->process
->console
->active
);
1489 else if ((obj
= get_handle_obj( current
->process
, req
->from
,
1490 FILE_READ_PROPERTIES
|FILE_WRITE_PROPERTIES
, &console_input_ops
)))
1492 struct console_input
*console
= (struct console_input
*)obj
;
1493 obj
= (console
->active
) ? grab_object( console
->active
) : NULL
;
1494 release_object( console
);
1497 /* FIXME: req->share is not used (as in screen buffer creation) */
1500 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->attributes
);
1501 release_object( obj
);
1503 else if (!get_error()) set_error( STATUS_ACCESS_DENIED
);
1506 /* set info about a console input */
1507 DECL_HANDLER(set_console_input_info
)
1509 set_console_input_info( req
, get_req_data(), get_req_data_size() );
1512 /* get info about a console (output only) */
1513 DECL_HANDLER(get_console_input_info
)
1515 struct console_input
*console
;
1517 if (!(console
= console_input_get( req
->handle
, FILE_READ_PROPERTIES
))) return;
1520 data_size_t len
= strlenW( console
->title
) * sizeof(WCHAR
);
1521 if (len
> get_reply_max_size()) len
= get_reply_max_size();
1522 set_reply_data( console
->title
, len
);
1524 reply
->history_mode
= console
->history_mode
;
1525 reply
->history_size
= console
->history_size
;
1526 reply
->history_index
= console
->history_index
;
1527 reply
->edition_mode
= console
->edition_mode
;
1528 reply
->input_cp
= console
->input_cp
;
1529 reply
->output_cp
= console
->output_cp
;
1530 reply
->win
= console
->win
;
1532 release_object( console
);
1535 /* get a console mode (input or output) */
1536 DECL_HANDLER(get_console_mode
)
1538 reply
->mode
= get_console_mode( req
->handle
);
1541 /* set a console mode (input or output) */
1542 DECL_HANDLER(set_console_mode
)
1544 set_console_mode( req
->handle
, req
->mode
);
1547 /* add input records to a console input queue */
1548 DECL_HANDLER(write_console_input
)
1550 struct console_input
*console
;
1553 if (!(console
= (struct console_input
*)get_handle_obj( current
->process
, req
->handle
,
1554 FILE_WRITE_PROPERTIES
, &console_input_ops
)))
1556 reply
->written
= write_console_input( console
, get_req_data_size() / sizeof(INPUT_RECORD
),
1558 release_object( console
);
1561 /* fetch input records from a console input queue */
1562 DECL_HANDLER(read_console_input
)
1564 int count
= get_reply_max_size() / sizeof(INPUT_RECORD
);
1565 reply
->read
= read_console_input( req
->handle
, count
, req
->flush
);
1568 /* appends a string to console's history */
1569 DECL_HANDLER(append_console_input_history
)
1571 struct console_input
*console
;
1573 if (!(console
= console_input_get( req
->handle
, FILE_WRITE_PROPERTIES
))) return;
1574 console_input_append_hist( console
, get_req_data(), get_req_data_size() / sizeof(WCHAR
) );
1575 release_object( console
);
1578 /* appends a string to console's history */
1579 DECL_HANDLER(get_console_input_history
)
1581 struct console_input
*console
;
1583 if (!(console
= console_input_get( req
->handle
, FILE_READ_PROPERTIES
))) return;
1584 reply
->total
= console_input_get_hist( console
, req
->index
);
1585 release_object( console
);
1588 /* creates a screen buffer */
1589 DECL_HANDLER(create_console_output
)
1591 struct console_input
* console
;
1592 struct screen_buffer
* screen_buffer
;
1597 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
1599 set_error( STATUS_INVALID_HANDLE
);
1604 if (!(console
= console_input_get( req
->handle_in
, FILE_WRITE_PROPERTIES
)))
1609 if (console_input_is_bare( console
) ^ (fd
!= -1))
1612 release_object( console
);
1613 set_error( STATUS_INVALID_HANDLE
);
1617 screen_buffer
= create_console_output( console
, fd
);
1620 /* FIXME: should store sharing and test it when opening the CONOUT$ device
1621 * see file.c on how this could be done */
1622 reply
->handle_out
= alloc_handle( current
->process
, screen_buffer
, req
->access
, req
->attributes
);
1623 release_object( screen_buffer
);
1625 release_object( console
);
1628 /* set info about a console screen buffer */
1629 DECL_HANDLER(set_console_output_info
)
1631 struct screen_buffer
*screen_buffer
;
1633 if ((screen_buffer
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
1634 FILE_WRITE_PROPERTIES
, &screen_buffer_ops
)))
1636 set_console_output_info( screen_buffer
, req
);
1637 release_object( screen_buffer
);
1641 /* get info about a console screen buffer */
1642 DECL_HANDLER(get_console_output_info
)
1644 struct screen_buffer
*screen_buffer
;
1646 if ((screen_buffer
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
1647 FILE_READ_PROPERTIES
, &screen_buffer_ops
)))
1649 reply
->cursor_size
= screen_buffer
->cursor_size
;
1650 reply
->cursor_visible
= screen_buffer
->cursor_visible
;
1651 reply
->cursor_x
= screen_buffer
->cursor_x
;
1652 reply
->cursor_y
= screen_buffer
->cursor_y
;
1653 reply
->width
= screen_buffer
->width
;
1654 reply
->height
= screen_buffer
->height
;
1655 reply
->attr
= screen_buffer
->attr
;
1656 reply
->win_left
= screen_buffer
->win
.left
;
1657 reply
->win_top
= screen_buffer
->win
.top
;
1658 reply
->win_right
= screen_buffer
->win
.right
;
1659 reply
->win_bottom
= screen_buffer
->win
.bottom
;
1660 reply
->max_width
= screen_buffer
->max_width
;
1661 reply
->max_height
= screen_buffer
->max_height
;
1662 release_object( screen_buffer
);
1666 /* read data (chars & attrs) from a screen buffer */
1667 DECL_HANDLER(read_console_output
)
1669 struct screen_buffer
*screen_buffer
;
1671 if ((screen_buffer
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
1672 FILE_READ_DATA
, &screen_buffer_ops
)))
1674 if (console_input_is_bare( screen_buffer
->input
))
1676 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1677 release_object( screen_buffer
);
1680 read_console_output( screen_buffer
, req
->x
, req
->y
, req
->mode
, req
->wrap
);
1681 reply
->width
= screen_buffer
->width
;
1682 reply
->height
= screen_buffer
->height
;
1683 release_object( screen_buffer
);
1687 /* write data (char and/or attrs) to a screen buffer */
1688 DECL_HANDLER(write_console_output
)
1690 struct screen_buffer
*screen_buffer
;
1692 if ((screen_buffer
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
1693 FILE_WRITE_DATA
, &screen_buffer_ops
)))
1695 if (console_input_is_bare( screen_buffer
->input
))
1697 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1698 release_object( screen_buffer
);
1701 reply
->written
= write_console_output( screen_buffer
, get_req_data_size(), get_req_data(),
1702 req
->mode
, req
->x
, req
->y
, req
->wrap
);
1703 reply
->width
= screen_buffer
->width
;
1704 reply
->height
= screen_buffer
->height
;
1705 release_object( screen_buffer
);
1709 /* fill a screen buffer with constant data (chars and/or attributes) */
1710 DECL_HANDLER(fill_console_output
)
1712 struct screen_buffer
*screen_buffer
;
1714 if ((screen_buffer
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
1715 FILE_WRITE_DATA
, &screen_buffer_ops
)))
1717 if (console_input_is_bare( screen_buffer
->input
))
1719 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1720 release_object( screen_buffer
);
1723 reply
->written
= fill_console_output( screen_buffer
, req
->data
, req
->mode
,
1724 req
->x
, req
->y
, req
->count
, req
->wrap
);
1725 release_object( screen_buffer
);
1729 /* move a rect of data in a screen buffer */
1730 DECL_HANDLER(move_console_output
)
1732 struct screen_buffer
*screen_buffer
;
1734 if ((screen_buffer
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
1735 FILE_WRITE_DATA
, &screen_buffer_ops
)))
1737 if (console_input_is_bare( screen_buffer
->input
))
1739 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1740 release_object( screen_buffer
);
1743 scroll_console_output( screen_buffer
, req
->x_src
, req
->y_src
, req
->x_dst
, req
->y_dst
,
1745 release_object( screen_buffer
);
1749 /* sends a signal to a console (process, group...) */
1750 DECL_HANDLER(send_console_signal
)
1754 group
= req
->group_id
? req
->group_id
: current
->process
->group_id
;
1757 set_error( STATUS_INVALID_PARAMETER
);
1759 propagate_console_signal( current
->process
->console
, req
->signal
, group
);
1762 /* get console which renderer is 'current' */
1763 static int cgwe_enum( struct process
* process
, void* user
)
1765 if (process
->console
&& process
->console
->renderer
== current
)
1767 *(struct console_input
**)user
= process
->console
;
1773 DECL_HANDLER(get_console_wait_event
)
1775 struct console_input
* console
= NULL
;
1777 if (current
->process
->console
)
1778 console
= (struct console_input
*)grab_object( (struct object
*)current
->process
->console
);
1779 else enum_processes(cgwe_enum
, &console
);
1783 reply
->handle
= alloc_handle( current
->process
, console
->event
, EVENT_ALL_ACCESS
, 0 );
1784 release_object( console
);
1786 else set_error( STATUS_INVALID_PARAMETER
);