Skip to content

Commit 9b0803b

Browse files
tonio73tias
authored andcommitted
Fixing xinput_do_set_prop into cleaner xinput_do_set_int_prop Also fixing property issue on invert axes
1 parent 41e0d3a commit 9b0803b

File tree

2 files changed

+59
-108
lines changed

2 files changed

+59
-108
lines changed

src/calibrator/Evdev.cpp

Lines changed: 54 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -308,80 +308,63 @@ bool CalibratorEvdev::set_swapxy(const int swap_xy)
308308
printf("\tSwapping X and Y axis...\n");
309309

310310
// xinput set-int-prop "divername" "Evdev Axes Swap" 8 0
311-
const char* arr_cmd[3];
312-
//arr_cmd[0] = "";
313-
arr_cmd[1] = "Evdev Axes Swap";
314-
char str_swap_xy[20];
315-
snprintf(str_swap_xy, 20, "%d", swap_xy);
316-
arr_cmd[2] = str_swap_xy;
311+
int arr_cmd[1];
312+
arr_cmd[0] = swap_xy;
317313

318-
int ret = xinput_do_set_prop(display, XA_INTEGER, 8, 3, arr_cmd);
314+
bool ret = xinput_do_set_int_prop("Evdev Axes Swap", display, 8, 1, arr_cmd);
319315

320316
if (verbose) {
321-
if (ret == EXIT_SUCCESS)
317+
if (ret == true)
322318
printf("DEBUG: Successfully set swapped X and Y axes = %d.\n", swap_xy);
323319
else
324320
printf("DEBUG: Failed to set swap X and Y axes.\n");
325321
}
326322

327-
return (ret == EXIT_SUCCESS);
323+
return ret;
328324
}
329325

330326
bool CalibratorEvdev::set_invert_xy(const int invert_x, const int invert_y)
331327
{
332328
printf("\tInverting X and/or Y axis...\n");
333329

334330
// xinput set-int-prop "divername" "Evdev Axis Inversion" 8 0 0
335-
const char* arr_cmd[3];
336-
//arr_cmd[0] = "";
337-
arr_cmd[1] = "Evdev Axis Inversion";
338-
char str_val[20];
339-
snprintf(str_val, 20, "%d %d", invert_x, invert_y);
340-
arr_cmd[2] = str_val;
331+
int arr_cmd[2];
332+
arr_cmd[0] = invert_x;
333+
arr_cmd[1] = invert_y;
341334

342-
int ret = xinput_do_set_prop(display, XA_INTEGER, 8, 3, arr_cmd);
335+
int ret = xinput_do_set_int_prop("Evdev Axis Inversion", display, 8, 2, arr_cmd);
343336

344337
if (verbose) {
345-
if (ret == EXIT_SUCCESS)
338+
if (ret == true)
346339
printf("DEBUG: Successfully set invert axis X=%d, Y=%d.\n", invert_x, invert_y);
347340
else
348341
printf("DEBUG: Failed to set axis inversion.\n");
349342
}
350343

351-
return (ret == EXIT_SUCCESS);
344+
return ret;
352345
}
353346

354347
bool CalibratorEvdev::set_calibration(const XYinfo new_axys)
355348
{
356349
printf("\tSetting calibration data: %d, %d, %d, %d\n", new_axys.x.min, new_axys.x.max, new_axys.y.min, new_axys.y.max);
357350

358351
// xinput set-int-prop 4 223 32 5 500 8 300
359-
const char* arr_cmd[6];
360-
//arr_cmd[0] = "";
361-
arr_cmd[1] = "Evdev Axis Calibration";
362-
char str_min_x[20];
363-
sprintf(str_min_x, "%d", new_axys.x.min);
364-
arr_cmd[2] = str_min_x;
365-
char str_max_x[20];
366-
sprintf(str_max_x, "%d", new_axys.x.max);
367-
arr_cmd[3] = str_max_x;
368-
char str_min_y[20];
369-
sprintf(str_min_y, "%d", new_axys.y.min);
370-
arr_cmd[4] = str_min_y;
371-
char str_max_y[20];
372-
sprintf(str_max_y, "%d", new_axys.y.max);
373-
arr_cmd[5] = str_max_y;
374-
375-
int ret = xinput_do_set_prop(display, XA_INTEGER, 32, 6, arr_cmd);
352+
int arr_cmd[4];
353+
arr_cmd[0] = new_axys.x.min;
354+
arr_cmd[1] = new_axys.x.max;
355+
arr_cmd[2] = new_axys.y.min;
356+
arr_cmd[3] = new_axys.y.max;
357+
358+
int ret = xinput_do_set_int_prop("Evdev Axis Calibration", display, 32, 4, arr_cmd);
376359

377360
if (verbose) {
378-
if (ret == EXIT_SUCCESS)
361+
if (ret == true)
379362
printf("DEBUG: Successfully applied axis calibration.\n");
380363
else
381364
printf("DEBUG: Failed to apply axis calibration.\n");
382365
}
383366

384-
return (ret == EXIT_SUCCESS);
367+
return ret;
385368
}
386369

387370
Atom CalibratorEvdev::xinput_parse_atom(Display *display, const char *name)
@@ -445,116 +428,80 @@ Display *display, const char *name, Bool only_extended)
445428
return found;
446429
}
447430

448-
int CalibratorEvdev::xinput_do_set_prop(Display *display, Atom type, int format, int argc, const char **argv)
431+
// Set Integer property on X
432+
bool CalibratorEvdev::xinput_do_set_int_prop( const char * name,
433+
Display *display,
434+
int format,
435+
int argc,
436+
const int *argv )
449437
{
450438
#ifndef HAVE_XI_PROP
451-
return EXIT_FAILURE;
439+
return false;
452440
#else
453441

454442
Atom prop;
455443
Atom old_type;
456-
const char *name;
457444
int i;
458-
Atom float_atom;
459-
int old_format, nelements = 0;
445+
int old_format;
460446
unsigned long act_nitems, bytes_after;
461-
char *endptr;
447+
462448
union {
463449
unsigned char *c;
464450
short *s;
465451
long *l;
466452
Atom *a;
467453
} data;
468454

469-
if (argc < 3)
455+
if (argc < 1)
470456
{
471-
fprintf(stderr, "Wrong usage of xinput_do_set_prop, need at least 3 arguments\n");
472-
return EXIT_FAILURE;
457+
fprintf(stderr, "Wrong usage of xinput_do_set_prop, need at least 1 arguments\n");
458+
return false;
473459
}
474460

475-
name = argv[1];
476-
477461
prop = xinput_parse_atom(display, name);
478462

479463
if (prop == None) {
480464
fprintf(stderr, "invalid property %s\n", name);
481-
return EXIT_FAILURE;
465+
return false;
482466
}
483467

484-
float_atom = XInternAtom(display, "FLOAT", False);
485-
486-
nelements = argc - 2;
487-
if (type == None || format == 0) {
468+
if ( format == 0) {
488469
if (XGetDeviceProperty(display, dev, prop, 0, 0, False, AnyPropertyType,
489470
&old_type, &old_format, &act_nitems,
490471
&bytes_after, &data.c) != Success) {
491472
fprintf(stderr, "failed to get property type and format for %s\n",
492473
name);
493-
return EXIT_FAILURE;
474+
return false;
494475
} else {
495-
if (type == None)
496-
type = old_type;
497-
if (format == 0)
498-
format = old_format;
476+
format = old_format;
499477
}
500478

501479
XFree(data.c);
502480
}
503481

504-
if (type == None) {
505-
fprintf(stderr, "property %s doesn't exist, you need to specify "
506-
"its type and format\n", name);
507-
return EXIT_FAILURE;
508-
}
482+
data.c = (unsigned char*)calloc(argc, sizeof(long));
509483

510-
data.c = (unsigned char*)calloc(nelements, sizeof(long));
484+
for (i = 0; i < argc; i++) {
485+
switch (format) {
486+
case 8:
487+
data.c[i] = argv[i];
488+
case 16:
489+
data.s[i] = argv[i];
490+
break;
491+
case 32:
492+
data.l[i] = argv[i];
493+
break;
511494

512-
for (i = 0; i < nelements; i++)
513-
{
514-
if (type == XA_INTEGER) {
515-
switch (format)
516-
{
517-
case 8:
518-
data.c[i] = atoi(argv[2 + i]);
519-
break;
520-
case 16:
521-
data.s[i] = atoi(argv[2 + i]);
522-
break;
523-
case 32:
524-
data.l[i] = atoi(argv[2 + i]);
525-
break;
526-
default:
527-
fprintf(stderr, "unexpected size for property %s", name);
528-
return EXIT_FAILURE;
529-
}
530-
} else if (type == float_atom) {
531-
if (format != 32) {
532-
fprintf(stderr, "unexpected format %d for property %s\n",
533-
format, name);
534-
return EXIT_FAILURE;
535-
}
536-
*(float *)(data.l + i) = strtod(argv[2 + i], &endptr);
537-
if (endptr == argv[2 + i]) {
538-
fprintf(stderr, "argument %s could not be parsed\n", argv[2 + i]);
539-
return EXIT_FAILURE;
540-
}
541-
} else if (type == XA_ATOM) {
542-
if (format != 32) {
543-
fprintf(stderr, "unexpected format %d for property %s\n",
544-
format, name);
545-
return EXIT_FAILURE;
546-
}
547-
data.a[i] = xinput_parse_atom(display, argv[2 + i]);
548-
} else {
549-
fprintf(stderr, "unexpected type for property %s\n", name);
550-
return EXIT_FAILURE;
551-
}
495+
default:
496+
fprintf(stderr, "unexpected size for property %s\n", name);
497+
return false;
498+
}
552499
}
553500

554-
XChangeDeviceProperty(display, dev, prop, type, format, PropModeReplace,
555-
data.c, nelements);
501+
XChangeDeviceProperty(display, dev, prop, XA_INTEGER, format, PropModeReplace,
502+
data.c, argc);
556503
free(data.c);
557-
return EXIT_SUCCESS;
504+
return true;
558505
#endif // HAVE_XI_PROP
559506

560507
}

src/calibrator/Evdev.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ class CalibratorEvdev: public Calibrator
6969
// xinput_ functions (from the xinput project)
7070
Atom xinput_parse_atom(Display *display, const char* name);
7171
XDeviceInfo* xinput_find_device_info(Display *display, const char* name, Bool only_extended);
72-
int xinput_do_set_prop(Display *display, Atom type, int format, int argc, const char** argv);
72+
bool xinput_do_set_int_prop( const char * name,
73+
Display *display,
74+
int format,
75+
int argc,
76+
const int* argv);
7377
protected:
7478
bool output_xorgconfd(const XYinfo new_axys);
7579
bool output_hal(const XYinfo new_axys);

0 commit comments

Comments
 (0)