hgame.c (16079c7233be8bd6c88e3421a70c7ca87cfea370) hgame.c (51b2216114546bb13b13002b29fb1263e600a0fc)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5 * Copyright (c) 2020 Greg V <greg@unrelenting.technology>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 38 unchanged lines hidden (view full) ---

47#include <dev/hid/hidbus.h>
48#include <dev/hid/hidquirk.h>
49#include <dev/hid/hidmap.h>
50
51#define HGAME_MAP_BRG(number_from, number_to, code) \
52 { HIDMAP_KEY_RANGE(HUP_BUTTON, number_from, number_to, code) }
53#define HGAME_MAP_ABS(usage, code) \
54 { HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code) }
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5 * Copyright (c) 2020 Greg V <greg@unrelenting.technology>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 38 unchanged lines hidden (view full) ---

47#include <dev/hid/hidbus.h>
48#include <dev/hid/hidquirk.h>
49#include <dev/hid/hidmap.h>
50
51#define HGAME_MAP_BRG(number_from, number_to, code) \
52 { HIDMAP_KEY_RANGE(HUP_BUTTON, number_from, number_to, code) }
53#define HGAME_MAP_ABS(usage, code) \
54 { HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code) }
55#define HGAME_MAP_GCB(usage, callback) \
56 { HIDMAP_ANY_CB(HUP_GENERIC_DESKTOP, HUG_##usage, callback) }
55#define HGAME_MAP_CRG(usage_from, usage_to, callback) \
56 { HIDMAP_ANY_CB_RANGE(HUP_GENERIC_DESKTOP, \
57 HUG_##usage_from, HUG_##usage_to, callback) }
58#define HGAME_FINALCB(cb) \
59 { HIDMAP_FINAL_CB(&cb) }
60
61static const struct hidmap_item hgame_map[] = {
62 HGAME_MAP_BRG(1, 16, BTN_TRIGGER),
63 HGAME_MAP_ABS(X, ABS_X),
64 HGAME_MAP_ABS(Y, ABS_Y),
65 HGAME_MAP_ABS(Z, ABS_Z),
66 HGAME_MAP_ABS(RX, ABS_RX),
67 HGAME_MAP_ABS(RY, ABS_RY),
68 HGAME_MAP_ABS(RZ, ABS_RZ),
57#define HGAME_MAP_CRG(usage_from, usage_to, callback) \
58 { HIDMAP_ANY_CB_RANGE(HUP_GENERIC_DESKTOP, \
59 HUG_##usage_from, HUG_##usage_to, callback) }
60#define HGAME_FINALCB(cb) \
61 { HIDMAP_FINAL_CB(&cb) }
62
63static const struct hidmap_item hgame_map[] = {
64 HGAME_MAP_BRG(1, 16, BTN_TRIGGER),
65 HGAME_MAP_ABS(X, ABS_X),
66 HGAME_MAP_ABS(Y, ABS_Y),
67 HGAME_MAP_ABS(Z, ABS_Z),
68 HGAME_MAP_ABS(RX, ABS_RX),
69 HGAME_MAP_ABS(RY, ABS_RY),
70 HGAME_MAP_ABS(RZ, ABS_RZ),
69 HGAME_MAP_ABS(HAT_SWITCH, ABS_HAT0X),
71 HGAME_MAP_GCB(HAT_SWITCH, hgame_hat_switch_cb),
70 HGAME_MAP_CRG(D_PAD_UP, D_PAD_LEFT, hgame_dpad_cb),
71 HGAME_MAP_BRG(17, 57, BTN_TRIGGER_HAPPY),
72 HGAME_FINALCB( hgame_final_cb),
73};
74
75static const struct hid_device_id hgame_devs[] = {
76 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_JOYSTICK),
77 HID_DRIVER_INFO(HUG_JOYSTICK) },
78 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_GAME_PAD),
79 HID_DRIVER_INFO(HUG_GAME_PAD) },
80};
81
72 HGAME_MAP_CRG(D_PAD_UP, D_PAD_LEFT, hgame_dpad_cb),
73 HGAME_MAP_BRG(17, 57, BTN_TRIGGER_HAPPY),
74 HGAME_FINALCB( hgame_final_cb),
75};
76
77static const struct hid_device_id hgame_devs[] = {
78 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_JOYSTICK),
79 HID_DRIVER_INFO(HUG_JOYSTICK) },
80 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_GAME_PAD),
81 HID_DRIVER_INFO(HUG_GAME_PAD) },
82};
83
84int
85hgame_hat_switch_cb(HIDMAP_CB_ARGS)
86{
87 static const struct { int32_t x; int32_t y; } hat_switch_map[] = {
88 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0},
89 {-1, -1},{0, 0}
90 };
91 struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV();
92 u_int idx;
93
94 switch (HIDMAP_CB_GET_STATE()) {
95 case HIDMAP_CB_IS_ATTACHING:
96 evdev_support_event(evdev, EV_ABS);
97 evdev_support_abs(evdev, ABS_HAT0X, -1, 1, 0, 0, 0);
98 evdev_support_abs(evdev, ABS_HAT0Y, -1, 1, 0, 0, 0);
99 break;
100
101 case HIDMAP_CB_IS_RUNNING:
102 idx = MIN(nitems(hat_switch_map) - 1, (u_int)ctx.data);
103 evdev_push_abs(evdev, ABS_HAT0X, hat_switch_map[idx].x);
104 evdev_push_abs(evdev, ABS_HAT0Y, hat_switch_map[idx].y);
105 break;
106
107 default:
108 break;
109 }
110
111 return (0);
112}
113
82/*
83 * Emulate the hat switch report via the D-pad usages
84 * found on XInput/XBox style devices
85 */
86int
87hgame_dpad_cb(HIDMAP_CB_ARGS)
88{
89 struct hgame_softc *sc = HIDMAP_CB_GET_SOFTC();

--- 115 unchanged lines hidden ---
114/*
115 * Emulate the hat switch report via the D-pad usages
116 * found on XInput/XBox style devices
117 */
118int
119hgame_dpad_cb(HIDMAP_CB_ARGS)
120{
121 struct hgame_softc *sc = HIDMAP_CB_GET_SOFTC();

--- 115 unchanged lines hidden ---