|
| 1 | +#include "device.h" |
| 2 | +#include "common.h" |
| 3 | +#include "helper.h" |
| 4 | + |
| 5 | +#include <vdr/device.h> |
| 6 | + |
| 7 | + |
| 8 | +int cDBusDevices::_requestedPrimaryDevice = -1; |
| 9 | +cMutex cDBusDevices::_requestedPrimaryDeviceMutex; |
| 10 | + |
| 11 | +void cDBusDevices::SetPrimaryDeviceRequest(int index) |
| 12 | +{ |
| 13 | + cMutexLock MutexLock(&_requestedPrimaryDeviceMutex); |
| 14 | + _requestedPrimaryDevice = index; |
| 15 | +} |
| 16 | + |
| 17 | +int cDBusDevices::RequestedPrimaryDevice(bool clear) |
| 18 | +{ |
| 19 | + cMutexLock MutexLock(&_requestedPrimaryDeviceMutex); |
| 20 | + int ret = _requestedPrimaryDevice; |
| 21 | + if (clear) |
| 22 | + _requestedPrimaryDevice = -1; |
| 23 | + return ret; |
| 24 | +} |
| 25 | + |
| 26 | +namespace cDBusDevicesHelper |
| 27 | +{ |
| 28 | + static const char *_xmlNodeInfo = |
| 29 | + "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n" |
| 30 | + " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n" |
| 31 | + "<node>\n" |
| 32 | + " <interface name=\""DBUS_VDR_DEVICE_INTERFACE"\">\n" |
| 33 | + " <method name=\"GetPrimary\">\n" |
| 34 | + " <arg name=\"index\" type=\"i\" direction=\"out\"/>\n" |
| 35 | + " <arg name=\"number\" type=\"i\" direction=\"out\"/>\n" |
| 36 | + " <arg name=\"hasDecoder\" type=\"b\" direction=\"out\"/>\n" |
| 37 | + " <arg name=\"isPrimary\" type=\"b\" direction=\"out\"/>\n" |
| 38 | + " <arg name=\"name\" type=\"s\" direction=\"out\"/>\n" |
| 39 | + " </method>\n" |
| 40 | + " <method name=\"RequestPrimary\">\n" |
| 41 | + " <arg name=\"index\" type=\"i\" direction=\"in\"/>\n" |
| 42 | + " </method>\n" |
| 43 | + " <method name=\"List\">\n" |
| 44 | + " <arg name=\"device\" type=\"a(iibbs)\" direction=\"out\"/>\n" |
| 45 | + " </method>\n" |
| 46 | + " </interface>\n" |
| 47 | + "</node>\n"; |
| 48 | + |
| 49 | + static void AddDevice(GVariantBuilder *Variant, const cDevice *Device, bool AsStruct) |
| 50 | + { |
| 51 | + gint32 index = -1; |
| 52 | + gint32 number = -1; |
| 53 | + gboolean hasDecoder = FALSE; |
| 54 | + gboolean isPrimary = FALSE; |
| 55 | + cString name = ""; |
| 56 | + if (Device != NULL) { |
| 57 | + index = Device->CardIndex(); |
| 58 | + number = Device->DeviceNumber(); |
| 59 | + hasDecoder = Device->HasDecoder(); |
| 60 | + isPrimary = Device->IsPrimaryDevice(); |
| 61 | + name = Device->DeviceName(); |
| 62 | + cDBusHelper::ToUtf8(name); |
| 63 | + } |
| 64 | + |
| 65 | + if (AsStruct) |
| 66 | + g_variant_builder_add(Variant, "(iibbs)", index, number, hasDecoder, isPrimary, *name); |
| 67 | + else { |
| 68 | + g_variant_builder_add(Variant, "i", index); |
| 69 | + g_variant_builder_add(Variant, "i", number); |
| 70 | + g_variant_builder_add(Variant, "b", hasDecoder); |
| 71 | + g_variant_builder_add(Variant, "b", isPrimary); |
| 72 | + g_variant_builder_add(Variant, "s", *name); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + static void GetPrimary(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation) |
| 77 | + { |
| 78 | + GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(iibbs)")); |
| 79 | + const cDevice *dev = cDevice::PrimaryDevice(); |
| 80 | + AddDevice(builder, dev, false); |
| 81 | + g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder)); |
| 82 | + g_variant_builder_unref(builder); |
| 83 | + } |
| 84 | + |
| 85 | + static void RequestPrimary(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation) |
| 86 | + { |
| 87 | + gint32 index = -1; |
| 88 | + g_variant_get(Parameters, "(i)", &index); |
| 89 | + cDBusDevices::SetPrimaryDeviceRequest(index); |
| 90 | + g_dbus_method_invocation_return_value(Invocation, NULL); |
| 91 | + } |
| 92 | + |
| 93 | + static void List(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation) |
| 94 | + { |
| 95 | + GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("a(iibbs)")); |
| 96 | + |
| 97 | + for (int i = 0; i < cDevice::NumDevices(); i++) { |
| 98 | + const cDevice *dev = cDevice::GetDevice(i); |
| 99 | + if (dev != NULL) |
| 100 | + AddDevice(array, dev, true); |
| 101 | + } |
| 102 | + |
| 103 | + GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(a(iibbs))")); |
| 104 | + g_variant_builder_add_value(builder, g_variant_builder_end(array)); |
| 105 | + g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder)); |
| 106 | + g_variant_builder_unref(array); |
| 107 | + g_variant_builder_unref(builder); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +cDBusDevices::cDBusDevices(void) |
| 113 | +:cDBusObject("/Devices", cDBusDevicesHelper::_xmlNodeInfo) |
| 114 | +{ |
| 115 | + AddMethod("GetPrimary", cDBusDevicesHelper::GetPrimary); |
| 116 | + AddMethod("RequestPrimary", cDBusDevicesHelper::RequestPrimary); |
| 117 | + AddMethod("List", cDBusDevicesHelper::List); |
| 118 | +} |
| 119 | + |
| 120 | +cDBusDevices::~cDBusDevices(void) |
| 121 | +{ |
| 122 | +} |
0 commit comments