[key-selector] Detects x86 and refactor code
This commit is contained in:
parent
46afa3abc4
commit
c0d5d9f5f6
|
|
@ -10,7 +10,7 @@ android {
|
||||||
externalNativeBuild {
|
externalNativeBuild {
|
||||||
cmake {
|
cmake {
|
||||||
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
|
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
|
||||||
cppFlags "-std=c++17"
|
cppFlags "-std=c++20"
|
||||||
cFlags "-std=gnu99"
|
cFlags "-std=gnu99"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -28,8 +28,8 @@ android {
|
||||||
release {
|
release {
|
||||||
externalNativeBuild {
|
externalNativeBuild {
|
||||||
cmake {
|
cmake {
|
||||||
cppFlags "-fvisibility=hidden -fvisibility-inlines-hidden -O2 -s -Wno-unused-value"
|
cppFlags "-fvisibility=hidden -fvisibility-inlines-hidden -Os -s -Wno-unused-value"
|
||||||
cFlags "-fvisibility=hidden -fvisibility-inlines-hidden -O2 -s -Wno-unused-value"
|
cFlags "-fvisibility=hidden -fvisibility-inlines-hidden -Os -s -Wno-unused-value"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,8 @@ public:
|
||||||
virtual const std::string desc_line_1() {
|
virtual const std::string desc_line_1() {
|
||||||
return "Select variant. Use Volume Down to move and Volume Up to confirm.";
|
return "Select variant. Use Volume Down to move and Volume Up to confirm.";
|
||||||
}
|
}
|
||||||
virtual const std::string desc_line_2(int seconds) {
|
virtual const std::string desc_line_2(uint16_t seconds) {
|
||||||
const char base[] = "The program will select YAHFA for you in %d seconds if you don't \nhave a physical volume button. ";
|
const char base[] = "The program will select YAHFA for you in %hu seconds if you don't \nhave a physical volume button. ";
|
||||||
std::string out;
|
std::string out;
|
||||||
out.resize(sizeof(base) + 20);
|
out.resize(sizeof(base) + 20);
|
||||||
sprintf(out.data(), base, seconds);
|
sprintf(out.data(), base, seconds);
|
||||||
|
|
@ -25,8 +25,8 @@ public:
|
||||||
const std::string desc_line_1() override {
|
const std::string desc_line_1() override {
|
||||||
return "请选择变种。使用音量减切换,音量加确定。";
|
return "请选择变种。使用音量减切换,音量加确定。";
|
||||||
}
|
}
|
||||||
const std::string desc_line_2(int seconds) override {
|
const std::string desc_line_2(uint16_t seconds) override {
|
||||||
const char base[] = "如果您的设备没有音量键,本程序将会在 %d 秒后自动选择 YAHFA 。";
|
const char base[] = "如果您的设备没有音量键,本程序将会在 %hu 秒后自动选择 YAHFA 。";
|
||||||
std::string out;
|
std::string out;
|
||||||
out.resize(sizeof(base) + 20);
|
out.resize(sizeof(base) + 20);
|
||||||
sprintf(out.data(), base, seconds);
|
sprintf(out.data(), base, seconds);
|
||||||
|
|
|
||||||
|
|
@ -16,19 +16,20 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <cerrno>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <cstring>
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
#include <linux/input.h>
|
||||||
|
#include <sstream>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
#include <sys/poll.h>
|
#include <sys/poll.h>
|
||||||
#include <linux/input.h>
|
|
||||||
#include <cerrno>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <sys/system_properties.h>
|
#include <sys/system_properties.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "Languages.h"
|
#include "Languages.h"
|
||||||
#include "key_selector.h"
|
#include "key_selector.h"
|
||||||
|
|
@ -48,7 +49,7 @@ static int open_device(const char *device)
|
||||||
char name[80];
|
char name[80];
|
||||||
char location[80];
|
char location[80];
|
||||||
char idstr[80];
|
char idstr[80];
|
||||||
struct input_id id;
|
input_id id{};
|
||||||
|
|
||||||
fd = open(device, O_RDWR);
|
fd = open(device, O_RDWR);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
|
|
@ -128,7 +129,7 @@ static int read_notify(const char *dirname, int nfd)
|
||||||
char devname[PATH_MAX];
|
char devname[PATH_MAX];
|
||||||
char *filename;
|
char *filename;
|
||||||
char event_buf[512];
|
char event_buf[512];
|
||||||
int event_size;
|
uint32_t event_size;
|
||||||
int event_pos = 0;
|
int event_pos = 0;
|
||||||
struct inotify_event *event;
|
struct inotify_event *event;
|
||||||
|
|
||||||
|
|
@ -163,30 +164,15 @@ static int read_notify(const char *dirname, int nfd)
|
||||||
|
|
||||||
static int scan_dir(const char *dirname)
|
static int scan_dir(const char *dirname)
|
||||||
{
|
{
|
||||||
char devname[PATH_MAX];
|
namespace fs = std::filesystem;
|
||||||
char *filename;
|
try {
|
||||||
DIR *dir;
|
for (auto &item: fs::directory_iterator(dirname)) {
|
||||||
struct dirent *de;
|
open_device(item.path().c_str());
|
||||||
|
}
|
||||||
dir = opendir(dirname);
|
} catch (const fs::filesystem_error &e) {
|
||||||
if (dir == NULL) {
|
std::cerr << e.what();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(devname, dirname);
|
|
||||||
filename = devname + strlen(devname);
|
|
||||||
*filename++ = '/';
|
|
||||||
|
|
||||||
while ((de = readdir(dir))) {
|
|
||||||
if (de->d_name[0] == '.' && (de->d_name[1] == '\0' ||
|
|
||||||
(de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
strcpy(filename, de->d_name);
|
|
||||||
open_device(devname);
|
|
||||||
}
|
|
||||||
closedir(dir);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -194,7 +180,7 @@ static int scan_dir(const char *dirname)
|
||||||
uint32_t get_event() {
|
uint32_t get_event() {
|
||||||
int i;
|
int i;
|
||||||
int res;
|
int res;
|
||||||
struct input_event event;
|
input_event event{};
|
||||||
const char *device_path = "/dev/input";
|
const char *device_path = "/dev/input";
|
||||||
unsigned char keys;
|
unsigned char keys;
|
||||||
|
|
||||||
|
|
@ -206,12 +192,14 @@ uint32_t get_event() {
|
||||||
|
|
||||||
res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE);
|
res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
throw std::logic_error("inotify_add_watch failed");
|
std::cerr << "inotify_add_watch failed" << std::endl;
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
res = scan_dir(device_path);
|
res = scan_dir(device_path);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
throw std::logic_error("scan dev failed");
|
std::cerr << "scan dev failed" << std::endl;
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
@ -245,7 +233,7 @@ uint32_t get_event() {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
if (getuid() != 0) {
|
if (getuid() != 0) {
|
||||||
std::cout << "Root required" << std::endl;
|
std::cerr << "Root required" << std::endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -254,20 +242,57 @@ int main() {
|
||||||
alarm(timeout);
|
alarm(timeout);
|
||||||
auto sig_handler = [](int){
|
auto sig_handler = [](int){
|
||||||
std::cout << "No operation after " << timeout << " seconds" << std::endl;
|
std::cout << "No operation after " << timeout << " seconds" << std::endl;
|
||||||
exit(YAHFA);
|
exit(static_cast<int>(Variant::YAHFA));
|
||||||
};
|
};
|
||||||
signal(SIGALRM, sig_handler);
|
signal(SIGALRM, sig_handler);
|
||||||
|
|
||||||
int cursor = YAHFA;
|
// get current arch
|
||||||
const int cursor_max = SandHook;
|
#if defined(__arm__)
|
||||||
|
const Arch arch = ARM;
|
||||||
|
#elif defined(__aarch64__)
|
||||||
|
const Arch arch = ARM64;
|
||||||
|
#elif defined(__i386__)
|
||||||
|
const Arch arch = x86;
|
||||||
|
#elif defined(__x86_64__)
|
||||||
|
const Arch arch = x86_64;
|
||||||
|
#else
|
||||||
|
#error "Unsupported arch"
|
||||||
|
#endif
|
||||||
|
|
||||||
auto print_status = [&cursor](){
|
std::unordered_map<Variant, VariantDetail> variants;
|
||||||
|
for (const auto i: AllVariants) {
|
||||||
|
switch (i) {
|
||||||
|
case Variant::YAHFA:
|
||||||
|
variants[i] = {
|
||||||
|
.expression = "YAHFA",
|
||||||
|
.supported_arch = {ARM, ARM64, x86, x86_64}
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case Variant::SandHook:
|
||||||
|
variants[i] = {
|
||||||
|
.expression = "SandHook",
|
||||||
|
.supported_arch = {ARM, ARM64}
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Variant cursor = Variant::YAHFA;
|
||||||
|
|
||||||
|
auto print_status = [&cursor, variants, arch](){
|
||||||
//std::cout << "\33[2K\r"; // clear this line
|
//std::cout << "\33[2K\r"; // clear this line
|
||||||
std::cout << "[";
|
std::stringstream ss;
|
||||||
std::cout << (cursor == YAHFA ? "x" : " ");
|
for (const auto &i: variants) {
|
||||||
std::cout << "] YAHFA [";
|
if (!i.second.supported_arch.contains(arch)) {
|
||||||
std::cout << (cursor == SandHook ? "x" : " ");
|
continue;
|
||||||
std::cout << "] SandHook" << std::endl;
|
}
|
||||||
|
ss << "[";
|
||||||
|
ss << (cursor == i.first ? "x" : " ");
|
||||||
|
ss << "] ";
|
||||||
|
ss << i.second.expression;
|
||||||
|
ss << " ";
|
||||||
|
}
|
||||||
|
std::cout << ss.str() << std::endl;
|
||||||
};
|
};
|
||||||
|
|
||||||
// languages
|
// languages
|
||||||
|
|
@ -292,9 +317,6 @@ int main() {
|
||||||
break;
|
break;
|
||||||
case KEYCHECK_PRESSED_VOLUMEDOWN:
|
case KEYCHECK_PRESSED_VOLUMEDOWN:
|
||||||
cursor++;
|
cursor++;
|
||||||
if (cursor > cursor_max) {
|
|
||||||
cursor = YAHFA;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cout << "ERROR\n";
|
std::cout << "ERROR\n";
|
||||||
|
|
@ -307,5 +329,5 @@ int main() {
|
||||||
|
|
||||||
// std::cout << std::endl << cursor << std::endl;
|
// std::cout << std::endl << cursor << std::endl;
|
||||||
delete l;
|
delete l;
|
||||||
return cursor;
|
return static_cast<int>(cursor);
|
||||||
}
|
}
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
#ifndef __KEYCHECK_H__
|
#ifndef __KEYCHECK_H__
|
||||||
#define __KEYCHECK_H__
|
#define __KEYCHECK_H__
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
// Constants: pressed keys
|
// Constants: pressed keys
|
||||||
#define KEYCHECK_CHECK_VOLUMEDOWN 0x01u
|
#define KEYCHECK_CHECK_VOLUMEDOWN 0x01u
|
||||||
|
|
@ -24,9 +26,37 @@
|
||||||
#define KEYCHECK_PRESSED_VOLUMEDOWN 41u
|
#define KEYCHECK_PRESSED_VOLUMEDOWN 41u
|
||||||
#define KEYCHECK_PRESSED_VOLUMEUP 42u
|
#define KEYCHECK_PRESSED_VOLUMEUP 42u
|
||||||
|
|
||||||
enum Variant {
|
enum class Variant {
|
||||||
YAHFA = 0x11,
|
YAHFA = 0x11,
|
||||||
SandHook = 0x12,
|
SandHook = 0x12,
|
||||||
|
End = SandHook,
|
||||||
|
};
|
||||||
|
const auto AllVariants = { Variant::YAHFA, Variant::SandHook };
|
||||||
|
|
||||||
|
Variant& operator++( Variant &c ) {
|
||||||
|
using IntType = typename std::underlying_type<Variant>::type;
|
||||||
|
c = static_cast<Variant>( static_cast<IntType>(c) + 1 );
|
||||||
|
if ( c > Variant::End )
|
||||||
|
c = Variant::YAHFA;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
Variant operator++( Variant &c, int ) {
|
||||||
|
Variant result = c;
|
||||||
|
++c;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Arch {
|
||||||
|
ARM,
|
||||||
|
ARM64,
|
||||||
|
x86,
|
||||||
|
x86_64
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VariantDetail {
|
||||||
|
const char* expression;
|
||||||
|
std::unordered_set<Arch> supported_arch;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __KEYCHECK_H__
|
#endif // __KEYCHECK_H__
|
||||||
Loading…
Reference in New Issue