Linux Input Archive on lore.kernel.org
 help / color / Atom feed
From: Daniel Morse <dmorse@speedfox.co.uk>
To: linux-input@vger.kernel.org, dh.herrmann@googlemail.com
Subject: [PATCH[ HID: Wiimote: Treat the d-pad as an analogue stick
Date: Sat, 23 May 2020 23:02:51 +0100
Message-ID: <CANFaMLGVGmS4i3fdH2rYeqSgqk3Gm=sxaLLMuKx4T1eY9ZvyEg@mail.gmail.com> (raw)

The controllers from the Super Nintendo Classic Edition (AKA the SNES
Mini) appear as a Classic Controller Pro when connected to a Wii
Remote. All the buttons work as the same, with the d-pad being mapped
the same as the d-pad on the Classic Controller Pro. This differs from
the behaviour of most controllers with d-pads and no analogue sticks,
where the d-pad maps to ABS_HAT1X for left and right, and ABS_HAT1Y
for up and down. This patch adds an option to the hid-wiimote module
to make the Super Nintendo Classic Controller behave this way.

The patch has been tested with a Super Nintendo Classic Controller
plugged into a Wii Remote in both with the option both enabled and
disabled. When enabled the d-pad acts as the analogue control, and
when disabled it acts as it did before the patch was applied. This
patch has not been tested with e Wii Classic Controller (either the
original or the pro version) as I do not have one of these
controllers.

Although I have not tested it with these controllers, I think it is
likely this patch will also work with the NES Classic Edition
Controllers.

Signed-off-by: Daniel G. Morse <dmorse@speedfox.co.uk>
---
 drivers/hid/hid-wiimote-core.c    |  6 ++
 drivers/hid/hid-wiimote-modules.c | 98 +++++++++++++++++++++++--------
 drivers/hid/hid-wiimote.h         |  1 +
 3 files changed, 82 insertions(+), 23 deletions(-)

diff --git a/drivers/hid/hid-wiimote-core.c b/drivers/hid/hid-wiimote-core.c
index 92874dbe4d4a..4e75d7b7446f 100644
--- a/drivers/hid/hid-wiimote-core.c
+++ b/drivers/hid/hid-wiimote-core.c
@@ -1870,6 +1870,12 @@ static const struct hid_device_id
wiimote_hid_devices[] = {
                 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
     { }
 };
+
+bool dpad_as_analog = false;
+module_param(dpad_as_analog, bool, 0644);
+MODULE_PARM_DESC(dpad_as_analog,
+        "Treats the D-Pad as the main analog input");
+
 MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);

 static struct hid_driver wiimote_hid_driver = {
diff --git a/drivers/hid/hid-wiimote-modules.c
b/drivers/hid/hid-wiimote-modules.c
index 2c3925357857..2c491c92cd8e 100644
--- a/drivers/hid/hid-wiimote-modules.c
+++ b/drivers/hid/hid-wiimote-modules.c
@@ -1110,19 +1110,85 @@ static void wiimod_classic_in_ext(struct
wiimote_data *wdata, const __u8 *ext)
     rt <<= 1;
     lt <<= 1;

-    input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20);
-    input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20);
     input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20);
     input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20);
     input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
     input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
+    if(dpad_as_analog) {
+        if(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE){
+            if((ext[4] & 0x80) && !(ext[1] & 0x01)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1X, 0x7F);
+            } else if(!(ext[4] & 0x80) && (ext[1] & 0x01)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1X, 0xFF);
+            } else {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1X, 0x00);
+            }
+
+            if((ext[4] & 0x40) && !(ext[0] & 0x01)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1Y, 0x7F);
+            } else if(!(ext[4] & 0x40) && (ext[0] & 0x01)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1Y, 0xFF);
+            } else {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1Y, 0x00);
+            }
+        } else {
+            if((ext[4] & 0x80) && !(ext[5] & 0x02)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1X, 0x7F);
+            } else if(!(ext[4] & 0x80) && (ext[5] & 0x02)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1X, 0xFF);
+            } else {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1X, 0x00);
+            }
+
+            if((ext[4] & 0x40) && !(ext[5] & 0x01)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1Y, 0x7F);
+            } else if(!(ext[4] & 0x40) && (ext[5] & 0x01)) {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1Y, 0xFF);
+            } else {
+                input_report_abs(wdata->extension.input,
+                        ABS_HAT1Y, 0x00);
+            }
+        }
+
+    } else {
+        input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20);
+        input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20);
+        input_report_key(wdata->extension.input,
+                 wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
+                 !(ext[4] & 0x80));
+        input_report_key(wdata->extension.input,
+                 wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
+                 !(ext[4] & 0x40));
+
+        if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
+            input_report_key(wdata->extension.input,
+                 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
+                 !(ext[1] & 0x01));
+            input_report_key(wdata->extension.input,
+                 wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
+                 !(ext[0] & 0x01));
+        } else {
+            input_report_key(wdata->extension.input,
+                 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
+                 !(ext[5] & 0x02));
+            input_report_key(wdata->extension.input,
+                 wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
+                 !(ext[5] & 0x01));
+        }
+    }
+

-    input_report_key(wdata->extension.input,
-             wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
-             !(ext[4] & 0x80));
-    input_report_key(wdata->extension.input,
-             wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
-             !(ext[4] & 0x40));
     input_report_key(wdata->extension.input,
              wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT],
              !(ext[4] & 0x20));
@@ -1157,21 +1223,7 @@ static void wiimod_classic_in_ext(struct
wiimote_data *wdata, const __u8 *ext)
              wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR],
              !(ext[5] & 0x04));

-    if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
-        input_report_key(wdata->extension.input,
-             wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
-             !(ext[1] & 0x01));
-        input_report_key(wdata->extension.input,
-             wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
-             !(ext[0] & 0x01));
-    } else {
-        input_report_key(wdata->extension.input,
-             wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
-             !(ext[5] & 0x02));
-        input_report_key(wdata->extension.input,
-             wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
-             !(ext[5] & 0x01));
-    }
+

     input_sync(wdata->extension.input);
 }
diff --git a/drivers/hid/hid-wiimote.h b/drivers/hid/hid-wiimote.h
index b2a26a0a8f12..0079801f599c 100644
--- a/drivers/hid/hid-wiimote.h
+++ b/drivers/hid/hid-wiimote.h
@@ -372,4 +372,5 @@ static inline int wiimote_cmd_wait_noint(struct
wiimote_data *wdata)
         return 0;
 }

+extern bool dpad_as_analog;
 #endif
--
2.25.1

             reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-23 22:02 Daniel Morse [this message]
2020-05-25  6:13 ` David Rheinsberg
2020-05-25 11:31   ` Daniel Morse
2020-05-26 21:41     ` [PATCH v2] " Daniel Morse
2020-05-29  7:56       ` David Rheinsberg
2020-05-30 12:03         ` Daniel Morse
2020-05-26 21:44     ` Daniel Morse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CANFaMLGVGmS4i3fdH2rYeqSgqk3Gm=sxaLLMuKx4T1eY9ZvyEg@mail.gmail.com' \
    --to=dmorse@speedfox.co.uk \
    --cc=dh.herrmann@googlemail.com \
    --cc=linux-input@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux Input Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-input/0 linux-input/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-input linux-input/ https://lore.kernel.org/linux-input \
		linux-input@vger.kernel.org
	public-inbox-index linux-input

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-input


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git