riched20: The background colour is set using \highlight.
[wine.git] / server / serial.c
blobfb29288510606169bcc18ccee5d2d1f38c61c55c
1 /*
2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000,2001 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <unistd.h>
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39 #ifdef HAVE_TERMIOS_H
40 #include <termios.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
49 #include "ntstatus.h"
50 #define WIN32_NO_STATUS
51 #include "windef.h"
52 #include "winternl.h"
53 #include "winioctl.h"
54 #include "ddk/ntddser.h"
56 #include "file.h"
57 #include "handle.h"
58 #include "thread.h"
59 #include "request.h"
61 static void serial_dump( struct object *obj, int verbose );
62 static struct fd *serial_get_fd( struct object *obj );
63 static void serial_destroy(struct object *obj);
65 static enum server_fd_type serial_get_fd_type( struct fd *fd );
66 static obj_handle_t serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
67 static void serial_queue_async( struct fd *fd, struct async *async, int type, int count );
68 static void serial_reselect_async( struct fd *fd, struct async_queue *queue );
70 struct serial
72 struct object obj;
73 struct fd *fd;
75 struct timeout_user *read_timer;
76 SERIAL_TIMEOUTS timeouts;
77 unsigned int eventmask;
78 unsigned int generation; /* event mask change counter */
79 unsigned int pending_write : 1;
80 unsigned int pending_wait : 1;
82 struct termios original;
84 /* FIXME: add dcb, comm status, handler module, sharing */
87 static const struct object_ops serial_ops =
89 sizeof(struct serial), /* size */
90 serial_dump, /* dump */
91 no_get_type, /* get_type */
92 add_queue, /* add_queue */
93 remove_queue, /* remove_queue */
94 default_fd_signaled, /* signaled */
95 no_satisfied, /* satisfied */
96 no_signal, /* signal */
97 serial_get_fd, /* get_fd */
98 default_fd_map_access, /* map_access */
99 default_get_sd, /* get_sd */
100 default_set_sd, /* set_sd */
101 no_lookup_name, /* lookup_name */
102 no_link_name, /* link_name */
103 NULL, /* unlink_name */
104 no_open_file, /* open_file */
105 fd_close_handle, /* close_handle */
106 serial_destroy /* destroy */
109 static const struct fd_ops serial_fd_ops =
111 default_fd_get_poll_events, /* get_poll_events */
112 default_poll_event, /* poll_event */
113 serial_get_fd_type, /* get_fd_type */
114 no_fd_read, /* read */
115 no_fd_write, /* write */
116 no_fd_flush, /* flush */
117 serial_ioctl, /* ioctl */
118 serial_queue_async, /* queue_async */
119 serial_reselect_async /* reselect_async */
122 /* check if the given fd is a serial port */
123 int is_serial_fd( struct fd *fd )
125 struct termios tios;
127 return !tcgetattr( get_unix_fd(fd), &tios );
130 /* create a serial object for a given fd */
131 struct object *create_serial( struct fd *fd )
133 struct serial *serial;
135 if (!(serial = alloc_object( &serial_ops ))) return NULL;
137 serial->read_timer = NULL;
138 serial->eventmask = 0;
139 serial->generation = 0;
140 serial->pending_write = 0;
141 serial->pending_wait = 0;
142 memset( &serial->timeouts, 0, sizeof(serial->timeouts) );
143 serial->fd = (struct fd *)grab_object( fd );
144 set_fd_user( fd, &serial_fd_ops, &serial->obj );
145 return &serial->obj;
148 static struct fd *serial_get_fd( struct object *obj )
150 struct serial *serial = (struct serial *)obj;
151 return (struct fd *)grab_object( serial->fd );
154 static void serial_destroy( struct object *obj)
156 struct serial *serial = (struct serial *)obj;
157 if (serial->read_timer) remove_timeout_user( serial->read_timer );
158 release_object( serial->fd );
161 static void serial_dump( struct object *obj, int verbose )
163 struct serial *serial = (struct serial *)obj;
164 assert( obj->ops == &serial_ops );
165 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
168 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
170 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
173 static enum server_fd_type serial_get_fd_type( struct fd *fd )
175 return FD_TYPE_SERIAL;
178 static obj_handle_t serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
180 struct serial *serial = get_fd_user( fd );
182 switch (code)
184 case IOCTL_SERIAL_GET_TIMEOUTS:
185 if (get_reply_max_size() >= sizeof(serial->timeouts))
186 set_reply_data( &serial->timeouts, sizeof(serial->timeouts ));
187 else
188 set_error( STATUS_BUFFER_TOO_SMALL );
189 return 0;
191 case IOCTL_SERIAL_SET_TIMEOUTS:
192 if (get_req_data_size() >= sizeof(serial->timeouts))
193 memcpy( &serial->timeouts, get_req_data(), sizeof(serial->timeouts) );
194 else
195 set_error( STATUS_BUFFER_TOO_SMALL );
196 return 0;
198 case IOCTL_SERIAL_GET_WAIT_MASK:
199 if (get_reply_max_size() >= sizeof(serial->eventmask))
200 set_reply_data( &serial->eventmask, sizeof(serial->eventmask) );
201 else
202 set_error( STATUS_BUFFER_TOO_SMALL );
203 return 0;
205 case IOCTL_SERIAL_SET_WAIT_MASK:
206 if (get_req_data_size() >= sizeof(serial->eventmask))
208 serial->eventmask = *(unsigned int *)get_req_data();
209 serial->generation++;
210 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
212 else set_error( STATUS_BUFFER_TOO_SMALL );
213 return 0;
215 default:
216 set_error( STATUS_NOT_SUPPORTED );
217 return 0;
221 static void serial_queue_async( struct fd *fd, struct async *async, int type, int count )
223 struct serial *serial = get_fd_user( fd );
224 timeout_t timeout = 0;
226 assert(serial->obj.ops == &serial_ops);
228 switch (type)
230 case ASYNC_TYPE_READ:
231 timeout = serial->timeouts.ReadTotalTimeoutConstant +
232 (timeout_t)serial->timeouts.ReadTotalTimeoutMultiplier * count;
233 break;
234 case ASYNC_TYPE_WRITE:
235 timeout = serial->timeouts.WriteTotalTimeoutConstant +
236 (timeout_t)serial->timeouts.WriteTotalTimeoutMultiplier * count;
237 break;
240 if (fd_queue_async( fd, async, type ))
242 if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
243 set_error( STATUS_PENDING );
247 static void serial_read_timeout( void *arg )
249 struct serial *serial = arg;
251 serial->read_timer = NULL;
252 fd_async_wake_up( serial->fd, ASYNC_TYPE_READ, STATUS_TIMEOUT );
255 static void serial_reselect_async( struct fd *fd, struct async_queue *queue )
257 struct serial *serial = get_fd_user( fd );
259 if (serial->read_timer)
261 if (!(default_fd_get_poll_events( fd ) & POLLIN))
263 remove_timeout_user( serial->read_timer );
264 serial->read_timer = NULL;
267 else if (serial->timeouts.ReadIntervalTimeout && (default_fd_get_poll_events( fd ) & POLLIN))
269 serial->read_timer = add_timeout_user( (timeout_t)serial->timeouts.ReadIntervalTimeout * -10000,
270 serial_read_timeout, serial );
272 default_fd_reselect_async( fd, queue );
275 DECL_HANDLER(get_serial_info)
277 struct serial *serial;
279 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
281 if (req->flags & SERIALINFO_PENDING_WAIT)
283 if (serial->pending_wait)
285 release_object( serial );
286 set_error( STATUS_INVALID_PARAMETER );
287 return;
289 serial->pending_wait = 1;
292 /* event mask */
293 reply->eventmask = serial->eventmask;
294 reply->cookie = serial->generation;
296 /* pending write */
297 reply->pending_write = serial->pending_write;
298 if (req->flags & SERIALINFO_PENDING_WRITE)
299 serial->pending_write = 0;
301 release_object( serial );
305 DECL_HANDLER(set_serial_info)
307 struct serial *serial;
309 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
311 if (req->flags & SERIALINFO_PENDING_WAIT)
313 if (!serial->pending_wait)
315 release_object( serial );
316 set_error( STATUS_INVALID_PARAMETER );
317 return;
319 serial->pending_wait = 0;
322 /* pending write */
323 if (req->flags & SERIALINFO_PENDING_WRITE)
324 serial->pending_write = 1;
326 release_object( serial );