Moved FILE_dommap() to memory/virtual.c (and renamed it VIRTUAL_mmap).
[wine/multimedia.git] / server / console.c
blobe8526a2f5161e555fc49fecd9d6a56e417bc525b
1 /*
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.
8 */
10 #include "config.h"
12 #include <assert.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #ifdef HAVE_SYS_ERRNO_H
19 #include <sys/errno.h>
20 #endif
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <time.h>
25 #include <unistd.h>
27 #include "winnt.h"
28 #include "winbase.h"
29 #include "wincon.h"
31 #include "handle.h"
32 #include "process.h"
33 #include "thread.h"
34 #include "request.h"
36 struct screen_buffer;
38 struct console_input
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 */
47 struct screen_buffer
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 );
69 /* common routine */
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 */
84 no_flush, /* flush */
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)
113 file_set_error();
114 return NULL;
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)
132 file_set_error();
133 return NULL;
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 );
152 return 0;
154 if ((process->console_in = create_console_input( -1 )))
156 if ((process->console_out = create_console_output( -1, process->console_in )))
157 return 1;
158 release_object( process->console_in );
160 return 0;
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;
169 return 1;
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;
176 struct object *obj;
178 if (!(obj = get_handle_obj( current->process, handle, 0, NULL )))
179 return 0;
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 );
192 else
194 set_error( STATUS_OBJECT_TYPE_MISMATCH );
195 release_object( obj );
196 return 0;
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 );
205 output->pid = pid;
206 release_object( input );
207 release_object( output );
208 return 1;
211 static int get_console_mode( int handle )
213 struct object *obj;
214 int ret = 0;
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;
222 else
223 set_error( STATUS_OBJECT_TYPE_MISMATCH );
224 release_object( obj );
226 return ret;
229 static int set_console_mode( int handle, int mode )
231 struct object *obj;
232 int ret = 0;
234 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
235 return 0;
236 if (obj->ops == &console_input_ops)
238 ((struct console_input *)obj)->mode = mode;
239 ret = 1;
241 else if (obj->ops == &screen_buffer_ops)
243 ((struct screen_buffer *)obj)->mode = mode;
244 ret = 1;
246 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
247 release_object( obj );
248 return ret;
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 )))
258 return 0;
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 );
267 if (new_title)
269 memcpy( new_title, title, len );
270 new_title[len] = 0;
271 if (console->title) free( console->title );
272 console->title = new_title;
275 release_object( console );
276 return 1;
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 )))
287 return -1;
288 if (!(new_rec = realloc( console->records,
289 (console->recnum + count) * sizeof(INPUT_RECORD) )))
291 set_error( STATUS_NO_MEMORY );
292 release_object( console );
293 return -1;
295 console->records = new_rec;
296 memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
297 console->recnum += count;
298 release_object( console );
299 return count;
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 )))
309 return -1;
311 if (!count)
313 /* special case: do not retrieve anything, but return
314 * the total number of records available */
315 count = console->recnum;
317 else
319 if (count > console->recnum) count = console->recnum;
320 memcpy( rec, console->records, count * sizeof(INPUT_RECORD) );
322 if (flush)
324 int i;
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;
333 else
335 free( console->records );
336 console->records = NULL;
339 release_object( console );
340 return count;
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 )
352 return POLLIN;
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;
365 req->attr = 0;
366 req->access_time = 0;
367 req->write_time = 0;
368 req->size_high = 0;
369 req->size_low = 0;
370 req->links = 0;
371 req->index_high = 0;
372 req->index_low = 0;
373 req->serial = 0;
374 return 1;
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 )
393 return POLLOUT;
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 );
425 in = -1;
427 free_console( current->process );
429 done:
430 req->handle_in = in;
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;
459 size_t len = 0;
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;
467 if (console->title)
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)
481 struct object *obj;
482 int fd_in, fd_out;
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 );
489 return;
491 fd_out = obj->ops->get_write_fd( obj );
492 release_object( obj );
493 if (fd_out != -1)
495 if (set_console_fd( req->handle, fd_in, fd_out, req->pid )) return;
496 close( fd_out );
498 close( fd_in );
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),
517 get_req_data(req) );
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) );
527 req->read = res;