2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2009, 2010 Codethink Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library 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 GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "accessible-leasing.h"
29 /*---------------------------------------------------------------------------*/
31 SpiLeasing *spi_global_leasing;
33 typedef struct _ExpiryElement
39 static void spi_leasing_dispose (GObject * object);
41 static void spi_leasing_finalize (GObject * object);
43 static void add_expiry_timeout (SpiLeasing * leasing);
45 /*---------------------------------------------------------------------------*/
47 G_DEFINE_TYPE (SpiLeasing, spi_leasing, G_TYPE_OBJECT)
49 static void spi_leasing_class_init (SpiLeasingClass * klass)
51 GObjectClass *object_class = (GObjectClass *) klass;
53 spi_leasing_parent_class = g_type_class_ref (G_TYPE_OBJECT);
55 object_class->finalize = spi_leasing_finalize;
56 object_class->dispose = spi_leasing_dispose;
60 spi_leasing_init (SpiLeasing * leasing)
62 leasing->expiry_queue = g_queue_new ();
63 leasing->expiry_func_id = 0;
67 spi_leasing_finalize (GObject * object)
69 SpiLeasing *leasing = SPI_LEASING (object);
71 g_free (leasing->expiry_queue);
72 G_OBJECT_CLASS (spi_leasing_parent_class)->finalize (object);
76 spi_leasing_dispose (GObject * object)
78 SpiLeasing *leasing = SPI_LEASING (object);
81 while (head = g_queue_pop_head (leasing->expiry_queue))
83 g_object_unref (head->object);
84 g_slice_free (ExpiryElement, head);
86 G_OBJECT_CLASS (spi_leasing_parent_class)->dispose (object);
89 /*---------------------------------------------------------------------------*/
92 End the lease on all objects whose expiry time has passed.
94 Check when the next event is and set the next expiry func.
97 expiry_func (gpointer data)
99 SpiLeasing *leasing = SPI_LEASING (data);
101 ExpiryElement *head, *current;
104 g_get_current_time (&t);
106 head = g_queue_peek_head (leasing->expiry_queue);
107 while (head != NULL && head->expiry_s <= t.tv_sec)
109 current = g_queue_pop_head (leasing->expiry_queue);
111 g_object_unref (current->object);
112 g_slice_free (ExpiryElement, current);
114 head = g_queue_peek_head (leasing->expiry_queue);
117 leasing->expiry_func_id = 0;
118 add_expiry_timeout (leasing);
123 /*---------------------------------------------------------------------------*/
126 Checks if an expiry timeout is already scheduled, if so returns.
127 This is becasue events will always be added to the end of the queue.
128 Events are always later in time to previoulsy added events.
130 Otherwise calculate the next wake time using the top of the queue
131 and add the next expiry function.
133 This function is called when a lease is added or at the end of the
134 expiry function to add the next expiry timeout.
137 add_expiry_timeout (SpiLeasing * leasing)
143 if (leasing->expiry_func_id != 0)
146 elem = (ExpiryElement *) g_queue_peek_head (leasing->expiry_queue);
150 /* The current time is implicitly rounded down here by ignoring the us */
151 g_get_current_time (&t);
152 next_expiry = elem->expiry_s - t.tv_sec;
153 leasing->expiry_func_id = g_timeout_add_seconds (next_expiry,
154 expiry_func, leasing);
157 /*---------------------------------------------------------------------------*/
160 The lease time is expected to be in seconds, the rounding is going to be to
161 intervals of 1 second.
163 The lease time is going to be rounded up, as the lease time should be
164 considered a MINIMUM that the object will be leased for.
166 #define LEASE_TIME_S 3
167 #define EXPIRY_TIME_S (LEASE_TIME_S + 1)
170 spi_leasing_take (SpiLeasing * leasing, GObject * object)
173 Get the current time.
175 Add the release event to the queue.
176 Check the next expiry.
184 g_get_current_time (&t);
185 expiry_s = t.tv_sec + EXPIRY_TIME_S;
187 elem = g_slice_new (ExpiryElement);
188 elem->expiry_s = expiry_s;
189 elem->object = g_object_ref (object);
191 g_queue_push_tail (leasing->expiry_queue, elem);
193 add_expiry_timeout (leasing);
198 /*END------------------------------------------------------------------------*/