3 #endif /* ifdef HAVE_CONFIG_H */
9 #include "ecore_x_private.h"
11 #include "Ecore_X_Atoms.h"
13 EAPI int ECORE_X_EVENT_XDND_ENTER = 0;
14 EAPI int ECORE_X_EVENT_XDND_POSITION = 0;
15 EAPI int ECORE_X_EVENT_XDND_STATUS = 0;
16 EAPI int ECORE_X_EVENT_XDND_LEAVE = 0;
17 EAPI int ECORE_X_EVENT_XDND_DROP = 0;
18 EAPI int ECORE_X_EVENT_XDND_FINISHED = 0;
20 static Ecore_X_DND_Source *_source = NULL;
21 static Ecore_X_DND_Target *_target = NULL;
22 static int _ecore_x_dnd_init_count = 0;
24 typedef struct _Version_Cache_Item
29 static Version_Cache_Item *_version_cache = NULL;
30 static int _version_cache_num = 0, _version_cache_alloc = 0;
31 static void (*_posupdatecb)(void *,
32 Ecore_X_Xdnd_Position *);
33 static void *_posupdatedata;
36 _ecore_x_dnd_init(void)
38 if (!_ecore_x_dnd_init_count)
40 _source = calloc(1, sizeof(Ecore_X_DND_Source));
42 _source->version = ECORE_X_DND_VERSION;
45 _source->state = ECORE_X_DND_SOURCE_IDLE;
46 _source->prev.window = 0;
48 _target = calloc(1, sizeof(Ecore_X_DND_Target));
56 _target->source = None;
57 _target->state = ECORE_X_DND_TARGET_IDLE;
59 ECORE_X_EVENT_XDND_ENTER = ecore_event_type_new();
60 ECORE_X_EVENT_XDND_POSITION = ecore_event_type_new();
61 ECORE_X_EVENT_XDND_STATUS = ecore_event_type_new();
62 ECORE_X_EVENT_XDND_LEAVE = ecore_event_type_new();
63 ECORE_X_EVENT_XDND_DROP = ecore_event_type_new();
64 ECORE_X_EVENT_XDND_FINISHED = ecore_event_type_new();
67 _ecore_x_dnd_init_count++;
71 _ecore_x_dnd_shutdown(void)
73 _ecore_x_dnd_init_count--;
74 if (_ecore_x_dnd_init_count > 0)
87 _ecore_x_dnd_init_count = 0;
91 _ecore_x_dnd_converter_copy(char *target __UNUSED__,
96 Ecore_X_Atom *tprop __UNUSED__,
97 int *count __UNUSED__)
99 XTextProperty text_prop;
101 XICCEncodingStyle style = XTextStyle;
106 mystr = calloc(1, size + 1);
110 memcpy(mystr, data, size);
112 if (XmbTextListToTextProperty(_ecore_x_disp, &mystr, 1, style,
113 &text_prop) == Success)
115 int bufsize = strlen((char *)text_prop.value) + 1;
116 *data_ret = malloc(bufsize);
122 memcpy(*data_ret, text_prop.value, bufsize);
124 XFree(text_prop.value);
136 ecore_x_dnd_aware_set(Ecore_X_Window win,
139 Ecore_X_Atom prop_data = ECORE_X_DND_VERSION;
141 LOGFN(__FILE__, __LINE__, __FUNCTION__);
143 ecore_x_window_prop_property_set(win, ECORE_X_ATOM_XDND_AWARE,
144 XA_ATOM, 32, &prop_data, 1);
146 ecore_x_window_prop_property_del(win, ECORE_X_ATOM_XDND_AWARE);
150 ecore_x_dnd_version_get(Ecore_X_Window win)
152 unsigned char *prop_data;
154 Version_Cache_Item *t;
156 LOGFN(__FILE__, __LINE__, __FUNCTION__);
157 // this looks hacky - and it is, but we need a way of caching info about
158 // a window while dragging, because we literally query this every mouse
159 // move and going to and from x multiple times per move is EXPENSIVE
160 // and slows things down, puts lots of load on x etc.
161 if (_source->state == ECORE_X_DND_SOURCE_DRAGGING)
166 for (i = 0; i < _version_cache_num; i++)
168 if (_version_cache[i].win == win)
169 return _version_cache[i].ver;
173 if (ecore_x_window_prop_property_get(win, ECORE_X_ATOM_XDND_AWARE,
174 XA_ATOM, 32, &prop_data, &num))
176 int version = (int)*prop_data;
178 if (_source->state == ECORE_X_DND_SOURCE_DRAGGING)
180 _version_cache_num++;
181 if (_version_cache_num > _version_cache_alloc)
182 _version_cache_alloc += 16;
184 t = realloc(_version_cache,
185 _version_cache_alloc *
186 sizeof(Version_Cache_Item));
189 _version_cache[_version_cache_num - 1].win = win;
190 _version_cache[_version_cache_num - 1].ver = version;
196 if (_source->state == ECORE_X_DND_SOURCE_DRAGGING)
198 _version_cache_num++;
199 if (_version_cache_num > _version_cache_alloc)
200 _version_cache_alloc += 16;
202 t = realloc(_version_cache, _version_cache_alloc *
203 sizeof(Version_Cache_Item));
206 _version_cache[_version_cache_num - 1].win = win;
207 _version_cache[_version_cache_num - 1].ver = 0;
214 ecore_x_dnd_type_isset(Ecore_X_Window win,
217 int num, i, ret = EINA_FALSE;
219 Ecore_X_Atom *atoms, atom;
221 LOGFN(__FILE__, __LINE__, __FUNCTION__);
222 if (!ecore_x_window_prop_property_get(win, ECORE_X_ATOM_XDND_TYPE_LIST,
223 XA_ATOM, 32, &data, &num))
226 atom = ecore_x_atom_get(type);
227 atoms = (Ecore_X_Atom *)data;
229 for (i = 0; i < num; ++i)
231 if (atom == atoms[i])
243 ecore_x_dnd_type_set(Ecore_X_Window win,
248 Ecore_X_Atom *oldset = NULL, *newset = NULL;
249 int i, j = 0, num = 0;
250 unsigned char *data = NULL;
251 unsigned char *old_data = NULL;
253 LOGFN(__FILE__, __LINE__, __FUNCTION__);
254 atom = ecore_x_atom_get(type);
255 ecore_x_window_prop_property_get(win, ECORE_X_ATOM_XDND_TYPE_LIST,
256 XA_ATOM, 32, &old_data, &num);
257 oldset = (Ecore_X_Atom *)old_data;
259 LOGFN(__FILE__, __LINE__, __FUNCTION__);
262 if (ecore_x_dnd_type_isset(win, type))
268 newset = calloc(num + 1, sizeof(Ecore_X_Atom));
272 data = (unsigned char *)newset;
274 for (i = 0; i < num; i++)
275 newset[i + 1] = oldset[i];
276 /* prepend the new type */
279 ecore_x_window_prop_property_set(win, ECORE_X_ATOM_XDND_TYPE_LIST,
280 XA_ATOM, 32, data, num + 1);
284 if (!ecore_x_dnd_type_isset(win, type))
290 newset = calloc(num - 1, sizeof(Ecore_X_Atom));
297 data = (unsigned char *)newset;
298 for (i = 0; i < num; i++)
299 if (oldset[i] != atom)
300 newset[j++] = oldset[i];
302 ecore_x_window_prop_property_set(win, ECORE_X_ATOM_XDND_TYPE_LIST,
303 XA_ATOM, 32, data, num - 1);
311 ecore_x_dnd_types_set(Ecore_X_Window win,
313 unsigned int num_types)
315 Ecore_X_Atom *newset = NULL;
317 unsigned char *data = NULL;
319 LOGFN(__FILE__, __LINE__, __FUNCTION__);
321 ecore_x_window_prop_property_del(win, ECORE_X_ATOM_XDND_TYPE_LIST);
324 newset = calloc(num_types, sizeof(Ecore_X_Atom));
328 data = (unsigned char *)newset;
329 for (i = 0; i < num_types; i++)
331 newset[i] = ecore_x_atom_get(types[i]);
332 ecore_x_selection_converter_atom_add(newset[i],
333 _ecore_x_dnd_converter_copy);
335 ecore_x_window_prop_property_set(win, ECORE_X_ATOM_XDND_TYPE_LIST,
336 XA_ATOM, 32, data, num_types);
342 ecore_x_dnd_actions_set(Ecore_X_Window win,
343 Ecore_X_Atom *actions,
344 unsigned int num_actions)
347 unsigned char *data = NULL;
349 LOGFN(__FILE__, __LINE__, __FUNCTION__);
351 ecore_x_window_prop_property_del(win, ECORE_X_ATOM_XDND_ACTION_LIST);
354 data = (unsigned char *)actions;
355 for (i = 0; i < num_actions; i++)
357 ecore_x_selection_converter_atom_add(actions[i],
358 _ecore_x_dnd_converter_copy);
360 ecore_x_window_prop_property_set(win, ECORE_X_ATOM_XDND_ACTION_LIST,
361 XA_ATOM, 32, data, num_actions);
366 * The DND position update cb is called Ecore_X sends a DND position to a
369 * It essentially mirrors some of the data sent in the position message.
370 * Generally this cb should be set just before position update is called.
371 * Please note well you need to look after your own data pointer if someone
372 * trashes you position update cb set.
374 * It is considered good form to clear this when the dnd event finishes.
376 * @param cb Callback to updated each time ecore_x sends a position update.
377 * @param data User data.
380 ecore_x_dnd_callback_pos_update_set(
382 Ecore_X_Xdnd_Position *data),
386 _posupdatedata = (void *)data; /* Discard the const early */
390 _ecore_x_dnd_source_get(void)
396 _ecore_x_dnd_target_get(void)
402 ecore_x_dnd_begin(Ecore_X_Window source,
406 LOGFN(__FILE__, __LINE__, __FUNCTION__);
407 if (!ecore_x_dnd_version_get(source))
410 /* Take ownership of XdndSelection */
411 if (!ecore_x_selection_xdnd_set(source, data, size))
416 free(_version_cache);
417 _version_cache = NULL;
418 _version_cache_num = 0;
419 _version_cache_alloc = 0;
422 ecore_x_window_shadow_tree_flush();
424 _source->win = source;
425 ecore_x_window_ignore_set(_source->win, 1);
426 _source->state = ECORE_X_DND_SOURCE_DRAGGING;
427 _source->time = _ecore_x_event_last_time;
428 _source->prev.window = 0;
430 /* Default Accepted Action: move */
431 _source->action = ECORE_X_ATOM_XDND_ACTION_MOVE;
432 _source->accepted_action = None;
433 _source->dest = None;
439 ecore_x_dnd_drop(void)
442 int status = EINA_FALSE;
444 LOGFN(__FILE__, __LINE__, __FUNCTION__);
447 xev.xany.type = ClientMessage;
448 xev.xany.display = _ecore_x_disp;
449 xev.xclient.format = 32;
450 xev.xclient.window = _source->dest;
452 if (_source->will_accept)
454 xev.xclient.message_type = ECORE_X_ATOM_XDND_DROP;
455 xev.xclient.data.l[0] = _source->win;
456 xev.xclient.data.l[1] = 0;
457 xev.xclient.data.l[2] = _source->time;
458 XSendEvent(_ecore_x_disp, _source->dest, False, 0, &xev);
459 _source->state = ECORE_X_DND_SOURCE_DROPPED;
464 xev.xclient.message_type = ECORE_X_ATOM_XDND_LEAVE;
465 xev.xclient.data.l[0] = _source->win;
466 xev.xclient.data.l[1] = 0;
467 XSendEvent(_ecore_x_disp, _source->dest, False, 0, &xev);
468 _source->state = ECORE_X_DND_SOURCE_IDLE;
473 /* Dropping on nothing */
474 ecore_x_selection_xdnd_clear();
475 _source->state = ECORE_X_DND_SOURCE_IDLE;
478 ecore_x_window_ignore_set(_source->win, 0);
480 _source->prev.window = 0;
486 ecore_x_dnd_send_status(Eina_Bool will_accept,
488 Ecore_X_Rectangle rectangle,
493 if (_target->state == ECORE_X_DND_TARGET_IDLE)
496 LOGFN(__FILE__, __LINE__, __FUNCTION__);
497 memset(&xev, 0, sizeof(XEvent));
499 _target->will_accept = will_accept;
501 xev.xclient.type = ClientMessage;
502 xev.xclient.display = _ecore_x_disp;
503 xev.xclient.message_type = ECORE_X_ATOM_XDND_STATUS;
504 xev.xclient.format = 32;
505 xev.xclient.window = _target->source;
507 xev.xclient.data.l[0] = _target->win;
508 xev.xclient.data.l[1] = 0;
510 xev.xclient.data.l[1] |= 0x1UL;
513 xev.xclient.data.l[1] |= 0x2UL;
515 /* Set rectangle information */
516 xev.xclient.data.l[2] = rectangle.x;
517 xev.xclient.data.l[2] <<= 16;
518 xev.xclient.data.l[2] |= rectangle.y;
519 xev.xclient.data.l[3] = rectangle.width;
520 xev.xclient.data.l[3] <<= 16;
521 xev.xclient.data.l[3] |= rectangle.height;
525 xev.xclient.data.l[4] = action;
526 _target->accepted_action = action;
530 xev.xclient.data.l[4] = None;
531 _target->accepted_action = action;
534 XSendEvent(_ecore_x_disp, _target->source, False, 0, &xev);
538 ecore_x_dnd_send_finished(void)
542 if (_target->state == ECORE_X_DND_TARGET_IDLE)
545 LOGFN(__FILE__, __LINE__, __FUNCTION__);
546 xev.xany.type = ClientMessage;
547 xev.xany.display = _ecore_x_disp;
548 xev.xclient.message_type = ECORE_X_ATOM_XDND_FINISHED;
549 xev.xclient.format = 32;
550 xev.xclient.window = _target->source;
552 xev.xclient.data.l[0] = _target->win;
553 xev.xclient.data.l[1] = 0;
554 xev.xclient.data.l[2] = 0;
555 if (_target->will_accept)
557 xev.xclient.data.l[1] |= 0x1UL;
558 xev.xclient.data.l[2] = _target->accepted_action;
561 XSendEvent(_ecore_x_disp, _target->source, False, 0, &xev);
563 _target->state = ECORE_X_DND_TARGET_IDLE;
567 ecore_x_dnd_source_action_set(Ecore_X_Atom action)
569 _source->action = action;
570 if (_source->prev.window)
571 _ecore_x_dnd_drag(_source->prev.window, _source->prev.x, _source->prev.y);
575 ecore_x_dnd_source_action_get(void)
577 return _source->action;
581 _ecore_x_dnd_drag(Ecore_X_Window root,
587 Ecore_X_Window *skip;
588 Ecore_X_Xdnd_Position pos;
591 if (_source->state != ECORE_X_DND_SOURCE_DRAGGING)
594 /* Preinitialize XEvent struct */
595 memset(&xev, 0, sizeof(XEvent));
596 xev.xany.type = ClientMessage;
597 xev.xany.display = _ecore_x_disp;
598 xev.xclient.format = 32;
600 /* Attempt to find a DND-capable window under the cursor */
601 skip = ecore_x_window_ignore_list(&num);
602 // WARNING - this function is HEAVY. it goes to and from x a LOT walking the
603 // window tree - use the SHADOW version - makes a 1-off tree copy, then uses
605 // win = ecore_x_window_at_xy_with_skip_get(x, y, skip, num);
606 win = ecore_x_window_shadow_tree_at_xy_with_skip_get(root, x, y, skip, num);
608 // NOTE: This now uses the shadow version to find parent windows
609 // while ((win) && !(ecore_x_dnd_version_get(win)))
610 // win = ecore_x_window_parent_get(win);
611 while ((win) && !(ecore_x_dnd_version_get(win)))
612 win = ecore_x_window_shadow_parent_get(root, win);
614 /* Send XdndLeave to current destination window if we have left it */
615 if ((_source->dest) && (win != _source->dest))
617 xev.xclient.window = _source->dest;
618 xev.xclient.message_type = ECORE_X_ATOM_XDND_LEAVE;
619 xev.xclient.data.l[0] = _source->win;
620 xev.xclient.data.l[1] = 0;
622 XSendEvent(_ecore_x_disp, _source->dest, False, 0, &xev);
623 _source->suppress = 0;
630 _source->version = MIN(ECORE_X_DND_VERSION,
631 ecore_x_dnd_version_get(win));
632 if (win != _source->dest)
638 ecore_x_window_prop_property_get(_source->win,
639 ECORE_X_ATOM_XDND_TYPE_LIST,
644 types = (Ecore_X_Atom *)data;
646 /* Entered new window, send XdndEnter */
647 xev.xclient.window = win;
648 xev.xclient.message_type = ECORE_X_ATOM_XDND_ENTER;
649 xev.xclient.data.l[0] = _source->win;
650 xev.xclient.data.l[1] = 0;
652 xev.xclient.data.l[1] |= 0x1UL;
654 xev.xclient.data.l[1] &= 0xfffffffeUL;
656 xev.xclient.data.l[1] |= ((unsigned long)_source->version) << 24;
658 for (i = 2; i < 5; i++)
659 xev.xclient.data.l[i] = 0;
660 for (i = 0; i < MIN(num, 3); ++i)
661 xev.xclient.data.l[i + 2] = types[i];
663 XSendEvent(_ecore_x_disp, win, False, 0, &xev);
664 _source->await_status = 0;
665 _source->will_accept = 0;
668 /* Determine if we're still in the rectangle from the last status */
669 x1 = _source->rectangle.x;
670 x2 = _source->rectangle.x + _source->rectangle.width;
671 y1 = _source->rectangle.y;
672 y2 = _source->rectangle.y + _source->rectangle.height;
674 if ((!_source->await_status) ||
675 (!_source->suppress) ||
676 ((x < x1) || (x > x2) || (y < y1) || (y > y2)))
678 xev.xclient.window = win;
679 xev.xclient.message_type = ECORE_X_ATOM_XDND_POSITION;
680 xev.xclient.data.l[0] = _source->win;
681 xev.xclient.data.l[1] = 0; /* Reserved */
682 xev.xclient.data.l[2] = ((x << 16) & 0xffff0000) | (y & 0xffff);
683 xev.xclient.data.l[3] = _source->time; /* Version 1 */
684 xev.xclient.data.l[4] = _source->action; /* Version 2, Needs to be pre-set */
685 XSendEvent(_ecore_x_disp, win, False, 0, &xev);
687 _source->await_status = 1;
696 pos.prev = _source->dest;
697 _posupdatecb(_posupdatedata, &pos);
702 _source->prev.window = root;
706 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/