make get_current_preset return list of messages describing preset rather than just one message with preset parameters

This commit is contained in:
Tomasz Moń
2009-05-06 13:10:46 +02:00
parent d7f10eb509
commit 4509c14bbc
5 changed files with 88 additions and 20 deletions

82
gdigi.c
View File

@@ -32,6 +32,7 @@ static char *device_port = "hw:1,0,0";
static GQueue *message_queue = NULL; static GQueue *message_queue = NULL;
static GMutex *message_queue_mutex = NULL; static GMutex *message_queue_mutex = NULL;
static GCond *message_queue_cond = NULL;
/** /**
* \param array data to calculate checksum * \param array data to calculate checksum
@@ -128,6 +129,11 @@ GString *pack_data(gchar *data, gint len)
return packed; return packed;
} }
static void message_free_func(GString *msg, gpointer user_data)
{
g_string_free(msg, TRUE);
}
/** /**
* \param msg message to unpack * \param msg message to unpack
* *
@@ -178,7 +184,7 @@ static void unpack_message(GString *msg)
* *
* \return MessageID, or -1 on error. * \return MessageID, or -1 on error.
**/ **/
static MessageID get_message_id(GString *msg) MessageID get_message_id(GString *msg)
{ {
/** \todo check if msg is valid SysEx message */ /** \todo check if msg is valid SysEx message */
g_return_val_if_fail(msg != NULL, -1); g_return_val_if_fail(msg != NULL, -1);
@@ -247,6 +253,7 @@ void push_message(GString *msg)
default: default:
g_mutex_lock(message_queue_mutex); g_mutex_lock(message_queue_mutex);
g_queue_push_tail(message_queue, msg); g_queue_push_tail(message_queue, msg);
g_cond_signal(message_queue_cond);
g_mutex_unlock(message_queue_mutex); g_mutex_unlock(message_queue_mutex);
} }
} }
@@ -308,6 +315,11 @@ gpointer read_data_thread(gboolean *stop)
int pos; int pos;
int bytes; int bytes;
if (string == NULL) {
while (buf[i] != 0xF0 && i < length)
i++;
}
pos = i; pos = i;
for (bytes = 0; (bytes<length-i) && (buf[i+bytes] != 0xF7); bytes++); for (bytes = 0; (bytes<length-i) && (buf[i+bytes] != 0xF7); bytes++);
@@ -640,18 +652,68 @@ GStrv query_preset_names(gchar bank)
/** /**
* Queries current edit buffer. * Queries current edit buffer.
* *
* \return GString containing RECEIVE_PRESET_PARAMETERS SysEx message. * \return GList with preset SysEx messages, which must be freed using preset_list_free.
**/ **/
GString *get_current_preset() GList *get_current_preset()
{ {
GString *data = NULL; GString *data = NULL;
GList *list = NULL;
guint x, len;
gboolean found = FALSE;
gboolean done = FALSE;
send_message(REQUEST_PRESET, "\x04\x00", 2); send_message(REQUEST_PRESET, "\x04\x00", 2);
/* read reply */ do {
data = get_message_by_id(RECEIVE_PRESET_PARAMETERS); g_mutex_lock(message_queue_mutex);
return data; len = g_queue_get_length(message_queue);
for (x = 0; x<len && (found == FALSE); x++) {
data = g_queue_peek_nth(message_queue, x);
if (get_message_id(data) == RECEIVE_PRESET_START) {
found = TRUE;
g_queue_pop_nth(message_queue, x);
unpack_message(data);
list = g_list_append(list, data);
break;
}
}
if (found == TRUE) {
int i;
int amt;
for (i = 10; (i < data->len) && data->str[i]; i++);
amt = (unsigned char)data->str[i+2];
while (amt) {
data = g_queue_pop_nth(message_queue, x);
if (data == NULL) {
g_cond_wait(message_queue_cond, message_queue_mutex);
} else {
unpack_message(data);
list = g_list_append(list, data);
amt--;
}
}
done = TRUE;
}
g_mutex_unlock(message_queue_mutex);
} while (done == FALSE);
return list;
}
void preset_list_free(GList *list)
{
g_return_if_fail(list != NULL);
g_list_foreach(list, (GFunc) message_free_func, NULL);
g_list_free(list);
} }
/** /**
@@ -725,11 +787,6 @@ static GOptionEntry options[] = {
#endif /* DOXYGEN_SHOULD_SKIP_THIS */ #endif /* DOXYGEN_SHOULD_SKIP_THIS */
static void message_queue_free_func(GString *msg, gpointer user_data)
{
g_string_free(msg, TRUE);
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
GError *error = NULL; GError *error = NULL;
GOptionContext *context; GOptionContext *context;
@@ -757,6 +814,7 @@ int main(int argc, char *argv[]) {
} else { } else {
message_queue = g_queue_new(); message_queue = g_queue_new();
message_queue_mutex = g_mutex_new(); message_queue_mutex = g_mutex_new();
message_queue_cond = g_cond_new();
read_thread = g_thread_create((GThreadFunc)read_data_thread, read_thread = g_thread_create((GThreadFunc)read_data_thread,
&stop_read_thread, &stop_read_thread,
TRUE, NULL); TRUE, NULL);
@@ -798,7 +856,7 @@ int main(int argc, char *argv[]) {
if (message_queue != NULL) { if (message_queue != NULL) {
g_message("%d unread messages in queue", g_message("%d unread messages in queue",
g_queue_get_length(message_queue)); g_queue_get_length(message_queue));
g_queue_foreach(message_queue, (GFunc) message_queue_free_func, NULL); g_queue_foreach(message_queue, (GFunc) message_free_func, NULL);
g_queue_free(message_queue); g_queue_free(message_queue);
} }

View File

@@ -738,6 +738,7 @@ typedef struct {
} SettingParam; } SettingParam;
void send_message(gint procedure, gchar *data, gint len); void send_message(gint procedure, gchar *data, gint len);
MessageID get_message_id(GString *msg);
void append_value(GString *msg, guint value); void append_value(GString *msg, guint value);
GString *get_message_by_id(MessageID id); GString *get_message_by_id(MessageID id);
SettingParam *setting_param_new(); SettingParam *setting_param_new();
@@ -748,6 +749,7 @@ void switch_preset(guint bank, guint x);
void store_preset_name(int x, const gchar *name); void store_preset_name(int x, const gchar *name);
void set_preset_level(int level); void set_preset_level(int level);
GStrv query_preset_names(gchar bank); GStrv query_preset_names(gchar bank);
GString *get_current_preset(); GList *get_current_preset();
void preset_list_free(GList *list);
#endif /* GDIGI_H */ #endif /* GDIGI_H */

6
gui.c
View File

@@ -192,9 +192,9 @@ static void apply_preset_to_gui(Preset *preset)
**/ **/
static void apply_current_preset() static void apply_current_preset()
{ {
GString *msg = get_current_preset(); GList *list = get_current_preset();
Preset *preset = create_preset_from_data(msg); Preset *preset = create_preset_from_data(list);
g_string_free(msg, TRUE); preset_list_free(list);
apply_preset_to_gui(preset); apply_preset_to_gui(preset);
preset_free(preset); preset_free(preset);
} }

View File

@@ -173,19 +173,27 @@ Preset *create_preset_from_xml_file(gchar *filename, GError **error)
} }
/** /**
* \param data unpacked RECEIVE_PRESET_PARAMETERS message * \param list list containing unpacked preset SysEx messages.
* *
* Parses message * Parses message
* *
* \return Preset which must be freed using preset_free, or NULL on error. * \return Preset which must be freed using preset_free, or NULL on error.
**/ **/
Preset *create_preset_from_data(GString *data) Preset *create_preset_from_data(GList *list)
{ {
GString *data;
GList *iter;
gint total; gint total;
gint n; gint n;
gint x; gint x;
g_return_val_if_fail(data != NULL, NULL); g_return_val_if_fail(list != NULL, NULL);
iter = list;
do {
data = (GString*) iter->data;
iter = g_list_next(iter);
} while (get_message_id(data) != RECEIVE_PRESET_PARAMETERS);
x = 0x09; x = 0x09;
n = 0; n = 0;

View File

@@ -25,7 +25,7 @@ typedef struct {
} Preset; } Preset;
Preset *create_preset_from_xml_file(gchar *filename, GError **error); Preset *create_preset_from_xml_file(gchar *filename, GError **error);
Preset *create_preset_from_data(GString *data); Preset *create_preset_from_data(GList *list);
void preset_free(Preset *preset); void preset_free(Preset *preset);
#endif /* GDIGI_PRESET_H */ #endif /* GDIGI_PRESET_H */