]> Dogcows Code - chaz/openbox/blob - openbox/ping.c
Various fixes for sparse warnings.
[chaz/openbox] / openbox / ping.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 client.h for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2008 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "ping.h"
21 #include "client.h"
22 #include "prop.h"
23 #include "event.h"
24 #include "debug.h"
25 #include "mainloop.h"
26 #include "openbox.h"
27
28 typedef struct _ObPingTarget
29 {
30 ObClient *client;
31 ObPingEventHandler h;
32 Time sent;
33 gint waiting;
34 } ObPingTarget;
35
36 static GSList *ping_targets = NULL;
37 static gboolean active = FALSE;
38
39 #define PING_TIMEOUT (G_USEC_PER_SEC * 3)
40 /*! Warn the user after this many PING_TIMEOUT intervals */
41 #define PING_TIMEOUT_WARN 3
42
43 static void ping_send(ObPingTarget *t);
44 static void ping_end(ObClient *client, gpointer data);
45 static gboolean ping_timeout(gpointer data);
46
47 void ping_start(struct _ObClient *client, ObPingEventHandler h)
48 {
49 GSList *it;
50 ObPingTarget *t;
51
52 g_assert(client->ping == TRUE);
53
54 /* make sure we're not already pinging it */
55 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
56 t = it->data;
57 if (t->client == client) return;
58 }
59
60 t = g_new(ObPingTarget, 1);
61 t->client = client;
62 t->h = h;
63 t->waiting = 1; /* first wait for a reply */
64
65 ping_send(t);
66 ping_targets = g_slist_prepend(ping_targets, t);
67 ob_main_loop_timeout_add(ob_main_loop, PING_TIMEOUT, ping_timeout,
68 t, g_direct_equal, NULL);
69
70 if (!active) {
71 active = TRUE;
72 /* listen for the client to disappear */
73 client_add_destroy_notify(ping_end, NULL);
74 }
75 }
76
77 void ping_stop(struct _ObClient *c)
78 {
79 ping_end(c, NULL);
80 }
81
82 void ping_got_pong(Time timestamp)
83 {
84 GSList *it;
85 ObPingTarget *t;
86
87 /* make sure we're not already pinging it */
88 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
89 t = it->data;
90 if (t->sent == timestamp) {
91 /*ob_debug("PONG: '%s' (timestamp %lu)\n", t->client->title,
92 t->sent);*/
93 if (t->waiting > PING_TIMEOUT_WARN) {
94 /* we had notified that they weren't responding, so now we
95 need to notify that they are again */
96 t->h(t->client, FALSE);
97 }
98 t->waiting = 0; /* not waiting for a reply anymore */
99 break;
100 }
101 }
102
103 if (it == NULL)
104 ob_debug("Got PONG with timestamp %lu but not waiting for one\n",
105 timestamp);
106 }
107
108 static void ping_send(ObPingTarget *t)
109 {
110 t->sent = event_get_server_time();
111 /*ob_debug("PING: '%s' (timestamp %lu)\n", t->client->title, t->sent);*/
112 PROP_MSG_TO(t->client->window, t->client->window, wm_protocols,
113 prop_atoms.net_wm_ping, t->sent, t->client->window, 0, 0,
114 NoEventMask);
115 }
116
117 static gboolean ping_timeout(gpointer data)
118 {
119 ObPingTarget *t = data;
120
121 if (t->waiting == 0) { /* got a reply already */
122 /* send another ping to make sure it's still alive */
123 ping_send(t);
124 }
125
126 if (t->waiting == PING_TIMEOUT_WARN)
127 t->h(t->client, TRUE); /* notify that the client isn't responding */
128
129 ++t->waiting;
130
131 return TRUE; /* repeat */
132 }
133
134 static void ping_end(ObClient *client, gpointer data)
135 {
136 GSList *it;
137 ObPingTarget *t;
138
139 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
140 t = it->data;
141 if (t->client == client) {
142 ping_targets = g_slist_remove_link(ping_targets, it);
143 ob_main_loop_timeout_remove_data(ob_main_loop, ping_timeout, t,
144 FALSE);
145 g_free(t);
146 break;
147 }
148 }
149
150 /* stop listening if we're not waiting for any more pings */
151 if (!ping_targets) {
152 active = FALSE;
153 client_remove_destroy_notify(ping_end);
154 }
155 }
This page took 0.044377 seconds and 5 git commands to generate.