1 /*
2  *   LASH
3  *
4  *   Copyright (C) 2002 Robert Ham <rah@bash.sh>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #define _GNU_SOURCE
22 
23 #include "config.h"
24 
25 #include <unistd.h>
26 
27 #include <lash/lash.h>
28 
29 #include "synth.h"
30 
31 #ifdef HAVE_GTK2
32 #  include "interface.h"
33 #endif
34 
35 void
save_data()36 save_data()
37 {
38 	lash_config_t *config;
39 
40 	config = lash_config_new_with_key("modulation");
41 	lash_config_set_value_double(config, modulation);
42 	lash_send_config(lash_client, config);
43 
44 	config = lash_config_new_with_key("attack");
45 	lash_config_set_value_double(config, attack);
46 	lash_send_config(lash_client, config);
47 
48 	config = lash_config_new_with_key("decay");
49 	lash_config_set_value_double(config, decay);
50 	lash_send_config(lash_client, config);
51 
52 	config = lash_config_new_with_key("sustain");
53 	lash_config_set_value_double(config, sustain);
54 	lash_send_config(lash_client, config);
55 
56 	config = lash_config_new_with_key("release");
57 	lash_config_set_value_double(config, release);
58 	lash_send_config(lash_client, config);
59 
60 	config = lash_config_new_with_key("gain");
61 	lash_config_set_value_double(config, gain);
62 	lash_send_config(lash_client, config);
63 
64 	config = lash_config_new_with_key("harmonic");
65 	lash_config_set_value_int(config, harmonic);
66 	lash_send_config(lash_client, config);
67 
68 	config = lash_config_new_with_key("subharmonic");
69 	lash_config_set_value_int(config, subharmonic);
70 	lash_send_config(lash_client, config);
71 
72 	config = lash_config_new_with_key("transpose");
73 	lash_config_set_value_int(config, transpose);
74 	lash_send_config(lash_client, config);
75 
76 }
77 
78 void
restore_data(lash_config_t * config)79 restore_data(lash_config_t * config)
80 {
81 	const char *key;
82 
83 	key = lash_config_get_key(config);
84 
85 	if (strcmp(key, "modulation") == 0) {
86 		modulation = lash_config_get_value_double(config);
87 		return;
88 	}
89 
90 	if (strcmp(key, "attack") == 0) {
91 		attack = lash_config_get_value_double(config);
92 		return;
93 	}
94 
95 	if (strcmp(key, "decay") == 0) {
96 		decay = lash_config_get_value_double(config);
97 		return;
98 	}
99 
100 	if (strcmp(key, "sustain") == 0) {
101 		sustain = lash_config_get_value_double(config);
102 		return;
103 	}
104 
105 	if (strcmp(key, "release") == 0) {
106 		release = lash_config_get_value_double(config);
107 		return;
108 	}
109 
110 	if (strcmp(key, "gain") == 0) {
111 		gain = lash_config_get_value_double(config);
112 		return;
113 	}
114 
115 	if (strcmp(key, "harmonic") == 0) {
116 		harmonic = lash_config_get_value_int(config);
117 		return;
118 	}
119 
120 	if (strcmp(key, "subharmonic") == 0) {
121 		subharmonic = lash_config_get_value_int(config);
122 		return;
123 	}
124 
125 	if (strcmp(key, "transpose") == 0) {
126 		transpose = lash_config_get_value_int(config);
127 		return;
128 	}
129 
130 }
131 
132 int
lash_main()133 lash_main()
134 {
135 	lash_event_t *event;
136 	lash_config_t *config;
137 
138 	while ((event = lash_get_event(lash_client))) {
139 		switch (lash_event_get_type(event)) {
140 		case LASH_Quit:
141 			quit = 1;
142 			lash_event_destroy(event);
143 			break;
144 		case LASH_Restore_Data_Set:
145 			lash_send_event(lash_client, event);
146 			break;
147 		case LASH_Save_Data_Set:
148 			save_data();
149 			lash_send_event(lash_client, event);
150 			break;
151 		case LASH_Server_Lost:
152 			return 1;
153 		default:
154 			printf("%s: receieved unknown LASH event of type %d",
155 				   __FUNCTION__, lash_event_get_type(event));
156 			lash_event_destroy(event);
157 			break;
158 		}
159 	}
160 
161 	while ((config = lash_get_config(lash_client))) {
162 		restore_data(config);
163 		lash_config_destroy(config);
164 	}
165 
166 	return 0;
167 }
168 
169 void *
lash_thread_main(void * data)170 lash_thread_main(void *data)
171 {
172 	printf("LASH thread running\n");
173 
174 	while (!lash_main())
175 		usleep(1000);
176 
177 	printf("LASH thread finished\n");
178 	return NULL;
179 }
180 
181 /* EOF */
182