File: player.c

package info (click to toggle)
termrec 0.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,148 kB
  • sloc: ansic: 8,430; makefile: 181; perl: 16; sh: 15
file content (278 lines) | stat: -rw-r--r-- 6,118 bytes parent folder | download | duplicates (3)
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
#include "config.h"
#include <tty.h>
#include <ttyrec.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "error.h"
#include "gettext.h"
#include "sys/threads.h"
#include "play/player.h"

static ttyrec_frame fr;
static struct timeval t0, tc;

ttyrec tr;

int speed;
tty term;

int waiting, loaded;
mutex_t waitm;
cond_t waitc;

static void waitm_unlock(void *arg)
{
    mutex_unlock(waitm);
}

static int player(void *arg)
{
    ttyrec_frame nf;
    struct timeval tt, wall;

    do
    {
        while ((nf=ttyrec_next_frame(tr, fr)))
        {
            tt=nf->t;
            tdiv1000(tt, speed);
            tadd(tt, t0);
            gettimeofday(&wall, 0);
            tsub(tt, wall);
            if (tt.tv_sec>0 || tt.tv_usec>0)
                select(0, 0, 0, 0, &tt);
            else if (tt.tv_sec<-1) // if not a minimal skip, slow down the clock
                tsub(t0, tt);      // (tt is negative)
            pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
            fr=nf;
            tty_write(term, fr->data, fr->len);
            pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
        }
        mutex_lock(waitm);
        if (!(nf=ttyrec_next_frame(tr, fr)) && !loaded)
            waiting=1;
        if (waiting)
        {
            pthread_cleanup_push(waitm_unlock, 0);
            cond_wait(waitc, waitm);
            pthread_cleanup_pop(0);
            nf=ttyrec_next_frame(tr, fr);
        }
        mutex_unlock(waitm);
    }
    while (nf || !loaded);
    return 0;
}


static thread_t playth;
static int play_state;

static void replay_start(void)
{
    if (play_state)
        return;
    play_state=1;
    gettimeofday(&t0, 0);
    tdiv1000(tc, speed);
    tsub(t0, tc);
    if (thread_create_joinable(&playth, player, 0))
        die("Cannot create thread");
}


static void replay_stop(void)
{
    if (!play_state)
        return;
    gettimeofday(&tc, 0);       // calc where we stopped
    tsub(tc, t0);
    tmul1000(tc, speed);
    play_state=0;
    pthread_cancel(playth);
    thread_join(playth);
    if (loaded && !ttyrec_next_frame(tr, fr))
        tc=fr->t;               // no stopping past the end
}


static void adjust_pos(int diff)
{
    int old_state=play_state;

    replay_stop();
    tc.tv_sec+=diff;
    if (tc.tv_sec<0)
        tc.tv_sec=tc.tv_usec=0;
    fr=ttyrec_seek(tr, &tc, &term);
    vtvt_attach(term, stdout, 1);
    if (old_state)
        replay_start();
}


static void adjust_speed(int dir)
{
    int old_state=play_state;

    replay_stop();
    switch (dir)
    {
    case 0:
        speed=1000;
        break;
    case -1:
        if ((speed/=2)==0)
            speed=1;
        break;
    case +1:
        if ((speed*=2)>1000000)
            speed=1000000;
        break;
    }
    if (old_state)
        replay_start();
}


static void replay_toggle(int dummy)
{
    if (play_state)
        replay_stop();
    else
        replay_start();
}


static void replay_rewind(int dummy)
{
    replay_stop();
    tc.tv_sec=tc.tv_usec=0;
}

static void adv_frame(int dummy)
{
    ttyrec_frame nf;

    replay_stop();
    if (!(nf=ttyrec_next_frame(tr, fr)))
        return;
    tc=nf->t;
    tty_write(term, nf->data, nf->len);
    fr=nf;
}

static struct bind
{
    const char *keycode;
    void(*func)(int);
    int arg;
} binds[]=
{
{"q",0/*quit*/,0},      // q/Q          -> quit
{"Q",0/*quit*/,0},
{" ",replay_toggle,0},  // space        -> play/pause
{"\e[D",adjust_pos,-10},// left arrow   -> -10sec
{"\eOD",adjust_pos,-10},
{"\eOt",adjust_pos,-10},
{"\e[C",adjust_pos,+10},// right arrow  -> +10sec
{"\eOC",adjust_pos,+10},
{"\eOv",adjust_pos,+10},
{"\e[B",adjust_pos,-60},// down arrow   -> -1min
{"\eOB",adjust_pos,-60},
{"\eOr",adjust_pos,-60},
{"\e[A",adjust_pos,+60},// up arrow     -> +1min
{"\eOA",adjust_pos,+60},
{"\eOx",adjust_pos,+60},
{"\e[6~",adjust_pos,-600},      // PgDn -> -10min
{"\eOs", adjust_pos,-600},
{"\e[5~",adjust_pos,+600},      // PgUp -> +10min
{"\eOy", adjust_pos,+600},
{"r",replay_rewind,0},  // r/R          -> rewind
{"R",replay_rewind,0},
{"1",adjust_speed,0},   // 1            -> speed 1.0
{"s",adjust_speed,-1},  // s/S/-        -> slow down
{"S",adjust_speed,-1},
{"-",adjust_speed,-1},
{"f",adjust_speed,+1},  // f/F/+        -> speed up
{"F",adjust_speed,+1},
{"+",adjust_speed,+1},
{"\n",adv_frame,+1},    // Enter        -> next frame
{"\r",adv_frame,+1},
{"\eOM",adv_frame,+1},
{0,0,0},
};

static char keycode[10];

#define GETKEY                  \
        if ((ch=getchar())==EOF)\
            goto end;           \
        *kptr++=ch;             \
        switch (ch)
void replay(void)
{
    char *kptr;
    int ch;
    struct bind *bp;

    play_state=0;
    tc.tv_sec=tc.tv_usec=0;
    fr=ttyrec_seek(tr, 0, &term);
    vtvt_attach(term, stdout, 1);
    replay_start();

    while (1)
    {
        kptr=keycode;
        GETKEY
        {
        case 27:
            GETKEY
            {
            case 'O':
                GETKEY{}
                // ESC O A
                break;
            case '[':
                GETKEY
                {
                case 'O':
                    GETKEY{}
                    // ESC [ O A
                    break;
                case '[':
                    GETKEY{}
                    // ESC [ [ A
                    break;
                case '0': case '1': case '2': case '3': case '4':
                case '5': case '6': case '7': case '8': case '9':
                    while (ch>='0' && ch<='9' && kptr-keycode<6)
                    {
                        GETKEY{}
                        // ESC [ 11 ~
                    }
                    break;
                // ESC [ A
                }
                break;
            }
            break;
        }
        *kptr=0;
        for (bp=binds; bp->keycode; bp++)
            if (!strcmp(bp->keycode, keycode))
            {
                if (bp->func)
                    bp->func(bp->arg);
                else
                    goto end;
            }

    }
end:
    replay_stop();
    tty_printf(term, "\e[f\e[200B");
    tty_free(term);
}