baa40e28 |
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 | |
2eae47af |
17 | #include <unistd.h> |
18 | #include <limits.h> |
19 | |
20 | #include <sys/param.h> |
21 | #include <string.h> |
22 | |
23 | #include <glib/gprintf.h> |
ba9914b6 |
24 | #include <glib-unix.h> |
2eae47af |
25 | |
3c3794ac |
26 | #include "timedated-gen.h" |
ce19cb8a |
27 | #include "timedated.h" |
3c3794ac |
28 | |
2eae47af |
29 | GPtrArray *timedated_freeable; |
30 | Timedate1 *timedated_interf; |
31 | |
ff036f75 |
32 | /* --- begin method/property/dbus signal code --- */ |
2eae47af |
33 | |
34 | /*static gboolean |
35 | on_handle_set_hostname(Timedate1 *hn1_passed_interf, |
36 | GDBusMethodInvocation *invoc, |
37 | const gchar *greet, |
38 | gpointer data) { |
39 | return FALSE; |
40 | }*/ |
41 | |
42 | /*const gchar * |
43 | our_get_hostname() { |
44 | |
45 | gchar *hostname_buf, *ret; |
46 | size_t hostname_divider; |
47 | |
48 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
49 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
50 | g_ptr_array_add(timedated_freeable, hostname_buf); |
51 | g_ptr_array_add(timedated_freeable, ret); |
52 | |
53 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) |
54 | return ""; |
55 | |
56 | hostname_divider = strcspn(hostname_buf, "."); |
57 | |
58 | return strncpy(ret, hostname_buf, hostname_divider); |
59 | }*/ |
60 | |
ff036f75 |
61 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
2eae47af |
62 | |
63 | static void timedated_on_bus_acquired(GDBusConnection *conn, |
64 | const gchar *name, |
65 | gpointer user_data) { |
66 | |
67 | g_print("got bus, name: %s\n", name); |
68 | |
69 | } |
70 | |
71 | static void timedated_on_name_acquired(GDBusConnection *conn, |
72 | const gchar *name, |
73 | gpointer user_data) { |
74 | |
75 | g_print("got '%s' on system bus\n", name); |
76 | |
77 | timedated_interf = timedate1_skeleton_new(); |
78 | |
79 | /* attach function pointers to generated struct's method handlers |
80 | g_signal_connect(timedated_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);*/ |
81 | |
82 | /* set our properties before export |
83 | timedate1_set_hostname(timedated_interf, our_get_hostname()); */ |
84 | |
85 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf), |
86 | conn, |
87 | "/org/freedesktop/timedate1", |
88 | NULL)) { |
89 | |
90 | g_printf("Failed to export Timedate1's interface!"); |
91 | } |
92 | |
93 | } |
94 | |
2eae47af |
95 | static void timedated_on_name_lost(GDBusConnection *conn, |
96 | const gchar *name, |
97 | gpointer user_data) { |
98 | |
99 | g_print("lost name %s, exiting...", name); |
100 | |
101 | timedated_mem_clean(); |
102 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf)); |
103 | |
104 | } |
105 | |
eb20fe6d |
106 | /* --- end bus/name handlers, begin misc unix functions --- */ |
107 | |
108 | /* free()'s */ |
109 | void timedated_mem_clean() { |
110 | |
111 | g_ptr_array_foreach(timedated_freeable, (GFunc) g_free, NULL); |
112 | g_ptr_array_free(timedated_freeable, TRUE); |
113 | } |
114 | |
3c3794ac |
115 | int main() { |
2eae47af |
116 | |
117 | guint bus_descriptor; |
118 | GMainLoop *timedated_loop; |
119 | |
120 | timedated_loop = g_main_loop_new(NULL, TRUE); |
121 | timedated_freeable = g_ptr_array_new(); |
122 | |
123 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
124 | "org.freedesktop.timedate1", |
125 | G_BUS_NAME_OWNER_FLAGS_NONE, |
126 | timedated_on_bus_acquired, |
127 | timedated_on_name_acquired, |
128 | timedated_on_name_lost, |
129 | NULL, |
130 | NULL); |
131 | |
132 | g_main_loop_run(timedated_loop); |
133 | g_main_loop_unref(timedated_loop); |
134 | |
135 | g_bus_unown_name(bus_descriptor); |
136 | |
137 | timedated_mem_clean(); |
138 | |
3c3794ac |
139 | return 0; |
140 | } |