sinek/src keys.c,1.46,1.47

[email protected] Tue, 18 Feb 2003 00:29:24 -0800
Newsgroups gmane.comp.video.sinek.cvs
Message-ID <[email protected]>
Update of /cvsroot/sinek/sinek/src
In directory sc8-pr-cvs1:/tmp/cvs-serv2646

Modified Files:
	keys.c 
Log Message:
use gmarkup instead of xml.c iks stuff


Index: keys.c
===================================================================
RCS file: /cvsroot/sinek/sinek/src/keys.c,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -d -r1.46 -r1.47
--- keys.c	17 Feb 2003 08:26:01 -0000	1.46
+++ keys.c	18 Feb 2003 08:29:20 -0000	1.47
@@ -195,7 +195,7 @@
 
 static action *find_action_by_event(char *key, int modifier);
 static action *find_action_by_name(char *action);
-static int load_keys(void);
+static int keys_load(void);
 static void reset_keys(void);
 static char *key_name(char *key, int modifier);
 
@@ -208,7 +208,7 @@
 	{
 		actions[i].label = gettext(actions[i].label);
 	}
-	if(!load_keys()) reset_keys();
+	if(!keys_load()) reset_keys();
 }
 
 
@@ -366,73 +366,120 @@
 	}
 }
 
-
-static int load_keys(void)
+static void keys_save(GtkWidget *but, GtkWidget *win)
 {
-	action *act;
-	char *key;
-	int modifier;
 	char *fname;
-	iks *x, *y;
+	FILE *fp;
+	binding *kb;
+	GString *str;
 
-	fname = g_strconcat(g_get_home_dir(), "/.xine/keys.xml", NULL);
-	x = iks_load(fname);
+	/* generate xml string */
+	str = g_string_new("<skb>\n");
+	for (kb = bindings; kb != NULL; kb = kb->next) {
+		g_string_append_printf(str, "<binding action='%s'>\n\t<key>%s</key>\n",
+				kb->action->name, kb->key);
+		if (kb->modifier) {
+			if (kb->modifier & MOD_SHIFT)
+				g_string_append(str, "\t<shift/>");
+			if (kb->modifier & MOD_CONTROL)
+				g_string_append(str, "\t<control/>");
+			if (kb->modifier & MOD_ALT)
+				g_string_append(str, "\t<alt/>");
+			g_string_append(str, "\n");
+		}
+		g_string_append(str, "</binding>\n");
+	}
+	g_string_append(str, "</skb>");
+
+	/* save to ~/.xine/keys.xml */
+	fname = g_build_filename(g_get_home_dir(), ".xine", "keys.xml", NULL);
+	fp = fopen(fname, "w");
+	if (fp) {
+		fputs(str->str, fp);
+		fclose(fp);
+	}
 	g_free(fname);
-	if(!x || iks_strcmp(iks_name(x), "skb") != 0) return(0);
+	g_string_free(str, TRUE);
 
-	for(y = iks_child(x); y; y = iks_next(y))
-	{
-		if(iks_type(y) == IKS_TAG && iks_strcmp(iks_name(y), "binding") == 0)
-		{
-			act = find_action_by_name(iks_find_attrib(y, "action"));
-			key = iks_strdup(iks_find_cdata(y, "key"));
-			modifier = 0;
-			if(iks_find(y, "shift")) modifier += MOD_SHIFT;
-			if(iks_find(y, "control")) modifier += MOD_CONTROL;
-			if(iks_find(y, "alt")) modifier += MOD_ALT;
-			key_add(act, key, modifier, FALSE);
+	/* done */
+	gtk_widget_hide(win);
+}
+
+static void keys_load_open_tag(GMarkupParseContext *c, const gchar *name,
+		const gchar **attribs, const gchar **attrib_vals, gpointer data, GError **err)
+{
+	binding *b = (binding *) data;
+
+	if (strcmp(name, "binding") == 0) {
+		memset((void *) b, 0, sizeof(*b));
+		if (attribs[0])
+			b->action = find_action_by_name(attribs[0]);
+	}
+	else if (strcmp(name, "shift") == 0)
+		b->modifier += MOD_SHIFT;
+	else if (strcmp(name, "control") == 0)
+		b->modifier += MOD_CONTROL;
+	else if (strcmp(name, "alt") == 0)
+		b->modifier += MOD_ALT;
+}
+
+static void keys_load_close_tag(GMarkupParseContext *c, const gchar *name,
+		gpointer data, GError **err)
+{
+	if (strcmp(name, "binding") == 0) {
+		binding *b = (binding *) data;
+		key_add(b->action, b->key, b->modifier, FALSE);
+		if (b->key) {
+			g_free(b->key);
+			b->key = NULL;
 		}
 	}
-	iks_delete(x);
-	return 1;
 }
 
+static void keys_load_text(GMarkupParseContext *c, const char *text,
+		gsize len, gpointer data, GError **err)
+{
+	if (strcmp(g_markup_parse_context_get_element(c), "key") == 0) {
+		binding *b = (binding *) data;
+		b->key = g_strdup(text);
+	}
+}
 
-static void save_keys(GtkWidget *but, GtkWidget *win)
+static int keys_load(void)
 {
 	char *fname;
-	iks *x, *y;
-	binding *kb;
-
-	fname = g_strconcat(g_get_home_dir(), "/.xine/keys.xml", NULL);
-	x = iks_new("skb");
-	iks_insert_cdata(x, "\n", 1);
-	for(kb = bindings; kb; kb = kb->next)
+	FILE *fp;
+	char buf[256];
+	binding binding; 
+	GError *error = NULL;
+	GMarkupParseContext *context;
+	GMarkupParser parser =
 	{
-		y = iks_insert(x, "binding");
-		iks_insert_attrib(y, "action", kb->action->name);
-		iks_insert_cdata(y, "\n\t", 2);
-		iks_insert_cdata(iks_insert(y, "key"), kb->key, -1);
-		if(kb->modifier)
-		{
-			iks_insert_cdata(y, "\n\t", 2);
-			if(kb->modifier & MOD_SHIFT) iks_insert(y, "shift");
-			if(kb->modifier & MOD_CONTROL) iks_insert(y, "control");
-			if(kb->modifier & MOD_ALT) iks_insert(y, "alt");
-			iks_insert_cdata(y, "\n", 1);
-		}
-		else
-		{
-			iks_insert_cdata(y, "\n", 1);
-		}
-		iks_insert_cdata(x, "\n", 1);
-	}
-	iks_save(fname, x);
-	iks_delete(x);
+		keys_load_open_tag,
+		keys_load_close_tag,
+		keys_load_text,
+		NULL,
+		NULL,
+	};
+
+	fname = g_build_filename(g_get_home_dir(), ".xine", "keys.xml", NULL);
+	fp = fopen(fname, "r");
 	g_free(fname);
-	gtk_widget_hide(win);
-}
+	if (!fp)
+		return 0;
 
+	context = g_markup_parse_context_new(&parser, 0, &binding, NULL);
+	while ((fgets(buf, sizeof(buf), fp))) {
+		if (!g_markup_parse_context_parse(context, buf, -1, &error))
+			break;
+	}
+	if (error == NULL)
+		g_markup_parse_context_end_parse(context, &error);
+	g_markup_parse_context_free(context);
+	fclose(fp);
+
+	return error == NULL ? 1 : 0;
+}
 
 static char *key_name(char *key, int modifier)
 {
@@ -680,7 +727,7 @@
 	t = gtk_button_new_from_stock(GTK_STOCK_SAVE);
 	gtk_widget_show(t);
 	gtk_container_add(GTK_CONTAINER(hb), t);
-	g_signal_connect(G_OBJECT(t), "clicked", G_CALLBACK(save_keys), (gpointer)self);
+	g_signal_connect(G_OBJECT(t), "clicked", G_CALLBACK(keys_save), (gpointer)self);
 
 	t = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
 	gtk_widget_show(t);
@@ -856,19 +903,23 @@
 			break;
 
 		case CMD_SPU_NEXT:
-			gtk_xine_set_spu_channel(GTK_XINE(sinek.xine), gtk_xine_get_spu_channel(GTK_XINE(sinek.xine)) + 1);
+			g_object_get(G_OBJECT(sinek.xine), "spu_channel", &tmp, NULL);
+			g_object_set(G_OBJECT(sinek.xine), "spu_channel", tmp + 1, NULL);
 			break;
 
 		case CMD_SPU_PREVIOUS:
-			gtk_xine_set_spu_channel(GTK_XINE(sinek.xine), gtk_xine_get_spu_channel(GTK_XINE(sinek.xine)) - 1);
+			g_object_get(G_OBJECT(sinek.xine), "spu_channel", &tmp, NULL);
+			g_object_set(G_OBJECT(sinek.xine), "spu_channel", tmp - 1, NULL);
 			break;
 
 		case CMD_AUDIO_NEXT:
-			gtk_xine_set_audio_channel(GTK_XINE(sinek.xine), gtk_xine_get_audio_channel(GTK_XINE(sinek.xine)) + 1);
+			g_object_get(G_OBJECT(sinek.xine), "audio_channel", &tmp, NULL);
+			g_object_set(G_OBJECT(sinek.xine), "audio_channel", tmp + 1, NULL);
 			break;
 
 		case CMD_AUDIO_PREVIOUS:
-			gtk_xine_set_audio_channel(GTK_XINE(sinek.xine), gtk_xine_get_audio_channel(GTK_XINE(sinek.xine)) - 1);
+			g_object_get(G_OBJECT(sinek.xine), "audio_channel", &tmp, NULL);
+			g_object_set(G_OBJECT(sinek.xine), "audio_channel", tmp - 1, NULL);
 			break;
 
 		case CMD_ZOOM_IN:
@@ -987,5 +1038,9 @@
 		case CMD_SET_MARK_9:
 			g_object_get(G_OBJECT(sinek.xine), "length", &sinek.marks[cmd - CMD_SET_MARK_1 + 1], NULL);
 			break;
+
+		default:
+			g_fatal(_("Unknown key received, programmer error!\n"));
+
 	}
 }




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf