summaryrefslogtreecommitdiff
path: root/src/include/disko.h
blob: e7dc14c1e9b97796fa0654549eb8fdf617897328 (plain)
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
128
129
130
131
132
133
134
135
136
/*
 * Schism Tracker - a cross-platform Impulse Tracker clone
 * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
 * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
 * copyright (c) 2009 Storlek & Mrs. Brisby
 * copyright (c) 2010-2012 Storlek
 * URL: http://schismtracker.org/
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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
 */
#ifndef __disko_h
#define __disko_h

#include <sys/types.h>


typedef struct disko disko_t;
struct disko {
	// Functions whose implementation depends on the backend in use
	// Use disko_write et al. instead of these.
	void (*_write)(disko_t *ds, const void *buf, size_t len);
	void (*_putc)(disko_t *ds, int c);
	void (*_seek)(disko_t *ds, long offset, int whence);
	long (*_tell)(disko_t *ds);

	// Temporary filename that's being written to
	char tempname[PATH_MAX];

	// Name to change it to on close (if successful)
	char filename[PATH_MAX];

	// these could be unionized
	// file pointer (only exists for disk files)
	FILE *file;
	// data for memory buffers (no filename/handle)
	uint8_t *data;

	// First errno value recorded after something went wrong.
	int error;

	/* untouched by diskwriter; driver may use for anything */
	void *userdata;

	// for memory buffers
	size_t pos, length, allocated;
};

enum {
	DW_OK = 1,
	DW_ERROR = 0,
	DW_NOT_RUNNING = -1,
};
enum {
	DW_SYNC_DONE = 0,
	DW_SYNC_ERROR = -1,
	DW_SYNC_MORE = 1,
};

/* fopen/fclose-ish writeout/finish wrapper that allocates a structure */
disko_t *disko_open(const char *filename);
/* Close the file. If there was no error writing the file, it is renamed
to the name specified in disko_open; otherwise, the original file is left
intact and the temporary file is deleted. Returns DW_OK on success,
DW_ERROR (and sets errno) if there was a file error.
'backup' parameter:
	<1  don't backup
	=1  backup to file~
	>1  keep numbered backups to file.n~
(the semantics of this might change later to allow finer control) */
int disko_close(disko_t *f, int backup);

/* alloc/free a memory buffer
if free_buffer is 0, the internal buffer is left alone when deallocating,
so that it can continue to be used later */
disko_t *disko_memopen(void);
int disko_memclose(disko_t *f, int free_buffer);


/* copy a pattern into a sample */
int disko_writeout_sample(int smpnum, int pattern, int bind);

/* copy a pattern into multiple samples (split by channel,
and writing into free slots starting at smpnum) */
int disko_multiwrite_samples(int firstsmp, int pattern);

/* Wrapper for the above that writes to the current sample,
and with a confirmation dialog if the sample already has data */
void song_pattern_to_sample(int pattern, int split, int bind);

/* export the song to a file */
struct save_format;
int disko_export_song(const char *filename, const struct save_format *format);

/* call periodically if (status.flags & DISKWRITER_ACTIVE) to write more stuff.
return: DW_SYNC_*, self explanatory */
int disko_sync(void);



/* For use by the diskwriter drivers: */

/* Write data to the file, as in fwrite() */
void disko_write(disko_t *ds, const void *buf, size_t len);

/* Write one character (unsigned char, cast to int) */
void disko_putc(disko_t *ds, int c);

/* Change file position. This CAN be used to seek past the end,
but be cognizant that random data might exist in the "gap". */
void disko_seek(disko_t *ds, long pos, int whence);

/* Get the position, as set by seek */
long disko_tell(disko_t *ds);

/* Call this to signal a nonrecoverable error condition. */
void disko_seterror(disko_t *ds, int err);

/* ------------------------------------------------------------------------- */

/* this call is used by audio/loadsave to send midi data */
int _disko_writemidi(const void *data, unsigned int len, unsigned int delay);

#endif
© All Rights Reserved