root/Player.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
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
#include <ncurses.h>
#include "Player.h"
#include "CurlResult.h"

void Player_init(struct Player* this) {
    int x, y;
    getmaxyx(stdscr, y,x);
    this->window = newwin(PLAYER_H, x, y-PLAYER_H, 0);
    this->paused = true;
    this->progress.duration = 0;
    this->progress.current = 0;
}

void Player_destroyMembers(struct Player* this) {

}

bool Player_handleInput(struct Player* this, int event) {
    if (event == 's' || event == 'c') {
        Player_stop(this->baseUrl);
    } else if (event == '>') {
        Player_moveForwards(this->baseUrl);
    } else if (event == '<') {
        Player_moveBackwards(this->baseUrl);
    } else if (event == ' ') {
        Player_togglePause(this->baseUrl);
    } else if (event == KEY_RESIZE) {
        int screenX, screenY;
        getmaxyx(stdscr, screenY, screenX);
        mvwin(this->window, screenY-PLAYER_H, 0);
        wresize(this->window, PLAYER_H, screenX);
    } else {
        return false;
    }
    return true;
}

void Player_draw(struct Player* this) {
    /* refresh(); */
    /* int oldX, oldY; */
    /* getyx(shows.window, oldY, oldX); */
    /* refresh(); */

    int screenW = getmaxx(this->window);

    wmove(this->window, 0,0);
    wprintw(this->window, "■ (c)     ");
    wprintw(this->window, "<<(<) ");
    wprintw(this->window, this->paused ? "▶ " : "||");
    wprintw(this->window, "( ) ");
    wprintw(this->window, ">>(>)");


    /* draw progress bar */
    if (this->paused) {
        this->progress.lastUpdate = time(NULL);
    } else {
        time_t now = time(NULL);
        double diff = difftime(now, this->progress.lastUpdate);
        if (diff > 0) {
            this->progress.current += diff;
            this->progress.lastUpdate = now;
        }
    }

    int x = sizeof("■ (c)     " "<<(<) " "||" "( ) " ">>(>)")-1;
    int w = (screenW - x - 2 > 1) ? (screenW - x - 2) : 1;
    wprintw(this->window, " [");

    float percent = this->progress.duration != 0
        ? (float)this->progress.current / (float)this->progress.duration
        : 0.5f;

    for (int i=0; i < w; ++i) {
        float p = (float)i / (float)w;
        float nextP = (float)(i+1) / (float)w;
        if (p <= percent) {
            if (nextP <= percent) {
                wprintw(this->window, "=");
            } else {
                wprintw(this->window, "O");
            }
        } else {
            wprintw(this->window, "─");
        }
    }
    wprintw(this->window, "]");

    /* wprintw(this->window, "================="); */
    wrefresh(this->window);
}

void Player_simpleCommand(const char* baseUrl, const char* pathUrl) {
    char url[2048];
    sprintf(url, "%s%s", baseUrl, pathUrl);

    struct CurlResult userdata;
    CURL* handle = Curl_defaultHandle(url, &userdata);
    curl_easy_perform(handle);

    CurlResult_destroyMembers(&userdata);
    curl_easy_cleanup(handle);
}

void Player_stop(const char* baseUrl) {
    Player_simpleCommand(baseUrl, "/api/player/stop");
}
void Player_togglePause(const char* baseUrl) {
    Player_simpleCommand(baseUrl, "/api/player/togglePause");
}
void Player_moveBackwards(const char* baseUrl) {
    Player_simpleCommand(baseUrl, "/api/player/backwards");
}
void Player_moveForwards(const char* baseUrl) {
    Player_simpleCommand(baseUrl, "/api/player/forwards");
}

void restoreMetadata(struct Player* this, json_value* json) {
    if (!json || json->type != json_object) { return; }
    for (int j=0; j < json->u.object.length; ++j) {
        char* mItemName = json->u.object.values[j].name;
        json_value* mValue = json->u.object.values[j].value;
        if (0 == strcmp(mItemName, "duration") && mValue->type == json_integer) {
            this->progress.duration = mValue->u.integer;
        }
    }
}

void Player_onServerEvent(const char* event, const char* data, void* userdata) {
    struct Player* this = userdata;

    if (0 == strcmp(event, "playbackStarted")) {
        json_value* json = json_parse(data, strlen(data));
        if (json && json->type == json_object) {
            for (int i=0; i < json->u.object.length; ++i) {
                char* itemName = json->u.object.values[i].name;
                json_value* itemValue = json->u.object.values[i].value;
                if (0 == strcmp(itemName, "metaData") && itemValue->type == json_object) {
                    restoreMetadata(this, itemValue);
                } else if (0 == strcmp(itemName, "seconds") && itemValue->type == json_integer) {
                    this->progress.current = itemValue->u.integer;
                }
            }
        }
        this->paused = false;
        this->progress.lastUpdate = time(NULL);
    } else if (0 == strcmp(event, "playbackEnded")) {
        this->paused = true;
        this->progress.duration = 0;
        this->progress.current = 0;
        this->progress.lastUpdate = time(NULL);
    } else if (0 == strcmp(event, "paused")) {
        this->paused = true;
        this->progress.lastUpdate = time(NULL);
    } else if (0 == strcmp(event, "unpaused")) {
        this->paused = false;
        this->progress.lastUpdate = time(NULL);
    } else if (0 == strcmp(event, "jumped")) {
        /* {"seconds":3} */
        json_value* json = json_parse(data, strlen(data));
        if (json && json->type == json_object) {
            for (int i=0; i < json->u.object.length; ++i) {
                char* itemName = json->u.object.values[i].name;
                json_value* itemValue = json->u.object.values[i].value;
                if (0 == strcmp(itemName, "seconds") && itemValue->type == json_integer) {
                    this->progress.current = itemValue->u.integer;
                }
            }
        }
        this->progress.lastUpdate = time(NULL);
    }

    Player_draw(this);
}

void restorePauseStatus(struct Player* this, json_value* json) {
    if (!json || json->type != json_object) { return; }
    for (int j=0; j < json->u.object.length; ++j) {
        char* mItemName = json->u.object.values[j].name;
        json_value* mValue = json->u.object.values[j].value;
        if (0 == strcmp(mItemName, "status") && mValue->type == json_string) {
            this->paused = 0 == strncmp("paused", mValue->u.string.ptr, sizeof("paused")-1);
        }
    }
}
void restoreProgress(struct Player* this, json_value* json) {
    if (!json || json->type != json_object) { return; }
    for (int j=0; j < json->u.object.length; ++j) {
        char* mItemName = json->u.object.values[j].name;
        json_value* mValue = json->u.object.values[j].value;
        if (0 == strcmp(mItemName, "seconds") && mValue->type == json_integer) {
            this->progress.current = mValue->u.integer;
        }
    }
}

void fetchGeneric(struct Player* this, const char* pathUrl,
                  void (*restoreFn)(struct Player*,json_value*)) {
    char url[2048];
    sprintf(url, "%s%s", this->baseUrl, pathUrl);

    struct CurlResult userdata;
    CURL* handle = Curl_defaultHandle(url, &userdata);
    curl_easy_perform(handle);
    json_value* json = CurlResult_parse(&userdata);

    restoreFn(this, json);

    json_value_free(json);
    CurlResult_destroyMembers(&userdata);
    curl_easy_cleanup(handle);
}
void fetchMetadata(struct Player* this) {
    fetchGeneric(this, "/api/player/metaData", restoreMetadata);
}
void fetchPauseStatus(struct Player* this) {
    fetchGeneric(this, "/api/player/pauseStatus", restorePauseStatus);
}
void fetchProgress(struct Player* this) {
    fetchGeneric(this, "/api/player/progress", restoreProgress);
}

void Player_fetch(struct Player* this, const char* url) {
    this->baseUrl = url;
    fetchMetadata(this);
    fetchPauseStatus(this);
    fetchProgress(this);

    Player_draw(this);
}