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 typedef struct _ExpiryElement
37 static void spi_leasing_dispose (GObject * object);
39 static void spi_leasing_finalize (GObject * object);
41 static void add_expiry_timeout (SpiLeasing * leasing);
43 /*---------------------------------------------------------------------------*/
45 G_DEFINE_TYPE (SpiLeasing, spi_leasing, G_TYPE_OBJECT)
47 static void spi_leasing_class_init (SpiLeasingClass * klass)
49 GObjectClass *object_class = (GObjectClass *) klass;
51 spi_leasing_parent_class = g_type_class_ref (G_TYPE_OBJECT);
53 object_class->finalize = spi_leasing_finalize;
54 object_class->dispose = spi_leasing_dispose;
58 spi_leasing_init (SpiLeasing * leasing)
60 leasing->expiry_queue = g_queue_new ();
61 leasing->expiry_func_id = 0;
65 spi_leasing_finalize (GObject * object)
67 SpiLeasing *leasing = SPI_LEASING (object);
69 g_free (leasing->expiry_queue);
70 G_OBJECT_CLASS (spi_leasing_parent_class)->finalize (object);
74 spi_leasing_dispose (GObject * object)
76 SpiLeasing *leasing = SPI_LEASING (object);
79 while (head = g_queue_pop_head (leasing->expiry_queue))
81 g_object_unref (head->object);
82 g_slice_free (ExpiryElement, head);
84 G_OBJECT_CLASS (spi_leasing_parent_class)->dispose (object);
87 /*---------------------------------------------------------------------------*/
90 End the lease on all objects whose expiry time has passed.
92 Check when the next event is and set the next expiry func.
95 expiry_func (gpointer data)
97 SpiLeasing *leasing = SPI_LEASING (data);
99 ExpiryElement *head, *current;
102 g_get_current_time (&t);
104 head = g_queue_peek_head (leasing->expiry_queue);
105 while (head != NULL && head->expiry_s <= t.tv_sec)
107 current = g_queue_pop_head (leasing->expiry_queue);
109 g_object_unref (current->object);
110 g_slice_free (ExpiryElement, current);
112 head = g_queue_peek_head (leasing->expiry_queue);
115 leasing->expiry_func_id = 0;
116 add_expiry_timeout (leasing);
121 /*---------------------------------------------------------------------------*/
124 Checks if an expiry timeout is already scheduled, if so returns.
125 This is becasue events will always be added to the end of the queue.
126 Events are always later in time to previoulsy added events.
128 Otherwise calculate the next wake time using the top of the queue
129 and add the next expiry function.
131 This function is called when a lease is added or at the end of the
132 expiry function to add the next expiry timeout.
135 add_expiry_timeout (SpiLeasing * leasing)
141 if (leasing->expiry_func_id != 0)
144 elem = (ExpiryElement *) g_queue_peek_head (leasing->expiry_queue);
148 /* The current time is implicitly rounded down here by ignoring the us */
149 g_get_current_time (&t);
150 next_expiry = elem->expiry_s - t.tv_sec;
151 leasing->expiry_func_id = g_timeout_add_seconds (next_expiry,
152 expiry_func, leasing);
155 /*---------------------------------------------------------------------------*/
158 The lease time is expected to be in seconds, the rounding is going to be to
159 intervals of 1 second.
161 The lease time is going to be rounded up, as the lease time should be
162 considered a MINIMUM that the object will be leased for.
164 #define LEASE_TIME_S 3
165 #define EXPIRY_TIME_S (LEASE_TIME_S + 1)
168 spi_leasing_take (SpiLeasing * leasing, GObject * object)
171 Get the current time.
173 Add the release event to the queue.
174 Check the next expiry.
182 g_get_current_time (&t);
183 expiry_s = t.tv_sec + EXPIRY_TIME_S;
185 elem = g_slice_new (ExpiryElement);
186 elem->expiry_s = expiry_s;
187 elem->object = g_object_ref (object);
189 g_queue_push_tail (leasing->expiry_queue, elem);
191 add_expiry_timeout (leasing);
196 /*END------------------------------------------------------------------------*/