devdraw: rewrite the Cocoa screen using Metal

Add a new macOS cocoa screen, cocoa-screen-metal.m.
Rewrite the macOS cocoa drawing code to use the builtin runloop,
and use Metal to push pixels with CAMetalLayer.

Remove all of the deprecated code, and simplify some of the logic.
Modify mkwsysrules.sh such that the new code is used only when
the system version is equal or higher than 10.14.

Allow touch events to simulate mouse clicks:
three finger tap for the middle mouse button;
four finger tap for the 2-1 chord.

Support Tresize.

Scale 16x16 Cursor up to 32x32 with an EPX algorithm.

Support macOS input sources including the basic dead keys and the
advanced CJK input methods.

Increase the communication buffers in cocoa-srv.c to allow more
input, especially for long sentences prepared by the macOS input
souces.
This commit is contained in:
Xiao-Yong Jin 2018-10-21 20:59:21 -05:00 committed by Russ Cox
parent de43b1629d
commit 9af9ceca26
8 changed files with 1307 additions and 21 deletions

View file

@ -1,11 +1,13 @@
Cursor bigarrow = { Cursor bigarrow = {
{0, 0}, { -1, -1 },
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFC, { 0xFF, 0xFF, 0x80, 0x01, 0x80, 0x02, 0x80, 0x0C,
0xFF, 0xF0, 0xFF, 0xF0, 0xFF, 0xF8, 0xFF, 0xFC, 0x80, 0x10, 0x80, 0x10, 0x80, 0x08, 0x80, 0x04,
0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFC, 0x80, 0x02, 0x80, 0x01, 0x80, 0x02, 0x8C, 0x04,
0xF3, 0xF8, 0xF1, 0xF0, 0xE0, 0xE0, 0xC0, 0x40}, 0x92, 0x08, 0x91, 0x10, 0xA0, 0xA0, 0xC0, 0x40,
},
{ 0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFC, 0x7F, 0xF0, { 0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFC, 0x7F, 0xF0,
0x7F, 0xE0, 0x7F, 0xE0, 0x7F, 0xF0, 0x7F, 0xF8, 0x7F, 0xE0, 0x7F, 0xE0, 0x7F, 0xF0, 0x7F, 0xF8,
0x7F, 0xFC, 0x7F, 0xFE, 0x7F, 0xFC, 0x73, 0xF8, 0x7F, 0xFC, 0x7F, 0xFE, 0x7F, 0xFC, 0x73, 0xF8,
0x61, 0xF0, 0x60, 0xE0, 0x40, 0x40, 0x00, 0x00}, 0x61, 0xF0, 0x60, 0xE0, 0x40, 0x40, 0x00, 0x00,
},
}; };

File diff suppressed because it is too large Load diff

View file

@ -16,5 +16,9 @@ void servep9p(void);
void zlock(void); void zlock(void);
void zunlock(void); void zunlock(void);
void resizeimg(void);
Rectangle mouserect; Rectangle mouserect;
int mouseresized; int mouseresized;
void resizewindow(Rectangle);

View file

@ -21,7 +21,7 @@ typedef struct Tagbuf Tagbuf;
struct Kbdbuf struct Kbdbuf
{ {
Rune r[32]; Rune r[256];
int ri; int ri;
int wi; int wi;
int stall; int stall;
@ -29,7 +29,7 @@ struct Kbdbuf
struct Mousebuf struct Mousebuf
{ {
Mouse m[32]; Mouse m[256];
Mouse last; Mouse last;
int ri; int ri;
int wi; int wi;
@ -38,7 +38,7 @@ struct Mousebuf
struct Tagbuf struct Tagbuf
{ {
int t[32]; int t[256];
int ri; int ri;
int wi; int wi;
}; };
@ -97,7 +97,7 @@ servep9p(void)
/* pick off messages one by one */ /* pick off messages one by one */
if(convM2W(mbuf, nn+4, &m) <= 0) if(convM2W(mbuf, nn+4, &m) <= 0)
sysfatal("cannot convert message"); sysfatal("cannot convert message");
if(trace) fprint(2, "<- %W\n", &m); if(trace) fprint(2, "%ud [%d] <- %W\n", nsec()/1000000, threadid(), &m);
runmsg(&m); runmsg(&m);
} }
} }
@ -191,6 +191,7 @@ runmsg(Wsysmsg *m)
break; break;
case Trddraw: case Trddraw:
zlock();
n = m->count; n = m->count;
if(n > sizeof buf) if(n > sizeof buf)
n = sizeof buf; n = sizeof buf;
@ -202,13 +203,16 @@ runmsg(Wsysmsg *m)
m->data = buf; m->data = buf;
replymsg(m); replymsg(m);
} }
zunlock();
break; break;
case Twrdraw: case Twrdraw:
zlock();
if(_drawmsgwrite(m->data, m->count) < 0) if(_drawmsgwrite(m->data, m->count) < 0)
replyerror(m); replyerror(m);
else else
replymsg(m); replymsg(m);
zunlock();
break; break;
case Ttop: case Ttop:
@ -217,7 +221,9 @@ runmsg(Wsysmsg *m)
break; break;
case Tresize: case Tresize:
// _xresizewindow(m->rect); #if OSX_VERSION >= 101400
resizewindow(m->rect);
#endif
replymsg(m); replymsg(m);
break; break;
} }
@ -238,7 +244,7 @@ replymsg(Wsysmsg *m)
if(m->type%2 == 0) if(m->type%2 == 0)
m->type++; m->type++;
if(trace) fprint(2, "-> %W\n", m); if(trace) fprint(2, "%ud [%d] -> %W\n", nsec()/1000000, threadid(), m);
/* copy to output buffer */ /* copy to output buffer */
n = sizeW2M(m); n = sizeW2M(m);
@ -293,11 +299,11 @@ matchmouse(void)
mousetags.ri = 0; mousetags.ri = 0;
m.mouse = mouse.m[mouse.ri]; m.mouse = mouse.m[mouse.ri];
m.resized = mouseresized; m.resized = mouseresized;
mouseresized = 0;
/* /*
if(m.resized) if(m.resized)
fprint(2, "sending resize\n"); fprint(2, "sending resize\n");
*/ */
mouseresized = 0;
mouse.ri++; mouse.ri++;
if(mouse.ri == nelem(mouse.m)) if(mouse.ri == nelem(mouse.m))
mouse.ri = 0; mouse.ri = 0;
@ -367,8 +373,6 @@ abortcompose(void)
keystroke(Kalt); keystroke(Kalt);
} }
void resizeimg(void);
void void
keystroke(int c) keystroke(int c)
{ {

View file

@ -25,4 +25,11 @@ qunlock(QLock *q)
{ {
pthread_mutex_unlock(&q->m); pthread_mutex_unlock(&q->m);
} }
int
threadid(void)
{
return pthread_mach_thread_np(pthread_self());
}
#endif #endif

View file

@ -30,4 +30,5 @@
void qlock(QLock*); void qlock(QLock*);
void qunlock(QLock*); void qunlock(QLock*);
int threadid(void);
#endif #endif

View file

@ -35,6 +35,9 @@ latin1.h: $PLAN9/lib/keyboard $O.mklatinkbd
$O.macargv: $MACARGV $O.macargv: $MACARGV
$LD -o $target $prereq $LD -o $target $prereq
cocoa-screen-metal-objc.$O: cocoa-screen-metal.m
$CC $CFLAGS $OBJCFLAGS -o $target cocoa-screen-metal.m
%-objc.$O: %.m %-objc.$O: %.m
$CC $CFLAGS -o $target $stem.m $CC $CFLAGS -o $target $stem.m

View file

@ -60,7 +60,12 @@ elif [ $WSYSTYPE = osx ]; then
echo 'WSYSOFILES=$WSYSOFILES osx-screen-carbon-objc.o osx-draw.o osx-srv.o' echo 'WSYSOFILES=$WSYSOFILES osx-screen-carbon-objc.o osx-draw.o osx-srv.o'
echo 'MACARGV=macargv.o' echo 'MACARGV=macargv.o'
elif [ $WSYSTYPE = osx-cocoa ]; then elif [ $WSYSTYPE = osx-cocoa ]; then
if sw_vers|awk '/ProductVersion/{split($2,a,".");exit(a[2]<14)}' >/dev/null; then # 0 is true in sh.
echo 'OBJCFLAGS=$OBJCFLAGS -fobjc-arc'
echo 'WSYSOFILES=$WSYSOFILES osx-draw.o cocoa-screen-metal-objc.o cocoa-srv.o cocoa-thread.o'
else
echo 'WSYSOFILES=$WSYSOFILES osx-draw.o cocoa-screen-objc.o cocoa-srv.o cocoa-thread.o' echo 'WSYSOFILES=$WSYSOFILES osx-draw.o cocoa-screen-objc.o cocoa-srv.o cocoa-thread.o'
fi
echo 'MACARGV=macargv-objc.o' echo 'MACARGV=macargv-objc.o'
elif [ $WSYSTYPE = nowsys ]; then elif [ $WSYSTYPE = nowsys ]; then
echo 'WSYSOFILES=nowsys.o' echo 'WSYSOFILES=nowsys.o'