[134] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
| 2 | * |
---|
| 3 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
| 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 18 | * |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "config.h" |
---|
| 22 | |
---|
| 23 | #include <stdlib.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <fcntl.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <string.h> |
---|
| 28 | |
---|
| 29 | #include <glib.h> |
---|
| 30 | #include <glib/gi18n.h> |
---|
| 31 | #include <glib-object.h> |
---|
| 32 | |
---|
| 33 | #include "gdm-display-store.h" |
---|
| 34 | #include "gdm-display.h" |
---|
| 35 | |
---|
| 36 | #define GDM_DISPLAY_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_DISPLAY_STORE, GdmDisplayStorePrivate)) |
---|
| 37 | |
---|
| 38 | struct GdmDisplayStorePrivate |
---|
| 39 | { |
---|
| 40 | GHashTable *displays; |
---|
| 41 | }; |
---|
| 42 | |
---|
| 43 | enum { |
---|
| 44 | DISPLAY_ADDED, |
---|
| 45 | DISPLAY_REMOVED, |
---|
| 46 | LAST_SIGNAL |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | static guint signals [LAST_SIGNAL] = { 0, }; |
---|
| 50 | |
---|
| 51 | static void gdm_display_store_class_init (GdmDisplayStoreClass *klass); |
---|
| 52 | static void gdm_display_store_init (GdmDisplayStore *display_store); |
---|
| 53 | static void gdm_display_store_finalize (GObject *object); |
---|
| 54 | |
---|
| 55 | G_DEFINE_TYPE (GdmDisplayStore, gdm_display_store, G_TYPE_OBJECT) |
---|
| 56 | |
---|
| 57 | GQuark |
---|
| 58 | gdm_display_store_error_quark (void) |
---|
| 59 | { |
---|
| 60 | static GQuark ret = 0; |
---|
| 61 | if (ret == 0) { |
---|
| 62 | ret = g_quark_from_static_string ("gdm_display_store_error"); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | return ret; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void |
---|
| 69 | gdm_display_store_clear (GdmDisplayStore *store) |
---|
| 70 | { |
---|
| 71 | g_return_if_fail (store != NULL); |
---|
| 72 | g_debug ("GdmDisplayStore: Clearing display store"); |
---|
| 73 | g_hash_table_remove_all (store->priv->displays); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | static gboolean |
---|
| 77 | remove_display (char *id, |
---|
| 78 | GdmDisplay *display, |
---|
| 79 | GdmDisplay *display_to_remove) |
---|
| 80 | { |
---|
| 81 | if (display == display_to_remove) { |
---|
| 82 | return TRUE; |
---|
| 83 | } |
---|
| 84 | return FALSE; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | gboolean |
---|
| 88 | gdm_display_store_remove (GdmDisplayStore *store, |
---|
| 89 | GdmDisplay *display) |
---|
| 90 | { |
---|
| 91 | g_return_val_if_fail (store != NULL, FALSE); |
---|
| 92 | |
---|
| 93 | gdm_display_store_foreach_remove (store, |
---|
| 94 | (GdmDisplayStoreFunc)remove_display, |
---|
| 95 | display); |
---|
| 96 | return FALSE; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void |
---|
| 100 | gdm_display_store_foreach (GdmDisplayStore *store, |
---|
| 101 | GdmDisplayStoreFunc func, |
---|
| 102 | gpointer user_data) |
---|
| 103 | { |
---|
| 104 | g_return_if_fail (store != NULL); |
---|
| 105 | g_return_if_fail (func != NULL); |
---|
| 106 | |
---|
| 107 | g_hash_table_find (store->priv->displays, |
---|
| 108 | (GHRFunc)func, |
---|
| 109 | user_data); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | GdmDisplay * |
---|
| 113 | gdm_display_store_find (GdmDisplayStore *store, |
---|
| 114 | GdmDisplayStoreFunc predicate, |
---|
| 115 | gpointer user_data) |
---|
| 116 | { |
---|
| 117 | GdmDisplay *display; |
---|
| 118 | |
---|
| 119 | g_return_val_if_fail (store != NULL, NULL); |
---|
| 120 | g_return_val_if_fail (predicate != NULL, NULL); |
---|
| 121 | |
---|
| 122 | display = g_hash_table_find (store->priv->displays, |
---|
| 123 | (GHRFunc)predicate, |
---|
| 124 | user_data); |
---|
| 125 | return display; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | guint |
---|
| 129 | gdm_display_store_foreach_remove (GdmDisplayStore *store, |
---|
| 130 | GdmDisplayStoreFunc func, |
---|
| 131 | gpointer user_data) |
---|
| 132 | { |
---|
| 133 | guint ret; |
---|
| 134 | |
---|
| 135 | g_return_val_if_fail (store != NULL, 0); |
---|
| 136 | g_return_val_if_fail (func != NULL, 0); |
---|
| 137 | |
---|
| 138 | ret = g_hash_table_foreach_remove (store->priv->displays, |
---|
| 139 | (GHRFunc)func, |
---|
| 140 | user_data); |
---|
| 141 | |
---|
| 142 | return ret; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void |
---|
| 146 | gdm_display_store_add (GdmDisplayStore *store, |
---|
| 147 | GdmDisplay *display) |
---|
| 148 | { |
---|
| 149 | char *id; |
---|
| 150 | |
---|
| 151 | g_return_if_fail (store != NULL); |
---|
| 152 | g_return_if_fail (display != NULL); |
---|
| 153 | |
---|
| 154 | gdm_display_get_id (display, &id, NULL); |
---|
| 155 | |
---|
| 156 | g_debug ("GdmDisplayStore: Adding display %s to store", id); |
---|
| 157 | |
---|
| 158 | g_hash_table_insert (store->priv->displays, |
---|
| 159 | id, |
---|
| 160 | g_object_ref (display)); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | static void |
---|
| 164 | gdm_display_store_class_init (GdmDisplayStoreClass *klass) |
---|
| 165 | { |
---|
| 166 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
---|
| 167 | |
---|
| 168 | object_class->finalize = gdm_display_store_finalize; |
---|
| 169 | |
---|
| 170 | signals [DISPLAY_ADDED] = |
---|
| 171 | g_signal_new ("display-added", |
---|
| 172 | G_TYPE_FROM_CLASS (object_class), |
---|
| 173 | G_SIGNAL_RUN_LAST, |
---|
| 174 | G_STRUCT_OFFSET (GdmDisplayStoreClass, display_added), |
---|
| 175 | NULL, |
---|
| 176 | NULL, |
---|
| 177 | g_cclosure_marshal_VOID__STRING, |
---|
| 178 | G_TYPE_NONE, |
---|
| 179 | 1, G_TYPE_STRING); |
---|
| 180 | signals [DISPLAY_REMOVED] = |
---|
| 181 | g_signal_new ("display-removed", |
---|
| 182 | G_TYPE_FROM_CLASS (object_class), |
---|
| 183 | G_SIGNAL_RUN_LAST, |
---|
| 184 | G_STRUCT_OFFSET (GdmDisplayStoreClass, display_removed), |
---|
| 185 | NULL, |
---|
| 186 | NULL, |
---|
| 187 | g_cclosure_marshal_VOID__STRING, |
---|
| 188 | G_TYPE_NONE, |
---|
| 189 | 1, G_TYPE_STRING); |
---|
| 190 | |
---|
| 191 | g_type_class_add_private (klass, sizeof (GdmDisplayStorePrivate)); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | static void |
---|
| 195 | display_unref (GdmDisplay *display) |
---|
| 196 | { |
---|
| 197 | g_debug ("GdmDisplayStore: Unreffing display: %p", display); |
---|
| 198 | g_object_unref (display); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | static void |
---|
| 202 | gdm_display_store_init (GdmDisplayStore *store) |
---|
| 203 | { |
---|
| 204 | |
---|
| 205 | store->priv = GDM_DISPLAY_STORE_GET_PRIVATE (store); |
---|
| 206 | |
---|
| 207 | store->priv->displays = g_hash_table_new_full (g_str_hash, |
---|
| 208 | g_str_equal, |
---|
| 209 | g_free, |
---|
| 210 | (GDestroyNotify) display_unref); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | static void |
---|
| 214 | gdm_display_store_finalize (GObject *object) |
---|
| 215 | { |
---|
| 216 | GdmDisplayStore *store; |
---|
| 217 | |
---|
| 218 | g_return_if_fail (object != NULL); |
---|
| 219 | g_return_if_fail (GDM_IS_DISPLAY_STORE (object)); |
---|
| 220 | |
---|
| 221 | store = GDM_DISPLAY_STORE (object); |
---|
| 222 | |
---|
| 223 | g_return_if_fail (store->priv != NULL); |
---|
| 224 | |
---|
| 225 | g_hash_table_destroy (store->priv->displays); |
---|
| 226 | |
---|
| 227 | G_OBJECT_CLASS (gdm_display_store_parent_class)->finalize (object); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | GdmDisplayStore * |
---|
| 231 | gdm_display_store_new (void) |
---|
| 232 | { |
---|
| 233 | GObject *object; |
---|
| 234 | |
---|
| 235 | object = g_object_new (GDM_TYPE_DISPLAY_STORE, |
---|
| 236 | NULL); |
---|
| 237 | |
---|
| 238 | return GDM_DISPLAY_STORE (object); |
---|
| 239 | } |
---|