-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathartwork_panel_cui.cpp
More file actions
2754 lines (2303 loc) · 111 KB
/
Copy pathartwork_panel_cui.cpp
File metadata and controls
2754 lines (2303 loc) · 111 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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CUI Artwork Panel Implementation - Full Featured
// This file implements complete artwork display functionality for Columns UI
// Prevent socket conflicts before including windows.h
#define _WINSOCKAPI_
#define NOMINMAX
#include <windows.h>
#include <windowsx.h>
#include <shlobj.h>
// Define CUI support for this file since we're not using precompiled headers
#define COLUMNS_UI_AVAILABLE
// Only compile CUI support if CUI SDK is available
#ifdef COLUMNS_UI_AVAILABLE
// Standard Windows headers
#include <windows.h>
#include <gdiplus.h>
#include <shlwapi.h>
#include <exception>
#include <wincodec.h>
#include "webp_decoder.h"
// Link WIC library
#pragma comment(lib, "windowscodecs.lib")
// Link GDI library for AlphaBlend function
#pragma comment(lib, "msimg32.lib")
#include <algorithm>
#include <thread>
#include <vector>
#include <mutex>
#include <memory>
#include <cmath>
#include <regex>
// Include CUI SDK headers directly - these contain their own foobar2000 SDK
#include "columns_ui/columns_ui-sdk/ui_extension.h"
#include "columns_ui/columns_ui-sdk/window.h"
#include "columns_ui/columns_ui-sdk/container_uie_window_v3.h"
// Also include base UI extension headers
#include "columns_ui/columns_ui-sdk/base.h"
// Include the unified artwork viewer popup and metadata cleaner
#include "artwork_viewer_popup.h"
#include "metadata_cleaner.h"
#include "artwork_manager.h"
// Include necessary foobar2000 SDK headers for artwork and playback callbacks
#include "columns_ui/foobar2000/SDK/album_art.h"
#include "columns_ui/foobar2000/SDK/playback_control.h"
#include "columns_ui/foobar2000/SDK/play_callback.h"
#include "columns_ui/foobar2000/SDK/cfg_var.h"
#include "columns_ui/foobar2000/SDK/console.h"
// Include CUI color API
#include "columns_ui/columns_ui-sdk/colours.h"
// Use the Gdiplus namespace
using namespace Gdiplus;
//=============================================================================
// Forward declarations and external interfaces
//=============================================================================
// Forward declare classes from main component
// artwork_manager is now included via artwork_manager.h
// External instances from main component
extern std::unique_ptr<artwork_manager> g_artwork_manager;
// Configuration variables that we can safely access
extern bool g_artwork_loading;
extern std::wstring g_current_artwork_path;
// Global preference settings
extern cfg_bool cfg_show_osd;
extern cfg_bool cfg_enable_custom_logos;
extern cfg_string cfg_logos_folder;
extern cfg_bool cfg_clear_panel_when_not_playing;
extern cfg_bool cfg_use_noart_image;
// Access to main component's artwork bitmap
extern HBITMAP get_main_component_artwork_bitmap();
// Shared bitmap from standalone search
extern HBITMAP g_shared_artwork_bitmap;
// Logo loading functions (declared in sdk_main.cpp)
extern pfc::string8 extract_domain_from_stream_url(metadb_handle_ptr track);
extern pfc::string8 extract_full_path_from_stream_url(metadb_handle_ptr track);
extern pfc::string8 extract_station_name_from_metadata(metadb_handle_ptr track);
extern HBITMAP load_station_logo(const pfc::string8& domain);
extern HBITMAP load_station_logo(metadb_handle_ptr track);
extern Gdiplus::Bitmap* load_station_logo_gdiplus(metadb_handle_ptr track);
extern HBITMAP load_noart_logo(metadb_handle_ptr track);
extern std::unique_ptr<Gdiplus::Bitmap> load_noart_logo_gdiplus(metadb_handle_ptr track);
extern HBITMAP load_noart_logo(const pfc::string8& domain);
extern HBITMAP load_generic_noart_logo(metadb_handle_ptr track);
extern HBITMAP load_generic_noart_logo();
extern std::unique_ptr<Gdiplus::Bitmap> load_generic_noart_logo_gdiplus();
// External functions for triggering main component search
extern void trigger_main_component_search(metadb_handle_ptr track);
extern void trigger_main_component_search_with_metadata(const std::string& artist, const std::string& title);
extern void trigger_main_component_local_search(metadb_handle_ptr track);
//=============================================================================
// Event-Driven Artwork System (Forward Declarations)
//=============================================================================
// Artwork event types
enum class ArtworkEventType {
ARTWORK_LOADED, // New artwork loaded successfully
ARTWORK_LOADING, // Search started
ARTWORK_FAILED, // Search failed
ARTWORK_CLEARED // Artwork cleared
};
// Artwork event data
struct ArtworkEvent {
ArtworkEventType type;
HBITMAP bitmap;
std::string source;
std::string artist;
std::string title;
ArtworkEvent(ArtworkEventType t, HBITMAP bmp = nullptr, const std::string& src = "",
const std::string& art = "", const std::string& ttl = "")
: type(t), bitmap(bmp), source(src), artist(art), title(ttl) {}
};
// Artwork event listener interface
class IArtworkEventListener {
public:
virtual ~IArtworkEventListener() = default;
virtual void on_artwork_event(const ArtworkEvent& event) = 0;
};
// Forward declare the event manager (implemented in main component)
class ArtworkEventManager {
public:
static ArtworkEventManager& get();
void subscribe(IArtworkEventListener* listener);
void unsubscribe(IArtworkEventListener* listener);
void notify(const ArtworkEvent& event);
};
// External references to event manager methods (defined in sdk_main.cpp)
extern ArtworkEventManager& get_artwork_event_manager();
extern void subscribe_to_artwork_events(IArtworkEventListener* listener);
extern void unsubscribe_from_artwork_events(IArtworkEventListener* listener);
//=============================================================================
// CUI Artwork Panel Class Definition - Full Implementation
//=============================================================================
class CUIArtworkPanel : public uie::container_uie_window_v3
, public now_playing_album_art_notify
, public play_callback
, public IArtworkEventListener
{
public:
CUIArtworkPanel();
~CUIArtworkPanel();
// Timer management for clear panel functionality
void update_clear_panel_timer(); // Start/stop clear panel monitoring timer based on setting
void force_clear_artwork_bitmap(); // Force clear bitmap for "clear panel when not playing" option
void load_noart_image(); // Load noart image for "use noart image" option
// Required uie::window interface
const GUID& get_extension_guid() const override;
void get_name(pfc::string_base& out) const override;
void get_category(pfc::string_base& out) const override;
unsigned get_type() const override;
// Window management
bool is_available(const uie::window_host_ptr& p_host) const override;
// Container window configuration (creates borderless panel like JScript Panel)
uie::container_window_v3_config get_window_config() override {
uie::container_window_v3_config config(L"foo_artwork_cui_panel_borderless", false);
// KEY: Keep default extended_window_styles = WS_EX_CONTROLPARENT
// Do NOT add WS_EX_CLIENTEDGE or WS_EX_STATICEDGE (which cause borders)
// This matches how JScript Panel creates borderless panels
// Add flicker-reduction window styles and enable double-clicks
config.window_styles |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
config.class_styles = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
config.class_cursor = IDC_ARROW;
config.class_background = nullptr; // No background brush (we handle it in WM_PAINT)
return config;
}
// Container window message handler (replaces manual window creation)
LRESULT on_message(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) override;
// Configuration
void set_config(stream_reader* p_reader, size_t p_size, abort_callback& p_abort) override {}
void get_config(stream_writer* p_writer, abort_callback& p_abort) const override {}
bool have_config_popup() const override { return false; }
// foobar2000 callbacks
void on_album_art(album_art_data::ptr data) noexcept override;
void on_playback_new_track(metadb_handle_ptr p_track) override;
void on_playback_stop(play_control::t_stop_reason p_reason) override;
// Required play_callback methods
void on_playback_starting(play_control::t_track_command p_command, bool p_paused) override {}
void on_playback_seek(double p_time) 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_playback_time(double p_time) override {}
void on_volume_change(float p_new_val) override {}
// IArtworkEventListener implementation
void on_artwork_event(const ArtworkEvent& event) override;
const char* bool_cast(const bool b) {
return b ? "true" : "false";
}
private:
// Window state (managed by container_uie_window_v3)
HWND m_hWnd;
// GDI+ objects for artwork rendering
std::unique_ptr<Gdiplus::Graphics> m_graphics;
std::unique_ptr<Gdiplus::Bitmap> m_artwork_bitmap;
IStream* m_artwork_stream = nullptr;
HBITMAP m_scaled_gdi_bitmap; // GDI bitmap for rendering (like Default UI)
// Artwork state
std::wstring m_current_artwork_path;
std::string m_current_artwork_source;
bool m_artwork_loaded;
bool m_fit_to_window;
bool m_was_playing; // Track previous playback state for clear panel detection
// OSD (On-Screen Display) system
bool m_show_osd;
std::string m_osd_text;
std::string m_artwork_source; // Track the source of current artwork
std::string m_delayed_search_artist; // Store artist for delayed search
std::string m_delayed_search_title; // Store title for delayed search
DWORD m_osd_start_time;
int m_osd_slide_offset;
UINT_PTR m_osd_timer_id;
bool m_osd_visible;
// Download icon hover state
bool m_mouse_hovering;
bool m_hover_over_download;
RECT m_download_icon_rect;
BYTE m_download_fade_alpha;
UINT_PTR m_download_fade_timer_id;
// Event-driven artwork system (replaces polling)
HBITMAP m_last_event_bitmap;
// Container window message handling (used by container_uie_window_v3)
// Note: LRESULT on_message() is already declared in public section
// Artwork loading and management
void load_artwork_from_data(album_art_data::ptr data);
void load_artwork_from_file(const std::wstring& file_path);
void clear_artwork();
// Rendering functions
void paint_artwork(HDC hdc);
void paint_no_artwork(HDC hdc);
void paint_loading(HDC hdc);
// OSD functions
void show_osd(const std::string& text);
void hide_osd();
void update_osd_animation();
void paint_osd(HDC hdc);
// Utility functions
void resize_artwork_to_fit();
std::wstring get_formatted_text(const std::string& text);
void initialize_gdiplus();
// Safe track information access
bool get_safe_track_path(metadb_handle_ptr track, pfc::string8& path);
bool is_safe_internet_stream(metadb_handle_ptr track);
bool is_stream_with_possible_artwork(metadb_handle_ptr track);
bool is_youtube_stream(metadb_handle_ptr track);
void cleanup_gdiplus();
bool copy_bitmap_from_main_component(HBITMAP source_bitmap);
bool load_custom_logo_with_wic(HBITMAP logo_bitmap); // WIC-based safe loading for CUI
void search_artwork_for_track(metadb_handle_ptr track);
void search_artwork_with_metadata(const std::string& artist, const std::string& title);
bool is_station_name(const std::string& artist, const std::string& title); // Station name detection helper
bool is_metadata_valid_for_search(const char* artist, const char* title); // Metadata validation
bool is_inverted_internet_stream(metadb_handle_ptr track, const file_info& p_info); //Inverted stream detection helper
// Stream dynamic info metadata storage
void clear_dinfo();
std::string m_dinfo_artist;
std::string m_dinfo_title;
public:
// Static color change handler (public so color client can access it)
static void g_on_colours_change();
// Constants
static const int OSD_DELAY_DURATION = 1000; // 1 second delay before animation starts
static const int OSD_DURATION_MS = 5000; // 5 seconds visible duration
static const int OSD_ANIMATION_SPEED = 8; // 120 FPS: 1000ms / 120fps ≈ 8ms
static const int OSD_SLIDE_DISTANCE = 200;
static const int OSD_SLIDE_IN_DURATION = 300; // 300ms smooth slide in
static const int OSD_SLIDE_OUT_DURATION = 300; // 300ms smooth slide out
};
//=============================================================================
// Static variables and registration
//=============================================================================
// Panel GUID - unique identifier for this CUI panel
static const GUID g_cui_artwork_panel_guid =
{ 0xB1C2D3E4, 0xF5F6, 0x7890, { 0xAB, 0xCD, 0xEF, 0x13, 0x24, 0x57, 0x68, 0x9B } };
// Factory registration
static uie::window_factory<CUIArtworkPanel> g_cui_artwork_panel_factory;
//=============================================================================
// Global functions for managing CUI clear panel timers
//=============================================================================
// Global list of CUI artwork panels for preference updates
static pfc::list_t<CUIArtworkPanel*> g_cui_artwork_panels;
// Static color change handler implementation
void CUIArtworkPanel::g_on_colours_change() {
for (t_size i = 0; i < g_cui_artwork_panels.get_count(); i++) {
CUIArtworkPanel* panel = g_cui_artwork_panels[i];
if (panel && panel->get_wnd()) {
InvalidateRect(panel->get_wnd(), NULL, TRUE);
}
}
}
// Global function to update CUI timers (called from sdk_main.cpp)
void update_all_cui_clear_panel_timers() {
for (t_size i = 0; i < g_cui_artwork_panels.get_count(); i++) {
g_cui_artwork_panels[i]->update_clear_panel_timer();
}
}
// Re-trigger artwork lookup on all CUI panels using the now-playing track
void refresh_all_cui_artwork_panels() {
static_api_ptr_t<playback_control> pc;
metadb_handle_ptr track;
if (!pc->get_now_playing(track) || !track.is_valid()) return;
for (t_size i = 0; i < g_cui_artwork_panels.get_count(); i++) {
CUIArtworkPanel* panel = g_cui_artwork_panels[i];
if (panel && panel->get_wnd()) {
panel->on_playback_new_track(track);
}
}
}
// Registration verification function
static class cui_registration_helper {
public:
cui_registration_helper() {
}
} g_cui_registration_helper;
//=============================================================================
// Download icon overlay helper
//=============================================================================
static void draw_download_icon(HDC hdc, const RECT& client_rect, bool hovered, RECT& out_icon_rect, BYTE fade_alpha = 255)
{
// Only show when foo_artgrab is loaded
HMODULE hGrab = GetModuleHandle(L"foo_artgrab.dll");
if (!hGrab || fade_alpha == 0) {
SetRectEmpty(&out_icon_rect);
return;
}
const int icon_size = 24;
const int padding = 10;
const int bg_pad = 6;
// Position at bottom-left
int ix = client_rect.left + padding;
int iy = client_rect.bottom - padding - icon_size;
// Background pill rect
RECT bg = { ix - bg_pad, iy - bg_pad, ix + icon_size + bg_pad, iy + icon_size + bg_pad };
Gdiplus::Graphics g(hdc);
g.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
// Semi-transparent rounded-rect background (modulated by fade alpha)
BYTE base_alpha = hovered ? (BYTE)200 : (BYTE)120;
BYTE alpha = (BYTE)((int)base_alpha * fade_alpha / 255);
Gdiplus::SolidBrush bgBrush(Gdiplus::Color(alpha, 0, 0, 0));
int radius = 6;
{
Gdiplus::GraphicsPath path;
Gdiplus::RectF rf((Gdiplus::REAL)bg.left, (Gdiplus::REAL)bg.top,
(Gdiplus::REAL)(bg.right - bg.left), (Gdiplus::REAL)(bg.bottom - bg.top));
float d = (float)radius * 2.0f;
path.AddArc(rf.X, rf.Y, d, d, 180, 90);
path.AddArc(rf.X + rf.Width - d, rf.Y, d, d, 270, 90);
path.AddArc(rf.X + rf.Width - d, rf.Y + rf.Height - d, d, d, 0, 90);
path.AddArc(rf.X, rf.Y + rf.Height - d, d, d, 90, 90);
path.CloseFigure();
g.FillPath(&bgBrush, &path);
}
// Draw the download icon (Material Design download arrow + tray)
// SVG path translated from 960x960 viewBox to 24x24
// Original: M480-320 280-520l56-58 104 104v-326h80v326l104-104 56 58-200 200Z
// M240-160q-33 0-56.5-23.5T160-240v-120h80v120h480v-120h80v120q0 33-23.5 56.5T720-160H240Z
float scale = (float)icon_size / 960.0f;
float ox = (float)ix;
float oy = (float)iy + (float)icon_size; // SVG y is negative, so offset from bottom
BYTE icon_alpha = (BYTE)((int)230 * fade_alpha / 255);
Gdiplus::SolidBrush iconBrush(Gdiplus::Color(icon_alpha, 255, 255, 255));
// Arrow portion (pointing down)
{
Gdiplus::GraphicsPath arrow;
// Convert SVG coords: SVG uses y-up with range -960..0, we map to 0..24
// SVG point (sx, sy) -> screen (ox + sx*scale, oy + sy*scale)
// where sy is negative in SVG space
Gdiplus::PointF pts[] = {
{ ox + 480*scale, oy + (-320)*scale }, // bottom tip
{ ox + 280*scale, oy + (-520)*scale }, // left of arrow head
{ ox + 336*scale, oy + (-578)*scale }, // left after 56,-58
{ ox + 440*scale, oy + (-474)*scale }, // inner left after 104,104
{ ox + 440*scale, oy + (-800)*scale }, // top left of shaft
{ ox + 520*scale, oy + (-800)*scale }, // top right of shaft
{ ox + 520*scale, oy + (-474)*scale }, // inner right
{ ox + 624*scale, oy + (-578)*scale }, // right after 104,-104
{ ox + 680*scale, oy + (-520)*scale }, // right of arrow head
};
arrow.AddPolygon(pts, 9);
g.FillPath(&iconBrush, &arrow);
}
// Tray portion (flat bottom with sides)
{
Gdiplus::GraphicsPath tray;
Gdiplus::PointF pts[] = {
{ ox + 160*scale, oy + (-240)*scale }, // bottom-left outer (after rounding)
{ ox + 160*scale, oy + (-360)*scale }, // top-left
{ ox + 240*scale, oy + (-360)*scale }, // inner top-left
{ ox + 240*scale, oy + (-280)*scale }, // inner bottom-left
{ ox + 720*scale, oy + (-280)*scale }, // inner bottom-right
{ ox + 720*scale, oy + (-360)*scale }, // inner top-right
{ ox + 800*scale, oy + (-360)*scale }, // top-right
{ ox + 800*scale, oy + (-240)*scale }, // bottom-right outer
{ ox + 720*scale, oy + (-160)*scale }, // bottom-right (after curve approx)
{ ox + 240*scale, oy + (-160)*scale }, // bottom-left (after curve approx)
};
tray.AddPolygon(pts, 10);
g.FillPath(&iconBrush, &tray);
}
// Output hit-test rect
out_icon_rect = bg;
}
//=============================================================================
// Constructor and Destructor
//=============================================================================
CUIArtworkPanel::CUIArtworkPanel()
: m_hWnd(NULL)
, m_artwork_loaded(false)
, m_fit_to_window(false)
, m_was_playing(false)
, m_show_osd(true)
, m_osd_start_time(0)
, m_osd_slide_offset(OSD_SLIDE_DISTANCE)
, m_osd_timer_id(0)
, m_osd_visible(false)
, m_last_event_bitmap(nullptr)
, m_artwork_stream(nullptr)
, m_scaled_gdi_bitmap(NULL)
, m_download_fade_alpha(0)
, m_download_fade_timer_id(0)
, m_mouse_hovering(false)
, m_hover_over_download(false)
, m_download_icon_rect{}
{
// Register for artwork events (replaces polling)
subscribe_to_artwork_events(this);
// Add to global list for preference updates
g_cui_artwork_panels.add_item(this);
}
CUIArtworkPanel::~CUIArtworkPanel() {
// Remove from global list
g_cui_artwork_panels.remove_item(this);
// Unregister from artwork events
unsubscribe_from_artwork_events(this);
if (m_scaled_gdi_bitmap) {
DeleteObject(m_scaled_gdi_bitmap);
m_scaled_gdi_bitmap = NULL;
}
cleanup_gdiplus();
}
//=============================================================================
// CUI Extension interface implementation
//=============================================================================
const GUID& CUIArtworkPanel::get_extension_guid() const {
return g_cui_artwork_panel_guid;
}
void CUIArtworkPanel::get_name(pfc::string_base& out) const {
out = "Artwork Display";
}
void CUIArtworkPanel::get_category(pfc::string_base& out) const {
out = "Panels";
}
unsigned CUIArtworkPanel::get_type() const {
return uie::type_panel;
}
//=============================================================================
// Window management
//=============================================================================
bool CUIArtworkPanel::is_available(const uie::window_host_ptr& p_host) const {
return true; // Always available
}
// Window creation is now handled by container_uie_window_v3
//=============================================================================
// Window procedure and message handling
//=============================================================================
LRESULT CUIArtworkPanel::on_message(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
// Store window handle for compatibility
if (!m_hWnd) {
m_hWnd = wnd;
}
switch (msg) {
case WM_CREATE:
// Initialize GDI+
initialize_gdiplus();
// Register for foobar2000 callbacks
try {
now_playing_album_art_notify_manager::get()->add(this);
play_callback_manager::get()->register_callback(this,
play_callback::flag_on_playback_new_track |
play_callback::flag_on_playback_stop |
play_callback::flag_on_playback_dynamic_info_track, false);
// Request artwork for current track if playing
auto pc = playback_control::get();
if (pc->is_playing()) {
metadb_handle_ptr track;
if (pc->get_now_playing(track)) {
on_playback_new_track(track);
}
m_was_playing = true; // Initialize state if currently playing
}
// Start timer to monitor playback state for clear panel functionality
if (cfg_clear_panel_when_not_playing) {
SetTimer(m_hWnd, 102, 500, NULL); // Timer ID 102, check every 0.5 seconds
m_current_artwork_source = "CUI Timer auto-started on creation";
InvalidateRect(m_hWnd, NULL, TRUE);
}
} catch (const std::exception& e) {
}
// Note: No need for polling timer - using event-driven artwork updates
break;
case WM_DESTROY:
// Stop timers
if (m_osd_timer_id) {
KillTimer(m_hWnd, m_osd_timer_id);
m_osd_timer_id = 0;
}
KillTimer(m_hWnd, 102); // Stop playback monitoring timer
if (m_download_fade_timer_id) {
KillTimer(m_hWnd, 1002);
m_download_fade_timer_id = 0;
}
// Note: No artwork polling timer to stop - using event-driven system
// Unregister callbacks
now_playing_album_art_notify_manager::get()->remove(this);
play_callback_manager::get()->unregister_callback(this);
// Cleanup GDI+
cleanup_gdiplus();
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(m_hWnd, &ps);
// Use double buffering to eliminate flicker during resizing
RECT client_rect;
GetClientRect(m_hWnd, &client_rect);
// Create memory DC and bitmap for double buffering
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBitmap = CreateCompatibleBitmap(hdc, client_rect.right, client_rect.bottom);
HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, memBitmap);
// Paint to memory DC first (off-screen)
paint_artwork(memDC);
// Draw download icon overlay when hovering (only when a track is playing, skip streams)
if (m_mouse_hovering || m_download_fade_alpha > 0) {
bool should_show = false;
if (m_mouse_hovering) {
static_api_ptr_t<playback_control> pc;
if (pc->is_playing()) {
bool is_stream = false;
metadb_handle_ptr track;
if (pc->get_now_playing(track) && track.is_valid()) {
pfc::string8 path = track->get_path();
is_stream = strstr(path.c_str(), "://") && !strstr(path.c_str(), "file://");
}
should_show = !is_stream;
}
}
if (should_show) {
// Fade in: jump to full opacity and stop any fade timer
if (m_download_fade_alpha < 255) {
m_download_fade_alpha = 255;
if (m_download_fade_timer_id) {
KillTimer(m_hWnd, 1002);
m_download_fade_timer_id = 0;
}
}
draw_download_icon(memDC, client_rect, m_hover_over_download, m_download_icon_rect, m_download_fade_alpha);
} else if (m_download_fade_alpha > 0) {
// Fading out - draw at current fade alpha
draw_download_icon(memDC, client_rect, false, m_download_icon_rect, m_download_fade_alpha);
} else {
SetRectEmpty(&m_download_icon_rect);
}
}
// Paint OSD if visible
if (m_osd_visible) {
paint_osd(memDC);
}
// Copy the entire off-screen buffer to screen in one operation (flicker-free)
BitBlt(hdc, 0, 0, client_rect.right, client_rect.bottom, memDC, 0, 0, SRCCOPY);
// Cleanup
SelectObject(memDC, oldBitmap);
DeleteObject(memBitmap);
DeleteDC(memDC);
EndPaint(m_hWnd, &ps);
return 0;
}
case WM_SIZE:
// Resize artwork to fit new window size
resize_artwork_to_fit();
// Use RedrawWindow for flicker-free resizing
RedrawWindow(m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_NOCHILDREN);
return 0; // Prevent default processing
case WM_ERASEBKGND:
// Always return 1 to prevent background erasing (causes flicker)
return 1;
case WM_TIMER:
if (wParam == 1002) {
// Download icon fade-out animation
const BYTE fade_step = 20; // ~200ms total fade at 60fps
if (m_download_fade_alpha <= fade_step) {
m_download_fade_alpha = 0;
KillTimer(wnd, 1002);
m_download_fade_timer_id = 0;
SetRectEmpty(&m_download_icon_rect);
} else {
m_download_fade_alpha -= fade_step;
}
InvalidateRect(wnd, nullptr, FALSE);
return 0;
} else if (wParam == m_osd_timer_id) {
update_osd_animation();
} else if (wParam == 100) {
// Fallback timer - no Default UI panel detected, handle station logos ourselves
KillTimer(m_hWnd, 100);
// CHECK: Only trigger fallback if we don't already have tagged artwork
if (m_artwork_loaded && !m_artwork_source.empty() && m_artwork_source == "Local artwork") {
break;
}
// Try to load station logo for internet stream
static_api_ptr_t<playback_control> pc;
metadb_handle_ptr current_track;
if (pc->get_now_playing(current_track) && current_track.is_valid()) {
// SAFE PATH ACCESS: Use safer helper function
if (is_safe_internet_stream(current_track)) {
// Manually trigger the fallback mechanism
PostMessage(m_hWnd, WM_USER + 11, 0, 0);
}
}
} else if (wParam == 101) {
// Delay timer fired - now do the delayed search
KillTimer(m_hWnd, 101);
// Use stored metadata for delayed search
if (!m_delayed_search_title.empty()) {
// Apply unified metadata cleaning for consistency with DUI mode
// Extract only the first artist for better artwork search results
std::string first_artist = MetadataCleaner::extract_first_artist(m_delayed_search_artist.c_str());
std::string final_artist = MetadataCleaner::clean_for_search(first_artist.c_str(), true);
std::string final_title = MetadataCleaner::clean_for_search(m_delayed_search_title.c_str(), true);
extern void trigger_main_component_search_with_metadata(const std::string& artist, const std::string& title);
trigger_main_component_search_with_metadata(final_artist, final_title);
// Clear stored metadata
m_delayed_search_artist.clear();
m_delayed_search_title.clear();
} else {
}
} else if (wParam == 102) {
// Timer ID 102 - playback state monitoring for clear panel
static_api_ptr_t<playback_control> pc;
bool is_playing = pc->is_playing();
// If we were playing but now we're not, clear the panel
if (m_was_playing && !is_playing && cfg_clear_panel_when_not_playing) {
if (cfg_use_noart_image) {
// Load and display noart image instead of clearing
load_noart_image();
} else {
// Just clear the panel
force_clear_artwork_bitmap();
}
}
// Update the previous state
m_was_playing = is_playing;
// If option is disabled, stop the timer
if (!cfg_clear_panel_when_not_playing) {
KillTimer(m_hWnd, 102);
}
}
break;
case WM_USER + 10: // Artwork event update (from background thread)
{
HBITMAP bitmap = (HBITMAP)wParam;
std::string* source_ptr = (std::string*)lParam;
// Extract the source string from the message
std::string artwork_source = source_ptr ? *source_ptr : "";
// Clean up the allocated source string
if (source_ptr) {
delete source_ptr;
}
// Dedup check and cancel fallback timer (safe on main thread)
if (bitmap == m_last_event_bitmap) {
break; // Already processed this bitmap
}
m_last_event_bitmap = bitmap;
KillTimer(m_hWnd, 100); // Cancel fallback timer since artwork was found
// PRIORITY CHECK: Don't let API results override tagged artwork, only overide when radio
static_api_ptr_t<playback_control> pc;
metadb_handle_ptr current_track;
if (pc->get_now_playing(current_track) && current_track.is_valid()) {
if (m_artwork_loaded && !m_artwork_source.empty() &&
m_artwork_source == "Local artwork" && artwork_source != "Local artwork" && is_stream_with_possible_artwork(current_track)) {
break;
}
}
// Now it's safe to call UI functions since we're on the main thread
if (bitmap && copy_bitmap_from_main_component(bitmap)) {
// Update the member variable with the correct source
m_artwork_source = artwork_source;
// IMPORTANT: Kill all fallback timers since we found artwork
if (artwork_source == "Local artwork") {
KillTimer(m_hWnd, 100); // Metadata arrival timer
KillTimer(m_hWnd, 101); // Delay timer
}
// Only show OSD for online sources, not local files
if (artwork_source != "Local file" && !artwork_source.empty()) {
show_osd("Artwork from " + artwork_source);
} else if (artwork_source == "Local artwork") {
show_osd("Tagged artwork");
}
InvalidateRect(m_hWnd, NULL, FALSE);
UpdateWindow(m_hWnd); // Force immediate repaint
} else {
// Clean up even if bitmap processing failed
}
}
break;
case WM_USER + 11: // Handle -noart fallback on main thread
{
// CHECK: Don't override existing tagged artwork with fallback images
if (m_artwork_loaded && !m_artwork_source.empty() && m_artwork_source == "Local artwork") {
break;
}
// Now it's safe to access foobar2000 APIs and UI functions
try {
static_api_ptr_t<playback_control> pc;
metadb_handle_ptr current_track;
if (pc->get_now_playing(current_track) && current_track.is_valid()) {
// SAFE PATH ACCESS: Add try-catch to prevent crashes
pfc::string8 path;
bool is_internet_stream = false;
// SAFE PATH ACCESS: Use safer helper function
if (get_safe_track_path(current_track, path)) {
is_internet_stream = (strstr(path.c_str(), "://") && !strstr(path.c_str(), "file://"));
} else {
is_internet_stream = false;
}
if (is_internet_stream && cfg_enable_custom_logos) {
// CRASH FIX: Add safe guards before complex file operations
try {
// First check if track is still valid
if (!current_track.is_valid()) return DefWindowProc(wnd, msg, wParam, lParam);
// Try to extract domain from URL - do this safely
pfc::string8 domain = extract_domain_from_stream_url(current_track);
if (!domain.is_empty() && domain.length() < 256) { // Prevent overly long domains
// CRASH FIX: Use safer logo loading without direct file operations in CUI
// Instead of complex file path building, use the SDK functions which are already crash-protected
bool fallback_loaded = false;
// Priority 1: Station logo (with full path + domain fallback)
if (!fallback_loaded) {
// First try direct GDI+ loading to preserve transparency
Gdiplus::Bitmap* gdi_logo = load_station_logo_gdiplus(current_track);
if (gdi_logo && gdi_logo->GetLastStatus() == Gdiplus::Ok) {
// Set the artwork directly from GDI+ bitmap
m_artwork_bitmap = std::unique_ptr<Gdiplus::Bitmap>(gdi_logo);
m_artwork_loaded = true;
m_artwork_source = "Station logo";
fallback_loaded = true;
InvalidateRect(get_wnd(), NULL, TRUE);
} else {
// Clean up failed GDI+ bitmap
delete gdi_logo;
// Fallback to HBITMAP method
HBITMAP logo_bitmap = load_station_logo(current_track);
if (logo_bitmap) {
// CRASH FIX: Use WIC-based loading for CUI compatibility
if (load_custom_logo_with_wic(logo_bitmap)) {
m_artwork_loaded = true;
m_artwork_source = "Station logo";
fallback_loaded = true;
}
DeleteObject(logo_bitmap); // Always clean up the source bitmap
}
}
}
// Priority 2: Station-specific noart (with full URL path support)
if (!fallback_loaded) {
auto noart_bitmap = load_noart_logo_gdiplus(current_track);
if (noart_bitmap && noart_bitmap->GetLastStatus() == Gdiplus::Ok) {
// Set the artwork directly from GDI+ bitmap
m_artwork_bitmap = std::move(noart_bitmap);
m_artwork_loaded = true;
m_artwork_source = "Station fallback (no artwork)";
fallback_loaded = true;
}
}
// Priority 3: Generic noart (with full URL path support)
if (!fallback_loaded) {
auto generic_bitmap = load_generic_noart_logo_gdiplus();
if (generic_bitmap && generic_bitmap->GetLastStatus() == Gdiplus::Ok) {
// Set the artwork directly from GDI+ bitmap
m_artwork_bitmap = std::move(generic_bitmap);
m_artwork_loaded = true;
m_artwork_source = "Generic fallback (no artwork)";
fallback_loaded = true;
}
}
if (fallback_loaded) {
resize_artwork_to_fit();
InvalidateRect(m_hWnd, NULL, FALSE);
UpdateWindow(m_hWnd);
}
} // End domain check
} catch (...) {
// Silently handle any exceptions in custom logo loading
}
}
}
} catch (...) {
// Silently handle any exceptions in track processing
}
}
break;
case WM_USER + 12: // Artwork cleared — reset dedup state on main thread
m_last_event_bitmap = nullptr;
break;
case WM_MOUSEMOVE:
{
if (!m_mouse_hovering) {
m_mouse_hovering = true;
TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, wnd, 0 };
TrackMouseEvent(&tme);
InvalidateRect(wnd, nullptr, FALSE);
}
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
bool over = !IsRectEmpty(&m_download_icon_rect) &&
PtInRect(&m_download_icon_rect, pt);
if (over != m_hover_over_download) {
m_hover_over_download = over;
InvalidateRect(wnd, &m_download_icon_rect, FALSE);
}
if (m_hover_over_download) {
SetCursor(LoadCursor(NULL, IDC_HAND));
}
return 0;
}
case WM_MOUSELEAVE:
{
m_mouse_hovering = false;
m_hover_over_download = false;
// Start fade-out animation if icon was visible
if (m_download_fade_alpha > 0 && !m_download_fade_timer_id) {
m_download_fade_timer_id = SetTimer(wnd, 1002, 16, NULL); // ~60 FPS
}
if (IsRectEmpty(&m_download_icon_rect)) {
InvalidateRect(wnd, nullptr, FALSE);
}
return 0;
}
case WM_SETCURSOR:
{
if (LOWORD(lParam) == HTCLIENT && m_hover_over_download) {
SetCursor(LoadCursor(NULL, IDC_HAND));
return TRUE;
}
return DefWindowProc(wnd, msg, wParam, lParam);
}
case WM_LBUTTONDOWN:
{
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
if (!IsRectEmpty(&m_download_icon_rect) && PtInRect(&m_download_icon_rect, pt)) {
typedef void (*pfn_open)(const char*, const char*, const char*);
HMODULE hGrab = GetModuleHandle(L"foo_artgrab.dll");