#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include "mapper.h"
char *current_config[CONFIG_MAX] = {
/* UINPUT_DEV */
"/dev/uinput",
/* EVENT_DEV */
"/dev/input/event",
NULL,
};
struct parameter {
char *name;
int has_value;
void (*handler)(void);
char *help;
};
void set_8bit_calibration(void) {
set_scale_factor(1, 256, 0);
}
void set_dynamic_calibration(void) {
set_dynamic_calibrate(1);
}
void set_background(void) {
set_daemonize(1);
}
void set_input_calibration(void) {
set_event_input_calibrate(1);
}
void kill_daemon(void) {
kill_running_joymap();
exit(0);
}
void skip_unused_axes(void) {
set_skip_unused();
}
void set_nolock(void) {
set_nograb();
}
void show_help(void);
struct parameter cmdline_arg[CONFIG_MAX] = {
/* UINPUT_DEV */
{"--uinput_dev", 1, NULL, "The location of the user input device (/dev/uinput)"},
/* EVENT_DEV */
{"--event_dev", 1, NULL, "The location of the event devices (/dev/input/event)"},
{"-s", 0, skip_unused_axes, "Don't register unused axes and buttons"},
{"--skip", 0, skip_unused_axes, "Don't register unused axes and buttons"},
{"-c", 0, set_input_calibration, "Use the information from the input event devices to calibrate axes to the range -32767 to 32767"},
{"-d", 0, set_dynamic_calibration, "Runtime recalibrate the output of virtual joysticks using the output values"},
{"--dynamic", 0, set_dynamic_calibration, "Runtime recalibrate the output of virtual joysticks using the output values"},
{"-8", 0, set_8bit_calibration, "Calibrate axes output to the range -127 to 127"},
{"-k", 0, kill_daemon, "Terminate any running loadmap processes and quit"},
{"--kill", 0, kill_daemon, "Terminate any running loadmap processes and quit"},
{"-b", 0, set_background, "After loading the config, run loadmap in the background"},
{"--background", 0, set_background, "After loading the config, run loadmap in the background"},
{"--daemon", 0, set_background, "After loading the config, run loadmap in the background"},
{"--nolock", 0, set_nolock, "Don't lock devices for joymap use"},
{"--ffconst", 0, set_fake_ff_constant, "Pretend to have force feedback constant, map it to periodic"},
{"-h", 0, show_help, "Show this help"},
{"--help", 0, show_help, "Show this help"},
{NULL, 0, NULL, NULL}
};
static char *pad(int len, int maxlen) {
static char padding[256];
int i;
for (i=0; i<maxlen-len; i++) {
padding[i] = ' ';
}
padding[maxlen-len] = '\0';
return padding;
}
void show_help(void) {
int i;
int len;
int maxlen = 0;
maxlen = strlen("filename.map");
i = 0;
while (cmdline_arg[i].name != NULL) {
len = strlen(cmdline_arg[i].name);
if (maxlen < len) maxlen = len;
i++;
}
printf("Usage: loadmap [options] filename.map\n");
len = strlen("filename.map");
printf(" filename.map %s The name of the map file to load\n", pad(len, maxlen));
i = 0;
while (cmdline_arg[i].name != NULL) {
len = strlen(cmdline_arg[i].name);
printf(" %s %s %s\n", cmdline_arg[i].name, pad(len, maxlen), cmdline_arg[i].help);
i++;
}
exit(0);
}
char *get_config(int key) {
if ((key >= CONFIG_MAX) || (key < 0)) {
fprintf(stderr, "ERR: Invalid config key in code: %d\n", key);
exit(1);
}
return current_config[key];
}
char *set_config(int key, char *value) {
char *old;
if ((key >= CONFIG_MAX) || (key < 0)) {
fprintf(stderr, "ERR: Invalid config key in code: %d\n", key);
exit(1);
}
old = current_config[key];
current_config[key] = value;
return old;
}
int match_config(char *arg) {
int i=0;
while (cmdline_arg[i].name != NULL) {
if (strcmp(cmdline_arg[i].name, arg) == 0)
return i;
i++;
}
return -1;
}
void cmdline_config(int argc, char *argv[]) {
int i, index;
for (i=0; i<argc; i++) {
index = match_config(argv[i]);
if (index >= 0) {
if (cmdline_arg[index].has_value) {
if (i + 1 >= argc) {
fprintf(stderr, "Missing argument for: %s\n", argv[i]);
}
current_config[index] = argv[++i];
} else {
cmdline_arg[index].handler();
}
}
}
}