summaryrefslogtreecommitdiff
path: root/registryd/desktop.c
blob: 70138fb7584d3de5a370b5e063172e69fccc6cc1 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc., Ximian Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* desktop.c: implements SpiDesktop.idl */

#include <config.h>
#include <stdio.h>
#include <string.h>
#include "desktop.h"
#include "registry.h"
#include <atk/atkcomponent.h>
#include <gdk/gdkscreen.h>
#include <gdk/gdkx.h>

G_DEFINE_TYPE(SpiDesktop, spi_desktop, G_TYPE_OBJECT)

/* SpiDesktop signals */
enum {
  APPLICATION_ADDED,
  APPLICATION_REMOVED,  
LAST_SIGNAL
};
static guint spi_desktop_signals[LAST_SIGNAL];


/* Our parent Gtk object type */
#define PARENT_TYPE SPI_ACCESSIBLE_TYPE

typedef struct {
	SpiDesktop *desktop;
	const char *path;
} Application;

static gboolean exiting = FALSE;

/* A pointer to our parent object class */
static GObjectClass *parent_class;

#define SPI_TYPE_ATK_DESKTOP		(spi_atk_desktop_get_type ())
#define SPI_ATK_DESKTOP(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), SPI_TYPE_ATK_DESKTOP, SpiAtkDesktop))
#define SPI_IS_ATK_DESKTOP(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), SPI_TYPE_ATK_DESKTOP))

typedef struct {
	AtkObject parent;

	GdkScreen *screen;
} SpiAtkDesktop;

typedef struct {
	AtkObjectClass parent;
} SpiAtkDesktopClass;

static void spi_atk_desktop_init (SpiAtkDesktop *desktop);
static void atk_component_interface_init (AtkComponentIface *iface);
static void spi_atk_desktop_get_extents	 (AtkComponent    *component,
                                          gint            *x,
                                          gint            *y,
                                          gint            *width,
                                          gint            *height,
                                          AtkCoordType    coord_type);

static GType 
spi_atk_desktop_get_type (void)
{
  static GType type = 0;

  if (!type)
    {
      static const GTypeInfo typeInfo =
      {
  	sizeof (SpiAtkDesktopClass),
  	(GBaseInitFunc) NULL,
  	(GBaseFinalizeFunc) NULL,
  	(GClassInitFunc) NULL,
  	(GClassFinalizeFunc) NULL,
  	NULL,
  	sizeof (SpiAtkDesktop),
  	0,
  	(GInstanceInitFunc) spi_atk_desktop_init,
      } ;
      static const GInterfaceInfo atk_component_info =
	{
        (GInterfaceInitFunc) atk_component_interface_init,
	(GInterfaceFinalizeFunc) NULL,
	NULL
      };

      type = g_type_register_static (ATK_TYPE_OBJECT,
                                     "SpiAtkDesktop", &typeInfo, 0);
      g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
                                   &atk_component_info);
    }
  return type;
}

static void 
spi_atk_desktop_init (SpiAtkDesktop *desktop)
{
  GdkDisplay *display;

  atk_object_set_name (ATK_OBJECT (desktop), "main");
  display = gdk_x11_lookup_xdisplay (GDK_DISPLAY ());
  desktop->screen = gdk_display_get_default_screen (display);
}

static void
atk_component_interface_init (AtkComponentIface *iface)
{
  g_return_if_fail (iface != NULL);

  iface->get_extents = spi_atk_desktop_get_extents;
}

static void 
spi_atk_desktop_get_extents (AtkComponent *component,
                             gint         *x,
                             gint	  *y,
                             gint	  *width,
                             gint	  *height,
                             AtkCoordType coord_type)
{
  SpiAtkDesktop *desktop;

  g_return_if_fail (SPI_IS_ATK_DESKTOP (component));
  desktop = SPI_ATK_DESKTOP (component);
  *x = 0;
  *y = 0;
  *width = gdk_screen_get_width (desktop->screen);
  *height = gdk_screen_get_height (desktop->screen);
}

static void
spi_desktop_init (SpiDesktop *desktop)
{
  desktop->applications = NULL;
}

static void
spi_desktop_dispose (GObject *object)
{
  SpiDesktop *desktop = (SpiDesktop *) object;

  while (desktop->applications)
    {
      Application *app = desktop->applications->data;
      g_assert (app != NULL);
      spi_desktop_remove_application (desktop, app->path);
    }

  G_OBJECT_CLASS (parent_class)->dispose (object); 
}

static dbus_bool_t
impl_desktop_get_child_count (const char *path, DBusMessageIter * iter,
		     void *user_data)
{
  SpiDesktop *desktop = SPI_REGISTRY(user_data)->desktop;

  if (desktop->applications)
    {
      return droute_return_v_int32(iter, g_list_length (desktop->applications));
    }
  else
    {
      return droute_return_v_int32(iter, 0);
    }
}

static DBusMessage *
impl_desktop_get_child_at_index (DBusConnection *bus, DBusMessage *message, void *user_data)
{
  SpiDesktop *desktop = SPI_REGISTRY(user_data)->desktop;
  DBusError error;
  dbus_int32_t index;
  Application *app;
  const char *path;
  DBusMessage *reply;

  dbus_error_init (&error);
  if (!dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
  {
    return spi_dbus_general_error (message);
  }
  app = g_list_nth_data (desktop->applications, index);
  path = (app? app->path: SPI_DBUS_PATH_NULL);

  reply = dbus_message_new_method_return (message);
  if (reply)
    {
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
    }

  return reply;
}

static void
spi_desktop_exiting (void)
{
  exiting = TRUE;
}

static void
spi_desktop_class_init (SpiDesktopClass *klass)
{
  GObjectClass * object_class = (GObjectClass *) klass;

  object_class->dispose = spi_desktop_dispose;
  
  parent_class = g_type_class_ref (G_TYPE_OBJECT);

  spi_desktop_signals[APPLICATION_ADDED] =
    g_signal_new ("application_added",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (SpiDesktopClass, application_added),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__UINT,
		  G_TYPE_NONE,
		  1, G_TYPE_UINT);
  spi_desktop_signals[APPLICATION_REMOVED] =
    g_signal_new ("application_removed",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (SpiDesktopClass, application_removed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__UINT,
		  G_TYPE_NONE,
		  1, G_TYPE_UINT);
  g_atexit (spi_desktop_exiting);
}

SpiDesktop *
spi_desktop_new (void)
{
  return g_object_new (SPI_DESKTOP_TYPE, NULL);
}

static void
abnormal_application_termination (gpointer object, Application *app)
{
  g_return_if_fail (SPI_IS_DESKTOP (app->desktop));

  if (!exiting)
    spi_desktop_remove_application (app->desktop, app->path);
}

void
spi_desktop_add_application (SpiDesktop *desktop,
			     const char *application)
{
  Application       *app;

  g_return_if_fail (SPI_IS_DESKTOP (desktop));

  spi_desktop_remove_application (desktop, application);

  app = g_new (Application, 1);
  app->desktop = desktop;
  app->path = application;

      desktop->applications = g_list_append (desktop->applications, app);

  // TODO: Listen for termination, and call abnormal_application_termination

  g_signal_emit (G_OBJECT (desktop),
		 spi_desktop_signals[APPLICATION_ADDED], 0,
		 g_list_index (desktop->applications, app));
}

void
spi_desktop_remove_application (SpiDesktop *desktop,
				const char *path)
{
  guint idx;
  GList *l;

  g_return_if_fail (path != NULL);
  g_return_if_fail (SPI_IS_DESKTOP (desktop));

  idx = 0;
  for (l = desktop->applications; l; l = l->next)
    {
      Application *app = (Application *) l->data;

      if (!strcmp(app->path, path))
        {
	  break;
	}
      idx++;
    }

  if (l)
    {
      Application *app = (Application *) l->data;

      desktop->applications = g_list_delete_link (desktop->applications, l);

      // TODO: unlisten for broken app, if appropriate
      g_free (app);
      
      g_signal_emit (G_OBJECT (desktop), spi_desktop_signals[APPLICATION_REMOVED], 0, idx);
    }
}

static DRouteMethod methods[] =
{
  { impl_desktop_get_child_at_index, "getChildAtIndex" },
  { NULL, NULL }
};

static DRouteProperty properties[] =
{
  { impl_desktop_get_child_count, NULL, "getChildCount" },
  { NULL, NULL, NULL }
};

void
spi_registry_initialize_desktop_interface (DRouteData * data)
{
  droute_add_interface (data, "org.freedesktop.atspi.Desktop", methods,
			properties, NULL, NULL);
};