]> Dogcows Code - chaz/openbox/blob - openbox/ping.c
can tell when a window that was "closed" has stopped responding now
[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 "mainloop.h"
25 #include "openbox.h"
26
27 typedef struct _ObPingTarget
28 {
29 ObClient *client;
30 ObPingEventHandler h;
31 Time sent;
32 gint waiting;
33 } ObPingTarget;
34
35 static GSList *ping_targets = NULL;
36 static gboolean active = FALSE;
37
38 #define PING_TIMEOUT (G_USEC_PER_SEC * 1)
39 /*! Warn the user after this many PING_TIMEOUT intervals */
40 #define PING_TIMEOUT_WARN 3
41
42 static void ping_send(ObPingTarget *t);
43 static void ping_end(ObClient *client, gpointer data);
44 static gboolean ping_timeout(gpointer data);
45
46 void ping_start(struct _ObClient *client, ObPingEventHandler h)
47 {
48 GSList *it;
49 ObPingTarget *t;
50
51 /* make sure we're not already pinging it */
52 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
53 t = it->data;
54 if (t->client == client) return;
55 }
56
57 t = g_new(ObPingTarget, 1);
58 t->client = client;
59 t->h = h;
60 t->waiting = 1; /* first wait for a reply */
61
62 ping_send(t);
63 ping_targets = g_slist_prepend(ping_targets, t);
64 ob_main_loop_timeout_add(ob_main_loop, PING_TIMEOUT, ping_timeout,
65 t, NULL, NULL);
66
67 if (!active) {
68 active = TRUE;
69 /* listen for the client to disappear */
70 client_add_destroy_notify(ping_end, NULL);
71 }
72 }
73
74 void ping_stop(struct _ObClient *c)
75 {
76 ping_end(c, NULL);
77 }
78
79 void ping_got_pong(Time timestamp)
80 {
81 GSList *it;
82 ObPingTarget *t;
83
84 /* make sure we're not already pinging it */
85 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
86 t = it->data;
87 if (t->sent == timestamp) {
88 ob_debug("Got PONG with timestamp %lu\n", timestamp);
89 if (t->waiting > PING_TIMEOUT_WARN) {
90 /* we had notified that they weren't responding, so now we
91 need to notify that they are again */
92 t->h(t->client, FALSE);
93 }
94 t->waiting = 0; /* not waiting for a reply anymore */
95 break;
96 }
97 }
98
99 if (it == NULL)
100 ob_debug("Got PONG with timestamp %lu but not waiting for one\n",
101 timestamp);
102 }
103
104 static void ping_send(ObPingTarget *t)
105 {
106 t->sent = event_get_server_time();
107 ob_debug("PINGing client 0x%x at %lu\n", t->client->window, t->sent);
108 PROP_MSG_TO(t->client->window, t->client->window, wm_protocols,
109 prop_atoms.net_wm_ping, t->sent, t->client->window, 0, 0,
110 NoEventMask);
111 }
112
113 static gboolean ping_timeout(gpointer data)
114 {
115 ObPingTarget *t = data;
116
117 if (t->waiting == 0) { /* got a reply already */
118 /* send another ping to make sure it's still alive */
119 ping_send(t);
120 }
121
122 if (t->waiting == PING_TIMEOUT_WARN)
123 t->h(t->client, TRUE); /* notify that the client isn't responding */
124
125 ++t->waiting;
126
127 return TRUE; /* repeat */
128 }
129
130 static void ping_end(ObClient *client, gpointer data)
131 {
132 GSList *it;
133 ObPingTarget *t;
134
135 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
136 t = it->data;
137 if (t->client == client) {
138 ping_targets = g_slist_remove_link(ping_targets, it);
139 ob_main_loop_timeout_remove_data(ob_main_loop, ping_timeout, t,
140 FALSE);
141 g_free(t);
142 break;
143 }
144 }
145
146 /* stop listening if we're not waiting for any more pings */
147 if (!ping_targets) {
148 active = TRUE;
149 client_remove_destroy_notify(ping_end);
150 }
151 }
This page took 0.040347 seconds and 5 git commands to generate.