Measure the text more reasonably. This handles intercharacter spacing
[wine/wine-kai.git] / server / serial.c
blob59118768ff243e1fbfb0aed0649822affeb18b81
1 /*
2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000,2001 Mike McCormack
7 */
9 #include "config.h"
11 #include <assert.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #ifdef HAVE_SYS_ERRNO_H
18 #include <sys/errno.h>
19 #endif
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <utime.h>
25 #include <termios.h>
26 #include <sys/ioctl.h>
28 #include "winerror.h"
29 #include "winbase.h"
31 #include "handle.h"
32 #include "thread.h"
33 #include "request.h"
34 #include "async.h"
36 static void serial_dump( struct object *obj, int verbose );
37 static int serial_get_fd( struct object *obj );
38 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
39 static int serial_get_poll_events( struct object *obj );
40 static struct async_queue * serial_queue_async(struct object *obj, struct async* async, int type, int count);
41 static void destroy_serial(struct object *obj);
42 static void serial_poll_event( struct object *obj, int event );
44 struct serial
46 struct object obj;
47 unsigned int access;
48 unsigned int attrib;
50 /* timeout values */
51 unsigned int readinterval;
52 unsigned int readconst;
53 unsigned int readmult;
54 unsigned int writeconst;
55 unsigned int writemult;
57 unsigned int eventmask;
58 unsigned int commerror;
60 struct termios original;
62 struct async_queue read_q;
63 struct async_queue write_q;
64 struct async_queue wait_q;
66 /* FIXME: add dcb, comm status, handler module, sharing */
69 static const struct object_ops serial_ops =
71 sizeof(struct serial), /* size */
72 serial_dump, /* dump */
73 default_poll_add_queue, /* add_queue */
74 default_poll_remove_queue, /* remove_queue */
75 default_poll_signaled, /* signaled */
76 no_satisfied, /* satisfied */
77 serial_get_poll_events, /* get_poll_events */
78 serial_poll_event, /* poll_event */
79 serial_get_fd, /* get_fd */
80 no_flush, /* flush */
81 serial_get_info, /* get_file_info */
82 serial_queue_async, /* queue_async */
83 destroy_serial /* destroy */
86 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
88 struct serial *serial;
89 struct termios tios;
90 int fd, flags = 0;
91 char *name;
93 if (!(name = mem_alloc( len + 1 ))) return NULL;
94 memcpy( name, nameptr, len );
95 name[len] = 0;
97 switch(access & (GENERIC_READ | GENERIC_WRITE))
99 case GENERIC_READ: flags |= O_RDONLY; break;
100 case GENERIC_WRITE: flags |= O_WRONLY; break;
101 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
102 default: break;
105 flags |= O_NONBLOCK;
107 fd = open( name, flags );
108 free( name );
109 if (fd < 0)
111 file_set_error();
112 return NULL;
115 /* check its really a serial port */
116 if (tcgetattr(fd,&tios))
118 file_set_error();
119 close( fd );
120 return NULL;
123 /* set the fd back to blocking if necessary */
124 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
125 if(0>fcntl(fd, F_SETFL, 0))
126 perror("fcntl");
128 if ((serial = alloc_object( &serial_ops, fd )))
130 serial->attrib = attributes;
131 serial->access = access;
132 serial->readinterval = 0;
133 serial->readmult = 0;
134 serial->readconst = 0;
135 serial->writemult = 0;
136 serial->writeconst = 0;
137 serial->eventmask = 0;
138 serial->commerror = 0;
139 init_async_queue(&serial->read_q);
140 init_async_queue(&serial->write_q);
141 init_async_queue(&serial->wait_q);
143 return serial;
146 static void destroy_serial( struct object *obj)
148 struct serial *serial = (struct serial *)obj;
150 destroy_async_queue(&serial->read_q);
151 destroy_async_queue(&serial->write_q);
152 destroy_async_queue(&serial->wait_q);
155 static void serial_dump( struct object *obj, int verbose )
157 struct serial *serial = (struct serial *)obj;
158 assert( obj->ops == &serial_ops );
159 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
162 struct serial *get_serial_obj( struct process *process, handle_t handle, unsigned int access )
164 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
167 static int serial_get_poll_events( struct object *obj )
169 struct serial *serial = (struct serial *)obj;
170 int events = 0;
171 assert( obj->ops == &serial_ops );
173 if(IS_READY(serial->read_q))
174 events |= POLLIN;
175 if(IS_READY(serial->write_q))
176 events |= POLLOUT;
177 if(IS_READY(serial->wait_q))
178 events |= POLLIN;
180 /* fprintf(stderr,"poll events are %04x\n",events); */
182 return events;
185 static int serial_get_fd( struct object *obj )
187 struct serial *serial = (struct serial *)obj;
188 assert( obj->ops == &serial_ops );
189 return serial->obj.fd;
192 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
194 struct serial *serial = (struct serial *) obj;
195 assert( obj->ops == &serial_ops );
197 if (reply)
199 reply->type = FILE_TYPE_CHAR;
200 reply->attr = 0;
201 reply->access_time = 0;
202 reply->write_time = 0;
203 reply->size_high = 0;
204 reply->size_low = 0;
205 reply->links = 0;
206 reply->index_high = 0;
207 reply->index_low = 0;
208 reply->serial = 0;
211 *flags = 0;
212 if(serial->attrib & FILE_FLAG_OVERLAPPED)
213 *flags |= FD_FLAG_OVERLAPPED;
214 else if(!((serial->readinterval == MAXDWORD) &&
215 (serial->readmult == 0) && (serial->readconst == 0)) )
216 *flags |= FD_FLAG_TIMEOUT;
218 return FD_TYPE_DEFAULT;
221 static void serial_poll_event(struct object *obj, int event)
223 struct serial *serial = (struct serial *)obj;
225 /* fprintf(stderr,"Poll event %02x\n",event); */
227 if(IS_READY(serial->read_q) && (POLLIN & event) )
228 async_notify(serial->read_q.head,STATUS_ALERTED);
230 if(IS_READY(serial->write_q) && (POLLOUT & event) )
231 async_notify(serial->write_q.head,STATUS_ALERTED);
233 if(IS_READY(serial->wait_q) && (POLLIN & event) )
234 async_notify(serial->wait_q.head,STATUS_ALERTED);
236 set_select_events(obj,obj->ops->get_poll_events(obj));
240 * This function is an abuse of overloading that deserves some explanation.
242 * It has three purposes:
244 * 1. get the queue for a type of async operation
245 * 2. requeue an async operation
246 * 3. queue a new async operation
248 * It is overloaded so that these three functions only take one function pointer
249 * in the object operations list.
251 * In all cases, it returns the async queue.
253 static struct async_queue *serial_queue_async(struct object *obj, struct async *async, int type, int count)
255 struct serial *serial = (struct serial *)obj;
256 struct async_queue *q;
257 int timeout;
259 assert(obj->ops == &serial_ops);
261 switch(type)
263 case ASYNC_TYPE_READ:
264 q = &serial->read_q;
265 timeout = serial->readconst + serial->readmult*count;
266 break;
267 case ASYNC_TYPE_WAIT:
268 q = &serial->wait_q;
269 timeout = 0;
270 break;
271 case ASYNC_TYPE_WRITE:
272 q = &serial->write_q;
273 timeout = serial->writeconst + serial->writemult*count;
274 break;
275 default:
276 set_error(STATUS_INVALID_PARAMETER);
277 return NULL;
280 if(async)
282 if(!async->q)
284 async_add_timeout(async,timeout);
285 async_insert(q, async);
289 return q;
292 /* create a serial */
293 DECL_HANDLER(create_serial)
295 struct serial *serial;
297 reply->handle = 0;
298 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
300 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
301 release_object( serial );
305 DECL_HANDLER(get_serial_info)
307 struct serial *serial;
309 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
311 /* timeouts */
312 reply->readinterval = serial->readinterval;
313 reply->readconst = serial->readconst;
314 reply->readmult = serial->readmult;
315 reply->writeconst = serial->writeconst;
316 reply->writemult = serial->writemult;
318 /* event mask */
319 reply->eventmask = serial->eventmask;
321 /* comm port error status */
322 reply->commerror = serial->commerror;
324 release_object( serial );
328 DECL_HANDLER(set_serial_info)
330 struct serial *serial;
332 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
334 /* timeouts */
335 if(req->flags & SERIALINFO_SET_TIMEOUTS)
337 serial->readinterval = req->readinterval;
338 serial->readconst = req->readconst;
339 serial->readmult = req->readmult;
340 serial->writeconst = req->writeconst;
341 serial->writemult = req->writemult;
344 /* event mask */
345 if(req->flags & SERIALINFO_SET_MASK)
347 serial->eventmask = req->eventmask;
350 /* comm port error status */
351 if(req->flags & SERIALINFO_SET_ERROR)
353 serial->commerror = req->commerror;
356 release_object( serial );