summaryrefslogtreecommitdiff
path: root/src/lib/efl_canvas_wl/copiedfromweston.x
blob: d5c35eee75f79a9f2fc137f89fc94d0c515ef339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright © 2011 Kristian Høgsberg
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \
                     WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \
                     WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)

static uint32_t
data_offer_choose_action(Comp_Data_Device_Offer *offer)
{
   uint32_t available_actions, preferred_action = 0;
   uint32_t source_actions, offer_actions;

   if (wl_resource_get_version(offer->res) >=
       WL_DATA_OFFER_ACTION_SINCE_VERSION)
     {
        offer_actions = offer->dnd_actions;
        preferred_action = offer->preferred_dnd_action;
     }
   else
     {
        offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
     }

   if (wl_resource_get_version(offer->source->res) >=
       WL_DATA_SOURCE_ACTION_SINCE_VERSION)
     source_actions = offer->source->dnd_actions;
   else
     source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;

   available_actions = offer_actions & source_actions;

   if (!available_actions)
     return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;

   if (offer->source->seat &&
       offer->source->compositor_action & available_actions)
     return offer->source->compositor_action;

   /* If the dest side has a preferred DnD action, use it */
   if ((preferred_action & available_actions) != 0)
     return preferred_action;

   /* Use the first found action, in bit order */
   return 1 << (ffs(available_actions) - 1);
}

static void
data_offer_update_action(Comp_Data_Device_Offer *offer)
{
   uint32_t action;

   if (!offer->source)
     return;

   action = data_offer_choose_action(offer);

   if (offer->source->current_dnd_action == action)
     return;

   offer->source->current_dnd_action = action;

   if (offer->in_ask)
     return;

   if (wl_resource_get_version(offer->source->res) >=
       WL_DATA_SOURCE_ACTION_SINCE_VERSION)
     wl_data_source_send_action(offer->source->res, action);

   if (wl_resource_get_version(offer->res) >=
       WL_DATA_OFFER_ACTION_SINCE_VERSION)
     wl_data_offer_send_action(offer->res, action);
}

static void
data_device_offer_set_actions(struct wl_client *client,
                              struct wl_resource *resource,
                              uint32_t dnd_actions, uint32_t preferred_action)
{
   Comp_Data_Device_Offer *offer = wl_resource_get_user_data(resource);

   if (dnd_actions & ~ALL_ACTIONS)
     {
        wl_resource_post_error(offer->res,
                               WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK,
                               "invalid action mask %x", dnd_actions);
        return;
     }

   if (preferred_action &&
       (!(preferred_action & dnd_actions) ||
        __builtin_popcount(preferred_action) > 1))
     {
        wl_resource_post_error(offer->res,
                               WL_DATA_OFFER_ERROR_INVALID_ACTION,
                               "invalid action %x", preferred_action);
        return;
     }

   offer->dnd_actions = dnd_actions;
   offer->preferred_dnd_action = preferred_action;
   data_offer_update_action(offer);
}

#ifdef HAVE_ECORE_X
static Ecore_X_Atom
action_convert(uint32_t action)
{
   if (action == WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE)
     return ECORE_X_ATOM_XDND_ACTION_MOVE;
   if (action == WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
     return ECORE_X_ATOM_XDND_ACTION_ASK;
   return ECORE_X_ATOM_XDND_ACTION_COPY;
}
#endif
static void
data_device_offer_accept(struct wl_client *client, struct wl_resource *resource,
                         uint32_t serial, const char *mime_type)
{
   Comp_Data_Device_Offer *offer = wl_resource_get_user_data(resource);
   Comp_Surface *cs;

   /* Protect against untimely calls from older data offers */
   if (!offer->source || offer != offer->source->offer)
     return;

   switch (offer->type)
     {
      case COMP_DATA_DEVICE_OFFER_TYPE_DND:
        cs = offer->source->seat->drag.enter;
        if (!offer->source->seat->drag.res) return;
        if ((!offer->source->seat->drag.source) &&
          (wl_resource_get_client(cs->res) != wl_resource_get_client(offer->source->seat->drag.res)))
          return;
#ifdef HAVE_ECORE_X
        if (offer->source->x11_owner)
          {
             Ecore_Window win = ecore_evas_window_get(ecore_evas_ecore_evas_get(offer->source->seat->c->evas));
             offer->source->accepted = mime_type != NULL;
             ecore_x_client_message32_send(offer->source->x11_owner,
               ECORE_X_ATOM_XDND_STATUS, ECORE_X_EVENT_MASK_NONE,
               win, 2 | !!mime_type, 0, 0,
               (!!mime_type) * action_convert(offer->source->current_dnd_action));
             return;
          }
#endif
        break;

      case COMP_DATA_DEVICE_OFFER_TYPE_CLIPBOARD:
        break;
      default: return;
     }
   if (offer->source->seat->client_offer)
     ecore_wl2_offer_accept(offer->source->seat->client_offer, mime_type);
   else
     wl_data_source_send_target(offer->source->res, mime_type);
   offer->source->accepted = mime_type != NULL;
}

static void
data_device_offer_receive(struct wl_client *client, struct wl_resource *resource,
                          const char *mime_type, int32_t fd)
{
   Comp_Data_Device_Offer *offer = wl_resource_get_user_data(resource);

   if (offer->source && offer == offer->source->offer)
     {
        if (offer->proxy)
          {
             Ecore_Wl2_Offer *off;
#ifdef HAVE_ECORE_X
             if (offer->source->x11_owner)
               {
                  x11_send_send(offer->source, mime_type, fd, offer->type);
                  return;
               }
#endif
             if (offer->type == COMP_DATA_DEVICE_OFFER_TYPE_CLIPBOARD)
               off = ecore_wl2_dnd_selection_get(offer->source->seat->seat);
             else
               {
                  off = offer->source->seat->client_offer;
                  offer->source->seat->client_offer = NULL;
               }
             ecore_wl2_offer_proxy_receive(off, mime_type, fd);
             offer->proxy_offer = off;
          }
        else
          wl_data_source_send_send(offer->source->res, mime_type, fd);
     }
   close(fd);
}

static void
data_source_notify_finish(Comp_Data_Device_Source *source)
{
   if (!source->actions_set)
     return;

   if (source->proxy && (!source->x11_owner))
     ecore_wl2_offer_finish(source->offer->proxy_offer);

   if (source->offer && source->offer->in_ask &&
       wl_resource_get_version(source->res) >=
       WL_DATA_SOURCE_ACTION_SINCE_VERSION)
     {
        wl_data_source_send_action(source->res,
                                   source->current_dnd_action);
     }

   if (wl_resource_get_version(source->res) >=
       WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION)
     {
        wl_data_source_send_dnd_finished(source->res);
     }

   source->offer = NULL;
}

static void
data_device_offer_finish(struct wl_client *client, struct wl_resource *resource)
{
   Comp_Data_Device_Offer *offer = wl_resource_get_user_data(resource);

   if (!offer->source || offer->source->offer != offer)
     return;

   /* Disallow finish while we have a grab driving drag-and-drop, or
    * if the negotiation is not at the right stage
    */
   if (((!offer->proxy) && offer->source->seat) ||
       !offer->source->accepted)
     {
        wl_resource_post_error(offer->res,
                               WL_DATA_OFFER_ERROR_INVALID_FINISH,
                               "premature finish request");
        return;
     }

   switch (offer->source->current_dnd_action)
     {
      case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
      case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
        wl_resource_post_error(offer->res,
                               WL_DATA_OFFER_ERROR_INVALID_OFFER,
                               "offer finished with an invalid action");
        return;

      default:
        break;
     }

   data_source_notify_finish(offer->source);
}

static void
data_device_offer_impl_destroy(struct wl_resource *resource)
{
   Comp_Data_Device_Offer *offer = wl_resource_get_user_data(resource);

   if (!offer->source)
     goto out;

   if (offer->source->offer != offer)
     goto out;

   if (offer->type == COMP_DATA_DEVICE_OFFER_TYPE_DND)
     {
        /* If the drag destination has version < 3, wl_data_offer.finish
         * won't be called, so do this here as a safety net, because
         * we still want the version >=3 drag source to be happy.
         */
        if (wl_resource_get_version(offer->res) <
            WL_DATA_OFFER_ACTION_SINCE_VERSION)
          {
             data_source_notify_finish(offer->source);
          }
        else if (offer->source->res &&
                 wl_resource_get_version(offer->source->res) >=
                 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION)
          {
             wl_data_source_send_cancelled(offer->source->res);
          }
     }

   offer->source->offer = NULL;
   if (offer->proxy_offer && offer->proxy)
     ecore_wl2_offer_proxy_receive_end(offer->proxy_offer);
out:
   free(offer);
}

static void
drag_grab_button(Comp_Seat *s,
                 uint32_t time, uint32_t button, uint32_t state_w)
{
   Comp_Data_Device_Source *data_source = s->drag.source;
   enum wl_pointer_button_state state = state_w;
   struct wl_resource *res;

   if (data_source &&
       s->drag.id == button &&
       state == WL_POINTER_BUTTON_STATE_RELEASED)
     {
        if ((s->drag.enter || (s->drag.x11_owner == ecore_evas_window_get(ecore_evas_ecore_evas_get(s->c->evas)))) &&
            data_source->accepted &&
            data_source->current_dnd_action)
          {
             if (s->drag.enter)
               {
                  res = data_device_find(s, s->drag.enter->res);
                  if (!res) return;

                  wl_data_device_send_drop(res);
               }
             if (wl_resource_get_version(data_source->res) >=
                 WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION)
               wl_data_source_send_dnd_drop_performed(data_source->res);

             if (data_source->offer)
               data_source->offer->in_ask =
                 data_source->current_dnd_action ==
                 WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;

             if (!data_source->proxy)
               data_source->seat = NULL;
          }
        else if (wl_resource_get_version(data_source->res) >=
                 WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION)
          {
             wl_data_source_send_cancelled(data_source->res);
          }
        seat_drag_end(s);
        if (!data_source->x11_owner)
          s->drag.source = NULL;
#ifdef HAVE_ECORE_X
        if (ecore_x_display_get())
          ecore_x_pointer_ungrab();
#endif
     }
}

#ifdef HAVE_ECORE_X
static xkb_mod_index_t x11_kbd_shift_mod;
static xkb_mod_index_t x11_kbd_caps_mod;
static xkb_mod_index_t x11_kbd_ctrl_mod;
static xkb_mod_index_t x11_kbd_alt_mod;
static xkb_mod_index_t x11_kbd_mod2_mod;
static xkb_mod_index_t x11_kbd_mod3_mod;
static xkb_mod_index_t x11_kbd_super_mod;
static xkb_mod_index_t x11_kbd_mod5_mod;

static void
keymap_mods_init(struct xkb_keymap *keymap)
{
   x11_kbd_shift_mod = xkb_keymap_mod_get_index(keymap,  XKB_MOD_NAME_SHIFT);
   x11_kbd_caps_mod = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
   x11_kbd_ctrl_mod = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
   x11_kbd_alt_mod = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT);
   x11_kbd_mod2_mod = xkb_keymap_mod_get_index(keymap, "Mod2");
   x11_kbd_mod3_mod = xkb_keymap_mod_get_index(keymap, "Mod3");
   x11_kbd_super_mod = xkb_keymap_mod_get_index(keymap,  XKB_MOD_NAME_LOGO);
   x11_kbd_mod5_mod = xkb_keymap_mod_get_index(keymap, "Mod5");
}

static uint32_t
get_xkb_mod_mask(uint32_t in)
{
	uint32_t ret = 0;

	if ((in & ECORE_X_MODIFIER_SHIFT) && x11_kbd_shift_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_shift_mod);
	if ((in & ECORE_X_LOCK_CAPS) && x11_kbd_caps_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_caps_mod);
	if ((in & ECORE_X_MODIFIER_CTRL) && x11_kbd_ctrl_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_ctrl_mod);
	if ((in & ECORE_X_MODIFIER_ALT) && x11_kbd_alt_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_alt_mod);
	if ((in & ECORE_X_LOCK_NUM) && x11_kbd_mod2_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_mod2_mod);
	if ((in & ECORE_X_LOCK_SCROLL) && x11_kbd_mod3_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_mod3_mod);
	if ((in & ECORE_X_MODIFIER_WIN) && x11_kbd_super_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_super_mod);
	if ((in & ECORE_X_MODIFIER_ALTGR) && x11_kbd_mod5_mod != XKB_MOD_INVALID)
		ret |= (1 << x11_kbd_mod5_mod);

	return ret;
}

#endif