mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 05:53:02 +01:00
Dnd works
This commit is contained in:
parent
70add920fe
commit
c825adc1e3
@ -967,6 +967,12 @@ static useconds_t optimal_msleepval(uint64_t *looptimer, uint64_t *loopcount, ui
|
|||||||
return new_sleep;
|
return new_sleep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cb(const char* asdv, DropType dt)
|
||||||
|
{
|
||||||
|
if (dt != DT_plain)
|
||||||
|
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, asdv);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
parse_args(argc, argv);
|
parse_args(argc, argv);
|
||||||
@ -1004,7 +1010,7 @@ int main(int argc, char *argv[])
|
|||||||
int settings_err = settings_load(user_settings, p);
|
int settings_err = settings_load(user_settings, p);
|
||||||
|
|
||||||
#ifdef X11
|
#ifdef X11
|
||||||
xtra_init();
|
xtra_init(cb);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Tox *m = init_tox();
|
Tox *m = init_tox();
|
||||||
|
360
src/xtra.c
360
src/xtra.c
@ -2,96 +2,255 @@
|
|||||||
|
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <X11/Xatom.h>
|
#include <X11/Xatom.h>
|
||||||
// #include <X11/X.h>
|
|
||||||
// #include <X11/Xutil.h>
|
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
static Display *the_d = NULL;
|
#include <unistd.h>
|
||||||
static Window self_window; /* We will assume that during initialization
|
#include <stdio.h>
|
||||||
* we have this window focused */
|
|
||||||
|
|
||||||
Atom XdndAware,
|
|
||||||
XdndEnter,
|
|
||||||
XdndLeave,
|
|
||||||
XdndPosition,
|
|
||||||
XdndStatus,
|
|
||||||
XdndDrop,
|
|
||||||
XdndSelection,
|
|
||||||
XdndDATA,
|
|
||||||
XdndActionCopy,
|
|
||||||
XdndActionAsk,
|
|
||||||
XdndTypeList;
|
|
||||||
|
|
||||||
|
|
||||||
void *event_loop(void* p)
|
const Atom XtraTerminate = 1;
|
||||||
|
const Atom XtraNil = 0;
|
||||||
|
|
||||||
|
static Atom XdndAware;
|
||||||
|
static Atom XdndEnter;
|
||||||
|
static Atom XdndLeave;
|
||||||
|
static Atom XdndPosition;
|
||||||
|
static Atom XdndStatus;
|
||||||
|
static Atom XdndDrop;
|
||||||
|
static Atom XdndSelection;
|
||||||
|
static Atom XdndDATA;
|
||||||
|
static Atom XdndTypeList;
|
||||||
|
static Atom XdndActionCopy;
|
||||||
|
static Atom XdndFinished;
|
||||||
|
|
||||||
|
struct _Xtra {
|
||||||
|
drop_callback on_drop;
|
||||||
|
Display *display;
|
||||||
|
Window terminal_window;
|
||||||
|
Window proxy_window;
|
||||||
|
Window source_window; /* When we have a drop */
|
||||||
|
Atom handling_version;
|
||||||
|
Atom expecting_type;
|
||||||
|
} Xtra;
|
||||||
|
|
||||||
|
typedef struct _Property
|
||||||
{
|
{
|
||||||
(void) p;
|
unsigned char *data;
|
||||||
|
int read_format;
|
||||||
|
unsigned long read_num;
|
||||||
|
Atom read_type;
|
||||||
|
} Property;
|
||||||
|
|
||||||
XEvent event;
|
Property read_property(Window s, Atom p)
|
||||||
while (the_d)
|
{
|
||||||
{
|
Atom read_type;
|
||||||
XNextEvent(the_d, &event);
|
int read_format;
|
||||||
|
unsigned long read_num;
|
||||||
|
unsigned long left_bytes;
|
||||||
|
unsigned char *data = NULL;
|
||||||
|
|
||||||
switch (event.type)
|
int read_bytes = 1024;
|
||||||
{
|
|
||||||
case ClientMessage:
|
/* Keep trying to read the property until there are no bytes unread */
|
||||||
{
|
do {
|
||||||
assert(0);
|
if (data) XFree(data);
|
||||||
if(event.xclient.message_type == XdndEnter) {
|
|
||||||
} else if(event.xclient.message_type == XdndPosition) {
|
XGetWindowProperty(Xtra.display, s,
|
||||||
Window src = event.xclient.data.l[0];
|
p, 0,
|
||||||
XEvent event = {
|
read_bytes,
|
||||||
|
False, AnyPropertyType,
|
||||||
|
&read_type, &read_format,
|
||||||
|
&read_num, &left_bytes,
|
||||||
|
&data);
|
||||||
|
|
||||||
|
read_bytes *= 2;
|
||||||
|
} while (left_bytes != 0);
|
||||||
|
|
||||||
|
Property property = {data, read_format, read_num, read_type};
|
||||||
|
return property;
|
||||||
|
}
|
||||||
|
|
||||||
|
Atom get_dnd_type(long *a, int l)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
for (; i < l; i ++) {
|
||||||
|
if (a[i] != XtraNil) return a[i]; /* Get first valid */
|
||||||
|
}
|
||||||
|
return XtraNil;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO maybe support only certain types in the future */
|
||||||
|
static void handle_xdnd_enter(XClientMessageEvent* e)
|
||||||
|
{
|
||||||
|
Xtra.handling_version = (e->data.l[1] >> 24);
|
||||||
|
|
||||||
|
if ((e->data.l[1] & 1)) {
|
||||||
|
// Fetch the list of possible conversions
|
||||||
|
Property p = read_property(e->data.l[0], XdndTypeList);
|
||||||
|
Xtra.expecting_type = get_dnd_type((long*)p.data, p.read_num);
|
||||||
|
XFree(p.data);
|
||||||
|
} else {
|
||||||
|
// Use the available list
|
||||||
|
Xtra.expecting_type = get_dnd_type(e->data.l + 2, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_xdnd_position(XClientMessageEvent* e)
|
||||||
|
{
|
||||||
|
XEvent ev = {
|
||||||
.xclient = {
|
.xclient = {
|
||||||
.type = ClientMessage,
|
.type = ClientMessage,
|
||||||
.display = the_d,
|
.display = e->display,
|
||||||
.window = src,
|
.window = e->data.l[0],
|
||||||
.message_type = XdndStatus,
|
.message_type = XdndStatus,
|
||||||
.format = 32,
|
.format = 32,
|
||||||
.data = {
|
.data = {
|
||||||
.l = {self_window, 1, 0, 0, XdndActionCopy}
|
.l = {
|
||||||
|
Xtra.proxy_window,
|
||||||
|
(Xtra.expecting_type != XtraNil),
|
||||||
|
0, 0,
|
||||||
|
XdndActionCopy
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
XSendEvent(the_d, src, 0, 0, &event);
|
|
||||||
} else if(event.xclient.message_type == XdndStatus) {
|
XSendEvent(Xtra.display, e->data.l[0], False, NoEventMask, &ev);
|
||||||
} else if(event.xclient.message_type == XdndDrop) {
|
XFlush(Xtra.display);
|
||||||
XConvertSelection(the_d, XdndSelection, XA_STRING, XdndDATA, self_window, CurrentTime);
|
}
|
||||||
} else if(event.xclient.message_type == XdndLeave) {
|
|
||||||
|
static void handle_xdnd_drop(XClientMessageEvent* e)
|
||||||
|
{
|
||||||
|
/* Not expecting any type */
|
||||||
|
if (Xtra.expecting_type == XtraNil) {
|
||||||
|
XEvent ev = {
|
||||||
|
.xclient = {
|
||||||
|
.type = ClientMessage,
|
||||||
|
.display = e->display,
|
||||||
|
.window = e->data.l[0],
|
||||||
|
.message_type = XdndFinished,
|
||||||
|
.format = 32,
|
||||||
|
.data = {
|
||||||
|
.l = {Xtra.proxy_window, 0, 0}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
XSendEvent(Xtra.display, e->data.l[0], False, NoEventMask, &ev);
|
||||||
} else {
|
} else {
|
||||||
goto exit;
|
Xtra.source_window = e->data.l[0];
|
||||||
|
XConvertSelection(Xtra.display,
|
||||||
|
XdndSelection,
|
||||||
|
Xtra.expecting_type,
|
||||||
|
XdndSelection,
|
||||||
|
Xtra.proxy_window,
|
||||||
|
Xtra.handling_version >= 1 ? e->data.l[2] : CurrentTime);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} break;
|
static void handle_xdnd_selection(XSelectionEvent* e)
|
||||||
|
{
|
||||||
default:
|
/* DnD succesfully finished, send finished and call callback */
|
||||||
// XSendEvent(the_d, self_window, 0, 0, &event);
|
XEvent ev = {
|
||||||
break;
|
.xclient = {
|
||||||
|
.type = ClientMessage,
|
||||||
|
.display = Xtra.display,
|
||||||
|
.window = Xtra.source_window,
|
||||||
|
.message_type = XdndFinished,
|
||||||
|
.format = 32,
|
||||||
|
.data = {
|
||||||
|
.l = {Xtra.proxy_window, 1, XdndActionCopy}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
XSendEvent(Xtra.display, Xtra.source_window, False, NoEventMask, &ev);
|
||||||
|
|
||||||
|
Property p = read_property(Xtra.proxy_window, XdndSelection);
|
||||||
|
DropType dt;
|
||||||
|
|
||||||
|
if (strcmp(XGetAtomName(Xtra.display, p.read_type), "text/uri-list") == 0)
|
||||||
|
dt = DT_file_list;
|
||||||
|
else /* text/uri-list */
|
||||||
|
dt = DT_plain;
|
||||||
|
|
||||||
|
|
||||||
|
/* Call callback for every entry */
|
||||||
|
if (Xtra.on_drop && p.read_num)
|
||||||
|
{
|
||||||
|
char *sptr;
|
||||||
|
char *str = strtok_r((char*)p.data, "\n\r", &sptr);
|
||||||
|
|
||||||
|
if (str) Xtra.on_drop(str, dt);
|
||||||
|
while ((str = strtok_r(NULL, "\n\r", &sptr)))
|
||||||
|
Xtra.on_drop(str, dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.data) XFree(p.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *event_loop(void* p)
|
||||||
|
{
|
||||||
|
/* Handle events like a real nigga */
|
||||||
|
|
||||||
|
(void) p; /* DINDUNOTHIN */
|
||||||
|
|
||||||
|
XEvent event;
|
||||||
|
int pending;
|
||||||
|
|
||||||
|
while (Xtra.display)
|
||||||
|
{
|
||||||
|
/* NEEDMOEVENTSFODEMPROGRAMS */
|
||||||
|
|
||||||
|
XLockDisplay(Xtra.display);
|
||||||
|
if((pending = XPending(Xtra.display))) XNextEvent(Xtra.display, &event);
|
||||||
|
|
||||||
|
if (!pending)
|
||||||
|
{
|
||||||
|
XUnlockDisplay(Xtra.display);
|
||||||
|
usleep(10000);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type == ClientMessage)
|
||||||
|
{
|
||||||
|
Atom type = event.xclient.message_type;
|
||||||
|
|
||||||
|
if (type == XdndEnter) handle_xdnd_enter(&event.xclient);
|
||||||
|
else if (type == XdndPosition) handle_xdnd_position(&event.xclient);
|
||||||
|
else if (type == XdndDrop) handle_xdnd_drop(&event.xclient);
|
||||||
|
else if (type == XtraTerminate) break;
|
||||||
|
}
|
||||||
|
else if (event.type == SelectionNotify) handle_xdnd_selection(&event.xselection);
|
||||||
|
/* AINNOBODYCANHANDLEDEMEVENTS*/
|
||||||
|
else XSendEvent(Xtra.display, Xtra.terminal_window, 0, 0, &event);
|
||||||
|
|
||||||
|
XUnlockDisplay(Xtra.display);
|
||||||
|
}
|
||||||
|
|
||||||
exit:
|
|
||||||
/* Actual XTRA termination
|
/* Actual XTRA termination
|
||||||
* Please call xtra_terminate() at exit
|
* Please call xtra_terminate() at exit
|
||||||
* otherwise bad stuff happens
|
* otherwise bad stuff happens
|
||||||
*/
|
*/
|
||||||
if (the_d) XCloseDisplay(the_d);
|
if (Xtra.display) XCloseDisplay(Xtra.display);
|
||||||
return NULL;
|
return (Xtra.display = NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void xtra_init()
|
int xtra_init(drop_callback d)
|
||||||
{
|
{
|
||||||
the_d = XOpenDisplay(NULL);
|
memset(&Xtra, 0, sizeof(Xtra));
|
||||||
self_window = xtra_focused_window_id();
|
|
||||||
|
|
||||||
Window m_wndProxy;
|
if (!d) return -1;
|
||||||
|
else Xtra.on_drop = d;
|
||||||
|
|
||||||
|
XInitThreads();
|
||||||
|
if ( !(Xtra.display = XOpenDisplay(NULL))) return -1;
|
||||||
|
|
||||||
|
Xtra.terminal_window = xtra_focused_window_id();
|
||||||
|
|
||||||
{
|
{
|
||||||
/* Create an invisible window which will act as proxy for the DnD
|
/* Create an invisible window which will act as proxy for the DnD operation. */
|
||||||
* operation. This window will be used for both the GH and HG
|
|
||||||
* direction.
|
|
||||||
*/
|
|
||||||
XSetWindowAttributes attr = {0};
|
XSetWindowAttributes attr = {0};
|
||||||
attr.event_mask = EnterWindowMask |
|
attr.event_mask = EnterWindowMask |
|
||||||
LeaveWindowMask |
|
LeaveWindowMask |
|
||||||
@ -109,71 +268,88 @@ void xtra_init()
|
|||||||
/* Since we cannot capture resize events for parent window we will have to create
|
/* Since we cannot capture resize events for parent window we will have to create
|
||||||
* this window to have maximum size as defined in root window
|
* this window to have maximum size as defined in root window
|
||||||
*/
|
*/
|
||||||
XGetGeometry(the_d, XDefaultRootWindow(the_d), &root, &x, &y, &wht, &hht, &b, &d);
|
XGetGeometry(Xtra.display,
|
||||||
|
XDefaultRootWindow(Xtra.display),
|
||||||
|
&root, &x, &y, &wht, &hht, &b, &d);
|
||||||
|
|
||||||
m_wndProxy = XCreateWindow(the_d, self_window /* Parent */,
|
if (! (Xtra.proxy_window = XCreateWindow
|
||||||
|
(Xtra.display, Xtra.terminal_window, /* Parent */
|
||||||
0, 0, /* Position */
|
0, 0, /* Position */
|
||||||
wht, hht, /* Width + height */
|
wht, hht, /* Width + height */
|
||||||
0, /* Border width */
|
0, /* Border width */
|
||||||
CopyFromParent, /* Depth */
|
CopyFromParent, /* Depth */
|
||||||
InputOnly, /* Class */
|
InputOnly, /* Class */
|
||||||
CopyFromParent, /* Visual */
|
CopyFromParent, /* Visual */
|
||||||
CWDontPropagate | CWEventMask | CWCursor, /* Value mask */
|
CWEventMask | CWCursor, /* Value mask */
|
||||||
&attr /* Attributes for value mask */);
|
&attr)) ) /* Attributes for value mask */
|
||||||
if (!m_wndProxy)
|
return -1;
|
||||||
{
|
|
||||||
//TODO return status
|
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
XMapWindow(the_d, m_wndProxy);
|
XMapWindow(Xtra.display, Xtra.proxy_window); /* Show window (sandwich) */
|
||||||
XLowerWindow(the_d, m_wndProxy); /* Don't interfere with parent lmao */
|
XLowerWindow(Xtra.display, Xtra.proxy_window); /* Don't interfere with parent lmao */
|
||||||
|
|
||||||
self_window = m_wndProxy;
|
XdndAware = XInternAtom(Xtra.display, "XdndAware", False);
|
||||||
|
XdndEnter = XInternAtom(Xtra.display, "XdndEnter", False);
|
||||||
|
XdndLeave = XInternAtom(Xtra.display, "XdndLeave", False);
|
||||||
|
XdndPosition = XInternAtom(Xtra.display, "XdndPosition", False);
|
||||||
|
XdndStatus = XInternAtom(Xtra.display, "XdndStatus", False);
|
||||||
|
XdndDrop = XInternAtom(Xtra.display, "XdndDrop", False);
|
||||||
|
XdndSelection = XInternAtom(Xtra.display, "XdndSelection", False);
|
||||||
|
XdndDATA = XInternAtom(Xtra.display, "XdndDATA", False);
|
||||||
|
XdndTypeList = XInternAtom(Xtra.display, "XdndTypeList", False);
|
||||||
|
XdndActionCopy = XInternAtom(Xtra.display, "XdndActionCopy", False);
|
||||||
|
XdndFinished = XInternAtom(Xtra.display, "XdndFinished", False);
|
||||||
|
|
||||||
XdndAware = XInternAtom(the_d, "XdndAware", False);
|
/* Inform my nigga windows that we are aware of dnd */
|
||||||
XdndEnter = XInternAtom(the_d, "XdndEnter", False);
|
Atom XdndVersion = 3;
|
||||||
XdndLeave = XInternAtom(the_d, "XdndLeave", False);
|
XChangeProperty(Xtra.display,
|
||||||
XdndPosition = XInternAtom(the_d, "XdndPosition", False);
|
Xtra.proxy_window,
|
||||||
XdndStatus = XInternAtom(the_d, "XdndStatus", False);
|
XdndAware,
|
||||||
XdndDrop = XInternAtom(the_d, "XdndDrop", False);
|
XA_ATOM,
|
||||||
XdndSelection = XInternAtom(the_d, "XdndSelection", False);
|
32,
|
||||||
XdndDATA = XInternAtom(the_d, "XdndDATA", False);
|
PropModeReplace,
|
||||||
XdndActionCopy = XInternAtom(the_d, "XdndActionCopy", False);
|
(unsigned char*)&XdndVersion, 1);
|
||||||
XdndActionAsk = XInternAtom(the_d, "XdndActionAsk", False);
|
|
||||||
XdndTypeList = XInternAtom(the_d, "XdndTypeList", False);
|
|
||||||
|
|
||||||
Atom Xdndversion = 3;
|
|
||||||
XChangeProperty(the_d, m_wndProxy, XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&Xdndversion, 1);
|
|
||||||
|
|
||||||
pthread_t id;
|
pthread_t id;
|
||||||
pthread_create(&id, NULL, event_loop, NULL);
|
pthread_create(&id, NULL, event_loop, NULL);
|
||||||
pthread_detach(id);
|
pthread_detach(id);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void xtra_terminate()
|
void xtra_terminate()
|
||||||
{
|
{
|
||||||
XEvent terminate_event;
|
if (!Xtra.display) return;
|
||||||
terminate_event.xclient.display = the_d;
|
|
||||||
terminate_event.type = ClientMessage;
|
|
||||||
|
|
||||||
|
XEvent terminate = {
|
||||||
|
.xclient = {
|
||||||
|
.type = ClientMessage,
|
||||||
|
.display = Xtra.display,
|
||||||
|
.message_type = XtraTerminate,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
XDeleteProperty(the_d, self_window, XdndAware);
|
XLockDisplay(Xtra.display);
|
||||||
XSendEvent(the_d, self_window, 0, NoEventMask, &terminate_event);
|
XDeleteProperty(Xtra.display, Xtra.proxy_window, XdndAware);
|
||||||
|
XSendEvent(Xtra.display, Xtra.proxy_window, 0, NoEventMask, &terminate);
|
||||||
|
XUnlockDisplay(Xtra.display);
|
||||||
|
|
||||||
|
while (Xtra.display); /* Wait for termination */
|
||||||
}
|
}
|
||||||
|
|
||||||
long unsigned int xtra_focused_window_id()
|
long unsigned int xtra_focused_window_id()
|
||||||
{
|
{
|
||||||
if (!the_d) return 0;
|
if (!Xtra.display) return 0;
|
||||||
|
|
||||||
Window focus;
|
Window focus;
|
||||||
int revert;
|
int revert;
|
||||||
XGetInputFocus(the_d, &focus, &revert);
|
XLockDisplay(Xtra.display);
|
||||||
|
XGetInputFocus(Xtra.display, &focus, &revert);
|
||||||
|
XUnlockDisplay(Xtra.display);
|
||||||
return focus;
|
return focus;
|
||||||
}
|
}
|
||||||
|
|
||||||
int xtra_is_this_focused()
|
int xtra_is_this_focused()
|
||||||
{
|
{
|
||||||
return self_window == xtra_focused_window_id();
|
return Xtra.proxy_window == xtra_focused_window_id();
|
||||||
}
|
}
|
||||||
|
13
src/xtra.h
13
src/xtra.h
@ -3,10 +3,17 @@
|
|||||||
|
|
||||||
/* NOTE: If no xlib present don't compile */
|
/* NOTE: If no xlib present don't compile */
|
||||||
|
|
||||||
void xtra_init();
|
typedef enum {
|
||||||
void xtra_terminate();
|
DT_plain,
|
||||||
|
DT_file_list
|
||||||
|
}
|
||||||
|
DropType;
|
||||||
|
|
||||||
|
typedef void (*drop_callback) (const char*, DropType);
|
||||||
|
|
||||||
|
int xtra_init(drop_callback d);
|
||||||
|
void xtra_terminate();
|
||||||
long unsigned int xtra_focused_window_id();
|
long unsigned int xtra_focused_window_id();
|
||||||
int xtra_is_this_focused(); /* returns bool */
|
int xtra_is_this_focused(); /* returns bool */
|
||||||
|
|
||||||
#endif /* DND_H */
|
#endif /* XTRA_H */
|
Loading…
Reference in New Issue
Block a user