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>
21 #define XDND_VERSION 3L
27 Atom _XA_XdndPosition
;
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;
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
)
64 long int xdnd_version
= 3;
66 MotifDragReceiverInfo info;
68 XChangeProperty(dpy
, window
, _XA_XdndAware
, XA_ATOM
, 32, PropModeAppend
, (char *)&xdnd_version
, 1);
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,
82 (unsigned char *)&info,
87 Bool
wXDNDProcessSelection(XEvent
* event
)
89 WScreen
*scr
= wScreenForWindow(event
->xselection
.requestor
);
93 unsigned long ret_item
;
94 unsigned long remain_byte
;
97 Window selowner
= XGetSelectionOwner(dpy
, _XA_XdndSelection
);
100 XGetWindowProperty(dpy
, event
->xselection
.requestor
,
101 _XA_WINDOWMAKER_XDNDEXCHANGE
,
102 0, 65536, True
, atom_support
, &ret_type
, &ret_format
,
103 &ret_item
, &remain_byte
, (unsigned char **)&delme
);
105 scr
->xdestring
= delme
;
109 memset(&xevent
, 0, sizeof(xevent
));
110 xevent
.xany
.type
= ClientMessage
;
111 xevent
.xany
.display
= dpy
;
112 xevent
.xclient
.window
= selowner
;
113 xevent
.xclient
.message_type
= _XA_XdndFinished
;
114 xevent
.xclient
.format
= 32;
115 XDND_FINISHED_TARGET_WIN(&xevent
) = event
->xselection
.requestor
;
116 XSendEvent(dpy
, selowner
, 0, 0, &xevent
);
118 /*process dropping */
119 if (scr
->xdestring
) {
120 WMArrayIterator iter
;
121 int length
, str_size
;
125 items
= WMCreateArray(4);
126 retain
= wstrdup(scr
->xdestring
);
127 XFree(scr
->xdestring
); /* since xdestring was created by Xlib */
129 length
= strlen(retain
);
131 /* search in string */
133 if (retain
[length
] == '\r') { /* useless char, nuke it */
136 if (retain
[length
] == '\n') {
137 str_size
= strlen(&retain
[length
+ 1]);
139 WMAddToArray(items
, wstrdup(&retain
[length
+ 1]));
140 total_size
+= str_size
+ 3; /* reserve for " \"\"" */
141 /* this is nonsense -- if (length)
142 WMAppendArray(items, WMCreateArray(1)); */
148 WMAddToArray(items
, wstrdup(retain
));
149 total_size
+= strlen(retain
) + 3;
152 /* now pack new string */
153 scr
->xdestring
= wmalloc(total_size
);
154 scr
->xdestring
[0] = 0; /* empty string */
155 WM_ETARETI_ARRAY(items
, tmp
, iter
) {
156 if (!strncmp(tmp
, "file:", 5)) {
157 /* add more 2 chars while removing 5 is harmless */
158 strcat(scr
->xdestring
, " \"");
159 strcat(scr
->xdestring
, &tmp
[5]);
160 strcat(scr
->xdestring
, "\"");
162 /* unsupport object, still need more " ? tell ]d */
163 strcat(scr
->xdestring
, &tmp
[5]);
168 wDockReceiveDNDDrop(scr
, event
);
171 puts(scr->xdestring);
173 wfree(scr
->xdestring
); /* this xdestring is not from Xlib (no XFree) */
179 Bool
isAwareXDND(Window window
)
183 unsigned long count
, remaining
;
184 unsigned char *data
= 0;
188 XGetWindowProperty(dpy
, window
, _XA_XdndAware
,
189 0, 0x8000000L
, False
, XA_ATOM
, &actual
, &format
, &count
, &remaining
, &data
);
190 if (actual
!= XA_ATOM
|| format
!= 32 || count
== 0 || !data
) {
200 Bool
acceptXDND(Window window
)
202 WScreen
*scr
= wScreenForWindow(window
);
207 if ((dock
= scr
->dock
) != NULL
) {
208 for (i
= 0; i
< dock
->max_icons
; i
++) {
209 if (dock
->icon_array
[i
]
210 && dock
->icon_array
[i
]->icon
->core
->window
== window
) {
216 if (icon_pos
< 0 && (dock
= scr
->workspaces
[scr
->current_workspace
]->clip
) != NULL
) {
217 for (i
= 0; i
< dock
->max_icons
; i
++) {
218 if (dock
->icon_array
[i
]
219 && dock
->icon_array
[i
]->icon
->core
->window
== window
) {
229 if (isAwareXDND(dock
->icon_array
[icon_pos
]->icon
->icon_win
))
232 if (dock
->icon_array
[icon_pos
]->dnd_command
!= NULL
)
238 Bool
wXDNDProcessClientMessage(XClientMessageEvent
* event
)
242 char *name
= XGetAtomName(dpy
, event
->message_type
);
244 printf("Get %s\n",name);
250 if (event->message_type == _XA_MOTIF_DRAG_AND_DROP_MESSAGE) {
251 printf("motif dnd msg %d\n",event->data.b[0]);
252 if (event->data.b[0] == XmDROP_START){
253 unsigned x_root, y_root, flags;
254 unsigned char reason;
255 unsigned long timestamp;
257 Window source_window;
258 MotifDragInitiatorInfo *initiator_info;
261 unsigned long ret_item;
262 unsigned long remain_byte;
264 reason = event->data.b[0];
265 flags = event->data.s[1];
266 timestamp = event->data.l[1];
267 x_root = event->data.s[4];
268 y_root = event->data.s[5];
269 atom = event->data.l[3];
270 source_window = event->data.l[4];
272 XGetWindowProperty(dpy, source_window, atom,
273 0, sizeof(*initiator_info), True, atom_support,
274 &ret_type, &ret_format,
275 &ret_item, &remain_byte, (unsigned char **)&initiator_info);
279 if (event
->message_type
== _XA_XdndEnter
) {
280 if ((event
->data
.l
[1] & 1) == 0) {
281 atom_support
= event
->data
.l
[2];
284 else puts("enter more than 3 types");
287 } else if (event
->message_type
== _XA_XdndLeave
) {
289 } else if (event
->message_type
== _XA_XdndDrop
) {
290 if (event
->data
.l
[0] == XGetSelectionOwner(dpy
, _XA_XdndSelection
)) {
291 XConvertSelection(dpy
, _XA_XdndSelection
, atom_support
,
292 _XA_WINDOWMAKER_XDNDEXCHANGE
, event
->window
, CurrentTime
);
294 puts("wierd selection owner? QT?");
295 XConvertSelection(dpy
, _XA_XdndSelection
, atom_support
,
296 _XA_WINDOWMAKER_XDNDEXCHANGE
, event
->window
, CurrentTime
);
299 } else if (event
->message_type
== _XA_XdndPosition
) {
301 Window srcwin
= event
->data
.l
[0];
302 if (atom_support
!= XInternAtom(dpy
, "text/uri-list", False
)) {
306 memset(&xevent
, 0, sizeof(xevent
));
307 xevent
.xany
.type
= ClientMessage
;
308 xevent
.xany
.display
= dpy
;
309 xevent
.xclient
.window
= srcwin
;
310 xevent
.xclient
.message_type
= _XA_XdndStatus
;
311 xevent
.xclient
.format
= 32;
313 XDND_STATUS_TARGET_WIN(&xevent
) = event
->window
;
314 XDND_STATUS_WILL_ACCEPT_SET(&xevent
, acceptXDND(event
->window
));
315 XDND_STATUS_WANT_POSITION_SET(&xevent
, True
);
316 XDND_STATUS_RECT_SET(&xevent
, 0, 0, 1024, 768);
317 XDND_STATUS_ACTION(&xevent
) = _XA_XdndActionCopy
;
319 XSendEvent(dpy
, srcwin
, 0, 0, &xevent
);