]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/ping.c
use hash tables in ping.[ch] instead of a list. we're pinging every window, not just...
[mikachu/openbox.git] / 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 GHashTable *ping_targets = NULL;
37 static GHashTable *ping_ids     = NULL;
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_startup(gboolean reconfigure)
49 {
50     if (reconfigure) return;
51
52     ping_targets = g_hash_table_new(g_direct_hash, g_int_equal);
53     ping_ids = g_hash_table_new(g_direct_hash, g_int_equal);
54
55     /* listen for clients to disappear */
56     client_add_destroy_notify(ping_end, NULL);
57 }
58
59 void ping_shutdown(gboolean reconfigure)
60 {
61     if (reconfigure) return;
62
63     g_hash_table_unref(ping_targets);
64     g_hash_table_unref(ping_ids);
65
66     client_remove_destroy_notify(ping_end);
67 }
68
69 void ping_start(struct _ObClient *client, ObPingEventHandler h)
70 {
71     ObPingTarget *t;
72
73     g_assert(client->ping == TRUE);
74
75     /* make sure we're not already pinging it */
76     g_assert(g_hash_table_lookup(ping_targets, &client) == NULL);
77
78     t = g_new0(ObPingTarget, 1);
79     t->client = client;
80     t->h = h;
81
82     g_hash_table_insert(ping_targets, &t->client, t);
83
84     ob_main_loop_timeout_add(ob_main_loop, PING_TIMEOUT, ping_timeout,
85                              t, g_direct_equal, NULL);
86     /* act like we just timed out immediately, to start the pinging process
87        now instead of after the first delay */
88     ping_timeout(t);
89 }
90
91 void ping_stop(struct _ObClient *c)
92 {
93     ping_end(c, NULL);
94 }
95
96 void ping_got_pong(guint32 id)
97 {
98     ObPingTarget *t;
99
100     if ((t = g_hash_table_lookup(ping_ids, &id))) {
101         /*g_print("-PONG: '%s' (id %u)\n", t->client->title, t->id);*/
102         if (t->waiting > PING_TIMEOUT_WARN) {
103             /* we had notified that they weren't responding, so now we
104                need to notify that they are again */
105             t->h(t->client, FALSE);
106         }
107         t->waiting = 0; /* not waiting for a reply anymore */
108     }
109     else
110         ob_debug("Got PONG with id %u but not waiting for one\n", id);
111 }
112
113 static void ping_send(ObPingTarget *t)
114 {
115     /* t->id is 0 when it hasn't been assigned an id ever yet.
116        we can reuse ids when t->waiting == 0, because we won't be getting a
117        pong for that id in the future again.  that way for apps that aren't
118        timing out we don't need to remove/add them from/to the hash table */
119     if (t->id == 0 || t->waiting > 0) {
120         /* pick an id, and reinsert in the hash table with the new id */
121         if (t->id) g_hash_table_remove(ping_ids, &t->id);
122         t->id = ping_next_id;
123         if (++ping_next_id == 0) ++ping_next_id; /* skip 0 on wraparound */
124         g_hash_table_insert(ping_ids, &t->id, t);
125     }
126
127     /*g_print("+PING: '%s' (id %u)\n", t->client->title, t->id);*/
128     PROP_MSG_TO(t->client->window, t->client->window, wm_protocols,
129                 prop_atoms.net_wm_ping, t->id, t->client->window, 0, 0,
130                 NoEventMask);
131 }
132
133 static gboolean ping_timeout(gpointer data)
134 {
135     ObPingTarget *t = data;
136
137     ping_send(t);
138
139     /* if the client hasn't been responding then do something about it */
140     if (t->waiting == PING_TIMEOUT_WARN)
141         t->h(t->client, TRUE); /* notify that the client isn't responding */
142
143     ++t->waiting;
144
145     return TRUE; /* repeat */
146 }
147
148 static void ping_end(ObClient *client, gpointer data)
149 {
150     ObPingTarget *t;
151
152     t = g_hash_table_lookup(ping_targets, &client);
153     g_assert(t);
154
155     g_hash_table_remove(ping_targets, &t->client);
156     g_hash_table_remove(ping_ids, &t->id);
157
158     ob_main_loop_timeout_remove_data(ob_main_loop, ping_timeout, t, FALSE);
159
160     g_free(t);
161 }