Skip to content

Commit 6ca429f

Browse files
committed
Merge pull request tias#47 from schnitzeltony/write-file-param
Add a new parameter --output-filename
2 parents e02de96 + dd263d4 commit 6ca429f

File tree

9 files changed

+217
-67
lines changed

9 files changed

+217
-67
lines changed

src/calibrator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ bool Calibrator::verbose = false;
3737
Calibrator::Calibrator(const char* const device_name0, const XYinfo& axys0,
3838
const int thr_misclick, const int thr_doubleclick,
3939
const OutputType output_type0, const char* geometry0,
40-
const bool use_timeout0)
40+
const bool use_timeout0, const char* output_filename0)
4141
: device_name(device_name0),
4242
threshold_doubleclick(thr_doubleclick), threshold_misclick(thr_misclick),
43-
output_type(output_type0), geometry(geometry0), use_timeout(use_timeout0)
43+
output_type(output_type0), geometry(geometry0), use_timeout(use_timeout0),
44+
output_filename(output_filename0)
4445
{
4546
old_axys = axys0;
4647

src/calibrator.hh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include <stdio.h>
3131
#include <vector>
3232

33+
// XXX: we currently don't handle lines that are longer than this
34+
#define MAX_LINE_LEN 1024
35+
3336
int xf86ScaleAxis(int Cx, int to_max, int to_min, int from_max, int from_min);
3437
float scaleAxis(float Cx, int to_max, int to_min, int from_max, int from_min);
3538

@@ -145,9 +148,10 @@ public:
145148
const int thr_doubleclick=0,
146149
const OutputType output_type=OUTYPE_AUTO,
147150
const char* geometry=0,
148-
const bool use_timeout=1);
151+
const bool use_timeout=1,
152+
const char* output_filename = 0);
149153

150-
~Calibrator() {}
154+
virtual ~Calibrator() {}
151155

152156
/// set the doubleclick treshold
153157
void set_threshold_doubleclick(int t)
@@ -180,6 +184,10 @@ public:
180184
const bool get_use_timeout() const
181185
{ return use_timeout; }
182186

187+
/// get output filename set at cmdline or NULL
188+
const char* get_output_filename() const
189+
{ return output_filename; }
190+
183191
protected:
184192
/// check whether the coordinates are along the respective axis
185193
bool along_axis(int xy, int x0, int y0);
@@ -230,6 +238,9 @@ protected:
230238
const char* geometry;
231239

232240
const bool use_timeout;
241+
242+
// manually specified output filename
243+
const char* output_filename;
233244
};
234245

235246
// Interfance for a CalibratorTester

src/calibrator/Evdev.cpp

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ CalibratorEvdev::CalibratorEvdev(const char* const device_name0,
4747
const int thr_doubleclick,
4848
const OutputType output_type,
4949
const char* geometry,
50-
const bool use_timeout)
51-
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout)
50+
const bool use_timeout,
51+
const char* output_filename)
52+
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout, output_filename)
5253
{
5354
// init
5455
display = XOpenDisplay(NULL);
@@ -166,8 +167,9 @@ CalibratorEvdev::CalibratorEvdev(const char* const device_name0,
166167
const int thr_doubleclick,
167168
const OutputType output_type,
168169
const char* geometry,
169-
const bool use_timeout)
170-
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry) { }
170+
const bool use_timeout,
171+
const char* output_filename)
172+
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, output_filename) { }
171173

172174
// Destructor
173175
CalibratorEvdev::~CalibratorEvdev () {
@@ -515,18 +517,41 @@ bool CalibratorEvdev::output_xorgconfd(const XYinfo new_axys)
515517
if (not_sysfs_name)
516518
sysfs_name = "!!Name_Of_TouchScreen!!";
517519

520+
if(output_filename == NULL || not_sysfs_name)
521+
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");
522+
else
523+
printf(" writing xorg.conf calibration data to '%s'\n", output_filename);
524+
518525
// xorg.conf.d snippet
519-
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");
520-
printf("Section \"InputClass\"\n");
521-
printf(" Identifier \"calibration\"\n");
522-
printf(" MatchProduct \"%s\"\n", sysfs_name);
523-
printf(" Option \"Calibration\" \"%d %d %d %d\"\n",
526+
char line[MAX_LINE_LEN];
527+
std::string outstr;
528+
529+
outstr += "Section \"InputClass\"\n";
530+
outstr += " Identifier \"calibration\"\n";
531+
sprintf(line, " MatchProduct \"%s\"\n", sysfs_name);
532+
outstr += line;
533+
sprintf(line, " Option \"Calibration\" \"%d %d %d %d\"\n",
524534
new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
525-
printf(" Option \"SwapAxes\" \"%d\"\n", new_axys.swap_xy);
526-
printf("EndSection\n");
535+
outstr += line;
536+
sprintf(line, " Option \"SwapAxes\" \"%d\"\n", new_axys.swap_xy);
537+
outstr += line;
538+
outstr += "EndSection\n";
527539

540+
// console out
541+
printf("%s", outstr.c_str());
528542
if (not_sysfs_name)
529543
printf("\nChange '%s' to your device's name in the snippet above.\n", sysfs_name);
544+
// file out
545+
else if(output_filename != NULL) {
546+
FILE* fid = fopen(output_filename, "w");
547+
if (fid == NULL) {
548+
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", output_filename);
549+
fprintf(stderr, "New calibration data NOT saved\n");
550+
return false;
551+
}
552+
fprintf(fid, "%s", outstr.c_str());
553+
fclose(fid);
554+
}
530555

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

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

571+
// HAL policy output
572+
char line[MAX_LINE_LEN];
573+
std::string outstr;
574+
575+
sprintf(line, "<match key=\"info.product\" contains=\"%s\">\n", sysfs_name);
576+
outstr += line;
577+
sprintf(line, " <merge key=\"input.x11_options.calibration\" type=\"string\">%d %d %d %d</merge>\n",
578+
new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
579+
outstr += line;
580+
sprintf(line, " <merge key=\"input.x11_options.swapaxes\" type=\"string\">%d</merge>\n",
581+
new_axys.swap_xy);
582+
outstr += "</match>\n";
583+
// console out
584+
printf("%s", outstr.c_str());
549585
if (not_sysfs_name)
550586
printf("\nChange '%s' to your device's name in the config above.\n", sysfs_name);
587+
// file out
588+
else if(output_filename != NULL) {
589+
FILE* fid = fopen(output_filename, "w");
590+
if (fid == NULL) {
591+
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", output_filename);
592+
fprintf(stderr, "New calibration data NOT saved\n");
593+
return false;
594+
}
595+
fprintf(fid, "%s", outstr.c_str());
596+
fclose(fid);
597+
}
551598

552599
return true;
553600
}
554601

555602
bool CalibratorEvdev::output_xinput(const XYinfo new_axys)
556603
{
604+
if(output_filename == NULL)
605+
printf(" Install the 'xinput' tool and copy the command(s) below in a script that starts with your X session\n");
606+
else
607+
printf(" writing calibration script to '%s'\n", output_filename);
608+
557609
// create startup script
558-
printf(" Install the 'xinput' tool and copy the command(s) below in a script that starts with your X session\n");
559-
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);
560-
printf(" xinput set-int-prop \"%s\" \"Evdev Axes Swap\" 8 %d\n", device_name, new_axys.swap_xy);
610+
char line[MAX_LINE_LEN];
611+
std::string outstr;
612+
613+
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);
614+
outstr += line;
615+
sprintf(line, " xinput set-int-prop \"%s\" \"Evdev Axes Swap\" 8 %d\n", device_name, new_axys.swap_xy);
616+
outstr += line;
617+
618+
// console out
619+
printf("%s", outstr.c_str());
620+
// file out
621+
if(output_filename != NULL) {
622+
FILE* fid = fopen(output_filename, "w");
623+
if (fid == NULL) {
624+
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", output_filename);
625+
fprintf(stderr, "New calibration data NOT saved\n");
626+
return false;
627+
}
628+
fprintf(fid, "%s", outstr.c_str());
629+
fclose(fid);
630+
}
561631

562632
return true;
563633
}

src/calibrator/Evdev.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class CalibratorEvdev: public Calibrator
4747
const int thr_doubleclick=0,
4848
const OutputType output_type=OUTYPE_AUTO,
4949
const char* geometry=0,
50-
const bool use_timeout=false);
50+
const bool use_timeout=false,
51+
const char* output_filename = 0);
5152

5253
public:
5354
CalibratorEvdev(const char* const device_name,
@@ -57,8 +58,9 @@ class CalibratorEvdev: public Calibrator
5758
const int thr_doubleclick=0,
5859
const OutputType output_type=OUTYPE_AUTO,
5960
const char* geometry=0,
60-
const bool use_timeout=false);
61-
~CalibratorEvdev();
61+
const bool use_timeout=false,
62+
const char* output_filename = 0);
63+
virtual ~CalibratorEvdev();
6264

6365
/// calculate and apply the calibration
6466
virtual bool finish(int width, int height);

src/calibrator/Usbtouchscreen.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ static const char *p_flip_x = "flip_x";
4848
static const char *p_flip_y = "flip_y";
4949
static const char *p_swap_xy = "swap_xy";
5050

51-
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)
52-
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout)
51+
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)
52+
: Calibrator(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry, use_timeout, output_filename)
5353
{
5454
if (strcmp(device_name, "Usbtouchscreen") != 0)
5555
throw WrongCalibratorException("Not a usbtouchscreen device");
@@ -104,16 +104,17 @@ bool CalibratorUsbtouchscreen::finish_data(const XYinfo new_axys)
104104
write_bool_parameter(p_swap_xy, new_axys.swap_xy);
105105

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

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

138-
fid = fopen(modprobe_conf_local, "w");
139+
fid = fopen(filename, "w");
139140
if (fid == NULL) {
140-
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", modprobe_conf_local);
141+
fprintf(stderr, "Error: Can't open '%s' for writing. Make sure you have the necessary rights\n", filename);
141142
fprintf(stderr, "New calibration data NOT saved\n");
142143
return false;
143144
}

src/calibrator/Usbtouchscreen.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class CalibratorUsbtouchscreen: public Calibrator
3535
CalibratorUsbtouchscreen(const char* const device_name, const XYinfo& axys,
3636
const int thr_misclick=0, const int thr_doubleclick=0,
3737
const OutputType output_type=OUTYPE_AUTO, const char* geometry=0,
38-
const bool use_timeout=false);
39-
~CalibratorUsbtouchscreen();
38+
const bool use_timeout=false, const char* output_filename = 0);
39+
virtual ~CalibratorUsbtouchscreen();
4040

4141
virtual bool finish_data(const XYinfo new_axys);
4242

0 commit comments

Comments
 (0)