2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
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.
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.
21 #include <sys/param.h>
24 #include <glib/gprintf.h>
25 #include <glib-unix.h>
27 #include "localed-gen.h"
30 GPtrArray
*localed_freeable
;
31 Locale1
*localed_interf
;
33 GMainLoop
*localed_loop
;
36 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
38 /* --- begin method/property/dbus signal code --- */
41 on_handle_set_hostname(Locale1 *hn1_passed_interf,
42 GDBusMethodInvocation *invoc,
51 gchar *hostname_buf, *ret;
52 size_t hostname_divider;
54 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
55 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
56 g_ptr_array_add(localed_freeable, hostname_buf);
57 g_ptr_array_add(localed_freeable, ret);
59 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
62 hostname_divider = strcspn(hostname_buf, ".");
64 return strncpy(ret, hostname_buf, hostname_divider);
67 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
69 static void localed_on_bus_acquired(GDBusConnection
*conn
,
73 g_print("got bus/name, exporting %s's interface...\n", name
);
75 localed_interf
= locale1_skeleton_new();
77 /* attach function pointers to generated struct's method handlers
78 g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */
80 /* set our properties before export
81 locale1_set_hostname(localed_interf, our_get_hostname()); */
83 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf
),
85 "/org/freedesktop/locale1",
88 g_printf("failed to export %s's interface!\n", name
);
93 dbus_interface_exported
= TRUE
;
94 g_printf("exported %s's interface on the system bus...\n", name
);
98 static void localed_on_name_acquired(GDBusConnection
*conn
,
100 gpointer user_data
) {
102 g_printf("success!\n");
105 static void localed_on_name_lost(GDBusConnection
*conn
,
107 gpointer user_data
) {
111 g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name
);
115 g_print("lost name %s, exiting...\n", name
);
120 /* --- end bus/name handlers, begin misc unix functions --- */
122 /* safe call to clean and then exit
123 * this stops our GMainLoop safely before letting main() return */
124 void localed_mem_clean() {
126 g_ptr_array_foreach(localed_freeable
, (GFunc
) g_free
, NULL
);
127 g_ptr_array_free(localed_freeable
, TRUE
);
132 localed_loop
= g_main_loop_new(NULL
, TRUE
);
133 localed_freeable
= g_ptr_array_new();
135 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
136 "org.freedesktop.locale1",
137 G_BUS_NAME_OWNER_FLAGS_NONE
,
138 localed_on_bus_acquired
,
139 localed_on_name_acquired
,
140 localed_on_name_lost
,
144 g_main_loop_run(localed_loop
);
145 g_main_loop_unref(localed_loop
);
147 g_bus_unown_name(bus_descriptor
);