~ubuntu-branches/ubuntu/trusty/geis/trusty

« back to all changes in this revision

Viewing changes to libgeis/geis_region.c

  • Committer: Package Import Robot
  • Author(s): Chase Douglas
  • Date: 2012-07-30 08:51:42 UTC
  • Revision ID: package-import@ubuntu.com-20120730085142-jrc33ygjvt0ob1wl
Tags: upstream-2.2.11
ImportĀ upstreamĀ versionĀ 2.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file libgeis/geis_region.c
 
3
 * @brief implementation of the GEIS v2.0 API region module
 
4
 *
 
5
 * Copyright 2010 Canonical Ltd.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or modify it under
 
8
 * the terms of the GNU Lesser General Public License as published by the Free
 
9
 * Software Foundation; either version 3 of the License, or (at your option) any
 
10
 * later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 
15
 * details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public License
 
18
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 
19
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
20
 */
 
21
#include "geis_config.h"
 
22
#include "geis_region.h"
 
23
 
 
24
#include "geis_atomic.h"
 
25
#include "geis_error.h"
 
26
#include "geis_logging.h"
 
27
#include <string.h>
 
28
#include <stdarg.h>
 
29
#include <stdlib.h>
 
30
 
 
31
 
 
32
struct _GeisRegion
 
33
{
 
34
  GeisRefCount refcount;
 
35
  GeisString   type;
 
36
  GeisString   name;
 
37
  union {
 
38
    int windowid;
 
39
  } data;
 
40
};
 
41
 
 
42
struct _GeisRegionBag
 
43
{
 
44
  GeisRegion *region_store;
 
45
  GeisSize    region_store_size;
 
46
  GeisSize    region_count;
 
47
};
 
48
 
 
49
static const int region_bag_growth_constant = 2;
 
50
 
 
51
 
 
52
/*
 
53
 * Constructs a region bag.
 
54
 */
 
55
GeisRegionBag
 
56
geis_region_bag_new()
 
57
{
 
58
  GeisRegionBag bag = calloc(1, sizeof(struct _GeisRegionBag));
 
59
  if (!bag)
 
60
  {
 
61
    geis_error("failed to allocate region bag");
 
62
    goto final_exit;
 
63
  }
 
64
 
 
65
  bag->region_store_size = 3;
 
66
  bag->region_count = 0;
 
67
  bag->region_store = calloc(bag->region_store_size, sizeof(GeisRegion));
 
68
  if (!bag->region_store)
 
69
  {
 
70
    geis_error("failed to allocate region bag store");
 
71
    goto unwind_bag;
 
72
  }
 
73
  goto final_exit;
 
74
 
 
75
unwind_bag:
 
76
  free(bag);
 
77
final_exit:
 
78
  return bag;
 
79
}
 
80
 
 
81
 
 
82
/*
 
83
 * Destroys a region bag.
 
84
 */
 
85
void
 
86
geis_region_bag_delete(GeisRegionBag bag)
 
87
{
 
88
  GeisSize i;
 
89
  for (i = bag->region_count; i > 0; --i)
 
90
  {
 
91
    geis_region_delete(bag->region_store[i-1]);
 
92
  }
 
93
  free(bag);
 
94
}
 
95
 
 
96
 
 
97
/*
 
98
 * Gets the number of regions held in a bag.
 
99
 */
 
100
GeisSize
 
101
geis_region_bag_count(GeisRegionBag bag)
 
102
{
 
103
  return bag->region_count;
 
104
}
 
105
 
 
106
 
 
107
/*
 
108
 * Gets an indicated region from a bag.
 
109
 */
 
110
GeisRegion
 
111
geis_region_bag_region(GeisRegionBag bag, GeisSize index)
 
112
{
 
113
  GeisRegion region = NULL;
 
114
  if (index < bag->region_count)
 
115
  {
 
116
    region = bag->region_store[index];
 
117
  }
 
118
  return region;
 
119
}
 
120
 
 
121
 
 
122
/*
 
123
 * Puts a region into a bag.
 
124
 */
 
125
GeisStatus
 
126
geis_region_bag_insert(GeisRegionBag bag, GeisRegion region)
 
127
{
 
128
  GeisStatus status = GEIS_STATUS_UNKNOWN_ERROR;
 
129
  if (bag->region_count >= bag->region_store_size)
 
130
  {
 
131
    GeisSize new_store_size = bag->region_store_size * region_bag_growth_constant;
 
132
    GeisRegion *new_store = realloc(bag->region_store,
 
133
             new_store_size * sizeof(struct _GeisRegion));
 
134
    if (!new_store)
 
135
    {
 
136
      geis_error("failed to reallocate region bag");
 
137
      goto error_exit;
 
138
    }
 
139
    bag->region_store = new_store;
 
140
    bag->region_store_size = new_store_size;
 
141
  }
 
142
  bag->region_store[bag->region_count++] = region;
 
143
  status = GEIS_STATUS_SUCCESS;
 
144
 
 
145
error_exit:
 
146
  return status;
 
147
}
 
148
 
 
149
 
 
150
/**
 
151
 * Takes a region out of a bag.
 
152
 */
 
153
GeisStatus
 
154
geis_region_bag_remove(GeisRegionBag bag, GeisRegion region)
 
155
{
 
156
  GeisSize i;
 
157
  GeisStatus status = GEIS_STATUS_SUCCESS;
 
158
  for (i = 0; i < bag->region_count; ++i)
 
159
  {
 
160
    if (bag->region_store[i] == region)
 
161
    {
 
162
      GeisSize j;
 
163
      geis_region_delete(bag->region_store[i]);
 
164
      --bag->region_count;
 
165
      for (j = i; j < bag->region_count; ++j)
 
166
      {
 
167
        bag->region_store[j] = bag->region_store[j+1];
 
168
      }
 
169
      break;
 
170
    }
 
171
  }
 
172
  return status;
 
173
}
 
174
 
 
175
 
 
176
/*
 
177
 * Constructs a region.
 
178
 */
 
179
GeisRegion
 
180
geis_region_new(Geis       geis,
 
181
                GeisString name,
 
182
                GeisString init_arg_name, ...)
 
183
{
 
184
  GeisRegion region = NULL;
 
185
  va_list    varargs;
 
186
 
 
187
  region = calloc(1, sizeof(struct _GeisRegion));
 
188
  if (!region)
 
189
  {
 
190
    geis_error_push(geis, GEIS_STATUS_UNKNOWN_ERROR);
 
191
    geis_error("error allocating region");
 
192
    goto final_exit;
 
193
  }
 
194
 
 
195
  va_start(varargs, init_arg_name);
 
196
  while (init_arg_name)
 
197
  {
 
198
    if (0 == strcmp(init_arg_name, GEIS_REGION_X11_ROOT))
 
199
    {
 
200
      if (region->type)
 
201
      {
 
202
        geis_warning("multiple region types requested, only using the first");
 
203
        break;
 
204
      }
 
205
      region->type = strdup(init_arg_name);
 
206
      geis_debug("using X11 root");
 
207
    }
 
208
    else if (0 == strcmp(init_arg_name, GEIS_REGION_X11_WINDOWID))
 
209
    {
 
210
      if (region->type)
 
211
      {
 
212
        geis_warning("multiple region types requested, only using the first");
 
213
        break;
 
214
      }
 
215
      region->type = strdup(init_arg_name);
 
216
      region->data.windowid = va_arg(varargs, int);
 
217
      geis_debug("using X11 windowid 0x%08x", region->data.windowid);
 
218
    }
 
219
 
 
220
    init_arg_name = va_arg(varargs, GeisString);
 
221
  }
 
222
  va_end(varargs);
 
223
 
 
224
  ++region->refcount;
 
225
  region->name = strdup(name);
 
226
 
 
227
final_exit:
 
228
  return region;
 
229
}
 
230
 
 
231
 
 
232
/*
 
233
 * Destroys a GEIS v2.0 region.
 
234
 */
 
235
GeisStatus
 
236
geis_region_delete(GeisRegion region)
 
237
{
 
238
  if (0 == geis_atomic_unref(&region->refcount))
 
239
  {
 
240
    free((char *)region->name);
 
241
    free((char *)region->type);
 
242
    free(region);
 
243
  }
 
244
  return GEIS_STATUS_SUCCESS;
 
245
}
 
246
 
 
247
 
 
248
/*
 
249
 * Gets the name of a GEIS v2.0 region.
 
250
 */
 
251
GeisString
 
252
geis_region_name(GeisRegion region)
 
253
{
 
254
  return region->name;
 
255
}
 
256
 
 
257
 
 
258
/*
 
259
 * Adds a reference to a region.
 
260
 */
 
261
void
 
262
geis_region_ref(GeisRegion region)
 
263
{
 
264
  geis_atomic_ref(&region->refcount);
 
265
}
 
266
 
 
267
 
 
268
/*
 
269
 * Gets the type of the region.
 
270
 */
 
271
GeisString
 
272
geis_region_type(GeisRegion region)
 
273
{
 
274
  return region->type;
 
275
}
 
276
 
 
277
 
 
278
/*
 
279
 * Gets the data (if any) associated with the region type.
 
280
 */
 
281
void *
 
282
geis_region_data(GeisRegion region)
 
283
{
 
284
  return &region->data;
 
285
}
 
286
 
 
287