document the change in prompt timeout handling
[vlock.git] / src / plugin.c
blob6e40836cad410d4f803444308a49f0b6f88a9f91
1 /* plugin.c -- generic plugin routines for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
10 * the author.
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
18 #include <glib.h>
20 #include "plugin.h"
21 #include "util.h"
23 GQuark vlock_plugin_error_quark(void)
25 return g_quark_from_static_string("vlock-plugin-error-quark");
28 G_DEFINE_TYPE(VlockPlugin, vlock_plugin, G_TYPE_OBJECT)
30 /* Initialize plugin to default values. */
31 static void vlock_plugin_init(VlockPlugin *self)
33 self->name = NULL;
34 self->save_disabled = false;
35 for (size_t i = 0; i < nr_dependencies; i++)
36 self->dependencies[i] = NULL;
39 /* Create new plugin object. */
40 static GObject *vlock_plugin_constructor(GType gtype,
41 guint n_properties,
42 GObjectConstructParam *properties)
44 GObjectClass *parent_class = G_OBJECT_CLASS(vlock_plugin_parent_class);
45 GObject *object = parent_class->constructor(gtype, n_properties, properties);
46 VlockPlugin *self = VLOCK_PLUGIN(object);
48 g_return_val_if_fail(self->name != NULL, NULL);
50 return object;
53 /* Destroy plugin object. */
54 static void vlock_plugin_finalize(GObject *object)
56 VlockPlugin *self = VLOCK_PLUGIN(object);
58 g_free(self->name);
59 self->name = NULL;
61 /* Destroy dependency lists. */
62 for (size_t i = 0; i < nr_dependencies; i++) {
63 while (self->dependencies[i] != NULL) {
64 g_free(self->dependencies[i]->data);
65 self->dependencies[i] = g_list_delete_link(self->dependencies[i],
66 self->dependencies[i]);
70 G_OBJECT_CLASS(vlock_plugin_parent_class)->finalize(object);
73 /* Properties. */
74 enum {
75 PROP_VLOCK_PLUGIN_0,
76 PROP_VLOCK_PLUGIN_NAME
79 static void vlock_plugin_set_name(VlockPlugin *self, const gchar *name)
81 /* For security plugin names must not contain a slash. */
82 char *last_slash = strrchr(name, '/');
84 if (last_slash != NULL)
85 name = last_slash+1;
87 self->name = g_strdup(name);
90 /* Set properties. */
91 static void vlock_plugin_set_property(GObject *object,
92 guint property_id,
93 const GValue *value,
94 GParamSpec *pspec)
96 VlockPlugin *self = VLOCK_PLUGIN(object);
98 switch (property_id)
100 case PROP_VLOCK_PLUGIN_NAME:
101 g_free(self->name);
102 vlock_plugin_set_name(self, g_value_get_string(value));
103 break;
104 default:
105 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
106 break;
110 /* Get properties. */
111 static void vlock_plugin_get_property(GObject *object,
112 guint property_id,
113 GValue *value,
114 GParamSpec *pspec)
116 VlockPlugin *self = VLOCK_PLUGIN(object);
118 switch (property_id)
120 case PROP_VLOCK_PLUGIN_NAME:
121 g_value_set_string(value, self->name);
122 break;
123 default:
124 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
125 break;
129 /* Initialize plugin class. */
130 static void vlock_plugin_class_init(VlockPluginClass *klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
133 GParamSpec *vlock_plugin_param_spec;
135 /* Virtual methods. */
136 klass->open = NULL;
137 klass->call_hook = NULL;
139 /* Install overridden methods. */
140 gobject_class->constructor = vlock_plugin_constructor;
141 gobject_class->finalize = vlock_plugin_finalize;
142 gobject_class->set_property = vlock_plugin_set_property;
143 gobject_class->get_property = vlock_plugin_get_property;
145 /* Install properties. */
146 vlock_plugin_param_spec = g_param_spec_string(
147 "name",
148 "plugin name",
149 "Set the plugin's name",
150 NULL,
151 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
154 g_object_class_install_property(
155 gobject_class,
156 PROP_VLOCK_PLUGIN_NAME,
157 vlock_plugin_param_spec
161 bool vlock_plugin_open(VlockPlugin *self, GError **error)
163 VlockPluginClass *klass = VLOCK_PLUGIN_GET_CLASS(self);
164 g_assert(klass->open != NULL);
165 return klass->open(self, error);
168 bool vlock_plugin_call_hook(VlockPlugin *self, const gchar *hook_name)
170 VlockPluginClass *klass = VLOCK_PLUGIN_GET_CLASS(self);
171 g_assert(klass->call_hook != NULL);
172 return klass->call_hook(self, hook_name);