pipa: Add Xiaomi Pen support to Xiaomi Peripheral Manager

Change-Id: I2698b754dd062d581f059456670639c71658f3fc
This commit is contained in:
Fabian Leutenegger
2023-08-10 12:08:46 +02:00
committed by Abdulwahab Isam
parent bf214c5b8a
commit 4ffd8bd262
5 changed files with 116 additions and 0 deletions

View File

@@ -14,4 +14,14 @@ android_app {
static_libs: [
"androidx.core_core",
],
required: [
"xiaomi-pen"
],
}
cc_binary {
name: "xiaomi-pen",
srcs: [
"xiaomi-pen.cpp",
],
}

View File

@@ -20,5 +20,6 @@ public class BootCompletedReceiver extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
if (DEBUG) Log.d(TAG, "Received boot completed intent");
KeyboardUtils.setup(context);
PenUtils.setup(context);
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.xiaomiperipheralmanager;
import android.content.Context;
import android.hardware.input.InputManager;
import android.hardware.input.InputManager.InputDeviceListener;
import android.os.SystemProperties;
import android.util.Log;
import android.view.InputDevice;
public class PenUtils {
private static final String TAG = "XiaomiPeripheralManagerPenUtils";
private static final boolean DEBUG = false;
private static final int penVendorId = 6421;
private static final int penProductId = 19841;
private static InputManager mInputManager;
public static void setup(Context context) {
mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
refreshPenMode();
mInputManager.registerInputDeviceListener(mInputDeviceListener, null);
}
private static void enablePenMode() {
Log.d(TAG, "enablePenMode: Enable Pen Mode");
SystemProperties.set("persist.sys.parts.pen", "18");
}
private static void disablePenMode() {
Log.d(TAG, "disablePenMode: Disable Pen Mode");
SystemProperties.set("persist.sys.parts.pen", "0");
}
private static void refreshPenMode() {
for (int id : mInputManager.getInputDeviceIds()) {
if (isDeviceXiaomiPen(id)) {
if (DEBUG) Log.d(TAG, "refreshPenMode: Found Xiaomi Pen");
enablePenMode();
return;
}
}
if (DEBUG) Log.d(TAG, "refreshPenMode: No Xiaomi Pen found");
disablePenMode();
}
private static boolean isDeviceXiaomiPen(int id) {
InputDevice inputDevice = mInputManager.getInputDevice(id);
return inputDevice.getVendorId() == penVendorId &&
inputDevice.getProductId() == penProductId;
}
private static InputDeviceListener mInputDeviceListener = new InputDeviceListener() {
@Override
public void onInputDeviceAdded(int id) {
refreshPenMode();
}
@Override
public void onInputDeviceRemoved(int id) {
refreshPenMode();
}
@Override
public void onInputDeviceChanged(int id) {
refreshPenMode();
}
};
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define SET_CUR_VALUE 0
#define TOUCH_PEN_MODE 20
#define TOUCH_MAGIC 't'
#define TOUCH_IOC_SETMODE _IO(TOUCH_MAGIC, SET_CUR_VALUE)
#define TOUCH_DEV_PATH "/dev/xiaomi-touch"
#define TOUCH_ID 0
int main(int argc, char **argv) {
if(argc != 2) {
fprintf(stderr, "Usage: %s <value>\n", argv[0]);
return -1;
}
int fd = open(TOUCH_DEV_PATH, O_RDWR);
int arg[3] = {TOUCH_ID, TOUCH_PEN_MODE, atoi(argv[1])};
ioctl(fd, TOUCH_IOC_SETMODE, &arg);
close(fd);
}

View File

@@ -7,3 +7,6 @@
on boot
# WiFi country code
setprop ro.boot.wificountrycode US
on property:persist.sys.parts.pen=*
exec /system/bin/xiaomi-pen ${persist.sys.parts.pen}