summaryrefslogtreecommitdiff
path: root/fmrxproxy.cpp
blob: 77fcfb162b71d83ba1ee5231017d9dcb43b2b18a (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "fmrxproxy.h"

#include <QtDebug>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

// Cannot really use QtDbus because lack of support for FD passing, pity.

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

#define BUS_MATCH_RULE "type='signal',sender='" BUS_NAME "',interface='" BUS_INTERFACE "'"

FmRxProxy::FmRxProxy(QObject *parent) :
    QObject(parent)
{
	GError *gerr = NULL;
	DBusGConnection *gconn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &gerr);
	if (!gconn) {
		qWarning("Failed to connect to D-Bus: %s", gerr->message);
		g_error_free(gerr);
	}

	// From dbus-glib all we want is main loop integration,
	// the rest we use plain D-Bus.

	m_conn = dbus_g_connection_get_connection(gconn);
	dbus_bus_add_match(m_conn, BUS_MATCH_RULE, NULL);
	dbus_connection_add_filter(m_conn, bus_message_filter, this, NULL);
}

FmRxProxy::~FmRxProxy()
{
	dbus_bus_remove_match(m_conn, BUS_MATCH_RULE, NULL);
	dbus_connection_remove_filter(m_conn, bus_message_filter, this);
}

int FmRxProxy::Connect()
{
	DBusError err;
	dbus_bool_t ret;
	int fd;
	DBusMessage *reply;
	DBusMessage *msg = dbus_message_new_method_call(BUS_NAME, BUS_PATH,
		BUS_INTERFACE, "Connect");
	Q_ASSERT(msg != NULL);

	dbus_error_init(&err);

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

	if (!reply) {
		qWarning("Failed to Connect(): %s", err.message);
		dbus_error_free(&err);
		return -1;
	}

	ret = dbus_message_get_args(reply, &err, DBUS_TYPE_UNIX_FD, &fd, DBUS_TYPE_INVALID);
	dbus_message_unref(reply);

	if (!ret) {
		qWarning("Failed to parse Connect() reply: %s", err.message);
		dbus_error_free(&err);
		return -1;
	}


	return fd;
}

void FmRxProxy::Tune(double f)
{
	DBusError err;
	DBusMessage *reply;
	DBusMessage *msg = dbus_message_new_method_call(BUS_NAME, BUS_PATH,
												  BUS_INTERFACE, "Tune");
	Q_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(m_conn, msg, -1, &err);
	dbus_message_unref(msg);

	if (!reply) {
		qWarning("Failed to Tune(): %s", err.message);
		dbus_error_free(&err);
		return;
	}

	dbus_message_unref(reply);
}

void FmRxProxy::SearchForward()
{
	DBusMessage *msg = dbus_message_new_method_call(BUS_NAME, BUS_PATH,
		BUS_INTERFACE, "SearchForward");
	Q_ASSERT(msg != NULL);

	dbus_connection_send(m_conn, msg, NULL);
	dbus_message_unref(msg);
}

void FmRxProxy::SearchBackward()
{
	DBusMessage *msg = dbus_message_new_method_call(BUS_NAME, BUS_PATH,
		BUS_INTERFACE, "SearchBackward");
	Q_ASSERT(msg != NULL);

	dbus_connection_send(m_conn, msg, NULL);
	dbus_message_unref(msg);
}

DBusHandlerResult FmRxProxy::bus_message_filter(DBusConnection *,
	DBusMessage *m, void *user_data)
{
	FmRxProxy *self = reinterpret_cast<FmRxProxy*>(user_data);
	if (dbus_message_has_interface(m, BUS_INTERFACE)) {
		if (dbus_message_is_signal(m, BUS_INTERFACE, "Tuned")) {
			double freq;
			if (dbus_message_get_args(m, NULL, DBUS_TYPE_DOUBLE, &freq, DBUS_TYPE_INVALID)) {
				emit self->Tuned(freq);
			}
		} else if (dbus_message_is_signal(m, BUS_INTERFACE, "Stopped")) {
			emit self->Stopped();
		} else if (dbus_message_is_signal(m, BUS_INTERFACE, "PiReceived")) {
			quint16 pi;
			if (dbus_message_get_args(m, NULL, DBUS_TYPE_UINT16, &pi, DBUS_TYPE_INVALID)) {
				emit self->PiReceived(pi);
			}
		} else if (dbus_message_is_signal(m, BUS_INTERFACE, "PsReceived")) {
			char * s;
			if (dbus_message_get_args(m, NULL, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
				emit self->PsReceived(QString::fromUtf8(s));
			}
		} else if (dbus_message_is_signal(m, BUS_INTERFACE, "RtReceived")) {
			char * s;
			if (dbus_message_get_args(m, NULL, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
				emit self->RtReceived(QString::fromUtf8(s));
			}
		}
	}

	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}