2.34.1
[platform/upstream/at-spi2-atk.git] / atk-adaptor / accessible-leasing.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2009, 2010 Codethink Ltd.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "accessible-leasing.h"
28 #include "event.h"
29
30 #ifdef SPI_ATK_DEBUG
31 #include "accessible-cache.h"
32 #endif
33
34 /*---------------------------------------------------------------------------*/
35
36 SpiLeasing *spi_global_leasing;
37
38 typedef struct _ExpiryElement
39 {
40   guint expiry_s;
41   GObject *object;
42 } ExpiryElement;
43
44 static void spi_leasing_dispose (GObject * object);
45
46 static void spi_leasing_finalize (GObject * object);
47
48 static void add_expiry_timeout (SpiLeasing * leasing);
49
50 /*---------------------------------------------------------------------------*/
51
52 G_DEFINE_TYPE (SpiLeasing, spi_leasing, G_TYPE_OBJECT)
53
54 static void spi_leasing_class_init (SpiLeasingClass * klass)
55 {
56   GObjectClass *object_class = (GObjectClass *) klass;
57
58   spi_leasing_parent_class = g_type_class_ref (G_TYPE_OBJECT);
59
60   object_class->finalize = spi_leasing_finalize;
61   object_class->dispose = spi_leasing_dispose;
62 }
63
64 static void
65 spi_leasing_init (SpiLeasing * leasing)
66 {
67   leasing->expiry_queue = g_queue_new ();
68   leasing->expiry_func_id = 0;
69 }
70
71 static void
72 spi_leasing_finalize (GObject * object)
73 {
74   SpiLeasing *leasing = SPI_LEASING (object);
75
76   if (leasing->expiry_func_id)
77     g_source_remove (leasing->expiry_func_id);
78   g_queue_free (leasing->expiry_queue);
79   G_OBJECT_CLASS (spi_leasing_parent_class)->finalize (object);
80 }
81
82 static void
83 spi_leasing_dispose (GObject * object)
84 {
85   SpiLeasing *leasing = SPI_LEASING (object);
86
87   ExpiryElement *head;
88   while ((head = g_queue_pop_head (leasing->expiry_queue)))
89     {
90       g_object_unref (head->object);
91       g_slice_free (ExpiryElement, head);
92     }
93   G_OBJECT_CLASS (spi_leasing_parent_class)->dispose (object);
94 }
95
96 /*---------------------------------------------------------------------------*/
97
98 /*
99   End the lease on all objects whose expiry time has passed.
100  
101   Check when the next event is and set the next expiry func.
102 */
103 static gboolean
104 expiry_func (gpointer data)
105 {
106   SpiLeasing *leasing = SPI_LEASING (data);
107
108   ExpiryElement *head, *current;
109   GTimeVal t;
110
111   g_get_current_time (&t);
112
113   head = g_queue_peek_head (leasing->expiry_queue);
114   while (head != NULL && head->expiry_s <= t.tv_sec)
115     {
116       current = g_queue_pop_head (leasing->expiry_queue);
117
118 #ifdef SPI_ATK_DEBUG
119       g_debug ("REVOKE - ");
120       spi_cache_print_info (current->object);
121 #endif
122
123       g_object_unref (current->object);
124       g_slice_free (ExpiryElement, current);
125
126       head = g_queue_peek_head (leasing->expiry_queue);
127     }
128
129   leasing->expiry_func_id = 0;
130   add_expiry_timeout (leasing);
131
132   return FALSE;
133 }
134
135 /*---------------------------------------------------------------------------*/
136
137 /*
138   Checks if an expiry timeout is already scheduled, if so returns.
139   This is becasue events will always be added to the end of the queue.
140   Events are always later in time to previoulsy added events.
141
142   Otherwise calculate the next wake time using the top of the queue
143   and add the next expiry function.
144
145   This function is called when a lease is added or at the end of the
146   expiry function to add the next expiry timeout.
147 */
148 static void
149 add_expiry_timeout (SpiLeasing * leasing)
150 {
151   ExpiryElement *elem;
152   GTimeVal t;
153   guint next_expiry;
154
155   if (leasing->expiry_func_id != 0)
156     return;
157
158   elem = (ExpiryElement *) g_queue_peek_head (leasing->expiry_queue);
159   if (elem == NULL)
160     return;
161
162   /* The current time is implicitly rounded down here by ignoring the us */
163   g_get_current_time (&t);
164   next_expiry = elem->expiry_s - t.tv_sec;
165   leasing->expiry_func_id = spi_timeout_add_seconds (next_expiry,
166                                                      expiry_func, leasing);
167 }
168
169 /*---------------------------------------------------------------------------*/
170
171 /*
172   The lease time is expected to be in seconds, the rounding is going to be to
173   intervals of 1 second.
174
175   The lease time is going to be rounded up, as the lease time should be
176   considered a MINIMUM that the object will be leased for.
177 */
178 #define LEASE_TIME_S 15
179 #define EXPIRY_TIME_S (LEASE_TIME_S + 1)
180
181 GObject *
182 spi_leasing_take (SpiLeasing * leasing, GObject * object)
183 {
184   /*
185      Get the current time.
186      Quantize the time.
187      Add the release event to the queue.
188      Check the next expiry.
189    */
190
191   GTimeVal t;
192   guint expiry_s;
193
194   ExpiryElement *elem;
195
196   g_get_current_time (&t);
197   expiry_s = t.tv_sec + EXPIRY_TIME_S;
198
199   elem = g_slice_new (ExpiryElement);
200   elem->expiry_s = expiry_s;
201   elem->object = g_object_ref (object);
202
203   g_queue_push_tail (leasing->expiry_queue, elem);
204
205   add_expiry_timeout (leasing);
206
207 #ifdef SPI_ATK_DEBUG
208   g_debug ("LEASE - ");
209   spi_cache_print_info (object);
210 #endif
211
212   return object;
213 }
214
215 /*END------------------------------------------------------------------------*/