1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 client.h for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2008 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
28 typedef struct _ObPingTarget
36 static GHashTable *ping_ids = NULL;
37 static guint32 ping_next_id = 1;
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
43 static void ping_send(ObPingTarget *t);
44 static void ping_end(ObClient *client, gpointer data);
45 static gboolean ping_timeout(gpointer data);
46 static gboolean find_client(gpointer key, gpointer value, gpointer client);
48 void ping_startup(gboolean reconfigure)
50 if (reconfigure) return;
52 ping_ids = g_hash_table_new(g_direct_hash, g_int_equal);
54 /* listen for clients to disappear */
55 client_add_destroy_notify(ping_end, NULL);
58 void ping_shutdown(gboolean reconfigure)
60 if (reconfigure) return;
62 g_hash_table_unref(ping_ids);
64 client_remove_destroy_notify(ping_end);
67 void ping_start(struct _ObClient *client, ObPingEventHandler h)
71 g_assert(g_hash_table_find(ping_ids, find_client, client) == NULL);
73 g_assert(client->ping == TRUE);
75 t = g_new0(ObPingTarget, 1);
79 ob_main_loop_timeout_add(ob_main_loop, PING_TIMEOUT, ping_timeout,
80 t, g_direct_equal, NULL);
81 /* act like we just timed out immediately, to start the pinging process
82 now instead of after the first delay */
86 void ping_stop(struct _ObClient *c)
91 void ping_got_pong(guint32 id)
95 if ((t = g_hash_table_lookup(ping_ids, &id))) {
96 /*g_print("-PONG: '%s' (id %u)\n", t->client->title, t->id);*/
97 if (t->waiting > PING_TIMEOUT_WARN) {
98 /* we had notified that they weren't responding, so now we
99 need to notify that they are again */
100 t->h(t->client, FALSE);
102 t->waiting = 0; /* not waiting for a reply anymore */
105 ob_debug("Got PONG with id %u but not waiting for one\n", id);
108 static gboolean find_client(gpointer key, gpointer value, gpointer client)
110 ObPingTarget *t = value;
111 return t->client == client;
114 static void ping_send(ObPingTarget *t)
116 /* t->id is 0 when it hasn't been assigned an id ever yet.
117 we can reuse ids when t->waiting == 0, because we won't be getting a
118 pong for that id in the future again. that way for apps that aren't
119 timing out we don't need to remove/add them from/to the hash table */
120 if (t->id == 0 || t->waiting > 0) {
121 /* pick an id, and reinsert in the hash table with the new id */
122 if (t->id) g_hash_table_remove(ping_ids, &t->id);
123 t->id = ping_next_id;
124 if (++ping_next_id == 0) ++ping_next_id; /* skip 0 on wraparound */
125 g_hash_table_insert(ping_ids, &t->id, t);
128 /*g_print("+PING: '%s' (id %u)\n", t->client->title, t->id);*/
129 PROP_MSG_TO(t->client->window, t->client->window, wm_protocols,
130 prop_atoms.net_wm_ping, t->id, t->client->window, 0, 0,
134 static gboolean ping_timeout(gpointer data)
136 ObPingTarget *t = data;
140 /* if the client hasn't been responding then do something about it */
141 if (t->waiting == PING_TIMEOUT_WARN)
142 t->h(t->client, TRUE); /* notify that the client isn't responding */
146 return TRUE; /* repeat */
149 static void ping_end(ObClient *client, gpointer data)
153 t = g_hash_table_find(ping_ids, find_client, client);
154 g_assert (t != NULL);
156 g_hash_table_remove(ping_ids, &t->id);
158 ob_main_loop_timeout_remove_data(ob_main_loop, ping_timeout, t, FALSE);