grey out edit->track properties when not connected
[disorder] / disobedience / client.c
1 /*
2 * This file is part of DisOrder.
3 * Copyright (C) 2006 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21 #include "disobedience.h"
22
23 /* GSource subclass for disorder_eclient */
24 struct eclient_source {
25 GSource gsource;
26 disorder_eclient *client;
27 time_t last_poll;
28 GPollFD pollfd;
29 };
30
31 /* Called before FDs are polled to choose a timeout. We ask for a 3s
32 * timeout and every 10s or so we force a dispatch. */
33 static gboolean gtkclient_prepare(GSource *source,
34 gint *timeout) {
35 const struct eclient_source *esource = (struct eclient_source *)source;
36 D(("gtkclient_prepare"));
37 if(time(0) > esource->last_poll + 10)
38 return TRUE; /* timed out */
39 *timeout = 3000/*milliseconds*/;
40 return FALSE; /* please poll */
41 }
42
43 /* Check whether we're ready to dispatch. */
44 static gboolean gtkclient_check(GSource *source) {
45 const struct eclient_source *esource = (struct eclient_source *)source;
46 D(("gtkclient_check fd=%d events=%x revents=%x",
47 esource->pollfd.fd, esource->pollfd.events, esource->pollfd.revents));
48 return esource->pollfd.fd != -1 && esource->pollfd.revents != 0;
49 }
50
51 /* Dispatch */
52 static gboolean gtkclient_dispatch(GSource *source,
53 GSourceFunc attribute((unused)) callback,
54 gpointer attribute((unused)) user_data) {
55 struct eclient_source *esource = (struct eclient_source *)source;
56 unsigned revents, mode;
57
58 D(("gtkclient_dispatch"));
59 revents = esource->pollfd.revents & esource->pollfd.events;
60 mode = 0;
61 if(revents & (G_IO_IN|G_IO_HUP|G_IO_ERR))
62 mode |= DISORDER_POLL_READ;
63 if(revents & (G_IO_OUT|G_IO_HUP|G_IO_ERR))
64 mode |= DISORDER_POLL_WRITE;
65 time(&esource->last_poll);
66 disorder_eclient_polled(esource->client, mode);
67 return TRUE; /* ??? not documented */
68 }
69
70 /* Table of callbacks for GSource subclass */
71 static const GSourceFuncs sourcefuncs = {
72 gtkclient_prepare,
73 gtkclient_check,
74 gtkclient_dispatch,
75 0,
76 0,
77 0,
78 };
79
80 /* Tell the mainloop what we need */
81 static void gtkclient_poll(void *u,
82 disorder_eclient attribute((unused)) *c,
83 int fd, unsigned mode) {
84 struct eclient_source *esource = u;
85 GSource *source = u;
86
87 D(("gtkclient_poll fd=%d mode=%x", fd, mode));
88 /* deconfigure the source if currently configured */
89 if(esource->pollfd.fd != -1) {
90 D(("calling g_source_remove_poll"));
91 g_source_remove_poll(source, &esource->pollfd);
92 esource->pollfd.fd = -1;
93 esource->pollfd.events = 0;
94 }
95 /* install new settings */
96 if(fd != -1 && mode) {
97 esource->pollfd.fd = fd;
98 esource->pollfd.events = 0;
99 if(mode & DISORDER_POLL_READ)
100 esource->pollfd.events |= G_IO_IN | G_IO_HUP | G_IO_ERR;
101 if(mode & DISORDER_POLL_WRITE)
102 esource->pollfd.events |= G_IO_OUT | G_IO_ERR;
103 /* reconfigure the source */
104 D(("calling g_source_add_poll"));
105 g_source_add_poll(source, &esource->pollfd);
106 }
107 }
108
109 /* Report a communication-level error. It will be automatically retried. */
110 static void gtkclient_comms_error(void attribute((unused)) *u,
111 const char *msg) {
112 D(("gtkclient_comms_error %s", msg));
113 /* Control buttons might have become unusable */
114 control_update();
115 menu_update(-1);
116 gtk_label_set_text(GTK_LABEL(report_label), msg);
117 }
118
119 /* Report a protocol error. It will not be retried. We offer a callback to
120 * the submitter of the original command and if none is supplied we pop up an
121 * error box. */
122 static void gtkclient_protocol_error(void attribute((unused)) *u,
123 void *v,
124 int code,
125 const char *msg) {
126 struct callbackdata *cbd = v;
127
128 D(("gtkclient_protocol_error %s", msg));
129 if(cbd && cbd->onerror)
130 cbd->onerror(cbd, code, msg);
131 else
132 popup_protocol_error(code, msg);
133 }
134
135 static void gtkclient_report(void attribute((unused)) *u,
136 const char *msg) {
137 if(!msg)
138 /* We're idle - clear the report line */
139 gtk_label_set_text(GTK_LABEL(report_label), "");
140 control_update();
141 menu_update(-1);
142 }
143
144 void popup_protocol_error(int attribute((unused)) code,
145 const char *msg) {
146 gtk_label_set_text(GTK_LABEL(report_label), msg);
147 popup_error(msg);
148 }
149
150 /* Table of eclient callbacks */
151 static const disorder_eclient_callbacks gtkclient_callbacks = {
152 gtkclient_comms_error,
153 gtkclient_protocol_error,
154 gtkclient_poll,
155 gtkclient_report
156 };
157
158 /* Create an eclient using the GLib main loop */
159 disorder_eclient *gtkclient(void) {
160 GSource *source;
161 struct eclient_source *esource;
162
163 D(("gtkclient"));
164 source = g_source_new((GSourceFuncs *)&sourcefuncs,
165 sizeof (struct eclient_source));
166 esource = (struct eclient_source *)source;
167 esource->pollfd.fd = -1;
168 esource->client = disorder_eclient_new(&gtkclient_callbacks, source);
169 if(!esource->client) {
170 g_source_destroy(source);
171 return 0;
172 }
173 g_source_attach(source, 0);
174 return esource->client;
175 }
176
177 /*
178 Local Variables:
179 c-basic-offset:2
180 comment-column:40
181 fill-column:79
182 indent-tabs-mode:nil
183 End:
184 */