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