Terminal emulator with all the bells and whistles https://www.enlightenment.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

5999 lines
176 KiB

#include "private.h"
#include <Elementary.h>
#include <Ecore_Input.h>
#include "termio.h"
#include "termiolink.h"
#include "termpty.h"
#include "termcmd.h"
#include "termptydbl.h"
#include "utf8.h"
#include "col.h"
#include "keyin.h"
#include "config.h"
#include "utils.h"
#include "media.h"
#include "dbus.h"
#include "miniview.h"
#include "gravatar.h"
#if defined (__MacOSX__) || (defined (__MACH__) && defined (__APPLE__))
# include <sys/proc_info.h>
# include <libproc.h>
#endif
typedef struct _Termio Termio;
struct _Termio
{
Evas_Object_Smart_Clipped_Data __clipped_data;
struct {
const char *name;
int size;
int chw, chh;
} font;
struct {
int w, h;
Evas_Object *obj;
} grid;
struct {
Evas_Object *top, *bottom, *theme;
} sel;
struct {
Evas_Object *obj;
int x, y;
} cursor;
struct {
int cx, cy;
int button;
} mouse;
struct {
char *string;
int x1, y1, x2, y2;
int suspend;
Eina_List *objs;
struct {
Evas_Object *dndobj;
Evas_Coord x, y;
unsigned char down : 1;
unsigned char dnd : 1;
unsigned char dndobjdel : 1;
} down;
} link;
Evas_Object *ctxpopup;
int zoom_fontsize_start;
int scroll;
Evas_Object *self;
Evas_Object *event;
Term *term;
Termpty *pty;
Ecore_Animator *anim;
Ecore_Timer *delayed_size_timer;
Ecore_Timer *link_do_timer;
Ecore_Timer *mouse_selection_scroll_timer;
Ecore_Job *mouse_move_job;
Ecore_Timer *mouseover_delay;
Evas_Object *win, *theme, *glayer;
Config *config;
const char *sel_str;
const char *preedit_str;
Eina_List *cur_chids;
Ecore_Job *sel_reset_job;
double set_sel_at;
Elm_Sel_Type sel_type;
Keys_Handler khdl;
unsigned char jump_on_change : 1;
unsigned char jump_on_keypress : 1;
unsigned char have_sel : 1;
unsigned char noreqsize : 1;
unsigned char didclick : 1;
unsigned char moved : 1;
unsigned char bottom_right : 1;
unsigned char top_left : 1;
unsigned char reset_sel : 1;
};
#define INT_SWAP(_a, _b) do { \
int _swap = _a; _a = _b; _b = _swap; \
} while (0)
static Evas_Smart *_smart = NULL;
static Evas_Smart_Class _parent_sc = EVAS_SMART_CLASS_INIT_NULL;
static Eina_List *terms = NULL;
static void _sel_set(Termio *sd, Eina_Bool enable);
static void _remove_links(Termio *sd, Evas_Object *obj);
static void _smart_update_queue(Evas_Object *obj, Termio *sd);
static void _smart_apply(Evas_Object *obj);
static void _smart_size(Evas_Object *obj, int w, int h, Eina_Bool force);
static void _smart_calculate(Evas_Object *obj);
static void _take_selection_text(Termio *sd, Elm_Sel_Type type, const char *text);
static void _smart_xy_to_cursor(Termio *sd, Evas_Coord x, Evas_Coord y, int *cx, int *cy);
static Eina_Bool _mouse_in_selection(Termio *sd, int cx, int cy);
/* {{{ Helpers */
void
termio_scroll(Evas_Object *obj, int direction, int start_y, int end_y)
{
Termpty *ty;
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
if ((!sd->jump_on_change) && // if NOT scroll to bottom on updates
(sd->scroll > 0))
{
Evas_Object *mv = term_miniview_get(sd->term);
if (mv) miniview_position_offset(mv, direction, EINA_FALSE);
// adjust scroll position for added scrollback
sd->scroll -= direction;
}
ty = sd->pty;
if (ty->selection.is_active)
{
int sel_start_y, sel_end_y;
sel_start_y = ty->selection.start.y;
sel_end_y = ty->selection.end.y;
if (!ty->selection.is_top_to_bottom)
INT_SWAP(sel_start_y, sel_end_y);
if (start_y <= sel_start_y &&
sel_end_y <= end_y)
{
ty->selection.orig.y += direction;
ty->selection.start.y += direction;
ty->selection.end.y += direction;
sel_start_y += direction;
sel_end_y += direction;
if (!(start_y <= sel_start_y &&
sel_end_y <= end_y))
{
_sel_set(sd, EINA_FALSE);
}
}
else if (!((start_y > sel_end_y) ||
(end_y < sel_start_y)))
{
_sel_set(sd, EINA_FALSE);
}
else if (sd->scroll > 0)
{
ty->selection.orig.y += direction;
ty->selection.start.y += direction;
ty->selection.end.y += direction;
}
}
if (sd->link.string)
{
if (sd->link.y1 <= end_y && sd->link.y2 >= start_y)
_remove_links(sd, obj);
}
}
void
termio_content_change(Evas_Object *obj, Evas_Coord x, Evas_Coord y,
int n)
{
Termpty *ty;
int start_x, start_y, end_x, end_y;
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
ty = sd->pty;
if (sd->link.string)
{
int _y = y + (x + n) / ty->w;
start_x = sd->link.x1;
start_y = sd->link.y1;
end_x = sd->link.x2;
end_y = sd->link.y2;
y = MAX(y, start_y);
for (; y <= MIN(_y, end_y); y++)
{
int d = MIN(n, ty->w - x);
if (!((x > end_x) || (x + d < start_x)))
{
_remove_links(sd, obj);
break;
}
n -= d;
x = 0;
}
}
if (!ty->selection.is_active) return;
start_x = sd->pty->selection.start.x;
start_y = sd->pty->selection.start.y;
end_x = sd->pty->selection.end.x;
end_y = sd->pty->selection.end.y;
if (!sd->pty->selection.is_top_to_bottom)
{
INT_SWAP(start_y, end_y);
INT_SWAP(start_x, end_x);
}
if (ty->selection.is_box)
{
int _y = y + (x + n) / ty->w;
y = MAX(y, start_y);
for (; y <= MIN(_y, end_y); y++)
{
int d = MIN(n, ty->w - x);
if (!((x > end_x) || (x + d < start_x)))
{
_sel_set(sd, EINA_FALSE);
break;
}
n -= d;
x = 0;
}
}
else
{
int sel_len;
Termcell *cells_changed, *cells_selection;
sel_len = end_x - start_x + ty->w * (end_y - start_y);
cells_changed = &(TERMPTY_SCREEN(ty, x, y));
cells_selection = &(TERMPTY_SCREEN(ty, start_x, start_y));
if (!((cells_changed > (cells_selection + sel_len)) ||
(cells_selection > (cells_changed + n))))
{
_sel_set(sd, EINA_FALSE);
}
}
}
static void
_win_obj_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *event EINA_UNUSED)
{
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
if (obj == sd->win)
{
evas_object_event_callback_del_full(sd->win, EVAS_CALLBACK_DEL,
_win_obj_del, data);
sd->win = NULL;
}
}
void
termio_win_set(Evas_Object *obj, Evas_Object *win)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
if (sd->win)
{
evas_object_event_callback_del_full(sd->win, EVAS_CALLBACK_DEL,
_win_obj_del, obj);
sd->win = NULL;
}
if (win)
{
sd->win = win;
evas_object_event_callback_add(sd->win, EVAS_CALLBACK_DEL,
_win_obj_del, obj);
}
}
void
termio_theme_set(Evas_Object *obj, Evas_Object *theme)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
if (theme) sd->theme = theme;
}
void
termio_mouseover_suspend_pushpop(Evas_Object *obj, int dir)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
sd->link.suspend += dir;
if (sd->link.suspend < 0) sd->link.suspend = 0;
if (sd->link.suspend)
{
if (sd->anim) ecore_animator_del(sd->anim);
sd->anim = NULL;
}
_smart_update_queue(obj, sd);
}
void
termio_size_get(Evas_Object *obj, int *w, int *h)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
if (w) *w = sd->grid.w;
if (h) *h = sd->grid.h;
}
int
termio_scroll_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, 0);
return sd->scroll;
}
void
termio_scroll_delta(Evas_Object *obj, int delta, int by_page)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
if (by_page)
{
int by = sd->grid.h - 2;
if (by > 1)
delta *= by;
}
sd->scroll += delta;
if (delta <= 0 && sd->scroll < 0)
sd->scroll = 0;
_smart_update_queue(obj, sd);
miniview_position_offset(term_miniview_get(sd->term), -delta, EINA_TRUE);
}
void
termio_scroll_set(Evas_Object *obj, int scroll)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
sd->scroll = scroll;
_remove_links(sd, obj);
_smart_apply(obj);
}
const char *
termio_title_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->pty->prop.title;
}
const char *
termio_icon_name_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->pty->prop.icon;
}
void
termio_media_mute_set(Evas_Object *obj, Eina_Bool mute)
{
Termio *sd = evas_object_smart_data_get(obj);
Eina_List *l;
Termblock *blk;
EINA_SAFETY_ON_NULL_RETURN(sd);
EINA_LIST_FOREACH(sd->pty->block.active, l, blk)
{
if (blk->obj && !blk->edje)
media_mute_set(blk->obj, mute);
}
}
void
termio_media_visualize_set(Evas_Object *obj, Eina_Bool visualize)
{
Termio *sd = evas_object_smart_data_get(obj);
Eina_List *l;
Termblock *blk;
EINA_SAFETY_ON_NULL_RETURN(sd);
EINA_LIST_FOREACH(sd->pty->block.active, l, blk)
{
if (blk->obj && !blk->edje)
media_visualize_set(blk->obj, visualize);
}
}
Eina_Bool
termio_selection_exists(const Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, EINA_FALSE);
return sd->pty->selection.is_active;
}
Termpty *
termio_pty_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->pty;
}
Evas_Object *
termio_miniview_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return term_miniview_get(sd->term);
}
Term*
termio_term_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->term;
}
static void
_font_size_set(Evas_Object *obj, int size)
{
Termio *sd = evas_object_smart_data_get(obj);
Config *config;
EINA_SAFETY_ON_NULL_RETURN(sd);
config = sd->config;
if (size < 5) size = 5;
else if (size > 100) size = 100;
if (config)
{
config->temporary = EINA_TRUE;
config->font.size = size;
sd->noreqsize = 1;
termio_config_update(obj);
sd->noreqsize = 0;
evas_object_data_del(obj, "sizedone");
}
}
void
termio_font_size_set(Evas_Object *obj, int size)
{
_font_size_set(obj, size);
}
void
termio_grid_size_set(Evas_Object *obj, int w, int h)
{
Termio *sd = evas_object_smart_data_get(obj);
Evas_Coord mw = 1, mh = 1;
EINA_SAFETY_ON_NULL_RETURN(sd);
if (w < 1) w = 1;
if (h < 1) h = 1;
evas_object_size_hint_min_get(obj, &mw, &mh);
evas_object_data_del(obj, "sizedone");
evas_object_size_hint_request_set(obj, mw * w, mh * h);
}
pid_t
termio_pid_get(const Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, 0);
return termpty_pid_get(sd->pty);
}
Eina_Bool
termio_cwd_get(const Evas_Object *obj, char *buf, size_t size)
{
Termio *sd = evas_object_smart_data_get(obj);
pid_t pid;
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, EINA_FALSE);
pid = termpty_pid_get(sd->pty);
#if defined (__MacOSX__) || (defined (__MACH__) && defined (__APPLE__))
struct proc_vnodepathinfo vpi;
if (proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &vpi, sizeof(vpi)) <= 0)
{
ERR(_("Could not get working directory of pid %i: %s"),
pid, strerror(errno));
return EINA_FALSE;
}
memcpy(buf, vpi.pvi_cdir.vip_path, size);
#else
char procpath[PATH_MAX];
ssize_t siz;
snprintf(procpath, sizeof(procpath), "/proc/%ld/cwd", (long) pid);
if ((siz = readlink(procpath, buf, size)) < 1)
{
ERR(_("Could not load working directory %s: %s"),
procpath, strerror(errno));
return EINA_FALSE;
}
buf[siz] = 0;
#endif
return EINA_TRUE;
}
Evas_Object *
termio_textgrid_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->grid.obj;
}
Evas_Object *
termio_win_get(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->win;
}
/* }}} */
/* {{{ Config */
void
termio_config_update(Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
Evas_Coord w, h, ow = 0, oh = 0;
char buf[4096];
EINA_SAFETY_ON_NULL_RETURN(sd);
if (sd->font.name) eina_stringshare_del(sd->font.name);
sd->font.name = NULL;
if (sd->config->font.bitmap)
{
snprintf(buf, sizeof(buf), "%s/fonts/%s",
elm_app_data_dir_get(), sd->config->font.name);
sd->font.name = eina_stringshare_add(buf);
}
else
sd->font.name = eina_stringshare_add(sd->config->font.name);
sd->font.size = sd->config->font.size;
sd->jump_on_change = sd->config->jump_on_change;
sd->jump_on_keypress = sd->config->jump_on_keypress;
termpty_backlog_size_set(sd->pty, sd->config->scrollback);
sd->scroll = 0;
if (evas_object_focus_get(obj))
{
edje_object_signal_emit(sd->cursor.obj, "focus,out", "terminology");
if (sd->config->disable_cursor_blink)
edje_object_signal_emit(sd->cursor.obj, "focus,in,noblink", "terminology");
else
edje_object_signal_emit(sd->cursor.obj, "focus,in", "terminology");
}
colors_term_init(sd->grid.obj, sd->theme, sd->config);
evas_object_scale_set(sd->grid.obj, elm_config_scale_get());
evas_object_textgrid_font_set(sd->grid.obj, sd->font.name, sd->font.size);
evas_object_textgrid_cell_size_get(sd->grid.obj, &w, &h);
if (w < 1) w = 1;
if (h < 1) h = 1;
sd->font.chw = w;
sd->font.chh = h;
evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
_smart_size(obj, ow / w, oh / h, EINA_TRUE);
}
Config *
termio_config_get(const Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->config;
}
void
termio_config_set(Evas_Object *obj, Config *config)
{
Termio *sd = evas_object_smart_data_get(obj);
Evas_Coord w = 2, h = 2;
sd->config = config;
sd->jump_on_change = config->jump_on_change;
sd->jump_on_keypress = config->jump_on_keypress;
if (config->font.bitmap)
{
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "%s/fonts/%s",
elm_app_data_dir_get(), config->font.name);
sd->font.name = eina_stringshare_add(buf);
}
else
sd->font.name = eina_stringshare_add(config->font.name);
sd->font.size = config->font.size;
evas_object_scale_set(sd->grid.obj, elm_config_scale_get());
evas_object_textgrid_font_set(sd->grid.obj, sd->font.name, sd->font.size);
evas_object_textgrid_size_get(sd->grid.obj, &w, &h);
if (w < 1) w = 1;
if (h < 1) h = 1;
evas_object_textgrid_size_set(sd->grid.obj, w, h);
evas_object_textgrid_cell_size_get(sd->grid.obj, &w, &h);
if (w < 1) w = 1;
if (h < 1) h = 1;
sd->font.chw = w;
sd->font.chh = h;
theme_apply(sd->cursor.obj, config, "terminology/cursor");
theme_auto_reload_enable(sd->cursor.obj);
evas_object_resize(sd->cursor.obj, sd->font.chw, sd->font.chh);
evas_object_show(sd->cursor.obj);
theme_apply(sd->sel.theme, config, "terminology/selection");
theme_auto_reload_enable(sd->sel.theme);
edje_object_part_swallow(sd->sel.theme, "terminology.top_left", sd->sel.top);
edje_object_part_swallow(sd->sel.theme, "terminology.bottom_right", sd->sel.bottom);
}
/* }}} */
/* {{{ Links */
static Eina_Bool
_should_inline(const Evas_Object *obj)
{
const Config *config = termio_config_get(obj);
const Evas *e;
const Evas_Modifier *mods;
if (!config->helper.inline_please) return EINA_FALSE;
e = evas_object_evas_get(obj);
mods = evas_key_modifier_get(e);
if (evas_key_modifier_is_set(mods, "Control")) return EINA_FALSE;
return EINA_TRUE;
}
const char *
termio_link_get(const Evas_Object *obj)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
return sd->link.string;
}
static void
_activate_link(Evas_Object *obj, Eina_Bool may_inline)
{
Termio *sd = evas_object_smart_data_get(obj);
Config *config;
char buf[PATH_MAX], *s, *escaped;
const char *path = NULL, *cmd = NULL;
Eina_Bool url = EINA_FALSE, email = EINA_FALSE, handled = EINA_FALSE;
int type;
EINA_SAFETY_ON_NULL_RETURN(sd);
config = sd->config;
if (!config) return;
if (!config->active_links) return;
if (!sd->link.string) return;
if (link_is_url(sd->link.string))
{
if (casestartswith(sd->link.string, "file://"))
// TODO: decode string: %XX -> char
path = sd->link.string + sizeof("file://") - 1;
else
url = EINA_TRUE;
}
else if (sd->link.string[0] == '/')
path = sd->link.string;
else if (link_is_email(sd->link.string))
email = EINA_TRUE;
if (url && casestartswith(sd->link.string, "mailto:"))
{
email = EINA_TRUE;
url = EINA_FALSE;
}
s = eina_str_escape(sd->link.string);
if (!s) return;
if (email)
{
const char *p = s;
// run mail client
cmd = "xdg-email";
if ((config->helper.email) &&
(config->helper.email[0]))
cmd = config->helper.email;
if (casestartswith(s, "mailto:"))
p += sizeof("mailto:") - 1;
escaped = ecore_file_escape_name(p);
if (escaped)
{
snprintf(buf, sizeof(buf), "%s %s", cmd, escaped);
free(escaped);
}
}
else if (path)
{
// locally accessible file
cmd = "xdg-open";
escaped = ecore_file_escape_name(s);
if (escaped)
{
type = media_src_type_get(sd->link.string);
if (may_inline && _should_inline(obj))
{
if ((type == MEDIA_TYPE_IMG) ||
(type == MEDIA_TYPE_SCALE) ||
(type == MEDIA_TYPE_EDJE))
{
evas_object_smart_callback_call(obj, "popup", NULL);
handled = EINA_TRUE;
}
else if (type == MEDIA_TYPE_MOV)
{
evas_object_smart_callback_call(obj, "popup", NULL);
handled = EINA_TRUE;
}
}
if (!handled)
{
if ((type == MEDIA_TYPE_IMG) ||
(type == MEDIA_TYPE_SCALE) ||
(type == MEDIA_TYPE_EDJE))
{
if ((config->helper.local.image) &&
(config->helper.local.image[0]))
cmd = config->helper.local.image;
}
else if (type == MEDIA_TYPE_MOV)
{
if ((config->helper.local.video) &&
(config->helper.local.video[0]))
cmd = config->helper.local.video;
}
else
{
if ((config->helper.local.general) &&
(config->helper.local.general[0]))
cmd = config->helper.local.general;
}
snprintf(buf, sizeof(buf), "%s %s", cmd, escaped);
free(escaped);
}
}
}
else if (url)
{
// remote file needs ecore-con-url
cmd = "xdg-open";
escaped = ecore_file_escape_name(s);
if (escaped)
{
type = media_src_type_get(sd->link.string);
if (may_inline && _should_inline(obj))
{
evas_object_smart_callback_call(obj, "popup", NULL);
handled = EINA_TRUE;
}
if (!handled)
{
if ((type == MEDIA_TYPE_IMG) ||
(type == MEDIA_TYPE_SCALE) ||
(type == MEDIA_TYPE_EDJE))
{
if ((config->helper.url.image) &&
(config->helper.url.image[0]))
cmd = config->helper.url.image;
}
else if (type == MEDIA_TYPE_MOV)
{
if ((config->helper.url.video) &&
(config->helper.url.video[0]))
cmd = config->helper.url.video;
}
else
{
if ((config->helper.url.general) &&
(config->helper.url.general[0]))
cmd = config->helper.url.general;
}
snprintf(buf, sizeof(buf), "%s %s", cmd, escaped);
free(escaped);
}
}
}
else
{
free(s);
return;
}
free(s);
if (!handled) ecore_exe_run(buf, NULL);
}
static void
_cb_ctxp_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED,
void *event EINA_UNUSED)
{
Termio *sd = data;
EINA_SAFETY_ON_NULL_RETURN(sd);
sd->ctxpopup = NULL;
}
static void
_cb_ctxp_dismissed(void *data EINA_UNUSED, Evas_Object *obj,
void *event EINA_UNUSED)
{
Termio *sd = data;
EINA_SAFETY_ON_NULL_RETURN(sd);
sd->ctxpopup = NULL;
evas_object_del(obj);
}
static void
_cb_ctxp_link_preview(void *data, Evas_Object *obj, void *event EINA_UNUSED)
{
Evas_Object *term = data;
Termio *sd = evas_object_smart_data_get(term);
EINA_SAFETY_ON_NULL_RETURN(sd);
_activate_link(term, EINA_TRUE);
sd->ctxpopup = NULL;
evas_object_del(obj);
}
static void
_cb_ctxp_link_open(void *data, Evas_Object *obj, void *event EINA_UNUSED)
{
Evas_Object *term = data;
Termio *sd = evas_object_smart_data_get(term);
EINA_SAFETY_ON_NULL_RETURN(sd);
_activate_link(term, EINA_FALSE);
sd->ctxpopup = NULL;
evas_object_del(obj);
}
static void
_cb_ctxp_link_copy(void *data, Evas_Object *obj, void *event EINA_UNUSED)
{
Evas_Object *term = data;
Termio *sd = evas_object_smart_data_get(term);
EINA_SAFETY_ON_NULL_RETURN(sd);
EINA_SAFETY_ON_NULL_RETURN(sd->link.string);
_take_selection_text(sd, ELM_SEL_TYPE_CLIPBOARD, sd->link.string);
sd->ctxpopup = NULL;
evas_object_del(obj);
}
static void
_cb_link_down(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event)
{
Evas_Event_Mouse_Down *ev = event;
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
if (ev->button == 1)
{
sd->link.down.down = EINA_TRUE;
sd->link.down.x = ev->canvas.x;
sd->link.down.y = ev->canvas.y;
}
else if (ev->button == 3)
{
Evas_Object *ctxp;
if (sd->pty->selection.is_active)
{
int cx = 0, cy = 0;
_smart_xy_to_cursor(sd, ev->canvas.x, ev->canvas.y, &cx, &cy);
if (_mouse_in_selection(sd, cx, cy))
return;
}
ctxp = elm_ctxpopup_add(sd->win);
sd->ctxpopup = ctxp;
if (sd->config->helper.inline_please)
{
int type = media_src_type_get(sd->link.string);
if ((type == MEDIA_TYPE_IMG) ||
(type == MEDIA_TYPE_SCALE) ||
(type == MEDIA_TYPE_EDJE) ||
(type == MEDIA_TYPE_MOV))
elm_ctxpopup_item_append(ctxp, _("Preview"), NULL,
_cb_ctxp_link_preview, sd->self);
}
elm_ctxpopup_item_append(ctxp, _("Open"), NULL, _cb_ctxp_link_open,
sd->self);
elm_ctxpopup_item_append(ctxp, _("Copy"), NULL, _cb_ctxp_link_copy,
sd->self);
evas_object_move(ctxp, ev->canvas.x, ev->canvas.y);
evas_object_show(ctxp);
evas_object_smart_callback_add(ctxp, "dismissed",
_cb_ctxp_dismissed, sd);
evas_object_event_callback_add(ctxp, EVAS_CALLBACK_DEL,
_cb_ctxp_del, sd);
}
}
static Eina_Bool
_cb_link_up_delay(void *data)
{
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, EINA_FALSE);
sd->link_do_timer = NULL;
if (!sd->didclick) _activate_link(data, EINA_TRUE);
sd->didclick = EINA_FALSE;
return EINA_FALSE;
}
static void
_cb_link_up(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event)
{
Evas_Event_Mouse_Up *ev = event;
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
if ((ev->button == 1) && (sd->link.down.down))
{
Evas_Coord dx, dy, finger_size;
dx = abs(ev->canvas.x - sd->link.down.x);
dy = abs(ev->canvas.y - sd->link.down.y);
finger_size = elm_config_finger_size_get();
if ((dx <= finger_size) && (dy <= finger_size))
{
if (sd->link_do_timer)
ecore_timer_reset(sd->link_do_timer);
else
sd->link_do_timer = ecore_timer_add(0.2, _cb_link_up_delay, data);
}
sd->link.down.down = EINA_FALSE;
}
}
#if !((ELM_VERSION_MAJOR == 1) && (ELM_VERSION_MINOR < 8))
static void
_cb_link_drag_move(void *data, Evas_Object *obj, Evas_Coord x, Evas_Coord y, Elm_Xdnd_Action action)
{
const Evas_Modifier *em = NULL;
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
DBG("dnd %i %i act %i", x, y, action);
em = evas_key_modifier_get(evas_object_evas_get(sd->event));
if (em)
{
if (evas_key_modifier_is_set(em, "Control"))
elm_drag_action_set(obj, ELM_XDND_ACTION_COPY);
else
elm_drag_action_set(obj, ELM_XDND_ACTION_MOVE);
}
}
static void
_cb_link_drag_accept(void *data, Evas_Object *obj EINA_UNUSED, Eina_Bool doaccept)
{
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
DBG("dnd accept: %i", doaccept);
}
static void
_cb_link_drag_done(void *data, Evas_Object *obj EINA_UNUSED)
{
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
DBG("dnd done");
sd->link.down.dnd = EINA_FALSE;
if ((sd->link.down.dndobjdel) && (sd->link.down.dndobj))
evas_object_del(sd->link.down.dndobj);
sd->link.down.dndobj = NULL;
}
static Evas_Object *
_cb_link_icon_new(void *data, Evas_Object *par, Evas_Coord *xoff, Evas_Coord *yoff)
{
Evas_Object *icon;
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
icon = elm_button_add(par);
elm_object_text_set(icon, sd->link.string);
*xoff = 0;
*yoff = 0;
return icon;
}
#endif
static void
_cb_link_move(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj
#if ((ELM_VERSION_MAJOR == 1) && (ELM_VERSION_MINOR < 8))
EINA_UNUSED
#endif
, void *event)
{
Evas_Event_Mouse_Move *ev = event;
Termio *sd = evas_object_smart_data_get(data);
Evas_Coord dx, dy;
EINA_SAFETY_ON_NULL_RETURN(sd);
if (!sd->link.down.down) return;
dx = abs(ev->cur.canvas.x - sd->link.down.x);
dy = abs(ev->cur.canvas.y - sd->link.down.y);
if ((sd->config->drag_links) &&
(sd->link.string) &&
((dx > elm_config_finger_size_get()) ||
(dy > elm_config_finger_size_get())))
{
sd->link.down.down = EINA_FALSE;
sd->link.down.dnd = EINA_TRUE;
#if !((ELM_VERSION_MAJOR == 1) && (ELM_VERSION_MINOR < 8))
DBG("dnd start %s %i %i", sd->link.string,
evas_key_modifier_is_set(ev->modifiers, "Control"),
evas_key_modifier_is_set(ev->modifiers, "Shift"));
if (evas_key_modifier_is_set(ev->modifiers, "Control"))
elm_drag_start(obj, ELM_SEL_FORMAT_IMAGE, sd->link.string,
ELM_XDND_ACTION_COPY,
_cb_link_icon_new, data,
_cb_link_drag_move, data,
_cb_link_drag_accept, data,
_cb_link_drag_done, data);
else
elm_drag_start(obj, ELM_SEL_FORMAT_IMAGE, sd->link.string,
ELM_XDND_ACTION_MOVE,
_cb_link_icon_new, data,
_cb_link_drag_move, data,
_cb_link_drag_accept, data,
_cb_link_drag_done, data);
sd->link.down.dndobj = obj;
sd->link.down.dndobjdel = EINA_FALSE;
#endif
}
}
static void
_update_link(Evas_Object *obj, Termio *sd,
Eina_Bool same_link, Eina_Bool same_geom)
{
Evas_Coord ox, oy, ow, oh;
Evas_Object *o;
Eina_Bool popup_exists;
int y;
EINA_SAFETY_ON_NULL_RETURN(sd);
if (!same_link)
{
// check link and re-probe/fetch create popup preview
}
if (same_geom)
return;
if (sd->link.suspend)
return;
evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
EINA_LIST_FREE(sd->link.objs, o)
{
if (sd->link.down.dndobj == o)
{
sd->link.down.dndobjdel = EINA_TRUE;
evas_object_hide(o);
}
else
evas_object_del(o);
}
if (!sd->link.string)
return;
popup_exists = main_term_popup_exists(sd->term);
if ((!popup_exists) &&
((sd->link.string[0] == '/') || (link_is_url(sd->link.string))))
{
Evas_Coord _x = ox, _y = oy;
uint64_t xwin;
_x += sd->mouse.cx * sd->font.chw;
_y += sd->mouse.cy * sd->font.chh;
#if (ELM_VERSION_MAJOR > 1) || (ELM_VERSION_MINOR >= 8)
xwin = elm_win_window_id_get(sd->win);
# if (ELM_VERSION_MAJOR > 1) || ((ELM_VERSION_MAJOR == 1) && (ELM_VERSION_MINOR > 8)) // not a typo
if (strstr(ecore_evas_engine_name_get(ecore_evas_ecore_evas_get(evas_object_evas_get(sd->win))), "wayland"))
xwin = ((uint64_t)xwin << 32) + (uint64_t)getpid();
# endif
#else
xwin = elm_win_xwindow_get(sd->win);
#endif
ty_dbus_link_mousein(xwin, sd->link.string, _x, _y);
}
for (y = sd->link.y1; y <= sd->link.y2; y++)
{
o = elm_layout_add(obj);
evas_object_smart_member_add(o, obj);
theme_apply(elm_layout_edje_get(o), sd->config, "terminology/link");
if (y == sd->link.y1)
{
evas_object_move(o, ox + (sd->link.x1 * sd->font.chw),
oy + (y * sd->font.chh));
if (sd->link.y1 == sd->link.y2)
evas_object_resize(o,
((sd->link.x2 - sd->link.x1 + 1) * sd->font.chw),
sd->font.chh);
else
evas_object_resize(o,
((sd->grid.w - sd->link.x1) * sd->font.chw),
sd->font.chh);
}
else if (y == sd->link.y2)
{
evas_object_move(o, ox, oy + (y * sd->font.chh));
evas_object_resize(o,
((sd->link.x2 + 1) * sd->font.chw),
sd->font.chh);
}
else
{
evas_object_move(o, ox, oy + (y * sd->font.chh));
evas_object_resize(o, (sd->grid.w * sd->font.chw),
sd->font.chh);
}
sd->link.objs = eina_list_append(sd->link.objs, o);
elm_object_cursor_set(o, "hand2");
evas_object_show(o);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN,
_cb_link_down, obj);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP,
_cb_link_up, obj);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_MOVE,
_cb_link_move, obj);
if ((!popup_exists) && link_is_email(sd->link.string))
{
gravatar_tooltip(o, sd->config, sd->link.string);
}
}
}
static void
_remove_links(Termio *sd, Evas_Object *obj)
{
Eina_Bool same_link = EINA_FALSE, same_geom = EINA_FALSE;
if (sd->link.string)
{
if ((sd->link.string[0] == '/') || (link_is_url(sd->link.string)))
{
Evas_Coord ox, oy;
uint64_t xwin;
evas_object_geometry_get(obj, &ox, &oy, NULL, NULL);
ox += sd->mouse.cx * sd->font.chw;
oy += sd->mouse.cy * sd->font.chh;
#if (ELM_VERSION_MAJOR > 1) || (ELM_VERSION_MINOR >= 8)
xwin = elm_win_window_id_get(sd->win);
# if (ELM_VERSION_MAJOR > 1) || ((ELM_VERSION_MAJOR == 1) && (ELM_VERSION_MINOR > 8)) // not a typo
if (strstr(ecore_evas_engine_name_get(ecore_evas_ecore_evas_get(evas_object_evas_get(sd->win))), "wayland"))
xwin = ((uint64_t)xwin << 32) + (uint64_t)getpid();
# endif
#else
xwin = elm_win_xwindow_get(sd->win);
#endif
ty_dbus_link_mouseout(xwin, sd->link.string, ox, oy);
}
free(sd->link.string);
sd->link.string = NULL;
}
sd->link.x1 = -1;
sd->link.y1 = -1;
sd->link.x2 = -1;
sd->link.y2 = -1;
sd->link.suspend = EINA_FALSE;
_update_link(obj, sd, same_link, same_geom);
}
/* }}} */
/* {{{ Blocks */
static void
_smart_media_clicked(void *data, Evas_Object *obj, void *info EINA_UNUSED)
{
// Termio *sd = evas_object_smart_data_get(data);
Termblock *blk;
const char *file = media_get(obj);
if (!file) return;
blk = evas_object_data_get(obj, "blk");
if (blk)
{
if (blk->link)
{
int type = media_src_type_get(blk->link);
Config *config = termio_config_get(data);
if (config)
{
if ((!config->helper.inline_please) ||
(!((type == MEDIA_TYPE_IMG) || (type == MEDIA_TYPE_SCALE) ||
(type == MEDIA_TYPE_EDJE) || (type == MEDIA_TYPE_MOV))))
{
const char *cmd = NULL;
file = blk->link;
if ((config->helper.local.general) &&
(config->helper.local.general[0]))
cmd = config->helper.local.general;
if (cmd)
{
char buf[PATH_MAX], *escaped;
escaped = ecore_file_escape_name(file);
if (escaped)
{
snprintf(buf, sizeof(buf), "%s %s", cmd, escaped);
ecore_exe_run(buf, NULL);
free(escaped);
}
return;
}
}
file = blk->link;
}
}
}
evas_object_smart_callback_call(data, "popup", (void *)file);
}
static void
_smart_media_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *info EINA_UNUSED)
{
Termblock *blk = data;
if (blk->obj == obj) blk->obj = NULL;
}
static void
_smart_media_play(void *data, Evas_Object *obj, void *info EINA_UNUSED)
{
Termblock *blk = data;
if (blk->obj != obj) return;
blk->mov_state = MOVIE_STATE_PLAY;
}
static void
_smart_media_pause(void *data, Evas_Object *obj, void *info EINA_UNUSED)
{
Termblock *blk = data;
if (blk->obj != obj) return;
blk->mov_state = MOVIE_STATE_PAUSE;
}
static void
_smart_media_stop(void *data, Evas_Object *obj, void *info EINA_UNUSED)
{
Termblock *blk = data;
if (blk->obj != obj) return;
blk->mov_state = MOVIE_STATE_STOP;
}
static void
_block_edje_signal_cb(void *data, Evas_Object *obj EINA_UNUSED, const char *sig, const char *src)
{
Termblock *blk = data;
Termio *sd = evas_object_smart_data_get(blk->pty->obj);
char *buf = NULL, *chid = NULL;
int buflen = 0;
Eina_List *l;
EINA_SAFETY_ON_NULL_RETURN(sd);
if ((!blk->chid) || (!sd->cur_chids)) return;
EINA_LIST_FOREACH(sd->cur_chids, l, chid)
{
if (!(!strcmp(blk->chid, chid))) break;
chid = NULL;
}
if (!chid) return;
if ((!strcmp(sig, "drag")) ||
(!strcmp(sig, "drag,start")) ||
(!strcmp(sig, "drag,stop")) ||
(!strcmp(sig, "drag,step")) ||
(!strcmp(sig, "drag,set")))
{
int v1, v2;
double f1 = 0.0, f2 = 0.0;
edje_object_part_drag_value_get(blk->obj, src, &f1, &f2);
v1 = (int)(f1 * 1000.0);
v2 = (int)(f2 * 1000.0);
buf = alloca(strlen(src) + strlen(blk->chid) + 256);
buflen = sprintf(buf, "%c};%s\n%s\n%s\n%i\n%i", 0x1b,
blk->chid, sig, src, v1, v2);
termpty_write(sd->pty, buf, buflen + 1);
}
else
{
buf = alloca(strlen(sig) + strlen(src) + strlen(blk->chid) + 128);
buflen = sprintf(buf, "%c}signal;%s\n%s\n%s", 0x1b,
blk->chid, sig, src);
termpty_write(sd->pty, buf, buflen + 1);
}
}
static void
_block_edje_message_cb(void *data, Evas_Object *obj EINA_UNUSED, Edje_Message_Type type, int id, void *msg)
{
Termblock *blk = data;
Termio *sd = evas_object_smart_data_get(blk->pty->obj);
char *chid = NULL, buf[4096];
Eina_List *l;
int buflen;
EINA_SAFETY_ON_NULL_RETURN(sd);
if ((!blk->chid) || (!sd->cur_chids)) return;
EINA_LIST_FOREACH(sd->cur_chids, l, chid)
{
if (!(!strcmp(blk->chid, chid))) break;
chid = NULL;
}
if (!chid) return;
switch (type)
{
case EDJE_MESSAGE_STRING:
{
Edje_Message_String *m = msg;
buflen = sprintf(buf, "%c}message;%s\n%i\nstring\n%s", 0x1b,
blk->chid, id, m->str);
termpty_write(sd->pty, buf, buflen + 1);
}
break;
case EDJE_MESSAGE_INT:
{
Edje_Message_Int *m = msg;
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nint\n%i", 0x1b,
blk->chid, id, m->val);
termpty_write(sd->pty, buf, buflen + 1);
}
break;
case EDJE_MESSAGE_FLOAT:
{
Edje_Message_Float *m = msg;
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nfloat\n%i", 0x1b,
blk->chid, id, (int)(m->val * 1000.0));
termpty_write(sd->pty, buf, buflen + 1);
}
break;
case EDJE_MESSAGE_STRING_SET:
{
Edje_Message_String_Set *m = msg;
int i;
char zero[1] = { 0 };
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nstring_set\n%i", 0x1b,
blk->chid, id, m->count);
termpty_write(sd->pty, buf, buflen);
for (i = 0; i < m->count; i++)
{
termpty_write(sd->pty, "\n", 1);
termpty_write(sd->pty, m->str[i], strlen(m->str[i]));
}
termpty_write(sd->pty, zero, 1);
}
break;
case EDJE_MESSAGE_INT_SET:
{
Edje_Message_Int_Set *m = msg;
int i;
char zero[1] = { 0 };
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nint_set\n%i", 0x1b,
blk->chid, id, m->count);
termpty_write(sd->pty, buf, buflen);
for (i = 0; i < m->count; i++)
{
termpty_write(sd->pty, "\n", 1);
buflen = snprintf(buf, sizeof(buf), "%i", m->val[i]);
termpty_write(sd->pty, buf, buflen);
}
termpty_write(sd->pty, zero, 1);
}
break;
case EDJE_MESSAGE_FLOAT_SET:
{
Edje_Message_Float_Set *m = msg;
int i;
char zero[1] = { 0 };
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nfloat_set\n%i", 0x1b,
blk->chid, id, m->count);
termpty_write(sd->pty, buf, buflen);
for (i = 0; i < m->count; i++)
{
termpty_write(sd->pty, "\n", 1);
buflen = snprintf(buf, sizeof(buf), "%i", (int)(m->val[i] * 1000.0));
termpty_write(sd->pty, buf, buflen);
}
termpty_write(sd->pty, zero, 1);
}
break;
case EDJE_MESSAGE_STRING_INT:
{
Edje_Message_String_Int *m = msg;
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nstring_int\n%s\n%i", 0x1b,
blk->chid, id, m->str, m->val);
termpty_write(sd->pty, buf, buflen + 1);
}
break;
case EDJE_MESSAGE_STRING_FLOAT:
{
Edje_Message_String_Float *m = msg;
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nstring_float\n%s\n%i", 0x1b,
blk->chid, id, m->str, (int)(m->val * 1000.0));
termpty_write(sd->pty, buf, buflen + 1);
}
break;
case EDJE_MESSAGE_STRING_INT_SET:
{
Edje_Message_String_Int_Set *m = msg;
int i;
char zero[1] = { 0 };
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nstring_int_set\n%i", 0x1b,
blk->chid, id, m->count);
termpty_write(sd->pty, buf, buflen);
termpty_write(sd->pty, "\n", 1);
termpty_write(sd->pty, m->str, strlen(m->str));
for (i = 0; i < m->count; i++)
{
termpty_write(sd->pty, "\n", 1);
buflen = snprintf(buf, sizeof(buf), "%i", m->val[i]);
termpty_write(sd->pty, buf, buflen);
}
termpty_write(sd->pty, zero, 1);
}
break;
case EDJE_MESSAGE_STRING_FLOAT_SET:
{
Edje_Message_String_Float_Set *m = msg;
int i;
char zero[1] = { 0 };
buflen = snprintf(buf, sizeof(buf),
"%c}message;%s\n%i\nstring_float_set\n%i", 0x1b,
blk->chid, id, m->count);
termpty_write(sd->pty, buf, buflen);
termpty_write(sd->pty, "\n", 1);
termpty_write(sd->pty, m->str, strlen(m->str));
for (i = 0; i < m->count; i++)
{
termpty_write(sd->pty, "\n", 1);
buflen = snprintf(buf, sizeof(buf), "%i", (int)(m->val[i] * 1000.0));
termpty_write(sd->pty, buf, buflen);
}
termpty_write(sd->pty, zero, 1);
}
break;
default:
break;
}
}
static void
_block_edje_cmds(Termpty *ty, Termblock *blk, Eina_List *cmds, Eina_Bool created)
{
Eina_List *l;
char *s;
#define ISCMD(cmd) !strcmp(s, cmd)
#define GETS(var) l = l->next; if (!l) return; var = l->data
#define GETI(var) l = l->next; if (!l) return; var = atoi(l->data)
#define GETF(var) l = l->next; if (!l) return; var = (double)atoi(l->data) / 1000.0
l = cmds;
while (l)
{
s = l->data;
/////////////////////////////////////////////////////////////////////
if (ISCMD("text")) // set text part
{
char *prt, *txt;
GETS(prt);
GETS(txt);
edje_object_part_text_set(blk->obj, prt, txt);
}
/////////////////////////////////////////////////////////////////////
else if (ISCMD("emit")) // emit signal
{
char *sig, *src;
GETS(sig);
GETS(src);
edje_object_signal_emit(blk->obj, sig, src);
}
/////////////////////////////////////////////////////////////////////
else if (ISCMD("drag")) // set dragable
{
char *prt, *val;
double v1, v2;
GETS(prt);
GETS(val);
GETF(v1);
GETF(v2);
if (!strcmp(val, "value"))
edje_object_part_drag_value_set(blk->obj, prt, v1, v2);
else if (!strcmp(val, "size"))
edje_object_part_drag_size_set(blk->obj, prt, v1, v2);
else if (!strcmp(val, "step"))
edje_object_part_drag_step_set(blk->obj, prt, v1, v2);
else if (!strcmp(val, "page"))
edje_object_part_drag_page_set(blk->obj, prt, v1, v2);
}
/////////////////////////////////////////////////////////////////////
else if (ISCMD("message")) // send message
{
int id;
char *typ;
GETI(id);
GETS(typ);
if (!strcmp(typ, "string"))
{
Edje_Message_String *m;
m = alloca(sizeof(Edje_Message_String));
GETS(m->str);
edje_object_message_send(blk->obj, EDJE_MESSAGE_STRING,
id, m);
}
else if (!strcmp(typ, "int"))
{
Edje_Message_Int *m;
m = alloca(sizeof(Edje_Message_Int));
GETI(m->val);
edje_object_message_send(blk->obj, EDJE_MESSAGE_INT,
id, m);
}
else if (!strcmp(typ, "float"))
{
Edje_Message_Float *m;
m = alloca(sizeof(Edje_Message_Float));
GETF(m->val);
edje_object_message_send(blk->obj, EDJE_MESSAGE_FLOAT,
id, m);
}
else if (!strcmp(typ, "string_set"))
{
Edje_Message_String_Set *m;
int i, count;
GETI(count);
m = alloca(sizeof(Edje_Message_String_Set) +
((count - 1) * sizeof(char *)));
m->count = count;
for (i = 0; i < m->count; i++)
{
GETS(m->str[i]);
}
edje_object_message_send(blk->obj,
EDJE_MESSAGE_STRING_SET,
id, m);
}
else if (!strcmp(typ, "int_set"))
{
Edje_Message_Int_Set *m;
int i, count;
GETI(count);
m = alloca(sizeof(Edje_Message_Int_Set) +
((count - 1) * sizeof(int)));
m->count = count;
for (i = 0; i < m->count; i++)
{
GETI(m->val[i]);
}
edje_object_message_send(blk->obj,
EDJE_MESSAGE_INT_SET,
id, m);
}
else if (!strcmp(typ, "float_set"))
{
Edje_Message_Float_Set *m;
int i, count;
GETI(count);
m = alloca(sizeof(Edje_Message_Float_Set) +
((count - 1) * sizeof(double)));
m->count = count;
for (i = 0; i < m->count; i++)
{
GETF(m->val[i]);
}
edje_object_message_send(blk->obj,
EDJE_MESSAGE_FLOAT_SET,
id, m);
}
else if (!strcmp(typ, "string_int"))
{
Edje_Message_String_Int *m;
m = alloca(sizeof(Edje_Message_String_Int));
GETS(m->str);
GETI(m->val);
edje_object_message_send(blk->obj, EDJE_MESSAGE_STRING_INT,
id, m);
}
else if (!strcmp(typ, "string_float"))
{
Edje_Message_String_Float *m;
m = alloca(sizeof(Edje_Message_String_Float));
GETS(m->str);
GETF(m->val);
edje_object_message_send(blk->obj, EDJE_MESSAGE_STRING_FLOAT,
id, m);
}
else if (!strcmp(typ, "string_int_set"))
{
Edje_Message_String_Int_Set *m;
int i, count;
GETI(count);
m = alloca(sizeof(Edje_Message_String_Int_Set) +
((count - 1) * sizeof(int)));
GETS(m->str);
m->count = count;
for (i = 0; i < m->count; i++)
{
GETI(m->val[i]);
}
edje_object_message_send(blk->obj,
EDJE_MESSAGE_STRING_INT_SET,
id, m);
}
else if (!strcmp(typ, "string_float_set"))
{
Edje_Message_String_Float_Set *m;
int i, count;
GETI(count);
m = alloca(sizeof(Edje_Message_String_Float_Set) +
((count - 1) * sizeof(double)));
GETS(m->str);
m->count = count;
for (i = 0; i < m->count; i++)
{
GETF(m->val[i]);
}
edje_object_message_send(blk->obj,
EDJE_MESSAGE_STRING_FLOAT_SET,
id, m);
}
}
/////////////////////////////////////////////////////////////////////
else if (ISCMD("chid")) // set callback channel id
{
char *chid;
GETS(chid);
if (!blk->chid)
{
blk->chid = eina_stringshare_add(chid);
termpty_block_chid_update(ty, blk);
}
if (created)
{
edje_object_signal_callback_add(blk->obj, "*", "*",
_block_edje_signal_cb,
blk);
edje_object_message_handler_set(blk->obj,
_block_edje_message_cb,
blk);
}
}
if (l) l = l->next;
}
}
static void
_block_edje_activate(Evas_Object *obj, Termblock *blk)
{
Termio *sd = evas_object_smart_data_get(obj);
Eina_Bool ok = EINA_FALSE;
EINA_SAFETY_ON_NULL_RETURN(sd);
if ((!blk->path) || (!blk->link)) return;
blk->obj = edje_object_add(evas_object_evas_get(obj));
if (blk->path[0] == '/')
{
ok = edje_object_file_set(blk->obj, blk->path, blk->link);
}
else if (!strcmp(blk->path, "THEME"))
{
ok = edje_object_file_set(blk->obj,
config_theme_path_default_get
(sd->config),
blk->link);
}
else
{
char path[PATH_MAX], home[PATH_MAX];
if (homedir_get(home, sizeof(home)))
{
snprintf(path, sizeof(path), "%s/.terminology/objlib/%s",
home, blk->path);
ok = edje_object_file_set(blk->obj, path, blk->link);
}
if (!ok)
{
snprintf(path, sizeof(path), "%s/objlib/%s",
elm_app_data_dir_get(), blk->path);
ok = edje_object_file_set(blk->obj, path, blk->link);
}
}
evas_object_smart_member_add(blk->obj, obj);
evas_object_stack_above(blk->obj, sd->event);
evas_object_show(blk->obj);
evas_object_data_set(blk->obj, "blk", blk);
if (ok)
{
_block_edje_cmds(sd->pty, blk, blk->cmds, EINA_TRUE);
}
else
{
ERR("failed to activate textblock of id %d", blk->id);
}
}
static void
_block_media_activate(Evas_Object *obj, Termblock *blk)
{
Termio *sd = evas_object_smart_data_get(obj);
Media_Type type;
int media = MEDIA_STRETCH;
Evas_Object *mctrl;
EINA_SAFETY_ON_NULL_RETURN(sd);
if (blk->scale_stretch)
media = MEDIA_STRETCH;
else if (blk->scale_center)
media = MEDIA_POP;
else if (blk->scale_fill)
media = MEDIA_BG;
else if (blk->thumb)
media = MEDIA_THUMB;
if (!blk->was_active_before || blk->mov_state == MOVIE_STATE_STOP)
media |= MEDIA_SAVE;
else
media |= MEDIA_RECOVER | MEDIA_SAVE;
type = media_src_type_get(blk->path);
blk->obj = media_add(obj, blk->path, sd->config, media, type);
if (type == MEDIA_TYPE_MOV)
media_play_set(blk->obj, blk->mov_state == MOVIE_STATE_PLAY);
evas_object_event_callback_add
(blk->obj, EVAS_CALLBACK_DEL, _smart_media_del, blk);
evas_object_smart_callback_add(blk->obj, "play", _smart_media_play, blk);
evas_object_smart_callback_add(blk->obj, "pause", _smart_media_pause, blk);
evas_object_smart_callback_add(blk->obj, "stop", _smart_media_stop, blk);
blk->type = type;
evas_object_smart_member_add(blk->obj, obj);
mctrl = media_control_get(blk->obj);
if (mctrl)
{
evas_object_smart_member_add(mctrl, obj);
evas_object_stack_above(mctrl, sd->event);
}
evas_object_stack_above(blk->obj, sd->grid.obj);
evas_object_show(blk->obj);
evas_object_data_set(blk->obj, "blk", blk);
if (blk->thumb)
{
evas_object_smart_callback_add(blk->obj, "clicked",
_smart_media_clicked, obj);
}
}
static void
_block_activate(Evas_Object *obj, Termblock *blk)
{
Termio *sd = evas_object_smart_data_get(obj);
EINA_SAFETY_ON_NULL_RETURN(sd);
if (blk->active)
return;
blk->active = EINA_TRUE;
if (blk->obj)
return;
if (blk->edje)
_block_edje_activate(obj, blk);
else
_block_media_activate(obj, blk);
blk->was_active_before = EINA_TRUE;
if (!blk->was_active)
sd->pty->block.active = eina_list_append(sd->pty->block.active, blk);
}
static void
_block_obj_del(Termblock *blk)
{
if (!blk->obj) return;
evas_object_event_callback_del_full
(blk->obj, EVAS_CALLBACK_DEL,
_smart_media_del, blk);
evas_object_del(blk->obj);
blk->obj = NULL;
}
/* }}} */
/* {{{ Keys */
static void
_smart_cb_key_up(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event)
{
Evas_Event_Key_Up *ev = event;
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
keyin_handle_up(&sd->khdl, ev);
}
static void
_smart_cb_key_down(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event)
{
const Evas_Event_Key_Down *ev = event;
Termio *sd = evas_object_smart_data_get(data);
int ctrl, alt, shift, win, meta, hyper;
EINA_SAFETY_ON_NULL_RETURN(sd);
EINA_SAFETY_ON_NULL_RETURN(ev->key);
if (miniview_handle_key(term_miniview_get(sd->term), ev))
return;
if (term_has_popmedia(sd->term) && !strcmp(ev->key, "Escape"))
{
term_popmedia_close(sd->term);
return;
}
ctrl = evas_key_modifier_is_set(ev->modifiers, "Control");
alt = evas_key_modifier_is_set(ev->modifiers, "Alt");
shift = evas_key_modifier_is_set(ev->modifiers, "Shift");
win = evas_key_modifier_is_set(ev->modifiers, "Super");
meta = evas_key_modifier_is_set(ev->modifiers, "Meta") ||
evas_key_modifier_is_set(ev->modifiers, "AltGr") ||
evas_key_modifier_is_set(ev->modifiers, "ISO_Level3_Shift");
hyper = evas_key_modifier_is_set(ev->modifiers, "Hyper");
if (keyin_handle(&sd->khdl, sd->pty, ev, ctrl, alt, shift, win, meta, hyper))
goto end;
if (sd->jump_on_keypress)
{
if (!key_is_modifier(ev->key))
{
sd->scroll = 0;
_smart_update_queue(data, sd);
}
}
end:
if (sd->config->flicker_on_key)
edje_object_signal_emit(sd->cursor.obj, "key,down", "terminology");
}
/* }}} */
/* {{{ Selection */
static Eina_Bool
_mouse_in_selection(Termio *sd, int cx, int cy)
{
int start_x = 0, start_y = 0, end_x = 0, end_y = 0;
if (!sd->pty->selection.is_active)
return EINA_FALSE;
start_x = sd->pty->selection.start.x;
start_y = sd->pty->selection.start.y;
end_x = sd->pty->selection.end.x;
end_y = sd->pty->selection.end.y;
if (!sd->pty->selection.is_top_to_bottom)
{
INT_SWAP(start_y, end_y);
INT_SWAP(start_x, end_x);
}
if (sd->pty->selection.is_box)
{
if ((start_y <= cy) && (cy <= end_y) &&
(start_x <= cx) && (cx <= end_x) )
return EINA_TRUE;
}
else
{
if ((cy < start_y) || (cy > end_y))
return EINA_FALSE;
if (((cy == start_y) && (cx < start_x)) ||
((cy == end_y) && (cx > end_x)))
return EINA_FALSE;
return EINA_TRUE;
}
return EINA_FALSE;
}
struct termio_sb {
char *buf;
size_t len;
size_t alloc;
};
static int
_sb_add(struct termio_sb *sb, const char *s, size_t len)
{
size_t new_len = sb->len + len;
if ((new_len >= sb->alloc) || !sb->buf)
{
size_t new_alloc = ((new_len + 15) / 16) * 24;
char *new_buf;
new_buf = realloc(sb->buf, new_alloc);
if (new_buf == NULL)
return -1;
sb->buf = new_buf;
sb->alloc = new_alloc;
}
memcpy(sb->buf + sb->len, s, len);
sb->len += len;
sb->buf[sb->len] = '\0';
return 0;
}
/* unlike eina_strbuf_rtrim, only trims \t, \f, ' ' */
static void
_sb_spaces_rtrim(struct termio_sb *sb)
{
if (!sb->buf)
return;
while (sb->len > 0)
{
char c = sb->buf[sb->len - 1];
if ((c != ' ') && (c != '\t') && (c != '\f'))
break;
sb->len--;
}
sb->buf[sb->len] = '\0';
}
char *
termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y,
size_t *lenp,
Eina_Bool rtrim)
{
Termio *sd = evas_object_smart_data_get(obj);
struct termio_sb sb = {.buf = NULL, .len = 0, .alloc = 0};
int x, y;
EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL);
termpty_backlog_lock();
for (y = c1y; y <= c2y; y++)
{
Termcell *cells;
ssize_t w;
int last0, v, start_x, end_x;
w = 0;
last0 = -1;
cells = termpty_cellrow_get(sd->pty, y, &w);
if (!cells || !w)
{
if (_sb_add(&sb, "\n", 1) < 0) goto err;
continue;
}
if (w > sd->grid.w) w = sd->grid.w;
if (y == c1y && c1x >= w)
{
if (rtrim)
_sb_spaces_rtrim(&sb);
if (_sb_add(&sb, "\n", 1) < 0) goto err;
continue;
}
start_x = c1x;
end_x = (c2x >= w) ? w - 1 : c2x;
if (c1y != c2y)
{
if (y == c1y) end_x = w - 1;
else if (y == c2y) start_x = 0;
else
{
start_x = 0;
end_x = w - 1;
}
}
for (x = start_x; x <= end_x; x++)
{
#if defined(SUPPORT_DBLWIDTH)
if ((cells[x].codepoint == 0) && (cells[x].att.dblwidth))
{
if (x < end_x) x++;
else break;
}
#endif
if (x >= w) break;
if (cells[x].att.newline)
{
last0 = -1;
if ((y != c2y) || (x != end_x))
{
if (rtrim)
_sb_spaces_rtrim(&sb);
if (_sb_add(&sb, "\n", 1) < 0) goto err;
}
break;
}
else if (cells[x].att.tab)
{
if (_sb_add(&sb, "\t", 1) < 0) goto err;
x = ((x + 8) / 8) * 8;
x--; /* counter the ++ of the for loop */
}
else if (cells[x].codepoint == 0)
{
if (last0 < 0) last0 = x;
}
else
{
char txt[8];
int txtlen;
if (last0 >= 0)
{
v = x - last0 - 1;
last0 = -1;
while (v >= 0)
{
if (_sb_add(&sb, " ", 1) < 0) goto err;
v--;
}
}
txtlen = codepoint_to_utf8(cells[x].codepoint, txt);
if (txtlen > 0)
if (_sb_add(&sb, txt, txtlen) < 0) goto err;
if ((x == (w - 1)) &&
((x != c2x) || (y != c2y)))
{
if (!cells[x].att.autowrapped)
{
if (rtrim)
_sb_spaces_rtrim(&sb);
if (_sb_add(&sb, "\n", 1) < 0) goto err;
}
}
}
}
if (last0 >= 0)
{
if (y == c2y)
{
Eina_Bool have_more = EINA_FALSE;
for (x = end_x + 1; x < w; x++)
{
#if defined(SUPPORT_DBLWIDTH)
if ((cells[x].codepoint == 0) &&
(cells[x].att.dblwidth))
{
if (x < (w - 1)) x++;
else break;
}
#endif
if (((cells[x].codepoint != 0) &&
(cells[x].codepoint != ' ')) ||
(cells[x].att.newline) ||
(cells[x].att.tab))
{
have_more = EINA_TRUE;
break;
}
}
if (!have_more)
{
if (rtrim)
_sb_spaces_rtrim(&sb);
if (_sb_add(&sb, "\n", 1) < 0) goto err;
}
else
{
for (x = last0; x <= end_x; x++)
{
#if defined(SUPPORT_DBLWIDTH)
if ((cells[x].codepoint == 0) &&
(cells[x].att.dblwidth))
{
if (x < (w - 1)) x++;
else break;
}
#endif
if (x >= w) break;
if (_sb_add(&sb, " ", 1) < 0) goto err;
}
}
}
else
{
if (rtrim)
_sb_spaces_rtrim(&sb);
if (_sb_add(&sb, "\n", 1) < 0) goto err;
}
}
}
termpty_backlog_unlock();
if (rtrim)
_sb_spaces_rtrim(&sb);
if (lenp)
*lenp = sb.len;
return sb.buf;
err:
free(sb.buf);
return NULL;
}
static void
_sel_set(Termio *sd, Eina_Bool enable)
{
if (sd->pty->selection.is_active == enable) return;
sd->pty->selection.is_active = enable;
if (enable)
evas_object_smart_callback_call(sd->win, "selection,on", NULL);
else
{
evas_object_smart_callback_call(sd->win, "selection,off", NULL);
sd->pty->selection.by_word = EINA_FALSE;
sd->pty->selection.by_line = EINA_FALSE;
}
}
static void _lost_selection(void *data, Elm_Sel_Type selection);
static void
_lost_selection_reset_job(void *data)
{
Termio *sd = evas_object_smart_data_get(data);
EINA_SAFETY_ON_NULL_RETURN(sd);
sd->sel_reset_job = NULL;
elm_cnp_selection_set(sd->win, sd->sel_type,
ELM_SEL_FORMAT_TEXT,
sd->sel_str, strlen(sd->sel_str));
elm_cnp_selection_loss_callback_set(sd->win, sd->sel_type,
_lost_selection, data);
}
static void
_lost_selection(void *data, Elm_Sel_Type selection)
{
Eina_List *l;
Evas_Object *obj;
double t = ecore_time_get();
EINA_LIST_FOREACH(terms, l, obj)
{
Termio *sd = evas_object_smart_data_get(obj);
if (!sd) continue;
if ((t - sd->set_sel_at) < 0.2) /// hack
{
if ((sd->have_sel) && (sd->sel_str) && (!sd->reset_sel))
{
sd->reset_sel = EINA_TRUE;
if (sd->sel_reset_job) ecore_job_del(sd->sel_reset_job);
sd->sel_reset_job = ecore_job_add
(_lost_selection_reset_job, data);
}
continue;
}
if (sd->have_sel)
{
if (sd->sel_str)
{
eina_stringshare_del(sd->sel_str);
sd->sel_str = NULL;
}
_sel_set(sd, EINA_FALSE);
elm_object_cnp_selection_clear(sd->win, selection);
_smart_update_queue(obj, sd);
sd->have_sel = EINA_FALSE;
}
}
}
static void
_take_selection_text(Termio *sd, Elm_Sel_Type type, const char *text)
{
EINA_SAFETY_ON_NULL_RETURN