summaryrefslogtreecommitdiff
path: root/atk-adaptor/accessible-leasing.c
blob: f60d7ffba36c8bfb1cc13570a65b1036d56f25c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2009, 2010 Codethink Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "accessible-leasing.h"
#include "event.h"

#ifdef SPI_ATK_DEBUG
#include "accessible-cache.h"
#endif

/*---------------------------------------------------------------------------*/

SpiLeasing *spi_global_leasing;

typedef struct _ExpiryElement
{
  gint64 expiry_s;
  GObject *object;
} ExpiryElement;

static void spi_leasing_dispose (GObject * object);

static void spi_leasing_finalize (GObject * object);

static void add_expiry_timeout (SpiLeasing * leasing);

/*---------------------------------------------------------------------------*/

G_DEFINE_TYPE (SpiLeasing, spi_leasing, G_TYPE_OBJECT)

static void spi_leasing_class_init (SpiLeasingClass * klass)
{
  GObjectClass *object_class = (GObjectClass *) klass;

  spi_leasing_parent_class = g_type_class_ref (G_TYPE_OBJECT);

  object_class->finalize = spi_leasing_finalize;
  object_class->dispose = spi_leasing_dispose;
}

static void
spi_leasing_init (SpiLeasing * leasing)
{
  leasing->expiry_queue = g_queue_new ();
  leasing->expiry_func_id = 0;
}

static void
spi_leasing_finalize (GObject * object)
{
  SpiLeasing *leasing = SPI_LEASING (object);

  if (leasing->expiry_func_id)
    g_source_remove (leasing->expiry_func_id);
  g_queue_free (leasing->expiry_queue);
  G_OBJECT_CLASS (spi_leasing_parent_class)->finalize (object);
}

static void
spi_leasing_dispose (GObject * object)
{
  SpiLeasing *leasing = SPI_LEASING (object);

  ExpiryElement *head;
  while ((head = g_queue_pop_head (leasing->expiry_queue)))
    {
      g_object_unref (head->object);
      g_slice_free (ExpiryElement, head);
    }
  G_OBJECT_CLASS (spi_leasing_parent_class)->dispose (object);
}

/*---------------------------------------------------------------------------*/

/*
  End the lease on all objects whose expiry time has passed.
 
  Check when the next event is and set the next expiry func.
*/
static gboolean
expiry_func (gpointer data)
{
  SpiLeasing *leasing = SPI_LEASING (data);

  ExpiryElement *head, *current;
  gint64 secs = g_get_monotonic_time () / 1000000;

  head = g_queue_peek_head (leasing->expiry_queue);
  while (head != NULL && head->expiry_s <= secs)
    {
      current = g_queue_pop_head (leasing->expiry_queue);

#ifdef SPI_ATK_DEBUG
      g_debug ("REVOKE - ");
      spi_cache_print_info (current->object);
#endif

      g_object_unref (current->object);
      g_slice_free (ExpiryElement, current);

      head = g_queue_peek_head (leasing->expiry_queue);
    }

  leasing->expiry_func_id = 0;
  add_expiry_timeout (leasing);

  return FALSE;
}

/*---------------------------------------------------------------------------*/

/*
  Checks if an expiry timeout is already scheduled, if so returns.
  This is becasue events will always be added to the end of the queue.
  Events are always later in time to previoulsy added events.

  Otherwise calculate the next wake time using the top of the queue
  and add the next expiry function.

  This function is called when a lease is added or at the end of the
  expiry function to add the next expiry timeout.
*/
static void
add_expiry_timeout (SpiLeasing * leasing)
{
  ExpiryElement *elem;
  gint64 secs = g_get_monotonic_time () / 1000000;
  guint next_expiry;

  if (leasing->expiry_func_id != 0)
    return;

  elem = (ExpiryElement *) g_queue_peek_head (leasing->expiry_queue);
  if (elem == NULL)
    return;

  next_expiry = elem->expiry_s - secs;
  leasing->expiry_func_id = spi_timeout_add_seconds (next_expiry,
                                                     expiry_func, leasing);
}

/*---------------------------------------------------------------------------*/

/*
  The lease time is expected to be in seconds, the rounding is going to be to
  intervals of 1 second.

  The lease time is going to be rounded up, as the lease time should be
  considered a MINIMUM that the object will be leased for.
*/
#define LEASE_TIME_S 15
#define EXPIRY_TIME_S (LEASE_TIME_S + 1)

GObject *
spi_leasing_take (SpiLeasing * leasing, GObject * object)
{
  /*
     Get the current time.
     Quantize the time.
     Add the release event to the queue.
     Check the next expiry.
   */

  gint64 secs = g_get_monotonic_time () / 1000000;
  guint expiry_s;

  ExpiryElement *elem;

  expiry_s = secs + EXPIRY_TIME_S;

  elem = g_slice_new (ExpiryElement);
  elem->expiry_s = expiry_s;
  elem->object = g_object_ref (object);

  g_queue_push_tail (leasing->expiry_queue, elem);

  add_expiry_timeout (leasing);

#ifdef SPI_ATK_DEBUG
  g_debug ("LEASE - ");
  spi_cache_print_info (object);
#endif

  return object;
}

/*END------------------------------------------------------------------------*/