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
35 static GSList *ping_targets = NULL;
36 static gboolean active = FALSE;
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
42 static void ping_send(ObPingTarget *t);
43 static void ping_end(ObClient *client, gpointer data);
44 static gboolean ping_timeout(gpointer data);
46 void ping_start(struct _ObClient *client, ObPingEventHandler h)
51 g_assert(client->ping == TRUE);
53 /* make sure we're not already pinging it */
54 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
56 if (t->client == client) return;
59 t = g_new(ObPingTarget, 1);
62 t->waiting = 1; /* first wait for a reply */
65 ping_targets = g_slist_prepend(ping_targets, t);
66 ob_main_loop_timeout_add(ob_main_loop, PING_TIMEOUT, ping_timeout,
71 /* listen for the client to disappear */
72 client_add_destroy_notify(ping_end, NULL);
76 void ping_stop(struct _ObClient *c)
81 void ping_got_pong(Time timestamp)
86 /* make sure we're not already pinging it */
87 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
89 if (t->sent == timestamp) {
90 ob_debug("Got PONG with timestamp %lu\n", timestamp);
91 if (t->waiting > PING_TIMEOUT_WARN) {
92 /* we had notified that they weren't responding, so now we
93 need to notify that they are again */
94 t->h(t->client, FALSE);
96 t->waiting = 0; /* not waiting for a reply anymore */
102 ob_debug("Got PONG with timestamp %lu but not waiting for one\n",
106 static void ping_send(ObPingTarget *t)
108 t->sent = event_get_server_time();
109 ob_debug("PINGing client 0x%x at %lu\n", t->client->window, t->sent);
110 PROP_MSG_TO(t->client->window, t->client->window, wm_protocols,
111 prop_atoms.net_wm_ping, t->sent, t->client->window, 0, 0,
115 static gboolean ping_timeout(gpointer data)
117 ObPingTarget *t = data;
119 if (t->waiting == 0) { /* got a reply already */
120 /* send another ping to make sure it's still alive */
124 if (t->waiting == PING_TIMEOUT_WARN)
125 t->h(t->client, TRUE); /* notify that the client isn't responding */
129 return TRUE; /* repeat */
132 static void ping_end(ObClient *client, gpointer data)
137 for (it = ping_targets; it != NULL; it = g_slist_next(it)) {
139 if (t->client == client) {
140 ping_targets = g_slist_remove_link(ping_targets, it);
141 ob_main_loop_timeout_remove_data(ob_main_loop, ping_timeout, t,
148 /* stop listening if we're not waiting for any more pings */
151 client_remove_destroy_notify(ping_end);