Utility file to add to input-utils

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dmitry and/or Vojtech, are either of you the right person to send a
new input utility to?

The attached file is a simple utility program that allows a user to
set the absolute value parameters (min, max, fuzz, and flat values)
for an absolute-value axis.  Can you add this to the input utils?

Thanks.
/*
 * absset.c
 * Sets Absolute limits/values
 * Copyright 2009 Dan Streetman <ddstreet@xxxxxxxx>
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>

char *progname;

void print_usage()
{
	printf("Usage: %s [param...] <axis> <device>\n", progname);
	printf("Modifies the parameters of an absolute-value axis.\n");
	printf("\n");
	printf("Params:\n");
	printf("  -v --value=VALUE  Set the current value.\n");
	printf("  -m --min=MIN      Set the minimum value.\n");
	printf("  -M --max=MAX      Set the maximum value.\n");
	printf("  -f --fuzz=FUZZ    Set the fuzz value.\n");
	printf("  -F --flat=FLAT    Set the flat value.\n");
	printf("\n");
	printf("Axis setting:\n");
	printf("  -a --axis=N       Change the values of axis number N.\n");
	printf("  -x                Change the X axis values, same as '-a 0'.\n");
	printf("  -y                Change the Y axis values, same as '-a 1'.\n");
	printf("  -z                Change the Z axis values, same as '-a 2'.\n");
}

int set_axis(char *arg, int *axisSet)
{
	if (*axisSet) {
		fprintf(stderr, "Cannot set more than 1 axis\n\n");
		print_usage();
		exit(1);
	}

	*axisSet = 1;

	return strtol(arg, NULL, 0);
}

int main(int argc, char** argv)
{
	int fd = 0;
	char *device_file_name;
	int retval = 0;

	int opt;
	char *shortopts = "hv:m:M:f:F:a:xyz";
	struct option longopts[] = {
		{ "value", 1, NULL, 'v' },
		{ "min", 1, NULL, 'm' },
		{ "max", 1, NULL, 'M' },
		{ "fuzz", 1, NULL, 'f' },
		{ "flat", 1, NULL, 'F' },
		{ "axis", 1, NULL, 'a' },
		{ 0, 0, 0, 0 }
	};

	__s32 value=0, min=0, max=0, fuzz=0, flat=0, axis=0;
	int valueSet=0, minSet=0, maxSet=0, fuzzSet=0, flatSet=0, axisSet=0;
	struct input_absinfo *ia = NULL;

	progname = argv[0];

	while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
		switch (opt) {
		case 'v': value = strtol(optarg, NULL, 0); valueSet = 1; break;
		case 'm': min = strtol(optarg, NULL, 0); minSet = 1; break;
		case 'M': max = strtol(optarg, NULL, 0); maxSet = 1; break;
		case 'f': fuzz = strtol(optarg, NULL, 0); fuzzSet = 1; break;
		case 'F': flat = strtol(optarg, NULL, 0); flatSet = 1; break;
		case 'a': axis = set_axis(optarg, &axisSet); break;
		case 'x': axis = set_axis("0", &axisSet); break;
		case 'y': axis = set_axis("1", &axisSet); break;
		case 'z': axis = set_axis("2", &axisSet); break;
		case 'h': /* fall through */
		default:
			print_usage();
			exit(1);
		}
	}

	if (optind >= argc) {
		fprintf(stderr, "No device node specified.\n\n");
		print_usage();
		exit(1);
	}

	device_file_name = argv[optind];

	if (!valueSet && !minSet && !maxSet && !fuzzSet && !flatSet && !axisSet) {
		fprintf(stderr, "No param specified.  You must specify at least one param.\n\n");
		print_usage();
		exit(1);
	}

	if (!axisSet) {
		fprintf(stderr, "No axis specified.\n\n");
		print_usage();
		exit(1);
	}

	/* Open device */
	fd = open(device_file_name, O_RDWR);
	if (fd == -1) {
		retval = -errno;
		fprintf(stderr, "Error opening device %s : %s\n", device_file_name, strerror(-retval));
		goto end;
	}

	if (!(ia = malloc(sizeof(*ia)))) {
		fprintf(stderr, "Out of memory!\n");
		retval = -ENOMEM;
		goto end;
	}

	if (!valueSet || !minSet || !maxSet || !fuzzSet || !flatSet || !axisSet) {
		if (ioctl(fd, EVIOCGABS(axis), ia) == -1) {
			retval = -errno;
			fprintf(stderr, "Error getting current axis %d values : %s\n", axis, strerror(-retval));
			goto end;
		}
	}

	if (valueSet)
		ia->value = value;
	if (minSet)
		ia->minimum = min;
	if (maxSet)
		ia->maximum = max;
	if (fuzzSet)
		ia->fuzz = fuzz;
	if (flatSet)
		ia->flat = flat;
	if (ioctl(fd, EVIOCSABS(axis), ia) == -1) {
		fprintf(stderr, "Error setting axis %d values : %s\n", axis, strerror(errno));
		retval = -1;
	} else {
		char *axisstr = "";
		switch (axis) {
		case 0: axisstr = " (X)"; break;
		case 1: axisstr = " (Y)"; break;
		case 2: axisstr = " (Z)"; break;
		}
		printf("Set axis %d%s parameters:\n\tvalue: %d\n\tminimum: %d\n\tmaximum: %d\n\tfuzz: %d\n\tflat: %d\n",
			axis, axisstr, ia->value, ia->minimum, ia->maximum, ia->fuzz, ia->flat);
	}

end:
	if (fd)
		close(fd);
	if (ia)
		free(ia);
	exit(retval);
}

[Index of Archives]     [Linux Media Devel]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Linux Wireless Networking]     [Linux Omap]

  Powered by Linux