Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/calibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,71 @@ bool Calibrator::finish(int width, int height)
new_axis.x.min = round(x_min); new_axis.x.max = round(x_max);
new_axis.y.min = round(y_min); new_axis.y.max = round(y_max);

if (output_type == OUTYPE_CALIBRATOR) {
output_restore_file(width, height);
}

// finish the data, driver/calibrator specific
return finish_data(new_axis);
}

bool Calibrator::try_restore() {
if (!restore_filename)
return false;

std::ifstream fs;
try {
fs.open(restore_filename);

reset();

int width, height;
fs >> width >> height;

for (int i = 0; i < 4; ++i) {
int x, y;
fs >> x >> y;
add_click(x, y);
}
fs.close();

finish(width, height);

if (verbose)
printf("DEBUG: Configuration restored from file: %s\n", restore_filename);

return true;
}
catch (std::exception& e) {
fprintf(stderr, "ERROR: Can't read restore file. %s\n", e.what());
return false;
}
}

bool Calibrator::output_restore_file(int width, int height) {
if (!output_filename) {
fprintf(stderr, "ERROR: Cant' save restore filename, check output filename\n");
return false;
}

try {
std::ofstream fs;
fs.open(output_filename);
fs << width << " " << height << "\n";
for (int i = 0; i < clicked.num; ++i) {
fs << clicked.x[i] << " " << clicked.y[i] << "\n";
}
fs.close();
}
catch (std::exception& e) {
fprintf(stderr, "ERROR: Can't save restore file. %s\n", e.what());
return false;
}

return true;
}


const char* Calibrator::get_sysfs_name()
{
if (is_sysfs_name(device_name))
Expand Down
18 changes: 17 additions & 1 deletion src/calibrator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ enum OutputType {
OUTYPE_AUTO,
OUTYPE_XORGCONFD,
OUTYPE_HAL,
OUTYPE_XINPUT
OUTYPE_XINPUT,
OUTYPE_CALIBRATOR
};

class WrongCalibratorException : public std::invalid_argument {
Expand Down Expand Up @@ -177,6 +178,11 @@ public:
bool add_click(int x, int y);
/// calculate and apply the calibration
virtual bool finish(int width, int height);

/// restores configuration from restore_filename
/// returns true if successful
bool try_restore();

/// get the sysfs name of the device,
/// returns NULL if it can not be found
const char* get_sysfs_name();
Expand All @@ -188,6 +194,10 @@ public:
const char* get_output_filename() const
{ return output_filename; }

/// get restore filename
const char* get_restore_filename() const
{ return restore_filename; }

protected:
/// check whether the coordinates are along the respective axis
bool along_axis(int xy, int x0, int y0);
Expand Down Expand Up @@ -242,9 +252,15 @@ protected:
// manually specified output filename
const char* output_filename;

// manually specified restore filename
const char* restore_filename;

// sysfs path/file
static const char* SYSFS_INPUT;
static const char* SYSFS_DEVNAME;

// saves restore file with specified output filename
bool output_restore_file(int width, int height);
};

// Interfance for a CalibratorTester
Expand Down
11 changes: 11 additions & 0 deletions src/calibrator/Evdev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ bool CalibratorEvdev::finish(int width, int height)
new_axis.x.min = round(x_min); new_axis.x.max = round(x_max);
new_axis.y.min = round(y_min); new_axis.y.max = round(y_max);

if (output_type == OUTYPE_CALIBRATOR) {
output_restore_file(width, height);
}

// finish the data, driver/calibrator specific
return finish_data(new_axis);
}
Expand Down Expand Up @@ -280,6 +284,10 @@ bool CalibratorEvdev::finish_data(const XYinfo new_axys)
// close
XSync(display, False);

// skip saving if restore
if (restore_filename != NULL)
return true;

printf("\t--> Making the calibration permanent <--\n");
switch (output_type) {
case OUTYPE_AUTO:
Expand All @@ -299,6 +307,9 @@ bool CalibratorEvdev::finish_data(const XYinfo new_axys)
case OUTYPE_XINPUT:
success &= output_xinput(new_axys);
break;
case OUTYPE_CALIBRATOR:
// skip saving for this type
break;
default:
fprintf(stderr, "ERROR: Evdev Calibrator does not support the supplied --output-type\n");
success = false;
Expand Down
14 changes: 9 additions & 5 deletions src/calibrator/Usbtouchscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ bool CalibratorUsbtouchscreen::finish_data(const XYinfo new_axys)
write_bool_parameter(p_flip_y, flip_y);
write_bool_parameter(p_swap_xy, new_axys.swap_xy);

// skip saving if restore or save to calibrator config
if (output_type == OUTYPE_CALIBRATOR || restore_filename != NULL)
return true;

// Read, then write calibration parameters to modprobe_conf_local,
// 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;
Expand All @@ -129,11 +133,11 @@ bool CalibratorUsbtouchscreen::finish_data(const XYinfo new_axys)

char *new_opt = new char[opt_len];
sprintf(new_opt, "%s %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%c %s=%c %s=%c %s=%c\n",
opt, p_range_x, range_x, p_range_y, range_y,
p_min_x, new_axys.x.min, p_min_y, new_axys.y.min,
p_max_x, new_axys.x.max, p_max_y, new_axys.y.max,
p_transform_xy, yesno(true), p_flip_x, yesno(flip_x),
p_flip_y, yesno(flip_y), p_swap_xy, yesno(new_axys.swap_xy));
opt, p_range_x, range_x, p_range_y, range_y,
p_min_x, new_axys.x.min, p_min_y, new_axys.y.min,
p_max_x, new_axys.x.max, p_max_y, new_axys.y.max,
p_transform_xy, yesno(true), p_flip_x, yesno(flip_x),
p_flip_y, yesno(flip_y), p_swap_xy, yesno(new_axys.swap_xy));
new_contents += new_opt;

fid = fopen(filename, "w");
Expand Down
6 changes: 6 additions & 0 deletions src/calibrator/XorgPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ bool CalibratorXorgPrint::finish_data(const XYinfo new_axys)
{
bool success = true;

// skip saving if restore
if (restore_filename != NULL)
return true;

printf("\t--> Making the calibration permanent <--\n");
switch (output_type) {
Expand All @@ -54,6 +57,9 @@ bool CalibratorXorgPrint::finish_data(const XYinfo new_axys)
case OUTYPE_HAL:
success &= output_hal(new_axys);
break;
case OUTYPE_CALIBRATOR:
// skip saving for this type
break;
default:
fprintf(stderr, "ERROR: XorgPrint Calibrator does not support the supplied --output-type\n");
success = false;
Expand Down
52 changes: 38 additions & 14 deletions src/main_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,12 @@ static void usage(char* cmd, unsigned thr_misclick)
fprintf(stderr, "\t--precalib: manually provide the current calibration setting (eg. the values in xorg.conf)\n");
fprintf(stderr, "\t--misclick: set the misclick threshold (0=off, default: %i pixels)\n",
thr_misclick);
fprintf(stderr, "\t--output-type <auto|xorg.conf.d|hal|xinput>: type of config to ouput (auto=automatically detect, default: auto)\n");
fprintf(stderr, "\t--output-type <auto|xorg.conf.d|hal|xinput|calibrator>: type of config to ouput (auto=automatically detect, default: auto)\n");
fprintf(stderr, "\t--fake: emulate a fake device (for testing purposes)\n");
fprintf(stderr, "\t--geometry: manually provide the geometry (width and height) for the calibration window\n");
fprintf(stderr, "\t--no-timeout: turns off the timeout\n");
fprintf(stderr, "\t--output-filename: write calibration data to file (USB: override default /etc/modprobe.conf.local\n");
fprintf(stderr, "\t--restore: restore calibration at runtime from config file in calibrator format\n");
}

Calibrator* Calibrator::make_calibrator(int argc, char** argv)
Expand All @@ -214,6 +215,7 @@ Calibrator* Calibrator::make_calibrator(int argc, char** argv)
const char* pre_device = NULL;
const char* geometry = NULL;
const char* output_filename = NULL;
const char* restore_filename = NULL;
unsigned thr_misclick = 15;
unsigned thr_doubleclick = 7;
OutputType output_type = OUTYPE_AUTO;
Expand Down Expand Up @@ -287,6 +289,8 @@ Calibrator* Calibrator::make_calibrator(int argc, char** argv)
output_type = OUTYPE_HAL;
else if (strcmp("xinput", argv[i]) == 0)
output_type = OUTYPE_XINPUT;
else if (strcmp("calibrator", argv[i]) == 0)
output_type = OUTYPE_CALIBRATOR;
else {
fprintf(stderr, "Error: --output-type needs one of auto|xorg.conf.d|hal|xinput.\n\n");
usage(argv[0], thr_misclick);
Expand Down Expand Up @@ -317,7 +321,18 @@ Calibrator* Calibrator::make_calibrator(int argc, char** argv)
// Output file
if (strcmp("--output-filename", argv[i]) == 0) {
output_filename = argv[++i];
}
} else

// Get restore file
if (strcmp("--restore", argv[i]) == 0) {
if (argc > i+1) {
restore_filename = argv[++i];
} else {
fprintf(stderr, "Error: --restore needs one argument.\n\n");
usage(argv[0], thr_misclick);
exit(1);
}
}

// unknown option
else {
Expand Down Expand Up @@ -388,9 +403,10 @@ Calibrator* Calibrator::make_calibrator(int argc, char** argv)


// Different device/driver, different ways to apply the calibration values
Calibrator* calibrator = NULL;
try {
// try Usbtouchscreen driver
return new CalibratorUsbtouchscreen(device_name, device_axys,
calibrator = new CalibratorUsbtouchscreen(device_name, device_axys,
thr_misclick, thr_doubleclick, output_type, geometry,
use_timeout, output_filename);

Expand All @@ -399,19 +415,27 @@ Calibrator* Calibrator::make_calibrator(int argc, char** argv)
printf("DEBUG: Not usbtouchscreen calibrator: %s\n", x.what());
}

try {
// next, try Evdev driver (with XID)
return new CalibratorEvdev(device_name, device_axys, device_id,
thr_misclick, thr_doubleclick, output_type, geometry,
use_timeout, output_filename);
if (!calibrator) {
try {
// next, try Evdev driver (with XID)
calibrator = new CalibratorEvdev(device_name, device_axys, device_id,
thr_misclick, thr_doubleclick, output_type, geometry,
use_timeout, output_filename);

} catch(WrongCalibratorException& x) {
if (verbose)
printf("DEBUG: Not evdev calibrator: %s\n", x.what());
} catch(WrongCalibratorException& x) {
if (verbose)
printf("DEBUG: Not evdev calibrator: %s\n", x.what());
}
}

// lastly, presume a standard Xorg driver (evtouch, mutouch, ...)
return new CalibratorXorgPrint(device_name, device_axys,
thr_misclick, thr_doubleclick, output_type, geometry,
use_timeout, output_filename);
if (!calibrator) {
calibrator = new CalibratorXorgPrint(device_name, device_axys,
thr_misclick, thr_doubleclick, output_type, geometry,
use_timeout, output_filename);
}

calibrator->restore_filename = restore_filename;

return calibrator;
}
2 changes: 2 additions & 0 deletions src/main_gtkmm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
int main(int argc, char** argv)
{
Calibrator* calibrator = Calibrator::make_calibrator(argc, argv);
if (calibrator->try_restore())
return 0;

// GTK-mm setup
Gtk::Main kit(argc, argv);
Expand Down
2 changes: 2 additions & 0 deletions src/main_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
int main(int argc, char** argv)
{
Calibrator* calibrator = Calibrator::make_calibrator(argc, argv);
if (calibrator->try_restore())
return 0;

GuiCalibratorX11::make_instance( calibrator );

Expand Down