more work on managing four interfaces as separate PIDs, new makefile rules for buildi...
[systembsd.git] / src / interfaces / localed / localed.c
CommitLineData
3b82e3c1 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
5b005882 17#include <unistd.h>
18#include <limits.h>
19
20#include <glib.h>
21#include <gio/gio.h>
22
23#include "localed.h"
24#include "localed-gen.c"
25
26GPtrArray *localed_freeable;
27GDBusNodeInfo *spect_data;
28localedLocale1 *localed_interf;
29
30/* begin method/property/signal code */
31
32/* TODO make sure these guys only work if called by root */
33
34/* Example:
35static gboolean
36on_handle_set_hostname(localedLocale1 *hn1_passed_interf,
37 GDBusMethodInvocation *invoc,
38 const gchar *greet,
39 gpointer data) {
40 return FALSE;
41} */
42
43/* note: all localed/locale1's properties are read-only,
44 * and do not need set_ functions, gdbus-codegen realized
45 * this from the XML and handled the to-be error of trying
46 * to set a read-only property's value
47 */
48
49/* Example:
50
51const gchar *
52our_get_hostname() {
53
54 return "TODO";
55}*/
56
57/* end method/property/signal code, begin bus/name handlers */
58
59static void localed_on_bus_acquired(GDBusConnection *conn,
60 const gchar *name,
61 gpointer user_data) {
62
63 g_print("got bus, name: %s\n", name);
64
65}
66
67static void localed_on_name_acquired(GDBusConnection *conn,
68 const gchar *name,
69 gpointer user_data) {
70
71 g_print("got '%s' on system bus\n", name);
72
73 localed_interf = localed_locale1_skeleton_new();
74
75 /* attach function pointers to generated struct's method handlers */
76 /*g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
77 g_signal_connect(localed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
78 g_signal_connect(localed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
79 g_signal_connect(localed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
80 g_signal_connect(localed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); */
81
82 /* set our properties before export */
83 /*localed_locale1_set_hostname(localed_interf, our_get_hostname());
84 localed_locale1_set_static_hostname(localed_interf, our_get_static_hostname());
85 localed_locale1_set_pretty_hostname(localed_interf, our_get_pretty_hostname());
86 localed_locale1_set_chassis(localed_interf, our_get_chassis());
87 localed_locale1_set_icon_name(localed_interf, our_get_icon_name());
88 localed_locale1_set_kernel_name(localed_interf, our_get_kernel_name());
89 localed_locale1_set_kernel_version(localed_interf, our_get_kernel_version());
90 localed_locale1_set_kernel_release(localed_interf, our_get_kernel_release());
91 localed_locale1_set_operating_system_cpename(localed_interf, our_get_os_cpename());
92 localed_locale1_set_operating_system_pretty_name(localed_interf, our_get_os_pretty_name()); */
93
94 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf),
95 conn,
96 "/org/freedesktop/locale1",
97 NULL)) {
98
99 g_printf("failed to export locale1's interface on system bus!");
100 }
101
102}
103
104static void localed_on_name_lost(GDBusConnection *conn,
105 const gchar *name,
106 gpointer user_data) {
107
108 g_print("lost name %s, exiting...", name);
109
110 localed_mem_clean();
111 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(localed_interf));
112
113 /* TODO exit through g_main_loop properly... */
114}
115
116/* safe call to try and start localed */
7f0a0212 117void localed_init() {
5b005882 118
119 guint bus_descriptor;
120 GError *err = NULL;
121 gchar **localed_ispect_xml;
122 gchar *localed_joined_xml;
123
124 localed_freeable = g_ptr_array_new();
125 localed_ispect_xml = g_malloc(3000);
126
127 g_file_get_contents("conf/localed-ispect.xml", localed_ispect_xml, NULL, NULL);
128 localed_joined_xml = g_strjoinv("\n", localed_ispect_xml);
129 spect_data = g_dbus_node_info_new_for_xml(localed_joined_xml, NULL);
130
131 g_free(localed_ispect_xml);
132 g_ptr_array_add(localed_freeable, localed_joined_xml);
133
134 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
135 "org.freedesktop.locale1",
136 G_BUS_NAME_OWNER_FLAGS_NONE,
137 localed_on_bus_acquired,
138 localed_on_name_acquired,
139 localed_on_name_lost,
140 NULL,
141 NULL);
142
143 /* TODO: malloc and return reference as if a main() closed */
5b005882 144}
145
146/* free()'s */
147void localed_mem_clean() {
148
149 g_ptr_array_foreach(localed_freeable, (GFunc) g_free, NULL);
150}
151
152/* TODO figure out DMI variables on obsd */
153/*static gchar *guess_icon_name() {
154
155 gchar *filebuf = NULL;
156 gchar *ret = NULL;
157
158 #if defined(__i386__) || defined(__x86_64__)
159
160 Taken with a few minor changes from systemd's localed.c,
161 copyright 2011 Lennart Poettering.
162
163 See the SMBIOS Specification 2.7.1 section 7.4.1 for
164 details about the values listed here:
165
166 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
167
168
169 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
170 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
171 case 0x3:
172 case 0x4:
173 case 0x5:
174 case 0x6:
175 case 0x7:
176 ret = g_strdup ("computer-desktop");
177 goto out;
178 case 0x9:
179 case 0xA:
180 case 0xE:
181 ret = g_strdup ("computer-laptop");
182 goto out;
183 case 0x11:
184 case 0x17:
185 case 0x1C:
186 case 0x1D:
187 ret = g_strdup ("computer-server");
188 goto out;
189 }
190 }
191 #endif
192 ret = g_strdup ("computer");
193 out:
194 g_free (filebuf);
195 return ret;
196}*/