-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpplayer.c
573 lines (429 loc) · 12.3 KB
/
gpplayer.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//SPDX-License-Identifier: GPL-2.0-or-later
/*
Copyright (C) 2007-2024 Cyril Hrubis <[email protected]>
*/
#include <loaders/gp_loaders.h>
#include <filters/gp_resize.h>
#include <widgets/gp_widgets.h>
#include "playlist.h"
#include "audio_mixer.h"
#include "audio_output.h"
#include "audio_decoder.h"
#include "gpplayer_conf.h"
static gp_htable *uids;
static const struct audio_decoder_ops *ad_ops;
struct player_tracks {
int playing;
};
static uint32_t playback_callback(gp_timer GP_UNUSED(*self))
{
return audio_decoder_tick(ad_ops);
}
static gp_timer playback_timer = {
.expires = 0,
.callback = playback_callback,
.id = "Playback",
};
static struct player_tracks tracks;
struct info_widgets {
gp_widget *album;
gp_widget *artist;
gp_widget *track;
gp_widget *playback;
gp_widget *cover_art;
gp_widget *playlist;
gp_widget *speaker_icon;
gp_widget *softvol_icon;
gp_widget *decoder_gain;
} info_widgets;
static void track_info(const char *artist, const char *album, const char *track)
{
GP_DEBUG(1, "Track name '%s' Album name '%s' Artist name '%s'",
track, album, artist);
if (album)
gp_widget_label_set(info_widgets.album, album);
if (artist)
gp_widget_label_set(info_widgets.artist, artist);
if (track)
gp_widget_label_set(info_widgets.track, track);
}
static void track_duration(long duration_ms)
{
if (!duration_ms) {
if (!playlist_next()) {
tracks.playing = 0;
gp_widgets_timer_rem(&playback_timer);
audio_decoder_track_pause(ad_ops);
return;
}
//TODO: playlist should call this
gp_widget_redraw(info_widgets.playlist);
audio_decoder_track_load(ad_ops, playlist_cur());
return;
}
gp_widget_pbar_val_set(info_widgets.playback, 0);
gp_widget_pbar_max_set(info_widgets.playback, duration_ms/1000);
}
static void track_pos(long offset_ms)
{
gp_widget_pbar_val_set(info_widgets.playback, offset_ms/1000);
}
static void track_art(void *data, size_t size)
{
gp_widget *cover_art = info_widgets.cover_art;
gp_io *io;
gp_pixmap *p;
if (!gp_widget_pixmap_get(cover_art))
return;
io = gp_io_mem(data, size, NULL);
if (!io)
return;
p = gp_read_image(io, NULL);
if (!p) {
gp_io_close(io);
return;
}
float rat = GP_MIN(1.00 * cover_art->h / p->h,
1.00 * cover_art->w / p->w);
gp_size rw = p->w * rat;
gp_size rh = p->h * rat;
gp_coord off_x = (cover_art->w - rw)/2;
gp_coord off_y = (cover_art->h - rh)/2;
gp_pixmap *resized = gp_filter_resize_alloc(p, rw, rh, GP_INTERP_LINEAR_LF_INT, NULL);
gp_blit(resized, off_x, off_y, resized->w, resized->h, gp_widget_pixmap_get(cover_art), 0, 0);
gp_pixmap_free(resized);
gp_widget_redraw(cover_art);
gp_pixmap_free(p);
gp_io_close(io);
}
static void start_playback_timer(void)
{
tracks.playing = 1;
audio_decoder_track_play(ad_ops);
playback_timer.expires = 0;
gp_widgets_timer_ins(&playback_timer);
}
int button_next_event(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (!playlist_next())
return 0;
//TODO: playlist should call this
gp_widget_redraw(info_widgets.playlist);
audio_decoder_track_load(ad_ops, playlist_cur());
return 0;
}
static gp_pixmap *alloc_backing_pixmap(gp_widget_event *ev)
{
gp_pixmap *p;
gp_widget *pix = ev->self;
p = gp_pixmap_alloc(pix->w, pix->h, ev->ctx->pixel_type);
if (p)
gp_fill(p, ev->ctx->bg_color);
return p;
}
int pixmap_event(gp_widget_event *ev)
{
switch (ev->type) {
case GP_WIDGET_EVENT_RESIZE:
gp_pixmap_free(gp_widget_pixmap_set(ev->self,
alloc_backing_pixmap(ev)));
break;
case GP_WIDGET_EVENT_COLOR_SCHEME:
gp_fill(gp_widget_pixmap_get(ev->self), ev->ctx->bg_color);
break;
default:
break;
}
return 0;
}
int button_prev_event(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (!playlist_prev())
return 0;
//TODO: playlist shoudl call this
gp_widget_redraw(info_widgets.playlist);
audio_decoder_track_load(ad_ops, playlist_cur());
return 1;
}
int button_play_event(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (tracks.playing == 1)
return 1;
start_playback_timer();
audio_decoder_track_play(ad_ops);
return 1;
}
int button_pause_event(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (tracks.playing == 0)
return 1;
tracks.playing = 0;
gp_widgets_timer_rem(&playback_timer);
audio_decoder_track_pause(ad_ops);
return 1;
}
int playlist_event(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (ev->sub_type != GP_WIDGET_TABLE_TRIGGER)
return 0;
if (!playlist_set(gp_widget_table_sel_get(ev->self)))
return 0;
audio_decoder_track_load(ad_ops, playlist_cur());
//TODO: playlist shoudl call this
gp_widget_redraw(info_widgets.playlist);
if (!tracks.playing)
start_playback_timer();
return 0;
}
int button_playlist_rem(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (!gp_widget_table_sel_get(info_widgets.playlist))
return 0;
playlist_rem(gp_widget_table_sel_get(info_widgets.playlist), 1);
gp_widget_table_refresh(info_widgets.playlist);
return 0;
}
int button_playlist_clear(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
playlist_clear();
gp_widget_table_refresh(info_widgets.playlist);
return 0;
}
int button_playlist_add(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
gp_dialog_file_opts opts = {
.flags = GP_DIALOG_OPEN_FILE | GP_DIALOG_OPEN_DIR,
};
gp_dialog *dialog = gp_dialog_file_open_new(gpplayer_conf->last_dialog_path, &opts);
if (gp_dialog_run(dialog) == GP_WIDGET_DIALOG_PATH) {
playlist_add(gp_dialog_file_path(dialog));
gpplayer_conf_last_dialog_path_set(gp_dialog_file_path(dialog));
}
gp_dialog_free(dialog);
return 0;
}
int button_playlist_move_up(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (!gp_widget_table_sel_has(info_widgets.playlist))
return 0;
//TODO: Move to playlist
unsigned int sel = gp_widget_table_sel_get(info_widgets.playlist);
if (playlist_move_up(sel))
gp_widget_table_sel_set(info_widgets.playlist, sel-1);
gp_widget_table_refresh(info_widgets.playlist);
return 0;
}
int button_playlist_move_down(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (!gp_widget_table_sel_has(info_widgets.playlist))
return 0;
if (!gp_widget_table_sel_has(info_widgets.playlist))
return 0;
//TODO: Move to playlist
unsigned int sel = gp_widget_table_sel_get(info_widgets.playlist);
if (playlist_move_down(sel))
gp_widget_table_sel_set(info_widgets.playlist, sel+1);
gp_widget_table_refresh(info_widgets.playlist);
return 0;
}
int playlist_repeat(gp_widget_event *ev)
{
switch (ev->type) {
case GP_WIDGET_EVENT_NEW:
gp_widget_bool_set(ev->self, gpplayer_conf->playlist_repeat);
break;
case GP_WIDGET_EVENT_WIDGET:
gpplayer_conf_playlist_repeat_set(gp_widget_bool_get(ev->self));
break;
}
return 0;
}
int playlist_shuffle(gp_widget_event *ev)
{
switch (ev->type) {
case GP_WIDGET_EVENT_NEW:
gp_widget_bool_set(ev->self, gpplayer_conf->playlist_shuffle);
break;
case GP_WIDGET_EVENT_WIDGET:
gpplayer_conf_playlist_shuffle_set(gp_widget_bool_get(ev->self));
playlist_shuffle_set(gp_widget_bool_get(ev->self));
break;
}
return 0;
}
int seek_on_event(gp_widget_event *ev)
{
uint64_t val = gp_widget_pbar_val_get(ev->self);
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
GP_DEBUG(1, "Seeking to %"PRIu64" min %"PRIu64" sec", val/60, val%60);
audio_decoder_track_seek(ad_ops, 1000 * val);
return 0;
}
static int app_handler(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_FREE)
return 0;
playlist_exit();
gpplayer_conf_save();
//stop audio output?
return 1;
}
static void update_speaker_icon(int vol, int max, int mute)
{
int type;
if (!info_widgets.speaker_icon)
return;
if (vol < max/3)
type = GP_WIDGET_STOCK_SPEAKER_MIN;
else if (vol < 2 * (max/3))
type = GP_WIDGET_STOCK_SPEAKER_MID;
else
type = GP_WIDGET_STOCK_SPEAKER_MAX;
if (mute)
type = GP_WIDGET_STOCK_SPEAKER_MUTE;
gp_widget_stock_type_set(info_widgets.speaker_icon, type);
}
static void mixer_volume_callback(struct audio_mixer *mixer, long volume, int mute)
{
long avol = volume - mixer->master_volume_min;
long amax = mixer->master_volume_max - mixer->master_volume_min;
gp_widget *self = mixer->priv;
int vol = (avol * gp_widget_int_max_get(self) + amax/2) / amax;
update_speaker_icon(vol, gp_widget_int_max_get(self), mute);
gp_widget_int_val_set(self, vol);
}
int audio_volume_set(gp_widget_event *ev)
{
struct audio_mixer *mixer = ev->self->priv;
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
long amax = mixer->master_volume_max - mixer->master_volume_min;
int vol = gp_widget_int_val_get(ev->self);
int max = gp_widget_int_max_get(ev->self);
long avol = (vol * amax + max/2) / max + mixer->master_volume_min;
audio_mixer_set_master_volume(mixer, avol);
return 0;
}
int speaker_icon_ev(gp_widget_event *ev)
{
struct audio_mixer *mixer = ev->self->priv;
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (gp_widget_stock_type_get(ev->self) == GP_WIDGET_STOCK_SPEAKER_MUTE)
audio_mixer_set_master_mute(mixer, 0);
else
audio_mixer_set_master_mute(mixer, 1);
return 0;
}
int set_softvol(gp_widget_event *ev)
{
if (ev->type == GP_WIDGET_EVENT_NEW) {
gp_widget_int_set(ev->self, 0, AUDIO_DECODER_SOFTVOL_MAX, gpplayer_conf->softvol);
return 0;
}
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
int64_t max = gp_widget_int_max_get(ev->self);
int64_t val = gp_widget_int_val_get(ev->self);
audio_decoder_softvol_set(ad_ops, val);
gpplayer_conf_softvol_set(val);
if (!info_widgets.softvol_icon)
return 0;
int type;
if (val < 1.00 * max / 3)
type = GP_WIDGET_STOCK_SPEAKER_MIN;
else if (val < 2.00 * max / 3)
type = GP_WIDGET_STOCK_SPEAKER_MID;
else
type = GP_WIDGET_STOCK_SPEAKER_MAX;
gp_widget_stock_type_set(info_widgets.softvol_icon, type);
return 0;
}
static struct audio_decoder_callbacks ad_callbacks = {
.track_info = track_info,
.track_art = track_art,
.track_duration = track_duration,
.track_pos = track_pos,
};
gp_app_info app_info = {
.name = "gpplayer",
.desc = "A simple mp3 player",
.version = "1.0",
.license = "GPL-2.0-or-later",
.url = "http://github.com/gfxprim/gpplayer",
.authors = (gp_app_info_author []) {
{.name = "Cyril Hrubis", .email = "[email protected]", .years = "2007-2024"},
{}
}
};
static void init_decoder(void)
{
ad_ops = audio_decoder_init(gpplayer_conf->decoder, &ad_callbacks);
/* Restore softvolume from config */
audio_decoder_softvol_set(ad_ops, gpplayer_conf->softvol);
}
int main(int argc, char *argv[])
{
int i;
struct audio_mixer mixer = {
.master_volume_callback = mixer_volume_callback,
};
gpplayer_conf_load();
gp_widget *layout = gp_app_layout_load("gpplayer", &uids);
info_widgets.artist = gp_widget_by_uid(uids, "artist", GP_WIDGET_LABEL);
info_widgets.album = gp_widget_by_uid(uids, "album", GP_WIDGET_LABEL);
info_widgets.track = gp_widget_by_uid(uids, "track", GP_WIDGET_LABEL);
info_widgets.playback = gp_widget_by_uid(uids, "playback", GP_WIDGET_PROGRESSBAR);
info_widgets.cover_art = gp_widget_by_uid(uids, "cover_art", GP_WIDGET_PIXMAP);
info_widgets.playlist = gp_widget_by_uid(uids, "playlist", GP_WIDGET_TABLE);
info_widgets.speaker_icon = gp_widget_by_uid(uids, "speaker_icon", GP_WIDGET_STOCK);
info_widgets.softvol_icon = gp_widget_by_uid(uids, "softvol_icon", GP_WIDGET_STOCK);
info_widgets.decoder_gain = gp_widget_by_uid(uids, "gain", GP_WIDGET_STOCK);
if (info_widgets.speaker_icon)
info_widgets.speaker_icon->priv = &mixer;
gp_widget_events_unmask(info_widgets.cover_art, GP_WIDGET_EVENT_RESIZE |
GP_WIDGET_EVENT_COLOR_SCHEME);
gp_widget *volume = gp_widget_by_uid(uids, "volume", GP_WIDGET_SLIDER);
volume->priv = &mixer;
mixer.priv = volume;
audio_mixer_init(&mixer, AUDIO_DEVICE_DEFAULT);
for (i = 0; i < (int)mixer.poll_fds_cnt; i++)
gp_widget_poll_add(&mixer.poll_fds[i]);
gp_widgets_getopt(&argc, &argv);
if (argc)
playlist_init(NULL);
else
playlist_init("gpapps/gpplayer/playlist.txt");
gp_app_on_event_set(app_handler);
init_decoder();
for (i = 0; i < argc; i++)
playlist_add(argv[i]);
if (playlist_cur()) {
tracks.playing = 1;
audio_decoder_track_load(ad_ops, playlist_cur());
gp_widgets_timer_ins(&playback_timer);
}
gp_widgets_main_loop(layout, NULL, 0, NULL);
return 0;
}