2 * This file is part of DisOrder.
3 * Copyright (C) 2006 Richard Kettlewell
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.
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.
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
21 #include "disobedience.h"
23 /* GSource subclass for disorder_eclient */
24 struct eclient_source {
26 disorder_eclient *client;
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,
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 */
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;
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;
58 D(("gtkclient_dispatch"));
59 revents = esource->pollfd.revents & esource->pollfd.events;
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 */
70 /* Table of callbacks for GSource subclass */
71 static const GSourceFuncs sourcefuncs = {
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;
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;
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);
109 /* Report a communication-level error. It will be automatically retried. */
110 static void gtkclient_comms_error(void attribute((unused)) *u,
112 D(("gtkclient_comms_error %s", msg));
113 /* Control buttons might have become unusable */
115 gtk_label_set_text(GTK_LABEL(report_label), msg);
118 /* Report a protocol error. It will not be retried. We offer a callback to
119 * the submitter of the original command and if none is supplied we pop up an
121 static void gtkclient_protocol_error(void attribute((unused)) *u,
125 struct callbackdata *cbd = v;
127 D(("gtkclient_protocol_error %s", msg));
128 if(cbd && cbd->onerror)
129 cbd->onerror(cbd, code, msg);
131 popup_protocol_error(code, msg);
134 static void gtkclient_report(void attribute((unused)) *u,
137 /* We're idle - clear the report line */
138 gtk_label_set_text(GTK_LABEL(report_label), "");
141 void popup_protocol_error(int attribute((unused)) code,
143 gtk_label_set_text(GTK_LABEL(report_label), msg);
147 /* Table of eclient callbacks */
148 static const disorder_eclient_callbacks gtkclient_callbacks = {
149 gtkclient_comms_error,
150 gtkclient_protocol_error,
155 /* Create an eclient using the GLib main loop */
156 disorder_eclient *gtkclient(void) {
158 struct eclient_source *esource;
161 source = g_source_new((GSourceFuncs *)&sourcefuncs,
162 sizeof (struct eclient_source));
163 esource = (struct eclient_source *)source;
164 esource->pollfd.fd = -1;
165 esource->client = disorder_eclient_new(>kclient_callbacks, source);
166 if(!esource->client) {
167 g_source_destroy(source);
170 g_source_attach(source, 0);
171 return esource->client;