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
|
/*
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 <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <assert.h>
#include "timeline.h"
#include "threading.h"
timeline_t *
timeline_create (void) {
timeline_t *tl = malloc (sizeof (timeline_t));
memset (tl, 0, sizeof (timeline_t));
return tl;
}
void
timeline_free (timeline_t *tl, int wait) {
if (tl->tid && wait) {
int tid = tl->tid;
tl->destroy = 1;
thread_join (tid);
}
else {
tl->destroy = 1;
}
}
void
timeline_init (timeline_t *tl, float seconds, float fps, int (*callback)(float _progress, int _last, void *_ctx), void *ctx) {
tl->fps = fps;
tl->duration = seconds;
tl->progress = 0;
tl->callback = callback;
tl->callback_ctx = ctx;
tl->destroy = 0;
tl->stop = 0;
}
void
timeline_stop (timeline_t *tl, int wait) {
int tid = tl->tid;
if (tid) {
tl->stop = 1;
if (wait) {
thread_join (tid);
}
}
}
void
timeline_thread_func (uintptr_t ctx) {
printf ("timeline thread started\n");
timeline_t *tl = (timeline_t *)ctx;
for (;;) {
if (tl->stop || tl->destroy) {
tl->callback (1, 1, tl->callback_ctx);
break;
}
struct timeval tm;
gettimeofday (&tm, NULL);
float dt = (tm.tv_sec - tl->time.tv_sec) + (tm.tv_usec - tl->time.tv_usec) / 1000000.0;
float t = tl->progress;
tl->progress += dt;
memcpy (&tl->time, &tm, sizeof (tm));
if (t > tl->duration) {
tl->callback (1, 1, tl->callback_ctx);
break;
}
else {
if (tl->callback (t/tl->duration, 0, tl->callback_ctx) < 0) {
break;
}
}
printf ("progress: %f\n", tl->progress);
// sleep until next frame
usleep (1000000 / tl->fps);
}
tl->tid = 0;
if (tl->destroy) {
printf ("timeline %p destroyed\n", tl);
free (tl);
}
printf ("timeline %p thread terminated\n", tl);
}
void
timeline_start (timeline_t *tl) {
gettimeofday (&tl->time, NULL);
tl->progress = 0;
tl->stop = 0;
tl->destroy = 0;
if (!tl->tid) {
tl->tid = thread_start (timeline_thread_func, (uintptr_t)tl);
}
else {
printf ("reusing existing thread\n");
}
}
|