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.
16 #include <sys/errno.h>
19 #include <sys/types.h>
26 #include "server/thread.h"
32 struct object obj; /* object header */
33 int fd; /* Unix file descriptor */
34 int mode; /* input mode */
35 struct screen_buffer *output; /* associated screen buffer */
40 struct object obj; /* object header */
41 int fd; /* Unix file descriptor */
42 int mode; /* output mode */
43 struct console_input *input; /* associated console input */
44 int cursor_size; /* size of cursor (percentage filled) */
45 int cursor_visible;/* cursor visibility flag */
46 int pid; /* xterm pid (hack) */
47 char *title; /* console title */
51 static void console_input_dump( struct object *obj, int verbose );
52 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry );
53 static void console_input_remove_queue( struct object *obj, struct wait_queue_entry *entry );
54 static int console_input_signaled( struct object *obj, struct thread *thread );
55 static int console_input_get_read_fd( struct object *obj );
56 static void console_input_destroy( struct object *obj );
58 static void screen_buffer_dump( struct object *obj, int verbose );
59 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
60 static void screen_buffer_remove_queue( struct object *obj, struct wait_queue_entry *entry );
61 static int screen_buffer_signaled( struct object *obj, struct thread *thread );
62 static int screen_buffer_get_write_fd( struct object *obj );
63 static void screen_buffer_destroy( struct object *obj );
66 static int console_get_info( struct object *obj, struct get_file_info_reply *reply );
68 static const struct object_ops console_input_ops =
71 console_input_add_queue,
72 console_input_remove_queue,
73 console_input_signaled,
75 console_input_get_read_fd,
82 static const struct object_ops screen_buffer_ops =
85 screen_buffer_add_queue,
86 screen_buffer_remove_queue,
87 screen_buffer_signaled,
90 screen_buffer_get_write_fd,
96 static const struct select_ops select_ops =
99 NULL /* we never set a timeout on a console */
102 int create_console( int fd, struct object *obj[2] )
104 struct console_input *console_input;
105 struct screen_buffer *screen_buffer;
106 int read_fd, write_fd;
108 if ((read_fd = (fd != -1) ? dup(fd) : dup(0)) == -1)
113 if ((write_fd = (fd != -1) ? dup(fd) : dup(1)) == -1)
119 if (!(console_input = mem_alloc( sizeof(struct console_input) )))
125 if (!(screen_buffer = mem_alloc( sizeof(struct screen_buffer) )))
129 free( console_input );
132 init_object( &console_input->obj, &console_input_ops, NULL );
133 init_object( &screen_buffer->obj, &screen_buffer_ops, NULL );
134 console_input->fd = read_fd;
135 console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
136 ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
137 console_input->output = screen_buffer;
138 screen_buffer->fd = write_fd;
139 screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
140 screen_buffer->input = console_input;
141 screen_buffer->cursor_size = 100;
142 screen_buffer->cursor_visible = 1;
143 screen_buffer->pid = 0;
144 screen_buffer->title = strdup( "Wine console" );
146 obj[0] = &console_input->obj;
147 obj[1] = &screen_buffer->obj;
151 int set_console_fd( int handle, int fd, int pid )
153 struct console_input *input;
154 struct screen_buffer *output;
158 if (!(obj = get_handle_obj( current->process, handle, 0, NULL )))
160 if (obj->ops == &console_input_ops)
162 input = (struct console_input *)obj;
163 output = input->output;
164 grab_object( output );
166 else if (obj->ops == &screen_buffer_ops)
168 output = (struct screen_buffer *)obj;
169 input = output->input;
170 grab_object( input );
174 SET_ERROR( ERROR_INVALID_HANDLE );
175 release_object( obj );
179 if ((fd_in = dup(fd)) == -1)
182 release_object( input );
183 release_object( output );
186 if ((fd_out = dup(fd)) == -1)
190 release_object( input );
191 release_object( output );
199 release_object( input );
200 release_object( output );
204 int get_console_mode( int handle, int *mode )
209 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
211 if (obj->ops == &console_input_ops)
213 *mode = ((struct console_input *)obj)->mode;
216 else if (obj->ops == &screen_buffer_ops)
218 *mode = ((struct screen_buffer *)obj)->mode;
221 else SET_ERROR( ERROR_INVALID_HANDLE );
222 release_object( obj );
226 int set_console_mode( int handle, int mode )
231 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
233 if (obj->ops == &console_input_ops)
235 ((struct console_input *)obj)->mode = mode;
238 else if (obj->ops == &screen_buffer_ops)
240 ((struct screen_buffer *)obj)->mode = mode;
243 else SET_ERROR( ERROR_INVALID_HANDLE );
244 release_object( obj );
248 /* set misc console information (output handle only) */
249 int set_console_info( int handle, struct set_console_info_request *req, const char *title )
251 struct screen_buffer *console;
252 if (!(console = (struct screen_buffer *)get_handle_obj( current->process, handle,
253 GENERIC_WRITE, &screen_buffer_ops )))
255 if (req->mask & SET_CONSOLE_INFO_CURSOR)
257 console->cursor_size = req->cursor_size;
258 console->cursor_visible = req->cursor_visible;
260 if (req->mask & SET_CONSOLE_INFO_TITLE)
262 if (console->title) free( console->title );
263 console->title = strdup( title );
265 release_object( console );
269 /* get misc console information (output handle only) */
270 int get_console_info( int handle, struct get_console_info_reply *reply, const char **title )
272 struct screen_buffer *console;
273 if (!(console = (struct screen_buffer *)get_handle_obj( current->process, handle,
274 GENERIC_READ, &screen_buffer_ops )))
276 reply->cursor_size = console->cursor_size;
277 reply->cursor_visible = console->cursor_visible;
278 reply->pid = console->pid;
279 *title = console->title;
280 release_object( console );
284 static void console_input_dump( struct object *obj, int verbose )
286 struct console_input *console = (struct console_input *)obj;
287 assert( obj->ops == &console_input_ops );
288 fprintf( stderr, "Console input fd=%d\n", console->fd );
291 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
293 struct console_input *console = (struct console_input *)obj;
294 assert( obj->ops == &console_input_ops );
295 if (!obj->head) /* first on the queue */
297 if (!add_select_user( console->fd, READ_EVENT, &select_ops, console ))
299 SET_ERROR( ERROR_OUTOFMEMORY );
303 add_queue( obj, entry );
307 static void console_input_remove_queue( struct object *obj, struct wait_queue_entry *entry )
309 struct console_input *console = (struct console_input *)grab_object(obj);
310 assert( obj->ops == &console_input_ops );
312 remove_queue( obj, entry );
313 if (!obj->head) /* last on the queue is gone */
314 remove_select_user( console->fd );
315 release_object( obj );
318 static int console_input_signaled( struct object *obj, struct thread *thread )
321 struct timeval tv = { 0, 0 };
322 struct console_input *console = (struct console_input *)obj;
323 assert( obj->ops == &console_input_ops );
326 FD_SET( console->fd, &fds );
327 return select( console->fd + 1, &fds, NULL, NULL, &tv ) > 0;
330 static int console_input_get_read_fd( struct object *obj )
332 struct console_input *console = (struct console_input *)obj;
333 assert( obj->ops == &console_input_ops );
334 return dup( console->fd );
337 static int console_get_info( struct object *obj, struct get_file_info_reply *reply )
339 memset( reply, 0, sizeof(*reply) );
340 reply->type = FILE_TYPE_CHAR;
344 static void console_input_destroy( struct object *obj )
346 struct console_input *console = (struct console_input *)obj;
347 assert( obj->ops == &console_input_ops );
348 close( console->fd );
349 if (console->output) console->output->input = NULL;
353 static void screen_buffer_dump( struct object *obj, int verbose )
355 struct screen_buffer *console = (struct screen_buffer *)obj;
356 assert( obj->ops == &screen_buffer_ops );
357 fprintf( stderr, "Console screen buffer fd=%d\n", console->fd );
360 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
362 struct screen_buffer *console = (struct screen_buffer *)obj;
363 assert( obj->ops == &screen_buffer_ops );
364 if (!obj->head) /* first on the queue */
366 if (!add_select_user( console->fd, WRITE_EVENT, &select_ops, console ))
368 SET_ERROR( ERROR_OUTOFMEMORY );
372 add_queue( obj, entry );
376 static void screen_buffer_remove_queue( struct object *obj, struct wait_queue_entry *entry )
378 struct screen_buffer *console = (struct screen_buffer *)grab_object(obj);
379 assert( obj->ops == &screen_buffer_ops );
381 remove_queue( obj, entry );
382 if (!obj->head) /* last on the queue is gone */
383 remove_select_user( console->fd );
384 release_object( obj );
387 static int screen_buffer_signaled( struct object *obj, struct thread *thread )
390 struct timeval tv = { 0, 0 };
391 struct screen_buffer *console = (struct screen_buffer *)obj;
392 assert( obj->ops == &screen_buffer_ops );
395 FD_SET( console->fd, &fds );
396 return select( console->fd + 1, NULL, &fds, NULL, &tv ) > 0;
399 static int screen_buffer_get_write_fd( struct object *obj )
401 struct screen_buffer *console = (struct screen_buffer *)obj;
402 assert( obj->ops == &screen_buffer_ops );
403 return dup( console->fd );
406 static void screen_buffer_destroy( struct object *obj )
408 struct screen_buffer *console = (struct screen_buffer *)obj;
409 assert( obj->ops == &screen_buffer_ops );
410 close( console->fd );
411 if (console->input) console->input->output = NULL;
412 if (console->pid) kill( console->pid, SIGTERM );
413 if (console->title) free( console->title );