summaryrefslogtreecommitdiff
path: root/aoview/aoview_replay.c
blob: 42728961cb73f14db1720282ad3ee8882b4d229e (plain) (blame)
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
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program 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 GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "aoview.h"

static GtkFileChooser	*replay_dialog;
static GtkWidget	*replay_ok;
static FILE		*replay_file;
static int		replay_tick;

static int
find_tick(char *line, gboolean *is_pad)
{
	char	*state = strstr(line, "STATE");
	if (!state)
		return -1;
	state = strchr(state, ' ');
	if (!state)
		return -1;
	while (*state == ' ')
		state++;
	*is_pad = strncmp(state, "pad", 3) == 0;
	while (*state && !isdigit(*state))
		state++;
	return atoi(state);
}

static void
aoview_replay_close(void)
{
	if (replay_file) {
		fclose(replay_file);
		replay_file = NULL;
	}
}

static char	replay_line[1024];

static gboolean
aoview_replay_read(gpointer data);

static gboolean
aoview_replay_execute(gpointer data)
{
	aoview_monitor_parse(replay_line);
	g_idle_add(aoview_replay_read, NULL);
	return FALSE;
}

static gboolean
aoview_replay_read(gpointer data)
{
	int		tick;
	gboolean	is_pad;

	if (!replay_file)
		return FALSE;
	if (fgets(replay_line, sizeof (replay_line), replay_file)) {
		tick = find_tick(replay_line, &is_pad);
		if (tick >= 0 && replay_tick >= 0 && !is_pad) {
			while (tick < replay_tick)
				tick += 65536;
			g_timeout_add((tick - replay_tick) * 10,
				      aoview_replay_execute,
				      NULL);
		} else {
			aoview_replay_execute(NULL);
		}
		replay_tick = tick;
	} else {
		aoview_replay_close();
	}
	return FALSE;
}

static void
aoview_replay_open(GtkWidget *widget, gpointer data)
{
	char		*replay_file_name;
	GtkWidget	*dialog;

	aoview_replay_close();
	replay_file_name = gtk_file_chooser_get_filename(replay_dialog);
	replay_file = fopen(replay_file_name, "r");
	if (!replay_file) {
		dialog = gtk_message_dialog_new(GTK_WINDOW(replay_dialog),
						GTK_DIALOG_DESTROY_WITH_PARENT,
						GTK_MESSAGE_ERROR,
						GTK_BUTTONS_CLOSE,
						"Error loading file '%s': %s",
						replay_file_name, g_strerror(errno));
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
	} else {
		replay_tick = -1;
		aoview_replay_read(NULL);
	}
	gtk_widget_hide(GTK_WIDGET(replay_dialog));
}

void
aoview_replay_init(GladeXML *xml)
{
	replay_dialog = GTK_FILE_CHOOSER(glade_xml_get_widget(xml, "ao_replay_dialog"));
	assert(replay_dialog);
	gtk_file_chooser_set_current_folder(replay_dialog, aoview_file_dir);

	replay_ok = glade_xml_get_widget(xml, "ao_replay_ok");
	assert(replay_ok);
	g_signal_connect(G_OBJECT(replay_ok), "clicked",
			 G_CALLBACK(aoview_replay_open),
			 replay_dialog);
}