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 int mode
; /* input mode */
42 struct screen_buffer
*output
; /* associated screen buffer */
43 int recnum
; /* number of input records */
44 INPUT_RECORD
*records
; /* input records */
49 struct object obj
; /* object header */
50 int mode
; /* output mode */
51 struct console_input
*input
; /* associated console input */
52 int cursor_size
; /* size of cursor (percentage filled) */
53 int cursor_visible
;/* cursor visibility flag */
54 int pid
; /* xterm pid (hack) */
55 char *title
; /* console title */
59 static void console_input_dump( struct object
*obj
, int verbose
);
60 static int console_input_get_poll_events( struct object
*obj
);
61 static int console_input_get_read_fd( struct object
*obj
);
62 static void console_input_destroy( struct object
*obj
);
64 static void screen_buffer_dump( struct object
*obj
, int verbose
);
65 static int screen_buffer_get_poll_events( struct object
*obj
);
66 static int screen_buffer_get_write_fd( struct object
*obj
);
67 static void screen_buffer_destroy( struct object
*obj
);
70 static int console_get_info( struct object
*obj
, struct get_file_info_request
*req
);
72 static const struct object_ops console_input_ops
=
74 sizeof(struct console_input
), /* size */
75 console_input_dump
, /* dump */
76 default_poll_add_queue
, /* add_queue */
77 default_poll_remove_queue
, /* remove_queue */
78 default_poll_signaled
, /* signaled */
79 no_satisfied
, /* satisfied */
80 console_input_get_poll_events
, /* get_poll_events */
81 default_poll_event
, /* poll_event */
82 console_input_get_read_fd
, /* get_read_fd */
83 no_write_fd
, /* get_write_fd */
85 console_get_info
, /* get_file_info */
86 console_input_destroy
/* destroy */
89 static const struct object_ops screen_buffer_ops
=
91 sizeof(struct screen_buffer
), /* size */
92 screen_buffer_dump
, /* dump */
93 default_poll_add_queue
, /* add_queue */
94 default_poll_remove_queue
, /* remove_queue */
95 default_poll_signaled
, /* signaled */
96 no_satisfied
, /* satisfied */
97 screen_buffer_get_poll_events
, /* get_poll_events */
98 default_poll_event
, /* poll_event */
99 no_read_fd
, /* get_read_fd */
100 screen_buffer_get_write_fd
, /* get_write_fd */
101 no_flush
, /* flush */
102 console_get_info
, /* get_file_info */
103 screen_buffer_destroy
/* destroy */
107 static struct object
*create_console_input( int fd
)
109 struct console_input
*console_input
;
111 if ((fd
= (fd
!= -1) ? dup(fd
) : dup(0)) == -1)
116 if (!(console_input
= alloc_object( &console_input_ops
, fd
))) return NULL
;
117 console_input
->mode
= ENABLE_PROCESSED_INPUT
| ENABLE_LINE_INPUT
|
118 ENABLE_ECHO_INPUT
| ENABLE_MOUSE_INPUT
;
119 console_input
->output
= NULL
;
120 console_input
->recnum
= 0;
121 console_input
->records
= NULL
;
122 return &console_input
->obj
;
125 static struct object
*create_console_output( int fd
, struct object
*input
)
127 struct console_input
*console_input
= (struct console_input
*)input
;
128 struct screen_buffer
*screen_buffer
;
130 if ((fd
= (fd
!= -1) ? dup(fd
) : dup(1)) == -1)
135 if (!(screen_buffer
= alloc_object( &screen_buffer_ops
, fd
))) return NULL
;
136 screen_buffer
->mode
= ENABLE_PROCESSED_OUTPUT
| ENABLE_WRAP_AT_EOL_OUTPUT
;
137 screen_buffer
->input
= console_input
;
138 screen_buffer
->cursor_size
= 100;
139 screen_buffer
->cursor_visible
= 1;
140 screen_buffer
->pid
= 0;
141 screen_buffer
->title
= strdup( "Wine console" );
142 console_input
->output
= screen_buffer
;
143 return &screen_buffer
->obj
;
146 /* allocate a console for this process */
147 int alloc_console( struct process
*process
)
149 if (process
->console_in
|| process
->console_out
)
151 set_error( STATUS_ACCESS_DENIED
);
154 if ((process
->console_in
= create_console_input( -1 )))
156 if ((process
->console_out
= create_console_output( -1, process
->console_in
)))
158 release_object( process
->console_in
);
163 /* free the console for this process */
164 int free_console( struct process
*process
)
166 if (process
->console_in
) release_object( process
->console_in
);
167 if (process
->console_out
) release_object( process
->console_out
);
168 process
->console_in
= process
->console_out
= NULL
;
172 static int set_console_fd( int handle
, int fd_in
, int fd_out
, int pid
)
174 struct console_input
*input
;
175 struct screen_buffer
*output
;
178 if (!(obj
= get_handle_obj( current
->process
, handle
, 0, NULL
)))
180 if (obj
->ops
== &console_input_ops
)
182 input
= (struct console_input
*)obj
;
183 output
= input
->output
;
184 grab_object( output
);
186 else if (obj
->ops
== &screen_buffer_ops
)
188 output
= (struct screen_buffer
*)obj
;
189 input
= output
->input
;
190 grab_object( input
);
194 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
195 release_object( obj
);
199 /* can't change the fd if someone is waiting on it */
200 assert( !input
->obj
.head
);
201 assert( !output
->obj
.head
);
203 change_select_fd( &input
->obj
, fd_in
);
204 change_select_fd( &output
->obj
, fd_out
);
206 release_object( input
);
207 release_object( output
);
211 static int get_console_mode( int handle
)
216 if ((obj
= get_handle_obj( current
->process
, handle
, GENERIC_READ
, NULL
)))
218 if (obj
->ops
== &console_input_ops
)
219 ret
= ((struct console_input
*)obj
)->mode
;
220 else if (obj
->ops
== &screen_buffer_ops
)
221 ret
= ((struct screen_buffer
*)obj
)->mode
;
223 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
224 release_object( obj
);
229 static int set_console_mode( int handle
, int mode
)
234 if (!(obj
= get_handle_obj( current
->process
, handle
, GENERIC_READ
, NULL
)))
236 if (obj
->ops
== &console_input_ops
)
238 ((struct console_input
*)obj
)->mode
= mode
;
241 else if (obj
->ops
== &screen_buffer_ops
)
243 ((struct screen_buffer
*)obj
)->mode
= mode
;
246 else set_error( STATUS_OBJECT_TYPE_MISMATCH
);
247 release_object( obj
);
251 /* set misc console information (output handle only) */
252 static int set_console_info( int handle
, struct set_console_info_request
*req
,
253 const char *title
, size_t len
)
255 struct screen_buffer
*console
;
256 if (!(console
= (struct screen_buffer
*)get_handle_obj( current
->process
, handle
,
257 GENERIC_WRITE
, &screen_buffer_ops
)))
259 if (req
->mask
& SET_CONSOLE_INFO_CURSOR
)
261 console
->cursor_size
= req
->cursor_size
;
262 console
->cursor_visible
= req
->cursor_visible
;
264 if (req
->mask
& SET_CONSOLE_INFO_TITLE
)
266 char *new_title
= mem_alloc( len
+ 1 );
269 memcpy( new_title
, title
, len
);
271 if (console
->title
) free( console
->title
);
272 console
->title
= new_title
;
275 release_object( console
);
279 /* add input events to a console input queue */
280 static int write_console_input( int handle
, int count
, INPUT_RECORD
*records
)
282 INPUT_RECORD
*new_rec
;
283 struct console_input
*console
;
285 if (!(console
= (struct console_input
*)get_handle_obj( current
->process
, handle
,
286 GENERIC_WRITE
, &console_input_ops
)))
288 if (!(new_rec
= realloc( console
->records
,
289 (console
->recnum
+ count
) * sizeof(INPUT_RECORD
) )))
291 set_error( STATUS_NO_MEMORY
);
292 release_object( console
);
295 console
->records
= new_rec
;
296 memcpy( new_rec
+ console
->recnum
, records
, count
* sizeof(INPUT_RECORD
) );
297 console
->recnum
+= count
;
298 release_object( console
);
302 /* retrieve a pointer to the console input records */
303 static int read_console_input( int handle
, int count
, INPUT_RECORD
*rec
, int flush
)
305 struct console_input
*console
;
307 if (!(console
= (struct console_input
*)get_handle_obj( current
->process
, handle
,
308 GENERIC_READ
, &console_input_ops
)))
313 /* special case: do not retrieve anything, but return
314 * the total number of records available */
315 count
= console
->recnum
;
319 if (count
> console
->recnum
) count
= console
->recnum
;
320 memcpy( rec
, console
->records
, count
* sizeof(INPUT_RECORD
) );
325 for (i
= count
; i
< console
->recnum
; i
++)
326 console
->records
[i
-count
] = console
->records
[i
];
327 if ((console
->recnum
-= count
) > 0)
329 INPUT_RECORD
*new_rec
= realloc( console
->records
,
330 console
->recnum
* sizeof(INPUT_RECORD
) );
331 if (new_rec
) console
->records
= new_rec
;
335 free( console
->records
);
336 console
->records
= NULL
;
339 release_object( console
);
343 static void console_input_dump( struct object
*obj
, int verbose
)
345 struct console_input
*console
= (struct console_input
*)obj
;
346 assert( obj
->ops
== &console_input_ops
);
347 fprintf( stderr
, "Console input fd=%d\n", console
->obj
.fd
);
350 static int console_input_get_poll_events( struct object
*obj
)
355 static int console_input_get_read_fd( struct object
*obj
)
357 struct console_input
*console
= (struct console_input
*)obj
;
358 assert( obj
->ops
== &console_input_ops
);
359 return dup( console
->obj
.fd
);
362 static int console_get_info( struct object
*obj
, struct get_file_info_request
*req
)
364 req
->type
= FILE_TYPE_CHAR
;
366 req
->access_time
= 0;
377 static void console_input_destroy( struct object
*obj
)
379 struct console_input
*console
= (struct console_input
*)obj
;
380 assert( obj
->ops
== &console_input_ops
);
381 if (console
->output
) console
->output
->input
= NULL
;
384 static void screen_buffer_dump( struct object
*obj
, int verbose
)
386 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
387 assert( obj
->ops
== &screen_buffer_ops
);
388 fprintf( stderr
, "Console screen buffer fd=%d\n", console
->obj
.fd
);
391 static int screen_buffer_get_poll_events( struct object
*obj
)
396 static int screen_buffer_get_write_fd( struct object
*obj
)
398 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
399 assert( obj
->ops
== &screen_buffer_ops
);
400 return dup( console
->obj
.fd
);
403 static void screen_buffer_destroy( struct object
*obj
)
405 struct screen_buffer
*console
= (struct screen_buffer
*)obj
;
406 assert( obj
->ops
== &screen_buffer_ops
);
407 if (console
->input
) console
->input
->output
= NULL
;
408 if (console
->title
) free( console
->title
);
411 /* allocate a console for the current process */
412 DECL_HANDLER(alloc_console
)
414 int in
= -1, out
= -1;
416 if (!alloc_console( current
->process
)) goto done
;
418 if ((in
= alloc_handle( current
->process
, current
->process
->console_in
,
419 req
->access
, req
->inherit
)) != -1)
421 if ((out
= alloc_handle( current
->process
, current
->process
->console_out
,
422 req
->access
, req
->inherit
)) != -1)
423 goto done
; /* everything is fine */
424 close_handle( current
->process
, in
);
427 free_console( current
->process
);
431 req
->handle_out
= out
;
434 /* free the console of the current process */
435 DECL_HANDLER(free_console
)
437 free_console( current
->process
);
440 /* open a handle to the process console */
441 DECL_HANDLER(open_console
)
443 struct object
*obj
= req
->output
? current
->process
->console_out
: current
->process
->console_in
;
445 if (obj
) req
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
446 else set_error( STATUS_ACCESS_DENIED
);
449 /* set info about a console (output only) */
450 DECL_HANDLER(set_console_info
)
452 set_console_info( req
->handle
, req
, get_req_data(req
), get_req_data_size(req
) );
455 /* get info about a console (output only) */
456 DECL_HANDLER(get_console_info
)
458 struct screen_buffer
*console
;
461 if ((console
= (struct screen_buffer
*)get_handle_obj( current
->process
, req
->handle
,
462 GENERIC_READ
, &screen_buffer_ops
)))
464 req
->cursor_size
= console
->cursor_size
;
465 req
->cursor_visible
= console
->cursor_visible
;
466 req
->pid
= console
->pid
;
469 len
= strlen( console
->title
);
470 if (len
> get_req_data_size(req
)) len
= get_req_data_size(req
);
471 memcpy( get_req_data(req
), console
->title
, len
);
473 release_object( console
);
475 set_req_data_size( req
, len
);
478 /* set a console fd */
479 DECL_HANDLER(set_console_fd
)
484 if (!(obj
= get_handle_obj( current
->process
, req
->file_handle
,
485 GENERIC_READ
| GENERIC_WRITE
, NULL
))) return;
486 if ((fd_in
= obj
->ops
->get_read_fd( obj
)) == -1)
488 release_object( obj
);
491 fd_out
= obj
->ops
->get_write_fd( obj
);
492 release_object( obj
);
495 if (set_console_fd( req
->handle
, fd_in
, fd_out
, req
->pid
)) return;
501 /* get a console mode (input or output) */
502 DECL_HANDLER(get_console_mode
)
504 req
->mode
= get_console_mode( req
->handle
);
507 /* set a console mode (input or output) */
508 DECL_HANDLER(set_console_mode
)
510 set_console_mode( req
->handle
, req
->mode
);
513 /* add input records to a console input queue */
514 DECL_HANDLER(write_console_input
)
516 req
->written
= write_console_input( req
->handle
, get_req_data_size(req
) / sizeof(INPUT_RECORD
),
520 /* fetch input records from a console input queue */
521 DECL_HANDLER(read_console_input
)
523 size_t size
= get_req_data_size(req
) / sizeof(INPUT_RECORD
);
524 int res
= read_console_input( req
->handle
, size
, get_req_data(req
), req
->flush
);
525 /* if size was 0 we didn't fetch anything */
526 if (size
) set_req_data_size( req
, res
* sizeof(INPUT_RECORD
) );