2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
6 * FIXME: all this stuff is a hack to avoid breaking
7 * the client-side console support.
18 #ifdef HAVE_SYS_ERRNO_H
19 #include <sys/errno.h>
23 #include <sys/types.h>
40 struct object obj
; /* object header */
41 struct select_user select
; /* select user */
42 int mode
; /* input mode */
43 struct screen_buffer
*output
; /* associated screen buffer */
44 int recnum
; /* number of input records */
45 INPUT_RECORD
*records
; /* input records */
50 struct object obj
; /* object header */
51 struct select_user select
; /* select user */
52 int mode
; /* output mode */
53 struct console_input
*input
; /* associated console input */
54 int cursor_size
; /* size of cursor (percentage filled) */
55 int cursor_visible
;/* cursor visibility flag */
56 int pid
; /* xterm pid (hack) */
57 char *title
; /* console title */
61 static void console_input_dump( struct object
*obj
, int verbose
);
62 static int console_input_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
63 static void console_input_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
64 static int console_input_signaled( struct object
*obj
, struct thread
*thread
);
65 static int console_input_get_read_fd( struct object
*obj
);
66 static void console_input_destroy( struct object
*obj
);
68 static void screen_buffer_dump( struct object
*obj
, int verbose
);
69 static int screen_buffer_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
70 static void screen_buffer_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
71 static int screen_buffer_signaled( struct object
*obj
, struct thread
*thread
);
72 static int screen_buffer_get_write_fd( struct object
*obj
);
73 static void screen_buffer_destroy( struct object
*obj
);
76 static int console_get_info( struct object
*obj
, struct get_file_info_request
*req
);
78 static const struct object_ops console_input_ops
=
80 sizeof(struct console_input
),
82 console_input_add_queue
,
83 console_input_remove_queue
,
84 console_input_signaled
,
86 console_input_get_read_fd
,
93 static const struct object_ops screen_buffer_ops
=
95 sizeof(struct screen_buffer
),
97 screen_buffer_add_queue
,
98 screen_buffer_remove_queue
,
99 screen_buffer_signaled
,
102 screen_buffer_get_write_fd
,
105 screen_buffer_destroy
109 static struct object
*create_console_input( int fd
)
111 struct console_input
*console_input
;
113 if ((fd
= (fd
!= -1) ? dup(fd
) : dup(0)) == -1)
118 if ((console_input
= alloc_object( &console_input_ops
)))
120 console_input
->select
.fd
= fd
;
121 console_input
->select
.func
= default_select_event
;
122 console_input
->select
.private = console_input
;
123 console_input
->mode
= ENABLE_PROCESSED_INPUT
| ENABLE_LINE_INPUT
|
124 ENABLE_ECHO_INPUT
| ENABLE_MOUSE_INPUT
;
125 console_input
->output
= NULL
;
126 console_input
->recnum
= 0;
127 console_input
->records
= NULL
;
128 register_select_user( &console_input
->select
);
129 return &console_input
->obj
;
135 static struct object
*create_console_output( int fd
, struct object
*input
)
137 struct console_input
*console_input
= (struct console_input
*)input
;
138 struct screen_buffer
*screen_buffer
;
140 if ((fd
= (fd
!= -1) ? dup(fd
) : dup(1)) == -1)
145 if ((screen_buffer
= alloc_object( &screen_buffer_ops
)))
147 screen_buffer
->select
.fd
= fd
;
148 screen_buffer
->select
.func
= default_select_event
;
149 screen_buffer
->select
.private = screen_buffer
;
150 screen_buffer
->mode
= ENABLE_PROCESSED_OUTPUT
| ENABLE_WRAP_AT_EOL_OUTPUT
;
151 screen_buffer
->input
= console_input
;
152 screen_buffer
->cursor_size
= 100;
153 screen_buffer
->cursor_visible
= 1;
154 screen_buffer
->pid
= 0;
155 screen_buffer
->title
= strdup( "Wine console" );
156 register_select_user( &screen_buffer
->select
);
157 console_input
->output
= screen_buffer
;
158 return &screen_buffer
->obj
;
164 /* allocate a console for this process */
165 int alloc_console( struct process
*process
)
167 if (process
->console_in
|| process
->console_out
)
169 set_error( ERROR_ACCESS_DENIED
);
172 if ((process
->console_in
= create_console_input( -1 )))
174 if ((process
->console_out
= create_console_output( -1, process
->console_in
)))
176 release_object( process
->console_in
);
181 /* free the console for this process */
182 int free_console( struct process
*process
)
184 if (process
->console_in
) release_object( process
->console_in
);
185 if (process
->console_out
) release_object( process
->console_out
);
186 process
->console_in
= process
->console_out
= NULL
;
190 static int set_console_fd( int handle
, int fd_in
, int fd_out
, int pid
)
192 struct console_input
*input
;
193 struct screen_buffer
*output
;
196 if (!(obj
= get_handle_obj( current
->process
, handle
, 0, NULL
)))
198 if (obj
->ops
== &console_input_ops
)
200 input
= (struct console_input
*)obj
;
201 output
= input
->output
;
202 grab_object( output
);
204 else if (obj
->ops
== &screen_buffer_ops
)
206 output
= (struct screen_buffer
*)obj
;
207 input
= output
->input
;
208 grab_object( input
);
212 set_error( ERROR_INVALID_HANDLE
);
213 release_object( obj
);
217 /* can't change the fd if someone is waiting on it */
218 assert( !input
->obj
.head
);
219 assert( !output
->obj
.head
);
221 unregister_select_user( &input
->select
);
222 unregister_select_user( &output
->select
);
223 close( input
->select
.fd
);
224 close( output
->select
.fd
);
225 input
->select
.fd
= fd_in
;
226 output
->select
.fd
= fd_out
;
228 register_select_user( &input
->select
);
229 register_select_user( &output
->select
);
230 release_object( input
);
231 release_object( output
);
235 static int get_console_mode( int handle
)
240 if ((obj
= get_handle_obj( current
->process
, handle
, GENERIC_READ
, NULL
)))
242 if (obj
->ops
== &console_input_ops
)
243 ret
= ((struct console_input
*)obj
)->mode
;
244 else if (obj
->ops
== &screen_buffer_ops
)
245 ret
= ((struct screen_buffer
*)obj
)->mode
;
247 set_error( ERROR_INVALID_HANDLE
);
248 release_object( obj
);
253 static int set_console_mode( int handle
, int mode
)
258 if (!(obj
= get_handle_obj( current
->process
, handle
, GENERIC_READ
, NULL
)))
260 if (obj
->ops
== &console_input_ops
)
262 ((struct console_input
*)obj
)->mode
= mode
;
265 else if (obj
->ops
== &screen_buffer_ops
)
267 ((struct screen_buffer
*)obj
)->mode
= mode
;
270 else set_error( ERROR_INVALID_HANDLE
);
271 release_object( obj
);
275 /* set misc console information (output handle only) */
276 static int set_console_info( int handle
, struct set_console_info_request
*req
,
277 const char *title
, size_t len
)
279 struct screen_buffer
*console
;
280 if (!(console
= (struct screen_buffer
*)get_handle_obj( current
->process
, handle
,
281 GENERIC_WRITE
, &screen_buffer_ops
)))
283 if (req
->mask
& SET_CONSOLE_INFO_CURSOR
)
285 console
->cursor_size
= req
->cursor_size
;
286 console
->cursor_visible
= req
->cursor_visible
;
288 if (req
->mask
& SET_CONSOLE_INFO_TITLE
)
290 char *new_title
= mem_alloc( len
+ 1 );
293 memcpy( new_title
, title
, len
);
295 if (console
->title
) free( console
->title
);
296 console
->title
= new_title
;
299 release_object( console
);
303 /* add input events to a console input queue */
304 static int write_console_input( int handle
, int count
, INPUT_RECORD
*records
)
306 INPUT_RECORD
*new_rec
;
307 struct console_input
*console
;
309 if (!(console
= (struct console_input
*)get_handle_obj( current
->process
, handle
,
310 GENERIC_WRITE
, &console_input_ops
)))
312 if (!(new_rec
= realloc( console
->records
,
313 (console
->recnum
+ count
) * sizeof(INPUT_RECORD
) )))
315 set_error( ERROR_NOT_ENOUGH_MEMORY
);
316 release_object( console
);
319 console
->records
= new_rec
;
320 memcpy( new_rec
+ console
->recnum
, records
, count
* sizeof(INPUT_RECORD
) );
321 console
->recnum
+= count
;
322 release_object( console
);
326 /* retrieve a pointer to the console input records */
327 static int read_console_input( int handle
, int count
, INPUT_RECORD
*rec
, int max
, int flush
)
329 struct console_input
*console
;
331 if (!(console
= (struct console_input
*)get_handle_obj( current
->process
, handle
,
332 GENERIC_READ
, &console_input_ops
)))
334 if ((count
< 0) || (count
> console
->recnum
)) count
= console
->recnum
;
335 if (count
> max
) count
= max
;
336 memcpy( rec
, console
->records
, count
* sizeof(INPUT_RECORD
) );
340 for (i
= count
; i
< console
->recnum
; i
++)
341 console
->records
[i
-count
] = console
->records
[i
];
342 if ((console
->recnum
-= count
) > 0)
344 INPUT_RECORD
*new_rec
= realloc( console
->records
,
345 console
->recnum
* sizeof(INPUT_RECORD
) );
346 if (new_rec
) console
->records
= new_rec
;
350 free( console
->records
);
351 console
->records
= NULL
;
354 release_object( console
);
358 static void console_input_dump( struct object
*obj
, int verbose
)
360 struct console_input
*console
= (struct console_input
*)obj
;
361 assert( obj
->ops
== &console_input_ops
);
362 fprintf( stderr
, "Console input fd=%d\n", console
->select
.fd
);
365 static int console_input_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
367 struct console_input
*console
= (struct console_input
*)obj
;
368 assert( obj
->ops
== &console_input_ops
);
369 if (!obj
->head
) /* first on the queue */
370 set_select_events( &console
->select
, READ_EVENT
);
371 add_queue( obj
, entry
);
375 static void console_input_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
377 struct console_input
*console
= (struct console_input
*)grab_object(obj
);
378 assert( obj
->ops
== &console_input_ops
);
380 remove_queue( obj
, entry
);
381 if (!obj
->head
) /* last on the queue is gone */
382 set_select_events( &console
->select
, 0 );
383 release_object( obj
);
386 static int console_input_signaled( struct object
*obj
, struct thread
*thread
)
388 struct console_input
*console
= (struct console_input
*)obj
;
389 assert( obj
->ops
== &console_input_ops
);
391 if (check_select_events( &console
->select
, READ_EVENT
))
393 /* stop waiting on select() if we are signaled */
394 set_select_events( &console
->select
, 0 );
399 /* restart waiting on select() if we are no longer signaled */
400 if (obj
->head
) set_select_events( &console
->select
, READ_EVENT
);
405 static int console_input_get_read_fd( struct object
*obj
)
407 struct console_input
*console
= (struct console_input
*)obj
;
408 assert( obj
->ops
== &console_input_ops
);
409 return dup( console
->select
.fd
);
412 static int console_get_info( struct object
*obj
, struct get_file_info_request
*req
)
414 req
->type
= FILE_TYPE_CHAR
;
416 req
->access_time
= 0;
427 static void console_input_destroy( struct object
*obj
)
429 struct console_input
*console
= (struct console_input
*)obj
;
430 assert( obj
->ops
== &console_input_ops
);
431 unregister_select_user( &console
->select
);
432 close( console
->select
.fd
);
433 if (console
->output
) console
->output
->input
= NULL
;
436 static void screen_buffer_dump( struct object
*obj
, int verbose
)
438 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
439 assert( obj
->ops
== &screen_buffer_ops
);
440 fprintf( stderr
, "Console screen buffer fd=%d\n", console
->select
.fd
);
443 static int screen_buffer_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
445 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
446 assert( obj
->ops
== &screen_buffer_ops
);
447 if (!obj
->head
) /* first on the queue */
448 set_select_events( &console
->select
, WRITE_EVENT
);
449 add_queue( obj
, entry
);
453 static void screen_buffer_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
455 struct screen_buffer
*console
= (struct screen_buffer
*)grab_object(obj
);
456 assert( obj
->ops
== &screen_buffer_ops
);
458 remove_queue( obj
, entry
);
459 if (!obj
->head
) /* last on the queue is gone */
460 set_select_events( &console
->select
, 0 );
461 release_object( obj
);
464 static int screen_buffer_signaled( struct object
*obj
, struct thread
*thread
)
466 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
467 assert( obj
->ops
== &screen_buffer_ops
);
469 if (check_select_events( &console
->select
, WRITE_EVENT
))
471 /* stop waiting on select() if we are signaled */
472 set_select_events( &console
->select
, 0 );
477 /* restart waiting on select() if we are no longer signaled */
478 if (obj
->head
) set_select_events( &console
->select
, WRITE_EVENT
);
483 static int screen_buffer_get_write_fd( struct object
*obj
)
485 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
486 assert( obj
->ops
== &screen_buffer_ops
);
487 return dup( console
->select
.fd
);
490 static void screen_buffer_destroy( struct object
*obj
)
492 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
493 assert( obj
->ops
== &screen_buffer_ops
);
494 unregister_select_user( &console
->select
);
495 close( console
->select
.fd
);
496 if (console
->input
) console
->input
->output
= NULL
;
497 if (console
->title
) free( console
->title
);
500 /* allocate a console for the current process */
501 DECL_HANDLER(alloc_console
)
503 int in
= -1, out
= -1;
505 if (!alloc_console( current
->process
)) goto done
;
507 if ((in
= alloc_handle( current
->process
, current
->process
->console_in
,
508 req
->access
, req
->inherit
)) != -1)
510 if ((out
= alloc_handle( current
->process
, current
->process
->console_out
,
511 req
->access
, req
->inherit
)) != -1)
512 goto done
; /* everything is fine */
513 close_handle( current
->process
, in
);
516 free_console( current
->process
);
520 req
->handle_out
= out
;
523 /* free the console of the current process */
524 DECL_HANDLER(free_console
)
526 free_console( current
->process
);
529 /* open a handle to the process console */
530 DECL_HANDLER(open_console
)
532 struct object
*obj
= req
->output
? current
->process
->console_out
: current
->process
->console_in
;
534 if (obj
) req
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
535 else set_error( ERROR_ACCESS_DENIED
);
538 /* set info about a console (output only) */
539 DECL_HANDLER(set_console_info
)
541 size_t len
= get_req_strlen( req
->title
);
542 set_console_info( req
->handle
, req
, req
->title
, len
);
545 /* get info about a console (output only) */
546 DECL_HANDLER(get_console_info
)
548 struct screen_buffer
*console
;
549 if ((console
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
550 GENERIC_READ
, &screen_buffer_ops
)))
552 req
->cursor_size
= console
->cursor_size
;
553 req
->cursor_visible
= console
->cursor_visible
;
554 req
->pid
= console
->pid
;
555 strcpy( req
->title
, console
->title
? console
->title
: "" );
556 release_object( console
);
560 /* set a console fd */
561 DECL_HANDLER(set_console_fd
)
566 if (!(obj
= get_handle_obj( current
->process
, req
->file_handle
,
567 GENERIC_READ
| GENERIC_WRITE
, NULL
))) return;
568 if ((fd_in
= obj
->ops
->get_read_fd( obj
)) == -1)
570 release_object( obj
);
573 fd_out
= obj
->ops
->get_write_fd( obj
);
574 release_object( obj
);
577 if (set_console_fd( req
->handle
, fd_in
, fd_out
, req
->pid
)) return;
583 /* get a console mode (input or output) */
584 DECL_HANDLER(get_console_mode
)
586 req
->mode
= get_console_mode( req
->handle
);
589 /* set a console mode (input or output) */
590 DECL_HANDLER(set_console_mode
)
592 set_console_mode( req
->handle
, req
->mode
);
595 /* add input records to a console input queue */
596 DECL_HANDLER(write_console_input
)
598 int max
= get_req_size( req
+ 1, sizeof(INPUT_RECORD
) );
599 int count
= req
->count
;
601 if (count
> max
) count
= max
;
602 req
->written
= write_console_input( req
->handle
, count
, (INPUT_RECORD
*)(req
+ 1) );
605 /* fetch input records from a console input queue */
606 DECL_HANDLER(read_console_input
)
608 int max
= get_req_size( req
+ 1, sizeof(INPUT_RECORD
) );
609 req
->read
= read_console_input( req
->handle
, req
->count
, (INPUT_RECORD
*)(req
+ 1),