summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-06-04 21:42:04 +1000
committerquou <quou@disroot.org>2024-06-04 21:42:04 +1000
commit557e07876de0086b70e301b104547bf35ec57959 (patch)
treeb2c9923ef989ea38d185d4598764a060c18f298e /main.c
parent6589107006fd4fc06bdb7d02cb4b1eef45395458 (diff)
Loading album art, seeking, pause/play.
Diffstat (limited to 'main.c')
-rw-r--r--main.c144
1 files changed, 140 insertions, 4 deletions
diff --git a/main.c b/main.c
index 94f78b5..2b0e763 100644
--- a/main.c
+++ b/main.c
@@ -6,14 +6,44 @@
Library lib;
Player pl;
+UIButton* play_button;
+UISlider* seek_slider;
+UIImageDisplay* cover_image;
+
+void ui_update_play(void) {
+ UI_FREE(play_button->label);
+ play_button->label = UIStringCopy(
+ pl.play? "Pause": "Play",
+ -1
+ );
+ UIElementRefresh((UIElement*)play_button);
+ UIImageDisplaySetContent(
+ cover_image,
+ pl.cover,
+ album_cover_w,
+ album_cover_h,
+ album_cover_w * 4
+ );
+ UIElementRepaint((UIElement*)cover_image, 0);
+}
+
+void ui_update_seek(void) {
+ float p;
+ if (!pl.song) return;
+ p = (float)pl.cs / (float)pl.ms;
+ seek_slider->position = p;
+ UIElementRefresh((UIElement*)seek_slider);
+}
int libtab_msg(
- UIElement *el,
+ UIElement* el,
UIMessage msg,
int di,
- void *dp
+ void* dp
) {
Song* song;
+ (void)di;
+ (void)dp;
if (msg == UI_MSG_TABLE_GET_ITEM) {
UITableGetItem *m = (UITableGetItem*)dp;
song = &lib.songs[lib.indices[m->index]];
@@ -56,18 +86,88 @@ int libtab_msg(
el->window->cursorY
);
song = &lib.songs[lib.indices[hit]];
- init_player(&pl);
play_song(&pl, song);
+ ui_update_play();
+ }
+ return 0;
+}
+
+int playpause_msg(
+ UIElement* el,
+ UIMessage msg,
+ int di,
+ void* dp
+) {
+ (void)el;
+ (void)di;
+ (void)dp;
+ if (msg == UI_MSG_CLICKED) {
+ if (!pl.song) return 0;
+ pl.play = !pl.play;
+ ui_update_play();
+ }
+ return 0;
+}
+
+int start_msg(
+ UIElement* el,
+ UIMessage msg,
+ int di,
+ void* dp
+) {
+ (void)el;
+ (void)di;
+ (void)dp;
+ if (msg == UI_MSG_CLICKED) {
+ if (!pl.song) return 0;
+ play_seek(&pl, 0.0f);
+ }
+ return 0;
+}
+
+int end_msg(
+ UIElement* el,
+ UIMessage msg,
+ int di,
+ void* dp
+) {
+ (void)el;
+ (void)di;
+ (void)dp;
+ if (msg == UI_MSG_CLICKED) {
+ if (!pl.song) return 0;
+ play_seek(&pl, 1.0f);
}
return 0;
}
+int seek_msg(
+ UIElement* el,
+ UIMessage msg,
+ int di,
+ void* dp
+) {
+ UISlider* sl;
+ (void)el;
+ (void)di;
+ (void)dp;
+ if (msg == UI_MSG_VALUE_CHANGED) {
+ sl = (UISlider*)el;
+ play_seek(&pl, sl->position);
+ } else if (msg == UI_MSG_ANIMATE) {
+ ui_update_seek();
+ }
+
+ return 0;
+}
+
int prog_main(void* mem) {
Arena liba;
UIWindow* wi;
UISplitPane* split1, * split2, * split3;
UIPanel* plib, * pctrl, * plist, * pqueue;
UITable* libtab;
+ int r;
memory_init(mem);
init_arena(
&liba,
@@ -75,6 +175,7 @@ int prog_main(void* mem) {
library_memory_size
);
build_library(&liba, &lib, library_path);
+ init_player(&pl);
UIInitialise();
wi = UIWindowCreate(
0,
@@ -102,6 +203,39 @@ int prog_main(void* mem) {
pctrl = UIPanelCreate(&split3->e, UI_PANEL_GRAY);
pctrl->gap = 5;
pctrl->border = UI_RECT_1(5);
+ cover_image = UIImageDisplayCreate(
+ &pctrl->e,
+ 0,
+ (unsigned*)pl.cover,
+ album_cover_w,
+ album_cover_h,
+ album_cover_w
+ );
+ play_button = UIButtonCreate(
+ &pctrl->e,
+ 0,
+ "Play",
+ -1
+ );
+ play_button->e.messageUser = playpause_msg;
+ UIButtonCreate(
+ &pctrl->e,
+ 0,
+ "Start",
+ -1
+ )->e.messageUser = start_msg;
+ UIButtonCreate(
+ &pctrl->e,
+ 0,
+ "End",
+ -1
+ )->e.messageUser = end_msg;
+ seek_slider = UISliderCreate(
+ &pctrl->e,
+ UI_ELEMENT_H_FILL
+ );
+ UIElementAnimate(&seek_slider->e, false);
+ seek_slider->e.messageUser = seek_msg;
pqueue = UIPanelCreate(&split3->e, UI_PANEL_GRAY);
pqueue->gap = 5;
@@ -140,5 +274,7 @@ int prog_main(void* mem) {
libtab->e.messageUser = libtab_msg;
UITableResizeColumns(libtab);
- return UIMessageLoop();
+ r = UIMessageLoop();
+ deinit_player(&pl);
+ return r;
}