File: editor_pane.c

package info (click to toggle)
amsynth 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,628 kB
  • sloc: sh: 10,131; cpp: 5,996; ansic: 3,298; makefile: 134
file content (357 lines) | stat: -rw-r--r-- 11,120 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
////////////////////////////////////////////////////////////////////////////////

#include "editor_pane.h"
#include "../controls.h"
#include "bitmap_button.h"
#include "bitmap_knob.h"
#include "bitmap_popup.h"

//#define ENABLE_LAYOUT_EDIT 1

////////////////////////////////////////////////////////////////////////////////

#define SIZEOF_ARRAY( a ) ( sizeof((a)) / sizeof((a)[0]) )

////////////////////////////////////////////////////////////////////////////////

static GdkPixbuf *editor_pane_bg = NULL;

////////////////////////////////////////////////////////////////////////////////

typedef struct
{
	GdkPixbuf *pixbuf;
	guint fr_width;
	guint fr_height;
	guint fr_count;
}
resource_info;

////////////////////////////////////////////////////////////////////////////////

static void
editor_pane_expose_event_handler (GtkWidget *widget, gpointer data)
{
	gdk_draw_pixbuf(
		widget->window,
		NULL,	// gc
		editor_pane_bg,
		0,	// src_x
		0,	// src_y
		widget->allocation.x,
		widget->allocation.y,
		gdk_pixbuf_get_width (editor_pane_bg),
		gdk_pixbuf_get_height (editor_pane_bg),
		GDK_RGB_DITHER_NONE, 0, 0
	);
}

////////////////////////////////////////////////////////////////////////////////

int deldir (const char *dir_path)
{
//	g_assert (dir_path);
//	gchar *command = g_strdup_printf("rm -r \"%s\"", dir_path);
//	int result = system (command);
//	g_free (command);
//	return result;
	return 0;
}

gchar *extract_skin (char *skin_file)
{
	gchar *tempdir = g_strconcat(g_get_tmp_dir(), "/amsynth.skin.XXXXXXXX", NULL);
	if (!mkdtemp(tempdir)) {
		g_message("Failed to create temporary directory. Unable to load skin.");
		g_free(tempdir);
		return NULL;
	}
	
	gchar *unzip_bin = "/usr/bin/unzip";
	gchar *command = g_strdup_printf("%s -qq -o -j \"%s\" -d %s", unzip_bin, skin_file, tempdir);
	int result = system (command);
	g_free (command);
	command = NULL;
	
	if (result != 0) {
		g_message("Failed to extract archive. Unable to load skin.");
		deldir (tempdir);
		g_free (tempdir);
		tempdir = NULL;
	}
	return tempdir;
}

////////////////////////////////////////////////////////////////////////////////

#if ENABLE_LAYOUT_EDIT /////////////////////////////////////////////////////////

static gboolean
control_move_handler (GtkWidget *widget, GdkEventMotion *event)
{
	static GtkWidget *current_widget = NULL;
	static gint drag_offset_x = 0;
	static gint drag_offset_y = 0;
	
	if (!widget || !event)
		return FALSE;
	
	if (event->type == GDK_MOTION_NOTIFY && event->state & GDK_BUTTON2_MASK)
	{
		if (current_widget != widget) {
			// remember where the drag operation began
			current_widget = widget;
			drag_offset_x = event->x;
			drag_offset_y = event->y;
			return TRUE;
		}
		GtkWidget *parent = gtk_widget_get_parent (widget);
		gtk_fixed_move (GTK_FIXED (parent), widget,
			widget->allocation.x - parent->allocation.x + event->x - drag_offset_x,
			widget->allocation.y - parent->allocation.y + event->y - drag_offset_y);
		return TRUE;
	}
    return FALSE;
}

static void
foreach_containter_widget (GtkWidget *widget, gpointer data)
{
	GtkWidget *parent = GTK_WIDGET (data);
	
	const gchar *name = gtk_buildable_get_name (GTK_BUILDABLE (widget));

	gchar *type = NULL, *resname = NULL;
	g_object_get (G_OBJECT (widget),
		"tooltip-text", &type,
		"name", &resname,
		NULL);
	
	printf (
		"[%s]\n"
		"resource=%s\n"
		"type=%s\n"
		"pos_x=%d\n"
		"pos_y=%d\n",
		name, resname, type,
		widget->allocation.x - parent->allocation.x,
		widget->allocation.y - parent->allocation.y);
	
	printf ("\n");
}

static gboolean
on_unrealize (GtkWidget *widget, gpointer user_data)
{
	GtkContainer *container = GTK_CONTAINER (widget);
	gtk_container_foreach (container, foreach_containter_widget, container);
	return FALSE;
}

#endif /////////////////////////////////////////////////////////////////////////

static void
on_control_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	if (event->type == GDK_BUTTON_PRESS && event->button == 3)
	{
		const size_t param_index = (size_t)data;
		const char *name = parameter_name_from_index(param_index);
		modal_midi_learn(param_index);
	}
}

////////////////////////////////////////////////////////////////////////////////

#define HANDLE_GERROR( gerror ) \
	if (gerror) { \
		g_critical ("%s", gerror->message); \
		g_error_free (gerror); \
		gerror = NULL; \
	}

#define KEY_CONTROL_TYPE		"type"
#define KEY_CONTROL_TYPE_BUTTON	"button"
#define KEY_CONTROL_TYPE_KNOB	"knob"
#define KEY_CONTROL_TYPE_POPUP	"popup"
#define KEY_CONTROL_POS_X		"pos_x"
#define KEY_CONTROL_POS_Y		"pos_y"
#define KEY_CONTROL_RESOURCE	"resource"
#define KEY_CONTROL_PARAM_NAME	"param_name"
#define KEY_CONTROL_PARAM_NUM	"param_num"

GtkWidget *
editor_pane_new (GtkAdjustment **adjustments)
{
	GtkWidget *fixed = gtk_fixed_new ();
	gtk_widget_set_usize (fixed, 400, 300);
	
	g_signal_connect (GTK_OBJECT (fixed), "expose-event", (GtkSignalFunc) editor_pane_expose_event_handler, (gpointer) NULL);
	
#if ENABLE_LAYOUT_EDIT
	g_signal_connect (GTK_OBJECT (fixed), "unrealize", (GtkSignalFunc) on_unrealize, (gpointer) NULL);
#endif
	
	gsize i;
	gchar *skin_dir = NULL;
	gchar *skin_path = (gchar *)g_getenv ("AMSYNTH_SKIN");
	if (skin_path == NULL) {
		skin_path = g_build_filename (g_getenv ("AMSYNTH_DATA_DIR"), "skins", "default", NULL);
	}
	
	if (!g_file_test (skin_path, G_FILE_TEST_EXISTS)) {
		g_critical ("cannot find skin '%s'", skin_path);
		return fixed;
	}
	
	if (g_file_test (skin_path, G_FILE_TEST_IS_DIR)) {
		skin_dir = g_strdup (skin_path);
	}
	
	if (g_file_test (skin_path, G_FILE_TEST_IS_REGULAR)) {
		skin_dir = extract_skin (skin_path);
		if (skin_dir == NULL) {
			g_critical ("Could not unpack skin file '%s'", skin_path);
			return fixed;
		}
	}
	
	{
		GData *resources = NULL;
		g_datalist_init (&resources);
	
		////////
		
		GError *gerror = NULL;
		GKeyFile *gkey_file = g_key_file_new ();
		gchar *ini_path = g_strconcat (skin_dir, "/layout.ini", NULL);
		if (!g_key_file_load_from_file (gkey_file, ini_path, G_KEY_FILE_NONE, NULL)) {
			g_critical ("Could not load layout.ini");
			return fixed;
		}
		g_key_file_set_list_separator (gkey_file, ',');
		g_free (ini_path); ini_path = NULL;
		
		////////
		
		{
			gchar *bg_name = g_key_file_get_string  (gkey_file, "layout", "background", &gerror); HANDLE_GERROR (gerror); g_strstrip (bg_name);
			gchar *path = g_strconcat (skin_dir, "/", bg_name, NULL);
			editor_pane_bg = gdk_pixbuf_new_from_file (path, &gerror); HANDLE_GERROR (gerror);
			g_assert (editor_pane_bg);
			g_free (bg_name);
			g_free (path);
			
			gtk_widget_set_size_request (fixed, gdk_pixbuf_get_width (editor_pane_bg), gdk_pixbuf_get_height (editor_pane_bg));
		}
		
		//// Load resources
		
		gsize num_resources = 0;
		gchar **resource_list = g_key_file_get_string_list (gkey_file, "layout", "resources", &num_resources, &gerror); HANDLE_GERROR (gerror);
		if (resource_list)
		{
			for (i=0; i<num_resources; i++)
			{
				gchar *resource_name = g_strstrip (resource_list[i]);
				
				gchar *file = g_key_file_get_string  (gkey_file, resource_name, "file",   &gerror); HANDLE_GERROR (gerror);
				gint width  = g_key_file_get_integer (gkey_file, resource_name, "width",  &gerror); HANDLE_GERROR (gerror);
				gint height = g_key_file_get_integer (gkey_file, resource_name, "height", &gerror); HANDLE_GERROR (gerror);
				gint frames = g_key_file_get_integer (gkey_file, resource_name, "frames", &gerror); HANDLE_GERROR (gerror);
				
				gchar *path = g_strconcat (skin_dir, "/", g_strstrip (file), NULL);
				
				GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (path, &gerror); HANDLE_GERROR (gerror); g_assert (pixbuf);
				
				g_assert (gdk_pixbuf_get_width (pixbuf) == width || gdk_pixbuf_get_height (pixbuf) == height);
				g_assert (gdk_pixbuf_get_width (pixbuf) == (width * frames) || gdk_pixbuf_get_height (pixbuf) == (height * frames));
				
				resource_info *info = g_malloc0 (sizeof (resource_info));
				info->pixbuf    = pixbuf;
				info->fr_width  = width;
				info->fr_height = height;
				info->fr_count  = frames;
				
				g_datalist_set_data (&resources, resource_name, (gpointer)info);
				
				g_free (file);
				g_free (path);
			}
			g_strfreev (resource_list);
			resource_list = NULL;
		}
		
		//// Create controls
		
		for (i=0; i<kControls_End; i++)
		{
			const gchar *control_name = parameter_name_from_index (i);
			
			if (!g_key_file_has_group (gkey_file, control_name)) {
				g_warning ("layout.ini contains no entry for control '%s'", control_name);
				continue;
			}
			
			gint pos_x = g_key_file_get_integer (gkey_file, control_name, KEY_CONTROL_POS_X, &gerror); HANDLE_GERROR (gerror);
			gint pos_y = g_key_file_get_integer (gkey_file, control_name, KEY_CONTROL_POS_Y, &gerror); HANDLE_GERROR (gerror);
			gchar *type = g_key_file_get_string (gkey_file, control_name, KEY_CONTROL_TYPE, &gerror); HANDLE_GERROR (gerror); g_strstrip (type);
			gchar *resn = g_key_file_get_string (gkey_file, control_name, KEY_CONTROL_RESOURCE, &gerror); HANDLE_GERROR (gerror); g_strstrip (resn);
			
			/////////////////////////
			
			GtkWidget *widget = NULL;
			resource_info *res = g_datalist_get_data (&resources, resn);
			if (!res) {
				g_warning ("layout.ini error: control '%s' references a non-existent resource '%s'", control_name, resn);
				continue;
			}
			GdkPixbuf *subpixpuf = gdk_pixbuf_new_subpixbuf (editor_pane_bg, pos_x, pos_y, res->fr_width, res->fr_height);
			GtkAdjustment *adj = adjustments[i];
			
			if (g_strcmp0 (KEY_CONTROL_TYPE_KNOB, type) == 0)
			{
				widget = bitmap_knob_new (adj, res->pixbuf, res->fr_width, res->fr_height, res->fr_count);
				bitmap_knob_set_bg (widget, subpixpuf);
			}
			else if (g_strcmp0 (KEY_CONTROL_TYPE_BUTTON, type) == 0)
			{
				widget = bitmap_button_new (adj, res->pixbuf, res->fr_width, res->fr_height, res->fr_count);
				bitmap_button_set_bg (widget, subpixpuf);
			}
			else if (g_strcmp0 (KEY_CONTROL_TYPE_POPUP, type) == 0)
			{
				gsize nstrings = 0;
				gchar **strings = g_key_file_get_string_list (gkey_file, control_name, "popup_strings", &nstrings, &gerror); HANDLE_GERROR (gerror);
				widget = bitmap_popup_new (adj, res->pixbuf, res->fr_width, res->fr_height, res->fr_count);
				bitmap_popup_set_strings (widget, (const char **)strings);	
				bitmap_popup_set_bg (widget, subpixpuf);
				g_strfreev (strings);
			}
			
			g_signal_connect_after(G_OBJECT(widget), "button-press-event", G_CALLBACK (on_control_press), GINT_TO_POINTER(i));
			gtk_fixed_put (GTK_FIXED (fixed), widget, pos_x, pos_y);
			
#if ENABLE_LAYOUT_EDIT
			gtk_buildable_set_name (GTK_BUILDABLE (widget), control_name);
			g_object_set (G_OBJECT (widget), "name", resn, "tooltip-text", type, NULL);
			g_signal_connect(widget, "motion-notify-event", G_CALLBACK(control_move_handler), NULL);
			gtk_widget_add_events(widget, GDK_BUTTON2_MOTION_MASK);
#endif
			
			g_object_unref (G_OBJECT (subpixpuf));
			g_free (resn);
			g_free (type);
		}
		
		g_key_file_free (gkey_file);
	}
	
	//deldir (skin_dir);
	g_free (skin_dir);

	return fixed;
}

////////////////////////////////////////////////////////////////////////////////