Accessing libwacom from Python ctypes

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Accessing libwacom from Python ctypes

J. Carlos Muro
Hello,

I am trying to access stuff of "libwacom" from Python in the following way:

from ctypes import *
from ctypes.util import find_library

libwacom = cdll.LoadLibrary(find_library('wacom'))
db = libwacom.libwacom_database_new()
list = libwacom.libwacom_list_devices_from_database(db, None)

print list  # prints an int value (eg 31310848)

for device in list:
print device

The "for" results in an error:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

And that makes sense because if I am not wrong what libwacom_list_devices_from_database(..) returns is an array of pointers to WacomDevice structs, so the int is the address of the first element of the array. 

I guess that in order to "parse" that array Python would need to know more about such a WacomDevice type but I am not sure how to do that.

I have started today reading about Python ctypes, and I am afraid that this is not so simple.
Has anyone gone through something similar?

Cheers,

Carlos


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

Marcelo Lacerda
I'm by no means a specialist in ctypes but I believe you will have to create your own version of WacomDevice**[1] and set it as the return type[2] of libwacom.libwacom_list_devices_from_database.restype

Something like this, I imagine:

class WACOMDEVICE(Structure):
     _fields_ = ...

WACOMDEVICE_ARRAY = WACOMDEVICE * 256

libwacom.libwacom_list_devices_from_database.restype =
ctypes.POINTER(WACOMDEVICE_ARRAY)


But I would be very impressed if python were able to work with such high level magic, so if all fails you can always go oldschool and read the memory positions one by one and convert them to the appropriated type[3].

Either way I'm sure it will be fun.

On 9 January 2017 at 20:33, J. Carlos Muro <[hidden email]> wrote:
Hello,

I am trying to access stuff of "libwacom" from Python in the following way:

from ctypes import *
from ctypes.util import find_library

libwacom = cdll.LoadLibrary(find_library('wacom'))
db = libwacom.libwacom_database_new()
list = libwacom.libwacom_list_devices_from_database(db, None)

print list  # prints an int value (eg 31310848)

for device in list:
print device

The "for" results in an error:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

And that makes sense because if I am not wrong what libwacom_list_devices_from_database(..) returns is an array of pointers to WacomDevice structs, so the int is the address of the first element of the array. 

I guess that in order to "parse" that array Python would need to know more about such a WacomDevice type but I am not sure how to do that.

I have started today reading about Python ctypes, and I am afraid that this is not so simple.
Has anyone gone through something similar?

Cheers,

Carlos


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss



------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

Peter Hutterer-3
In reply to this post by J. Carlos Muro
On Tue, Jan 10, 2017 at 12:33:19AM +0100, J. Carlos Muro wrote:

> Hello,
>
> I am trying to access stuff of "libwacom" from Python in the following way:
>
> from ctypes import *
> from ctypes.util import find_library
>
> libwacom = cdll.LoadLibrary(find_library('wacom'))
> db = libwacom.libwacom_database_new()
> list = libwacom.libwacom_list_devices_from_database(db, None)
>
> print list  # prints an int value (eg 31310848)
>
> for device in list:
> print device
>
> The "for" results in an error:
>
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable
>
> And that makes sense because if I am not wrong what
> libwacom_list_devices_from_database(..) returns is an array of pointers
> to WacomDevice structs, so the int is the address of the first element of
> the array.
>
> I guess that in order to "parse" that array Python would need to know more
> about such a WacomDevice type but I am not sure how to do that.
>
> I have started today reading about Python ctypes, and I am afraid that this
> is not so simple.
> Has anyone gone through something similar?

have a look at the evemu source code. we ship python bindings with it and
use ctypes for that. https://www.freedesktop.org/wiki/Evemu/

That should get you started. Note I'm by no means an expert in what's best
etc., but that should provide a good-enough blueprint.

Cheers,
   Peter

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

J. Carlos Muro
In reply to this post by Marcelo Lacerda

Definitely, implementing WacomDevice(Structure) seems to be the way to go. 

About the arrays, the "list" returned by "libwacom_list_devices_from_database" is an array of "WacomDevice *" where the last element I belive is NULL (to delimit the list).
This is a reduced version of "libwacom_list_devices_from_database":

WacomDevice**
libwacom_list_devices_from_database(const WacomDeviceDatabase *db, WacomError *error)
{
GList *cur, *devices;
WacomDevice **list, **p;

devices = g_hash_table_get_values (db->device_ht);
list = calloc (g_list_length (devices) + 1, sizeof (WacomDevice *));

for (p = list, cur = devices; cur; cur = g_list_next (cur))
*p++ = (WacomDevice *) cur->data;

return list;
}

The list has potentially a variable length that we don't know ahead. Hence it's probably not a good idea to use a "fixed length array" type to collect the return value. 

I have tried setting "libwacom.libwacom_list_devices_from_database.restype = POINTER(POINTER(WacomDevice))" and that seems to return the list.
I can even iterate it, but I end up getting a "Segmentation fault (core dumped)"

class WacomDevice(Structure):
_fields_ = [
   ("name", c_char_p),
# ...
]

libwacom = cdll.LoadLibrary(find_library('wacom'))
libwacom.libwacom_list_devices_from_database.argtypes=(c_void_p,)
libwacom.libwacom_list_devices_from_database.restype=POINTER(POINTER(WacomDevice))

db = libwacom.libwacom_database_new()
list = libwacom.libwacom_list_devices_from_database(db, None)

for device_p in list:
if device_p == None: break

try: 
if device_p.contents == None: continue
except ValueError:
continue

print "Device:", device_p.contents.name

This is the output:

...
Device: Wacom ISDv4 EF
Device: Wacom Intuos4 WL
Device: Wacom ISDv4 ED
Device: Wacom ISDv4 EC
Device: Wacom DTI520UB/L
Segmentation fault (core dumped)

I have tried reducing the database to only 3 ".tablet" files, and the output is:

Device: Wacom Intuos5 touch L
Device: Wacom Intuos5 touch M
Device: Wacom Intuos5 touch S
Segmentation fault (core dumped)

So the problem seems to be with the last element, the one that is supposed to be NULL in the list. The question is, why doesn't "if device_p == None: break" detecting that NULL? 

The problem seems to be that "device_p" isn't NULL in the last iteration. There, I get a "ValueError" exception if I try to check "if device_p.contents == NULL". That's why I added the "try/except" block. 

To provide more insight, I have iterated the list in a different way:

i = 0
while True:
device_p = list[i]
try: 
if device_p != None: 
print i, "device_p is not NULL", device_p, addressof(device_p)
else:
print i, "device_p is NULL!", device_p, addressof(device_p)
break

if device_p.contents != None: 
print i, "device_p.contents is not NULL", device_p.contents, addressof(device_p.contents)
else:
print i, "device_p.contents is NULL!", device_p.contents, addressof(device_p.contents)
break
except ValueError:
break

print i, "Device:", device_p.contents.name
i += 1

This is the output:

0 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce710> 32983024
0 device_p.contents is not NULL <libwacom.WacomDevice object at 0x7f0772fce830> 32984240
0 Device: Wacom Intuos5 touch L
1 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce830> 32983032
1 device_p.contents is not NULL <libwacom.WacomDevice object at 0x7f0772fce710> 32982464
1 Device: Wacom Intuos5 touch M
2 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce710> 32983040
2 device_p.contents is not NULL <libwacom.WacomDevice object at 0x7f0772fce830> 32973696
2 Device: Wacom Intuos5 touch S
3 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce830> 32983048

Which seems to confirm that "device_p" is not NULL but "device_p.contents" is something that can't even be checked for NULL. Maybe there is a different way to check for NULLs (None) in Python/Ctypes?

So my questions are:
- Am I right assuming that the "list" returned by "libwacom_list_devices_from_database" is a "N+1" elements list where the last one is supposed to be NULL?
- Shouldn't the check "if device_p == None" evaluate to True for the last element of the list?

Many thanks! 
Carlos



On Tue, Jan 10, 2017 at 1:21 AM, Marcelo Lacerda <[hidden email]> wrote:
I'm by no means a specialist in ctypes but I believe you will have to create your own version of WacomDevice**[1] and set it as the return type[2] of libwacom.libwacom_list_devices_from_database.restype

Something like this, I imagine:

class WACOMDEVICE(Structure):
     _fields_ = ...

WACOMDEVICE_ARRAY = WACOMDEVICE * 256

libwacom.libwacom_list_devices_from_database.restype =
ctypes.POINTER(WACOMDEVICE_ARRAY)


But I would be very impressed if python were able to work with such high level magic, so if all fails you can always go oldschool and read the memory positions one by one and convert them to the appropriated type[3].

Either way I'm sure it will be fun.

On 9 January 2017 at 20:33, J. Carlos Muro <[hidden email]> wrote:
Hello,

I am trying to access stuff of "libwacom" from Python in the following way:

from ctypes import *
from ctypes.util import find_library

libwacom = cdll.LoadLibrary(find_library('wacom'))
db = libwacom.libwacom_database_new()
list = libwacom.libwacom_list_devices_from_database(db, None)

print list  # prints an int value (eg 31310848)

for device in list:
print device

The "for" results in an error:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

And that makes sense because if I am not wrong what libwacom_list_devices_from_database(..) returns is an array of pointers to WacomDevice structs, so the int is the address of the first element of the array. 

I guess that in order to "parse" that array Python would need to know more about such a WacomDevice type but I am not sure how to do that.

I have started today reading about Python ctypes, and I am afraid that this is not so simple.
Has anyone gone through something similar?

Cheers,

Carlos


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss




------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

J. Carlos Muro
In reply to this post by Peter Hutterer-3
That was super helpful! I have now implemented my own libwacom wrapper based on that and can detect my own device, print stuff about it, etc.. 
Thank you very much! 

Cheers,
Carlos

On Tue, Jan 10, 2017 at 4:57 AM, Peter Hutterer <[hidden email]> wrote:
On Tue, Jan 10, 2017 at 12:33:19AM +0100, J. Carlos Muro wrote:
> Hello,
>
> I am trying to access stuff of "libwacom" from Python in the following way:
>
> from ctypes import *
> from ctypes.util import find_library
>
> libwacom = cdll.LoadLibrary(find_library('wacom'))
> db = libwacom.libwacom_database_new()
> list = libwacom.libwacom_list_devices_from_database(db, None)
>
> print list  # prints an int value (eg 31310848)
>
> for device in list:
> print device
>
> The "for" results in an error:
>
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable
>
> And that makes sense because if I am not wrong what
> libwacom_list_devices_from_database(..) returns is an array of pointers
> to WacomDevice structs, so the int is the address of the first element of
> the array.
>
> I guess that in order to "parse" that array Python would need to know more
> about such a WacomDevice type but I am not sure how to do that.
>
> I have started today reading about Python ctypes, and I am afraid that this
> is not so simple.
> Has anyone gone through something similar?

have a look at the evemu source code. we ship python bindings with it and
use ctypes for that. https://www.freedesktop.org/wiki/Evemu/

That should get you started. Note I'm by no means an expert in what's best
etc., but that should provide a good-enough blueprint.

Cheers,
   Peter


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

Peter Hutterer-3
On Sun, Jan 15, 2017 at 01:25:26PM +0100, J. Carlos Muro wrote:
> That was super helpful! I have now implemented my own libwacom wrapper
> based on that and can detect my own device, print stuff about it, etc..
> Thank you very much!

Great! Do you want to link to it? it may come in handy for others too.

Cheers,
   Peter

> On Tue, Jan 10, 2017 at 4:57 AM, Peter Hutterer <[hidden email]>
> wrote:
>
> > On Tue, Jan 10, 2017 at 12:33:19AM +0100, J. Carlos Muro wrote:
> > > Hello,
> > >
> > > I am trying to access stuff of "libwacom" from Python in the following
> > way:
> > >
> > > from ctypes import *
> > > from ctypes.util import find_library
> > >
> > > libwacom = cdll.LoadLibrary(find_library('wacom'))
> > > db = libwacom.libwacom_database_new()
> > > list = libwacom.libwacom_list_devices_from_database(db, None)
> > >
> > > print list  # prints an int value (eg 31310848)
> > >
> > > for device in list:
> > > print device
> > >
> > > The "for" results in an error:
> > >
> > > Traceback (most recent call last):
> > >  File "<stdin>", line 1, in <module>
> > > TypeError: 'int' object is not iterable
> > >
> > > And that makes sense because if I am not wrong what
> > > libwacom_list_devices_from_database(..) returns is an array of pointers
> > > to WacomDevice structs, so the int is the address of the first element of
> > > the array.
> > >
> > > I guess that in order to "parse" that array Python would need to know
> > more
> > > about such a WacomDevice type but I am not sure how to do that.
> > >
> > > I have started today reading about Python ctypes, and I am afraid that
> > this
> > > is not so simple.
> > > Has anyone gone through something similar?
> >
> > have a look at the evemu source code. we ship python bindings with it and
> > use ctypes for that. https://www.freedesktop.org/wiki/Evemu/
> >
> > That should get you started. Note I'm by no means an expert in what's best
> > etc., but that should provide a good-enough blueprint.
> >
> > Cheers,
> >    Peter
> >

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

Peter Hutterer-3
In reply to this post by J. Carlos Muro
On Sun, Jan 15, 2017 at 01:19:25PM +0100, J. Carlos Muro wrote:

> Definitely, implementing WacomDevice(Structure) seems to be the way to go.
>
> About the arrays, the "list" returned by
> "libwacom_list_devices_from_database" is an array of "WacomDevice *" where
> the last element I belive is NULL (to delimit the list).
> This is a reduced version of "libwacom_list_devices_from_database":
>
> WacomDevice**
> libwacom_list_devices_from_database(const WacomDeviceDatabase *db,
> WacomError *error)
> {
> GList *cur, *devices;
> WacomDevice **list, **p;
>
> devices = g_hash_table_get_values (db->device_ht);
> list = calloc (g_list_length (devices) + 1, sizeof (WacomDevice *));
>
> for (p = list, cur = devices; cur; cur = g_list_next (cur))
> *p++ = (WacomDevice *) cur->data;
>
> return list;
> }
>
> The list has potentially a variable length that we don't know ahead. Hence
> it's probably not a good idea to use a "fixed length array" type to collect
> the return value.
>
> I have tried setting "libwacom.libwacom_list_devices_from_database.restype
> = POINTER(POINTER(WacomDevice))" and that seems to return the list.
> I can even iterate it, but I end up getting a "Segmentation fault (core
> dumped)"
>
> class WacomDevice(Structure):
> _fields_ = [
>    ("name", c_char_p),
> # ...
> ]
>
> libwacom = cdll.LoadLibrary(find_library('wacom'))
> libwacom.libwacom_list_devices_from_database.argtypes=(c_void_p,)
> libwacom.libwacom_list_devices_from_database.restype=POINTER(POINTER(WacomDevice))
>
> db = libwacom.libwacom_database_new()
> list = libwacom.libwacom_list_devices_from_database(db, None)
>
> for device_p in list:
> if device_p == None: break
>
> try:
> if device_p.contents == None: continue
> except ValueError:
> continue
>
> print "Device:", device_p.contents.name
>
> This is the output:
>
> ...
> Device: Wacom ISDv4 EF
> Device: Wacom Intuos4 WL
> Device: Wacom ISDv4 ED
> Device: Wacom ISDv4 EC
> Device: Wacom DTI520UB/L
> Segmentation fault (core dumped)
>
> I have tried reducing the database to only 3 ".tablet" files, and the
> output is:
>
> Device: Wacom Intuos5 touch L
> Device: Wacom Intuos5 touch M
> Device: Wacom Intuos5 touch S
> Segmentation fault (core dumped)
>
> So the problem seems to be with the last element, the one that is supposed
> to be NULL in the list. The question is, why doesn't "if device_p == None:
> break" detecting that NULL?
>
> The problem seems to be that "device_p" isn't NULL in the last iteration.
> There, I get a "ValueError" exception if I try to check "if
> device_p.contents == NULL". That's why I added the "try/except" block.
>
> To provide more insight, I have iterated the list in a different way:
>
> i = 0
> while True:
> device_p = list[i]
> try:
> if device_p != None:
> print i, "device_p is not NULL", device_p, addressof(device_p)
> else:
> print i, "device_p is NULL!", device_p, addressof(device_p)
> break
>
> if device_p.contents != None:
> print i, "device_p.contents is not NULL", device_p.contents,
> addressof(device_p.contents)
> else:
> print i, "device_p.contents is NULL!", device_p.contents,
> addressof(device_p.contents)
> break
> except ValueError:
> break
>
> print i, "Device:", device_p.contents.name
> i += 1
>
> This is the output:
>
> 0 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce710>
> 32983024
> 0 device_p.contents is not NULL <libwacom.WacomDevice object at
> 0x7f0772fce830> 32984240
> 0 Device: Wacom Intuos5 touch L
> 1 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce830>
> 32983032
> 1 device_p.contents is not NULL <libwacom.WacomDevice object at
> 0x7f0772fce710> 32982464
> 1 Device: Wacom Intuos5 touch M
> 2 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce710>
> 32983040
> 2 device_p.contents is not NULL <libwacom.WacomDevice object at
> 0x7f0772fce830> 32973696
> 2 Device: Wacom Intuos5 touch S
> 3 device_p is not NULL <libwacom.LP_WacomDevice object at 0x7f0772fce830>
> 32983048
>
> Which seems to confirm that "device_p" is not NULL but "device_p.contents"
> is something that can't even be checked for NULL. Maybe there is a
> different way to check for NULLs (None) in Python/Ctypes?
>
> So my questions are:
> - Am I right assuming that the "list" returned by
> "libwacom_list_devices_from_database" is a "N+1" elements list where the
> last one is supposed to be NULL?

yes, it should be.

> - Shouldn't the check "if device_p == None" evaluate to True for the last
> element of the list?

this one I don't know for sure, sorry. but I vaguely remember that
null-pointers in ctypes aren't that obvious and checking for a null-pointer
(iirc) was to check directly whether it was false, i.e. use 'if not
device_p' instead of the None comparison. Think of it like a special type
that isn't None, similar to 'if False == None' failing even though both
individually would evaluate to false.

Cheers,
   Peter

> On Tue, Jan 10, 2017 at 1:21 AM, Marcelo Lacerda <[hidden email]>
> wrote:
>
> > I'm by no means a specialist in ctypes but I believe you will have to
> > create your own version of WacomDevice**[1] and set it as the return
> > type[2] of libwacom.libwacom_list_devices_from_database.restype
> >
> > Something like this, I imagine:
> >
> > class WACOMDEVICE(Structure):     _fields_ = ...
> >
> > WACOMDEVICE_ARRAY = WACOMDEVICE * 256
> >
> > libwacom.libwacom_list_devices_from_database.restype =
> >     ctypes.POINTER(WACOMDEVICE_ARRAY)
> >
> >
> >
> > But I would be very impressed if python were able to work with such high
> > level magic, so if all fails you can always go oldschool and read the
> > memory positions one by one and convert them to the appropriated type[3].
> >
> > Either way I'm sure it will be fun.
> >
> > [1] - https://docs.python.org/2/library/ctypes.html#arrays
> > [2] - https://docs.python.org/2/library/ctypes.html#return-types
> > [3] - http://stackoverflow.com/questions/1555944/how-to-
> > dereference-a-memory-location-from-python-ctypes
> >
> >
> >
> > On 9 January 2017 at 20:33, J. Carlos Muro <[hidden email]> wrote:
> >
> >> Hello,
> >>
> >> I am trying to access stuff of "libwacom" from Python in the following
> >> way:
> >>
> >> from ctypes import *
> >> from ctypes.util import find_library
> >>
> >> libwacom = cdll.LoadLibrary(find_library('wacom'))
> >> db = libwacom.libwacom_database_new()
> >> list = libwacom.libwacom_list_devices_from_database(db, None)
> >>
> >> print list  # prints an int value (eg 31310848)
> >>
> >> for device in list:
> >> print device
> >>
> >> The "for" results in an error:
> >>
> >> Traceback (most recent call last):
> >>  File "<stdin>", line 1, in <module>
> >> TypeError: 'int' object is not iterable
> >>
> >> And that makes sense because if I am not wrong what
> >> libwacom_list_devices_from_database(..) returns is an array of pointers
> >> to WacomDevice structs, so the int is the address of the first element of
> >> the array.
> >>
> >> I guess that in order to "parse" that array Python would need to know
> >> more about such a WacomDevice type but I am not sure how to do that.
> >>
> >> I have started today reading about Python ctypes, and I am afraid that
> >> this is not so simple.
> >> Has anyone gone through something similar?
> >>
> >> Cheers,
> >>
> >> Carlos
> >>
> >>
> >> ------------------------------------------------------------
> >> ------------------
> >> Developer Access Program for Intel Xeon Phi Processors
> >> Access to Intel Xeon Phi processor-based developer platforms.
> >> With one year of Intel Parallel Studio XE.
> >> Training and support from Colfax.
> >> Order your platform today. http://sdm.link/xeonphi
> >> _______________________________________________
> >> Linuxwacom-discuss mailing list
> >> [hidden email]
> >> https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
> >>
> >>
> >

> ------------------------------------------------------------------------------
> Developer Access Program for Intel Xeon Phi Processors
> Access to Intel Xeon Phi processor-based developer platforms.
> With one year of Intel Parallel Studio XE.
> Training and support from Colfax.
> Order your platform today. http://sdm.link/xeonphi

> _______________________________________________
> Linuxwacom-discuss mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

J. Carlos Muro
In reply to this post by Peter Hutterer-3
That would be my pleasure! 
I will see if I can upload something more or less presentable to my brand new GitHub repo. 

This is part of a GUI app (GSetWacom) that I started a couple of weeks ago more like a "just for fun" thing. It was probably pushed by Santa delivering a new Wacom tablet to my daughter last Xmas. I have to mention that I am by no means a Python expert whatsoever. 

Do you think "libwacom.py" is fine as a name for the Python module, or it might conflict at some point with the "Libwacom" project? 

This is the way it can be used (based upon libwacom.h) so ar:

from libwacom import LibWacom, WacomDevice, LibraryWrapperException, WacomFallbackFlags

_lw = LibWacom()
_db = _lw.libwacom_database_new()
_error = _lw.libwacom_error_new()

try:
#_device = _lw.libwacom_new_from_path(_db, DEVICE_PATH[0], WacomFallbackFlags.WFALLBACK_NONE, _error)
_device = _lw.libwacom_new_from_usbid(_db, DEVICE_VENDOR, DEVICE_PRODUCT, _error)
if _device != None:
_lw.libwacom_print_device_description(1, _device)
except LibraryWrapperException as lwe:
print_libwacom_last_error(_lw, _error)
return 1
finally:
if _device: 
_lw.libwacom_destroy(_device)
_lw.libwacom_database_destroy(_db)
_lw.libwacom_error_free(c_void_p(_error))

The next step is wrapping the device object on a higher level class that handles errors, device keep-alive check thread, maintain properties of sub-devices (tablet, pen, touch, ..), etc... 

I still have some more questions that I think I'd better send in new posts. Do you think this is the correct list or rather I should be sending these questions to "linuxwacom-devel"?

Cheers,
Carlos

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing libwacom from Python ctypes

Peter Hutterer-3
On Mon, Jan 16, 2017 at 12:10:15PM +0100, J. Carlos Muro wrote:

> That would be my pleasure!
> I will see if I can upload something more or less presentable to my brand
> new GitHub repo.
>
> This is part of a GUI app (GSetWacom) that I started a couple of weeks ago
> more like a "just for fun" thing. It was probably pushed by Santa
> delivering a new Wacom tablet to my daughter last Xmas. I have to mention
> that I am by no means a Python expert whatsoever.
>
> Do you think "libwacom.py" is fine as a name for the Python module, or it
> might conflict at some point with the "Libwacom" project?

I think the traditional convention is py<projectname> or
python-<projectname>. I'd go with the latter, it avoids confusion. We have
no plans of adding python bindings to libwacom at this point, if we ever do
we'll happily assimilate any project that already provides them ;)

> This is the way it can be used (based upon libwacom.h) so ar:
>
> from libwacom import LibWacom, WacomDevice, LibraryWrapperException,
> WacomFallbackFlags
>
> _lw = LibWacom()
> _db = _lw.libwacom_database_new()
> _error = _lw.libwacom_error_new()

> try:
> #_device = _lw.libwacom_new_from_path(_db, DEVICE_PATH[0],
> WacomFallbackFlags.WFALLBACK_NONE, _error)
> _device = _lw.libwacom_new_from_usbid(_db, DEVICE_VENDOR, DEVICE_PRODUCT,
> _error)
> if _device != None:
> _lw.libwacom_print_device_description(1, _device)
> except LibraryWrapperException as lwe:
> print_libwacom_last_error(_lw, _error)
> return 1
> finally:
> if _device:
> _lw.libwacom_destroy(_device)
> _lw.libwacom_database_destroy(_db)
> _lw.libwacom_error_free(c_void_p(_error))
>
> The next step is wrapping the device object on a higher level class that
> handles errors, device keep-alive check thread, maintain properties of
> sub-devices (tablet, pen, touch, ..), etc...
>
> I still have some more questions that I think I'd better send in new posts.
> Do you think this is the correct list or rather I should be sending these
> questions to "linuxwacom-devel"?

good question. I'd send to -devel because it's more likely that developers
hang out there, which gets you higher exposure to Python experience (and
libwacom experience).

Cheers,
   Peter


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Linuxwacom-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-discuss
Loading...