Skip to content

Commit

Permalink
Added permanent key mappings (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
donniebreve authored Aug 24, 2022
1 parent 775d5db commit 1b1400c
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 336 deletions.
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@ clean:
-rm -f obj/*.o
-rm -f $(OUTPATH)/$(TARGET)

debug:
@echo "# Stopping the service"
-systemctl --user stop $(SERVICE)
@echo ""

@echo "# Chown root $(OUTPATH)/$(TARGET)"
-sudo chown root $(OUTPATH)/$(TARGET)
@echo "# Chmod u+s $(OUTPATH)/$(TARGET)"
-sudo chmod u+s $(OUTPATH)/$(TARGET)
@echo "# Run $(OUTPATH)/$(TARGET)"
$(OUTPATH)/$(TARGET)

install:
@echo "# Stopping the service"
-systemctl --user stop $(SERVICE)
@echo ""

@echo "# Copying application to $(INSTALLPATH)"
@echo "# This action requires sudo."
sudo cp $(OUTPATH)/$(TARGET) $(INSTALLPATH)
Expand All @@ -59,12 +71,12 @@ install:
mkdir -p $(CONFIGPATH)
cp -n $(CONFIGFILE) $(CONFIGPATH)
@echo ""

@echo "# Copying service file to $(SERVICEPATH)"
mkdir -p $(SERVICEPATH)
cp -f $(SERVICEFILE) $(SERVICEPATH)
@echo ""

@echo "# Enabling and starting the service"
systemctl --user daemon-reload
systemctl --user enable $(SERVICE)
Expand Down
4 changes: 2 additions & 2 deletions src/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int output = -1;

/**
* Binds to the input device using ioctl.
*/
* */
int bindInput(char* eventPath)
{
// Open the keyboard device
Expand Down Expand Up @@ -58,7 +58,7 @@ int bindInput(char* eventPath)

/**
* Creates and binds a virtual output device using ioctl and uinput.
*/
* */
int bindOutput()
{
// Define the virtual keyboard
Expand Down
12 changes: 10 additions & 2 deletions src/binding.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef binding_header
#define binding_header
#ifndef binding_h
#define binding_h

/**
* @brief The highest input event code to enable key events handling for.
Expand All @@ -14,9 +14,17 @@
#define MAX_KEYS_TO_ENABLE_KEY_EVENTS_HANDLING_FOR 572

extern int input;

/**
* Binds to the input device using ioctl.
* */
int bindInput(char* fileDescriptor);

extern int output;

/**
* Creates and binds a virtual output device using ioctl and uinput.
* */
int bindOutput();

#endif
116 changes: 68 additions & 48 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

char configFilePath[256];
char eventPath[18];

int hyperKey;
int keymap[256];

int remap[256];

/**
* @brief Trim comment from the end of the string started by '#' character.
*
* @param s String to be trimmed.
* @return char* Trimmed string without any comments.
*/
* */
char* trimComment(char* s)
{
if (s != NULL)
Expand All @@ -35,7 +38,7 @@ char* trimComment(char* s)
/**
* Trims a string.
* credit to chux: https://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way#122721
*/
* */
char* trimString(char* s)
{
while (isspace((unsigned char)*s)) s++;
Expand Down Expand Up @@ -160,20 +163,21 @@ void findDeviceEvent(char* deviceConfigValue)

static enum sections
{
none,
device,
hyper,
bindings
configuration_none,
configuration_device,
configuration_remap,
configuration_hyper,
configuration_bindings
} section;

/**
* Reads the configuration file.
*/
* */
void readConfiguration()
{
// Find the configuration file
configFilePath[0] = '\0';
FILE* configFile;

char* homePath = getenv("HOME");
if (!homePath)
{
Expand All @@ -198,76 +202,92 @@ void readConfiguration()
return;
}
fprintf(stdout, "info: found the configuration file\n");

// Parse the configuration file
char* buffer = NULL;
size_t length = 0;
ssize_t result = -1;
while ((result = getline(&buffer, &length, configFile)) != -1)
{
char* line = trimComment(buffer);
line = trimString(line);

// Comment or empty line
if (isCommentOrEmpty(line)) continue;

if (isCommentOrEmpty(line))
{
continue;
}
// Check for section
if (strncmp(line, "[Device]", strlen(line)) == 0)
{
section = device;
section = configuration_device;
continue;
}
if (strncmp(line, "[Remap]", strlen(line)) == 0)
{
section = configuration_remap;
continue;
}
if (strncmp(line, "[Hyper]", strlen(line)) == 0)
{
section = hyper;
section = configuration_hyper;
continue;
}
if (strncmp(line, "[Bindings]", strlen(line)) == 0)
{
section = bindings;
section = configuration_bindings;
continue;
}

// Read configurations
switch (section)
{
case device:
{
if (eventPath[0] == '\0')
case configuration_device:
{
findDeviceEvent(line);
if (eventPath[0] == '\0')
{
findDeviceEvent(line);
}
continue;
}
continue;
}

case hyper:
{
char* tokens = line;
char* token = strsep(&tokens, "=");
token = strsep(&tokens, "=");
int code = convertKeyStringToCode(token);
hyperKey = code;
break;
}

case bindings:
{
char* tokens = line;
char* token = strsep(&tokens, "=");
int fromCode = convertKeyStringToCode(token);
token = strsep(&tokens, "=");
int toCode = convertKeyStringToCode(token);
keymap[fromCode] = toCode;
break;
}

case none:
case configuration_remap:
{
char* tokens = line;
char* token = strsep(&tokens, "=");
int fromCode = convertKeyStringToCode(token);
token = strsep(&tokens, "=");
int toCode = convertKeyStringToCode(token);
remap[fromCode] = toCode;
break;
}
case configuration_hyper:
{
char* tokens = line;
char* token = strsep(&tokens, "=");
token = strsep(&tokens, "=");
int code = convertKeyStringToCode(token);
hyperKey = code;
break;
}
case configuration_bindings:
{
char* tokens = line;
char* token = strsep(&tokens, "=");
int fromCode = convertKeyStringToCode(token);
token = strsep(&tokens, "=");
int toCode = convertKeyStringToCode(token);
keymap[fromCode] = toCode;
break;
}
case configuration_none:
default:
continue;
{
continue;
}
}
}

fclose(configFile);
if (buffer) free(buffer);
if (buffer)
{
free(buffer);
}
}

/**
Expand Down
19 changes: 12 additions & 7 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#ifndef config_header
#define config_header
#ifndef config_h
#define config_h

/**
* The event path for the device.
*/
* */
extern char eventPath[18];

/**
* The hyper key;
*/
* */
extern int hyperKey;

/**
* Map for keys and their conversion.
*/
* */
extern int keymap[256];

/**
* Map for permanently remapped keys.
* */
extern int remap[256];

/**
* Reads the configuration file.
*/
* */
void readConfiguration();

#endif
#endif
2 changes: 1 addition & 1 deletion src/emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Emits a key event.
*/
* */
int emit(int type, int code, int value)
{
//printf("emit: code=%i value=%i\n", code, value);
Expand Down
9 changes: 6 additions & 3 deletions src/emit.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef emit_header
#define emit_header
#ifndef emit_h
#define emit_h

/**
* Emits a key event.
* */
int emit(int type, int code, int value);

#endif
#endif
Loading

0 comments on commit 1b1400c

Please # to comment.