Fixed calculation of buffer length after switch of window text to
[wine/dcerpc.git] / server / console.c
blob5c6fad8ffbb1d1e3d2392c67a2cd4bbf1e7fd426
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 max, 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;
310 if ((count < 0) || (count > console->recnum)) count = console->recnum;
311 if (count > max) count = max;
312 memcpy( rec, console->records, count * sizeof(INPUT_RECORD) );
313 if (flush)
315 int i;
316 for (i = count; i < console->recnum; i++)
317 console->records[i-count] = console->records[i];
318 if ((console->recnum -= count) > 0)
320 INPUT_RECORD *new_rec = realloc( console->records,
321 console->recnum * sizeof(INPUT_RECORD) );
322 if (new_rec) console->records = new_rec;
324 else
326 free( console->records );
327 console->records = NULL;
330 release_object( console );
331 return count;
334 static void console_input_dump( struct object *obj, int verbose )
336 struct console_input *console = (struct console_input *)obj;
337 assert( obj->ops == &console_input_ops );
338 fprintf( stderr, "Console input fd=%d\n", console->obj.fd );
341 static int console_input_get_poll_events( struct object *obj )
343 return POLLIN;
346 static int console_input_get_read_fd( struct object *obj )
348 struct console_input *console = (struct console_input *)obj;
349 assert( obj->ops == &console_input_ops );
350 return dup( console->obj.fd );
353 static int console_get_info( struct object *obj, struct get_file_info_request *req )
355 req->type = FILE_TYPE_CHAR;
356 req->attr = 0;
357 req->access_time = 0;
358 req->write_time = 0;
359 req->size_high = 0;
360 req->size_low = 0;
361 req->links = 0;
362 req->index_high = 0;
363 req->index_low = 0;
364 req->serial = 0;
365 return 1;
368 static void console_input_destroy( struct object *obj )
370 struct console_input *console = (struct console_input *)obj;
371 assert( obj->ops == &console_input_ops );
372 if (console->output) console->output->input = NULL;
375 static void screen_buffer_dump( struct object *obj, int verbose )
377 struct screen_buffer *console = (struct screen_buffer *)obj;
378 assert( obj->ops == &screen_buffer_ops );
379 fprintf( stderr, "Console screen buffer fd=%d\n", console->obj.fd );
382 static int screen_buffer_get_poll_events( struct object *obj )
384 return POLLOUT;
387 static int screen_buffer_get_write_fd( struct object *obj )
389 struct screen_buffer *console = (struct screen_buffer *)obj;
390 assert( obj->ops == &screen_buffer_ops );
391 return dup( console->obj.fd );
394 static void screen_buffer_destroy( struct object *obj )
396 struct screen_buffer *console = (struct screen_buffer *)obj;
397 assert( obj->ops == &screen_buffer_ops );
398 if (console->input) console->input->output = NULL;
399 if (console->title) free( console->title );
402 /* allocate a console for the current process */
403 DECL_HANDLER(alloc_console)
405 int in = -1, out = -1;
407 if (!alloc_console( current->process )) goto done;
409 if ((in = alloc_handle( current->process, current->process->console_in,
410 req->access, req->inherit )) != -1)
412 if ((out = alloc_handle( current->process, current->process->console_out,
413 req->access, req->inherit )) != -1)
414 goto done; /* everything is fine */
415 close_handle( current->process, in );
416 in = -1;
418 free_console( current->process );
420 done:
421 req->handle_in = in;
422 req->handle_out = out;
425 /* free the console of the current process */
426 DECL_HANDLER(free_console)
428 free_console( current->process );
431 /* open a handle to the process console */
432 DECL_HANDLER(open_console)
434 struct object *obj= req->output ? current->process->console_out : current->process->console_in;
436 if (obj) req->handle = alloc_handle( current->process, obj, req->access, req->inherit );
437 else set_error( STATUS_ACCESS_DENIED );
440 /* set info about a console (output only) */
441 DECL_HANDLER(set_console_info)
443 size_t len = get_req_strlen( req, req->title );
444 set_console_info( req->handle, req, req->title, len );
447 /* get info about a console (output only) */
448 DECL_HANDLER(get_console_info)
450 struct screen_buffer *console;
451 req->title[0] = 0;
452 if ((console = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
453 GENERIC_READ, &screen_buffer_ops )))
455 req->cursor_size = console->cursor_size;
456 req->cursor_visible = console->cursor_visible;
457 req->pid = console->pid;
458 if (console->title) strcpy( req->title, console->title );
459 release_object( console );
463 /* set a console fd */
464 DECL_HANDLER(set_console_fd)
466 struct object *obj;
467 int fd_in, fd_out;
469 if (!(obj = get_handle_obj( current->process, req->file_handle,
470 GENERIC_READ | GENERIC_WRITE, NULL ))) return;
471 if ((fd_in = obj->ops->get_read_fd( obj )) == -1)
473 release_object( obj );
474 return;
476 fd_out = obj->ops->get_write_fd( obj );
477 release_object( obj );
478 if (fd_out != -1)
480 if (set_console_fd( req->handle, fd_in, fd_out, req->pid )) return;
481 close( fd_out );
483 close( fd_in );
486 /* get a console mode (input or output) */
487 DECL_HANDLER(get_console_mode)
489 req->mode = get_console_mode( req->handle );
492 /* set a console mode (input or output) */
493 DECL_HANDLER(set_console_mode)
495 set_console_mode( req->handle, req->mode );
498 /* add input records to a console input queue */
499 DECL_HANDLER(write_console_input)
501 int max = get_req_size( req, req + 1, sizeof(INPUT_RECORD) );
502 int count = req->count;
504 if (count > max) count = max;
505 req->written = write_console_input( req->handle, count, (INPUT_RECORD *)(req + 1) );
508 /* fetch input records from a console input queue */
509 DECL_HANDLER(read_console_input)
511 int max = get_req_size( req, req + 1, sizeof(INPUT_RECORD) );
512 req->read = read_console_input( req->handle, req->count, (INPUT_RECORD *)(req + 1),
513 max, req->flush );