Add non blocking virtio port code
[vd_agent/hramrach.git] / vdagent.c
blob1eb6c3a0529559e6771adf608ea9ad0daf33e144
1 /* vdagent.c xorg-client to vdagentd (daemon).
3 Copyright 2010 Red Hat, Inc.
5 Red Hat Authors:
6 Hans de Goede <hdegoede@redhat.com>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program 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
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <sys/select.h>
29 #include "udscs.h"
30 #include "vdagentd-proto.h"
31 #include "vdagent-x11.h"
33 int daemon_read_complete(struct udscs_connection *conn,
34 struct udscs_message_header *header, const uint8_t *data)
36 return 0;
39 static void usage(FILE *fp)
41 fprintf(fp,
42 "vdagent -- spice agent xorg client\n"
43 "options:\n"
44 " -h print this text\n"
45 " -d print debug messages\n");
48 int main(int argc, char *argv[])
50 struct udscs_connection *client;
51 struct vdagent_x11 *x11;
52 fd_set readfds, writefds;
53 int c, n, nfds, x11_fd;
54 int verbose = 0;
56 for (;;) {
57 if (-1 == (c = getopt(argc, argv, "dh")))
58 break;
59 switch (c) {
60 case 'd':
61 verbose++;
62 break;
63 case 'h':
64 usage(stdout);
65 exit(0);
66 default:
67 usage(stderr);
68 exit(1);
72 client = udscs_connect(VDAGENTD_SOCKET, daemon_read_complete, NULL);
73 if (!client)
74 exit(1);
76 x11 = vdagent_x11_create(client, verbose);
77 if (!x11) {
78 udscs_destroy_connection(&client);
79 exit(1);
82 while (client) {
83 FD_ZERO(&readfds);
84 FD_ZERO(&writefds);
86 nfds = udscs_client_fill_fds(client, &readfds, &writefds);
87 x11_fd = vdagent_x11_get_fd(x11);
88 FD_SET(x11_fd, &readfds);
89 if (x11_fd >= nfds)
90 nfds = x11_fd + 1;
92 n = select(nfds, &readfds, &writefds, NULL, NULL);
93 if (n == -1) {
94 if (errno == EINTR)
95 continue;
96 perror("select");
97 exit(1);
100 if (FD_SET(x11_fd, &readfds))
101 vdagent_x11_do_read(x11);
102 udscs_client_handle_fds(&client, &readfds, &writefds);
105 vdagent_x11_destroy(x11);
107 return 0;