[PATCH libinput] util: harmonize container_of() definition with linux kernel one

2017-05-15 Thread Gabriel Laskar
commit 3925936 introduced changes to container_of, this is hopefully the
last part of it.

In the linux kernel, container_of() takes a type name, and not a
variable. Without this, in some cases it is needed to declare an unused
variable in order to call container_of().

example:

return container_of(dispatch, struct fallback_dispatch, base);

instead of:

struct fallback_dispatch *p;
return container_of(dispatch, p, base);

This introduce also list_first_entry(), a simple wrapper around
container_of() to retrieve the first element of a non empty list. It
allows to simplify list_for_each() and list_for_each_safe().

Signed-off-by: Gabriel Laskar 
---

As they were no documentation for all the list functions/macros I did not add
one for list_first_entry(), if it is necessary, I will add them.

 src/evdev-lid.c |  4 +---
 src/evdev-mt-touchpad.h |  4 +---
 src/evdev-tablet-pad.h  |  4 +---
 src/evdev-tablet.h  |  4 +---
 src/evdev.h |  8 ++--
 src/libinput-util.h | 19 +++
 6 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/src/evdev-lid.c b/src/evdev-lid.c
index baf7185..9e694ba 100644
--- a/src/evdev-lid.c
+++ b/src/evdev-lid.c
@@ -44,11 +44,9 @@ struct lid_switch_dispatch {
 static inline struct lid_switch_dispatch*
 lid_dispatch(struct evdev_dispatch *dispatch)
 {
-   struct lid_switch_dispatch *l;
-
evdev_verify_dispatch_type(dispatch, DISPATCH_LID_SWITCH);
 
-   return container_of(dispatch, l, base);
+   return container_of(dispatch, struct lid_switch_dispatch, base);
 }
 
 static void
diff --git a/src/evdev-mt-touchpad.h b/src/evdev-mt-touchpad.h
index 304e92f..ef0171d 100644
--- a/src/evdev-mt-touchpad.h
+++ b/src/evdev-mt-touchpad.h
@@ -391,11 +391,9 @@ struct tp_dispatch {
 static inline struct tp_dispatch*
 tp_dispatch(struct evdev_dispatch *dispatch)
 {
-   struct tp_dispatch *tp;
-
evdev_verify_dispatch_type(dispatch, DISPATCH_TOUCHPAD);
 
-   return container_of(dispatch, tp, base);
+   return container_of(dispatch, struct tp_dispatch, base);
 }
 
 #define tp_for_each_touch(_tp, _t) \
diff --git a/src/evdev-tablet-pad.h b/src/evdev-tablet-pad.h
index 5569007..c80e144 100644
--- a/src/evdev-tablet-pad.h
+++ b/src/evdev-tablet-pad.h
@@ -73,11 +73,9 @@ struct pad_dispatch {
 static inline struct pad_dispatch*
 pad_dispatch(struct evdev_dispatch *dispatch)
 {
-   struct pad_dispatch *p;
-
evdev_verify_dispatch_type(dispatch, DISPATCH_TABLET_PAD);
 
-   return container_of(dispatch, p, base);
+   return container_of(dispatch, struct pad_dispatch, base);
 }
 
 static inline struct libinput *
diff --git a/src/evdev-tablet.h b/src/evdev-tablet.h
index 43ed897..7d17e36 100644
--- a/src/evdev-tablet.h
+++ b/src/evdev-tablet.h
@@ -88,11 +88,9 @@ struct tablet_dispatch {
 static inline struct tablet_dispatch*
 tablet_dispatch(struct evdev_dispatch *dispatch)
 {
-   struct tablet_dispatch *t;
-
evdev_verify_dispatch_type(dispatch, DISPATCH_TABLET);
 
-   return container_of(dispatch, t, base);
+   return container_of(dispatch, struct tablet_dispatch, base);
 }
 
 static inline enum libinput_tablet_tool_axis
diff --git a/src/evdev.h b/src/evdev.h
index c9a44f8..2bd58c1 100644
--- a/src/evdev.h
+++ b/src/evdev.h
@@ -247,9 +247,7 @@ struct evdev_device {
 static inline struct evdev_device *
 evdev_device(struct libinput_device *device)
 {
-   struct evdev_device *d;
-
-   return container_of(device, d, base);
+   return container_of(device, struct evdev_device, base);
 }
 
 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
@@ -371,11 +369,9 @@ struct fallback_dispatch {
 static inline struct fallback_dispatch*
 fallback_dispatch(struct evdev_dispatch *dispatch)
 {
-   struct fallback_dispatch *f;
-
evdev_verify_dispatch_type(dispatch, DISPATCH_FALLBACK);
 
-   return container_of(dispatch, f, base);
+   return container_of(dispatch, struct fallback_dispatch, base);
 }
 
 struct evdev_device *
diff --git a/src/libinput-util.h b/src/libinput-util.h
index 6d9bbe2..e34a500 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -86,22 +86,25 @@ void list_insert(struct list *list, struct list *elm);
 void list_remove(struct list *elm);
 bool list_empty(const struct list *list);
 
-#define container_of(ptr, sample, member)  \
-   (__typeof__(sample))((char *)(ptr) -\
-offsetof(__typeof__(*sample), member))
+#define container_of(ptr, type, member)
\
+   (__typeof__(type) *)((char *)(ptr) -\
+offsetof(__typeof__(type), member))
+
+#define list_first_entry(head, pos, member)\
+   container_of((head)->next, __typeof__(*pos), member)
 
 #define list_for_each(pos, head, member)   \
-   

[PATCH libinput 1/2] util: use offsetof in container_of

2017-05-14 Thread Gabriel Laskar
gcc and clang supports offsetof (defined in stddef.h) as defined by C99
and POSIX.1-2001, use it in container_of.

Signed-off-by: Gabriel Laskar 
---
 src/libinput-util.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/libinput-util.h b/src/libinput-util.h
index 9ecded7..6d9bbe2 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -34,6 +34,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -86,8 +87,8 @@ void list_remove(struct list *elm);
 bool list_empty(const struct list *list);
 
 #define container_of(ptr, sample, member)  \
-   (__typeof__(sample))((char *)(ptr)  -   \
-((char *)&((typeof(sample))0)->member))
+   (__typeof__(sample))((char *)(ptr) -\
+offsetof(__typeof__(*sample), member))
 
 #define list_for_each(pos, head, member)   \
for (pos = 0, pos = container_of((head)->next, pos, member);\
-- 
2.12.2

___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel


[PATCH libinput 2/2] udev/hwdb_parser.py: use python3 from env instead of /usr/bin

2017-05-14 Thread Gabriel Laskar
python installation does not always lives in /usr/bin, this allows to
use virtualenv for example.

Signed-off-by: Gabriel Laskar 
---
 udev/parse_hwdb.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/udev/parse_hwdb.py b/udev/parse_hwdb.py
index 97f04f4..b4f0b1b 100755
--- a/udev/parse_hwdb.py
+++ b/udev/parse_hwdb.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python3
+#!/usr/bin/env python3
 # vim: set expandtab shiftwidth=4:
 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
 #
-- 
2.12.2

___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel


[PATCH libinput 0/2] Small fixes

2017-05-14 Thread Gabriel Laskar
As said in
https://lists.freedesktop.org/archives/wayland-devel/2017-May/034074.html, we
probably can assume that offsetof will be supported by the compiler.

Also some bugs came by:
* hwdb_parser.py assumes that python3 lives in /usr/bin, this forbid the usage
  of virtualenv (or a python not installed in /usr/bin)
* The test suite seems broken on recent versions of libcheck, there is a report
  here: https://github.com/libcheck/check/issues/18.

I have seen another issue in the usage of container_of, maybe I am wrong but
here, container_of takes a variable instead of a type name. This causes all the
usages in the following files to have a unused variable in order for
container_of to work:
* src/evdev-lid.c
* src/evdev-mt-touchpad.h
* src/evdev-tablet.h
* src/evdev.h

This requires minimal changes, but a change in the semantics of the version of
container_of that is used to match more closely the one used in the linux
kernel.

Would you care for a patch for that?

Gabriel Laskar (2):
  util: use offsetof in container_of
  udev/hwdb_parser.py: use python3 from env instead of /usr/bin

 src/libinput-util.h | 5 +++--
 udev/parse_hwdb.py  | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

-- 
2.12.2

___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel


Re: [PATCH libinput 1/5] util: drop GCC specifics for container_of

2017-05-10 Thread Gabriel Laskar
On Wed, May 10, 2017 at 01:47:56PM +1000, Peter Hutterer wrote:
> clang supports __typeof__ which was the only real difference. Not sure any
> other compilers matter (that don't support __typeof__)
> 
> Signed-off-by: Peter Hutterer 
> ---
>  src/libinput-util.h | 6 --
>  1 file changed, 6 deletions(-)
> 
> diff --git a/src/libinput-util.h b/src/libinput-util.h
> index 4e97e011..a9a2b660 100644
> --- a/src/libinput-util.h
> +++ b/src/libinput-util.h
> @@ -85,15 +85,9 @@ void list_insert(struct list *list, struct list *elm);
>  void list_remove(struct list *elm);
>  bool list_empty(const struct list *list);
>  
> -#ifdef __GNUC__
>  #define container_of(ptr, sample, member)\
>   (__typeof__(sample))((char *)(ptr)  -   \
>((char *)&(sample)->member - (char *)(sample)))

Is there any reason for container_of to not use offsetof defined in
stddef.h?


-- 
Gabriel Laskar
___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel