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 Eina_Bool _e_bg_slide_animator(void *data);
8 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);
9 static void _e_bg_image_import_done(void *data, Eina_Bool ok, const char *image_path, const char *edje_path);
10 static void _e_bg_handler_image_imported(void *data, const char *image_path);
12 /* local subsystem globals */
13 EAPI int E_EVENT_BG_UPDATE = 0;
14 static E_Fm2_Mime_Handler *bg_hdl = NULL;
16 typedef struct _E_Bg_Anim_Params E_Bg_Anim_Params;
17 struct _E_Bg_Anim_Params
31 struct _E_Bg_Image_Import_Handle
34 void (*func)(void *data, const char *edje_file);
38 E_Util_Image_Import_Handle *importer;
42 /* externally accessible functions */
47 E_Config_Desktop_Background *cfbg = NULL;
49 /* Register mime handler */
50 bg_hdl = e_fm2_mime_handler_new(_("Set As Background"),
51 "preferences-desktop-wallpaper",
52 e_bg_handler_set, NULL,
53 e_bg_handler_test, NULL);
56 e_fm2_mime_handler_glob_add(bg_hdl, "*.edj");
57 e_fm2_mime_handler_mime_add(bg_hdl, "image/png");
58 e_fm2_mime_handler_mime_add(bg_hdl, "image/jpeg");
61 /* Register files in use */
62 if (e_config->desktop_default_background)
63 e_filereg_register(e_config->desktop_default_background);
65 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
68 e_filereg_register(cfbg->file);
71 E_EVENT_BG_UPDATE = ecore_event_type_new();
79 E_Config_Desktop_Background *cfbg = NULL;
81 /* Deregister mime handler */
84 e_fm2_mime_handler_glob_del(bg_hdl, "*.edj");
85 e_fm2_mime_handler_free(bg_hdl);
88 /* Deregister files in use */
89 if (e_config->desktop_default_background)
90 e_filereg_deregister(e_config->desktop_default_background);
92 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
95 e_filereg_deregister(cfbg->file);
102 * Find the configuration for a given desktop background
103 * Use -1 as a wild card for each parameter.
104 * The most specific match will be returned
106 EAPI const E_Config_Desktop_Background *
107 e_bg_config_get(int container_num, int zone_num, int desk_x, int desk_y)
109 Eina_List *l, *ll, *entries;
110 E_Config_Desktop_Background *bg = NULL, *cfbg = NULL;
111 const char *bgfile = "";
113 int current_spec = 0; /* how specific the setting is - we want the least general one that applies */
115 /* look for desk specific background. */
116 if (container_num >= 0 || zone_num >= 0 || desk_x >= 0 || desk_y >= 0)
118 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
124 if (cfbg->container == container_num) spec++;
125 else if (cfbg->container >= 0) continue;
126 if (cfbg->zone == zone_num) spec++;
127 else if (cfbg->zone >= 0) continue;
128 if (cfbg->desk_x == desk_x) spec++;
129 else if (cfbg->desk_x >= 0) continue;
130 if (cfbg->desk_y == desk_y) spec++;
131 else if (cfbg->desk_y >= 0) continue;
133 if (spec <= current_spec) continue;
137 if (bgfile[0] != '/')
141 bf = e_path_find(path_backgrounds, bgfile);
145 entries = edje_file_collection_list(bgfile);
148 EINA_LIST_FOREACH(entries, ll, entry)
150 if (!strcmp(entry, "e/desktop/background"))
156 edje_file_collection_list_free(entries);
164 e_bg_file_get(int container_num, int zone_num, int desk_x, int desk_y)
166 const E_Config_Desktop_Background *cfbg;
167 Eina_List *l, *entries;
168 const char *bgfile = "";
172 cfbg = e_bg_config_get(container_num, zone_num, desk_x, desk_y);
174 /* fall back to default */
180 if (bgfile[0] != '/')
184 bf = e_path_find(path_backgrounds, bgfile);
191 bgfile = e_config->desktop_default_background;
194 if (bgfile[0] != '/')
198 bf = e_path_find(path_backgrounds, bgfile);
202 entries = edje_file_collection_list(bgfile);
205 EINA_LIST_FOREACH(entries, l, entry)
207 if (!strcmp(entry, "e/desktop/background"))
213 edje_file_collection_list_free(entries);
216 bgfile = e_theme_edje_file_get("base/theme/background",
217 "e/desktop/background");
224 e_bg_zone_update(E_Zone *zone, E_Bg_Transition transition)
227 const char *bgfile = "";
228 const char *trans = "";
231 if (transition == E_BG_TRANSITION_START) trans = e_config->transition_start;
232 else if (transition == E_BG_TRANSITION_DESK) trans = e_config->transition_desk;
233 else if (transition == E_BG_TRANSITION_CHANGE) trans = e_config->transition_change;
234 if ((!trans) || (!trans[0])) transition = E_BG_TRANSITION_NONE;
235 if (e_config->desk_flip_pan_bg) transition = E_BG_TRANSITION_NONE;
237 desk = e_desk_current_get(zone);
239 bgfile = e_bg_file_get(zone->container->num, zone->num, desk->x, desk->y);
241 bgfile = e_bg_file_get(zone->container->num, zone->num, -1, -1);
245 const char *pfile = "";
247 edje_object_file_get(zone->bg_object, &pfile, NULL);
248 if ((!e_util_strcmp(pfile, bgfile)) && !e_config->desk_flip_pan_bg) return;
251 if (transition == E_BG_TRANSITION_NONE)
255 evas_object_del(zone->bg_object);
256 zone->bg_object = NULL;
265 if (zone->prev_bg_object)
266 evas_object_del(zone->prev_bg_object);
267 zone->prev_bg_object = zone->bg_object;
268 if (zone->transition_object)
269 evas_object_del(zone->transition_object);
270 zone->transition_object = NULL;
271 zone->bg_object = NULL;
273 o = edje_object_add(zone->container->bg_evas);
274 zone->transition_object = o;
275 /* FIXME: segv if zone is deleted while up??? */
276 evas_object_data_set(o, "e_zone", zone);
277 snprintf(buf, sizeof(buf), "e/transitions/%s", trans);
278 e_theme_edje_object_set(o, "base/theme/transitions", buf);
279 edje_object_signal_callback_add(o, "e,state,done", "*", _e_bg_signal, zone);
280 evas_object_move(o, zone->x, zone->y);
281 evas_object_resize(o, zone->w, zone->h);
282 evas_object_layer_set(o, -1);
283 evas_object_clip_set(o, zone->bg_clip_object);
286 o = edje_object_add(zone->container->bg_evas);
288 evas_object_data_set(o, "e_zone", zone);
289 edje_object_file_set(o, bgfile, "e/desktop/background");
290 if (transition == E_BG_TRANSITION_NONE)
292 evas_object_move(o, zone->x, zone->y);
293 evas_object_resize(o, zone->w, zone->h);
294 evas_object_layer_set(o, -1);
296 evas_object_clip_set(o, zone->bg_clip_object);
298 if (e_config->desk_flip_pan_bg)
302 o = zone->bg_scrollframe;
305 o = e_scrollframe_add(zone->container->bg_evas);
306 zone->bg_scrollframe = o;
307 e_scrollframe_custom_theme_set(o, "base/theme/background",
308 "e/desktop/background/scrollframe");
309 e_scrollframe_policy_set(o, E_SCROLLFRAME_POLICY_OFF, E_SCROLLFRAME_POLICY_OFF);
310 e_scrollframe_child_pos_set(o, 0, 0);
313 e_scrollframe_child_set(o, zone->bg_object);
319 e_bg_zone_slide(zone, x, y);
323 if (transition != E_BG_TRANSITION_NONE)
325 edje_extern_object_max_size_set(zone->prev_bg_object, 65536, 65536);
326 edje_extern_object_min_size_set(zone->prev_bg_object, 0, 0);
327 edje_object_part_swallow(zone->transition_object, "e.swallow.bg.old",
328 zone->prev_bg_object);
329 edje_extern_object_max_size_set(zone->bg_object, 65536, 65536);
330 edje_extern_object_min_size_set(zone->bg_object, 0, 0);
331 edje_object_part_swallow(zone->transition_object, "e.swallow.bg.new",
333 edje_object_signal_emit(zone->transition_object, "e,action,start", "e");
338 e_bg_zone_slide(E_Zone *zone, int prev_x, int prev_y)
342 Evas_Coord w, h, maxw, maxh, step_w, step_h;
343 Ecore_Animator *anim;
344 E_Bg_Anim_Params *params;
345 Evas_Coord vw, vh, px, py;
349 desk = e_desk_current_get(zone);
350 edje_object_size_max_get(zone->bg_object, &w, &h);
351 maxw = zone->w * zone->desk_x_count;
352 maxh = zone->h * zone->desk_y_count;
355 evas_object_resize(zone->bg_object, w, h);
356 if (zone->desk_x_count > 1)
357 step_w = ((double) (w - zone->w)) / (zone->desk_x_count - 1);
359 if (zone->desk_y_count > 1)
360 step_h = ((double) (h - zone->h)) / (zone->desk_y_count - 1);
363 o = zone->bg_scrollframe;
364 evas_object_move(o, zone->x, zone->y);
365 evas_object_resize(o, zone->w, zone->h);
366 evas_object_layer_set(o, -1);
367 evas_object_clip_set(o, zone->bg_clip_object);
369 data = edje_object_data_get(zone->bg_object, "directional_freedom");
370 e_scrollframe_child_viewport_size_get(o, &vw, &vh);
371 e_scrollframe_child_pos_get(o, &px, &py);
372 params = evas_object_data_get(zone->bg_object, "switch_animator_params");
374 params = E_NEW(E_Bg_Anim_Params, 1);
376 params->start_x = px;
377 params->start_y = py;
378 params->end_x = desk->x * step_w * e_config->desk_flip_pan_x_axis_factor;
379 params->end_y = desk->y * step_h * e_config->desk_flip_pan_y_axis_factor;
380 params->start_time = 0.0;
381 if ((data) && (sscanf(data, "%d %d", &fx, &fy) == 2))
385 params->freedom.x = EINA_TRUE;
386 params->start_x = prev_x * step_w * e_config->desk_flip_pan_x_axis_factor;
390 params->freedom.y = EINA_TRUE;
391 params->start_y = prev_y * step_h * e_config->desk_flip_pan_y_axis_factor;
395 anim = evas_object_data_get(zone->bg_object, "switch_animator");
396 if (anim) ecore_animator_del(anim);
397 anim = ecore_animator_add(_e_bg_slide_animator, params);
398 evas_object_data_set(zone->bg_object, "switch_animator", anim);
399 evas_object_data_set(zone->bg_object, "switch_animator_params", params);
403 e_bg_default_set(const char *file)
405 E_Event_Bg_Update *ev;
408 file = eina_stringshare_add(file);
409 changed = file != e_config->desktop_default_background;
413 eina_stringshare_del(file);
417 if (e_config->desktop_default_background)
419 e_filereg_deregister(e_config->desktop_default_background);
420 eina_stringshare_del(e_config->desktop_default_background);
425 e_filereg_register(file);
426 e_config->desktop_default_background = file;
429 e_config->desktop_default_background = NULL;
431 ev = E_NEW(E_Event_Bg_Update, 1);
436 ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
440 e_bg_add(int container, int zone, int desk_x, int desk_y, const char *file)
443 E_Config_Desktop_Background *cfbg;
444 E_Event_Bg_Update *ev;
446 file = eina_stringshare_add(file);
448 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
451 (cfbg->container == container) &&
452 (cfbg->zone == zone) &&
453 (cfbg->desk_x == desk_x) &&
454 (cfbg->desk_y == desk_y) &&
455 (cfbg->file == file))
457 eina_stringshare_del(file);
462 e_bg_del(container, zone, desk_x, desk_y);
463 cfbg = E_NEW(E_Config_Desktop_Background, 1);
464 cfbg->container = container;
466 cfbg->desk_x = desk_x;
467 cfbg->desk_y = desk_y;
469 e_config->desktop_backgrounds = eina_list_append(e_config->desktop_backgrounds, cfbg);
471 e_filereg_register(cfbg->file);
473 ev = E_NEW(E_Event_Bg_Update, 1);
474 ev->container = container;
478 ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
482 e_bg_del(int container, int zone, int desk_x, int desk_y)
485 E_Config_Desktop_Background *cfbg;
486 E_Event_Bg_Update *ev;
488 EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
491 if ((cfbg->container == container) && (cfbg->zone == zone) &&
492 (cfbg->desk_x == desk_x) && (cfbg->desk_y == desk_y))
494 e_config->desktop_backgrounds = eina_list_remove_list(e_config->desktop_backgrounds, l);
495 e_filereg_deregister(cfbg->file);
496 if (cfbg->file) eina_stringshare_del(cfbg->file);
502 ev = E_NEW(E_Event_Bg_Update, 1);
503 ev->container = container;
507 ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
513 Eina_List *l, *ll, *lll;
518 EINA_LIST_FOREACH(e_manager_list(), l, man)
520 EINA_LIST_FOREACH(man->containers, ll, con)
522 EINA_LIST_FOREACH(con->zones, lll, zone)
524 e_zone_bg_reconfigure(zone);
530 static inline Eina_Bool
531 _e_bg_file_edje_check(const char *path)
534 const size_t extlen = sizeof(".edj") - 1;
537 if (!path) return EINA_FALSE;
540 if (len <= extlen) return EINA_FALSE;
541 ext = path + len - extlen;
542 return memcmp(ext, ".edj", extlen) == 0;
546 * Go through process of importing an image as E background.
548 * This will go through process of importing an image as E
549 * background. It will ask fill/tile mode of the image, as well as
552 * User can cancel operation at any time, and in this case callback is
553 * called with @c NULL as second parameter.
555 * The operation can be canceled by application/module as well using
556 * e_bg_image_import_cancel(). Even in this case the callback is called so user
557 * can free possibly allocated data.
559 * @param image_file source file to use, must be supported by Evas.
560 * @param cb callback to call when import process is done. The first
561 * argument is the given data, while the second is the path to
562 * the imported background file (edje) that can be used with
563 * e_bg_add() or e_bg_default_set(). Note that if @a edje_file
564 * is @c NULL, then the import process was canceled!
565 * This function is @b always called and after it returns
566 * E_Bg_Image_Import_Handle is deleted.
567 * @param data pointer to data to be given to callback.
569 * @return handle to the import process. It will die automatically
570 * when user cancels the process or when code automatically
571 * calls e_bg_image_import_cancel(). Before dying, callback
572 * will always be called.
574 EAPI E_Bg_Image_Import_Handle *
575 e_bg_image_import(const char *image_file, void (*cb)(void *data, const char *edje_file), const void *data)
577 E_Bg_Image_Import_Handle *handle;
579 if (!image_file) return NULL;
580 if (!cb) return NULL;
582 handle = E_NEW(E_Bg_Image_Import_Handle, 1);
583 if (!handle) return NULL;
584 handle->cb.func = cb;
585 handle->cb.data = (void *)data;
587 handle->dia = e_util_image_import_settings_new
588 (image_file, _e_bg_image_import_dialog_done, handle);
594 e_dialog_show(handle->dia);
600 * Cancels previously started import process.
602 * Note that his handle will be deleted when process import, so don't
603 * call it after your callback is called!
606 e_bg_image_import_cancel(E_Bg_Image_Import_Handle *handle)
610 handle->canceled = EINA_TRUE;
614 handle->cb.func(handle->cb.data, NULL);
615 handle->cb.func = NULL;
619 e_object_del(E_OBJECT(handle->dia));
622 else if (handle->importer)
624 e_util_image_import_cancel(handle->importer);
625 handle->importer = NULL;
631 * Set background to image, as required in e_fm2_mime_handler_new()
634 e_bg_handler_set(Evas_Object *obj __UNUSED__, const char *path, void *data __UNUSED__)
638 if (_e_bg_file_edje_check(path))
643 E_Container *con = e_container_current_get(e_manager_current_get());
644 E_Zone *zone = e_zone_current_get(con);
645 E_Desk *desk = e_desk_current_get(zone);
647 /* if not in system dir or user dir, copy to user dir */
648 e_prefix_data_concat_static(buf, "data/backgrounds");
649 if (!strncmp(buf, path, strlen(buf)))
653 e_user_dir_concat_static(buf, "backgrounds");
654 if (!strncmp(buf, path, strlen(buf)))
662 file = ecore_file_file_get(path);
663 name = ecore_file_strip_ext(file);
665 e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s-%f.edj", name, ecore_time_unix_get());
668 if (!ecore_file_exists(buf))
670 ecore_file_cp(path, buf);
671 e_bg_add(con->num, zone->num, desk->x, desk->y, buf);
674 e_bg_add(con->num, zone->num, desk->x, desk->y, path);
677 e_bg_add(con->num, zone->num, desk->x, desk->y, path);
680 e_config_save_queue();
684 e_bg_image_import(path, _e_bg_handler_image_imported, NULL);
688 * Test if possible to set background to file, as required in
689 * e_fm2_mime_handler_new()
691 * This handler tests for files that would be acceptable for setting
694 * You should just register it with "*.edj" (glob matching extension)
695 * or "image/" (mimetypes)that are acceptable with Evas loaders.
697 * Just edje files with "e/desktop/background" group are used.
700 e_bg_handler_test(Evas_Object *obj __UNUSED__, const char *path, void *data __UNUSED__)
705 if (_e_bg_file_edje_check(path))
707 if (edje_file_group_exists(path, "e/desktop/background")) return 1;
711 /* it's image/png or image/jpeg, we'll import it. */
715 /* local subsystem functions */
717 _e_bg_signal(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
721 if (zone->prev_bg_object)
723 evas_object_del(zone->prev_bg_object);
724 zone->prev_bg_object = NULL;
726 if (zone->transition_object)
728 evas_object_del(zone->transition_object);
729 zone->transition_object = NULL;
731 evas_object_move(zone->bg_object, zone->x, zone->y);
732 evas_object_resize(zone->bg_object, zone->w, zone->h);
733 evas_object_layer_set(zone->bg_object, -1);
734 evas_object_clip_set(zone->bg_object, zone->bg_clip_object);
735 evas_object_show(zone->bg_object);
739 _e_bg_event_bg_update_free(void *data __UNUSED__, void *event)
745 _e_bg_slide_animator(void *data)
747 E_Bg_Anim_Params *params;
752 Evas_Coord px, py, rx, ry, bw, bh, panw, panh;
753 Edje_Message_Int_Set *msg;
757 t = ecore_loop_time_get();
758 spd = e_config->desk_flip_animate_time;
760 o = zone->bg_scrollframe;
761 if (!params->start_time)
762 st = params->start_time = t;
764 st = params->start_time;
767 if (dt > 1.0) dt = 1.0;
769 dt *= dt; /* decelerate - could be a better hack */
771 if (params->end_x > params->start_x)
772 rx = params->start_x + (params->end_x - params->start_x) * (1.0 - dt);
774 rx = params->end_x + (params->start_x - params->end_x) * dt;
775 if (params->freedom.x) px = zone->x;
778 if (params->end_y > params->start_y)
779 ry = params->start_y + (params->end_y - params->start_y) * (1.0 - dt);
781 ry = params->end_y + (params->start_y - params->end_y) * dt;
782 if (params->freedom.y) py = zone->y;
785 e_scrollframe_child_pos_set(o, px, py);
787 evas_object_geometry_get(zone->bg_object, NULL, NULL, &bw, &bh);
789 if (panw < 0) panw = 0;
791 if (panh < 0) panh = 0;
792 msg = alloca(sizeof(Edje_Message_Int_Set) + (5 * sizeof(int)));
800 edje_object_message_send(zone->bg_object, EDJE_MESSAGE_INT_SET, 0, msg);
804 evas_object_data_del(zone->bg_object, "switch_animator");
805 evas_object_data_del(zone->bg_object, "switch_animator_params");
807 return ECORE_CALLBACK_CANCEL;
809 return ECORE_CALLBACK_RENEW;
813 _e_bg_image_import_dialog_done(void *data, const char *path, Eina_Bool ok, Eina_Bool external, int quality, E_Image_Import_Mode mode)
815 E_Bg_Image_Import_Handle *handle = data;
822 if (!ok) goto aborted;
824 file = ecore_file_file_get(path);
825 if (!file) goto aborted;
826 name = ecore_file_strip_ext(file);
827 if (!name) goto aborted;
829 used = e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s.edj", name);
831 if (used >= sizeof(buf)) goto aborted;
833 off = used - (sizeof(".edj") - 1);
835 for (num = 0; ecore_file_exists(buf); num++)
836 snprintf(buf + off, sizeof(buf) - off, "-%u.edj", num);
838 handle->importer = e_util_image_import
839 (path, buf, "e/desktop/background", external, quality, mode,
840 _e_bg_image_import_done, handle);
841 if (!handle->importer) goto aborted;
848 handle->cb.func(handle->cb.data, NULL);
849 handle->cb.func = NULL;
851 if (!handle->canceled) E_FREE(handle);
855 _e_bg_image_import_done(void *data, Eina_Bool ok, const char *image_path __UNUSED__, const char *edje_path)
857 E_Bg_Image_Import_Handle *handle = data;
859 if (!ok) edje_path = NULL;
863 handle->cb.func(handle->cb.data, edje_path);
864 handle->cb.func = NULL;
867 if (!handle->canceled) E_FREE(handle);
871 _e_bg_handler_image_imported(void *data __UNUSED__, const char *image_path)
873 E_Container *con = e_container_current_get(e_manager_current_get());
874 E_Zone *zone = e_zone_current_get(con);
875 E_Desk *desk = e_desk_current_get(zone);
877 if (!image_path) return;
879 e_bg_add(con->num, zone->num, desk->x, desk->y, image_path);
881 e_config_save_queue();