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 void wXDNDClearAwareness(Window window
)
89 XDeleteProperty(dpy
, window
, _XA_XdndAware
);
92 Bool
wXDNDProcessSelection(XEvent
* event
)
94 WScreen
*scr
= wScreenForWindow(event
->xselection
.requestor
);
98 unsigned long ret_item
;
99 unsigned long remain_byte
;
102 Window selowner
= XGetSelectionOwner(dpy
, _XA_XdndSelection
);
105 XGetWindowProperty(dpy
, event
->xselection
.requestor
,
106 _XA_WINDOWMAKER_XDNDEXCHANGE
,
107 0, 65536, True
, atom_support
, &ret_type
, &ret_format
,
108 &ret_item
, &remain_byte
, (unsigned char **)&delme
);
110 scr
->xdestring
= delme
;
114 memset(&xevent
, 0, sizeof(xevent
));
115 xevent
.xany
.type
= ClientMessage
;
116 xevent
.xany
.display
= dpy
;
117 xevent
.xclient
.window
= selowner
;
118 xevent
.xclient
.message_type
= _XA_XdndFinished
;
119 xevent
.xclient
.format
= 32;
120 XDND_FINISHED_TARGET_WIN(&xevent
) = event
->xselection
.requestor
;
121 XSendEvent(dpy
, selowner
, 0, 0, &xevent
);
123 /*process dropping */
124 if (scr
->xdestring
) {
125 WMArrayIterator iter
;
126 int length
, str_size
;
130 items
= WMCreateArray(4);
131 retain
= wstrdup(scr
->xdestring
);
132 XFree(scr
->xdestring
); /* since xdestring was created by Xlib */
134 length
= strlen(retain
);
136 /* search in string */
138 if (retain
[length
] == '\r') { /* useless char, nuke it */
141 if (retain
[length
] == '\n') {
142 str_size
= strlen(&retain
[length
+ 1]);
144 WMAddToArray(items
, wstrdup(&retain
[length
+ 1]));
145 total_size
+= str_size
+ 3; /* reserve for " \"\"" */
146 /* this is nonsense -- if (length)
147 WMAppendArray(items, WMCreateArray(1)); */
153 WMAddToArray(items
, wstrdup(retain
));
154 total_size
+= strlen(retain
) + 3;
157 /* now pack new string */
158 scr
->xdestring
= wmalloc(total_size
);
159 scr
->xdestring
[0] = 0; /* empty string */
160 WM_ETARETI_ARRAY(items
, tmp
, iter
) {
161 if (!strncmp(tmp
, "file:", 5)) {
162 /* add more 2 chars while removing 5 is harmless */
163 strcat(scr
->xdestring
, " \"");
164 strcat(scr
->xdestring
, &tmp
[5]);
165 strcat(scr
->xdestring
, "\"");
167 /* unsupport object, still need more " ? tell ]d */
168 strcat(scr
->xdestring
, &tmp
[5]);
173 wDockReceiveDNDDrop(scr
, event
);
176 puts(scr->xdestring);
178 wfree(scr
->xdestring
); /* this xdestring is not from Xlib (no XFree) */
184 Bool
isAwareXDND(Window window
)
188 unsigned long count
, remaining
;
189 unsigned char *data
= 0;
193 XGetWindowProperty(dpy
, window
, _XA_XdndAware
,
194 0, 0x8000000L
, False
, XA_ATOM
, &actual
, &format
, &count
, &remaining
, &data
);
195 if (actual
!= XA_ATOM
|| format
!= 32 || count
== 0 || !data
) {
205 Bool
acceptXDND(Window window
)
207 WScreen
*scr
= wScreenForWindow(window
);
212 if ((dock
= scr
->dock
) != NULL
) {
213 for (i
= 0; i
< dock
->max_icons
; i
++) {
214 if (dock
->icon_array
[i
]
215 && dock
->icon_array
[i
]->icon
->core
->window
== window
) {
221 if (icon_pos
< 0 && (dock
= scr
->workspaces
[scr
->current_workspace
]->clip
) != NULL
) {
222 for (i
= 0; i
< dock
->max_icons
; i
++) {
223 if (dock
->icon_array
[i
]
224 && dock
->icon_array
[i
]->icon
->core
->window
== window
) {
234 if (isAwareXDND(dock
->icon_array
[icon_pos
]->icon
->icon_win
))
237 if (dock
->icon_array
[icon_pos
]->dnd_command
!= NULL
)
243 Bool
wXDNDProcessClientMessage(XClientMessageEvent
* event
)
247 char *name
= XGetAtomName(dpy
, event
->message_type
);
249 printf("Get %s\n",name);
255 if (event->message_type == _XA_MOTIF_DRAG_AND_DROP_MESSAGE) {
256 printf("motif dnd msg %d\n",event->data.b[0]);
257 if (event->data.b[0] == XmDROP_START){
258 unsigned x_root, y_root, flags;
259 unsigned char reason;
260 unsigned long timestamp;
262 Window source_window;
263 MotifDragInitiatorInfo *initiator_info;
266 unsigned long ret_item;
267 unsigned long remain_byte;
269 reason = event->data.b[0];
270 flags = event->data.s[1];
271 timestamp = event->data.l[1];
272 x_root = event->data.s[4];
273 y_root = event->data.s[5];
274 atom = event->data.l[3];
275 source_window = event->data.l[4];
277 XGetWindowProperty(dpy, source_window, atom,
278 0, sizeof(*initiator_info), True, atom_support,
279 &ret_type, &ret_format,
280 &ret_item, &remain_byte, (unsigned char **)&initiator_info);
284 if (event
->message_type
== _XA_XdndEnter
) {
285 if ((event
->data
.l
[1] & 1) == 0) {
286 atom_support
= event
->data
.l
[2];
289 else puts("enter more than 3 types");
292 } else if (event
->message_type
== _XA_XdndLeave
) {
294 } else if (event
->message_type
== _XA_XdndDrop
) {
295 if (event
->data
.l
[0] == XGetSelectionOwner(dpy
, _XA_XdndSelection
)) {
296 XConvertSelection(dpy
, _XA_XdndSelection
, atom_support
,
297 _XA_WINDOWMAKER_XDNDEXCHANGE
, event
->window
, CurrentTime
);
299 puts("wierd selection owner? QT?");
300 XConvertSelection(dpy
, _XA_XdndSelection
, atom_support
,
301 _XA_WINDOWMAKER_XDNDEXCHANGE
, event
->window
, CurrentTime
);
304 } else if (event
->message_type
== _XA_XdndPosition
) {
306 Window srcwin
= event
->data
.l
[0];
307 if (atom_support
!= XInternAtom(dpy
, "text/uri-list", False
)) {
311 memset(&xevent
, 0, sizeof(xevent
));
312 xevent
.xany
.type
= ClientMessage
;
313 xevent
.xany
.display
= dpy
;
314 xevent
.xclient
.window
= srcwin
;
315 xevent
.xclient
.message_type
= _XA_XdndStatus
;
316 xevent
.xclient
.format
= 32;
318 XDND_STATUS_TARGET_WIN(&xevent
) = event
->window
;
319 XDND_STATUS_WILL_ACCEPT_SET(&xevent
, acceptXDND(event
->window
));
320 XDND_STATUS_WANT_POSITION_SET(&xevent
, True
);
321 XDND_STATUS_RECT_SET(&xevent
, 0, 0, 1024, 768);
322 XDND_STATUS_ACTION(&xevent
) = _XA_XdndActionCopy
;
324 XSendEvent(dpy
, srcwin
, 0, 0, &xevent
);