From 4511fe7778dafe3cdeacff72cddd7bc15a4f4b42 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Thu, 19 May 2016 15:28:28 +0200
Subject: [PATCH] add key modifiers to button event

---
 common/utils/T/tracer/gui/container.c       |  8 ++++----
 common/utils/T/tracer/gui/gui.h             |  5 +++++
 common/utils/T/tracer/gui/gui_defs.h        |  3 ++-
 common/utils/T/tracer/gui/label.c           |  6 ++++--
 common/utils/T/tracer/gui/positioner.c      |  5 +++--
 common/utils/T/tracer/gui/textlist.c        |  3 ++-
 common/utils/T/tracer/gui/timeline.c        |  3 ++-
 common/utils/T/tracer/gui/toplevel_window.c |  5 +++--
 common/utils/T/tracer/gui/widget.c          |  8 ++++----
 common/utils/T/tracer/gui/x.c               | 12 ++++++++++--
 10 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/common/utils/T/tracer/gui/container.c b/common/utils/T/tracer/gui/container.c
index d3ce2a110b..8de4347933 100644
--- a/common/utils/T/tracer/gui/container.c
+++ b/common/utils/T/tracer/gui/container.c
@@ -227,7 +227,7 @@ static void horizontal_hints(gui *_gui, widget *_w, int *width, int *height)
 }
 
 static void horizontal_button(gui *_g, widget *_this, int x, int y,
-    int button, int up)
+    int key_modifiers, int button, int up)
 {
   LOGD("BUTTON container horizontal %p xy %d %d button %d up %d\n", _this, x, y, button, up);
   struct gui *g = _g;
@@ -237,7 +237,7 @@ static void horizontal_button(gui *_g, widget *_this, int x, int y,
   l = this->common.children;
   while (l) {
     if (l->item->x <= x && x < l->item->x + l->item->width) {
-      l->item->button(g, l->item, x, y, button, up);
+      l->item->button(g, l->item, x, y, key_modifiers, button, up);
       break;
     }
     l = l->next;
@@ -245,7 +245,7 @@ static void horizontal_button(gui *_g, widget *_this, int x, int y,
 }
 
 static void vertical_button(gui *_g, widget *_this, int x, int y,
-    int button, int up)
+    int key_modifiers, int button, int up)
 {
   LOGD("BUTTON container vertical %p xy %d %d button %d up %d\n", _this, x, y, button, up);
   struct gui *g = _g;
@@ -255,7 +255,7 @@ static void vertical_button(gui *_g, widget *_this, int x, int y,
   l = this->common.children;
   while (l) {
     if (l->item->y <= y && y < l->item->y + l->item->height) {
-      l->item->button(g, l->item, x, y, button, up);
+      l->item->button(g, l->item, x, y, key_modifiers, button, up);
       break;
     }
     l = l->next;
diff --git a/common/utils/T/tracer/gui/gui.h b/common/utils/T/tracer/gui/gui.h
index e470d2d366..5ae623f559 100644
--- a/common/utils/T/tracer/gui/gui.h
+++ b/common/utils/T/tracer/gui/gui.h
@@ -12,6 +12,11 @@ typedef void widget;
 #define BACKGROUND_COLOR 0
 #define FOREGROUND_COLOR 1
 
+/* key modifiers */
+#define KEY_SHIFT   (1<<0)
+#define KEY_CONTROL (1<<1)
+#define KEY_ALT     (1<<2)
+
 gui *gui_init(void);
 
 /* position = -1 to put at the end */
diff --git a/common/utils/T/tracer/gui/gui_defs.h b/common/utils/T/tracer/gui/gui_defs.h
index 8f15747490..5b8469ad9a 100644
--- a/common/utils/T/tracer/gui/gui_defs.h
+++ b/common/utils/T/tracer/gui/gui_defs.h
@@ -53,7 +53,8 @@ struct widget {
   void (*paint)(gui *g, widget *this);
   void (*clear)(gui *g, widget *this);
   /* user input */
-  void (*button)(gui *g, widget *this, int x, int y, int button, int up);
+  void (*button)(gui *g, widget *this, int x, int y, int key_modifiers,
+      int button, int up);
 };
 
 struct widget_list {
diff --git a/common/utils/T/tracer/gui/label.c b/common/utils/T/tracer/gui/label.c
index 80d885f1d2..bbe6e98eaf 100644
--- a/common/utils/T/tracer/gui/label.c
+++ b/common/utils/T/tracer/gui/label.c
@@ -45,7 +45,8 @@ widget *new_label(gui *_gui, const char *label)
   return w;
 }
 
-static void button(gui *gui, widget *_this, int x, int y, int button, int up)
+static void button(gui *gui, widget *_this, int x, int y,
+    int key_modifiers, int button, int up)
 {
   LOGD("BUTTON label %p xy %d %d button %d up %d\n", _this, x, y, button, up);
 
@@ -55,7 +56,8 @@ static void button(gui *gui, widget *_this, int x, int y, int button, int up)
 }
 
 /* we could use default_button, but it's in widget.c, so, well... */
-static void no_button(gui *gui, widget *_this, int x,int y,int button,int up)
+static void no_button(gui *gui, widget *_this, int x, int y,
+    int key_modifiers, int button, int up)
 {
   /* do nothing */
 }
diff --git a/common/utils/T/tracer/gui/positioner.c b/common/utils/T/tracer/gui/positioner.c
index 5bd23e147e..673ffb32de 100644
--- a/common/utils/T/tracer/gui/positioner.c
+++ b/common/utils/T/tracer/gui/positioner.c
@@ -50,14 +50,15 @@ static void hints(gui *_gui, widget *_w, int *width, int *height)
   else { *width = *height = 1; }
 }
 
-static void button(gui *_g, widget *_this, int x, int y, int button, int up)
+static void button(gui *_g, widget *_this, int x, int y,
+    int key_modifiers, int button, int up)
 {
   LOGD("BUTTON positioner %p xy %d %d button %d up %d\n", _this, x, y, button, up);
   struct gui *g = _g;
   struct positioner_widget *this = _this;
   struct widget_list *l = this->common.children;
   if (l != NULL)
-    l->item->button(g, l->item, x, y, button, up);
+    l->item->button(g, l->item, x, y, key_modifiers, button, up);
 }
 
 static void paint(gui *_gui, widget *_this)
diff --git a/common/utils/T/tracer/gui/textlist.c b/common/utils/T/tracer/gui/textlist.c
index 50a21cd459..6803d2301d 100644
--- a/common/utils/T/tracer/gui/textlist.c
+++ b/common/utils/T/tracer/gui/textlist.c
@@ -44,7 +44,8 @@ static void allocate(
   LOGD("ALLOCATE textlist %p xywh %d %d %d %d nlines %d\n", this, x, y, width, height, this->allocated_nlines);
 }
 
-static void button(gui *_g, widget *_this, int x, int y, int button, int up)
+static void button(gui *_g, widget *_this, int x, int y,
+    int key_modifiers, int button, int up)
 {
   struct gui *g = _g;
   struct textlist_widget *this = _this;
diff --git a/common/utils/T/tracer/gui/timeline.c b/common/utils/T/tracer/gui/timeline.c
index e627567aaf..43cbcc525f 100644
--- a/common/utils/T/tracer/gui/timeline.c
+++ b/common/utils/T/tracer/gui/timeline.c
@@ -54,7 +54,8 @@ static void allocate(gui *_gui, widget *_this,
   gui_notify(_gui, "resize", _this, &width);
 }
 
-static void button(gui *_g, widget *_this, int x, int y, int button, int up)
+static void button(gui *_g, widget *_this, int x, int y,
+    int key_modifiers, int button, int up)
 {
   struct gui *g = _g;
   struct timeline_widget *this = _this;
diff --git a/common/utils/T/tracer/gui/toplevel_window.c b/common/utils/T/tracer/gui/toplevel_window.c
index 03d5091bc7..d2436c84f9 100644
--- a/common/utils/T/tracer/gui/toplevel_window.c
+++ b/common/utils/T/tracer/gui/toplevel_window.c
@@ -55,13 +55,14 @@ static void paint(gui *_gui, widget *_this)
   g->xwin = NULL;    /* TODO: remove? it's just in case */
 }
 
-static void button(gui *_g, widget *_this, int x, int y, int button, int up)
+static void button(gui *_g, widget *_this, int x, int y,
+    int key_modifiers, int button, int up)
 {
   struct gui *g = _g;
   struct toplevel_window_widget *this = _this;
   g->xwin = this->x;
   this->common.children->item->button(_g, this->common.children->item,
-      x, y, button, up);
+      x, y, key_modifiers, button, up);
   g->xwin = NULL;    /* TODO: remove? it's just in case */
 }
 
diff --git a/common/utils/T/tracer/gui/widget.c b/common/utils/T/tracer/gui/widget.c
index 944d27548f..1d2b03a25f 100644
--- a/common/utils/T/tracer/gui/widget.c
+++ b/common/utils/T/tracer/gui/widget.c
@@ -14,8 +14,8 @@ static void default_add_child(
     gui *_gui, widget *_this, widget *child, int position);
 static void default_del_child(gui *_gui, widget *_this, widget *child);
 static void default_hints(gui *g, widget *this, int *width, int *height);
-static void default_button(gui *gui, widget *_this, int x, int y, int button,
-    int up);
+static void default_button(gui *gui, widget *_this, int x, int y,
+    int key_modifiers, int button, int up);
 
 static void toplevel_list_append(struct gui *g, struct widget *e)
 {
@@ -228,8 +228,8 @@ static void default_hints(gui *g, widget *this, int *width, int *height)
   *height = 1;
 }
 
-static void default_button(gui *gui, widget *_this, int x, int y, int button,
-    int up)
+static void default_button(gui *gui, widget *_this, int x, int y,
+    int key_modifiers, int button, int up)
 {
   /* nothing */
 }
diff --git a/common/utils/T/tracer/gui/x.c b/common/utils/T/tracer/gui/x.c
index bbc51e8a94..0a2d145039 100644
--- a/common/utils/T/tracer/gui/x.c
+++ b/common/utils/T/tracer/gui/x.c
@@ -171,13 +171,21 @@ void x_events(gui *_gui)
       break;
     case ButtonPress:
       if ((w = find_x_window(g, ev.xbutton.window)) != NULL) {
-        w->common.button(g, w, ev.xbutton.x, ev.xbutton.y,
+        int key_modifiers = 0;
+        if (ev.xbutton.state & ShiftMask)   key_modifiers |= KEY_SHIFT;
+        if (ev.xbutton.state & Mod1Mask)    key_modifiers |= KEY_ALT;
+        if (ev.xbutton.state & ControlMask) key_modifiers |= KEY_CONTROL;
+        w->common.button(g, w, ev.xbutton.x, ev.xbutton.y, key_modifiers,
             ev.xbutton.button, 0);
       }
       break;
     case ButtonRelease:
       if ((w = find_x_window(g, ev.xbutton.window)) != NULL) {
-        w->common.button(g, w, ev.xbutton.x, ev.xbutton.y,
+        int key_modifiers = 0;
+        if (ev.xbutton.state & ShiftMask)   key_modifiers |= KEY_SHIFT;
+        if (ev.xbutton.state & Mod1Mask)    key_modifiers |= KEY_ALT;
+        if (ev.xbutton.state & ControlMask) key_modifiers |= KEY_CONTROL;
+        w->common.button(g, w, ev.xbutton.x, ev.xbutton.y, key_modifiers,
             ev.xbutton.button, 1);
       }
       break;
-- 
GitLab