On Wed, Oct 12, 2011 at 01:12:56PM +0200, Eduard Hasenleithner wrote:
> Hi
> 
> 2011/9/28 Jason Gerecke <[email protected]>:
> > Replaces several near-identical calls to XGetDeviceProperty with
> > a single centralized function to do the same thing. The new '_get'
> > function is responsible for reading the property from the server,
> > verifying its format matches that which is intended, and returning
> > a pointer to the requested piece.
> >
> > Signed-off-by: Jason Gerecke <[email protected]>
> > ---
> >  tools/xsetwacom.c |  217 
> > ++++++++++++++++++++++++++++++++++++-----------------
> >  1 files changed, 147 insertions(+), 70 deletions(-)
> >
> > diff --git a/tools/xsetwacom.c b/tools/xsetwacom.c
> > index 7202b23..392b063 100644
> > --- a/tools/xsetwacom.c
> > +++ b/tools/xsetwacom.c
> > @@ -101,6 +101,8 @@ typedef struct _param
> >
> >
> >  /* get_func/set_func calls for special parameters */
> > +static void* _get_property(Display *dpy, XDevice *dev, const char 
> > *prop_name, int format, Atom type, unsigned long *items);
> > +static void* _get(Display *dpy, XDevice *dev, const char *prop_name, int 
> > prop_offset, int format, Atom type, int items);
> >  static void map_actions(Display *dpy, XDevice *dev, param_t *param, int 
> > argc, char **argv);
> >  static void set_mode(Display *dpy, XDevice *dev, param_t *param, int argc, 
> > char **argv);
> >  static void get_mode(Display *dpy, XDevice *dev, param_t *param, int argc, 
> > char **argv);
> > @@ -1650,6 +1652,127 @@ out:
> >        free(data);
> >  }
> >
> > +/**
> > + * Obtains a property from the server. This function is a wrapper around
> > + * XGetDeviceProperty which ensures that a pointer to the entire property
> > + * data is returned, as well as verifying it has the expected type and
> > + * format. If this function fails for any reason, *items will be zero and
> > + * the function will return NULL.
> > + *
> > + * @param dpy        X11 display to connect to
> > + * @param dev        Device to query
> > + * @param prop_name  Name of device property
> > + * @param format     Format of the property (8/16/32)
> > + * @param type       Type of the property
> > + * @param items      Number of 'format'-bit items successfully retrieved 
> > from the server
> > + * @return           Pointer to the requested data. Must be 'free()'-ed
> > + */
> > +static void* _get_property(Display *dpy, XDevice *dev, const char 
> > *prop_name,
> > +                          int format, Atom type, unsigned long *items)
> > +{
> > +       int read_format, error;
> > +       unsigned long read_items, bytes_after;
> > +       Atom read_prop, read_type;
> > +       unsigned char *read = NULL;
> > +       void *data = NULL;
> > +
> > +       *items = 0;
> > +
> > +       read_prop = XInternAtom(dpy, prop_name, True);
> > +       if (!read_prop || !test_property(dpy, dev, read_prop))
> > +       {
> > +               printf("Property '%s' does not exist on device.\n", 
> > prop_name);
> > +               goto error;
> > +       }
> > +
> > +       do
> > +       {
> > +               int offset = *items * format / 8;
> > +               void *tmp;
> > +
> > +               error = XGetDeviceProperty(dpy, dev, read_prop, offset / 4, 
> > 1,
> > +                                           False, AnyPropertyType, 
> > &read_type,
> > +                                           &read_format, &read_items, 
> > &bytes_after,
> > +                                           &read);
> > +
> > +               if (error != 0)
> > +               {
> > +                       fprintf(stderr, "   %-23s = XGetDeviceProperty 
> > error %d\n",
> > +                               prop_name, error);
> > +                       XFree(read);
> > +                       goto error;
> > +               }
> > +               else if (format != read_format || type != read_type)
> > +               {
> > +                       fprintf(stderr, "   %-23s = property mismatch: 
> > expected type %d, format %d; got type %d, format %d\n",
> > +                               prop_name, format, type, read_format, 
> > read_type);
> > +                       XFree(read);
> > +                       goto error;
> > +               }
> > +
> > +               *items += read_items;
> > +               tmp = realloc(data, *items * format / 8);
> 
> Does this really work for the format==32 && type==XA_INTEGER case,
> considering that sizeof(long)==64 on amd64?

good catch. the type itself doesn't matter either. API stipulates that any
32 bit value is returned as long.

Cheers,
  Peter
 
> I'd suggest a _get_size function:
> size_t _get_size(Atom type, int format)
> {
>       if (type == XA_INTEGER && format == 32)
>               return sizeof(long);
>       else
>               return format / 8;
> }
> 
> and use that in combination with memcpy.
> 
> > +               if (tmp == NULL)
> > +               {
> > +                       fprintf(stderr, "Unable to allocate memory.\n");
> > +                       XFree(read);
> > +                       goto error;
> > +               }
> > +               else
> > +               {
> > +                       data = tmp;
> > +                       memcpy(&data[offset], read, read_items * format / 
> > 8);
> > +                       XFree(read);
> > +               }
> > +       }
> > +       while (bytes_after > 0);
> > +       return data;
> > +
> > +error:
> > +       free(data);
> > +       *items = 0;
> > +       return NULL;
> > +}
> 
> Eduard
> 
> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure contains a
> definitive record of customers, application performance, security
> threats, fraudulent activity and more. Splunk takes this data and makes
> sense of it. Business sense. IT sense. Common sense.
> http://p.sf.net/sfu/splunk-d2d-oct
> _______________________________________________
> Linuxwacom-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel
> 

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2d-oct
_______________________________________________
Linuxwacom-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel

Reply via email to