summaryrefslogtreecommitdiff
path: root/session.c
blob: 73b1eda5e7e181a2e714d3a69b452ba9d2cc1e55 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009  Alexey Yakovenko

    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "session.h"
#include "common.h"

#define SESS_CURRENT_VER 2

// NOTE: dont forget to update session_reset when changing that
char session_dir[2048];
float session_volume;
int8_t session_playlist_order;
int8_t session_playlist_looping;
int8_t session_scroll_follows_playback = 1;
int session_win_attrs[5] = { 40, 40, 500, 300, 0 };
static uint8_t sessfile_magic[] = { 0xdb, 0xef, 0x5e, 0x55 }; // dbefsess in hexspeak

void
session_reset (void) {
    session_volume = 0;
    session_dir[0] = 0;
    session_playlist_looping = 0;
    session_playlist_order = 0;
    session_scroll_follows_playback = 1;
    session_win_attrs[0] = 40;
    session_win_attrs[1] = 40;
    session_win_attrs[2] = 500;
    session_win_attrs[3] = 300;
    session_win_attrs[4] = 0;
}

static int
write_i16_be (uint16_t val, FILE *fp) {
    uint8_t b;
    b = (uint8_t)(val&0xff);
    if (fwrite (&b, 1, 1, fp) != 1) {
        return 0;
    }
    b = (uint8_t)((val&0xff00)>>8);
    if (fwrite (&b, 1, 1, fp) != 1) {
        return 1;
    }
    return 2;
}

static int
write_i32_be (uint32_t val, FILE *fp) {
    uint8_t b;
    b = (uint8_t)(val&0xff);
    if (fwrite (&b, 1, 1, fp) != 1) {
        return 0;
    }
    b = (uint8_t)((val&0xff00)>>8);
    if (fwrite (&b, 1, 1, fp) != 1) {
        return 1;
    }
    b = (uint8_t)((val&0xff0000)>>16);
    if (fwrite (&b, 1, 1, fp) != 1) {
        return 2;
    }
    b = (uint8_t)((val&0xff000000)>>24);
    if (fwrite (&b, 1, 1, fp) != 1) {
        return 3;
    }
    return 4;
}

static int
read_i16_be (uint16_t *pval, FILE *fp) {
    uint16_t val = 0;
    uint8_t b;
    if (fread (&b, 1, 1, fp) != 1) {
        return 0;
    }
    val |= (uint16_t)b;
    if (fread (&b, 1, 1, fp) != 1) {
        return 1;
    }
    val |= (((uint16_t)b) << 8);
    *pval = val;
    return 2;
}

static int
read_i32_be (uint32_t *pval, FILE *fp) {
    uint32_t val = 0;
    uint8_t b;
    if (fread (&b, 1, 1, fp) != 1) {
        return 0;
    }
    val |= (uint32_t)b;
    if (fread (&b, 1, 1, fp) != 1) {
        return 1;
    }
    val |= (((uint32_t)b) << 8);
    if (fread (&b, 1, 1, fp) != 1) {
        return 2;
    }
    val |= (((uint32_t)b) << 16);
    if (fread (&b, 1, 1, fp) != 1) {
        return 3;
    }
    val |= (((uint32_t)b) << 24);
    *pval = val;
    return 4;
}

int
session_save (const char *fname) {
    FILE *fp = fopen (fname, "w+b");
    if (!fp) {
        fprintf (stderr, "failed to save session, file %s could not be opened\n");
        return -1;
    }
    // magic
    if (fwrite (sessfile_magic, 1, 4, fp) != 4) {
        goto session_save_fail;
    }
    uint8_t version = SESS_CURRENT_VER;
    if (fwrite (&version, 1, 1, fp) != 1) {
        goto session_save_fail;
    }
    uint16_t l = strlen (session_dir);
    if (write_i16_be (l, fp) != 2) {
        goto session_save_fail;
    }
    if (fwrite (session_dir, 1, l, fp) != l) {
        goto session_save_fail;
    }
    if (write_i32_be (*((uint32_t*)&session_volume), fp) != 4) {
        goto session_save_fail;
    }
    if (fwrite (&session_playlist_order, 1, 1, fp) != 1) {
        goto session_save_fail;
    }
    if (fwrite (&session_playlist_looping, 1, 1, fp) != 1) {
        goto session_save_fail;
    }
    if (fwrite (&session_scroll_follows_playback, 1, 1, fp) != 1) {
        goto session_save_fail;
    }
    for (int k = 0; k < 5; k++) {
        if (write_i32_be (session_win_attrs[k], fp) != 4) {
            goto session_save_fail;
        }
    }
    fclose (fp);
    return 0;
session_save_fail:
    fprintf (stderr, "failed to save session, seems to be a disk error\n");
    fclose (fp);
    return -1;
}

int
session_load (const char *fname) {
    FILE *fp = fopen (fname, "r+b");
    if (!fp) {
        return -1;
    }
    // magic
    uint8_t magic[4];
    if (fread (magic, 1, 4, fp) != 4) {
        goto session_load_fail;
    }
    if (memcmp (magic, sessfile_magic, 4)) {
        goto session_load_fail;
    }
    uint8_t version;
    if (fread (&version, 1, 1, fp) != 1) {
        goto session_load_fail;
    }
    if (version != SESS_CURRENT_VER) {
        goto session_load_fail;
    }
    uint16_t l;
    if (read_i16_be (&l, fp) != 2) {
        goto session_load_fail;
    }
    if (l >= 2048) {
        goto session_load_fail;
    }
    if (fread (session_dir, 1, l, fp) != l) {
        goto session_load_fail;
    }
    session_dir[l] = 0;
    if (read_i32_be ((uint32_t*)&session_volume, fp) != 4) {
        goto session_load_fail;
    }
    if (fread (&session_playlist_order, 1, 1, fp) != 1) {
        goto session_load_fail;
    }
    if (session_playlist_order < 0 || session_playlist_order > 2) {
        goto session_load_fail;
    }
    if (fread (&session_playlist_looping, 1, 1, fp) != 1) {
        goto session_load_fail;
    }
    if (session_playlist_looping < 0 || session_playlist_looping > 2) {
        goto session_load_fail;
    }
    if (fread (&session_scroll_follows_playback, 1, 1, fp) != 1) {
        goto session_load_fail;
    }
    for (int k = 0; k < 5; k++) {
        if (read_i32_be (&session_win_attrs[k], fp) != 4) {
            goto session_load_fail;
        }
    }
//    printf ("dir: %s\n", session_dir);
//    printf ("volume: %f\n", session_volume);
//    printf ("win: %d %d %d %d %d\n", session_win_attrs[0], session_win_attrs[1], session_win_attrs[2], session_win_attrs[3], session_win_attrs[4]);
    fclose (fp);
    return 0;
session_load_fail:
    fprintf (stderr, "failed to load session, session file is corrupt\n");
    fclose (fp);
    session_reset ();
    return -1;
}

void
session_set_directory (const char *path) {
    strncpy (session_dir, path, 2048);
}

void
session_set_volume (float vol) {
    session_volume = vol;
}

const char *
session_get_directory (void) {
    return session_dir;
}

float
session_get_volume (void) {
    return session_volume;
}

void
session_set_playlist_order (int order) {
    session_playlist_order = order;
}

int
session_get_playlist_order (void) {
    return session_playlist_order;
}

void
session_set_playlist_looping (int looping) {
    session_playlist_looping = looping;
}

int
session_get_playlist_looping (void) {
    return session_playlist_looping;
}

void
session_set_scroll_follows_playback (int on) {
    session_scroll_follows_playback = on;
}

int
session_get_scroll_follows_playback (void) {
    return session_scroll_follows_playback;
}