Add a class for taking leased references of GObjects.
[platform/core/uifw/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 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.
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  * Library General Public License for more details.
16  *
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.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <accessible-leasing.h>
28
29 /*---------------------------------------------------------------------------*/
30
31 typedef struct _ExpiryElement
32 {
33   guint expiry_s;
34   GObject *object;
35 } ExpiryElement;
36
37 static void spi_leasing_dispose (GObject * object);
38
39 static void spi_leasing_finalize (GObject * object);
40
41 static void add_expiry_timeout (SpiLeasing * leasing);
42
43 /*---------------------------------------------------------------------------*/
44
45 G_DEFINE_TYPE (SpiLeasing, spi_leasing, G_TYPE_OBJECT)
46
47 static void spi_leasing_class_init (SpiLeasingClass * klass)
48 {
49   GObjectClass *object_class = (GObjectClass *) klass;
50
51   spi_leasing_parent_class = g_type_class_ref (G_TYPE_OBJECT);
52
53   object_class->finalize = spi_leasing_finalize;
54   object_class->dispose = spi_leasing_dispose;
55 }
56
57 static void
58 spi_leasing_init (SpiLeasing * leasing)
59 {
60   leasing->expiry_queue = g_queue_new ();
61   leasing->expiry_func_id = 0;
62 }
63
64 static void
65 spi_leasing_finalize (GObject * object)
66 {
67   SpiLeasing *leasing = SPI_LEASING (object);
68
69   g_free (leasing->expiry_queue);
70   G_OBJECT_CLASS (spi_leasing_parent_class)->finalize (object);
71 }
72
73 static void
74 spi_leasing_dispose (GObject * object)
75 {
76   SpiLeasing *leasing = SPI_LEASING (object);
77
78   ExpiryElement *head;
79   while (head = g_queue_pop_head (leasing->expiry_queue))
80     {
81       g_object_unref (head->object);
82       g_slice_free (ExpiryElement, head);
83     }
84   G_OBJECT_CLASS (spi_leasing_parent_class)->dispose (object);
85 }
86
87 /*---------------------------------------------------------------------------*/
88
89 /*
90   End the lease on all objects whose expiry time has passed.
91  
92   Check when the next event is and set the next expiry func.
93 */
94 static gboolean
95 expiry_func (gpointer data)
96 {
97   SpiLeasing *leasing = SPI_LEASING (data);
98
99   ExpiryElement *head, *current;
100   GTimeVal t;
101
102   g_get_current_time (&t);
103
104   head = g_queue_peek_head (leasing->expiry_queue);
105   while (head != NULL && head->expiry_s <= t.tv_sec)
106     {
107       current = g_queue_pop_head (leasing->expiry_queue);
108
109       g_object_unref (current->object);
110       g_slice_free (ExpiryElement, current);
111
112       head = g_queue_peek_head (leasing->expiry_queue);
113     }
114
115   leasing->expiry_func_id = 0;
116   add_expiry_timeout (leasing);
117
118   return FALSE;
119 }
120
121 /*---------------------------------------------------------------------------*/
122
123 /*
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.
127
128   Otherwise calculate the next wake time using the top of the queue
129   and add the next expiry function.
130
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.
133 */
134 static void
135 add_expiry_timeout (SpiLeasing * leasing)
136 {
137   ExpiryElement *elem;
138   GTimeVal t;
139   guint next_expiry;
140
141   if (leasing->expiry_func_id != 0)
142     return;
143
144   elem = (ExpiryElement *) g_queue_peek_head (leasing->expiry_queue);
145   if (elem == NULL)
146     return;
147
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);
153 }
154
155 /*---------------------------------------------------------------------------*/
156
157 /*
158   The lease time is expected to be in seconds, the rounding is going to be to
159   intervals of 1 second.
160
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.
163 */
164 #define LEASE_TIME_S 3
165 #define EXPIRY_TIME_S (LEASE_TIME_S + 1)
166
167 GObject *
168 spi_leasing_take (SpiLeasing * leasing, GObject * object)
169 {
170   /*
171      Get the current time.
172      Quantize the time.
173      Add the release event to the queue.
174      Check the next expiry.
175    */
176
177   GTimeVal t;
178   guint expiry_s;
179
180   ExpiryElement *elem;
181
182   g_get_current_time (&t);
183   expiry_s = t.tv_sec + EXPIRY_TIME_S;
184
185   elem = g_slice_new (ExpiryElement);
186   elem->expiry_s = expiry_s;
187   elem->object = g_object_ref (object);
188
189   g_queue_push_tail (leasing->expiry_queue, elem);
190
191   add_expiry_timeout (leasing);
192
193   return object;
194 }
195
196 /*END------------------------------------------------------------------------*/