Skip to content

Commit

Permalink
(shmif) mousestate helper improvements
Browse files Browse the repository at this point in the history
Previously the caller was always responsible for maintaining the
storage for the state blob which was a poor decision. As to not break
API we overload and accept a NULL mstate holder to mean 'use the
internal shmif tracking structure'.
  • Loading branch information
letoram committed Jun 10, 2023
1 parent f8931b1 commit eaafb6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/shmif/arcan_shmif_control.c
Expand Up @@ -56,6 +56,19 @@ enum debug_level {
DETAILED = 2
};

struct mstate {
union {
struct {
int32_t ax, ay, lx, ly;
uint8_t rel : 1;
uint8_t inrel : 1;
};
uint8_t state[ASHMIF_MSTATE_SZ];
};
};

_Static_assert(sizeof(struct mstate) == ASHMIF_MSTATE_SZ, "invalid mstate sz");

/*
* Accessor for redirectable log output related to a shmif context
* The context association is a placeholder for being able to handle
Expand Down Expand Up @@ -242,6 +255,8 @@ struct shmif_hidden {
char key[256];
} pseg;

struct mstate mstate;

/* The 'guard' structure is used by a separate monitoring thread that will
* track a pid or descriptor for aliveness. If the tracking fails, it will
* unlock semaphores and trigger an at_exit- like handler. This is practically
Expand Down Expand Up @@ -3197,23 +3212,16 @@ int arcan_shmif_segkind(struct arcan_shmif_cont* con)
return (!con || !con->priv) ? SEGID_UNKNOWN : con->priv->type;
}

struct mstate {
union {
struct {
int32_t ax, ay, lx, ly;
uint8_t rel : 1;
uint8_t inrel : 1;
};
uint8_t state[ASHMIF_MSTATE_SZ];
};
};

_Static_assert(sizeof(struct mstate) == ASHMIF_MSTATE_SZ, "invalid mstate sz");

void arcan_shmif_mousestate_setup(
struct arcan_shmif_cont* con, bool relative, uint8_t* state)
{
if (!con || !con->priv)
return;

struct mstate* ms = (struct mstate*) state;
if (!ms)
ms = &con->priv->mstate;

*ms = (struct mstate){
.rel = relative
};
Expand Down Expand Up @@ -3259,8 +3267,13 @@ bool arcan_shmif_mousestate_ioev(
struct arcan_ioevent* inev, int* out_x, int* out_y)
{
struct mstate* ms = (struct mstate*) state;
if (!con || !con->priv)
return false;

if (!state)
ms = &con->priv->mstate;

if (!state || !out_x || !out_y || !con)
if (!ms|| !out_x || !out_y)
return false;

if (!inev){
Expand All @@ -3271,7 +3284,7 @@ bool arcan_shmif_mousestate_ioev(
return true;
}

if (!state ||
if (!ms ||
inev->datatype != EVENT_IDATATYPE_ANALOG ||
inev->devkind != EVENT_IDEVKIND_MOUSE
)
Expand Down
5 changes: 5 additions & 0 deletions src/shmif/arcan_shmif_interop.h
Expand Up @@ -494,6 +494,11 @@ void arcan_shmif_bgcopy(
* devkind == EVENT_IDEVKIND_MOUSE for datatype == EVENT_IDATATYPE_ANALOG.
* If >true< the status of have changed since last time.
*
* If [state=NULL] an internal state tracking store inside the context will be
* used. This might not always be desired if you want to inspect the values
* yourself or if you are juggling multiple contexts with a shared state
* machine.
*
* uint8_t mstate[ASHMIF_MSTATE_SZ];
* arcan_shmif_mousestate_setup(acon, false, mstate);
* ... in event loop ...
Expand Down

0 comments on commit eaafb6c

Please sign in to comment.