]> Dogcows Code - chaz/openbox/blob - openbox/ping.c
use unique IDs for pings rather than a timestamp. avoids duplicates.
[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 guint32 id;
33 gint waiting;
34 } ObPingTarget;
35
36 static GSList *ping_targets = NULL;
37 static gboolean active = FALSE;
38 static guint32 ping_next_id = 1;
39
40 #define PING_TIMEOUT (G_USEC_PER_SEC * 3)
41 /*! Warn the user after this many PING_TIMEOUT intervals */
42 #define PING_TIMEOUT_WARN 3
43
44 static void ping_send(ObPingTarget *t);
45 static void ping_end(ObClient *client, gpointer data);
46 static gboolean ping_timeout(gpointer data);
47
48 void ping_start(struct _ObClient *client, ObPingEventHandler h)
49 {
50 GSList *it;
51 ObPingTarget *t;
52
53 g_assert(client->ping == TRUE);
54
55 /* make sure we're not already pinging it */
56 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
57 t = it->data;
58 if (t->client == client) return;
59 }
60
61 t = g_new(ObPingTarget, 1);
62 t->client = client;
63 t->h = h;
64 t->waiting = 1; /* first wait for a reply */
65
66 ping_send(t);
67 ping_targets = g_slist_prepend(ping_targets, t);
68 ob_main_loop_timeout_add(ob_main_loop, PING_TIMEOUT, ping_timeout,
69 t, g_direct_equal, NULL);
70
71 if (!active) {
72 active = TRUE;
73 /* listen for the client to disappear */
74 client_add_destroy_notify(ping_end, NULL);
75 }
76 }
77
78 void ping_stop(struct _ObClient *c)
79 {
80 ping_end(c, NULL);
81 }
82
83 void ping_got_pong(guint32 id)
84 {
85 GSList *it;
86 ObPingTarget *t;
87
88 /* make sure we're not already pinging it */
89 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
90 t = it->data;
91 if (t->id == id) {
92 /*g_print("-PONG: '%s' (id %u)\n", t->client->title, t->id);*/
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 id %u but not waiting for one\n", id);
105 }
106
107 static void ping_send(ObPingTarget *t)
108 {
109 t->id = ping_next_id++;
110 /*g_print("+PING: '%s' (id %u)\n", t->client->title, t->id);*/
111 PROP_MSG_TO(t->client->window, t->client->window, wm_protocols,
112 prop_atoms.net_wm_ping, t->id, t->client->window, 0, 0,
113 NoEventMask);
114 }
115
116 static gboolean ping_timeout(gpointer data)
117 {
118 ObPingTarget *t = data;
119
120 if (t->waiting == 0) { /* got a reply already */
121 /* send another ping to make sure it's still alive */
122 ping_send(t);
123 }
124
125 if (t->waiting == PING_TIMEOUT_WARN)
126 t->h(t->client, TRUE); /* notify that the client isn't responding */
127
128 ++t->waiting;
129
130 return TRUE; /* repeat */
131 }
132
133 static void ping_end(ObClient *client, gpointer data)
134 {
135 GSList *it;
136 ObPingTarget *t;
137
138 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
139 t = it->data;
140 if (t->client == client) {
141 ping_targets = g_slist_remove_link(ping_targets, it);
142 ob_main_loop_timeout_remove_data(ob_main_loop, ping_timeout, t,
143 FALSE);
144 g_free(t);
145 break;
146 }
147 }
148
149 /* stop listening if we're not waiting for any more pings */
150 if (!ping_targets) {
151 active = FALSE;
152 client_remove_destroy_notify(ping_end);
153 }
154 }
This page took 0.043562 seconds and 5 git commands to generate.