[PATCH] xinput: add set-prop command

Simon Thum simon.thum at gmx.de
Mon Apr 20 05:12:50 PDT 2009


Julien Cristau wrote:
> v2: fixed the float handling for 64bit as Peter did for set-float-prop
[...]
> +    union {
> +        unsigned char *c;
> +        short *s;
> +        long *l;
> +        Atom *a;
> +    } data;
Wouldn't it be necessary to store Atoms long-aligned as well ? 
(disclaimer: I did't look up the Atom definition, nor associated specs)

Cheers,

Simon

> +
> +    if (argc < 3)
> +    {
> +        fprintf(stderr, "Usage: xinput %s %s\n", n, desc);
> +        return EXIT_FAILURE;
> +    }
> +
> +    info = find_device_info(dpy, argv[0], False);
> +    if (!info)
> +    {
> +        fprintf(stderr, "unable to find device %s\n", argv[0]);
> +        return EXIT_FAILURE;
> +    }
> +
> +    dev = XOpenDevice(dpy, info->id);
> +    if (!dev)
> +    {
> +        fprintf(stderr, "unable to open device %s\n", argv[0]);
> +        return EXIT_FAILURE;
> +    }
> +
> +    name = argv[1];
>  
> +    prop = parse_atom(dpy, name);
> +
> +    if (prop == None) {
> +        fprintf(stderr, "invalid property %s\n", name);
> +        return EXIT_FAILURE;
> +    }
> +
> +    float_atom = XInternAtom(dpy, "FLOAT", False);
> +
> +    nelements = argc - 2;
> +    if (XGetDeviceProperty(dpy, dev, prop, 0, 0, False, AnyPropertyType,
> +                           &type, &format, &act_nitems, &bytes_after, &data.c)
> +            != Success) {
> +        fprintf(stderr, "failed to get property type and format for %s\n", name);
> +        return EXIT_FAILURE;
> +    }
> +
> +    XFree(data.c);
> +
> +    if (type == None) {
> +        fprintf(stderr, "property %s doesn't exist\n", name);
> +        return EXIT_FAILURE;
> +    }
> +
> +    data.c = calloc(nelements, sizeof(long));
> +
> +    for (i = 0; i < nelements; i++)
> +    {
> +        if (type == XA_INTEGER) {
> +            switch (format)
> +            {
> +                case 8:
> +                    data.c[i] = atoi(argv[2 + i]);
> +                    break;
> +                case 16:
> +                    data.s[i] = atoi(argv[2 + i]);
> +                    break;
> +                case 32:
> +                    data.l[i] = atoi(argv[2 + i]);
> +                    break;
> +                default:
> +                    fprintf(stderr, "unexpected size for property %s", name);
> +                    return EXIT_FAILURE;
> +            }
> +        } else if (type == float_atom) {
> +            *(float *)(data.l + i) = strtod(argv[2 + i], &endptr);
> +            if (endptr == argv[2 + i]) {
> +                fprintf(stderr, "argument %s could not be parsed\n", argv[2 + i]);
> +                return EXIT_FAILURE;
> +            }
> +        } else if (type == XA_ATOM) {
> +            data.a[i] = parse_atom(dpy, argv[2 + i]);
> +        } else {
> +            fprintf(stderr, "unexpected type for property %s\n", name);
> +            return EXIT_FAILURE;
> +        }
> +    }
> +
> +    XChangeDeviceProperty(dpy, dev, prop, type, format, PropModeReplace,
> +                          data.c, nelements);
> +    free(data.c);
> +    XCloseDevice(dpy, dev);
> +    return EXIT_SUCCESS;
> +}
> diff --git a/src/xinput.c b/src/xinput.c
> index b319326..f584459 100644
> --- a/src/xinput.c
> +++ b/src/xinput.c
> @@ -131,6 +131,10 @@ static entry drivers[] =
>        "<device> <property>",
>        delete_prop
>      },
> +    { "set-prop",
> +      "<device> <property> <val> [<val> ...]",
> +      set_prop
> +    },
>      {NULL, NULL, NULL
>      }
>  };
> diff --git a/src/xinput.h b/src/xinput.h
> index 3c36497..24c0417 100644
> --- a/src/xinput.h
> +++ b/src/xinput.h
> @@ -289,4 +289,15 @@ delete_prop(
>  #endif
>  );
>  
> +int
> +set_prop(
> +#if NeedFunctionPrototypes
> +		Display*	display,
> +		int		argc,
> +		char		*argv[],
> +		char		*prog_name,
> +		char		*prog_desc
> +#endif
> +);
> +
>  /* end of xinput.h */



More information about the xorg-devel mailing list