ea207ed3 |
1 | #include <gio/gio.h> |
2 | |
387173cb |
3 | /* testing, for now */ |
4 | static GDBusNodeInfo *spect_data = NULL; |
5 | static const gchar spect_xml[] = |
6 | "<node>" |
7 | " <interface name='org.freedesktop.DBus.Peer'>" |
8 | " <method name='Ping'/>" |
9 | " <method name='GetMachineId'>" |
10 | " <arg type='s' name='machine_uuid' direction='out'/>" |
11 | " </method>" |
12 | " </interface>" |
13 | " <interface name='org.freedesktop.DBus.Introspectable'>" |
14 | " <method name='Introspect'>" |
15 | " <arg name='data' type='s' direction='out'/>" |
16 | " </method>" |
17 | " </interface>" |
18 | " <interface name='org.freedesktop.DBus.Properties'>" |
19 | " <method name='Get'>" |
20 | " <arg name='interface' direction='in' type='s'/>" |
21 | " <arg name='property' direction='in' type='s'/>" |
22 | " <arg name='value' direction='out' type='v'/>" |
23 | " </method>" |
24 | " <method name='GetAll'>" |
25 | " <arg name='interface' direction='in' type='s'/>" |
26 | " <arg name='properties' direction='out' type='a{sv}'/>" |
27 | " </method>" |
28 | " <method name='Set'>" |
29 | " <arg name='interface' direction='in' type='s'/>" |
30 | " <arg name='property' direction='in' type='s'/>" |
31 | " <arg name='value' direction='in' type='v'/>" |
32 | " </method>" |
33 | " <signal name='PropertiesChanged'>" |
34 | " <arg type='s' name='interface'/>" |
35 | " <arg type='a{sv}' name='changed_properties'/>" |
36 | " <arg type='as' name='invalidated_properties'/>" |
37 | " </signal>" |
38 | " </interface>" |
39 | " <interface name='org.freedesktop.hostname1'>" |
40 | " <property name='Hostname' type='s' access='read'>" |
41 | " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='false'/>" |
42 | " </property>" |
43 | " <property name='StaticHostname' type='s' access='read'>" |
44 | " </property>" |
45 | " <property name='PrettyHostname' type='s' access='read'>" |
46 | " </property>" |
47 | " <property name='IconName' type='s' access='read'>" |
48 | " </property>" |
49 | " <property name='Chassis' type='s' access='read'>" |
50 | " </property>" |
51 | " <property name='KernelName' type='s' access='read'>" |
52 | " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='const'/>" |
53 | " </property>" |
54 | " <property name='KernelRelease' type='s' access='read'>" |
55 | " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='const'/>" |
56 | " </property>" |
57 | " <property name='KernelVersion' type='s' access='read'>" |
58 | " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='const'/>" |
59 | " </property>" |
60 | " <property name='OperatingSystemPrettyName' type='s' access='read'>" |
61 | " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='const'/>" |
62 | " </property>" |
63 | " <property name='OperatingSystemCPEName' type='s' access='read'>" |
64 | " <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='const'/>" |
65 | " </property>" |
66 | " <method name='SetHostname'>" |
67 | " <arg type='s' direction='in'/>" |
68 | " <arg type='b' direction='in'/>" |
69 | " </method>" |
70 | " <method name='SetStaticHostname'>" |
71 | " <arg type='s' direction='in'/>" |
72 | " <arg type='b' direction='in'/>" |
73 | " </method>" |
74 | " <method name='SetPrettyHostname'>" |
75 | " <arg type='s' direction='in'/>" |
76 | " <arg type='b' direction='in'/>" |
77 | " </method>" |
78 | " <method name='SetIconName'>" |
79 | " <arg type='s' direction='in'/>" |
80 | " <arg type='b' direction='in'/>" |
81 | " </method>" |
82 | " <method name='SetChassis'>" |
83 | " <arg type='s' direction='in'/>" |
84 | " <arg type='b' direction='in'/>" |
85 | " </method>" |
86 | " </interface>" |
87 | "</node>"; |
88 | |
0df0018d |
89 | static const GDBusInterfaceVTable interface_vtable = |
90 | { |
91 | handle_method_call, |
92 | handle_get_property, |
93 | handle_set_property |
94 | }; |
95 | |
780e1f19 |
96 | static void handle_method_call(GDBusConnection *conn, |
97 | const gchar *sender, |
98 | const gchar *obj_path, |
99 | const gchar *interf_name, |
100 | const gchar *method_name, |
101 | GVariant *params, |
102 | GDBusMethodInvocation *invc, |
103 | gpointer usrdat) { |
104 | |
105 | g_printf("%s wants to call %s, at %s with interface %s\n", sender, method_name, obj_path, interf_name); |
106 | |
107 | g_dbus_method_invocation_return_value(invc, &spect_xml); |
108 | |
109 | } |
110 | |
111 | |
112 | static GVariant * handle_get_property(GDBusConnection *conn, |
113 | const gchar *sender, |
114 | const gchar *obj_path, |
115 | const gchar *interf_name, |
116 | const gchar *prop_name, |
117 | GError **err, |
118 | gpointer usr_data) { |
119 | |
120 | GVariant *ret; |
121 | ret = g_variant_new_string(""); |
122 | g_snprintf(ret, 100, "%s touched property %s at %s", sender, prop_name, obj_path); |
123 | |
124 | return ret; |
125 | } |
126 | |
127 | static gboolean handle_set_property(GDBusConnection *conn, |
128 | const gchar *sender, |
129 | const gchar *obj_path, |
130 | const gchar *interf_name, |
131 | const gchar *prop_name, |
132 | GVariant *val, |
133 | GError **err, |
134 | gpointer usr_data) { |
135 | g_dbus_connection_emit_signal(conn, |
136 | NULL, |
137 | obj_path, |
138 | "org.freedesktop.DBus.Properties", |
139 | "PropertiesChanged", |
140 | NULL, /* incorrect */ |
141 | NULL); |
142 | |
143 | return TRUE; |
144 | } |
387173cb |
145 | |
0df0018d |
146 | /* end method/property functions, begin bus name handlers |
147 | * TODO: these should be intertwined as to handle edge cases |
148 | * for when the system cannot immediately grab the name, as |
149 | * well as cases where the system unintendedly loses the name |
150 | */ |
387173cb |
151 | |
152 | |
ea207ed3 |
153 | static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
154 | g_print("got bus, name: %s\n", name); |
387173cb |
155 | |
387173cb |
156 | guint reg_id; |
ea207ed3 |
157 | |
387173cb |
158 | reg_id = g_dbus_connection_register_object (conn, |
159 | "/org/freedesktop/hostname1", |
160 | spect_data->interfaces[0], |
161 | &interface_vtable, |
162 | NULL, |
163 | NULL, |
164 | NULL ); |
165 | g_assert(reg_id > 0); |
ea207ed3 |
166 | } |
167 | |
168 | static void on_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
169 | g_print("got name %s\n", name); |
ea207ed3 |
170 | } |
171 | |
172 | static void on_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
780e1f19 |
173 | g_print("lost name %s, exiting...\n", name); |
174 | exit(1); |
ea207ed3 |
175 | } |
176 | |
0df0018d |
177 | /* safe call to try and start hostnamed */ |
178 | GError hostnamed_init() { |
387173cb |
179 | |
ea207ed3 |
180 | guint bus_descriptor; |
181 | GError *err = NULL; |
780e1f19 |
182 | GMainLoop *loop; |
183 | |
0df0018d |
184 | /* TODO: there is a correct way to generate introspection XML, switch to that */ |
780e1f19 |
185 | spect_data = g_dbus_node_info_new_for_xml(spect_xml, NULL); |
186 | |
187 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SESSION, |
ea207ed3 |
188 | (gchar *)"org.freedesktop.hostname1", |
189 | G_BUS_NAME_OWNER_FLAGS_NONE, |
190 | on_bus_acquired, |
191 | on_name_acquired, |
192 | on_name_lost, |
193 | NULL, |
194 | NULL); |
496f5d66 |
195 | |
780e1f19 |
196 | loop = g_main_loop_new(NULL, FALSE); |
197 | g_main_loop_run(loop); |
198 | |
0df0018d |
199 | /* i am not sure what the system state is once g_main_loop exits */ |
780e1f19 |
200 | |
201 | g_bus_unown_name(bus_descriptor); |
202 | g_dbus_node_info_unref(spect_data); |
203 | } |
387173cb |
204 | |