projectm-sys 1.2.2

Bindings for ProjectM
Documentation
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include "PlaylistCWrapper.hpp"

#include "projectM-4/callbacks.h"
#include "projectM-4/core.h"

namespace libprojectM {
namespace Playlist {

PlaylistCWrapper::PlaylistCWrapper(projectm_handle projectMInstance)
    : m_projectMInstance(projectMInstance)
{
    if (m_projectMInstance != nullptr)
    {
        projectm_set_preset_switch_requested_event_callback(m_projectMInstance, &OnPresetSwitchRequested, this);
        projectm_set_preset_switch_failed_event_callback(m_projectMInstance, &OnPresetSwitchFailed, this);
    }
}


void PlaylistCWrapper::Connect(projectm_handle projectMInstance)
{
    if (m_projectMInstance != nullptr)
    {
        projectm_set_preset_switch_requested_event_callback(m_projectMInstance, nullptr, nullptr);
        projectm_set_preset_switch_failed_event_callback(m_projectMInstance, nullptr, nullptr);
    }

    m_projectMInstance = projectMInstance;

    if (m_projectMInstance != nullptr)
    {
        projectm_set_preset_switch_requested_event_callback(m_projectMInstance, &OnPresetSwitchRequested, this);
        projectm_set_preset_switch_failed_event_callback(m_projectMInstance, &OnPresetSwitchFailed, this);
    }
}


void PlaylistCWrapper::OnPresetSwitchRequested(bool isHardCut, void* userData)
{
    if (userData == nullptr)
    {
        return;
    }

    auto* playlist = reinterpret_cast<PlaylistCWrapper*>(userData);

    try
    {
        playlist->PlayPresetIndex(playlist->NextPresetIndex(), isHardCut, true);
    }
    catch (PlaylistEmptyException&)
    {
    }
}


void PlaylistCWrapper::OnPresetSwitchFailed(const char* presetFilename, const char* message, void* userData)
{
    if (userData == nullptr)
    {
        return;
    }

    auto* playlist = reinterpret_cast<PlaylistCWrapper*>(userData);
    auto lastDirection = playlist->GetLastNavigationDirection();

    if (lastDirection != NavigationDirection::Last)
    {
        // Don't let the user go back to a broken preset.
        playlist->RemoveLastHistoryEntry();
    }

    // Preset switch may fail due to broken presets, retry a few times before giving up.
    if (playlist->m_presetSwitchFailedCount >= playlist->m_presetSwitchRetryCount)
    {
        if (playlist->m_presetSwitchFailedEventCallback != nullptr)
        {
            playlist->m_presetSwitchFailedEventCallback(presetFilename, message,
                                                        playlist->m_presetSwitchFailedEventUserData);
        }

        return;
    }

    playlist->m_presetSwitchFailedCount++;

    uint32_t playlistIndex{};
    switch (lastDirection)
    {
        case NavigationDirection::Previous:
            playlistIndex = playlist->PreviousPresetIndex();
            break;

        case NavigationDirection::Next:
            playlistIndex = playlist->NextPresetIndex();
            break;

        case NavigationDirection::Last:
            playlistIndex = playlist->LastPresetIndex();
            break;
    }

    playlist->PlayPresetIndex(playlistIndex, playlist->m_hardCutRequested, false);
}


void PlaylistCWrapper::SetRetryCount(uint32_t retryCount)
{
    m_presetSwitchRetryCount = retryCount;
}


auto PlaylistCWrapper::RetryCount() -> uint32_t
{
    return m_presetSwitchRetryCount;
}


void PlaylistCWrapper::SetPresetSwitchedCallback(projectm_playlist_preset_switched_event callback, void* userData)
{
    m_presetSwitchedEventCallback = callback;
    m_presetSwitchedEventUserData = userData;
}


void PlaylistCWrapper::SetPresetSwitchFailedCallback(projectm_playlist_preset_switch_failed_event callback, void* userData)
{
    m_presetSwitchFailedEventCallback = callback;
    m_presetSwitchFailedEventUserData = userData;
}


void PlaylistCWrapper::PlayPresetIndex(uint32_t index, bool hardCut, bool resetFailureCount)
{
    if (resetFailureCount)
    {
        m_presetSwitchFailedCount = 0;
    }

    m_hardCutRequested = hardCut;

    const auto& playlistItems = Items();

    if (playlistItems.size() <= index)
    {
        return;
    }

    projectm_load_preset_file(m_projectMInstance,
                              playlistItems.at(index).Filename().c_str(), !hardCut);

    if (m_presetSwitchedEventCallback != nullptr)
    {
        m_presetSwitchedEventCallback(hardCut, index, m_presetSwitchedEventUserData);
    }
}


void PlaylistCWrapper::SetLastNavigationDirection(PlaylistCWrapper::NavigationDirection direction)
{
    m_lastNavigationDirection = direction;
}


auto PlaylistCWrapper::GetLastNavigationDirection() const -> PlaylistCWrapper::NavigationDirection
{
    return m_lastNavigationDirection;
}

} // namespace Playlist
} // namespace libprojectM

auto playlist_handle_to_instance(projectm_playlist_handle instance) -> libprojectM::Playlist::PlaylistCWrapper*
{
    return reinterpret_cast<libprojectM::Playlist::PlaylistCWrapper*>(instance);
}


void projectm_playlist_free_string(char* string)
{
    delete[] string;
}


void projectm_playlist_free_string_array(char** array)
{
    int index{0};
    while (array[index] != nullptr)
    {
        delete[] array[index];
        index++;
    }
    delete[] array;
}


auto projectm_playlist_create(projectm_handle projectm_instance) -> projectm_playlist_handle
{
    try
    {
        auto* instance = new libprojectM::Playlist::PlaylistCWrapper(projectm_instance);
        return reinterpret_cast<projectm_playlist*>(instance);
    }
    catch (...)
    {
    }

    return nullptr;
}


void projectm_playlist_destroy(projectm_playlist_handle instance)
{
    auto* playlist = playlist_handle_to_instance(instance);
    delete playlist;
}


void projectm_playlist_set_preset_switched_event_callback(projectm_playlist_handle instance,
                                                          projectm_playlist_preset_switched_event callback,
                                                          void* user_data)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->SetPresetSwitchedCallback(callback, user_data);
}


void projectm_playlist_set_preset_switch_failed_event_callback(projectm_playlist_handle instance,
                                                               projectm_playlist_preset_switch_failed_event callback,
                                                               void* user_data)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->SetPresetSwitchFailedCallback(callback, user_data);
}


void projectm_playlist_connect(projectm_playlist_handle instance, projectm_handle projectm_instance)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->Connect(projectm_instance);
}


auto projectm_playlist_size(projectm_playlist_handle instance) -> uint32_t
{
    auto* playlist = playlist_handle_to_instance(instance);
    return playlist->Size();
}


void projectm_playlist_clear(projectm_playlist_handle instance)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->Clear();
}


auto projectm_playlist_items(projectm_playlist_handle instance, uint32_t start, uint32_t count) -> char**
{
    auto* playlist = playlist_handle_to_instance(instance);

    const auto& items = playlist->Items();

    if (start >= items.size())
    {
        auto* array = new char* [1] {};
        return array;
    }

    size_t endPos = std::min(static_cast<size_t>(start + count), items.size());
    auto* array = new char* [endPos - start + 1] {};

    for (size_t index{start}; index < endPos; index++)
    {
        auto filename = items[index].Filename();
        array[index - start] = new char[filename.length() + 1]{};
        filename.copy(array[index - start], filename.length());
    }

    return array;
}


char* projectm_playlist_item(projectm_playlist_handle instance, uint32_t index)
{
    auto* playlist = playlist_handle_to_instance(instance);

    if (playlist->Empty() || index >= playlist->Size())
    {
        return nullptr;
    }

    auto filename = playlist->Items().at(index).Filename();
    auto buffer = new char[filename.length() + 1]{};
    filename.copy(buffer, filename.length());

    return buffer;
}


auto projectm_playlist_add_path(projectm_playlist_handle instance, const char* path,
                                bool recurse_subdirs, bool allow_duplicates) -> uint32_t
{
    auto* playlist = playlist_handle_to_instance(instance);

    return playlist->AddPath(path, libprojectM::Playlist::Playlist::InsertAtEnd, recurse_subdirs, allow_duplicates);
}


auto projectm_playlist_insert_path(projectm_playlist_handle instance, const char* path,
                                   uint32_t index, bool recurse_subdirs, bool allow_duplicates) -> uint32_t
{
    auto* playlist = playlist_handle_to_instance(instance);

    return playlist->AddPath(path, index, recurse_subdirs, allow_duplicates);
}


auto projectm_playlist_add_preset(projectm_playlist_handle instance, const char* filename,
                                  bool allow_duplicates) -> bool
{
    auto* playlist = playlist_handle_to_instance(instance);

    return playlist->AddItem(filename, libprojectM::Playlist::Playlist::InsertAtEnd, allow_duplicates);
}


auto projectm_playlist_insert_preset(projectm_playlist_handle instance, const char* filename,
                                     uint32_t index, bool allow_duplicates) -> bool
{
    auto* playlist = playlist_handle_to_instance(instance);

    return playlist->AddItem(filename, index, allow_duplicates);
}


uint32_t projectm_playlist_add_presets(projectm_playlist_handle instance, const char** filenames,
                                       uint32_t count, bool allow_duplicates)
{
    if (filenames == nullptr)
    {
        return 0;
    }

    auto* playlist = playlist_handle_to_instance(instance);

    uint32_t addCount{0};

    for (uint32_t index{0}; index < count; ++index)
    {
        if (filenames[index] == nullptr)
        {
            continue;
        }

        if (playlist->AddItem(filenames[index], libprojectM::Playlist::Playlist::InsertAtEnd, allow_duplicates))
        {
            addCount++;
        }
    }

    return addCount;
}


auto projectm_playlist_insert_presets(projectm_playlist_handle instance, const char** filenames,
                                      uint32_t count, unsigned int index, bool allow_duplicates) -> uint32_t
{
    if (filenames == nullptr)
    {
        return 0;
    }

    auto* playlist = playlist_handle_to_instance(instance);

    uint32_t addCount{0};

    for (uint32_t filenameIndex{0}; filenameIndex < count; ++filenameIndex)
    {
        if (filenames[filenameIndex] == nullptr)
        {
            continue;
        }

        if (playlist->AddItem(filenames[filenameIndex], index + addCount, allow_duplicates))
        {
            addCount++;
        }
    }

    return addCount;
}


bool projectm_playlist_remove_preset(projectm_playlist_handle instance, uint32_t index)
{
    auto* playlist = playlist_handle_to_instance(instance);

    return playlist->RemoveItem(index);
}


uint32_t projectm_playlist_remove_presets(projectm_playlist_handle instance, uint32_t index,
                                          uint32_t count)
{
    auto* playlist = playlist_handle_to_instance(instance);

    uint32_t removeCount{0};

    for (uint32_t iteration{0}; iteration < count; ++iteration)
    {
        if (!playlist->RemoveItem(index))
        {
            // No need to iterate further, as the end of the playlist was reached.
            break;
        }

        removeCount++;
    }

    return removeCount;
}


bool projectm_playlist_get_shuffle(projectm_playlist_handle instance)
{
    auto* playlist = playlist_handle_to_instance(instance);
    return playlist->Shuffle();
}


void projectm_playlist_set_shuffle(projectm_playlist_handle instance, bool shuffle)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->SetShuffle(shuffle);
}


void projectm_playlist_sort(projectm_playlist_handle instance, uint32_t start_index, uint32_t count,
                            projectm_playlist_sort_predicate predicate, projectm_playlist_sort_order order)
{
    auto* playlist = playlist_handle_to_instance(instance);

    libprojectM::Playlist::Playlist::SortPredicate predicatePlaylist{libprojectM::Playlist::Playlist::SortPredicate::FullPath};
    libprojectM::Playlist::Playlist::SortOrder orderPlaylist{libprojectM::Playlist::Playlist::SortOrder::Ascending};

    if (predicate == SORT_PREDICATE_FILENAME_ONLY)
    {
        predicatePlaylist = libprojectM::Playlist::Playlist::SortPredicate::FilenameOnly;
    }

    if (order == SORT_ORDER_DESCENDING)
    {
        orderPlaylist = libprojectM::Playlist::Playlist::SortOrder::Descending;
    }

    playlist->Sort(start_index, count, predicatePlaylist, orderPlaylist);
}


uint32_t projectm_playlist_get_retry_count(projectm_playlist_handle instance)
{
    auto* playlist = playlist_handle_to_instance(instance);
    return playlist->RetryCount();
}


void projectm_playlist_set_retry_count(projectm_playlist_handle instance, uint32_t retry_count)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->SetRetryCount(retry_count);
}


auto projectm_playlist_get_position(projectm_playlist_handle instance) -> uint32_t
{
    auto* playlist = playlist_handle_to_instance(instance);
    try
    {
        return playlist->PresetIndex();
    }
    catch (libprojectM::Playlist::PlaylistEmptyException&)
    {
        return 0;
    }
}


auto projectm_playlist_set_position(projectm_playlist_handle instance, uint32_t new_position,
                                    bool hard_cut) -> uint32_t
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->SetLastNavigationDirection(libprojectM::Playlist::PlaylistCWrapper::NavigationDirection::Next);
    try
    {
        auto newIndex = playlist->SetPresetIndex(new_position);
        playlist->PlayPresetIndex(newIndex, hard_cut, true);
        return playlist->PresetIndex();
    }
    catch (libprojectM::Playlist::PlaylistEmptyException&)
    {
        return 0;
    }
}


uint32_t projectm_playlist_play_next(projectm_playlist_handle instance, bool hard_cut)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->SetLastNavigationDirection(libprojectM::Playlist::PlaylistCWrapper::NavigationDirection::Next);
    try
    {
        auto newIndex = playlist->NextPresetIndex();
        playlist->PlayPresetIndex(newIndex, hard_cut, true);
        return playlist->PresetIndex();
    }
    catch (libprojectM::Playlist::PlaylistEmptyException&)
    {
        return 0;
    }
}


uint32_t projectm_playlist_play_previous(projectm_playlist_handle instance, bool hard_cut)
{
    auto* playlist = playlist_handle_to_instance(instance);
    playlist->SetLastNavigationDirection(libprojectM::Playlist::PlaylistCWrapper::NavigationDirection::Previous);
    try
    {
        auto newIndex = playlist->PreviousPresetIndex();
        playlist->PlayPresetIndex(newIndex, hard_cut, true);
        return playlist->PresetIndex();
    }
    catch (libprojectM::Playlist::PlaylistEmptyException&)
    {
        return 0;
    }
}


uint32_t projectm_playlist_play_last(projectm_playlist_handle instance, bool hard_cut)
{
    auto* playlist = playlist_handle_to_instance(instance);

    playlist->SetLastNavigationDirection(libprojectM::Playlist::PlaylistCWrapper::NavigationDirection::Last);
    try
    {
        auto newIndex = playlist->LastPresetIndex();
        playlist->PlayPresetIndex(newIndex, hard_cut, true);
        return playlist->PresetIndex();
    }
    catch (libprojectM::Playlist::PlaylistEmptyException&)
    {
        return 0;
    }
}


void projectm_playlist_set_filter(projectm_playlist_handle instance, const char** filter_list,
                                  size_t count)
{
    auto* playlist = playlist_handle_to_instance(instance);

    std::vector<std::string> filterList;
    for (size_t index = 0; index < count; index++)
    {
        if (filter_list[index] == nullptr)
        {
            continue;
        }
        filterList.emplace_back(filter_list[index]);
    }

    playlist->Filter().SetList(filterList);
}


auto projectm_playlist_get_filter(projectm_playlist_handle instance, size_t* count) -> char**
{
    auto* playlist = playlist_handle_to_instance(instance);

    const auto& filterList = playlist->Filter().List();

    auto* array = new char* [filterList.size() + 1] {};

    int index{0};
    for (const auto& filter : filterList)
    {
        array[index] = new char[filter.length() + 1]{};
        filter.copy(array[index], filter.length());
        index++;
    }

    *count = filterList.size();
    return array;
}


auto projectm_playlist_apply_filter(projectm_playlist_handle instance) -> size_t
{
    auto* playlist = playlist_handle_to_instance(instance);
    return playlist->ApplyFilter();
}