~ubuntu-branches/ubuntu/raring/vice/raring

« back to all changes in this revision

Viewing changes to src/event.c

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2009-03-31 00:37:15 UTC
  • mfrom: (1.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: james.westby@ubuntu.com-20090331003715-mzclchtl0dp7fcl0
Tags: upstream-2.1.dfsg
ImportĀ upstreamĀ versionĀ 2.1.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "attach.h"
37
37
#include "clkguard.h"
38
38
#include "cmdline.h"
 
39
#include "crc32.h"
39
40
#include "datasette.h"
40
41
#include "debug.h"
41
42
#include "event.h"
50
51
#include "resources.h"
51
52
#include "snapshot.h"
52
53
#include "tape.h"
53
 
#ifdef HAS_TRANSLATION
54
54
#include "translate.h"
55
 
#endif
56
55
#include "types.h"
57
56
#include "ui.h"
58
57
#include "util.h"
93
92
static char *event_end_snapshot = NULL;
94
93
static char *event_snapshot_path_str = NULL;
95
94
static int event_start_mode;
 
95
static int event_image_include;
96
96
 
97
97
 
98
98
static char *event_snapshot_path(const char *snapshot_file)
118
118
    while (event_image_list_ptr->next != NULL) {
119
119
        if (strcmp(filename, event_image_list_ptr->next->orig_filename) == 0) {
120
120
            if (mapped_name != NULL) {
121
 
                if (append == 0)
122
 
                    *mapped_name = lib_stralloc(event_image_list_ptr->next->mapped_filename);
123
 
                else
 
121
                if (append == 0) {
 
122
                    if (event_image_list_ptr->next->mapped_filename != NULL) {
 
123
                        *mapped_name = lib_stralloc(event_image_list_ptr->next->mapped_filename);
 
124
                    } else {
 
125
                        return 1;
 
126
                    }
 
127
                } else {
124
128
                    event_image_list_ptr->next->mapped_filename = lib_stralloc(*mapped_name);
 
129
                }
125
130
            }
126
131
            return 0;
127
132
        }
128
 
 
129
133
        event_image_list_ptr = event_image_list_ptr->next;
130
134
    }
131
135
 
136
140
    event_image_list_ptr->next = NULL;
137
141
    event_image_list_ptr->orig_filename = lib_stralloc(filename);
138
142
    event_image_list_ptr->mapped_filename = NULL;
139
 
    if (mapped_name != NULL)
 
143
    if (mapped_name != NULL && append)
140
144
        event_image_list_ptr->mapped_filename = lib_stralloc(*mapped_name);
141
145
 
142
146
    return 1;
148
152
{
149
153
    char *event_data;
150
154
    unsigned int size;
 
155
    char *strdir, *strfile;
151
156
 
152
157
    list->current->type = EVENT_ATTACHIMAGE;
153
158
    list->current->clk = maincpu_clk;
154
159
    list->current->next
155
160
        = (event_list_t *)lib_calloc(1, sizeof(event_list_t));
156
161
 
157
 
    size = strlen(filename) + 3;
 
162
    util_fname_split(filename, &strdir, &strfile);
 
163
 
 
164
    if (event_image_include)
 
165
        size = strlen(filename) + 3;
 
166
    else
 
167
        size = strlen(strfile) + sizeof(long) + 4;
158
168
 
159
169
    event_data = lib_malloc(size);
160
170
    event_data[0] = unit;
161
171
    event_data[1] = read_only;
162
 
    strcpy(&event_data[2], filename);
163
172
 
164
 
    if (event_image_append(filename, NULL, 0) == 1) {
165
 
        FILE *fd;
166
 
        size_t file_len = 0;
 
173
    if (event_image_include) {
 
174
        strcpy(&event_data[2], filename);
 
175
        if (event_image_append(filename, NULL, 0) == 1) {
 
176
            FILE *fd;
 
177
            size_t file_len = 0;
167
178
        
168
 
        fd = fopen(filename, MODE_READ);
169
 
 
170
 
        if (fd != NULL) {
171
 
            file_len = util_file_length(fd);
172
 
            event_data = lib_realloc(event_data, size + file_len);
173
 
 
174
 
            if (fread(&event_data[size], file_len, 1, fd) != 1)
175
 
                log_error(event_log, "Cannot load image file %s", filename);
176
 
 
177
 
            fclose(fd);
178
 
        } else {
179
 
            log_error(event_log, "Cannot open image file %s", filename);
 
179
            fd = fopen(filename, MODE_READ);
 
180
 
 
181
            if (fd != NULL) {
 
182
                file_len = util_file_length(fd);
 
183
                event_data = lib_realloc(event_data, size + file_len);
 
184
 
 
185
                if (fread(&event_data[size], file_len, 1, fd) != 1)
 
186
                    log_error(event_log, "Cannot load image file %s", filename);
 
187
 
 
188
                fclose(fd);
 
189
            } else {
 
190
                log_error(event_log, "Cannot open image file %s", filename);
 
191
            }
 
192
            size += file_len;
180
193
        }
181
 
        size += file_len;
 
194
    } else {
 
195
        strcpy(&event_data[2], "");
 
196
        *(unsigned long *)(event_data + 3) = crc32_file(filename);
 
197
        strcpy(&event_data[3 + sizeof(long)], strfile);
182
198
    }
183
199
 
 
200
    lib_free(strdir);
 
201
    lib_free(strfile);
 
202
 
184
203
    list->current->size = size;
185
204
    list->current->data = event_data;
186
205
    list->current = list->current->next;
201
220
    unsigned int unit, read_only;
202
221
    char *orig_filename, *filename = NULL;
203
222
    size_t file_len;
 
223
    unsigned long crc_to_attach;
204
224
 
205
225
    unit = (unsigned int)((char*)data)[0];
206
226
    read_only = (unsigned int)((char*)data)[1];
207
227
    orig_filename = &((char*)data)[2];
208
 
    file_len  = size - strlen(orig_filename) - 3;
209
 
 
210
 
    if (file_len > 0) {
211
 
        FILE *fd;
212
 
 
213
 
        fd = archdep_mkstemp_fd(&filename, MODE_WRITE);
214
 
 
215
 
        if (fd == NULL) {
216
 
#ifdef HAS_TRANSLATION
217
 
            ui_error(translate_text(IDGS_CANNOT_CREATE_IMAGE), filename);
218
 
#else
219
 
            ui_error(_("Cannot create image file!"));
220
 
#endif
221
 
            goto error;
222
 
        }
223
 
 
224
 
        if (fwrite((char*)data + strlen(orig_filename) + 3, file_len, 1, fd) != 1) {
225
 
#ifdef HAS_TRANSLATION
226
 
            ui_error(translate_text(IDGS_CANNOT_WRITE_IMAGE_FILE_S), filename);
227
 
#else
228
 
            ui_error(_("Cannot write image file %s"), filename);
229
 
#endif
230
 
            goto error;
231
 
        }
232
 
 
233
 
        fclose(fd);
234
 
        event_image_append(orig_filename, &filename, 1);
235
 
    } else {
 
228
 
 
229
    if (*orig_filename == 0) {
 
230
        /* no image attached */
 
231
        orig_filename = (char *) data + 3 + sizeof(long);
 
232
 
236
233
        if (event_image_append(orig_filename, &filename, 0) != 0) {
237
 
#ifdef HAS_TRANSLATION
238
 
            ui_error(translate_text(IDGS_CANNOT_FIND_MAPPED_NAME_S), orig_filename);
239
 
#else
240
 
            ui_error(_("Cannot find mapped name for %s"), orig_filename);
241
 
#endif
242
 
            return;
 
234
            crc_to_attach = *(unsigned long *)(((char *)data)+3);
 
235
            do {
 
236
                filename = ui_get_file("Please attach image %s (CRC32 checksum 0x%x)",
 
237
                            (char *) data + 3 + sizeof(long), crc_to_attach);
 
238
            } while (filename != NULL && crc_to_attach != crc32_file(filename));
 
239
            if (filename == NULL) {
 
240
                ui_error("Image wasn't attached. Playback will probably get out of sync.");
 
241
                return;
 
242
            }
 
243
            event_image_append(orig_filename, &filename, 1);
 
244
        }
 
245
    } else {
 
246
        file_len  = size - strlen(orig_filename) - 3;
 
247
 
 
248
        if (file_len > 0) {
 
249
            FILE *fd;
 
250
 
 
251
            fd = archdep_mkstemp_fd(&filename, MODE_WRITE);
 
252
    
 
253
            if (fd == NULL) {
 
254
                ui_error(translate_text(IDGS_CANNOT_CREATE_IMAGE), filename);
 
255
                goto error;
 
256
            }
 
257
 
 
258
            if (fwrite((char*)data + strlen(orig_filename) + 3, file_len, 1, fd) != 1) {
 
259
                ui_error(translate_text(IDGS_CANNOT_WRITE_IMAGE_FILE_S), filename);
 
260
                goto error;
 
261
            }
 
262
 
 
263
            fclose(fd);
 
264
            event_image_append(orig_filename, &filename, 1);
 
265
        } else {
 
266
            if (event_image_append(orig_filename, &filename, 0) != 0) {
 
267
                ui_error(translate_text(IDGS_CANNOT_FIND_MAPPED_NAME_S), orig_filename);
 
268
                return;
 
269
            }
243
270
        }
244
271
    }
245
 
 
246
272
    /* now filename holds the name to attach    */
247
273
    /* FIXME: read_only isn't handled for tape  */
248
274
    if (unit == 1) {
614
640
      case EVENT_START_MODE_FILE_SAVE:
615
641
        if (machine_write_snapshot(event_snapshot_path(event_start_snapshot),
616
642
                                    1, 1, 0) < 0) {
617
 
#ifdef HAS_TRANSLATION
618
643
            ui_error(translate_text(IDGS_CANT_CREATE_START_SNAP_S), 
619
 
#else
620
 
            ui_error(_("Could not create start snapshot file %s."), 
621
 
#endif
622
644
                        event_snapshot_path(event_start_snapshot));
623
645
            ui_display_recording(0);
624
646
            return;
633
655
      case EVENT_START_MODE_FILE_LOAD:
634
656
        if (machine_read_snapshot(
635
657
                event_snapshot_path(event_end_snapshot), 1) < 0) {
636
 
#ifdef HAS_TRANSLATION
637
658
            ui_error(translate_text(IDGS_ERROR_READING_END_SNAP_S),
638
 
#else
639
 
            ui_error(_("Error reading end snapshot file %s."),
640
 
#endif
641
659
                        event_snapshot_path(event_end_snapshot));
642
660
            return;
643
661
        }
701
719
{
702
720
    if (machine_write_snapshot(
703
721
            event_snapshot_path(event_end_snapshot), 1, 1, 1) < 0) {
704
 
#ifdef HAS_TRANSLATION
705
722
        ui_error(translate_text(IDGS_CANT_CREATE_END_SNAP_S),
706
 
#else
707
 
        ui_error(_("Could not create end snapshot file %s."),
708
 
#endif
709
723
                    event_snapshot_path(event_end_snapshot));
710
724
        return;
711
725
    }
769
783
        event_snapshot_path(event_end_snapshot), &major, &minor, machine_name);
770
784
 
771
785
    if (s == NULL) {
772
 
#ifdef HAS_TRANSLATION
773
786
        ui_error(translate_text(IDGS_CANT_OPEN_END_SNAP_S), 
774
 
#else
775
 
        ui_error(_("Could not open end snapshot file %s."), 
776
 
#endif
777
787
                    event_snapshot_path(event_end_snapshot));
778
788
        ui_display_playback(0, NULL);
779
789
        return;
784
794
 
785
795
    if (event_snapshot_read_module(s, 1) < 0) {
786
796
        snapshot_close(s);
787
 
#ifdef HAS_TRANSLATION
788
797
        ui_error(translate_text(IDGS_CANT_FIND_SECTION_END_SNAP));
789
 
#else
790
 
        ui_error(_("Could not find event section in end snapshot file."));
791
 
#endif
792
798
        ui_display_playback(0, NULL);
793
799
        return;
794
800
    }
808
814
                    event_snapshot_path(event_start_snapshot), 0) < 0)
809
815
            {
810
816
                char *st = lib_stralloc(event_snapshot_path((char *)(&data[1])));
811
 
#ifdef HAS_TRANSLATION
812
817
                ui_error(translate_text(IDGS_ERROR_READING_START_SNAP_TRIED),
813
 
#else
814
 
                ui_error(_("Error reading start snapshot file. Tried %s and %s"),
815
 
#endif
816
818
                            st, event_snapshot_path(event_start_snapshot));
817
819
                lib_free(st);
818
820
                ui_display_playback(0, NULL);
838
840
    } else {
839
841
        if (machine_read_snapshot(
840
842
                event_snapshot_path(event_start_snapshot), 0) < 0) {
841
 
#ifdef HAS_TRANSLATION
842
843
            ui_error(translate_text(IDGS_ERROR_READING_START_SNAP));
843
 
#else
844
 
            ui_error(_("Error reading start snapshot file."));
845
 
#endif
846
844
            ui_display_playback(0, NULL);
847
845
            return;
848
846
        }
895
893
{
896
894
    if (machine_write_snapshot(
897
895
        event_snapshot_path(event_end_snapshot), 1, 1, 1) < 0) {
898
 
#ifdef HAS_TRANSLATION
899
896
            ui_error(translate_text(IDGS_CANT_CREATE_END_SNAP_S),
900
 
#else
901
 
            ui_error(_("Could not create end snapshot file %s."),
902
 
#endif
903
897
                        event_snapshot_path(event_end_snapshot));
904
898
    } else {
905
899
        milestone_timestamp_alarm = next_timestamp_clk;
928
922
 
929
923
    if (machine_read_snapshot(
930
924
            event_snapshot_path(event_end_snapshot), 1) < 0) {
931
 
#ifdef HAS_TRANSLATION
932
925
        ui_error(translate_text(IDGS_ERROR_READING_END_SNAP_S),
933
 
#else
934
 
        ui_error(_("Error reading end snapshot file %s."),
935
 
#endif
936
926
                    event_snapshot_path(event_end_snapshot));
937
927
        return;
938
928
    }
1173
1163
    return 0;
1174
1164
}
1175
1165
 
 
1166
static int set_event_image_include(int enable, void *param)
 
1167
{
 
1168
    event_image_include = enable;
 
1169
    return 0;
 
1170
}
 
1171
 
1176
1172
static const resource_string_t resources_string[] = {
1177
1173
    { "EventSnapshotDir", 
1178
1174
      FSDEVICE_DEFAULT_DIR FSDEV_DIR_SEP_STR, RES_EVENT_NO, NULL,
1187
1183
static const resource_int_t resources_int[] = {
1188
1184
    { "EventStartMode", EVENT_START_MODE_FILE_SAVE, RES_EVENT_NO, NULL,
1189
1185
      &event_start_mode, set_event_start_mode, NULL },
 
1186
    { "EventImageInclude", 1, RES_EVENT_NO, NULL,
 
1187
      &event_image_include, set_event_image_include, NULL },
1190
1188
    { NULL }
1191
1189
};
1192
1190
 
1215
1213
    return event_playback_start();
1216
1214
}
1217
1215
 
1218
 
#ifdef HAS_TRANSLATION
1219
 
static const cmdline_option_t cmdline_options[] = {
1220
 
    { "-playback", CALL_FUNCTION, 0, cmdline_help, NULL, NULL, NULL,
1221
 
      0, IDCLS_PLAYBACK_RECORDED_EVENTS },
1222
 
    { NULL }
1223
 
};
1224
 
#else
1225
 
static const cmdline_option_t cmdline_options[] = {
1226
 
    { "-playback", CALL_FUNCTION, 0, cmdline_help, NULL, NULL, NULL,
1227
 
      NULL, N_("Playback recorded events") },
1228
 
    { NULL }
1229
 
};
1230
 
#endif
 
1216
static const cmdline_option_t cmdline_options[] = {
 
1217
    { "-playback", CALL_FUNCTION, 0,
 
1218
      cmdline_help, NULL, NULL, NULL,
 
1219
      USE_PARAM_STRING, USE_DESCRIPTION_ID,
 
1220
      IDCLS_UNUSED, IDCLS_PLAYBACK_RECORDED_EVENTS,
 
1221
      NULL, NULL },
 
1222
    { NULL }
 
1223
};
1231
1224
 
1232
1225
int event_cmdline_options_init(void)
1233
1226
{