forked from Paremo/foo_bbookmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmark_worker.cpp
More file actions
275 lines (218 loc) · 8.36 KB
/
Copy pathbookmark_worker.cpp
File metadata and controls
275 lines (218 loc) · 8.36 KB
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
#include "stdafx.h"
#include "bookmark_core.h"
#include "bookmark_store.h"
#include "bookmark_persistence.h"
#include "bookmark_preferences.h"
#include "bookmark_worker.h"
double g_pendingSeek;
using namespace glb;
bookmark_worker::bookmark_worker()
{
//..
}
bookmark_worker::~bookmark_worker()
{
//..
}
void bookmark_worker::store(const bookmark_t bookmark) {
bookmark_t newMark;
if (cfg_monitor.get()) {
newMark = bookmark;
newMark.set_time(playback_control::get()->playback_get_position());
}
else {
pfc::string_formatter songDesc;
metadb_handle_ptr dbHandle_item;
auto playback_control_ptr = playback_control::get();
if (!playback_control_ptr->get_now_playing(dbHandle_item)) {
//We can NOT obtain the currently playing item - fizzle out
FB2K_console_print_e("Get_now_playing failed, can only store time.");
songDesc << "Could not find playing song info.";
newMark.set_time(playback_control_ptr->playback_get_position());
newMark.desc = songDesc.c_str();
newMark.playlist = "";
newMark.guid_playlist = pfc::guid_null;
newMark.path = "";
newMark.subsong = 0;
gimme_date(newMark);
}
else {
titleformat_object::ptr desc_format;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(desc_format, cfg_desc_format.get_value().c_str());
if (!dbHandle_item->format_title(NULL, songDesc, desc_format, NULL)) {
songDesc << "Could not generate Description.";
}
//TODO: graceful failure?!
pfc::string8 playing_pl_name = cfg_monitor ? "Could not read playlist name." : "";
size_t index_playlist;
GUID guid_playlist = pfc::guid_null;
size_t index_item;
auto playlist_manager_ptr = playlist_manager_v5::get();
if (playlist_manager_ptr->get_playing_item_location(&index_playlist, &index_item)) {
playlist_manager_ptr->playlist_get_name(index_playlist, playing_pl_name);
guid_playlist = playlist_manager_v5::get()->playlist_get_guid(index_playlist);
}
pfc::string8 songPath = dbHandle_item->get_path();
newMark.set_time(playback_control_ptr->playback_get_position());
newMark.desc = songDesc;
newMark.playlist = playing_pl_name.c_str(); //without using c_str(), the full 80 characters are written every time
newMark.guid_playlist = guid_playlist;
newMark.path = songPath;
newMark.subsong = dbHandle_item->get_subsong_index();
gimme_date(newMark);
if (newMark.isRadio()) {
titleformat_object::ptr p_script;
pfc::string8 titleformat = cfg_desc_format.get_value();
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(p_script, titleformat);
pfc::string_formatter songDesc;
if (playback_control::get()->playback_format_title(NULL, songDesc, p_script, NULL, playback_control::display_level_all)) {
newMark.desc = songDesc.c_str();
}
}
}
}
g_store.AddItem(newMark);
}
void bookmark_worker::restore(size_t index) {
const std::vector<bookmark_t>& masterList = g_store.GetMasterList();
bool bempty = g_store.GetMasterList().empty();
if (masterList.empty() || index >= masterList.size()) {
FB2K_console_print_v("Restore Bookmark failed... invalid position");
return;
}
if (index >= 0 && index < masterList.size()) { //load using the index
auto rec = masterList[index];
g_bmAuto.SetRestoredDummy(rec);
if (!(bool)rec.path.get_length()) {
FB2K_console_print_v("Restore Bookmark failed...no track in bookmark.");
return;
}
if (!rec.isRadio()) {
abort_callback_impl p_abort;
try {
if (!filesystem_v3::g_exists(rec.path.c_str(), p_abort)) {
FB2K_console_print_e("Error restoring bookmark... object not found.");
return;
}
}
catch (exception_aborted) {
return;
}
}
//restore track:
auto metadb_ptr = metadb::get();
auto playlist_manager_ptr = playlist_manager::get(); //Get index of stored playlist
auto playback_control_ptr = playback_control::get();
metadb_handle_ptr track_bm = metadb_ptr->handle_create(rec.path.c_str(), rec.subsong); //Identify track to restore
size_t index_pl = ~0;
size_t index_item = ~0;
bool bplaylist_item_fault = true;
if (pfc::guid_equal(rec.guid_playlist, pfc::guid_null)) {
FB2K_console_print_v("Item queued... ", rec.desc);
}
else {
index_pl = playlist_manager_v5::get()->find_playlist_by_guid(rec.guid_playlist);
bplaylist_item_fault = index_pl == pfc_infinite || !playlist_manager_ptr->playlist_find_item(index_pl, track_bm, index_item);
if (bplaylist_item_fault) {
//Complain if no playlist of that name exists anymore or item not in playlist
FB2K_console_print_v("Item queued... '", rec.playlist, "' playlist/item mismatch.");
}
}
if (is_cfg_Queuing() || bplaylist_item_fault) {
if (track_bm.get_ptr()) {
if (is_cfg_Flush_Queue()) {
playlist_manager_ptr->queue_flush();
}
bool bplaying = playback_control_ptr->is_playing();
bool bpaused = playback_control_ptr->is_paused();
auto cq = playlist_manager_ptr->queue_get_count();
//todo: if queue is modified after this, seek time will be applied to the wrong item ?
if (!(bpaused || cq)) {
//only the first item send to an empty queue can be seek
g_pendingSeek = rec.get_time();
}
else {
g_pendingSeek = 0.0;
g_bmAuto.ResetRestoredDummyTime();
}
if (bplaylist_item_fault) {
playlist_manager_ptr->queue_add_item(track_bm);
}
else {
playlist_manager_ptr->queue_add_item_playlist(index_pl, index_item);
}
//todo: rev update dummy relies on new track event
if (is_cfg_Queuing() && playlist_manager_ptr->queue_get_count() == 1 && !playback_control_ptr->is_playing()) {
//..
}
else {
if ((playlist_manager_ptr->queue_get_count() > 1) && playback_control_ptr->is_playing()) {
return;
}
}
playback_control_ptr->play_or_unpause();
}
}
else {
size_t plpos;
playlist_manager_ptr->playlist_find_item(index_pl, track_bm, plpos);
if (plpos != ~0) {
playlist_manager_ptr->set_active_playlist(index_pl);
playlist_manager_ptr->set_playing_playlist(index_pl);
playlist_manager_ptr->playlist_set_selection(index_pl, bit_array_true(), bit_array_false());
playlist_manager_ptr->playlist_set_selection_single(index_pl, plpos, true);
playlist_manager_ptr->playlist_set_focus_item(index_pl, plpos);
playlist_manager_ptr->playlist_execute_default_action(index_pl, plpos);
}
g_pendingSeek = rec.get_time();
if (g_pendingSeek == 0.0) { //If a time change was not queued up, the track is either already correct or could not be determined
if (!core_api::assert_main_thread()) {
FB2K_console_print_v("(Not in main thread)");
}
FB2K_console_print_v("Restoring time:", rec.get_time());
playback_control_ptr->playback_seek(rec.get_time());
}
//unpause
playback_control_ptr->pause(false);
}
}
}
else { //Index invalid, fall back to the last entry
restore(masterList.size() - 1);
return;
}
}
// worker play callback
class bm_worker_play_callback : public play_callback_static {
public:
/* play_callback methods go here */
void on_playback_starting(play_control::t_track_command p_command, bool p_paused) override {}
void on_playback_stop(play_control::t_stop_reason p_reason) override {}
void on_playback_pause(bool p_state) override {}
void on_playback_edited(metadb_handle_ptr p_track) override {}
void on_playback_dynamic_info(const file_info & p_info) override {}
void on_playback_dynamic_info_track(const file_info & p_info) override {}
void on_volume_change(float p_new_val) override {}
void on_playback_seek(double p_time) override {}
void on_playback_time(double p_time) override {}
//To apply the seek to the right track, we need to wait for it to start playing;
//and to prevent race conditions we need to get the actual seeking out of the on_playback
//(by scheduling it in the main thread)
void on_playback_new_track(metadb_handle_ptr p_track) override {
if (g_pendingSeek != 0.0) {
//Lambda can't capture the global variable directly
fb2k::inMainThread([d = g_pendingSeek] {
if (playback_control::get()->playback_can_seek()) {
playback_control::get()->playback_seek(d);
}
});
g_pendingSeek = 0.0;
g_bmAuto.ResetRestoredDummyTime();
}
}
// select worker play callbacks
virtual unsigned get_flags() {
return flag_on_playback_new_track;
}
};
static service_factory_single_t<bm_worker_play_callback> g_worker_play_callback_static_factory;