| 1 | /* |
| 2 | * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <unistd.h> |
| 18 | #include <limits.h> |
| 19 | |
| 20 | #include <sys/param.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <glib/gprintf.h> |
| 24 | |
| 25 | #include "timedated-gen.h" |
| 26 | |
| 27 | GPtrArray *timedated_freeable; |
| 28 | Timedate1 *timedated_interf; |
| 29 | |
| 30 | /* --- begin method/property/signal code --- */ |
| 31 | |
| 32 | /*static gboolean |
| 33 | on_handle_set_hostname(Timedate1 *hn1_passed_interf, |
| 34 | GDBusMethodInvocation *invoc, |
| 35 | const gchar *greet, |
| 36 | gpointer data) { |
| 37 | return FALSE; |
| 38 | }*/ |
| 39 | |
| 40 | /*const gchar * |
| 41 | our_get_hostname() { |
| 42 | |
| 43 | gchar *hostname_buf, *ret; |
| 44 | size_t hostname_divider; |
| 45 | |
| 46 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
| 47 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
| 48 | g_ptr_array_add(timedated_freeable, hostname_buf); |
| 49 | g_ptr_array_add(timedated_freeable, ret); |
| 50 | |
| 51 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) |
| 52 | return ""; |
| 53 | |
| 54 | hostname_divider = strcspn(hostname_buf, "."); |
| 55 | |
| 56 | return strncpy(ret, hostname_buf, hostname_divider); |
| 57 | }*/ |
| 58 | |
| 59 | /* --- end method/property/signal code, begin bus/name handlers --- */ |
| 60 | |
| 61 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
| 62 | const gchar *name, |
| 63 | gpointer user_data) { |
| 64 | |
| 65 | g_print("got bus, name: %s\n", name); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | static void timedated_on_name_acquired(GDBusConnection *conn, |
| 70 | const gchar *name, |
| 71 | gpointer user_data) { |
| 72 | |
| 73 | g_print("got '%s' on system bus\n", name); |
| 74 | |
| 75 | timedated_interf = timedate1_skeleton_new(); |
| 76 | |
| 77 | /* attach function pointers to generated struct's method handlers |
| 78 | g_signal_connect(timedated_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);*/ |
| 79 | |
| 80 | /* set our properties before export |
| 81 | timedate1_set_hostname(timedated_interf, our_get_hostname()); */ |
| 82 | |
| 83 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
| 84 | conn, |
| 85 | "/org/freedesktop/timedate1", |
| 86 | NULL)) { |
| 87 | |
| 88 | g_printf("Failed to export Timedate1's interface!"); |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | /* --- end bus/name handlers, begin misc functions --- */ |
| 94 | |
| 95 | /* free()'s */ |
| 96 | void timedated_mem_clean() { |
| 97 | |
| 98 | g_ptr_array_foreach(timedated_freeable, (GFunc) g_free, NULL); |
| 99 | g_ptr_array_free(timedated_freeable, TRUE); |
| 100 | } |
| 101 | |
| 102 | static void timedated_on_name_lost(GDBusConnection *conn, |
| 103 | const gchar *name, |
| 104 | gpointer user_data) { |
| 105 | |
| 106 | g_print("lost name %s, exiting...", name); |
| 107 | |
| 108 | timedated_mem_clean(); |
| 109 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf)); |
| 110 | |
| 111 | } |
| 112 | |
| 113 | int main() { |
| 114 | |
| 115 | guint bus_descriptor; |
| 116 | GMainLoop *timedated_loop; |
| 117 | |
| 118 | timedated_loop = g_main_loop_new(NULL, TRUE); |
| 119 | timedated_freeable = g_ptr_array_new(); |
| 120 | |
| 121 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
| 122 | "org.freedesktop.timedate1", |
| 123 | G_BUS_NAME_OWNER_FLAGS_NONE, |
| 124 | timedated_on_bus_acquired, |
| 125 | timedated_on_name_acquired, |
| 126 | timedated_on_name_lost, |
| 127 | NULL, |
| 128 | NULL); |
| 129 | |
| 130 | g_main_loop_run(timedated_loop); |
| 131 | g_main_loop_unref(timedated_loop); |
| 132 | |
| 133 | g_bus_unown_name(bus_descriptor); |
| 134 | |
| 135 | timedated_mem_clean(); |
| 136 | |
| 137 | return 0; |
| 138 | } |