clipboard fixes

This commit is contained in:
Matthew 2025-10-05 15:03:17 +11:00
parent 57e3634691
commit 83dd2f6039

View File

@ -528,7 +528,7 @@ struct PlatformWindow
Inputs[2] inputs; Inputs[2] inputs;
TicketMut input_mutex; TicketMut input_mutex;
Mut cb_mut; TicketMut cb_mut;
Selection[CBM.max] selections; Selection[CBM.max] selections;
version(linux) u32 cb_transfer_size; version(linux) u32 cb_transfer_size;
}; };
@ -566,7 +566,7 @@ CreateWindow(string name, u16 width, u16 height)
h: height, h: height,
input_mutex: CreateTicketMut(), input_mutex: CreateTicketMut(),
msg_queue: CreateMessageQueue(), msg_queue: CreateMessageQueue(),
cb_mut: CreateMut(), cb_mut: CreateTicketMut(),
inputs: [ inputs: [
{ arena: CreateArena(MB(1)) }, { arena: CreateArena(MB(1)) },
{ arena: CreateArena(MB(1)) }, { arena: CreateArena(MB(1)) },
@ -819,8 +819,8 @@ TransmitSelection(PlatformWindow* w, xcb_selection_request_event_t* ev)
else if(ev.target == w.atoms[Atoms.Utf8String]) else if(ev.target == w.atoms[Atoms.Utf8String])
{ {
Selection* sel = null; Selection* sel = null;
if(TryLock(&w.cb_mut)) Lock(&w.cb_mut);
{
foreach(i; CBM.min .. CBM.max) foreach(i; CBM.min .. CBM.max)
{ {
if(w.selections[i].xmode == ev.selection) if(w.selections[i].xmode == ev.selection)
@ -848,7 +848,6 @@ TransmitSelection(PlatformWindow* w, xcb_selection_request_event_t* ev)
Unlock(&w.cb_mut); Unlock(&w.cb_mut);
} }
}
return result; return result;
} }
@ -858,11 +857,10 @@ ClipboardOwns(PlatformWindow* w, ClipboardMode mode)
{ {
bool result; bool result;
if(TryLock(&w.cb_mut)) Lock(&w.cb_mut);
{
result = w.selections[mode].owned; result = w.selections[mode].owned;
Unlock(&w.cb_mut); Unlock(&w.cb_mut);
}
return result; return result;
} }
@ -872,8 +870,8 @@ ClipboardText(PlatformWindow* w, ClipboardMode mode)
{ {
u8[] buf; u8[] buf;
if(TryLock(&w.cb_mut)) Lock(&w.cb_mut);
{
scope(exit) Unlock(&w.cb_mut); scope(exit) Unlock(&w.cb_mut);
Selection* sel = &w.selections[mode]; Selection* sel = &w.selections[mode];
@ -910,15 +908,12 @@ ClipboardText(PlatformWindow* w, ClipboardMode mode)
timeout.tv_nsec = timeout.tv_nsec % 1000000000UL; timeout.tv_nsec = timeout.tv_nsec % 1000000000UL;
} }
while(pret == 0 && sel.data.length == 0) Lock(&w.cb_mut);
{ Unlock(&w.cb_mut);
pret = PThreadCondTimedWait(&w.thread.cond, &w.thread.mut, &timeout);
}
GetClipboardSelection(w, sel); GetClipboardSelection(w, sel);
} }
} }
}
return buf; return buf;
} }
@ -934,8 +929,11 @@ SetClipboard(PlatformWindow* w, u8[] data, ClipboardMode mode)
{ {
bool result = true; bool result = true;
if(data.length > 0 && TryLock(&w.cb_mut)) if(data.length > 0)
{ {
Lock(&w.cb_mut);
scope(exit) Unlock(&w.cb_mut);
Selection* sel = &w.selections[mode]; Selection* sel = &w.selections[mode];
if(sel.data.length > 0) if(sel.data.length > 0)
{ {
@ -1043,8 +1041,10 @@ RetrieveSelection(PlatformWindow* w, xcb_selection_notify_event_t* ev)
} }
} }
if(buf != null && TryLock(&w.cb_mut)) if(buf != null)
{ {
Lock(&w.cb_mut);
Selection* sel; Selection* sel;
foreach(i; CBM.min .. CBM.max) foreach(i; CBM.min .. CBM.max)
{ {
@ -1082,8 +1082,10 @@ ClearSelection(PlatformWindow* w, xcb_selection_clear_event_t* ev)
foreach(i; CBM.min .. CBM.max) foreach(i; CBM.min .. CBM.max)
{ {
Selection* sel = &w.selections[i]; Selection* sel = &w.selections[i];
if(sel.xmode == ev.selection && TryLock(&w.cb_mut)) if(sel.xmode == ev.selection)
{ {
Lock(&w.cb_mut);
FreeArray(sel.data); FreeArray(sel.data);
sel.data = []; sel.data = [];
sel.owned = false; sel.owned = false;