devdraw: enable gestures on OS X

Remove swipe gestures.
Keep only 2-finger pinching and 3-finger tapping.

R=rsc
CC=plan9port.codebot
http://codereview.appspot.com/5498094
This commit is contained in:
David Jeannot 2012-01-20 22:31:00 -05:00 committed by Russ Cox
parent 5d434a3ff3
commit f4792e43ae

View file

@ -102,8 +102,6 @@ struct
int mbuttons; int mbuttons;
NSPoint mpos; NSPoint mpos;
int mscroll; int mscroll;
int undo;
int touchevent;
int willactivate; int willactivate;
} in; } in;
@ -134,8 +132,6 @@ static NSCursor* makecursor(Cursor*);
} }
- (void)windowDidBecomeKey:(id)arg - (void)windowDidBecomeKey:(id)arg
{ {
in.touchevent = 0;
getmousepos(); getmousepos();
sendmouse(); sendmouse();
} }
@ -506,7 +502,6 @@ static void updatecursor(void);
- (void)keyDown:(NSEvent*)e{ getkeyboard(e);} - (void)keyDown:(NSEvent*)e{ getkeyboard(e);}
- (void)flagsChanged:(NSEvent*)e{ getkeyboard(e);} - (void)flagsChanged:(NSEvent*)e{ getkeyboard(e);}
- (void)swipeWithEvent:(NSEvent*)e{ getgesture(e);}
- (void)magnifyWithEvent:(NSEvent*)e{ getgesture(e);} - (void)magnifyWithEvent:(NSEvent*)e{ getgesture(e);}
- (void)touchesBeganWithEvent:(NSEvent*)e - (void)touchesBeganWithEvent:(NSEvent*)e
@ -779,57 +774,20 @@ getmouse(NSEvent *e)
sendmouse(); sendmouse();
} }
#define Minpinch 0.050 #define Minpinch 0.02
enum
{
Left = -1,
Right = +1,
Up = +2,
Down = -2,
};
static int
getdir(int dx, int dy)
{
return dx + 2*dy;
}
static void interpretswipe(int);
static void static void
getgesture(NSEvent *e) getgesture(NSEvent *e)
{ {
static float sum;
int dir;
if(usegestures == 0)
return;
switch([e type]){ switch([e type]){
case NSEventTypeMagnify: case NSEventTypeMagnify:
sum += [e magnification]; if(fabs([e magnification]) > Minpinch)
if(fabs(sum) > Minpinch){
togglefs(); togglefs();
sum = 0;
}
break;
case NSEventTypeSwipe:
dir = getdir(-[e deltaX], [e deltaY]);
if(in.touchevent)
if(dir==Up || dir==Down)
break;
interpretswipe(dir);
break; break;
} }
} }
static void sendclick(int); static void sendclick(int);
static void sendchord(int, int);
static void sendcmd(int);
static uint static uint
msec(void) msec(void)
@ -837,43 +795,16 @@ msec(void)
return nsec()/1000000; return nsec()/1000000;
} }
#define Inch 72
#define Cm Inch/2.54
#define Mindelta 0.0*Cm
#define Xminswipe 0.5*Cm
#define Yminswipe 0.1*Cm
enum
{
Finger = 1,
Msec = 1,
Maxtap = 400*Msec,
Maxtouch = 3*Finger,
};
static void static void
gettouch(NSEvent *e, int type) gettouch(NSEvent *e, int type)
{ {
static NSPoint delta; static int tapping;
static NSTouch *toucha[Maxtouch];
static NSTouch *touchb[Maxtouch];
static int done, ntouch, odir, tapping;
static uint taptime; static uint taptime;
NSArray *a;
NSPoint d;
NSSet *set; NSSet *set;
NSSize s; int p;
int dir, i, p;
if(usegestures == 0)
return;
switch(type){ switch(type){
case NSTouchPhaseBegan: case NSTouchPhaseBegan:
in.touchevent = 1;
p = NSTouchPhaseTouching; p = NSTouchPhaseTouching;
set = [e touchesMatchingPhase:p inView:nil]; set = [e touchesMatchingPhase:p inView:nil];
if(set.count == 3){ if(set.count == 3){
@ -882,80 +813,19 @@ gettouch(NSEvent *e, int type)
}else }else
if(set.count > 3) if(set.count > 3)
tapping = 0; tapping = 0;
return; break;
case NSTouchPhaseMoved: case NSTouchPhaseMoved:
p = NSTouchPhaseMoved; tapping = 0;
set = [e touchesMatchingPhase:p inView:nil]; break;
a = [set allObjects];
if(set.count > Maxtouch)
return;
if(ntouch==0){
ntouch = set.count;
for(i=0; i<ntouch; i++){
// assert(toucha[i] == nil);
toucha[i] = [[a objectAtIndex:i] retain];
}
return;
}
if(ntouch != set.count)
break;
if(done)
return;
d = NSMakePoint(0,0);
for(i=0; i<ntouch; i++){
// assert(touchb[i] == nil);
touchb[i] = [a objectAtIndex:i];
d.x += touchb[i].normalizedPosition.x;
d.y += touchb[i].normalizedPosition.y;
d.x -= toucha[i].normalizedPosition.x;
d.y -= toucha[i].normalizedPosition.y;
}
s = toucha[0].deviceSize;
d.x = d.x/ntouch * s.width;
d.y = d.y/ntouch * s.height;
if(fabs(d.x)>Mindelta || fabs(d.y)>Mindelta){
tapping = 0;
if(ntouch != 3){
done = 1;
goto Return;
}
delta = NSMakePoint(delta.x+d.x, delta.y+d.y);
d = NSMakePoint(fabs(delta.x), fabs(delta.y));
if(d.x>Xminswipe || d.y>Yminswipe){
if(d.x > d.y)
dir = delta.x>0? Right : Left;
else
dir = delta.y>0? Up : Down;
if(dir != odir){
// if(ntouch == 3)
if(dir==Up || dir==Down)
interpretswipe(dir);
odir = dir;
}
goto Return;
}
for(i=0; i<ntouch; i++){
[toucha[i] release];
toucha[i] = [touchb[i] retain];
}
}
Return:
for(i=0; i<ntouch; i++)
touchb[i] = nil;
return;
case NSTouchPhaseEnded: case NSTouchPhaseEnded:
p = NSTouchPhaseTouching; p = NSTouchPhaseTouching;
set = [e touchesMatchingPhase:p inView:nil]; set = [e touchesMatchingPhase:p inView:nil];
if(set.count == 0){ if(set.count == 0){
if(tapping && msec()-taptime<Maxtap) if(tapping && msec()-taptime<400)
sendclick(2); sendclick(2);
odir = 0;
tapping = 0; tapping = 0;
in.undo = 0;
in.touchevent = 0;
} }
break; break;
@ -965,40 +835,6 @@ Return:
default: default:
panic("gettouch: unexpected event type"); panic("gettouch: unexpected event type");
} }
for(i=0; i<ntouch; i++){
[toucha[i] release];
toucha[i] = nil;
}
delta = NSMakePoint(0,0);
done = 0;
ntouch = 0;
}
static void
interpretswipe(int dir)
{
if(dir == Left)
sendcmd('x');
else
if(dir == Right)
sendcmd('v');
else
if(dir == Up)
sendcmd('c');
else
if(dir == Down)
sendchord(2,1);
}
static void
sendcmd(int c)
{
if(in.touchevent && (c=='x' || c=='v')){
if(in.undo)
c = 'z';
in.undo = ! in.undo;
}
keystroke(Kcmd+c);
} }
static void static void
@ -1010,17 +846,6 @@ sendclick(int b)
sendmouse(); sendmouse();
} }
static void
sendchord(int b1, int b2)
{
in.mbuttons = b1;
sendmouse();
in.mbuttons |= b2;
sendmouse();
in.mbuttons = 0;
sendmouse();
}
static void static void
sendmouse(void) sendmouse(void)
{ {