Skip to content

Commit

Permalink
Merge pull request tias#47 from schnitzeltony/write-file-param
Browse files Browse the repository at this point in the history
Add a new parameter --output-filename
  • Loading branch information
tias committed Feb 7, 2013
2 parents e02de96 + dd263d4 commit 6ca429f
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 67 deletions.
5 changes: 3 additions & 2 deletions src/calibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ bool Calibrator::verbose = false;
Calibrator::Calibrator(const char* const device_name0, const XYinfo& axys0,
const int thr_misclick, const int thr_doubleclick,
const OutputType output_type0, const char* geometry0,
const bool use_timeout0)
const bool use_timeout0, const char* output_filename0)
: device_name(device_name0),
threshold_doubleclick(thr_doubleclick), threshold_misclick(thr_misclick),
output_type(output_type0), geometry(geometry0), use_timeout(use_timeout0)
output_type(output_type0), geometry(geometry0), use_timeout(use_timeout0),
output_filename(output_filename0)
{
old_axys = axys0;

Expand Down
15 changes: 13 additions & 2 deletions src/calibrator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include <stdio.h>
#include <vector>

// XXX: we currently don't handle lines that are longer than this
#define MAX_LINE_LEN 1024

int xf86ScaleAxis(int Cx, int to_max, int to_min, int from_max, int from_min);
float scaleAxis(float Cx, int to_max, int to_min, int from_max, int from_min);

Expand Down Expand Up @@ -145,9 +148,10 @@ public:
const int thr_doubleclick=0,
const OutputType output_type=OUTYPE_AUTO,
const char* geometry=0,
const bool use_timeout=1);
const bool use_timeout=1,
const char* output_filename = 0);

~Calibrator() {}
virtual ~Calibrator() {}

/// set the doubleclick treshold
void set_threshold_doubleclick(int t)
Expand Down Expand Up @@ -180,6 +184,10 @@ public:
const bool get_use_timeout() const
{ return use_timeout; }

/// get output filename set at cmdline or NULL
const char* get_output_filename() const
{ return output_filename; }

protected:
/// check whether the coordinates are along the respective axis
bool along_axis(int xy, int x0, int y0);
Expand Down Expand Up @@ -230,6 +238,9 @@ protected:
const char* geometry;

const bool use_timeout;

// manually specified output filename
const char* output_filename;
};

// Interfance for a CalibratorTester
Expand Down
112 changes: 91 additions & 21 deletions src/calibrator/Evdev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ CalibratorEvdev::CalibratorEvdev(const char* const device_name0,
const int thr_doubleclick,
const OutputType output_type,
const char* geometry,
const bool use_timeout)
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout)
const bool use_timeout,
const char* output_filename)
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout, output_filename)
{
// init
display = XOpenDisplay(NULL);
Expand Down Expand Up @@ -166,8 +167,9 @@ CalibratorEvdev::CalibratorEvdev(const char* const device_name0,
const int thr_doubleclick,
const OutputType output_type,
const char* geometry,
const bool use_timeout)
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry) { }
const bool use_timeout,
const char* output_filename)
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, output_filename) { }

// Destructor
CalibratorEvdev::~CalibratorEvdev () {
Expand Down Expand Up @@ -515,18 +517,41 @@ bool CalibratorEvdev::output_xorgconfd(const XYinfo new_axys)
if (not_sysfs_name)
sysfs_name = "!!Name_Of_TouchScreen!!";

if(output_filename == NULL || not_sysfs_name)
printf(" copy the snippet below into '/etc/X11/xorg.conf.d/99-calibration.conf' (/usr/share/X11/xorg.conf.d/ in some distro's)\n");
else
printf(" writing xorg.conf calibration data to '%s'\n", output_filename);

// xorg.conf.d snippet
printf(" copy the snippet below into '/etc/X11/xorg.conf.d/99-calibration.conf' (/usr/share/X11/xorg.conf.d/ in some distro's)\n");
printf("Section \"InputClass\"\n");
printf(" Identifier \"calibration\"\n");
printf(" MatchProduct \"%s\"\n", sysfs_name);
printf(" Option \"Calibration\" \"%d %d %d %d\"\n",
char line[MAX_LINE_LEN];
std::string outstr;

outstr += "Section \"InputClass\"\n";
outstr += " Identifier \"calibration\"\n";
sprintf(line, " MatchProduct \"%s\"\n", sysfs_name);
outstr += line;
sprintf(line, " Option \"Calibration\" \"%d %d %d %d\"\n",
new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
printf(" Option \"SwapAxes\" \"%d\"\n", new_axys.swap_xy);
printf("EndSection\n");
outstr += line;
sprintf(line, " Option \"SwapAxes\" \"%d\"\n", new_axys.swap_xy);
outstr += line;
outstr += "EndSection\n";

// console out
printf("%s", outstr.c_str());
if (not_sysfs_name)
printf("\nChange '%s' to your device's name in the snippet above.\n", sysfs_name);
// file out
else if(output_filename != NULL) {
FILE* fid = fopen(output_filename, "w");
if (fid == NULL) {
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", output_filename);
fprintf(stderr, "New calibration data NOT saved\n");
return false;
}
fprintf(fid, "%s", outstr.c_str());
fclose(fid);
}

return true;
}
Expand All @@ -538,26 +563,71 @@ bool CalibratorEvdev::output_hal(const XYinfo new_axys)
if (not_sysfs_name)
sysfs_name = "!!Name_Of_TouchScreen!!";

// HAL policy output
printf(" copy the policy below into '/etc/hal/fdi/policy/touchscreen.fdi'\n\
<match key=\"info.product\" contains=\"%s\">\n\
<merge key=\"input.x11_options.calibration\" type=\"string\">%d %d %d %d</merge>\n"
, sysfs_name, new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
printf(" <merge key=\"input.x11_options.swapaxes\" type=\"string\">%d</merge>\n", new_axys.swap_xy);
printf("</match>\n");
if(output_filename == NULL || not_sysfs_name)
printf(" copy the policy below into '/etc/hal/fdi/policy/touchscreen.fdi'\n");
else
printf(" writing HAL calibration data to '%s'\n", output_filename);

// HAL policy output
char line[MAX_LINE_LEN];
std::string outstr;

sprintf(line, "<match key=\"info.product\" contains=\"%s\">\n", sysfs_name);
outstr += line;
sprintf(line, " <merge key=\"input.x11_options.calibration\" type=\"string\">%d %d %d %d</merge>\n",
new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
outstr += line;
sprintf(line, " <merge key=\"input.x11_options.swapaxes\" type=\"string\">%d</merge>\n",
new_axys.swap_xy);
outstr += "</match>\n";
// console out
printf("%s", outstr.c_str());
if (not_sysfs_name)
printf("\nChange '%s' to your device's name in the config above.\n", sysfs_name);
// file out
else if(output_filename != NULL) {
FILE* fid = fopen(output_filename, "w");
if (fid == NULL) {
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", output_filename);
fprintf(stderr, "New calibration data NOT saved\n");
return false;
}
fprintf(fid, "%s", outstr.c_str());
fclose(fid);
}

return true;
}

bool CalibratorEvdev::output_xinput(const XYinfo new_axys)
{
if(output_filename == NULL)
printf(" Install the 'xinput' tool and copy the command(s) below in a script that starts with your X session\n");
else
printf(" writing calibration script to '%s'\n", output_filename);

// create startup script
printf(" Install the 'xinput' tool and copy the command(s) below in a script that starts with your X session\n");
printf(" xinput set-int-prop \"%s\" \"Evdev Axis Calibration\" 32 %d %d %d %d\n", device_name, new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
printf(" xinput set-int-prop \"%s\" \"Evdev Axes Swap\" 8 %d\n", device_name, new_axys.swap_xy);
char line[MAX_LINE_LEN];
std::string outstr;

sprintf(line, " xinput set-int-prop \"%s\" \"Evdev Axis Calibration\" 32 %d %d %d %d\n", device_name, new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
outstr += line;
sprintf(line, " xinput set-int-prop \"%s\" \"Evdev Axes Swap\" 8 %d\n", device_name, new_axys.swap_xy);
outstr += line;

// console out
printf("%s", outstr.c_str());
// file out
if(output_filename != NULL) {
FILE* fid = fopen(output_filename, "w");
if (fid == NULL) {
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", output_filename);
fprintf(stderr, "New calibration data NOT saved\n");
return false;
}
fprintf(fid, "%s", outstr.c_str());
fclose(fid);
}

return true;
}
8 changes: 5 additions & 3 deletions src/calibrator/Evdev.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class CalibratorEvdev: public Calibrator
const int thr_doubleclick=0,
const OutputType output_type=OUTYPE_AUTO,
const char* geometry=0,
const bool use_timeout=false);
const bool use_timeout=false,
const char* output_filename = 0);

public:
CalibratorEvdev(const char* const device_name,
Expand All @@ -57,8 +58,9 @@ class CalibratorEvdev: public Calibrator
const int thr_doubleclick=0,
const OutputType output_type=OUTYPE_AUTO,
const char* geometry=0,
const bool use_timeout=false);
~CalibratorEvdev();
const bool use_timeout=false,
const char* output_filename = 0);
virtual ~CalibratorEvdev();

/// calculate and apply the calibration
virtual bool finish(int width, int height);
Expand Down
17 changes: 9 additions & 8 deletions src/calibrator/Usbtouchscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ static const char *p_flip_x = "flip_x";
static const char *p_flip_y = "flip_y";
static const char *p_swap_xy = "swap_xy";

CalibratorUsbtouchscreen::CalibratorUsbtouchscreen(const char* const device_name0, const XYinfo& axys0, const int thr_misclick, const int thr_doubleclick, const OutputType output_type, const char* geometry, const bool use_timeout)
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout)
CalibratorUsbtouchscreen::CalibratorUsbtouchscreen(const char* const device_name0, const XYinfo& axys0, const int thr_misclick, const int thr_doubleclick, const OutputType output_type, const char* geometry, const bool use_timeout, const char* output_filename)
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout, output_filename)
{
if (strcmp(device_name, "Usbtouchscreen") != 0)
throw WrongCalibratorException("Not a usbtouchscreen device");
Expand Down Expand Up @@ -104,16 +104,17 @@ bool CalibratorUsbtouchscreen::finish_data(const XYinfo new_axys)
write_bool_parameter(p_swap_xy, new_axys.swap_xy);

// Read, then write calibration parameters to modprobe_conf_local,
// to keep the for the next boot
FILE *fid = fopen(modprobe_conf_local, "r");
// or the file set by --output-filename to keep the for the next boot
const char* filename = output_filename == NULL ? modprobe_conf_local : output_filename;
FILE *fid = fopen(filename, "r");
if (fid == NULL) {
fprintf(stderr, "Error: Can't open '%s' for reading. Make sure you have the necessary rights\n", modprobe_conf_local);
fprintf(stderr, "Error: Can't open '%s' for reading. Make sure you have the necessary rights\n", filename);
fprintf(stderr, "New calibration data NOT saved\n");
return false;
}

std::string new_contents;
const int len = 1024; // XXX: we currently don't handle lines that are longer than this
const int len = MAX_LINE_LEN;
char line[len];
const char *opt = "options usbtouchscreen";
const int opt_len = strlen(opt);
Expand All @@ -135,9 +136,9 @@ bool CalibratorUsbtouchscreen::finish_data(const XYinfo new_axys)
p_flip_y, yesno(flip_y), p_swap_xy, yesno(new_axys.swap_xy));
new_contents += new_opt;

fid = fopen(modprobe_conf_local, "w");
fid = fopen(filename, "w");
if (fid == NULL) {
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", modprobe_conf_local);
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", filename);
fprintf(stderr, "New calibration data NOT saved\n");
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/calibrator/Usbtouchscreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class CalibratorUsbtouchscreen: public Calibrator
CalibratorUsbtouchscreen(const char* const device_name, const XYinfo& axys,
const int thr_misclick=0, const int thr_doubleclick=0,
const OutputType output_type=OUTYPE_AUTO, const char* geometry=0,
const bool use_timeout=false);
~CalibratorUsbtouchscreen();
const bool use_timeout=false, const char* output_filename = 0);
virtual ~CalibratorUsbtouchscreen();

virtual bool finish_data(const XYinfo new_axys);

Expand Down
Loading

0 comments on commit 6ca429f

Please sign in to comment.