summaryrefslogtreecommitdiff
path: root/src/feature
diff options
context:
space:
mode:
Diffstat (limited to 'src/feature')
-rw-r--r--src/feature/battery.c148
-rw-r--r--src/feature/battery.h32
-rw-r--r--src/feature/reprogrammable.c162
-rw-r--r--src/feature/reprogrammable.h32
4 files changed, 374 insertions, 0 deletions
diff --git a/src/feature/battery.c b/src/feature/battery.c
new file mode 100644
index 0000000..7e2ed26
--- /dev/null
+++ b/src/feature/battery.c
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2012
+ * Lars-Dominik Braun <lars@6xq.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include "battery.h"
+#include "usb.h"
+#include "device.h"
+
+typedef enum {
+ HIDPP_BATTERY_STATUS_DISCHARGING = 0,
+ HIDPP_BATTERY_STATUS_RECHARGING = 1,
+ HIDPP_BATTERY_STATUS_CHARGE_FINAL = 2,
+ HIDPP_BATTERY_STATUS_CHARGE_COMPLETE = 3,
+ HIDPP_BATTERY_STATUS_RECHARGING_SLOW = 4,
+ HIDPP_BATTERY_STATUS_INVALID_BATTERY = 5,
+ HIDPP_BATTERY_STATUS_THERMAL_ERROR = 6,
+ HIDPP_BATTERY_STATUS_CHARGE_ERROR = 7,
+} hidppBatteryStatus_t;
+
+typedef enum {
+ HIDPP_BATTERY_FLAGS_DISABLED = (1 << 0),
+ HIDPP_BATTERY_FLAGS_MILEAGE = (1 << 1),
+ HIDPP_BATTERY_FLAGS_RECHARGEABLE = (1 << 2),
+} hidppBatteryFlags_t;
+
+typedef struct {
+ uint8_t level, nextLevel, criticalLevel; /* in percent */
+ hidppBatteryStatus_t status;
+ uint8_t levelsCount;
+ hidppBatteryFlags_t flags;
+ uint16_t life;
+} hidppBattery_t;
+
+const char *hidppBatteryStatusToStr (const hidppBatteryStatus_t s) {
+ switch (s) {
+ case HIDPP_BATTERY_STATUS_DISCHARGING:
+ return "discharging";
+ break;
+
+ case HIDPP_BATTERY_STATUS_RECHARGING:
+ return "recharging";
+ break;
+
+ case HIDPP_BATTERY_STATUS_CHARGE_FINAL:
+ return "charge in final state";
+ break;
+
+ case HIDPP_BATTERY_STATUS_CHARGE_COMPLETE:
+ return "charge complete";
+ break;
+
+ case HIDPP_BATTERY_STATUS_RECHARGING_SLOW:
+ return "recharging below optimal speed";
+ break;
+
+ case HIDPP_BATTERY_STATUS_INVALID_BATTERY:
+ return "invalid battery";
+ break;
+
+ case HIDPP_BATTERY_STATUS_THERMAL_ERROR:
+ return "thermal error";
+ break;
+
+ case HIDPP_BATTERY_STATUS_CHARGE_ERROR:
+ return "charge error";
+ break;
+
+ default:
+ return "unkown";
+ break;
+ }
+}
+
+void hidppBatteryPrint (const void * const data) {
+ const hidppBattery_t * const b = data;
+
+ assert (b != NULL);
+
+ printf (" Battery:\n"
+ " Status: %s (%u)\n"
+ " Level: %u%%\n"
+ " Next level: %u%%\n"
+ " Critical level: %u%%\n"
+ " Number of levels: %u\n"
+ " Flags:%s%s%s (0x%x)\n"
+ " Life: %u\n",
+ hidppBatteryStatusToStr (b->status), b->status,
+ b->level, b->nextLevel, b->criticalLevel, b->levelsCount,
+ (b->flags & HIDPP_BATTERY_FLAGS_DISABLED) ? " disabled" : "",
+ (b->flags & HIDPP_BATTERY_FLAGS_MILEAGE) ? " mileage" : "",
+ (b->flags & HIDPP_BATTERY_FLAGS_RECHARGEABLE) ? " rechargeable" : "",
+ b->flags,
+ b->life);
+}
+
+void hidppBatteryGet (hidppDevice_t * const d, void ** const data) {
+ hidppBattery_t *b;
+ hidppPacket_t rp;
+
+ assert (d != NULL);
+
+ b = calloc (1, sizeof (*b));
+
+ hidppUsbRequest (d, HIDPP_FEATURE_BATTERY_STATUS, 0x0, NULL, 0, &rp);
+ b->level = rp.args[0];
+ b->nextLevel = rp.args[1];
+
+ hidppUsbRequest (d, HIDPP_FEATURE_BATTERY_STATUS, 0x1, NULL, 0, &rp);
+ b->levelsCount = rp.args[0];
+ b->flags = rp.args[1];
+ b->life = (rp.args[2] << 8) | rp.args[3];
+ b->criticalLevel = rp.args[4];
+
+ *data = b;
+}
+
+bool hidppBatteryAvailable (const hidppDevice_t * const d) {
+ uint8_t ret;
+ return hidppFeatureIdToIdx (d, HIDPP_FEATURE_BATTERY_STATUS, &ret);
+}
+
+hidppModule_t moduleBattery = {hidppBatteryAvailable, hidppBatteryGet,
+ hidppBatteryPrint};
+
diff --git a/src/feature/battery.h b/src/feature/battery.h
new file mode 100644
index 0000000..d53c0bf
--- /dev/null
+++ b/src/feature/battery.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2012
+ * Lars-Dominik Braun <lars@6xq.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef _FEATURE_BATTERY_H
+#define _FEATURE_BATTERY_H
+
+#include "common.h"
+
+extern hidppModule_t moduleBattery;
+
+#endif /* _FEATURE_BATTERY_H */
+
diff --git a/src/feature/reprogrammable.c b/src/feature/reprogrammable.c
new file mode 100644
index 0000000..2c3b61d
--- /dev/null
+++ b/src/feature/reprogrammable.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2012
+ * Lars-Dominik Braun <lars@6xq.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include "reprogrammable.h"
+#include "usb.h"
+#include "device.h"
+
+typedef enum {
+ HIDPP_TASK_VOLUP = 1,
+ HIDPP_TASK_VOLDOWN = 2,
+ HIDPP_TASK_MUTE = 3,
+ HIDPP_TASK_PLAYPAUSE = 4,
+ HIDPP_TASK_NEXTFF = 5,
+ HIDPP_TASK_PREVRW = 6,
+ HIDPP_TASK_STOP = 7,
+ HIDPP_TASK_APPSWITCH = 8,
+} hidppReprogrammableKeyTask_t;
+
+typedef enum {
+ /* reprogrammmable */
+ HIDPP_RK_REPGROGRAMMABLE = (1 << 4),
+ /* affected by fn key */
+ HIDPP_RK_FNTOGGLE = (1 << 3),
+ /* hotkey */
+ HIDPP_RK_HOTKEY = (1 << 2),
+ /* fn key */
+ HIDPP_RK_FN = (1 << 1),
+ /* mouse button */
+ HIDPP_RK_MOUSE = (1 << 0),
+} hidppReprogrammableKeyFlags_t;
+
+typedef struct {
+ uint16_t id;
+ hidppReprogrammableKeyTask_t task;
+ hidppReprogrammableKeyFlags_t flags;
+} hidppReprogrammableKey_t;
+
+typedef struct {
+ hidppReprogrammableKey_t *key;
+ size_t count;
+} hidppReprogrammableData_t;
+
+const char *hidppReprogrammableTaskToStr (const hidppReprogrammableKeyTask_t t) {
+ switch (t) {
+ case HIDPP_TASK_VOLUP:
+ return "volume up";
+ break;
+
+ case HIDPP_TASK_VOLDOWN:
+ return "volume down";
+ break;
+
+ case HIDPP_TASK_MUTE:
+ return "mute";
+ break;
+
+ case HIDPP_TASK_PLAYPAUSE:
+ return "play/pause";
+ break;
+
+ case HIDPP_TASK_NEXTFF:
+ return "next/fast forward";
+ break;
+
+ case HIDPP_TASK_PREVRW:
+ return "previous/rewind";
+ break;
+
+ case HIDPP_TASK_STOP:
+ return "stop";
+ break;
+
+ case HIDPP_TASK_APPSWITCH:
+ return "application switcher";
+ break;
+
+ default:
+ return "unknown";
+ break;
+ }
+}
+
+void hidppReprogrammablePrint (const void * const data) {
+ const hidppReprogrammableData_t * const d = data;
+
+ assert (d != NULL);
+
+ for (size_t i = 0; i < d->count; i++) {
+ const hidppReprogrammableKey_t * const k = &d->key[i];
+ printf (" Key: <name> (0x%x)\n"
+ " Task: %s (%u)\n"
+ " Flags:%s%s%s%s%s (0x%x)\n",
+ k->id,
+ hidppReprogrammableTaskToStr (k->task), k->task,
+ (k->flags & HIDPP_RK_REPGROGRAMMABLE) ? " reprogrammable" : "",
+ (k->flags & HIDPP_RK_FNTOGGLE) ? " fntoggle" : "",
+ (k->flags & HIDPP_RK_HOTKEY) ? " hotkey" : "",
+ (k->flags & HIDPP_RK_FN) ? " fn" : "",
+ (k->flags & HIDPP_RK_MOUSE) ? " mouse" : "",
+ k->flags);
+ }
+}
+
+void hidppReprogrammableGet (hidppDevice_t *d, void ** const retData) {
+ hidppReprogrammableData_t *data;
+ hidppPacket_t rp;
+
+ assert (d != NULL);
+ assert (retData != NULL);
+
+ data = calloc (1, sizeof (*data));
+
+ hidppUsbRequest (d, HIDPP_FEATURE_PROGRAMMABLE_KEYS, 0x0, NULL, 0, &rp);
+ data->count = rp.args[0];
+ data->key = calloc (data->count, sizeof (*data->key));
+
+ for (uint8_t i = 0; i < data->count; i++) {
+ hidppReprogrammableKey_t * const k = &data->key[i];
+
+ hidppUsbRequest (d, HIDPP_FEATURE_PROGRAMMABLE_KEYS, 0x1, &i, 1, &rp);
+
+ k->id = (rp.args[0] << 8) | rp.args[1];
+ k->task = (rp.args[2] << 8) | rp.args[3];
+ k->flags = rp.args[4];
+ }
+
+ *retData = data;
+}
+
+bool hidppReprogrammableAvailable (const hidppDevice_t * const d) {
+ uint8_t ret;
+ return hidppFeatureIdToIdx (d, HIDPP_FEATURE_PROGRAMMABLE_KEYS, &ret);
+}
+
+hidppModule_t moduleReprogrammable = {hidppReprogrammableAvailable,
+ hidppReprogrammableGet, hidppReprogrammablePrint};
+
diff --git a/src/feature/reprogrammable.h b/src/feature/reprogrammable.h
new file mode 100644
index 0000000..90ec325
--- /dev/null
+++ b/src/feature/reprogrammable.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2012
+ * Lars-Dominik Braun <lars@6xq.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef _FEATURE_REPROGRAMMABLE_H
+#define _FEATURE_REPROGRAMMABLE_H
+
+#include "common.h"
+
+extern hidppModule_t moduleReprogrammable;
+
+#endif /* _FEATURE_REPROGRAMMABLE_H */
+