create rotation job and EAPI e_border_rotation_set
[platform/core/uifw/e17.git] / src / bin / e_bg.c
1 #include "e.h"
2
3 /* local subsystem functions */
4 static void _e_bg_signal(void *data, Evas_Object *obj, const char *emission, const char *source);
5 static void _e_bg_event_bg_update_free(void *data, void *event);
6 static void e_bg_handler_set(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *path);
7 static int e_bg_handler_test(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *path);
8 static void _e_bg_handler_image_imported(const char *image_path, void *data);
9
10 /* local subsystem globals */
11 EAPI int E_EVENT_BG_UPDATE = 0;
12 static E_Fm2_Mime_Handler *bg_hdl = NULL;
13
14 /* externally accessible functions */
15 EINTERN int
16 e_bg_init(void)
17 {
18    Eina_List *l = NULL;
19    E_Config_Desktop_Background *cfbg = NULL;
20
21    /* Register mime handler */
22    bg_hdl = e_fm2_mime_handler_new(_("Set As Background"),
23                                    "preferences-desktop-wallpaper",
24                                    e_bg_handler_set, NULL,
25                                    e_bg_handler_test, NULL);
26    if (bg_hdl)
27      {
28         e_fm2_mime_handler_glob_add(bg_hdl, "*.edj");
29         e_fm2_mime_handler_mime_add(bg_hdl, "image/png");
30         e_fm2_mime_handler_mime_add(bg_hdl, "image/jpeg");
31      }
32
33    /* Register files in use */
34    if (e_config->desktop_default_background)
35      e_filereg_register(e_config->desktop_default_background);
36
37    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
38      {
39         if (!cfbg) continue;
40         e_filereg_register(cfbg->file);
41      }
42
43    E_EVENT_BG_UPDATE = ecore_event_type_new();
44    return 1;
45 }
46
47 EINTERN int
48 e_bg_shutdown(void)
49 {
50    Eina_List *l = NULL;
51    E_Config_Desktop_Background *cfbg = NULL;
52
53    /* Deregister mime handler */
54    if (bg_hdl)
55      {
56         e_fm2_mime_handler_glob_del(bg_hdl, "*.edj");
57         e_fm2_mime_handler_free(bg_hdl);
58      }
59
60    /* Deregister files in use */
61    if (e_config->desktop_default_background)
62      e_filereg_deregister(e_config->desktop_default_background);
63
64    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
65      {
66         if (!cfbg) continue;
67         e_filereg_deregister(cfbg->file);
68      }
69
70    return 1;
71 }
72
73 /**
74  * Find the configuration for a given desktop background
75  * Use -1 as a wild card for each parameter.
76  * The most specific match will be returned
77  */
78 EAPI const E_Config_Desktop_Background *
79 e_bg_config_get(int container_num, int zone_num, int desk_x, int desk_y)
80 {
81    Eina_List *l, *ll, *entries;
82    E_Config_Desktop_Background *bg = NULL, *cfbg = NULL;
83    const char *bgfile = "";
84    char *entry;
85    int current_spec = 0; /* how specific the setting is - we want the least general one that applies */
86
87    /* look for desk specific background. */
88    if (container_num >= 0 || zone_num >= 0 || desk_x >= 0 || desk_y >= 0)
89      {
90         EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
91           {
92              int spec;
93
94              if (!cfbg) continue;
95              spec = 0;
96              if (cfbg->container == container_num) spec++;
97              else if (cfbg->container >= 0)
98                continue;
99              if (cfbg->zone == zone_num) spec++;
100              else if (cfbg->zone >= 0)
101                continue;
102              if (cfbg->desk_x == desk_x) spec++;
103              else if (cfbg->desk_x >= 0)
104                continue;
105              if (cfbg->desk_y == desk_y) spec++;
106              else if (cfbg->desk_y >= 0)
107                continue;
108
109              if (spec <= current_spec) continue;
110              bgfile = cfbg->file;
111              if (bgfile)
112                {
113                   if (bgfile[0] != '/')
114                     {
115                        const char *bf;
116
117                        bf = e_path_find(path_backgrounds, bgfile);
118                        if (bf) bgfile = bf;
119                     }
120                }
121              if (eina_str_has_extension(bgfile, ".edj"))
122                {
123                   entries = edje_file_collection_list(bgfile);
124                   if (entries)
125                     {
126                        EINA_LIST_FOREACH(entries, ll, entry)
127                          {
128                             if (!strcmp(entry, "e/desktop/background"))
129                               {
130                                  bg = cfbg;
131                                  current_spec = spec;
132                               }
133                          }
134                        edje_file_collection_list_free(entries);
135                     }
136                }
137              else
138                {
139                   bg = cfbg;
140                   current_spec = spec;
141                }
142           }
143      }
144    return bg;
145 }
146
147 EAPI const char *
148 e_bg_file_get(int container_num, int zone_num, int desk_x, int desk_y)
149 {
150    const E_Config_Desktop_Background *cfbg;
151    Eina_List *l, *entries;
152    const char *bgfile = "";
153    char *entry;
154    int ok = 0;
155
156    cfbg = e_bg_config_get(container_num, zone_num, desk_x, desk_y);
157
158    /* fall back to default */
159    if (cfbg)
160      {
161         bgfile = cfbg->file;
162         if (bgfile)
163           {
164              if (bgfile[0] != '/')
165                {
166                   const char *bf;
167
168                   bf = e_path_find(path_backgrounds, bgfile);
169                   if (bf) bgfile = bf;
170                }
171           }
172      }
173    else
174      {
175         bgfile = e_config->desktop_default_background;
176         if (bgfile)
177           {
178              if (bgfile[0] != '/')
179                {
180                   const char *bf;
181
182                   bf = e_path_find(path_backgrounds, bgfile);
183                   if (bf) bgfile = bf;
184                }
185           }
186         if (bgfile && eina_str_has_extension(bgfile, ".edj"))
187           {
188              entries = edje_file_collection_list(bgfile);
189              if (entries)
190                {
191                   EINA_LIST_FOREACH(entries, l, entry)
192                     {
193                        if (!strcmp(entry, "e/desktop/background"))
194                          {
195                             ok = 1;
196                             break;
197                          }
198                     }
199                   edje_file_collection_list_free(entries);
200                }
201           }
202         else if ((bgfile) && (bgfile[0]))
203           ok = 1;
204         if (!ok)
205           bgfile = e_theme_edje_file_get("base/theme/background",
206                                          "e/desktop/background");
207      }
208
209    return bgfile;
210 }
211
212 EAPI void
213 e_bg_zone_update(E_Zone *zone, E_Bg_Transition transition)
214 {
215    Evas_Object *o;
216    const char *bgfile = "";
217    const char *trans = "";
218    E_Desk *desk;
219
220    if (transition == E_BG_TRANSITION_START) trans = e_config->transition_start;
221    else if (transition == E_BG_TRANSITION_DESK)
222      trans = e_config->transition_desk;
223    else if (transition == E_BG_TRANSITION_CHANGE)
224      trans = e_config->transition_change;
225    if ((!trans) || (!trans[0])) transition = E_BG_TRANSITION_NONE;
226
227    desk = e_desk_current_get(zone);
228    if (desk)
229      bgfile = e_bg_file_get(zone->container->num, zone->num, desk->x, desk->y);
230    else
231      bgfile = e_bg_file_get(zone->container->num, zone->num, -1, -1);
232
233    if (zone->bg_object)
234      {
235         const char *pfile = "";
236
237         edje_object_file_get(zone->bg_object, &pfile, NULL);
238         if (!e_util_strcmp(pfile, bgfile)) return;
239      }
240
241    if (transition == E_BG_TRANSITION_NONE)
242      {
243         if (zone->bg_object)
244           {
245              evas_object_del(zone->bg_object);
246              zone->bg_object = NULL;
247           }
248      }
249    else
250      {
251         char buf[4096];
252
253         if (zone->bg_object)
254           {
255              if (zone->prev_bg_object)
256                evas_object_del(zone->prev_bg_object);
257              zone->prev_bg_object = zone->bg_object;
258              if (zone->transition_object)
259                evas_object_del(zone->transition_object);
260              zone->transition_object = NULL;
261              zone->bg_object = NULL;
262           }
263         o = edje_object_add(zone->container->bg_evas);
264         zone->transition_object = o;
265         /* FIXME: segv if zone is deleted while up??? */
266         evas_object_data_set(o, "e_zone", zone);
267         snprintf(buf, sizeof(buf), "e/transitions/%s", trans);
268         e_theme_edje_object_set(o, "base/theme/transitions", buf);
269         edje_object_signal_callback_add(o, "e,state,done", "*", _e_bg_signal, zone);
270         evas_object_move(o, zone->x, zone->y);
271         evas_object_resize(o, zone->w, zone->h);
272         evas_object_layer_set(o, -1);
273         evas_object_clip_set(o, zone->bg_clip_object);
274         evas_object_show(o);
275      }
276    if (eina_str_has_extension(bgfile, ".edj"))
277      {
278         o = edje_object_add(zone->container->bg_evas);
279         evas_object_data_set(o, "e_zone", zone);
280         edje_object_file_set(o, bgfile, "e/desktop/background");
281         if (edje_object_data_get(o, "noanimation"))
282           edje_object_animation_set(o, EINA_FALSE);
283      }
284    else
285      {
286         o = e_icon_add(zone->container->bg_evas);
287         evas_object_data_set(o, "e_zone", zone);
288         e_icon_file_key_set(o, bgfile, NULL);
289         e_icon_fill_inside_set(o, 0);
290      }
291    zone->bg_object = o;
292    if (transition == E_BG_TRANSITION_NONE)
293      {
294         evas_object_move(o, zone->x, zone->y);
295         evas_object_resize(o, zone->w, zone->h);
296         evas_object_layer_set(o, -1);
297      }
298    evas_object_clip_set(o, zone->bg_clip_object);
299    evas_object_show(o);
300
301    if (transition != E_BG_TRANSITION_NONE)
302      {
303         edje_extern_object_max_size_set(zone->prev_bg_object, 65536, 65536);
304         edje_extern_object_min_size_set(zone->prev_bg_object, 0, 0);
305         edje_object_part_swallow(zone->transition_object, "e.swallow.bg.old",
306                                  zone->prev_bg_object);
307         edje_extern_object_max_size_set(zone->bg_object, 65536, 65536);
308         edje_extern_object_min_size_set(zone->bg_object, 0, 0);
309         edje_object_part_swallow(zone->transition_object, "e.swallow.bg.new",
310                                  zone->bg_object);
311         edje_object_signal_emit(zone->transition_object, "e,action,start", "e");
312      }
313 }
314
315 EAPI void
316 e_bg_default_set(const char *file)
317 {
318    E_Event_Bg_Update *ev;
319    Eina_Bool changed;
320
321    file = eina_stringshare_add(file);
322    changed = file != e_config->desktop_default_background;
323
324    if (!changed)
325      {
326         eina_stringshare_del(file);
327         return;
328      }
329
330    if (e_config->desktop_default_background)
331      {
332         e_filereg_deregister(e_config->desktop_default_background);
333         eina_stringshare_del(e_config->desktop_default_background);
334      }
335
336    if (file)
337      {
338         e_filereg_register(file);
339         e_config->desktop_default_background = file;
340      }
341    else
342      e_config->desktop_default_background = NULL;
343
344    ev = E_NEW(E_Event_Bg_Update, 1);
345    ev->container = -1;
346    ev->zone = -1;
347    ev->desk_x = -1;
348    ev->desk_y = -1;
349    ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
350 }
351
352 EAPI void
353 e_bg_add(int container, int zone, int desk_x, int desk_y, const char *file)
354 {
355    const Eina_List *l;
356    E_Config_Desktop_Background *cfbg;
357    E_Event_Bg_Update *ev;
358
359    file = eina_stringshare_add(file);
360
361    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
362      {
363         if ((cfbg) &&
364             (cfbg->container == container) &&
365             (cfbg->zone == zone) &&
366             (cfbg->desk_x == desk_x) &&
367             (cfbg->desk_y == desk_y) &&
368             (cfbg->file == file))
369           {
370              eina_stringshare_del(file);
371              return;
372           }
373      }
374
375    e_bg_del(container, zone, desk_x, desk_y);
376    cfbg = E_NEW(E_Config_Desktop_Background, 1);
377    cfbg->container = container;
378    cfbg->zone = zone;
379    cfbg->desk_x = desk_x;
380    cfbg->desk_y = desk_y;
381    cfbg->file = file;
382    e_config->desktop_backgrounds = eina_list_append(e_config->desktop_backgrounds, cfbg);
383
384    e_filereg_register(cfbg->file);
385
386    ev = E_NEW(E_Event_Bg_Update, 1);
387    ev->container = container;
388    ev->zone = zone;
389    ev->desk_x = desk_x;
390    ev->desk_y = desk_y;
391    ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
392 }
393
394 EAPI void
395 e_bg_del(int container, int zone, int desk_x, int desk_y)
396 {
397    Eina_List *l;
398    E_Config_Desktop_Background *cfbg;
399    E_Event_Bg_Update *ev;
400
401    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
402      {
403         if (!cfbg) continue;
404         if ((cfbg->container == container) && (cfbg->zone == zone) &&
405             (cfbg->desk_x == desk_x) && (cfbg->desk_y == desk_y))
406           {
407              e_config->desktop_backgrounds = eina_list_remove_list(e_config->desktop_backgrounds, l);
408              e_filereg_deregister(cfbg->file);
409              if (cfbg->file) eina_stringshare_del(cfbg->file);
410              free(cfbg);
411              break;
412           }
413      }
414
415    ev = E_NEW(E_Event_Bg_Update, 1);
416    ev->container = container;
417    ev->zone = zone;
418    ev->desk_x = desk_x;
419    ev->desk_y = desk_y;
420    ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
421 }
422
423 EAPI void
424 e_bg_update(void)
425 {
426    Eina_List *l, *ll, *lll;
427    E_Manager *man;
428    E_Container *con;
429    E_Zone *zone;
430
431    EINA_LIST_FOREACH(e_manager_list(), l, man)
432      {
433         EINA_LIST_FOREACH(man->containers, ll, con)
434           {
435              EINA_LIST_FOREACH(con->zones, lll, zone)
436                {
437                   e_zone_bg_reconfigure(zone);
438                }
439           }
440      }
441 }
442
443 /* local subsystem functions */
444
445 /**
446  * Set background to image, as required in e_fm2_mime_handler_new()
447  */
448 static void
449 e_bg_handler_set(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *path)
450 {
451    E_Container *con;
452    char buf[4096];
453    int copy = 1;
454    E_Zone *zone;
455    E_Desk *desk;
456
457    if (!path) return;
458
459    con = e_container_current_get(e_manager_current_get());
460    if (!eina_str_has_extension(path, "edj"))
461      {
462         e_import_config_dialog_show(con, path, (Ecore_End_Cb)_e_bg_handler_image_imported, NULL);
463         return;
464      }
465    zone = e_zone_current_get(con);
466    desk = e_desk_current_get(zone);
467
468    /* if not in system dir or user dir, copy to user dir */
469    e_prefix_data_concat_static(buf, "data/backgrounds");
470    if (!strncmp(buf, path, strlen(buf)))
471      copy = 0;
472    if (copy)
473      {
474         e_user_dir_concat_static(buf, "backgrounds");
475         if (!strncmp(buf, path, strlen(buf)))
476           copy = 0;
477      }
478    if (copy)
479      {
480         const char *file;
481         char *name;
482
483         file = ecore_file_file_get(path);
484         name = ecore_file_strip_ext(file);
485
486         e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s-%f.edj", name, ecore_time_unix_get());
487         free(name);
488
489         if (!ecore_file_exists(buf))
490           {
491              if (!ecore_file_cp(path, buf)) return;
492              e_bg_add(con->num, zone->num, desk->x, desk->y, buf);
493           }
494         else
495           e_bg_add(con->num, zone->num, desk->x, desk->y, path);
496      }
497    else
498      e_bg_add(con->num, zone->num, desk->x, desk->y, path);
499
500    e_bg_update();
501    e_config_save_queue();
502 }
503
504 /**
505  * Test if possible to set background to file, as required in
506  * e_fm2_mime_handler_new()
507  *
508  * This handler tests for files that would be acceptable for setting
509  * background.
510  *
511  * You should just register it with "*.edj" (glob matching extension)
512  * or "image/" (mimetypes)that are acceptable with Evas loaders.
513  *
514  * Just edje files with "e/desktop/background" group are used.
515  */
516 static int
517 e_bg_handler_test(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *path)
518 {
519    if (!path) return 0;
520
521    if (eina_str_has_extension(path, "edj"))
522      {
523         if (edje_file_group_exists(path, "e/desktop/background")) return 1;
524         return 0;
525      }
526
527    /* it's image/png or image/jpeg, we'll import it. */
528    return 1;
529 }
530
531 static void
532 _e_bg_signal(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
533 {
534    E_Zone *zone = data;
535
536    if (zone->prev_bg_object)
537      {
538         evas_object_del(zone->prev_bg_object);
539         zone->prev_bg_object = NULL;
540      }
541    if (zone->transition_object)
542      {
543         evas_object_del(zone->transition_object);
544         zone->transition_object = NULL;
545      }
546    evas_object_move(zone->bg_object, zone->x, zone->y);
547    evas_object_resize(zone->bg_object, zone->w, zone->h);
548    evas_object_layer_set(zone->bg_object, -1);
549    evas_object_clip_set(zone->bg_object, zone->bg_clip_object);
550    evas_object_show(zone->bg_object);
551 }
552
553 static void
554 _e_bg_event_bg_update_free(void *data __UNUSED__, void *event)
555 {
556    free(event);
557 }
558
559 static void
560 _e_bg_handler_image_imported(const char *image_path, void *data __UNUSED__)
561 {
562    E_Container *con = e_container_current_get(e_manager_current_get());
563    E_Zone *zone = e_zone_current_get(con);
564    E_Desk *desk = e_desk_current_get(zone);
565
566    e_bg_add(con->num, zone->num, desk->x, desk->y, image_path);
567    e_bg_update();
568    e_config_save_queue();
569 }
570