1 /* Many part of code are ripped of an example from JX's site */
8 #include "WindowMaker.h"
14 #include "workspace.h"
19 #include <X11/Xatom.h>
22 #define XDND_VERSION 3L
28 Atom _XA_XdndPosition
;
30 Atom _XA_XdndActionCopy
;
31 Atom _XA_XdndSelection
;
32 Atom _XA_XdndFinished
;
33 Atom _XA_WINDOWMAKER_XDNDEXCHANGE
;
36 Atom _XA_MOTIF_DRAG_RECEIVER_INFO;
37 Atom _XA_MOTIF_DRAG_AND_DROP_MESSAGE;
42 void wXDNDInitializeAtoms()
45 _XA_XdndAware
= XInternAtom(dpy
, "XdndAware", False
);
46 _XA_XdndEnter
= XInternAtom(dpy
, "XdndEnter", False
);
47 _XA_XdndLeave
= XInternAtom(dpy
, "XdndLeave", False
);
48 _XA_XdndDrop
= XInternAtom(dpy
, "XdndDrop", False
);
49 _XA_XdndPosition
= XInternAtom(dpy
, "XdndPosition", False
);
50 _XA_XdndStatus
= XInternAtom(dpy
, "XdndStatus", False
);
51 _XA_XdndActionCopy
= XInternAtom(dpy
, "XdndActionCopy", False
);
52 _XA_XdndSelection
= XInternAtom(dpy
, "XdndSelection", False
);
53 _XA_XdndFinished
= XInternAtom(dpy
, "XdndFinished", False
);
55 _XA_WINDOWMAKER_XDNDEXCHANGE
= XInternAtom(dpy
, "_WINDOWMAKER_XDNDEXCHANGE", False
);
58 _XA_MOTIF_DRAG_RECEIVER_INFO = XInternAtom(dpy, "_MOTIF_DRAG_RECEIVER_INFO",False);
59 _XA_MOTIF_DRAG_AND_DROP_MESSAGE = XInternAtom(dpy, "_MOTIF_DRAG_AND_DROP_MESSAGE", False);
63 void wXDNDMakeAwareness(Window window
) {
64 long int xdnd_version
= 3;
66 MotifDragReceiverInfo info;
68 XChangeProperty (dpy
, window
, _XA_XdndAware
, XA_ATOM
,
69 32, PropModeAppend
, (char *)&xdnd_version
, 1);
72 info.byte_order = '\0';
73 info.protocol_version = 0;
74 info.protocol_style = XmDRAG_DYNAMIC;
75 info.proxy_window = 0;
76 info.num_drop_sites = 0;
77 info.total_size = sizeof(info);
79 XChangeProperty (dpy, window,
80 _XA_MOTIF_DRAG_RECEIVER_INFO,
81 _XA_MOTIF_DRAG_RECEIVER_INFO,
83 (unsigned char *)&info,
88 void wXDNDClearAwareness(Window window
) {
89 long int xdnd_version
= 3;
90 XDeleteProperty (dpy
, window
, _XA_XdndAware
);
94 wXDNDProcessSelection(XEvent
*event
)
96 WScreen
*scr
= wScreenForWindow(event
->xselection
.requestor
);
100 unsigned long ret_item
;
101 unsigned long remain_byte
;
104 Window selowner
= XGetSelectionOwner(dpy
,_XA_XdndSelection
);
108 XGetWindowProperty(dpy
, event
->xselection
.requestor
,
109 _XA_WINDOWMAKER_XDNDEXCHANGE
,
110 0, 65536, True
, atom_support
, &ret_type
, &ret_format
,
111 &ret_item
, &remain_byte
, (unsigned char **)&delme
);
113 scr
->xdestring
=delme
;
117 memset (&xevent
, 0, sizeof(xevent
));
118 xevent
.xany
.type
= ClientMessage
;
119 xevent
.xany
.display
= dpy
;
120 xevent
.xclient
.window
= selowner
;
121 xevent
.xclient
.message_type
= _XA_XdndFinished
;
122 xevent
.xclient
.format
= 32;
123 XDND_FINISHED_TARGET_WIN(&xevent
) = event
->xselection
.requestor
;
124 XSendEvent(dpy
, selowner
, 0, 0, &xevent
);
127 if (scr
->xdestring
) {
128 WMArrayIterator iter
;
129 int length
, str_size
;
133 items
= WMCreateArray(4);
134 retain
= wstrdup(scr
->xdestring
);
135 XFree(scr
->xdestring
); /* since xdestring was created by Xlib */
137 length
= strlen(retain
);
139 /* search in string */
141 if (retain
[length
] == '\r') { /* useless char, nuke it */
144 if (retain
[length
] == '\n') {
145 str_size
= strlen(&retain
[length
+ 1]);
147 WMAddToArray(items
, wstrdup(&retain
[length
+ 1]));
148 total_size
+= str_size
+ 3; /* reserve for " \"\"" */
149 /* this is nonsense -- if (length)
150 WMAppendArray(items, WMCreateArray(1));*/
156 WMAddToArray(items
, wstrdup(retain
));
157 total_size
+= strlen(retain
) + 3;
160 /* now pack new string */
161 scr
->xdestring
= wmalloc(total_size
);
162 scr
->xdestring
[0]=0; /* empty string */
163 WM_ETARETI_ARRAY(items
, tmp
, iter
) {
164 if (!strncmp(tmp
,"file:",5)) {
165 /* add more 2 chars while removing 5 is harmless */
166 strcat(scr
->xdestring
, " \"");
167 strcat(scr
->xdestring
, &tmp
[5]);
168 strcat(scr
->xdestring
, "\"");
170 /* unsupport object, still need more " ? tell ]d */
171 strcat(scr
->xdestring
, &tmp
[5]);
176 wDockReceiveDNDDrop(scr
,event
);
179 puts(scr->xdestring);
181 wfree(scr
->xdestring
); /* this xdestring is not from Xlib (no XFree) */
184 /* why doesn't this function return anything ? -Dan */
188 isAwareXDND(Window window
)
192 unsigned long count
, remaining
;
193 unsigned char *data
=0;
195 if (!window
) return False
;
196 XGetWindowProperty (dpy
, window
, _XA_XdndAware
,
197 0, 0x8000000L
, False
, XA_ATOM
,
199 &count
, &remaining
, &data
);
200 if (actual
!= XA_ATOM
|| format
!= 32 || count
== 0 || !data
) {
211 acceptXDND(Window window
)
213 WScreen
*scr
= wScreenForWindow(window
);
218 if ((dock
= scr
->dock
)!=NULL
) {
219 for (i
=0; i
<dock
->max_icons
; i
++) {
220 if (dock
->icon_array
[i
]
221 && dock
->icon_array
[i
]->icon
->core
->window
==window
) {
227 if (icon_pos
<0 && (dock
= scr
->workspaces
[scr
->current_workspace
]->clip
)!=NULL
) {
228 for (i
=0; i
<dock
->max_icons
; i
++) {
229 if (dock
->icon_array
[i
]
230 && dock
->icon_array
[i
]->icon
->core
->window
==window
) {
236 if (icon_pos
<0) return False
;
237 if (!dock
) return False
;
238 if (isAwareXDND(dock
->icon_array
[icon_pos
]->icon
->icon_win
)) return False
;
240 if (dock
->icon_array
[icon_pos
]->dnd_command
!=NULL
) return True
;
246 wXDNDProcessClientMessage(XClientMessageEvent
*event
)
250 char * name
= XGetAtomName(dpy
, event
->message_type
);
252 printf("Get %s\n",name);
258 if (event->message_type == _XA_MOTIF_DRAG_AND_DROP_MESSAGE) {
259 printf("motif dnd msg %d\n",event->data.b[0]);
260 if (event->data.b[0] == XmDROP_START){
261 unsigned x_root, y_root, flags;
262 unsigned char reason;
263 unsigned long timestamp;
265 Window source_window;
266 MotifDragInitiatorInfo *initiator_info;
269 unsigned long ret_item;
270 unsigned long remain_byte;
272 reason = event->data.b[0];
273 flags = event->data.s[1];
274 timestamp = event->data.l[1];
275 x_root = event->data.s[4];
276 y_root = event->data.s[5];
277 atom = event->data.l[3];
278 source_window = event->data.l[4];
280 XGetWindowProperty(dpy, source_window, atom,
281 0, sizeof(*initiator_info), True, atom_support,
282 &ret_type, &ret_format,
283 &ret_item, &remain_byte, (unsigned char **)&initiator_info);
287 if (event
->message_type
== _XA_XdndEnter
) {
288 if ((event
->data
.l
[1] & 1) == 0){
289 atom_support
= event
->data
.l
[2];
292 else puts("enter more than 3 types");
295 } else if (event
->message_type
== _XA_XdndLeave
) {
297 } else if (event
->message_type
== _XA_XdndDrop
) {
298 if (event
->data
.l
[0] == XGetSelectionOwner(dpy
, _XA_XdndSelection
)){
299 XConvertSelection(dpy
, _XA_XdndSelection
, atom_support
,
300 _XA_WINDOWMAKER_XDNDEXCHANGE
, event
->window
, CurrentTime
);
303 puts("wierd selection owner? QT?");
304 XConvertSelection(dpy
, _XA_XdndSelection
, atom_support
,
305 _XA_WINDOWMAKER_XDNDEXCHANGE
, event
->window
, CurrentTime
);
308 } else if (event
->message_type
== _XA_XdndPosition
) {
310 Window srcwin
= event
->data
.l
[0];
311 if (atom_support
!= XInternAtom(dpy
, "text/uri-list", False
)) {
315 memset (&xevent
, 0, sizeof(xevent
));
316 xevent
.xany
.type
= ClientMessage
;
317 xevent
.xany
.display
= dpy
;
318 xevent
.xclient
.window
= srcwin
;
319 xevent
.xclient
.message_type
= _XA_XdndStatus
;
320 xevent
.xclient
.format
= 32;
322 XDND_STATUS_TARGET_WIN (&xevent
) = event
->window
;
323 XDND_STATUS_WILL_ACCEPT_SET (&xevent
, acceptXDND(event
->window
));
324 XDND_STATUS_WANT_POSITION_SET(&xevent
, True
);
325 XDND_STATUS_RECT_SET(&xevent
, 0, 0, 1024,768);
326 XDND_STATUS_ACTION(&xevent
) = _XA_XdndActionCopy
;
328 XSendEvent(dpy
, srcwin
, 0, 0, &xevent
);