summaryrefslogtreecommitdiff
path: root/fmrx-ctl.c
blob: 5738c99693433ea371a93d17dedfb1a9628996e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <unistd.h>
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

static GMainLoop *main_loop;
static DBusConnection *bus;

static gdouble cmd_tune = 0.0;
static gboolean cmd_next = FALSE;
static gboolean cmd_prev = FALSE;

static GOptionEntry entries[] = {
	{ "tune", 't', 0, G_OPTION_ARG_DOUBLE, &cmd_tune, "Tune to a specific frequency", "MHz" },
	{ "next", 'n', 0, G_OPTION_ARG_NONE, &cmd_next, "Forward scan for a signal", NULL },
	{ "prev", 'p', 0, G_OPTION_ARG_NONE, &cmd_prev, "Backward scan for a signal", NULL },
	{ NULL }
};

#define BUS_NAME      "com.javispedro.fmrxd"
#define BUS_PATH      "/com/javispedro/fmrxd"
#define BUS_INTERFACE BUS_NAME

static void tune(double f)
{
	DBusError err;
	DBusMessage *reply;
	DBusMessage *msg = dbus_message_new_method_call(BUS_NAME, BUS_PATH,
												  BUS_INTERFACE, "Tune");
	g_assert(msg != NULL);

	dbus_error_init(&err);

	dbus_message_append_args(msg, DBUS_TYPE_DOUBLE, &f, DBUS_TYPE_INVALID);

	reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err);
	dbus_message_unref(msg);

	if (!reply) {
		g_assert(dbus_error_is_set(&err));
		g_printerr("Tune error: %s", err.message);
		dbus_error_free(&err);
		return;
	}

	dbus_message_unref(reply);
}

static void next()
{
	DBusError err;
	DBusMessage *reply;
	DBusMessage *msg = dbus_message_new_method_call(BUS_NAME, BUS_PATH,
												  BUS_INTERFACE, "SearchForward");
	g_assert(msg != NULL);

	dbus_error_init(&err);

	reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err);
	dbus_message_unref(msg);

	if (!reply) {
		g_assert(dbus_error_is_set(&err));
		g_printerr("Tune error: %s", err.message);
		dbus_error_free(&err);
		return;
	}

	dbus_message_unref(reply);
}

static void prev()
{
	DBusError err;
	DBusMessage *reply;
	DBusMessage *msg = dbus_message_new_method_call(BUS_NAME, BUS_PATH,
												  BUS_INTERFACE, "SearchBackward");
	g_assert(msg != NULL);

	dbus_error_init(&err);

	reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err);
	dbus_message_unref(msg);

	if (!reply) {
		g_assert(dbus_error_is_set(&err));
		g_printerr("Tune error: %s", err.message);
		dbus_error_free(&err);
		return;
	}

	dbus_message_unref(reply);
}

static void dbus_init()
{
	DBusError err;	dbus_error_init(&err);
	bus = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
	g_assert(bus != NULL);
	g_assert(!dbus_error_is_set(&err));
	dbus_connection_setup_with_g_main(bus, g_main_loop_get_context(main_loop));
}

int main(int argc, char **argv)
{
	GError *error = NULL;
	GOptionContext *context = g_option_context_new("- control the fmrxd daemon");
	main_loop = g_main_loop_new(NULL, FALSE);

	g_option_context_add_main_entries(context, entries, NULL);
	if (!g_option_context_parse(context, &argc, &argv, &error)) {
		g_printerr("Option parsing failed: %s\n", error->message);
		return 1;
	}

	dbus_init();

	if (cmd_tune > 0.0) {
		tune(cmd_tune);
	}
	if (cmd_prev) {
		prev();
	}
	if (cmd_next) {
		next();
	}

	return 0;
}