add optional file argument to sAVE command

Signed-off-by: Michel-FK <michel.stempin@funkey-project.com>
This commit is contained in:
Michel-FK 2021-04-22 23:07:33 +02:00
parent 5b62d09a7e
commit 285c2faa2e
5 changed files with 57 additions and 14 deletions

View File

@ -36,6 +36,7 @@ KEYUP <key_code> Send a key up event with the
LOAD <configuration_file> Load a configuration file
MAP <button_combination> TO KEY <key_code> Map a button combination to a keycode
MAP <button_combination> TO COMMAND <shell_command> Map a button combination to a Shell command
SAVE <configuration_file> Save to a configuration file
SLEEP <delays_ms> Sleep for the given delay in ms
TYPE <character_string> Type in a character string
UNMAP <button_combination> Unmap a button combination

View File

@ -27,6 +27,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <syslog.h>
#include <unistd.h>
#include <linux/input.h>

View File

@ -25,6 +25,7 @@
* This file contains the mapping list handling functions
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -303,46 +304,84 @@ void dump_mapping_list(mapping_list_t *list)
}
/* Save a mapping */
void save_mapping(mapping_t *mapping)
bool save_mapping(FILE *fp, mapping_t *mapping)
{
int i, length;
uint32_t gpio_mask;
printf("MAP ");
if (fprintf(fp, "MAP ") < 0) {
return false;
}
for (i = 0, length = 0, gpio_mask = mapping->gpio_mask; i < MAX_NUM_GPIO;
i++, gpio_mask >>= 1) {
if (gpio_mask & 1) {
printf("%s%s", gpio_name(i), gpio_mask == 1 ? " " : "+");
length += strlen(gpio_name(i)) + 1;
if (fprintf(fp, "%s%s", gpio_name(i), gpio_mask == 1 ? " " : "+") < 0) {
return false;
}
length += strlen(gpio_name(i)) + 1;
}
}
for (i = 9 - length; i > 0; i--) {
printf(" ");
if (fprintf(fp, " ") < 0) {
return false;
}
}
switch (mapping->type) {
case MAPPING_COMMAND:
printf("TO COMMAND %s\n", mapping->value.command);
if (fprintf(fp, "TO COMMAND %s\n", mapping->value.command) < 0) {
return false;
}
break;
case MAPPING_KEY:
printf("TO KEY %s\n", keycode_name(mapping->value.keycode));
if (fprintf(fp, "TO KEY %s\n",
keycode_name(mapping->value.keycode)) < 0) {
return false;
}
break;
default:
FK_ERROR("Unknown mapping type %d\n", mapping->type);
return false;
break;
}
return true;
}
/* Save a mapping list */
void save_mapping_list(mapping_list_t *list)
bool save_mapping_list(const char *name, mapping_list_t *list)
{
struct mapping_list_t *p;
mapping_t *tmp;
FILE *fp;
printf("CLEAR\n");
if (name[0] == '\0') {
fp = stdout;
} else {
fp = fopen(name, "w");
}
if (fp == NULL) {
FK_ERROR("Cannot open save file \"%s\": %s\n", name, strerror(errno));
return false;
}
fprintf(fp, "CLEAR\n");
list_for_each_prev(p, list) {
tmp = list_entry(p, mapping_t, mappings);
save_mapping(tmp);
if (save_mapping(fp, tmp) == false) {
FK_ERROR("Cannot write to save file \"%s\": %s\n", name,
strerror(errno));
if (fp != stdout) {
fclose(fp);
}
return false;
}
}
if (fp == stdout) {
return true;
}
if (fclose(fp) < 0) {
FK_ERROR("Cannot close save file \"%s\": %s\n", name, strerror(errno));
return false;
}
return true;
}

View File

@ -27,6 +27,7 @@
#ifndef _MAPPING_LIST_H_
#define _MAPPING_LIST_H_
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
@ -68,7 +69,7 @@ mapping_t *find_mapping(mapping_list_t *list, uint32_t gpio_mask);
bool remove_mapping(mapping_list_t *list, mapping_t *mapping);
void dump_mapping(mapping_t *mapping);
void dump_mapping_list(mapping_list_t *list);
void save_mapping(mapping_t *mapping);
void save_mapping_list(mapping_list_t *list);
bool save_mapping(FILE *fp, mapping_t *mapping);
bool save_mapping_list(const char *name, mapping_list_t *list);
#endif // _MAPPING_LIST_H_

View File

@ -286,7 +286,6 @@ bool parse_config_line(char *line, mapping_list_t *list,
case STATE_CLEAR:
case STATE_DUMP:
case STATE_SAVE:
break;
case STATE_SLEEP:
@ -297,6 +296,7 @@ bool parse_config_line(char *line, mapping_list_t *list,
}
}
case STATE_LOAD:
case STATE_SAVE:
case STATE_TYPE:
if (buffer[0] != '\0') {
strncat(buffer, " ", MAX_LINE_LENGTH);
@ -461,7 +461,8 @@ bool parse_config_line(char *line, mapping_list_t *list,
break;
case STATE_SAVE:
save_mapping_list(list);
FK_DEBUG("SAVE file \"%s\"\n", buffer);
return save_mapping_list(buffer, list);
break;
case STATE_INIT: