Last active
May 29, 2020 11:10
-
-
Save Logic-gate/1f9c1bf8ccc553ed399a088067b449a9 to your computer and use it in GitHub Desktop.
Fxtec Pro 1 Orientation Lock + Keyboard Shortcuts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is not a recommended way to achieve landscape lock...but it works... | |
Start.. | |
touch $HOME/.Xdbus | |
chmod 600 $HOME/.Xdbus | |
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus | |
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus | |
touch $HOME/.keyboard-state | |
touch $HOME/.dynamic-state | |
Then.. | |
python /home/nemo/scripts/key.py & # not the best way...no way to close it afterwards...should export $! to a file | |
or `make` keymapper. First command in keymapper config will just write to `/home/nemo/.dynamic-state` | |
Create orientationLock-keyboard.sh with permissions. | |
Create a service and a timer, cp the respective files. | |
start the service..do not enable...my Requires are wrong. | |
There is a slight delay due to the structure of the process, read data > write to file > read from file. I am not sure but I think it's safer this way. | |
Also, change paths accordinlgy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#I run this with python key.py & | |
import struct | |
import time | |
import sys | |
infile_path = "/dev/input/event3" #Fxtec Keyboard | |
FORMAT = 'llHHI' | |
EVENT_SIZE = struct.calcsize(FORMAT) | |
in_file = open(infile_path, "rb") | |
event = in_file.read(EVENT_SIZE) | |
DYNAMIC_COMBO = {125, 24} #125 = FXKEY, 24=LETTER O | |
CURRENT_KEY = set() | |
IO = "/home/nemo/.dynamic-state" | |
def press(key): | |
if key in DYNAMIC_COMBO: | |
CURRENT_KEY.add(key) | |
if all(k in CURRENT_KEY for k in DYNAMIC_COMBO): | |
f = open(IO, 'w+') | |
f.write("1") | |
f.close() | |
#print(1) | |
#print("FX KEY + O") | |
elif key == 125: | |
#FXkey will enable screen locking | |
f = open(IO, 'w+') | |
f.write("0") | |
f.close() | |
else: | |
pass | |
#print("Not Active") | |
def release(key): | |
try: | |
CURRENT_KEY.remove(key) | |
except KeyError: | |
pass | |
while event: | |
(tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event) | |
if type != 0 or code != 0 or value != 0: | |
if value == 1: | |
press(code) | |
elif value == 0: | |
release(code) | |
else: | |
pass | |
event = in_file.read(EVENT_SIZE) | |
in_file.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Not recommended to run keymapper as root, | |
name = "Fxtec Pro 1 Keymap shortcuts"; | |
keymap = | |
{ | |
orientationLock = { holder = 125; | |
trigger = 14; | |
cmd = "cat /home/nemo/.dynamic-state | grep 1 ; echo $? > /home/nemo/.dynamic-state"; | |
}; | |
test = { holder = 125; | |
trigger = 100; | |
cmd = "ls"; }; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <linux/input.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <stdlib.h> | |
#include <libconfig.h> | |
#include <string.h> | |
#include "keymapper.h" | |
void INThandler(){ | |
exit(0); | |
} | |
void debug_(char note[], char details[]) | |
{ | |
printf("%s --> %s\n", note, details); | |
} | |
int log_keys(int store, char *state){ | |
FILE *fptr; | |
int first_key; | |
if(state == "w") | |
{ | |
fptr = fopen("key.log","w+"); | |
if(fptr == NULL) | |
{ | |
debug_("Error!", "File is NULL"); | |
exit(1); | |
} | |
fprintf(fptr,"%d\n", store); | |
} | |
else if(state == "r") | |
{ | |
fptr = fopen("key.log","r"); | |
fscanf(fptr, "%d", &first_key); | |
return first_key; | |
} | |
fclose(fptr); | |
} | |
const char* read_config(int key) | |
{ | |
config_t cfg; | |
config_setting_t *keymapSetting; | |
const char *str; | |
config_init(&cfg); | |
if(! config_read_file(&cfg, "keymap.config")) | |
{ | |
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), | |
config_error_line(&cfg), config_error_text(&cfg)); | |
config_destroy(&cfg); | |
} | |
keymapSetting = config_lookup(&cfg, "keymap"); | |
if(keymapSetting != NULL) | |
{ | |
int countKeymap = config_setting_length(keymapSetting); | |
int i; | |
for(i = 0; i < countKeymap; i++) | |
{ | |
config_setting_t *keymap = config_setting_get_elem(keymapSetting, i); | |
if(!(config_setting_lookup_int(keymap, "holder", &holder) | |
&& config_setting_lookup_int(keymap, "trigger", &trigger) | |
&& config_setting_lookup_string(keymap, "cmd", &cmd))) | |
continue; | |
if(key == holder) | |
{ | |
para_keys.holder = key; | |
log_keys(para_keys.holder, "w"); | |
} | |
else if(key == trigger) | |
{ | |
para_keys.trigger = key; | |
} | |
else | |
{ | |
continue; | |
} | |
para_keys.holder = log_keys(0, "r"); | |
if(para_keys.trigger == trigger && para_keys.holder == holder) | |
{ | |
system(cmd); | |
log_keys(0, "w"); | |
} | |
} | |
} | |
} | |
int release() | |
{ | |
para_keys.holder = 0; | |
para_keys.trigger = 0; | |
return 1; | |
} | |
int check_key(int key) | |
{ | |
const char* exec = read_config(key); | |
system(exec); | |
release(); | |
return 1; | |
} | |
void run(char *param) | |
{ | |
char devname[] = "/dev/input/event3"; | |
int device = open(devname, O_RDONLY); | |
struct input_event ev; | |
signal(SIGINT, INThandler); | |
while(1) | |
{ | |
read(device,&ev, sizeof(ev)); | |
if(ev.type != 0 || ev.code != 0 || ev.value != 0) | |
{ | |
if(ev.value == 1) | |
{ | |
if(param == "start") | |
{ | |
check_key(ev.code); | |
} | |
else if(param == "listen") | |
{ | |
printf("CODE --> %d\n", ev.code); | |
} | |
} | |
} | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
if(2 > argc) | |
{ | |
printf("%s\n", "Syntax: keymapper [listen|start]"); | |
return 1; | |
} | |
if(0 == strcmp(argv[1], "start")) | |
{ | |
run("start"); | |
} | |
else if (0 == strcmp(argv[1], "listen")) | |
{ | |
run("listen"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ORIENTATIONLOCK_KEYBOARD_H_ | |
#define ORIENTATIONLOCK_KEYBOARD_H_ | |
struct Key_Combo | |
{ | |
int holder; | |
int trigger; | |
}; | |
struct Key_Combo para_keys; | |
const char *cmd; | |
int holder, trigger; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC=gcc | |
CFLAGS = `pkg-config --cflags libconfig` | |
LIBS = `pkg-config --libs libconfig` | |
DEP = keymapper.h | |
%.o: %c $(DEP) | |
$(CC) -c -o $@ $< $(CFLAGS) | |
keymapper: keymapper.o | |
touch key.log | |
$(CC) -o keymapper keymapper.o $(LIBS) | |
clean: | |
rm -f keymapper.o keymapper key.log | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description= Orientation Loc-Keyboard lid | |
Requires=mce.service | |
[Service] | |
Type=simple | |
#Restart=always | |
#RemainAfterExit=true | |
#RestartSec=1 | |
ExecStart=/bin/sh /usr/local/bin/orientationLock-keyboard | |
[Install] | |
WantedBy=multi-user.target | |
Also=dbus.service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#WAYLAND_DISPLAY=../../display/wayland-0 | |
#Just to be safe, we need the display | |
#We also need to export DBUS_SESSION_BUS_ADDRESS for dconf to work here. | |
#env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus | |
#echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus | |
#This will change on every restart, this should have a seperate init script at boot. | |
cmd="$(dbus-send --system --print-reply --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.keyboard_slide_state_req)" | |
state="$(cat /home/nemo/.keyboard-state)" | |
io="$(cat /home/nemo/.dynamic-state)" | |
if [[ "$cmd" == *"$state"* ]] && [[ "$io" == "1" ]]; then | |
source /home/nemo/.Xdbus; dconf write /lipstick/orientationLock \'dynamic\' | |
echo "keyboard state is" $state "nothing to do." | |
elif [[ "$cmd" != "$state" ]]; then | |
if [[ "$io" == "0" ]]; then | |
if [[ "$cmd" == *"open"* ]]; then | |
source /home/nemo/.Xdbus; dconf write /lipstick/orientationLock \'landscape\' | |
echo "slide open - locking landscape" | |
echo "open" > /home/nemo/.keyboard-state | |
elif [[ "$cmd" == *"closed"* ]]; then | |
source /home/nemo/.Xdbus; dconf write /lipstick/orientationLock \'portrait\' | |
echo "slide closed - locking portriat" | |
echo "closed" > /home/nemo/.keyboard-state | |
fi | |
fi | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description= Timer for Keyboard Orientation Lock | |
[Timer] | |
OnUnitActiveSec=1s | |
AccuracySec=1 | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment