ref: 3224333549a381765671e17c31043211ac6bd4c4
parent: 4338bb921e84cfb5186fe60950cf5704f997d2c2
author: sirjofri <sirjofri@sirjofri.de>
date: Tue May 14 10:55:57 EDT 2024
code cleanup. capture mouse/keyboard input directly
--- a/plan9.c
+++ b/plan9.c
@@ -38,8 +38,13 @@
long draw;
} ptimes;
+enum {
+ GAME = 0,
+ SETTINGS = 1,
+};
+
void
-frontend_default_colour(frontend *fe, float *output)
+frontend_default_colour(frontend *, float *output)
{
output[0] = .9;
output[1] = .9;
@@ -102,6 +107,7 @@
static void p9_draw_text(void *handle, int x, int y, int fonttype, int fontsize, int align, int color, const char *text)
{
+ // todo: align, fontsize, fonttype
frontend *fe = (frontend*)handle;
#ifdef DIRECTDRAW
string(screen, addpt(Pt(x, y), fe->ZP), fe->colors[color], ZP, font, text);
@@ -193,6 +199,7 @@
static void
p9_draw_update(void *handle, int x, int y, int w, int h)
{
+ USED(handle, x, y, w, h);
//frontend *fe = (frontend*)handle;
//print("draw_update\n");
//chanprint(fe->cs->ctl, showcmd);
@@ -217,6 +224,7 @@
static void
p9_start_draw(void *handle)
{
+ USED(handle);
#ifdef PROFILE
drawtime = times(nil);
#endif
@@ -244,6 +252,7 @@
p9_blitter_new(void *handle, int w, int h)
{
blitter *bl;
+ USED(handle);
bl = malloc(sizeof(blitter));
bl->blimg = allocimage(display, Rect(0, 0, w, h), screen->chan, 0, 0);
return bl;
@@ -252,6 +261,7 @@
static void
p9_blitter_free(void *handle, blitter *bl)
{
+ USED(handle);
freeimage(bl->blimg);
free(bl);
}
@@ -317,11 +327,10 @@
void
initui(Controlset *cs, Channel *c)
{
- Control *b_game, *b_settings, *c_game, *c_settings, *stackmain, *menu;
- Control *l_status;
+ Control *b_game, *b_settings, *c_game, *c_settings, *stackmain;
Point p;
- menu = createrow(cs, "rowmain");
+ createrow(cs, "rowmain");
stackmain = createstack(cs, "stackmain");
chanprint(cs->ctl, "stackmain border 1");
@@ -353,7 +362,7 @@
controlwire(c_settings, "event", c);
controlwire(b_settings, "event", c);
- l_status = createlabel(cs, "l_status");
+ createlabel(cs, "l_status");
chanprint(cs->ctl, "stackmain add c_game c_settings");
chanprint(cs->ctl, "rowmain add b_game\nrowmain add b_settings");
@@ -364,13 +373,13 @@
}
void
-initfe(frontend *fe)
+initfe(frontend *fe, Mousectl *mousectl)
{
float *colors;
int ncolors;
int r, g, b;
- int col;
float bgcol[3];
+ Channel *c, *d;
fe->image = allocimage(display, screen->r, screen->chan, 0, 0);
@@ -387,7 +396,9 @@
}
free(colors);
- fe->cs = newcontrolset(screen, nil, nil, nil);
+ c = chancreate(sizeof(Mouse), 0);
+ d = chancreate(sizeof(Rune), 0);
+ fe->cs = newcontrolset(screen, d, c, mousectl->resizec);
fe->c = chancreate(sizeof(char*), 0);
ctldeletequits = 1;
namectlimage(fe->image, "frame");
@@ -465,7 +476,7 @@
chanprint(cs->ctl, "l_status rect %R\nl_status show", sarea);
#ifdef DIRECTDRAW
- if (fe->showframe == 0)
+ if (fe->showframe == GAME)
midend_force_redraw(fe->me);
#endif
@@ -556,7 +567,7 @@
void
showframe(int frame)
{
- if (frame == 0)
+ if (frame == GAME)
midend_force_redraw(fe->me);
fe->showframe = frame;
chanprint(fe->cs->ctl, "stackmain reveal %d", frame);
@@ -563,7 +574,7 @@
}
int
-keyev(int k)
+keyev(Rune k)
{
switch (k) {
case 'q':
@@ -571,9 +582,10 @@
return 1; /* return 1 to quit */
case 'n':
midend_process_key(fe->me, 0, 0, UI_NEWGAME);
+ chanprint(fe->cs->ctl, showcmd);
break;
default:
- if (k >= 0 && midend_process_key(fe->me, 0, 0, k) == PKR_QUIT)
+ if (midend_process_key(fe->me, 0, 0, k) == PKR_QUIT)
return 1;
}
return 0;
@@ -582,7 +594,9 @@
void
tick(float delta)
{
+#ifdef PROFILE
char msg[128];
+#endif
if (fe->timeractive) {
midend_timer(fe->me, delta);
@@ -621,11 +635,50 @@
}
void
+processmouse(Mouse *m, int *lm)
+{
+ int x, y, r;
+ Control *c;
+
+ if (fe->showframe != GAME)
+ goto Ctrl;
+
+ c = controlcalled("c_game");
+
+ if (!ptinrect(m->xy, c->rect))
+ goto Ctrl;
+
+ x = m->xy.x - c->rect.min.x;
+ y = m->xy.y - c->rect.min.y;
+ r = -1;
+
+ if ( ((*lm)&1) && !(m->buttons&1))
+ r = midend_process_key(fe->me, x, y, LEFT_RELEASE);
+ if (!((*lm)&1) && (m->buttons&1))
+ r = midend_process_key(fe->me, x, y, LEFT_BUTTON);
+ if ( ((*lm)&2) && !(m->buttons&2))
+ r = midend_process_key(fe->me, x, y, MIDDLE_RELEASE);
+ if (!((*lm)&2) && (m->buttons&2))
+ r = midend_process_key(fe->me, x, y, MIDDLE_BUTTON);
+ if ( ((*lm)&4) && !(m->buttons&4))
+ r = midend_process_key(fe->me, x, y, RIGHT_RELEASE);
+ if (!((*lm)&4) && (m->buttons&2))
+ r = midend_process_key(fe->me, x, y, RIGHT_BUTTON);
+ if (r >= 0) {
+ chanprint(fe->cs->ctl, showcmd);
+ }
+ *lm = m->buttons;
+
+ return;
+
+Ctrl:
+ send(fe->cs->mousec, m);
+}
+
+void
threadmain(int argc, char **argv)
{
- int x, y, n, r;
int lastmouse;
- long l;
char *s, *args[6];
int doprintoptions = 0;
char *wintitle;
@@ -633,9 +686,15 @@
config_item *cfg;
int changedprefs = 0;
float delta;
+ Mousectl *mousectl;
+ Mouse m;
+ Keyboardctl *keyboardctl;
+ Rune rune;
Alt a[] = {
{ nil, &s, CHANRCV },
{ nil, &delta, CHANRCV },
+ { nil, &m, CHANRCV },
+ { nil, &rune, CHANRCV },
{ nil, nil, CHANEND },
};
@@ -674,14 +733,19 @@
sysfatal("initdraw failed: %r");
}
+ mousectl = initmouse(nil, screen);
+ keyboardctl = initkeyboard(nil);
+
initcontrols();
- initfe(fe);
+ initfe(fe, mousectl);
midend_new_game(fe->me);
resizecontrolset(fe->cs);
a[0].c = fe->c;
a[1].c = chancreate(sizeof(float), 0);
+ a[2].c = mousectl->c;
+ a[3].c = keyboardctl->c;
proccreate(timerproc, a[1].c, 4096);
@@ -688,52 +752,29 @@
for (;;) {
switch (alt(a)) {
case 0: /* libcontrol event channel */
- n = tokenize(s, args, nelem(args));
+ tokenize(s, args, nelem(args));
if (strcmp(args[0], "c_settings:") == 0) {
print("c_settings event: %s\n", args[1]);
} else
- if (strcmp(args[0], "c_game:") == 0) {
- if (strcmp(args[1], "mouse") == 0) {
- x = atoi(args[2]+1) - fe->ZP.x; /* ignore '[' */
- y = atoi(args[3]) - fe->ZP.y;
- n = atoi(args[4]);
- r = -1;
- if ( (lastmouse&1) && !(n&1))
- r = midend_process_key(fe->me, x, y, LEFT_RELEASE);
- if (!(lastmouse&1) && (n&1))
- r = midend_process_key(fe->me, x, y, LEFT_BUTTON);
- if ( (lastmouse&2) && !(n&2))
- r = midend_process_key(fe->me, x, y, MIDDLE_RELEASE);
- if (!(lastmouse&2) && (n&2))
- r = midend_process_key(fe->me, x, y, MIDDLE_BUTTON);
- if ( (lastmouse&4) && !(n&4))
- r = midend_process_key(fe->me, x, y, RIGHT_RELEASE);
- if (!(lastmouse&4) && (n&2))
- r = midend_process_key(fe->me, x, y, RIGHT_BUTTON);
- if (r >= 0) {
- chanprint(fe->cs->ctl, showcmd);
- }
- lastmouse = n;
- } else
- if (strcmp(args[1], "key") == 0) {
- l = strtol(args[2], nil, 0);
- if (keyev(l))
- goto Out;
- }
- } else
if (strcmp(args[0], "b_game:") == 0) {
- showframe(0);
+ showframe(GAME);
chanprint(fe->cs->ctl, "b_game value 0");
} else
if (strcmp(args[0], "b_settings:") == 0) {
- showframe(1);
+ showframe(SETTINGS);
chanprint(fe->cs->ctl, "b_settings value 0");
- } else
- print("event from %s: %s\n", args[0], args[1]);
+ }
break;
case 1: /* timer */
tick(delta);
+ break;
+ case 2: /* mouse */
+ processmouse(&m, &lastmouse);
+ break;
+ case 3: /* keyboard */
+ if (keyev(rune))
+ goto Out;
break;
}
}