Skip to content

Commit

Permalink
(shmif/tui) move push_message to shmif from tui
Browse files Browse the repository at this point in the history
The need for this re-appears practically every time someone wants
to add a short message to the clipboard, so better to make it part
of the interop support functions.
  • Loading branch information
letoram committed Aug 11, 2023
1 parent 0795aae commit d036ede
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 49 deletions.
45 changes: 45 additions & 0 deletions src/shmif/arcan_shmif_control.c
Expand Up @@ -3747,6 +3747,51 @@ shmif_reset_hook arcan_shmif_resetfunc(
return old_hook;
}

#include "../frameserver/util/utf8.c"
bool arcan_shmif_pushutf8(
struct arcan_shmif_cont* acon, struct arcan_event* base,
const char* msg, size_t len)
{
uint32_t state = 0, codepoint = 0;
const char* outs = msg;
size_t maxlen = sizeof(base->ext.message.data) - 1;

/* utf8- point aligned against block size */
while (len > maxlen){
size_t i, lastok = 0;
state = 0;
for (i = 0; i <= maxlen - 1; i++){
if (UTF8_ACCEPT == utf8_decode(&state, &codepoint, (uint8_t)(msg[i])))
lastok = i;

if (i != lastok){
if (0 == i)
return false;
}
}

memcpy(base->ext.message.data, outs, lastok);
base->ext.message.data[lastok] = '\0';
len -= lastok;
outs += lastok;
if (len)
base->ext.message.multipart = 1;
else
base->ext.message.multipart = 0;

arcan_shmif_enqueue(acon, base);
}

/* flush remaining */
if (len){
snprintf((char*)base->ext.message.data, maxlen, "%s", outs);
base->ext.message.multipart = 0;
arcan_shmif_enqueue(acon, base);
}

return true;
}

/*
* Missing: special behavior for SHMIF_RHINT_SUBREGION_CHAIN, setup chain of
* atomic [uint32_t, bitfl] and walk from first position to last free. Bitfl
Expand Down
14 changes: 14 additions & 0 deletions src/shmif/arcan_shmif_interop.h
Expand Up @@ -524,6 +524,20 @@ bool arcan_shmif_mousestate_ioev(
struct arcan_shmif_cont*, uint8_t* state,
struct arcan_ioevent* inev, int* out_x, int* out_y);

/*
* Support function for taking a small utf-8 encoded text string on top
* of [baseev] (will be modified).
*
* It will validate and terminate / return false if [msg] fails proper utf-8
* validation.
*
* This is mainly useful for _MESSAGE events on clipboard subsegments for
* shorter (comparable to non-incr text/utf-8 in X11).
*/
bool arcan_shmif_pushutf8(
struct arcan_shmif_cont*, struct arcan_event* baseev,
const char* msg, size_t msg_sz);

/*
* Part of auxiliary library, pulls in more dependencies and boiler-plate
* for setting up accelerated graphics
Expand Down
45 changes: 1 addition & 44 deletions src/shmif/tui/core/clipboard.c
Expand Up @@ -37,49 +37,6 @@ void tui_clipboard_check(struct tui_context* tui)
arcan_shmif_drop(&tui->clip_in);
}

bool tui_push_message(struct arcan_shmif_cont* acon,
struct arcan_event* base, const char* msg, size_t len)
{
uint32_t state = 0, codepoint = 0;
const char* outs = msg;
size_t maxlen = sizeof(base->ext.message.data) - 1;

/* utf8- point aligned against block size */
while (len > maxlen){
size_t i, lastok = 0;
state = 0;
for (i = 0; i <= maxlen - 1; i++){
if (UTF8_ACCEPT == utf8_decode(&state, &codepoint, (uint8_t)(msg[i])))
lastok = i;

if (i != lastok){
if (0 == i)
return false;
}
}

memcpy(base->ext.message.data, outs, lastok);
base->ext.message.data[lastok] = '\0';
len -= lastok;
outs += lastok;
if (len)
base->ext.message.multipart = 1;
else
base->ext.message.multipart = 0;

arcan_shmif_enqueue(acon, base);
}

/* flush remaining */
if (len){
snprintf((char*)base->ext.message.data, maxlen, "%s", outs);
base->ext.message.multipart = 0;
arcan_shmif_enqueue(acon, base);
}

return true;
}

bool tui_clipboard_push(struct tui_context* tui, const char* sel, size_t len)
{
/*
Expand All @@ -95,5 +52,5 @@ bool tui_clipboard_push(struct tui_context* tui, const char* sel, size_t len)
.ext.kind = ARCAN_EVENT(MESSAGE)
};

return tui_push_message(&tui->clip_out, &msgev, sel, len);
return arcan_shmif_pushutf8(&tui->clip_out, &msgev, sel, len);
}
4 changes: 2 additions & 2 deletions src/shmif/tui/tui.c
Expand Up @@ -1075,14 +1075,14 @@ void arcan_tui_message(
workstr[0] = '>';
memcpy(&workstr[1], msg, len);
workstr[len+1] = '\0';
tui_push_message(&c->acon, &outev, msg, len);
arcan_shmif_pushutf8(&c->acon, &outev, msg, len);
free(workstr);
return;
}
else
return;

tui_push_message(&c->acon, &outev, msg, len);
arcan_shmif_pushutf8(&c->acon, &outev, msg, len);
}

pid_t arcan_tui_handover(
Expand Down
3 changes: 0 additions & 3 deletions src/shmif/tui/tui_int.h
Expand Up @@ -218,9 +218,6 @@ int tui_tpack_unpack(struct tui_context* tui,
/* DISPATCH (tui_dispatch.c) related code */
/* ========================================================================== */

bool tui_push_message(struct arcan_shmif_cont* tui,
struct arcan_event* base, const char* msg, size_t len);

/*
* Poll the incoming event queue on the tui segment, process TARGET events
* and forward IO events to
Expand Down

0 comments on commit d036ede

Please sign in to comment.