summaryrefslogtreecommitdiff
path: root/src/pip.c
blob: 45d021b4a8234b03d4b6ee606105ed31a1ad0b3d (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
#include "e_mod_main.h"

static E_Client_Menu_Hook *hook = NULL;
static Eina_Hash *pips = NULL;
static E_Action *act = NULL;
static Ecore_Event_Handler *handlers[2];

static Ecore_Event_Handler *action_handler = NULL;

static Eina_Bool editing = EINA_FALSE;

typedef struct Pip
{
   Evas_Object *pip;
   Evas_Point down;
   unsigned char opacity;
   E_Pointer_Mode resize_mode;
   Eina_Bool move : 1;
   Eina_Bool resize : 1;
} Pip;

static void
pips_edit(void)
{
   Pip *pip;
   Eina_Iterator *it;
   E_Comp *comp;

   comp = e_comp_get(NULL);
   if (comp->nocomp) return;
   editing = EINA_TRUE;
   ds_fade_setup(comp);
   it = eina_hash_iterator_data_new(pips);
   EINA_ITERATOR_FOREACH(it, pip)
     {
        evas_object_layer_set(pip->pip, E_LAYER_MENU + 1);
        evas_object_pass_events_set(pip->pip, 0);
     }
   eina_iterator_free(it);
   e_comp_shape_queue(comp);
}

static void
pips_noedit(void)
{
   Pip *pip;
   Eina_Iterator *it;

   editing = EINA_FALSE;
   ds_fade_end(NULL);
   it = eina_hash_iterator_data_new(pips);
   EINA_ITERATOR_FOREACH(it, pip)
     {
        evas_object_layer_set(pip->pip, E_LAYER_CLIENT_PRIO);
        evas_object_pass_events_set(pip->pip, 1);
     }
   eina_iterator_free(it);
   e_comp_shape_queue(e_comp_get(NULL));
   E_FREE_FUNC(action_handler, ecore_event_handler_del);
}

static void
pip_free(Pip *pip)
{
   evas_object_del(pip->pip);
   free(pip);
}

static Eina_Bool
_pip_mouse_move(Pip *pip, int t EINA_UNUSED, Ecore_Event_Mouse_Move *ev)
{
   int x, y, w, h;

   evas_object_geometry_get(pip->pip, &x, &y, &w, &h);
   if (pip->resize)
     {
        if ((pip->resize_mode == E_POINTER_RESIZE_B) ||
            (pip->resize_mode == E_POINTER_RESIZE_BL) ||
            (pip->resize_mode == E_POINTER_RESIZE_BR))
          h = MAX(ev->root.y - y, 5);
        else if ((pip->resize_mode == E_POINTER_RESIZE_T) ||
            (pip->resize_mode == E_POINTER_RESIZE_TL) ||
            (pip->resize_mode == E_POINTER_RESIZE_TR))
          {
             h = MAX((y + h) - (ev->root.y - pip->down.y), 5);
             y = ev->root.y - pip->down.y;
          }
        if ((pip->resize_mode == E_POINTER_RESIZE_R) ||
            (pip->resize_mode == E_POINTER_RESIZE_TR) ||
            (pip->resize_mode == E_POINTER_RESIZE_BR))
          w = MAX(ev->root.x - x, 5);
        else if ((pip->resize_mode == E_POINTER_RESIZE_L) ||
            (pip->resize_mode == E_POINTER_RESIZE_TL) ||
            (pip->resize_mode == E_POINTER_RESIZE_BL))
          {
             w = MAX((x + w) - (ev->root.x - pip->down.x), 5);
             x = ev->root.x - pip->down.x;
          }
        evas_object_geometry_set(pip->pip, x, y, w, h);
     }
   else if (pip->move)
     {
        E_Comp *comp = e_comp_util_evas_object_comp_get(pip->pip);
        evas_object_move(pip->pip,
          E_CLAMP(ev->root.x - pip->down.x, 0, comp->man->w - (w / 2)),
          E_CLAMP(ev->root.y - pip->down.y, 0, comp->man->h - (h / 2)));
     }
   return ECORE_CALLBACK_RENEW;
}

static void
_pip_mouse_wheel(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Ecore_Event_Mouse_Wheel *ev = event_info;
   Pip *pip = data;

   if (ev->z < 0)
     pip->opacity = E_CLAMP(pip->opacity + 15, 0, 255);
   else if (ev->z > 0)
     pip->opacity = E_CLAMP(pip->opacity - 15, 0, 255);
   efx_fade(pip->pip, EFX_EFFECT_SPEED_LINEAR, EFX_COLOR(pip->opacity, pip->opacity, pip->opacity), pip->opacity, 0.2, NULL, NULL);
}

static void
_pip_mouse_up(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Pip *pip = data;

   pip->down.x = pip->down.y = 0;
   pip->move = pip->resize = 0;
   pip->resize_mode = E_POINTER_RESIZE_NONE;
   E_FREE_FUNC(action_handler, ecore_event_handler_del);
}

static void
_pip_mouse_down(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info)
{
   Evas_Event_Mouse_Down *ev = event_info;
   Pip *pip = data;
   int x, y, w, h;

   if (ev->button == 3)
     {
        eina_hash_del_by_data(pips, pip);
        return;
     }
   evas_object_geometry_get(obj, &x, &y, &w, &h);
   pip->down.x = ev->output.x - x;
   pip->down.y = ev->output.y - y;
   pip->move = ev->button == 1;
   pip->resize = ev->button == 2;
   if (pip->resize)
     {
        if ((ev->output.x > (x + w / 5)) &&
            (ev->output.x < (x + w * 4 / 5)))
          {
             if (ev->output.y < (y + h / 2))
               {
                  pip->resize_mode = E_POINTER_RESIZE_T;
               }
             else
               {
                  pip->resize_mode = E_POINTER_RESIZE_B;
               }
          }
        else if (ev->output.x < (x + w / 2))
          {
             if ((ev->output.y > (y + h / 5)) &&
                 (ev->output.y < (y + h * 4 / 5)))
               {
                  pip->resize_mode = E_POINTER_RESIZE_L;
               }
             else if (ev->output.y < (y + h / 2))
               {
                  pip->resize_mode = E_POINTER_RESIZE_TL;
               }
             else
               {
                  pip->resize_mode = E_POINTER_RESIZE_BL;
               }
          }
        else
          {
             if ((ev->output.y > (y + h / 5)) &&
                 (ev->output.y < (y + h * 4 / 5)))
               {
                  pip->resize_mode = E_POINTER_RESIZE_R;
               }
             else if (ev->output.y < (y + h / 2))
               {
                  pip->resize_mode = E_POINTER_RESIZE_TR;
               }
             else
               {
                  pip->resize_mode = E_POINTER_RESIZE_BR;
               }
          }
     }
   action_handler = ecore_event_handler_add(ECORE_EVENT_MOUSE_MOVE, (Ecore_Event_Handler_Cb)_pip_mouse_move, pip);
}

static void
_pip_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   E_Client *ec = data;
   eina_hash_del_by_key(pips, &ec->frame);
   if (editing && (!eina_hash_population(pips)))
     pips_noedit();
}

static void
_pip_delete(void *data, E_Menu *m EINA_UNUSED, E_Menu_Item *mi EINA_UNUSED)
{
   _pip_del(data, NULL, NULL, NULL);
}

static void
_pip_create(void *data, E_Menu *m EINA_UNUSED, E_Menu_Item *mi EINA_UNUSED)
{
   E_Client *ec = data;
   Pip *pip;
   Evas_Object *o;

   o = e_comp_object_util_mirror_add(ec->frame);
   if (!o) return; //FIXME throw real error

   pip = E_NEW(Pip, 1);
   pip->pip = o;
   pip->resize_mode = E_POINTER_RESIZE_NONE;
   pip->opacity = 255;

   evas_object_geometry_set(o, ec->zone->x + 1, ec->zone->y + 1, (ec->w * (ec->zone->h / 4)) / ec->h, ec->zone->h / 4);
   e_comp_object_util_center(o);
   evas_object_data_set(o, "comp_skip", (void*)1);
   evas_object_layer_set(o, E_LAYER_CLIENT_PRIO);
   evas_object_pass_events_set(o, 1);
   evas_object_show(o);
   evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _pip_mouse_down, pip);
   evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, _pip_mouse_up, pip);
   evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_WHEEL, _pip_mouse_wheel, pip);
   evas_object_event_callback_add(o, EVAS_CALLBACK_DEL, _pip_del, ec);

   efx_fade(o, EFX_EFFECT_SPEED_LINEAR, EFX_COLOR(0, 0, 0), 0, 0.0, NULL, NULL);
   efx_fade(o, EFX_EFFECT_SPEED_LINEAR, EFX_COLOR(255, 255, 255), 255, 0.2, NULL, NULL);

   eina_hash_add(pips, &ec->frame, pip);
}

static void
_pip_hook(void *d EINA_UNUSED, E_Client *ec)
{
   E_Menu_Item *mi;
   const Eina_List *l;
   Eina_Bool exists;

   if (!ec->border_menu) return;
   if (!e_comp_config_get()->enable_advanced_features) return;

   /* only one per client for now */
   exists = !!eina_hash_find(pips, &ec->frame);

   EINA_LIST_REVERSE_FOREACH(ec->border_menu->items, l, mi)
     if (mi->separator) break;

   mi = eina_list_data_get(l->prev);
   mi = e_menu_item_new(mi->submenu);
   if (exists)
     e_menu_item_label_set(mi, D_("Delete Mini"));
   else
     e_menu_item_label_set(mi, D_("Create Mini"));
   e_menu_item_icon_edje_set(mi, mod->edje_file, "icon");
   if (exists)
     e_menu_item_callback_set(mi, _pip_delete, ec);
   else
     e_menu_item_callback_set(mi, _pip_create, ec);
}

static void
_pip_action_cb(E_Object *obj EINA_UNUSED, const char *params EINA_UNUSED)
{
   if (editing)
     pips_noedit();
   else
     pips_edit();
}

static Eina_Bool
pip_comp_disable()
{
   if (editing)
     {
        pips_noedit();
        editing = EINA_TRUE;
     }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
pip_comp_enable()
{
   if (editing) pips_edit();
   return ECORE_CALLBACK_RENEW;
}

EINTERN void
pip_init(void)
{
   hook = e_int_client_menu_hook_add(_pip_hook, NULL);
   pips = eina_hash_pointer_new((Eina_Free_Cb)pip_free);
   handlers[0] = ecore_event_handler_add(E_EVENT_COMPOSITOR_DISABLE, pip_comp_disable, NULL);
   handlers[1] = ecore_event_handler_add(E_EVENT_COMPOSITOR_ENABLE, pip_comp_enable, NULL);

   act = e_action_add("pip");
   if (act)
     {
        act->func.go = _pip_action_cb;
        e_action_predef_name_set(D_("Compositor"), D_("Manage Minis"),
                                 "pip", NULL, NULL, 0);
     }
}

EINTERN void
pip_shutdown(void)
{
   E_FREE_FUNC(hook, e_int_client_menu_hook_del);
   E_FREE_FUNC(pips, eina_hash_free);
   E_FREE_FUNC(handlers[0], ecore_event_handler_del);
   E_FREE_FUNC(handlers[1], ecore_event_handler_del);
   ds_fade_end(NULL);
   e_action_predef_name_del(D_("Compositor"), D_("Manage Minis"));
   e_action_del("pips");
   act = NULL;
   E_FREE_FUNC(action_handler, ecore_event_handler_del);
   editing = EINA_FALSE;
}