1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id: keyboard.c 2268 2006-11-06 17:18:51Z roms $ */
3
4 /* TiEmu - Tiemu Is an EMUlator
5 *
6 * Copyright (c) 2007, Romain Li�vin
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdio.h>
28
29 #include "intl.h"
30 #include "struct.h"
31 #include "tie_error.h"
32 #include "keymap.h"
33
34 static FILE *f_rec = NULL;
35 static FILE *f_ply = NULL;
36
37 static GTimer* timer = NULL;
38
kp_recording_stop(void)39 int kp_recording_stop(void)
40 {
41 fclose(f_rec);
42 f_rec = NULL;
43
44 g_timer_destroy(timer);
45 timer = NULL;
46
47 return 0;
48 }
49
kp_recording_start(const char * filename)50 int kp_recording_start(const char *filename)
51 {
52 if(f_rec)
53 kp_recording_stop();
54
55 f_rec = fopen(filename, "wt");
56 if(f_rec == NULL)
57 {
58 tiemu_err(0, _("Can't open file."));
59 return -1;
60 }
61
62 timer = g_timer_new();
63 g_timer_start(timer);
64
65 return 0;
66 }
67
kp_playing_stop(void)68 int kp_playing_stop(void)
69 {
70 fclose(f_ply);
71 f_ply = NULL;
72
73 return 0;
74 }
75
kp_playing_start(const char * filename)76 int kp_playing_start(const char *filename)
77 {
78 if(f_ply)
79 kp_playing_stop();
80
81 f_ply = fopen(filename, "rt");
82 if(f_ply == NULL)
83 {
84 tiemu_err(0, _("Can't open file."));
85 return -1;
86 }
87
88 return 0;
89 }
90
kp_recording_key(int key,int action)91 int kp_recording_key(int key, int action)
92 {
93 if(options.kp_rec_enabled && f_rec)
94 {
95 fprintf(f_rec, "%f:%s=%i\n",
96 g_timer_elapsed(timer, NULL),
97 keymap_value_to_string(tikeys, key),
98 action);
99 }
100
101 return 0;
102 }
103
kp_playing_key(int * key,int * action)104 int kp_playing_key(int *key, int *action)
105 {
106 double time;
107 char line[256];
108 char **split;
109
110 if(f_ply == NULL) return -1;
111 if(feof(f_ply)) return -1;
112
113 fgets(line, sizeof(line), f_ply);
114 split = g_strsplit_set(line, ":=", 3);
115
116 if(!split[0] || !split[1] || !split[2])
117 return -1;
118
119 sscanf(split[0], "%f", &time);
120 *key = keymap_string_to_value(tikeys, split[1]);
121 *action = (split[2][0] == '1') ? 1 : 0;
122
123 return 0;
124 }
125