unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Touch events
@ 2017-06-24  8:53 Alan Third
  2017-06-24  9:33 ` martin rudalics
                   ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Alan Third @ 2017-06-24  8:53 UTC (permalink / raw)
  To: Emacs-Devel devel

[-- Attachment #1: Type: text/plain, Size: 1126 bytes --]

I’ve created a small patch that adds some touch event handling to
Emacs. Currently it only works on macOS, but the same functionality is
available on GTK3, MS Windows and vanilla Xorg.

I wanted to know if I’m going about this the right way, or if it’s
even worth my while continuing.

I’ve created a number of new events: touch-scroll, touch-pinch, etc.,
that can be bound to functions. I’ve attached a simple script that
binds touch-pinch to the text-scale-increase/decrease functions.

BTW, how do you create a lisp float from C? I tried something like

    Lisp_Object delta;
    XSETFLOAT (delta, [event magnification]);

but it just complains that [event magnification] returns a double and
not void *.

NOTE: If you install this patch, scrolling with the touchpad will no
longer work on macOS (a real mousewheel will be fine, though). It
needs some effort to get it working, and my elisp skills aren’t up to
much. It either needs a compatibility function in lisp, or a way to
disable it and use the existing mousewheel code. It could be worth
trying to tie it into the new pixel scroll mode.
-- 
Alan Third

[-- Attachment #2: Test functions --]
[-- Type: text/plain, Size: 531 bytes --]

(defun touch--scroll (event)
  (interactive (list last-input-event))
  (message "%s" event)
  (set-window-vscroll (caadr event) (- (window-vscroll (caadr event) t)
				       (cadr (caddr event))) t))

(defun touch--pinch (event)
  (interactive (list last-input-event))
  (message "%s" event)
  (let ((delta (caddr event)))
    (cond ((> delta 0)
  	   (text-scale-increase delta))
	  ((< delta 0)
	   (text-scale-decrease (- delta))))))

(global-set-key [touch-pinch] 'touch--pinch)
(global-set-key [touch-scroll] 'touch--scroll)

[-- Attachment #3: 0001-Add-touch-events.patch --]
[-- Type: text/plain, Size: 6937 bytes --]

From 0cc26156f8f13622073488ecee647bf6a656fee8 Mon Sep 17 00:00:00 2001
From: Alan Third <alan@idiocy.org>
Date: Sat, 24 Jun 2017 09:36:03 +0100
Subject: [PATCH] Add touch events

---
 src/keyboard.c  | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/nsterm.m    | 66 +++++++++++++++++++++++++++++++++++++++++++++------------
 src/termhooks.h |  4 ++++
 3 files changed, 116 insertions(+), 13 deletions(-)

diff --git a/src/keyboard.c b/src/keyboard.c
index 55486c6d9a..8e1face509 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -4556,6 +4556,7 @@ static Lisp_Object accent_key_syms;
 static Lisp_Object func_key_syms;
 static Lisp_Object mouse_syms;
 static Lisp_Object wheel_syms;
+static Lisp_Object touch_syms;
 static Lisp_Object drag_n_drop_syms;
 
 /* This is a list of keysym codes for special "accent" characters.
@@ -5106,6 +5107,12 @@ static const char *const lispy_wheel_names[] =
   "wheel-up", "wheel-down", "wheel-left", "wheel-right"
 };
 
+static const char *const touch_names[] =
+{
+  "touch-scroll", "touch-pinch", "touch-rotate", "touch-swipe-up",
+  "touch-swipe-down", "touch-swipe-left", "touch-swipe-right"
+};
+
 /* drag-n-drop events are generated when a set of selected files are
    dragged from another application and dropped onto an Emacs window.  */
 static const char *const lispy_drag_n_drop_names[] =
@@ -5999,6 +6006,48 @@ make_lispy_event (struct input_event *event)
 	return list3 (head, position, files);
       }
 
+    case TOUCH_SCROLL_EVENT:
+      {
+	Lisp_Object position;
+	Lisp_Object head;
+        Lisp_Object deltas = event->arg;
+	struct frame *f = XFRAME (event->frame_or_window);
+
+	/* Ignore touch events that were made on frame that have
+	   been deleted.  */
+	if (! FRAME_LIVE_P (f))
+	  return Qnil;
+
+	position = make_lispy_position (f, event->x, event->y,
+					event->timestamp);
+
+        head = modify_event_symbol (0, event->modifiers, Qtouch_scroll, Qnil,
+                                    touch_names, &touch_syms, ASIZE (touch_syms));
+
+        return list3 (head, position, deltas);
+      }
+
+    case TOUCH_PINCH_EVENT:
+      {
+	Lisp_Object position;
+	Lisp_Object head;
+        Lisp_Object delta = event->arg;
+	struct frame *f = XFRAME (event->frame_or_window);
+
+	/* Ignore touch events that were made on frame that have
+	   been deleted.  */
+	if (! FRAME_LIVE_P (f))
+	  return Qnil;
+
+	position = make_lispy_position (f, event->x, event->y,
+					event->timestamp);
+
+        head = modify_event_symbol (1, event->modifiers, Qtouch_pinch, Qnil,
+                                    touch_names, &touch_syms, ASIZE (touch_syms));
+
+        return list3 (head, position, delta);
+      }
+
 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
     || defined (HAVE_NS) || defined (USE_GTK)
     case MENU_BAR_EVENT:
@@ -11054,6 +11103,14 @@ syms_of_keyboard (void)
   /* The values of Qevent_kind properties.  */
   DEFSYM (Qmouse_click, "mouse-click");
 
+  DEFSYM (Qtouch_scroll, "touch-scroll");
+  DEFSYM (Qtouch_pinch, "touch-pinch");
+  DEFSYM (Qtouch_rotate, "touch-rotate");
+  DEFSYM (Qtouch_swipe_up, "touch-swipe-up");
+  DEFSYM (Qtouch_swipe_down, "touch-swipe-down");
+  DEFSYM (Qtouch_swipe_left, "touch-swipe-left");
+  DEFSYM (Qtouch_swipe_right, "touch-swipe-right");
+
   DEFSYM (Qdrag_n_drop, "drag-n-drop");
   DEFSYM (Qsave_session, "save-session");
   DEFSYM (Qconfig_changed_event, "config-changed-event");
@@ -11190,6 +11247,8 @@ syms_of_keyboard (void)
   wheel_syms = Fmake_vector (make_number (ARRAYELTS (lispy_wheel_names)),
 			     Qnil);
   staticpro (&wheel_syms);
+  touch_syms = Fmake_vector (make_number (6), Qnil);
+  staticpro (&touch_syms);
 
   {
     int i;
diff --git a/src/nsterm.m b/src/nsterm.m
index bf83550b3d..00dc286feb 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -6392,24 +6392,36 @@ - (void)mouseDown: (NSEvent *)theEvent
 
   if ([theEvent type] == NSEventTypeScrollWheel)
     {
-      CGFloat delta = [theEvent deltaY];
-      /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
-      if (delta == 0)
+      if ([theEvent hasPreciseScrollingDeltas])
         {
-          delta = [theEvent deltaX];
+          Lisp_Object dx, dy;
+          XSETINT (dx, [theEvent scrollingDeltaX]);
+          XSETINT (dy, [theEvent scrollingDeltaY]);
+
+          emacs_event->kind = TOUCH_SCROLL_EVENT;
+          emacs_event->arg = list2 (dx, dy);
+        }
+      else
+        {
+          CGFloat delta = [theEvent deltaY];
+          /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
           if (delta == 0)
             {
-              NSTRACE_MSG ("deltaIsZero");
-              return;
+              delta = [theEvent deltaX];
+              if (delta == 0)
+                {
+                  NSTRACE_MSG ("deltaIsZero");
+                  return;
+                }
+              emacs_event->kind = HORIZ_WHEEL_EVENT;
             }
-          emacs_event->kind = HORIZ_WHEEL_EVENT;
-        }
-      else
-        emacs_event->kind = WHEEL_EVENT;
+          else
+            emacs_event->kind = WHEEL_EVENT;
 
-      emacs_event->code = 0;
-      emacs_event->modifiers = EV_MODIFIERS (theEvent) |
-        ((delta > 0) ? up_modifier : down_modifier);
+          emacs_event->code = 0;
+          emacs_event->modifiers = EV_MODIFIERS (theEvent) |
+            ((delta > 0) ? up_modifier : down_modifier);
+        }
     }
   else
     {
@@ -6555,6 +6567,34 @@ - (void)otherMouseDragged: (NSEvent *)e
 }
 
 
+- (void)magnifyWithEvent: (NSEvent *) e
+{
+  struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (emacsframe);
+  NSPoint p = [self convertPoint: [e locationInWindow] fromView: nil];
+  Lisp_Object delta;
+
+  NSTRACE ("[EmacsView magnifyWithEvent:]");
+
+  [self deleteWorkingText];
+
+  if (!emacs_event)
+    return;
+
+  dpyinfo->last_mouse_frame = emacsframe;
+
+  XSETINT (delta, (int)([e magnification] * 100));
+
+  emacs_event->kind = TOUCH_PINCH_EVENT;
+  emacs_event->arg = delta;
+  emacs_event->modifiers = EV_MODIFIERS (e)
+    | EV_UDMODIFIERS (e);
+
+  XSETINT (emacs_event->x, lrint (p.x));
+  XSETINT (emacs_event->y, lrint (p.y));
+  EV_TRAILER (e);
+}
+
+
 - (BOOL)windowShouldClose: (id)sender
 {
   NSEvent *e =[[self window] currentEvent];
diff --git a/src/termhooks.h b/src/termhooks.h
index 14ec397346..07894c8154 100644
--- a/src/termhooks.h
+++ b/src/termhooks.h
@@ -120,6 +120,10 @@ enum event_kind
   HORIZ_WHEEL_EVENT,            /* A wheel event generated by a second
                                    horizontal wheel that is present on some
                                    mice. See WHEEL_EVENT.  */
+  TOUCH_SCROLL_EVENT,           /* A touchpad scroll event.  */
+  TOUCH_PINCH_EVENT,
+  TOUCH_SWIPE_EVENT,
+  TOUCH_ROTATE_EVENT,
 #ifdef HAVE_NTGUI
   LANGUAGE_CHANGE_EVENT,	/* A LANGUAGE_CHANGE_EVENT is
 				   generated when HAVE_NTGUI or on Mac OS
-- 
2.12.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2017-09-03  9:14 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-24  8:53 Touch events Alan Third
2017-06-24  9:33 ` martin rudalics
2017-06-24  9:58   ` Alan Third
2017-06-24 13:18 ` Clément Pit-Claudel
2017-06-24 17:00   ` Lars Ingebrigtsen
2017-06-24 23:22     ` Alan Third
2017-06-24 23:43       ` Clément Pit-Claudel
2017-06-25  0:04         ` Lars Ingebrigtsen
2017-06-25 10:40           ` Alan Third
2017-06-25 16:56             ` Clément Pit-Claudel
2017-06-25 17:04               ` Noam Postavsky
2017-06-25 17:10               ` martin rudalics
2017-06-25 18:09                 ` Clément Pit-Claudel
2017-06-25 19:22         ` Stefan Monnier
2017-06-26 10:08 ` Anders Lindgren
2017-06-26 18:28   ` Alan Third
2017-06-28  8:25     ` John Wiegley
2017-06-28 15:14       ` raman
2017-06-28 20:50         ` Alan Third
2017-09-03  9:14           ` Alan Third
2017-06-27 20:08   ` Alan Third
2017-06-28  9:40     ` Anders Lindgren
2017-06-28 17:23       ` James Nguyen
2017-06-28 21:06       ` Alan Third

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).