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);
7 static void _e_bg_image_import_dialog_done(void *data, const char *path, Eina_Bool ok, Eina_Bool external, int quality, E_Image_Import_Mode mode);
8 static void _e_bg_image_import_done(void *data, Eina_Bool ok, const char *image_path, const char *edje_path);
9 static void _e_bg_handler_image_imported(void *data, const char *image_path);
11 /* local subsystem globals */
12 EAPI int E_EVENT_BG_UPDATE = 0;
13 static E_Fm2_Mime_Handler *bg_hdl = NULL;
15 struct _E_Bg_Image_Import_Handle
18 void (*func)(void *data, const char *edje_file);
22 E_Util_Image_Import_Handle *importer;
26 /* externally accessible functions */
31 E_Config_Desktop_Background *cfbg = NULL;
33 /* Register mime handler */
34 bg_hdl = e_fm2_mime_handler_new(_("Set As Background"),
35 "preferences-desktop-wallpaper",
36 e_bg_handler_set, NULL,
37 e_bg_handler_test, NULL);
40 e_fm2_mime_handler_glob_add(bg_hdl, "*.edj");
41 e_fm2_mime_handler_mime_add(bg_hdl, "image/png");
42 e_fm2_mime_handler_mime_add(bg_hdl, "image/jpeg");
45 /* Register files in use */
46 if (e_config->desktop_default_background)
47 e_filereg_register(e_config->desktop_default_background);
49 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
52 e_filereg_register(cfbg->file);
55 E_EVENT_BG_UPDATE = ecore_event_type_new();
63 E_Config_Desktop_Background *cfbg = NULL;
65 /* Deregister mime handler */
68 e_fm2_mime_handler_glob_del(bg_hdl, "*.edj");
69 e_fm2_mime_handler_free(bg_hdl);
72 /* Deregister files in use */
73 if (e_config->desktop_default_background)
74 e_filereg_deregister(e_config->desktop_default_background);
76 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
79 e_filereg_deregister(cfbg->file);
86 * Find the configuration for a given desktop background
87 * Use -1 as a wild card for each parameter.
88 * The most specific match will be returned
90 EAPI const E_Config_Desktop_Background *
91 e_bg_config_get(int container_num, int zone_num, int desk_x, int desk_y)
93 Eina_List *l, *ll, *entries;
94 E_Config_Desktop_Background *bg = NULL, *cfbg = NULL;
95 const char *bgfile = "";
97 int current_spec = 0; /* how specific the setting is - we want the least general one that applies */
99 /* look for desk specific background. */
100 if (container_num >= 0 || zone_num >= 0 || desk_x >= 0 || desk_y >= 0)
102 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
108 if (cfbg->container == container_num) spec++;
109 else if (cfbg->container >= 0) continue;
110 if (cfbg->zone == zone_num) spec++;
111 else if (cfbg->zone >= 0) continue;
112 if (cfbg->desk_x == desk_x) spec++;
113 else if (cfbg->desk_x >= 0) continue;
114 if (cfbg->desk_y == desk_y) spec++;
115 else if (cfbg->desk_y >= 0) continue;
117 if (spec <= current_spec) continue;
121 if (bgfile[0] != '/')
125 bf = e_path_find(path_backgrounds, bgfile);
129 if (eina_str_has_extension(bgfile, ".edj"))
131 entries = edje_file_collection_list(bgfile);
134 EINA_LIST_FOREACH(entries, ll, entry)
136 if (!strcmp(entry, "e/desktop/background"))
142 edje_file_collection_list_free(entries);
156 e_bg_file_get(int container_num, int zone_num, int desk_x, int desk_y)
158 const E_Config_Desktop_Background *cfbg;
159 Eina_List *l, *entries;
160 const char *bgfile = "";
164 cfbg = e_bg_config_get(container_num, zone_num, desk_x, desk_y);
166 /* fall back to default */
172 if (bgfile[0] != '/')
176 bf = e_path_find(path_backgrounds, bgfile);
183 bgfile = e_config->desktop_default_background;
186 if (bgfile[0] != '/')
190 bf = e_path_find(path_backgrounds, bgfile);
194 if (bgfile && eina_str_has_extension(bgfile, ".edj"))
196 entries = edje_file_collection_list(bgfile);
199 EINA_LIST_FOREACH(entries, l, entry)
201 if (!strcmp(entry, "e/desktop/background"))
207 edje_file_collection_list_free(entries);
210 else if ((bgfile) && (bgfile[0])) ok = 1;
212 bgfile = e_theme_edje_file_get("base/theme/background",
213 "e/desktop/background");
220 e_bg_zone_update(E_Zone *zone, E_Bg_Transition transition)
223 const char *bgfile = "";
224 const char *trans = "";
227 if (transition == E_BG_TRANSITION_START) trans = e_config->transition_start;
228 else if (transition == E_BG_TRANSITION_DESK) trans = e_config->transition_desk;
229 else if (transition == E_BG_TRANSITION_CHANGE) trans = e_config->transition_change;
230 if ((!trans) || (!trans[0])) transition = E_BG_TRANSITION_NONE;
232 desk = e_desk_current_get(zone);
234 bgfile = e_bg_file_get(zone->container->num, zone->num, desk->x, desk->y);
236 bgfile = e_bg_file_get(zone->container->num, zone->num, -1, -1);
240 const char *pfile = "";
242 edje_object_file_get(zone->bg_object, &pfile, NULL);
243 if (!e_util_strcmp(pfile, bgfile)) return;
246 if (transition == E_BG_TRANSITION_NONE)
250 evas_object_del(zone->bg_object);
251 zone->bg_object = NULL;
260 if (zone->prev_bg_object)
261 evas_object_del(zone->prev_bg_object);
262 zone->prev_bg_object = zone->bg_object;
263 if (zone->transition_object)
264 evas_object_del(zone->transition_object);
265 zone->transition_object = NULL;
266 zone->bg_object = NULL;
268 o = edje_object_add(zone->container->bg_evas);
269 zone->transition_object = o;
270 /* FIXME: segv if zone is deleted while up??? */
271 evas_object_data_set(o, "e_zone", zone);
272 snprintf(buf, sizeof(buf), "e/transitions/%s", trans);
273 e_theme_edje_object_set(o, "base/theme/transitions", buf);
274 edje_object_signal_callback_add(o, "e,state,done", "*", _e_bg_signal, zone);
275 evas_object_move(o, zone->x, zone->y);
276 evas_object_resize(o, zone->w, zone->h);
277 evas_object_layer_set(o, -1);
278 evas_object_clip_set(o, zone->bg_clip_object);
281 if (eina_str_has_extension(bgfile, ".edj"))
283 o = edje_object_add(zone->container->bg_evas);
284 evas_object_data_set(o, "e_zone", zone);
285 edje_object_file_set(o, bgfile, "e/desktop/background");
289 o = e_icon_add(zone->container->bg_evas);
290 evas_object_data_set(o, "e_zone", zone);
291 e_icon_file_key_set(o, bgfile, NULL);
292 e_icon_fill_inside_set(o, 0);
295 if (transition == E_BG_TRANSITION_NONE)
297 evas_object_move(o, zone->x, zone->y);
298 evas_object_resize(o, zone->w, zone->h);
299 evas_object_layer_set(o, -1);
301 evas_object_clip_set(o, zone->bg_clip_object);
304 if (transition != E_BG_TRANSITION_NONE)
306 edje_extern_object_max_size_set(zone->prev_bg_object, 65536, 65536);
307 edje_extern_object_min_size_set(zone->prev_bg_object, 0, 0);
308 edje_object_part_swallow(zone->transition_object, "e.swallow.bg.old",
309 zone->prev_bg_object);
310 edje_extern_object_max_size_set(zone->bg_object, 65536, 65536);
311 edje_extern_object_min_size_set(zone->bg_object, 0, 0);
312 edje_object_part_swallow(zone->transition_object, "e.swallow.bg.new",
314 edje_object_signal_emit(zone->transition_object, "e,action,start", "e");
319 e_bg_default_set(const char *file)
321 E_Event_Bg_Update *ev;
324 file = eina_stringshare_add(file);
325 changed = file != e_config->desktop_default_background;
329 eina_stringshare_del(file);
333 if (e_config->desktop_default_background)
335 e_filereg_deregister(e_config->desktop_default_background);
336 eina_stringshare_del(e_config->desktop_default_background);
341 e_filereg_register(file);
342 e_config->desktop_default_background = file;
345 e_config->desktop_default_background = NULL;
347 ev = E_NEW(E_Event_Bg_Update, 1);
352 ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
356 e_bg_add(int container, int zone, int desk_x, int desk_y, const char *file)
359 E_Config_Desktop_Background *cfbg;
360 E_Event_Bg_Update *ev;
362 file = eina_stringshare_add(file);
364 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
367 (cfbg->container == container) &&
368 (cfbg->zone == zone) &&
369 (cfbg->desk_x == desk_x) &&
370 (cfbg->desk_y == desk_y) &&
371 (cfbg->file == file))
373 eina_stringshare_del(file);
378 e_bg_del(container, zone, desk_x, desk_y);
379 cfbg = E_NEW(E_Config_Desktop_Background, 1);
380 cfbg->container = container;
382 cfbg->desk_x = desk_x;
383 cfbg->desk_y = desk_y;
385 e_config->desktop_backgrounds = eina_list_append(e_config->desktop_backgrounds, cfbg);
387 e_filereg_register(cfbg->file);
389 ev = E_NEW(E_Event_Bg_Update, 1);
390 ev->container = container;
394 ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
398 e_bg_del(int container, int zone, int desk_x, int desk_y)
401 E_Config_Desktop_Background *cfbg;
402 E_Event_Bg_Update *ev;
404 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
407 if ((cfbg->container == container) && (cfbg->zone == zone) &&
408 (cfbg->desk_x == desk_x) && (cfbg->desk_y == desk_y))
410 e_config->desktop_backgrounds = eina_list_remove_list(e_config->desktop_backgrounds, l);
411 e_filereg_deregister(cfbg->file);
412 if (cfbg->file) eina_stringshare_del(cfbg->file);
418 ev = E_NEW(E_Event_Bg_Update, 1);
419 ev->container = container;
423 ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
429 Eina_List *l, *ll, *lll;
434 EINA_LIST_FOREACH(e_manager_list(), l, man)
436 EINA_LIST_FOREACH(man->containers, ll, con)
438 EINA_LIST_FOREACH(con->zones, lll, zone)
440 e_zone_bg_reconfigure(zone);
446 static inline Eina_Bool
447 _e_bg_file_edje_check(const char *path)
450 const size_t extlen = sizeof(".edj") - 1;
453 if (!path) return EINA_FALSE;
456 if (len <= extlen) return EINA_FALSE;
457 ext = path + len - extlen;
458 return memcmp(ext, ".edj", extlen) == 0;
462 * Go through process of importing an image as E background.
464 * This will go through process of importing an image as E
465 * background. It will ask fill/tile mode of the image, as well as
468 * User can cancel operation at any time, and in this case callback is
469 * called with @c NULL as second parameter.
471 * The operation can be canceled by application/module as well using
472 * e_bg_image_import_cancel(). Even in this case the callback is called so user
473 * can free possibly allocated data.
475 * @param image_file source file to use, must be supported by Evas.
476 * @param cb callback to call when import process is done. The first
477 * argument is the given data, while the second is the path to
478 * the imported background file (edje) that can be used with
479 * e_bg_add() or e_bg_default_set(). Note that if @a edje_file
480 * is @c NULL, then the import process was canceled!
481 * This function is @b always called and after it returns
482 * E_Bg_Image_Import_Handle is deleted.
483 * @param data pointer to data to be given to callback.
485 * @return handle to the import process. It will die automatically
486 * when user cancels the process or when code automatically
487 * calls e_bg_image_import_cancel(). Before dying, callback
488 * will always be called.
490 EAPI E_Bg_Image_Import_Handle *
491 e_bg_image_import(const char *image_file, void (*cb)(void *data, const char *edje_file), const void *data)
493 E_Bg_Image_Import_Handle *handle;
495 if (!image_file) return NULL;
496 if (!cb) return NULL;
498 handle = E_NEW(E_Bg_Image_Import_Handle, 1);
499 if (!handle) return NULL;
500 handle->cb.func = cb;
501 handle->cb.data = (void *)data;
503 handle->dia = e_util_image_import_settings_new
504 (image_file, _e_bg_image_import_dialog_done, handle);
510 e_dialog_show(handle->dia);
516 * Cancels previously started import process.
518 * Note that his handle will be deleted when process import, so don't
519 * call it after your callback is called!
522 e_bg_image_import_cancel(E_Bg_Image_Import_Handle *handle)
526 handle->canceled = EINA_TRUE;
530 handle->cb.func(handle->cb.data, NULL);
531 handle->cb.func = NULL;
535 e_object_del(E_OBJECT(handle->dia));
538 else if (handle->importer)
540 e_util_image_import_cancel(handle->importer);
541 handle->importer = NULL;
547 * Set background to image, as required in e_fm2_mime_handler_new()
550 e_bg_handler_set(Evas_Object *obj __UNUSED__, const char *path, void *data __UNUSED__)
554 if (_e_bg_file_edje_check(path))
559 E_Container *con = e_container_current_get(e_manager_current_get());
560 E_Zone *zone = e_zone_current_get(con);
561 E_Desk *desk = e_desk_current_get(zone);
563 /* if not in system dir or user dir, copy to user dir */
564 e_prefix_data_concat_static(buf, "data/backgrounds");
565 if (!strncmp(buf, path, strlen(buf)))
569 e_user_dir_concat_static(buf, "backgrounds");
570 if (!strncmp(buf, path, strlen(buf)))
578 file = ecore_file_file_get(path);
579 name = ecore_file_strip_ext(file);
581 e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s-%f.edj", name, ecore_time_unix_get());
584 if (!ecore_file_exists(buf))
586 ecore_file_cp(path, buf);
587 e_bg_add(con->num, zone->num, desk->x, desk->y, buf);
590 e_bg_add(con->num, zone->num, desk->x, desk->y, path);
593 e_bg_add(con->num, zone->num, desk->x, desk->y, path);
596 e_config_save_queue();
600 e_bg_image_import(path, _e_bg_handler_image_imported, NULL);
604 * Test if possible to set background to file, as required in
605 * e_fm2_mime_handler_new()
607 * This handler tests for files that would be acceptable for setting
610 * You should just register it with "*.edj" (glob matching extension)
611 * or "image/" (mimetypes)that are acceptable with Evas loaders.
613 * Just edje files with "e/desktop/background" group are used.
616 e_bg_handler_test(Evas_Object *obj __UNUSED__, const char *path, void *data __UNUSED__)
621 if (_e_bg_file_edje_check(path))
623 if (edje_file_group_exists(path, "e/desktop/background")) return 1;
627 /* it's image/png or image/jpeg, we'll import it. */
631 /* local subsystem functions */
633 _e_bg_signal(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
637 if (zone->prev_bg_object)
639 evas_object_del(zone->prev_bg_object);
640 zone->prev_bg_object = NULL;
642 if (zone->transition_object)
644 evas_object_del(zone->transition_object);
645 zone->transition_object = NULL;
647 evas_object_move(zone->bg_object, zone->x, zone->y);
648 evas_object_resize(zone->bg_object, zone->w, zone->h);
649 evas_object_layer_set(zone->bg_object, -1);
650 evas_object_clip_set(zone->bg_object, zone->bg_clip_object);
651 evas_object_show(zone->bg_object);
655 _e_bg_event_bg_update_free(void *data __UNUSED__, void *event)
661 _e_bg_image_import_dialog_done(void *data, const char *path, Eina_Bool ok, Eina_Bool external, int quality, E_Image_Import_Mode mode)
663 E_Bg_Image_Import_Handle *handle = data;
670 if (!ok) goto aborted;
672 file = ecore_file_file_get(path);
673 if (!file) goto aborted;
674 name = ecore_file_strip_ext(file);
675 if (!name) goto aborted;
677 used = e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s.edj", name);
679 if (used >= sizeof(buf)) goto aborted;
681 off = used - (sizeof(".edj") - 1);
683 for (num = 0; ecore_file_exists(buf); num++)
684 snprintf(buf + off, sizeof(buf) - off, "-%u.edj", num);
686 handle->importer = e_util_image_import
687 (path, buf, "e/desktop/background", external, quality, mode,
688 _e_bg_image_import_done, handle);
689 if (!handle->importer) goto aborted;
696 handle->cb.func(handle->cb.data, NULL);
697 handle->cb.func = NULL;
699 if (!handle->canceled) E_FREE(handle);
703 _e_bg_image_import_done(void *data, Eina_Bool ok, const char *image_path __UNUSED__, const char *edje_path)
705 E_Bg_Image_Import_Handle *handle = data;
707 if (!ok) edje_path = NULL;
711 handle->cb.func(handle->cb.data, edje_path);
712 handle->cb.func = NULL;
715 if (!handle->canceled) E_FREE(handle);
719 _e_bg_handler_image_imported(void *data __UNUSED__, const char *image_path)
721 E_Container *con = e_container_current_get(e_manager_current_get());
722 E_Zone *zone = e_zone_current_get(con);
723 E_Desk *desk = e_desk_current_get(zone);
725 if (!image_path) return;
727 e_bg_add(con->num, zone->num, desk->x, desk->y, image_path);
729 e_config_save_queue();