From 88537cfe9b483846ccd7367c81fde5dcfe16a4d5 Mon Sep 17 00:00:00 2001 From: qwx Date: Sun, 13 May 2018 07:01:47 +0200 Subject: demote libemu to common code - too small, nothing else that won't be under /games will use it - "libemu" as a name doesn't make sense here --- sys/man/2/emu | 137 -------------------- sys/src/games/2600/2600.c | 2 +- sys/src/games/2600/cpu.c | 2 +- sys/src/games/2600/mem.c | 2 +- sys/src/games/2600/mkfile | 3 + sys/src/games/2600/tia.c | 2 +- sys/src/games/c64/c64.c | 2 +- sys/src/games/c64/cpu.c | 2 +- sys/src/games/c64/mem.c | 2 +- sys/src/games/c64/mkfile | 5 +- sys/src/games/c64/vic.c | 2 +- sys/src/games/eui.c | 316 ++++++++++++++++++++++++++++++++++++++++++++++ sys/src/games/eui.h | 19 +++ sys/src/games/gb/apu.c | 2 +- sys/src/games/gb/cpu.c | 2 +- sys/src/games/gb/gb.c | 2 +- sys/src/games/gb/mem.c | 2 +- sys/src/games/gb/mkfile | 3 + sys/src/games/gb/ppu.c | 2 +- sys/src/games/gba/apu.c | 2 +- sys/src/games/gba/cpu.c | 2 +- sys/src/games/gba/gba.c | 2 +- sys/src/games/gba/mem.c | 2 +- sys/src/games/gba/mkfile | 3 + sys/src/games/gba/ppu.c | 2 +- sys/src/games/md/cpu.c | 2 +- sys/src/games/md/md.c | 2 +- sys/src/games/md/mem.c | 2 +- sys/src/games/md/mkfile | 3 + sys/src/games/md/vdp.c | 2 +- sys/src/games/md/ym.c | 2 +- sys/src/games/nes/apu.c | 2 +- sys/src/games/nes/mem.c | 2 +- sys/src/games/nes/mkfile | 3 + sys/src/games/nes/nes.c | 2 +- sys/src/games/nes/ppu.c | 2 +- sys/src/games/snes/cpu.c | 2 +- sys/src/games/snes/dsp.c | 2 +- sys/src/games/snes/mem.c | 2 +- sys/src/games/snes/mkfile | 3 + sys/src/games/snes/ppu.c | 2 +- sys/src/games/snes/snes.c | 2 +- sys/src/games/snes/spc.c | 2 +- sys/src/libemu/emu.c | 316 ---------------------------------------------- sys/src/libemu/mkfile | 20 --- 45 files changed, 390 insertions(+), 507 deletions(-) delete mode 100644 sys/man/2/emu create mode 100644 sys/src/games/eui.c create mode 100644 sys/src/games/eui.h delete mode 100644 sys/src/libemu/emu.c delete mode 100644 sys/src/libemu/mkfile diff --git a/sys/man/2/emu b/sys/man/2/emu deleted file mode 100644 index 68026416b..000000000 --- a/sys/man/2/emu +++ /dev/null @@ -1,137 +0,0 @@ -.TH EMU 2 -.SH NAME -initemu, regkeyfn, flushmouse, flushscreen, flushaudio, screenwipe \- graphical emulator-like software scaffolding -.SH SYNOPSIS -.nf -.ft L -#include -#include -#include -.PP -.ta +\w'\fLvoid fP'u -.B -void flushmouse(int discard); -.PP -.B -void flushscreen(void); -.PP -.B -void flushaudio(int (*fn)(void)); -.PP -.B -void regkeyfn(Rune r, void (*fn)(void)); -.PP -.B -void regkey(char *joyk, Rune r, int k); -.PP -.B -void initemu(int dx, int dy, int bpp, ulong chan, -.B - int dokey, void(*kproc)(void*)); -.SH DESCRIPTION -.I Libemu -implements common user interfaces for programs controlled -with a joypad or a limited number of keys. -.PP -.B initemu -initializes the display for the given internal screen size -.B dx -by -.B dy -and -.B bpp -bit depth. -.B Chan -is an -.B Image -pixel format descriptor to be used for an internal framebuffer (see -.IR draw (2)). -.PP -If -.B dokey -is true, -a keyboard process is started which sets a 64-bit wide bit vector for input keys. -.PP -Keys are set via -.B regkey. -Pressing the key corresponding to the -.B r -rune, or writing -.B joyk -to standard in will -.L OR -.B k -with the key bit vector. -.PP -.B Regkeyfn -registers an additional rune and a callback for the keyboard process. -.PP -Normally, a joypad process is also started, and parses standard input for key presses. -If -.B dokey -is false, only the joypad process will be started. -If -.B kproc -is a valid function pointer, -it will be used for keyboard processing instead of the library-provided one, -and no joypad process will be started. -.PP -.IP -.EX -.ta 6n -uchar *pic; -.EE -.PP -Once -.B initemu -is called, a framebuffer of the specifized size is allocated, -and may be accessed via -.BR pic . -.L Libemu -scales the framebuffer to fit the greatest multiple of the framebuffer's -width in the window. -The scaling is horizontal only and needs to be taken into account for drawing -within the program. -.PP -Typically, mouse event handling is followed by drawing the final image from the -internal framebuffer render and writing a constant amount of audio samples, -thereby synchronizing the program's framerate to the audio writes. -.IP -.EX -.ta 6n -Mouse m; -extern Mousectl *mc; - -flushmouse(0); -while(nbrecv(mc->c, &m) > 0){ - ... -} -flushscreen(); -flushaudio(audioout); -.EE -.PP -Besides window resizing, mouse events are discarded by default. -If -.B discard -is false -.B flushmouse -will let the user program handle mouse events prior to flushing the screen (see -.BR event (2)). -.PP -.B Flushscreen -handles re-scaling and re-allocating the buffers used, as well as drawing to -the screen, either directly, or by duplicating pre-scaled scanlines. -.SH SOURCE -.B /sys/src/libemu -.SH "SEE ALSO" -.IR draw (2), -.IR event (2) -.SH BUGS -The semantics for -.B initemu -input selection are confusing. -.PP -A greater effort should be made to simplify automatic scaling for user programs. -.SH HISTORY -.I Libemu -first appeared in 9front in May, 2018. diff --git a/sys/src/games/2600/2600.c b/sys/src/games/2600/2600.c index 38d7c856f..728401d89 100644 --- a/sys/src/games/2600/2600.c +++ b/sys/src/games/2600/2600.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/2600/cpu.c b/sys/src/games/2600/cpu.c index 4dedfb9eb..66842f292 100644 --- a/sys/src/games/2600/cpu.c +++ b/sys/src/games/2600/cpu.c @@ -1,6 +1,6 @@ #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/2600/mem.c b/sys/src/games/2600/mem.c index be24a17e1..c521915b2 100644 --- a/sys/src/games/2600/mem.c +++ b/sys/src/games/2600/mem.c @@ -1,6 +1,6 @@ #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/2600/mkfile b/sys/src/games/2600/mkfile index df08bd2d8..1b1657e07 100644 --- a/sys/src/games/2600/mkfile +++ b/sys/src/games/2600/mkfile @@ -8,7 +8,10 @@ OFILES=\ cpu.$O\ mem.$O\ tia.$O\ + eui.$O\ HFILES=dat.h fns.h #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/c64/c64.c b/sys/src/games/c64/c64.c index 9b4e0504b..2ca976fe7 100644 --- a/sys/src/games/c64/c64.c +++ b/sys/src/games/c64/c64.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/c64/cpu.c b/sys/src/games/c64/cpu.c index cafd8e333..3a24e7783 100644 --- a/sys/src/games/c64/cpu.c +++ b/sys/src/games/c64/cpu.c @@ -1,6 +1,6 @@ #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/c64/mem.c b/sys/src/games/c64/mem.c index 8e00bec4b..6d0682b7a 100644 --- a/sys/src/games/c64/mem.c +++ b/sys/src/games/c64/mem.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/c64/mkfile b/sys/src/games/c64/mkfile index af2ff2913..27d7f2641 100644 --- a/sys/src/games/c64/mkfile +++ b/sys/src/games/c64/mkfile @@ -7,7 +7,10 @@ OFILES=\ cpu.$O\ mem.$O\ vic.$O\ - + eui.$O\ + HFILES=dat.h fns.h #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/eui.c b/sys/src/games/eui.c new file mode 100644 index 000000000..d462c76d8 --- /dev/null +++ b/sys/src/games/eui.c @@ -0,0 +1,316 @@ +#include +#include +#include +#include +#include +#include +#include "eui.h" + +typedef struct Kfn Kfn; + +u64int keys, keys2; +int trace, paused; +int savereq, loadreq; +QLock pauselock; +int scale, warp10; +uchar *pic; +Rectangle picr; +Mousectl *mc; +Image *bg; + +static int profile, framestep; +static int vwdx, vwdy, vwbpp; +static ulong vwchan; +static Image *fb; + +struct Kfn{ + Rune r; + int k; + char joyk[16]; + void(*fn)(void); + Kfn *n; +}; +Kfn kfn, kkn; + +void * +emalloc(ulong sz) +{ + void *v; + + v = mallocz(sz, 1); + if(v == nil) + sysfatal("malloc: %r"); + setmalloctag(v, getcallerpc(&sz)); + return v; +} + +static void +joyproc(void *) +{ + char *s, *down[9]; + static char buf[64]; + int n, k, j; + Kfn *kp; + + j = 1; + + for(;;){ + n = read(0, buf, sizeof(buf) - 1); + if(n <= 0) + sysfatal("read: %r"); + buf[n] = 0; + n = getfields(buf, down, nelem(down), 1, " "); + k = 0; + for(n--; n >= 0; n--){ + s = down[n]; + if(strcmp(s, "joy1") == 0) + j = 1; + else if(strcmp(s, "joy2") == 0) + j = 2; + for(kp=kkn.n; kp!=nil; kp=kp->n){ + if(strcmp(kp->joyk, s) == 0) + k |= kp->k; + } + } + if(j == 2) + keys2 = k; + else + keys = k; + } +} + +static void +keyproc(void *) +{ + int fd, n, k; + static char buf[256]; + char *s; + Rune r; + Kfn *kp; + + fd = open("/dev/kbd", OREAD); + if(fd < 0) + sysfatal("open: %r"); + for(;;){ + if(buf[0] != 0){ + n = strlen(buf)+1; + memmove(buf, buf+n, sizeof(buf)-n); + } + if(buf[0] == 0){ + n = read(fd, buf, sizeof(buf)-1); + if(n <= 0) + sysfatal("read /dev/kbd: %r"); + buf[n-1] = 0; + buf[n] = 0; + } + if(buf[0] == 'c'){ + if(utfrune(buf, Kdel)){ + close(fd); + threadexitsall(nil); + } + if(utfrune(buf, KF|5)) + savereq = 1; + if(utfrune(buf, KF|6)) + loadreq = 1; + if(utfrune(buf, KF|12)) + profile ^= 1; + if(utfrune(buf, 't')) + trace = !trace; + for(kp=kfn.n; kp!=nil; kp=kp->n){ + if(utfrune(buf, kp->r)) + kp->fn(); + } + } + if(buf[0] != 'k' && buf[0] != 'K') + continue; + s = buf + 1; + k = 0; + while(*s != 0){ + s += chartorune(&r, s); + switch(r){ + case Kdel: close(fd); threadexitsall(nil); + case Kesc: + if(paused) + qunlock(&pauselock); + else + qlock(&pauselock); + paused = !paused; + break; + case KF|1: + if(paused){ + qunlock(&pauselock); + paused=0; + } + framestep = !framestep; + break; + case '`': + warp10 = !warp10; + break; + } + for(kp=kkn.n; kp!=nil; kp=kp->n){ + if(utfrune(buf, kp->r)) + k |= kp->k; + } + } + k &= ~(k << 1 & 0xa0 | k >> 1 & 0x50); + keys = k; + } +} + +static void +timing(void) +{ + static int fcount; + static vlong old; + static char buf[32]; + vlong new; + + if(++fcount == 60) + fcount = 0; + else + return; + new = nsec(); + if(new != old) + sprint(buf, "%6.2f%%", 1e11 / (new - old)); + else + buf[0] = 0; + draw(screen, rectaddpt(Rect(10, 10, vwdx-40, 30), screen->r.min), bg, nil, ZP); + string(screen, addpt(screen->r.min, Pt(10, 10)), display->black, ZP, display->defaultfont, buf); + old = nsec(); +} + +static void +screeninit(void) +{ + Point p; + + scale = Dx(screen->r) / vwdx; + if(scale <= 0) + scale = 1; + else if(scale > 16) + scale = 16; + p = divpt(addpt(screen->r.min, screen->r.max), 2); + picr = Rpt(subpt(p, Pt(scale * vwdx/2, scale * vwdy/2)), + addpt(p, Pt(scale * vwdx/2, scale * vwdy/2))); + freeimage(fb); + fb = allocimage(display, Rect(0, 0, scale * vwdx, scale > 1 ? 1 : scale * vwdy), + vwchan, scale > 1, 0); + free(pic); + pic = emalloc(vwdx * vwdy * vwbpp * scale); + draw(screen, screen->r, bg, nil, ZP); +} + +void +flushmouse(int discard) +{ + Mouse m; + + if(nbrecvul(mc->resizec) > 0){ + if(getwindow(display, Refnone) < 0) + sysfatal("resize failed: %r"); + screeninit(); + } + if(discard) + while(nbrecv(mc->c, &m) > 0) + ; +} + +void +flushscreen(void) +{ + flushmouse(1); + if(scale == 1){ + loadimage(fb, fb->r, pic, vwdx * vwdy * vwbpp); + draw(screen, picr, fb, nil, ZP); + } else { + Rectangle r; + uchar *s; + int w; + + s = pic; + r = picr; + w = vwdx * vwbpp * scale; + while(r.min.y < picr.max.y){ + loadimage(fb, fb->r, s, w); + s += w; + r.max.y = r.min.y+scale; + draw(screen, r, fb, nil, ZP); + r.min.y = r.max.y; + } + } + flushimage(display, 1); + if(profile) + timing(); +} + +void +flushaudio(int (*audioout)(void)) +{ + static vlong old, delta; + vlong new, diff; + + if(audioout == nil || audioout() < 0 && !warp10){ + new = nsec(); + diff = 0; + if(old != 0){ + diff = BILLION/60 - (new - old) - delta; + if(diff >= MILLION) + sleep(diff/MILLION); + } + old = nsec(); + if(diff > 0){ + diff = (old - new) - (diff / MILLION) * MILLION; + delta += (diff - delta) / 100; + } + } + if(framestep){ + paused = 1; + qlock(&pauselock); + framestep = 0; + } +} + +void +regkeyfn(Rune r, void (*fn)(void)) +{ + Kfn *kp; + + for(kp=&kfn; kp->n!=nil; kp=kp->n) + ; + kp->n = emalloc(sizeof *kp); + kp->n->r = r; + kp->n->fn = fn; +} + +void +regkey(char *joyk, Rune r, int k) +{ + Kfn *kp; + + for(kp=&kkn; kp->n!=nil; kp=kp->n) + ; + kp->n = emalloc(sizeof *kp); + strncpy(kp->n->joyk, joyk, sizeof(kp->n->joyk)-1); + kp->n->r = r; + kp->n->k = k; +} + +void +initemu(int dx, int dy, int bpp, ulong chan, int dokey, void(*kproc)(void*)) +{ + vwdx = dx; + vwdy = dy; + vwchan = chan; + vwbpp = bpp; + if(initdraw(nil, nil, nil) < 0) + sysfatal("initdraw: %r"); + mc = initmouse(nil, screen); + if(mc == nil) + sysfatal("initmouse: %r"); + if(dokey) + proccreate(kproc != nil ? kproc : keyproc, nil, mainstacksize); + if(kproc == nil) + proccreate(joyproc, nil, mainstacksize*2); + bg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0xCCCCCCFF); + screeninit(); +} diff --git a/sys/src/games/eui.h b/sys/src/games/eui.h new file mode 100644 index 000000000..949cf7cb2 --- /dev/null +++ b/sys/src/games/eui.h @@ -0,0 +1,19 @@ +enum{ + MILLION = 1000000, + BILLION = 1000000000, +}; + +extern u64int keys, keys2; +extern int trace, paused; +extern int savereq, loadreq; +extern QLock pauselock; +extern int scale, warp10; +extern uchar *pic; + +void* emalloc(ulong); +void flushmouse(int); +void flushscreen(void); +void flushaudio(int(*)(void)); +void regkeyfn(Rune, void(*)(void)); +void regkey(char*, Rune, int); +void initemu(int, int, int, ulong, int, void(*)(void*)); diff --git a/sys/src/games/gb/apu.c b/sys/src/games/gb/apu.c index dc78992ac..900f7e793 100644 --- a/sys/src/games/gb/apu.c +++ b/sys/src/games/gb/apu.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gb/cpu.c b/sys/src/games/gb/cpu.c index 81c45a034..bff692d02 100644 --- a/sys/src/games/gb/cpu.c +++ b/sys/src/games/gb/cpu.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gb/gb.c b/sys/src/games/gb/gb.c index 55ced730a..b82729023 100644 --- a/sys/src/games/gb/gb.c +++ b/sys/src/games/gb/gb.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gb/mem.c b/sys/src/games/gb/mem.c index a6c6c7599..44a10820c 100644 --- a/sys/src/games/gb/mem.c +++ b/sys/src/games/gb/mem.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gb/mkfile b/sys/src/games/gb/mkfile index b9e767497..80f1d0110 100644 --- a/sys/src/games/gb/mkfile +++ b/sys/src/games/gb/mkfile @@ -10,7 +10,10 @@ OFILES=\ ev.$O\ state.$O\ apu.$O\ + eui.$O\ HFILES=dat.h fns.h #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gba/apu.c b/sys/src/games/gba/apu.c index 4858aced9..31c0f7682 100644 --- a/sys/src/games/gba/apu.c +++ b/sys/src/games/gba/apu.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gba/cpu.c b/sys/src/games/gba/cpu.c index a8354324f..e4f42542e 100644 --- a/sys/src/games/gba/cpu.c +++ b/sys/src/games/gba/cpu.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gba/gba.c b/sys/src/games/gba/gba.c index 06e90b998..ec8c1bc7a 100644 --- a/sys/src/games/gba/gba.c +++ b/sys/src/games/gba/gba.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gba/mem.c b/sys/src/games/gba/mem.c index 032f35eae..c5a56abbb 100644 --- a/sys/src/games/gba/mem.c +++ b/sys/src/games/gba/mem.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/gba/mkfile b/sys/src/games/gba/mkfile index b519b2376..0d12ae943 100644 --- a/sys/src/games/gba/mkfile +++ b/sys/src/games/gba/mkfile @@ -10,7 +10,10 @@ OFILES=\ ev.$O\ apu.$O\ state.$O\ + eui.$O\ HFILES=dat.h fns.h #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/md/cpu.c b/sys/src/games/md/cpu.c index 1a0325f52..6a223c3b1 100644 --- a/sys/src/games/md/cpu.c +++ b/sys/src/games/md/cpu.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/md/md.c b/sys/src/games/md/md.c index 557668dc7..2c0ed3fda 100644 --- a/sys/src/games/md/md.c +++ b/sys/src/games/md/md.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/md/mem.c b/sys/src/games/md/mem.c index 083abaa29..323632d12 100644 --- a/sys/src/games/md/mem.c +++ b/sys/src/games/md/mem.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/md/mkfile b/sys/src/games/md/mkfile index b8363745a..7cad014fc 100644 --- a/sys/src/games/md/mkfile +++ b/sys/src/games/md/mkfile @@ -9,7 +9,10 @@ OFILES=\ vdp.$O\ z80.$O\ ym.$O\ + eui.$O\ HFILES=dat.h fns.h #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/md/ym.c b/sys/src/games/md/ym.c index ca3988433..6d6031958 100644 --- a/sys/src/games/md/ym.c +++ b/sys/src/games/md/ym.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/nes/apu.c b/sys/src/games/nes/apu.c index 52e8e36b5..1db370fde 100644 --- a/sys/src/games/nes/apu.c +++ b/sys/src/games/nes/apu.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/nes/mem.c b/sys/src/games/nes/mem.c index 52e3162ba..cfa71a987 100644 --- a/sys/src/games/nes/mem.c +++ b/sys/src/games/nes/mem.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/nes/mkfile b/sys/src/games/nes/mkfile index 98c8a14e0..d658229b4 100644 --- a/sys/src/games/nes/mkfile +++ b/sys/src/games/nes/mkfile @@ -9,7 +9,10 @@ OFILES=\ ppu.$O\ state.$O\ apu.$O\ + eui.$O\ HFILES=dat.h fns.h #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/nes/ppu.c b/sys/src/games/nes/ppu.c index 498c20082..0f7010198 100644 --- a/sys/src/games/nes/ppu.c +++ b/sys/src/games/nes/ppu.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/snes/cpu.c b/sys/src/games/snes/cpu.c index dbec220a4..7bdc95df9 100644 --- a/sys/src/games/snes/cpu.c +++ b/sys/src/games/snes/cpu.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/snes/dsp.c b/sys/src/games/snes/dsp.c index 991c45316..d3a03028f 100644 --- a/sys/src/games/snes/dsp.c +++ b/sys/src/games/snes/dsp.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/snes/mem.c b/sys/src/games/snes/mem.c index 885713a85..6afb3a9ef 100644 --- a/sys/src/games/snes/mem.c +++ b/sys/src/games/snes/mem.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/snes/mkfile b/sys/src/games/snes/mkfile index 6171c70ac..6cd40ba38 100644 --- a/sys/src/games/snes/mkfile +++ b/sys/src/games/snes/mkfile @@ -10,7 +10,10 @@ OFILES=\ spc.$O\ dsp.$O\ state.$O\ + eui.$O\ HFILES=dat.h fns.h #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/snes/snes.c b/sys/src/games/snes/snes.c index 3e43b631d..769ecb473 100644 --- a/sys/src/games/snes/snes.c +++ b/sys/src/games/snes/snes.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/games/snes/spc.c b/sys/src/games/snes/spc.c index a5fc7ab8a..d00d58efa 100644 --- a/sys/src/games/snes/spc.c +++ b/sys/src/games/snes/spc.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../eui.h" #include "dat.h" #include "fns.h" diff --git a/sys/src/libemu/emu.c b/sys/src/libemu/emu.c deleted file mode 100644 index 908727b75..000000000 --- a/sys/src/libemu/emu.c +++ /dev/null @@ -1,316 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -typedef struct Kfn Kfn; - -u64int keys, keys2; -int trace, paused; -int savereq, loadreq; -QLock pauselock; -int scale, warp10; -uchar *pic; -Rectangle picr; -Mousectl *mc; -Image *bg; - -static int profile, framestep; -static int vwdx, vwdy, vwbpp; -static ulong vwchan; -static Image *fb; - -struct Kfn{ - Rune r; - int k; - char joyk[16]; - void(*fn)(void); - Kfn *n; -}; -Kfn kfn, kkn; - -void * -emalloc(ulong sz) -{ - void *v; - - v = mallocz(sz, 1); - if(v == nil) - sysfatal("malloc: %r"); - setmalloctag(v, getcallerpc(&sz)); - return v; -} - -static void -joyproc(void *) -{ - char *s, *down[9]; - static char buf[64]; - int n, k, j; - Kfn *kp; - - j = 1; - - for(;;){ - n = read(0, buf, sizeof(buf) - 1); - if(n <= 0) - sysfatal("read: %r"); - buf[n] = 0; - n = getfields(buf, down, nelem(down), 1, " "); - k = 0; - for(n--; n >= 0; n--){ - s = down[n]; - if(strcmp(s, "joy1") == 0) - j = 1; - else if(strcmp(s, "joy2") == 0) - j = 2; - for(kp=kkn.n; kp!=nil; kp=kp->n){ - if(strcmp(kp->joyk, s) == 0) - k |= kp->k; - } - } - if(j == 2) - keys2 = k; - else - keys = k; - } -} - -static void -keyproc(void *) -{ - int fd, n, k; - static char buf[256]; - char *s; - Rune r; - Kfn *kp; - - fd = open("/dev/kbd", OREAD); - if(fd < 0) - sysfatal("open: %r"); - for(;;){ - if(buf[0] != 0){ - n = strlen(buf)+1; - memmove(buf, buf+n, sizeof(buf)-n); - } - if(buf[0] == 0){ - n = read(fd, buf, sizeof(buf)-1); - if(n <= 0) - sysfatal("read /dev/kbd: %r"); - buf[n-1] = 0; - buf[n] = 0; - } - if(buf[0] == 'c'){ - if(utfrune(buf, Kdel)){ - close(fd); - threadexitsall(nil); - } - if(utfrune(buf, KF|5)) - savereq = 1; - if(utfrune(buf, KF|6)) - loadreq = 1; - if(utfrune(buf, KF|12)) - profile ^= 1; - if(utfrune(buf, 't')) - trace = !trace; - for(kp=kfn.n; kp!=nil; kp=kp->n){ - if(utfrune(buf, kp->r)) - kp->fn(); - } - } - if(buf[0] != 'k' && buf[0] != 'K') - continue; - s = buf + 1; - k = 0; - while(*s != 0){ - s += chartorune(&r, s); - switch(r){ - case Kdel: close(fd); threadexitsall(nil); - case Kesc: - if(paused) - qunlock(&pauselock); - else - qlock(&pauselock); - paused = !paused; - break; - case KF|1: - if(paused){ - qunlock(&pauselock); - paused=0; - } - framestep = !framestep; - break; - case '`': - warp10 = !warp10; - break; - } - for(kp=kkn.n; kp!=nil; kp=kp->n){ - if(utfrune(buf, kp->r)) - k |= kp->k; - } - } - k &= ~(k << 1 & 0xa0 | k >> 1 & 0x50); - keys = k; - } -} - -static void -timing(void) -{ - static int fcount; - static vlong old; - static char buf[32]; - vlong new; - - if(++fcount == 60) - fcount = 0; - else - return; - new = nsec(); - if(new != old) - sprint(buf, "%6.2f%%", 1e11 / (new - old)); - else - buf[0] = 0; - draw(screen, rectaddpt(Rect(10, 10, vwdx-40, 30), screen->r.min), bg, nil, ZP); - string(screen, addpt(screen->r.min, Pt(10, 10)), display->black, ZP, display->defaultfont, buf); - old = nsec(); -} - -static void -screeninit(void) -{ - Point p; - - scale = Dx(screen->r) / vwdx; - if(scale <= 0) - scale = 1; - else if(scale > 16) - scale = 16; - p = divpt(addpt(screen->r.min, screen->r.max), 2); - picr = Rpt(subpt(p, Pt(scale * vwdx/2, scale * vwdy/2)), - addpt(p, Pt(scale * vwdx/2, scale * vwdy/2))); - freeimage(fb); - fb = allocimage(display, Rect(0, 0, scale * vwdx, scale > 1 ? 1 : scale * vwdy), - vwchan, scale > 1, 0); - free(pic); - pic = emalloc(vwdx * vwdy * vwbpp * scale); - draw(screen, screen->r, bg, nil, ZP); -} - -void -flushmouse(int discard) -{ - Mouse m; - - if(nbrecvul(mc->resizec) > 0){ - if(getwindow(display, Refnone) < 0) - sysfatal("resize failed: %r"); - screeninit(); - } - if(discard) - while(nbrecv(mc->c, &m) > 0) - ; -} - -void -flushscreen(void) -{ - flushmouse(1); - if(scale == 1){ - loadimage(fb, fb->r, pic, vwdx * vwdy * vwbpp); - draw(screen, picr, fb, nil, ZP); - } else { - Rectangle r; - uchar *s; - int w; - - s = pic; - r = picr; - w = vwdx * vwbpp * scale; - while(r.min.y < picr.max.y){ - loadimage(fb, fb->r, s, w); - s += w; - r.max.y = r.min.y+scale; - draw(screen, r, fb, nil, ZP); - r.min.y = r.max.y; - } - } - flushimage(display, 1); - if(profile) - timing(); -} - -void -flushaudio(int (*audioout)(void)) -{ - static vlong old, delta; - vlong new, diff; - - if(audioout == nil || audioout() < 0 && !warp10){ - new = nsec(); - diff = 0; - if(old != 0){ - diff = BILLION/60 - (new - old) - delta; - if(diff >= MILLION) - sleep(diff/MILLION); - } - old = nsec(); - if(diff > 0){ - diff = (old - new) - (diff / MILLION) * MILLION; - delta += (diff - delta) / 100; - } - } - if(framestep){ - paused = 1; - qlock(&pauselock); - framestep = 0; - } -} - -void -regkeyfn(Rune r, void (*fn)(void)) -{ - Kfn *kp; - - for(kp=&kfn; kp->n!=nil; kp=kp->n) - ; - kp->n = emalloc(sizeof *kp); - kp->n->r = r; - kp->n->fn = fn; -} - -void -regkey(char *joyk, Rune r, int k) -{ - Kfn *kp; - - for(kp=&kkn; kp->n!=nil; kp=kp->n) - ; - kp->n = emalloc(sizeof *kp); - strncpy(kp->n->joyk, joyk, sizeof(kp->n->joyk)-1); - kp->n->r = r; - kp->n->k = k; -} - -void -initemu(int dx, int dy, int bpp, ulong chan, int dokey, void(*kproc)(void*)) -{ - vwdx = dx; - vwdy = dy; - vwchan = chan; - vwbpp = bpp; - if(initdraw(nil, nil, nil) < 0) - sysfatal("initdraw: %r"); - mc = initmouse(nil, screen); - if(mc == nil) - sysfatal("initmouse: %r"); - if(dokey) - proccreate(kproc != nil ? kproc : keyproc, nil, mainstacksize); - if(kproc == nil) - proccreate(joyproc, nil, mainstacksize*2); - bg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0xCCCCCCFF); - screeninit(); -} diff --git a/sys/src/libemu/mkfile b/sys/src/libemu/mkfile deleted file mode 100644 index f436124a2..000000000 --- a/sys/src/libemu/mkfile +++ /dev/null @@ -1,20 +0,0 @@ -