aboutsummaryrefslogtreecommitdiff
path: root/sandbox_ui.lv2/sandbox_efl.c
blob: 59344a021666235602856a098fe59b2d0c418040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (c) 2015-2016 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sandbox_slave.h>

#include <lv2/lv2plug.in/ns/extensions/ui/ui.h>

#include <Elementary.h>

typedef struct _app_t app_t;

struct _app_t {
	sandbox_slave_t *sb;

	Evas_Object *win;
	Evas_Object *bg;
	Evas_Object *widget;
	Ecore_Fd_Handler *fd;
};

static Eina_Bool
_recv(void *data, Ecore_Fd_Handler *fd_handler)
{
	sandbox_slave_t *sb = data;

	sandbox_slave_recv(sb);

	return ECORE_CALLBACK_RENEW;
}

static void
_del_request(void *data, Evas_Object *obj, void *event_info)
{
	app_t *app = data;

	elm_exit();
	app->bg = NULL;
	app->win = NULL;
}

static inline int
_init(sandbox_slave_t *sb, void *data)
{
	app_t *app= data;

	int w = 640;
	int h = 360;

	const char *title = sandbox_slave_title_get(sb);
	app->win = elm_win_add(NULL, title, ELM_WIN_BASIC);
	if(!app->win)
	{
		fprintf(stderr, "elm_win_add failed\n");
		goto fail;
	}
	elm_win_title_set(app->win, title);
	elm_win_autodel_set(app->win, EINA_TRUE);
	evas_object_smart_callback_add(app->win, "delete,request", _del_request, app);

	app->bg = elm_bg_add(app->win);
	if(!app->bg)
	{
		fprintf(stderr, "elm_bg_add failed\n");
		goto fail;
	}
	elm_bg_color_set(app->bg, 64, 64, 64);
	evas_object_size_hint_weight_set(app->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(app->bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
	evas_object_show(app->bg);
	elm_win_resize_object_add(app->win, app->bg);

	const LV2_Feature parent_feature = {
		.URI = LV2_UI__parent,
		.data = app->win
	};

	if(  !sandbox_slave_instantiate(sb, &parent_feature, &app->widget)
		|| !app->widget)
	{
		fprintf(stderr, "sandbox_slave_instantiate failed\n");
		goto fail;
	}
	evas_object_size_hint_weight_set(app->widget, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(app->widget, EVAS_HINT_FILL, EVAS_HINT_FILL);

	// get widget size hint
	int W, H;
	evas_object_size_hint_min_get(app->widget, &W, &H);
	if(W != 0)
		w = W;
	if(H != 0)
		h = H;
	evas_object_show(app->widget);
	elm_win_resize_object_add(app->win, app->widget);

	evas_object_resize(app->win, w, h);
	evas_object_show(app->win);

	int fd;
	sandbox_slave_fd_get(sb, &fd);
	if(fd == -1)
	{
		fprintf(stderr, "sandbox_slave_instantiate failed\n");
		goto fail;
	}

	app->fd= ecore_main_fd_handler_add(fd, ECORE_FD_READ,
		_recv, sb, NULL, NULL);
	if(!app->fd)
	{
		fprintf(stderr, "ecore_main_fd_handler_add failed\n");
		goto fail;
	}

	return 0;

fail:
	return -1;
}

static inline void
_run(sandbox_slave_t *sb, void *data)
{
	app_t *app = data;

	elm_run();
}

static inline void
_deinit(void *data)
{
	app_t *app = data;

	if(app->fd)
		ecore_main_fd_handler_del(app->fd);

	if(app->bg)
	{
		elm_win_resize_object_del(app->win, app->bg);
		evas_object_hide(app->bg);
		evas_object_del(app->bg);
	}

	if(app->win)
	{
		evas_object_hide(app->win);
		evas_object_del(app->win);
	}
}

static const sandbox_slave_driver_t driver = {
	.init_cb = _init,
	.run_cb = _run,
	.deinit_cb = _deinit,
	.resize_cb = NULL
};

static int
elm_main(int argc, char **argv)
{
	static app_t app;
	
#ifdef ELM_1_10
	elm_config_accel_preference_set("gl");
#endif

	app.sb = sandbox_slave_new(argc, argv, &driver, &app);
	if(app.sb)
	{
		sandbox_slave_run(app.sb);
		sandbox_slave_free(app.sb);
		printf("bye from %s\n", argv[0]);
		return 0;
	}

	printf("fail from %s\n", argv[0]);
	return -1;
}

ELM_MAIN();