-
Notifications
You must be signed in to change notification settings - Fork 3
/
device.c
135 lines (118 loc) · 4.71 KB
/
device.c
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
#include "device.h"
#include "common.h"
#include "helper.h"
#include <vdr/device.h>
int cDBusDevices::_requestedPrimaryDevice = -1;
cMutex cDBusDevices::_requestedPrimaryDeviceMutex;
int cDBusDevices::_nulldeviceIndex = -1;
void cDBusDevices::SetPrimaryDeviceRequest(int index)
{
cMutexLock MutexLock(&_requestedPrimaryDeviceMutex);
_requestedPrimaryDevice = index;
}
int cDBusDevices::RequestedPrimaryDevice(bool clear)
{
cMutexLock MutexLock(&_requestedPrimaryDeviceMutex);
int ret = _requestedPrimaryDevice;
if (clear)
_requestedPrimaryDevice = -1;
return ret;
}
namespace cDBusDevicesHelper
{
static const char *_xmlNodeInfo =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\""DBUS_VDR_DEVICE_INTERFACE"\">\n"
" <method name=\"GetPrimary\">\n"
" <arg name=\"index\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"number\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"hasDecoder\" type=\"b\" direction=\"out\"/>\n"
" <arg name=\"isPrimary\" type=\"b\" direction=\"out\"/>\n"
" <arg name=\"name\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"GetNullDevice\">\n"
" <arg name=\"index\" type=\"i\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"RequestPrimary\">\n"
" <arg name=\"index\" type=\"i\" direction=\"in\"/>\n"
" </method>\n"
" <method name=\"List\">\n"
" <arg name=\"device\" type=\"a(iibbs)\" direction=\"out\"/>\n"
" </method>\n"
" </interface>\n"
"</node>\n";
static void AddDevice(GVariantBuilder *Variant, const cDevice *Device, bool AsStruct)
{
gint32 index = -1;
gint32 number = -1;
gboolean hasDecoder = FALSE;
gboolean isPrimary = FALSE;
cString name = "";
if (Device != NULL) {
index = Device->CardIndex();
number = Device->DeviceNumber();
hasDecoder = Device->HasDecoder();
isPrimary = Device->IsPrimaryDevice();
name = Device->DeviceName();
cDBusHelper::ToUtf8(name);
}
if (AsStruct)
g_variant_builder_add(Variant, "(iibbs)", index, number, hasDecoder, isPrimary, *name);
else {
g_variant_builder_add(Variant, "i", index);
g_variant_builder_add(Variant, "i", number);
g_variant_builder_add(Variant, "b", hasDecoder);
g_variant_builder_add(Variant, "b", isPrimary);
g_variant_builder_add(Variant, "s", *name);
}
}
static void GetPrimary(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(iibbs)"));
const cDevice *dev = cDevice::PrimaryDevice();
AddDevice(builder, dev, false);
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(builder);
}
static void GetNullDevice(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(i)"));
g_variant_builder_add(builder, "i", cDBusDevices::_nulldeviceIndex);
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(builder);
}
static void RequestPrimary(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
gint32 index = -1;
g_variant_get(Parameters, "(i)", &index);
cDBusDevices::SetPrimaryDeviceRequest(index);
g_dbus_method_invocation_return_value(Invocation, NULL);
}
static void List(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("a(iibbs)"));
for (int i = 0; i < cDevice::NumDevices(); i++) {
const cDevice *dev = cDevice::GetDevice(i);
if (dev != NULL)
AddDevice(array, dev, true);
}
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(a(iibbs))"));
g_variant_builder_add_value(builder, g_variant_builder_end(array));
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(array);
g_variant_builder_unref(builder);
}
}
cDBusDevices::cDBusDevices(void)
:cDBusObject("/Devices", cDBusDevicesHelper::_xmlNodeInfo)
{
AddMethod("GetPrimary", cDBusDevicesHelper::GetPrimary);
AddMethod("GetNullDevice", cDBusDevicesHelper::GetNullDevice);
AddMethod("RequestPrimary", cDBusDevicesHelper::RequestPrimary);
AddMethod("List", cDBusDevicesHelper::List);
}
cDBusDevices::~cDBusDevices(void)
{
}