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"
30 #include "accessible-cache.h"
33 /*---------------------------------------------------------------------------*/
35 SpiLeasing *spi_global_leasing;
37 typedef struct _ExpiryElement
43 static void spi_leasing_dispose (GObject * object);
45 static void spi_leasing_finalize (GObject * object);
47 static void add_expiry_timeout (SpiLeasing * leasing);
49 /*---------------------------------------------------------------------------*/
51 G_DEFINE_TYPE (SpiLeasing, spi_leasing, G_TYPE_OBJECT)
53 static void spi_leasing_class_init (SpiLeasingClass * klass)
55 GObjectClass *object_class = (GObjectClass *) klass;
57 spi_leasing_parent_class = g_type_class_ref (G_TYPE_OBJECT);
59 object_class->finalize = spi_leasing_finalize;
60 object_class->dispose = spi_leasing_dispose;
64 spi_leasing_init (SpiLeasing * leasing)
66 leasing->expiry_queue = g_queue_new ();
67 leasing->expiry_func_id = 0;
71 spi_leasing_finalize (GObject * object)
73 SpiLeasing *leasing = SPI_LEASING (object);
75 if (leasing->expiry_func_id)
76 g_source_remove (leasing->expiry_func_id);
77 g_queue_free (leasing->expiry_queue);
78 G_OBJECT_CLASS (spi_leasing_parent_class)->finalize (object);
82 spi_leasing_dispose (GObject * object)
84 SpiLeasing *leasing = SPI_LEASING (object);
87 while (head = g_queue_pop_head (leasing->expiry_queue))
89 g_object_unref (head->object);
90 g_slice_free (ExpiryElement, head);
92 G_OBJECT_CLASS (spi_leasing_parent_class)->dispose (object);
95 /*---------------------------------------------------------------------------*/
98 End the lease on all objects whose expiry time has passed.
100 Check when the next event is and set the next expiry func.
103 expiry_func (gpointer data)
105 SpiLeasing *leasing = SPI_LEASING (data);
107 ExpiryElement *head, *current;
110 g_get_current_time (&t);
112 head = g_queue_peek_head (leasing->expiry_queue);
113 while (head != NULL && head->expiry_s <= t.tv_sec)
115 current = g_queue_pop_head (leasing->expiry_queue);
118 g_debug ("REVOKE - ");
119 spi_cache_print_info (current->object);
122 g_object_unref (current->object);
123 g_slice_free (ExpiryElement, current);
125 head = g_queue_peek_head (leasing->expiry_queue);
128 leasing->expiry_func_id = 0;
129 add_expiry_timeout (leasing);
134 /*---------------------------------------------------------------------------*/
137 Checks if an expiry timeout is already scheduled, if so returns.
138 This is becasue events will always be added to the end of the queue.
139 Events are always later in time to previoulsy added events.
141 Otherwise calculate the next wake time using the top of the queue
142 and add the next expiry function.
144 This function is called when a lease is added or at the end of the
145 expiry function to add the next expiry timeout.
148 add_expiry_timeout (SpiLeasing * leasing)
154 if (leasing->expiry_func_id != 0)
157 elem = (ExpiryElement *) g_queue_peek_head (leasing->expiry_queue);
161 /* The current time is implicitly rounded down here by ignoring the us */
162 g_get_current_time (&t);
163 next_expiry = elem->expiry_s - t.tv_sec;
164 leasing->expiry_func_id = g_timeout_add_seconds (next_expiry,
165 expiry_func, leasing);
168 /*---------------------------------------------------------------------------*/
171 The lease time is expected to be in seconds, the rounding is going to be to
172 intervals of 1 second.
174 The lease time is going to be rounded up, as the lease time should be
175 considered a MINIMUM that the object will be leased for.
177 #define LEASE_TIME_S 15
178 #define EXPIRY_TIME_S (LEASE_TIME_S + 1)
181 spi_leasing_take (SpiLeasing * leasing, GObject * object)
184 Get the current time.
186 Add the release event to the queue.
187 Check the next expiry.
195 g_get_current_time (&t);
196 expiry_s = t.tv_sec + EXPIRY_TIME_S;
198 elem = g_slice_new (ExpiryElement);
199 elem->expiry_s = expiry_s;
200 elem->object = g_object_ref (object);
202 g_queue_push_tail (leasing->expiry_queue, elem);
204 add_expiry_timeout (leasing);
207 g_debug ("LEASE - ");
208 spi_cache_print_info (object);
214 /*END------------------------------------------------------------------------*/