aboutsummaryrefslogtreecommitdiff
path: root/common/fmplayer_drumrom_unix.c
blob: 587be20857d6cfca5b7bd4b326724999f7bcd82e (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
#include "fmplayer_drumrom.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libopna/opnadrum.h"

static struct {
  uint8_t drum_rom[OPNA_ROM_SIZE];
  bool loaded;
} g;

#define DATADIR "/.local/share/fmplayer/"

static void loadfile(void) {
  const char *path = "ym2608_adpcm_rom.bin";
  const char *home = getenv("HOME");
  char *dpath = 0;
  if (home) {
    const char *datadir = DATADIR;
    dpath = malloc(strlen(home)+strlen(datadir)+strlen(path) + 1);
    if (dpath) {
      strcpy(dpath, home);
      strcat(dpath, datadir);
      strcat(dpath, path);
      path = dpath;
    }
  }
  FILE *rhythm = fopen(path, "r");
  free(dpath);
  if (!rhythm) goto err;
  if (fseek(rhythm, 0, SEEK_END) != 0) goto err_file;
  long size = ftell(rhythm);
  if (size != OPNA_ROM_SIZE) goto err_file;
  if (fseek(rhythm, 0, SEEK_SET) != 0) goto err_file;
  if (fread(g.drum_rom, 1, OPNA_ROM_SIZE, rhythm) != OPNA_ROM_SIZE) goto err_file;
  fclose(rhythm);
  g.loaded = true;
  return;
err_file:
  fclose(rhythm);
err:
  return;
}

bool fmplayer_drum_rom_load(struct opna_drum *drum) {
  if (!g.loaded) {
    loadfile();
  }
  if (g.loaded) {
    opna_drum_set_rom(drum, g.drum_rom);
  }
  return g.loaded;
}