Skip to content

Commit

Permalink
feat: add get_available_devices api
Browse files Browse the repository at this point in the history
fix: buffer free

feat: pause gui in bg
  • Loading branch information
michal4132 committed Dec 1, 2022
1 parent ea9c289 commit 8eba04c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define SPOTIFY_API_GET_USERS_PLAYLISTS "https://api.spotify.com/v1/me/playlists"
#define SPOTIFY_API_GET_PLAYLIST_ITEMS_s "https://api.spotify.com/v1/playlists/"
#define SPOTIFY_API_GET_PLAYLIST_ITEMS_e "/tracks"
#define SPOTIFY_API_GET_AVAILABLE_DEVICES "https://api.spotify.com/v1/me/player/devices"

class API {
public:
Expand All @@ -19,6 +20,7 @@ class API {
void play_by_uri(std::string uri, uint32_t offset_pos, uint32_t position_ms);
int get_current_users_playlists(uint8_t **buf, uint16_t limit, uint16_t offset);
int get_playlist_items(uint8_t **buf, std::string playlist_id, std::string fields, uint16_t limit, uint16_t offset);
int get_available_devices(uint8_t **buf);

private:
std::string token;
Expand Down
1 change: 1 addition & 0 deletions include/Gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class GUI {
void init();
void start();
std::atomic<bool> isRunning = true;
std::atomic<bool> paused = true;
ImFont *font;
ImFont *playback_icon_font;
ImFont *icon_font;
Expand Down
21 changes: 20 additions & 1 deletion src/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void API::play_by_uri(std::string uri, uint32_t offset_pos, uint32_t position_ms
int len = download(url.c_str(), &buf, "PUT", post_data, headers);
if (len > 0) {
CSPOT_LOG(info, "play_by_uri response: %.*s", len, buf);
sce_paf_free(buf);
free(buf);
}
}

Expand Down Expand Up @@ -97,3 +97,22 @@ int API::get_playlist_items(uint8_t **buf, std::string playlist_id, std::string
return len;
}

int API::get_available_devices(uint8_t **buf) {
if (token.size() == 0) {
return -1;
}

Headers headers = { {"Accept", "application/json"},
{"Content-Type", "application/json"},
{"Authorization", "Bearer " + token} };

int len = download(SPOTIFY_API_GET_AVAILABLE_DEVICES, buf, "GET", "", headers);
if (len <= 0) {
buf = NULL;
return 0;
}

CSPOT_LOG(info, "get_available_devices response: %.*s", len, *buf);
return len;
}

4 changes: 4 additions & 0 deletions src/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ void GUI::start() {
// bool show = true;
// ImGui::ShowDemoWindow(&show);

while (paused) {
sceKernelDelayThread(500000);
}

ImGui::End();
}

Expand Down
2 changes: 1 addition & 1 deletion src/PlaybackScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ void PlaybackScreen::drawSubmenu() {
case Submenu::PLAYLISTS:
for (uint16_t i = 0; i < playlists.size(); i++) {
if (ImGui::TreeNode((void*)(intptr_t)i, playlists[i].name.c_str())) {
// track list not yet loaded
if (!playlists[i].tracks_loaded) {
CSPOT_LOG(debug, "request track list for playlist: %s", playlists[i].name.c_str());
getTracks(i);
Expand Down Expand Up @@ -251,6 +250,7 @@ void PlaybackScreen::drawSubmenu() {
gui->isRunning = false;
remove(CREDENTIALS_FILE_NAME);
}

break;
default:
break;
Expand Down
4 changes: 3 additions & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ int download(const char *url, uint8_t **return_buffer, const char *method, std::
res = curl_easy_perform(curl_handle);
int httpresponsecode = 0;
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpresponsecode);
CSPOT_LOG(debug, "response code: %d", httpresponsecode);
if (httpresponsecode != 200) {
CSPOT_LOG(debug, "response code: %d", httpresponsecode);
}

if (res != CURLE_OK) {
CSPOT_LOG(error, "curl_easy_perform() failed: %s", curl_easy_strerror(res));
Expand Down
11 changes: 9 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
#include <psp2/audioout.h>
#include <psp2/bgapputil.h>
#include <psp2/ctrl.h>
#include <psp2/kernel/processmgr.h>
#include <psp2/kernel/threadmgr.h>
#include <psp2/io/fcntl.h>
#include <imgui_vita.h>
#include <vitaGL.h>

#include "Paf.h"
Expand Down Expand Up @@ -61,6 +59,15 @@ SceVoid watch_dog(SceSize _args, void *_argp) {
case SCE_APP_EVENT_REQUEST_QUIT:
gui->isRunning = false;
return;
case SCE_APP_EVENT_ON_DEACTIVATE:
gui->paused = true;
break;
case SCE_APP_EVENT_ON_ACTIVATE:
gui->paused = false;
break;
default:
// CSPOT_LOG(debug, "watchdog event: %x", appEvent.event);
break;
}
sceKernelDelayThread(1000000);
sceKernelPowerTick(SCE_KERNEL_POWER_TICK_DISABLE_AUTO_SUSPEND);
Expand Down

0 comments on commit 8eba04c

Please sign in to comment.