xdnd drop support on dock.
[wmaker-crm.git] / src / xdnd.c
blob8c863e6e244448b029efebbc69ee46bc3c5e3827
1 /* Many part of code are ripped of an example from JX's site */
3 #include "wconfig.h"
5 #ifdef XDND
7 #include <X11/Xlib.h>
8 #include "WindowMaker.h"
9 #include "window.h"
10 #include "dock.h"
11 #include "xdnd.h"
12 #include "motif.h"
14 #include "workspace.h"
16 #include "stdlib.h"
18 #include <X11/Xatom.h>
21 #define XDND_VERSION 3L
23 Atom _XA_XdndAware;
24 Atom _XA_XdndEnter;
25 Atom _XA_XdndLeave;
26 Atom _XA_XdndDrop;
27 Atom _XA_XdndPosition;
28 Atom _XA_XdndStatus;
29 Atom _XA_XdndActionCopy;
30 Atom _XA_XdndSelection;
31 Atom _XA_XdndFinished;
32 Atom _XA_WINDOWMAKER_XDNDEXCHANGE;
35 Atom _XA_MOTIF_DRAG_RECEIVER_INFO;
36 Atom _XA_MOTIF_DRAG_AND_DROP_MESSAGE;
39 Atom atom_support;
41 void wXDNDInitializeAtoms()
44 _XA_XdndAware = XInternAtom(dpy, "XdndAware", False);
45 _XA_XdndEnter = XInternAtom(dpy, "XdndEnter", False);
46 _XA_XdndLeave = XInternAtom(dpy, "XdndLeave", False);
47 _XA_XdndDrop = XInternAtom(dpy, "XdndDrop", False);
48 _XA_XdndPosition = XInternAtom(dpy, "XdndPosition", False);
49 _XA_XdndStatus = XInternAtom(dpy, "XdndStatus", False);
50 _XA_XdndActionCopy = XInternAtom(dpy, "XdndActionCopy", False);
51 _XA_XdndSelection = XInternAtom(dpy, "XdndSelection", False);
52 _XA_XdndFinished = XInternAtom(dpy, "XdndFinished", False);
54 _XA_WINDOWMAKER_XDNDEXCHANGE = XInternAtom(dpy, "_WINDOWMAKER_XDNDEXCHANGE", False);
57 _XA_MOTIF_DRAG_RECEIVER_INFO = XInternAtom(dpy, "_MOTIF_DRAG_RECEIVER_INFO",False);
58 _XA_MOTIF_DRAG_AND_DROP_MESSAGE = XInternAtom(dpy, "_MOTIF_DRAG_AND_DROP_MESSAGE", False);
62 void wXDNDMakeAwareness(Window window) {
63 long int xdnd_version = 3;
65 MotifDragReceiverInfo info;
67 XChangeProperty (dpy, window, _XA_XdndAware, XA_ATOM,
68 32, PropModeAppend, (char *)&xdnd_version, 1);
70 /*** MOTIF ***
71 info.byte_order = '\0';
72 info.protocol_version = 0;
73 info.protocol_style = XmDRAG_DYNAMIC;
74 info.proxy_window = 0;
75 info.num_drop_sites = 0;
76 info.total_size = sizeof(info);
78 XChangeProperty (dpy, window,
79 _XA_MOTIF_DRAG_RECEIVER_INFO,
80 _XA_MOTIF_DRAG_RECEIVER_INFO,
81 8, PropModeReplace,
82 (unsigned char *)&info,
83 sizeof (info));
87 void wXDNDClearAwareness(Window window) {
88 long int xdnd_version = 3;
89 XDeleteProperty (dpy, window, _XA_XdndAware);
92 Bool
93 wXDNDProcessSelection(XSelectionEvent *event)
95 WScreen *scr = wScreenForWindow(event->requestor);
96 char *dropdata;
97 Atom ret_type;
98 int ret_format;
99 unsigned long ret_item;
100 unsigned long remain_byte;
101 char * delme;
102 Window selowner = XGetSelectionOwner(dpy,_XA_XdndSelection);
103 XGetWindowProperty(dpy, event->requestor, _XA_WINDOWMAKER_XDNDEXCHANGE,
104 0, 65536, True, atom_support, &ret_type, &ret_format,
105 &ret_item, &remain_byte, (unsigned char **)&delme);
106 if (delme){
108 printf("get -%s-\n",delme);
110 scr->xdestring=delme;
114 /*finished*/
115 XEvent xevent;
116 memset (&xevent, 0, sizeof(xevent));
117 xevent.xany.type = ClientMessage;
118 xevent.xany.display = dpy;
119 xevent.xclient.window = selowner;
120 xevent.xclient.message_type = _XA_XdndFinished;
121 xevent.xclient.format = 32;
123 XDND_FINISHED_TARGET_WIN(&xevent) = event->requestor;
124 XSendEvent(dpy, selowner, 0, 0, &xevent);
128 Bool
129 isAwareXDND(Window window)
131 Atom actual;
132 int format;
133 unsigned long count, remaining;
134 unsigned char *data=0;
135 Atom *types, *t;
137 if (!window) return False;
138 XGetWindowProperty (dpy, window, _XA_XdndAware,
139 0, 0x8000000L, False, XA_ATOM,
140 &actual, &format,
141 &count, &remaining, &data);
142 if (actual != XA_ATOM || format != 32 || count == 0 || !data) {
143 if (data)
144 XFree (data);
145 return False;
147 if (data)
148 XFree (data);
149 return True;
152 Bool
153 acceptXDND(Window window)
155 WScreen *scr = wScreenForWindow(window);
156 WWindow *wwin = wWindowFor(window);
157 WDock *dock;
158 int icon_pos,i;
160 icon_pos = -1;
161 if ((dock = scr->dock)!=NULL) {
162 for (i=0; i<dock->max_icons; i++) {
163 if (dock->icon_array[i]
164 && dock->icon_array[i]->icon->core->window==window) {
165 icon_pos = i;
166 break;
170 if (icon_pos<0 && (dock = scr->workspaces[scr->current_workspace]->clip)!=NULL) {
171 for (i=0; i<dock->max_icons; i++) {
172 if (dock->icon_array[i]
173 && dock->icon_array[i]->icon->core->window==window) {
174 icon_pos = i;
175 break;
179 if (icon_pos<0) return False;
180 if (!dock) return False;
181 if (isAwareXDND(dock->icon_array[icon_pos]->icon->icon_win)) return False;
183 if (dock->icon_array[icon_pos]->dnd_command!=NULL) return True;
185 return False;
188 Bool
189 wXDNDProcessClientMessage(XClientMessageEvent *event)
191 /* test */
193 char * name = XGetAtomName(dpy, event->message_type);
195 printf("Get %s\n",name);
197 XFree(name);
201 if (event->message_type == _XA_MOTIF_DRAG_AND_DROP_MESSAGE) {
202 printf("motif dnd msg %d\n",event->data.b[0]);
203 if (event->data.b[0] == XmDROP_START){
204 unsigned x_root, y_root, flags;
205 unsigned char reason;
206 unsigned long timestamp;
207 Atom atom;
208 Window source_window;
209 MotifDragInitiatorInfo *initiator_info;
210 Atom ret_type;
211 int ret_format;
212 unsigned long ret_item;
213 unsigned long remain_byte;
215 reason = event->data.b[0];
216 flags = event->data.s[1];
217 timestamp = event->data.l[1];
218 x_root = event->data.s[4];
219 y_root = event->data.s[5];
220 atom = event->data.l[3];
221 source_window = event->data.l[4];
223 XGetWindowProperty(dpy, source_window, atom,
224 0, sizeof(*initiator_info), True, atom_support,
225 &ret_type, &ret_format,
226 &ret_item, &remain_byte, (unsigned char **)&initiator_info);
229 else */
230 if (event->message_type == _XA_XdndEnter) {
231 if ((event->data.l[1] & 1) == 0){
232 atom_support = event->data.l[2];
235 else puts("enter more than 3 types");
237 return True;
238 } else if (event->message_type == _XA_XdndLeave) {
239 return True;
240 } else if (event->message_type == _XA_XdndDrop) {
241 if (event->data.l[0] == XGetSelectionOwner(dpy, _XA_XdndSelection)){
242 XConvertSelection(dpy, _XA_XdndSelection, atom_support,
243 _XA_WINDOWMAKER_XDNDEXCHANGE, event->window, CurrentTime);
246 else puts("wierd selection owner");
248 return True;
249 } else if (event->message_type == _XA_XdndPosition) {
250 XEvent xevent;
251 Window srcwin = event->data.l[0];
252 if (atom_support != XInternAtom(dpy, "text/uri-list", False)) {
253 return True;
256 memset (&xevent, 0, sizeof(xevent));
257 xevent.xany.type = ClientMessage;
258 xevent.xany.display = dpy;
259 xevent.xclient.window = srcwin;
260 xevent.xclient.message_type = _XA_XdndStatus;
261 xevent.xclient.format = 32;
263 XDND_STATUS_TARGET_WIN (&xevent) = event->window;
264 XDND_STATUS_WILL_ACCEPT_SET (&xevent, acceptXDND(event->window));
265 XDND_STATUS_WANT_POSITION_SET(&xevent, True);
266 XDND_STATUS_RECT_SET(&xevent, 0, 0, 1024,768);
267 XDND_STATUS_ACTION(&xevent) = _XA_XdndActionCopy;
269 XSendEvent(dpy, srcwin, 0, 0, &xevent);
271 return True;
273 return False;
276 #endif