| 1 | #include <gio/gio.h> |
| 2 | |
| 3 | GDBusNodeInfo *spect_data; |
| 4 | |
| 5 | static void handle_method_call(GDBusConnection *conn, |
| 6 | const gchar *sender, |
| 7 | const gchar *obj_path, |
| 8 | const gchar *interf_name, |
| 9 | const gchar *method_name, |
| 10 | GVariant *params, |
| 11 | GDBusMethodInvocation *invc, |
| 12 | gpointer usrdat) { |
| 13 | |
| 14 | //if(g_strcmp0(method_name, "Introspect" |
| 15 | |
| 16 | GVariant *xml_ret_gvar; |
| 17 | GString *xml_ret; |
| 18 | |
| 19 | g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret); |
| 20 | xml_ret_gvar = g_variant_new_string(xml_ret->str); |
| 21 | g_dbus_method_invocation_return_value(invc, xml_ret_gvar); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | static GVariant * handle_get_property(GDBusConnection *conn, |
| 26 | const gchar *sender, |
| 27 | const gchar *obj_path, |
| 28 | const gchar *interf_name, |
| 29 | const gchar *prop_name, |
| 30 | GError **err, |
| 31 | gpointer usr_data) { |
| 32 | |
| 33 | GVariant *ret; |
| 34 | |
| 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | static gboolean handle_set_property(GDBusConnection *conn, |
| 39 | const gchar *sender, |
| 40 | const gchar *obj_path, |
| 41 | const gchar *interf_name, |
| 42 | const gchar *prop_name, |
| 43 | GVariant *val, |
| 44 | GError **err, |
| 45 | gpointer usr_data) { |
| 46 | |
| 47 | g_dbus_connection_emit_signal(conn, |
| 48 | NULL, |
| 49 | obj_path, |
| 50 | "org.freedesktop.DBus.Properties", |
| 51 | "PropertiesChanged", |
| 52 | NULL, /* incorrect */ |
| 53 | NULL); |
| 54 | |
| 55 | return TRUE; |
| 56 | } |
| 57 | |
| 58 | /* "hot" functions initially passed to gdbus */ |
| 59 | static const GDBusInterfaceVTable interface_vtable = |
| 60 | { |
| 61 | handle_method_call, |
| 62 | handle_get_property, |
| 63 | handle_set_property |
| 64 | }; |
| 65 | |
| 66 | /* end method/property functions, begin bus name handlers |
| 67 | * TODO: these should be intertwined as to handle edge cases |
| 68 | * for when the system cannot immediately grab the name, as |
| 69 | * well as cases where the system unintendedly loses the name |
| 70 | */ |
| 71 | static void on_bus_acquired(GDBusConnection *conn, |
| 72 | const gchar *name, |
| 73 | gpointer user_data) { |
| 74 | g_print("got bus, name: %s\n", name); |
| 75 | |
| 76 | guint reg_id; |
| 77 | |
| 78 | reg_id = g_dbus_connection_register_object(conn, |
| 79 | "/org/freedesktop/hostname1", |
| 80 | spect_data->interfaces[0], |
| 81 | &interface_vtable, |
| 82 | NULL, |
| 83 | NULL, |
| 84 | NULL); |
| 85 | } |
| 86 | |
| 87 | static void on_name_acquired(GDBusConnection *conn, |
| 88 | const gchar *name, |
| 89 | gpointer user_data) { |
| 90 | |
| 91 | g_print("got name %s\n", name); |
| 92 | } |
| 93 | |
| 94 | static void on_name_lost(GDBusConnection *conn, |
| 95 | const gchar *name, |
| 96 | gpointer user_data) { |
| 97 | |
| 98 | g_print("lost name %s, exiting...", name); |
| 99 | //TODO exit through g_main_loop properly... |
| 100 | exit(0); |
| 101 | } |
| 102 | |
| 103 | /* safe call to try and start hostnamed */ |
| 104 | GError * hostnamed_init() { |
| 105 | |
| 106 | guint bus_descriptor; |
| 107 | GError *err = NULL; |
| 108 | |
| 109 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
| 110 | (gchar *)"org.freedesktop.hostname1", |
| 111 | G_BUS_NAME_OWNER_FLAGS_NONE, |
| 112 | on_bus_acquired, |
| 113 | on_name_acquired, |
| 114 | on_name_lost, |
| 115 | NULL, |
| 116 | NULL); |
| 117 | |
| 118 | //TODO: malloc and return reference as if a main() closed |
| 119 | return err; |
| 120 | } |