Playing Touhou Project with Mobile Phone
Tags: touhou, python, winio, webdev
Published: 2023-01-17 13:36:49

Note: this passage won't have much technical details in it. It was written just to record some of my thoughts (as well as mistakes and detours). 

**Disclaimer:** some procedures I mentioned in this passage may cause TERRIBLE results to your device (such as BitLocker). Don't try it unless you have 100% confidence in yourself.

## 0. the motive behind this...
I've been a fan of Touhou Project (or Touhou for short) for a few years. But frankly speaking, I have limited talent in it, as well as other STG games. Then it occurred to me that, what if, the limited talent doesn't have a dominant negative effect on my gaming performance, while the limited equipment does. Meanwhile, maybe it's time for me to get myself a gamepad-like stuff. So why not make one on my own?

## 1. which method?

I have very limited experience in hardware developing, which made it more realistic to develop my gamepad with my phone. I once decided to make it with Android Studio. But that would make its cross-platform performance terrible. As an alternative to making APPs, I chose webpage as my user interface.

## 2. logic

The logic was quite simple: user push (or release) a button, JavaScript transmits the data (push or release, and which button) to the server code running on the Intranet. And this server code is deployed on the machine for playing Touhou. The code then somehow simulates keyboard clicks.

## 3. the most terrible (and terrifying) part
I DID encountered some difficulties such as messing with JavaScript events, but compared with the part, the total disaster which cost me 7 hours to fix, these JavaScript stuff seemed so 'harmless' and 'innocent'.

### 3.1 the corrupted win32?
As mentioned above, I need to find a way to simulate keyboard interactions. The first thing which appeared to me is Win32, and its Python wrapping - pywin32. It has _this_ function, which can make fake keyboard input.
`win32api.keybd_event(bVk, bScan, dwFlags, dwExtraInfo)`
Sounds quite simple, right? It would be simple...if it can actually work with Touhou. I found that when I use my 'gamepad', literally _nothing_ would happen. No exceptions, no warnings, and the game seemed 'unwilling' to respond to Win32 module's control.
I uninstalled and re-installed my pywin32 module, but it didn't get better.

### 3.2 no soft keyboard!
Then I tried to use the Windows built-in soft keyboard to play Touhou. To my great astonishment, it was somehow 'disabled' either! But why?

### 3.3 a tiny 'feature' of DirectX ruins the day
I asked in a chat group how Touhou get the keyboard interactions. They simply answered - 'DirectX'. And it _is_ the key point. With the limited information on Internet I found that in order to have faster respond speed, DirectX override the Win32 keyboard message system (that's why Win32 and soft keyboard seemed to be 'disabled'), and get interactions directly from hardware.

Luckily, there's still one way to simulate 'hardware' interactions - pywinio.

### 3.4 long way configuring environment
pywinio uses some DLLs to run its services. However, these DLLs are not certificated, and Windows would refuse running them. To solve this, I have to enter the 'testing mode', and before that, I must disable my laptop's 'safe boot' function.

But my laptop somehow 'protected' both of my drives with BitLocker, which forced me to enable 'safe boot'.

At last, it took me...

- 1h   programming.
- 1.5h turning off BitLocker and enter 'testing mode'.
- 2h   debugging.
- 2.5h messing with the feature of DirectX.

Anyway, I can play Touhou with my 'gamepad' now, and it's not about my talent...


![Playing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8vxgz9upvb2igmcrbpi.png)

![GUI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5cwtiughn802jz71xe8.png)

![Miss](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6478ujskwvldbwjj7y7n.png)

Maybe...

## 4. External Links
https://github.com/Creeper2333/Touhou-Project-Controller