| 1 | #include <gio/gio.h> |
| 2 | |
| 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 | |
| 89 | static void handle_method_call(GDBusConnection *conn, |
| 90 | const gchar *sender, |
| 91 | const gchar *obj_path, |
| 92 | const gchar *interf_name, |
| 93 | const gchar *method_name, |
| 94 | GVariant *params, |
| 95 | GDBusMethodInvocation *invc, |
| 96 | gpointer usrdat) { |
| 97 | |
| 98 | g_printf("%s wants to call %s, at %s with interface %s\n", sender, method_name, obj_path, interf_name); |
| 99 | |
| 100 | g_dbus_method_invocation_return_value(invc, &spect_xml); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static GVariant * handle_get_property(GDBusConnection *conn, |
| 106 | const gchar *sender, |
| 107 | const gchar *obj_path, |
| 108 | const gchar *interf_name, |
| 109 | const gchar *prop_name, |
| 110 | GError **err, |
| 111 | gpointer usr_data) { |
| 112 | |
| 113 | GVariant *ret; |
| 114 | ret = g_variant_new_string(""); |
| 115 | g_snprintf(ret, 100, "%s touched property %s at %s", sender, prop_name, obj_path); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static gboolean handle_set_property(GDBusConnection *conn, |
| 121 | const gchar *sender, |
| 122 | const gchar *obj_path, |
| 123 | const gchar *interf_name, |
| 124 | const gchar *prop_name, |
| 125 | GVariant *val, |
| 126 | GError **err, |
| 127 | gpointer usr_data) { |
| 128 | g_dbus_connection_emit_signal(conn, |
| 129 | NULL, |
| 130 | obj_path, |
| 131 | "org.freedesktop.DBus.Properties", |
| 132 | "PropertiesChanged", |
| 133 | NULL, /* incorrect */ |
| 134 | NULL); |
| 135 | |
| 136 | return TRUE; |
| 137 | } |
| 138 | |
| 139 | static const GDBusInterfaceVTable interface_vtable = |
| 140 | { |
| 141 | handle_method_call, |
| 142 | handle_get_property, |
| 143 | handle_set_property |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | |
| 148 | static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
| 149 | g_print("got bus, name: %s\n", name); |
| 150 | |
| 151 | guint reg_id; |
| 152 | |
| 153 | reg_id = g_dbus_connection_register_object (conn, |
| 154 | "/org/freedesktop/hostname1", |
| 155 | spect_data->interfaces[0], |
| 156 | &interface_vtable, |
| 157 | NULL, |
| 158 | NULL, |
| 159 | NULL ); |
| 160 | g_assert(reg_id > 0); |
| 161 | } |
| 162 | |
| 163 | static void on_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
| 164 | g_print("got name %s\n", name); |
| 165 | } |
| 166 | |
| 167 | static void on_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data) { |
| 168 | g_print("lost name %s, exiting...\n", name); |
| 169 | exit(1); |
| 170 | } |
| 171 | |
| 172 | void hostnamed_init() { |
| 173 | |
| 174 | guint bus_descriptor; |
| 175 | GError *err = NULL; |
| 176 | GMainLoop *loop; |
| 177 | |
| 178 | spect_data = g_dbus_node_info_new_for_xml(spect_xml, NULL); |
| 179 | |
| 180 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SESSION, |
| 181 | (gchar *)"org.freedesktop.hostname1", |
| 182 | G_BUS_NAME_OWNER_FLAGS_NONE, |
| 183 | on_bus_acquired, |
| 184 | on_name_acquired, |
| 185 | on_name_lost, |
| 186 | NULL, |
| 187 | NULL); |
| 188 | |
| 189 | loop = g_main_loop_new(NULL, FALSE); |
| 190 | g_main_loop_run(loop); |
| 191 | |
| 192 | /* unclear */ |
| 193 | |
| 194 | g_bus_unown_name(bus_descriptor); |
| 195 | g_dbus_node_info_unref(spect_data); |
| 196 | } |
| 197 | |