]>
Dogcows Code - chaz/openbox/blob - openbox/ping.c
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.
27 typedef struct _ObPingTarget
36 static GHashTable
*ping_ids
= NULL
;
37 static guint32 ping_next_id
= 1;
39 #define PING_TIMEOUT 3000 /* in MS */
40 /*! Warn the user after this many PING_TIMEOUT intervals */
41 #define PING_TIMEOUT_WARN 2
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_int_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
);
65 client_remove_destroy_notify(ping_end
);
68 void ping_start(struct _ObClient
*client
, ObPingEventHandler h
)
72 /* make sure the client supports ping! */
73 g_assert(client
->ping
== TRUE
);
75 /* make sure we're not already pinging the client */
76 if (g_hash_table_find(ping_ids
, find_client
, client
) != NULL
) return;
78 t
= g_slice_new0(ObPingTarget
);
82 t
->loopid
= g_timeout_add_full(G_PRIORITY_DEFAULT
, PING_TIMEOUT
,
83 ping_timeout
, t
, NULL
);
84 /* act like we just timed out immediately, to start the pinging process
85 now instead of after the first delay. this makes sure the client
86 ends up in the ping_ids hash table now. */
89 /* make sure we can remove the client later */
90 g_assert(g_hash_table_find(ping_ids
, find_client
, client
) != NULL
);
93 void ping_got_pong(guint32 id
)
97 if ((t
= g_hash_table_lookup(ping_ids
, &id
))) {
98 /*ob_debug("-PONG: '%s' (id %u)", t->client->title, t->id);*/
99 if (t
->waiting
> PING_TIMEOUT_WARN
) {
100 /* we had notified that they weren't responding, so now we
101 need to notify that they are again */
102 t
->h(t
->client
, FALSE
);
104 t
->waiting
= 0; /* not waiting for a reply anymore */
106 /* we got a pong so we're happy now */
107 ping_end(t
->client
, NULL
);
110 ob_debug("Got PONG with id %u but not waiting for one", id
);
113 static gboolean
find_client(gpointer key
, gpointer value
, gpointer client
)
115 ObPingTarget
*t
= value
;
116 return t
->client
== client
;
119 static void ping_send(ObPingTarget
*t
)
121 /* t->id is 0 when it hasn't been assigned an id ever yet.
122 we can reuse ids when t->waiting == 0, because we won't be getting a
123 pong for that id in the future again. that way for apps that aren't
124 timing out we don't need to remove/add them from/to the hash table */
125 if (t
->id
== 0 || t
->waiting
> 0) {
126 /* pick an id, and reinsert in the hash table with the new id */
127 if (t
->id
) g_hash_table_remove(ping_ids
, &t
->id
);
128 t
->id
= ping_next_id
;
129 if (++ping_next_id
== 0) ++ping_next_id
; /* skip 0 on wraparound */
130 g_hash_table_insert(ping_ids
, &t
->id
, t
);
133 /*ob_debug("+PING: '%s' (id %u)", t->client->title, t->id);*/
134 OBT_PROP_MSG_TO(t
->client
->window
, t
->client
->window
, WM_PROTOCOLS
,
135 OBT_PROP_ATOM(NET_WM_PING
), t
->id
, t
->client
->window
, 0, 0,
139 static gboolean
ping_timeout(gpointer data
)
141 ObPingTarget
*t
= data
;
145 /* if the client hasn't been responding then do something about it */
146 if (t
->waiting
== PING_TIMEOUT_WARN
)
147 t
->h(t
->client
, TRUE
); /* notify that the client isn't responding */
151 return TRUE
; /* repeat */
154 static void ping_end(ObClient
*client
, gpointer data
)
158 if ((t
= g_hash_table_find(ping_ids
, find_client
, client
))) {
159 g_hash_table_remove(ping_ids
, &t
->id
);
161 g_source_remove(t
->loopid
);
163 g_slice_free(ObPingTarget
, t
);
This page took 0.040009 seconds and 4 git commands to generate.