-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.c
155 lines (125 loc) · 2.72 KB
/
Common.c
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
/* This file is part of the Elliott 803 emulator.
Copyright © 2020 Peter Onion
See LICENCE file.
*/
#define G_LOG_USE_STRUCTURED
#include <stdio.h>
#include <fcntl.h>
#include <gtk/gtk.h>
#include "Common.h"
#include "Sound.h"
struct sndEffect *readWavData(const char *wavFileName)
{
int fd;
ssize_t n;
char header[40];
size_t length;
guint32 len;
char *buffer = NULL;
struct sndEffect *se;
fd = open(wavFileName,O_RDONLY);
if(fd == -1)
{
// Not all sound effect files exist, so just ignore missing ones.
//g_debug("Failed to open file (%s)\n",wavFileName);
return NULL;
}
n = read(fd,header,40);
length = 0;
n = read(fd,&len,4);
length = (size_t) len;
buffer = (char *) malloc(length);
n = read(fd,buffer,length);
if ((n<0) || ((size_t)n != length))
{
g_error("read failed for bulk of %s\n",wavFileName);
free(buffer);
buffer = NULL;
}
close(fd);
se = (struct sndEffect *) malloc(sizeof(struct sndEffect));
se->frames = (gint16 *) buffer;
se->frameCount = (int) (length/2);
return(se);
}
/*********************** FSM support *********************/
void doFSM(struct fsm *fsm,int event,void *data)
{
int (*handler)(int,int,void *,guint);
int state;
struct fsmtable *fsmEntry;
nextEvent:
fsmEntry = fsm->table;
state = fsm->state;
if(fsm->debugFlag)
{
g_debug("\tFSM %s ",fsm->name);
if(fsm->stateNames != NULL)
{
g_debug("state %s ",(fsm->stateNames)[state]);
}
else
{
g_debug("state %d ",state);
}
if(fsm->eventNames != NULL)
{
if(event < 0)
g_debug("event = FSMNoEvent ");
else
g_debug("event %s \n",(fsm->eventNames)[event]);
}
else
{
g_debug("event %d \n",event);
}
}
while(fsmEntry->state != -1)
{
if( (fsmEntry->state == state) && (fsmEntry->event == event))
{
handler = fsmEntry->handler;
if(handler == NULL)
{
state = fsmEntry->nextState;
}
else
{
state = (handler)(state,event,data,0);
if(state == -1)
state = fsmEntry->nextState;
}
fsm->state = state;
if(fsm->nextEvent != -1)
{
event = fsm->nextEvent;
fsm->nextEvent = -1;
goto nextEvent;
}
return;
}
fsmEntry++;
}
if(fsm->debugFlag)
{
if(fsm->stateNames == NULL)
{
g_debug("No entry in FSM table %s for state %d ",fsm->name,state);
}
else
{
g_debug("No entry in FSM table %s for state %s",fsm->name,(fsm->stateNames)[state]);
}
if(fsm->eventNames == NULL)
{
g_debug("event = %d",event);
}
else
{
if(event < 0)
g_debug("event = FSMNoEvent ");
else
g_debug("event = %s",(fsm->eventNames)[event]);
}
}
}