Player.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package Plugins::SqueezeESP32::Player;
  2. use strict;
  3. use base qw(Slim::Player::SqueezePlay);
  4. use Digest::MD5 qw(md5);
  5. use List::Util qw(min);
  6. use Slim::Utils::Log;
  7. use Slim::Utils::Prefs;
  8. my $prefs = preferences('plugin.squeezeesp32');
  9. my $log = logger('plugin.squeezeesp32');
  10. sub model { 'squeezeesp32' }
  11. sub modelName { 'SqueezeESP32' }
  12. sub hasIR { 0 }
  13. sub init {
  14. my $client = shift;
  15. $client->SUPER::init(@_);
  16. $client->config_artwork();
  17. }
  18. # Allow the player to define it's display width (and probably more)
  19. sub playerSettingsFrame {
  20. my $client = shift;
  21. my $data_ref = shift;
  22. my $value;
  23. my $id = unpack('C', $$data_ref);
  24. # New SETD command 0xfe for display width & height
  25. if ($id == 0xfe) {
  26. $value = (unpack('Cn', $$data_ref))[1];
  27. if ($value > 100 && $value < 400) {
  28. $prefs->client($client)->set('width', $value);
  29. my $height = (unpack('Cnn', $$data_ref))[2];
  30. $prefs->client($client)->set('height', $height || 0);
  31. $client->display->modes($client->display->build_modes);
  32. $client->display->widthOverride(1, $value);
  33. $client->update;
  34. main::INFOLOG && $log->is_info && $log->info("Setting player $value" . "x" . "$height for ", $client->name);
  35. }
  36. }
  37. $client->SUPER::playerSettingsFrame($data_ref);
  38. }
  39. sub hasScrolling {
  40. return 1;
  41. }
  42. sub update_artwork {
  43. my $client = shift;
  44. my $cprefs = $prefs->client($client);
  45. my $artwork = $cprefs->get('artwork') || return;
  46. return unless $artwork->{'enable'};
  47. my $s = min($cprefs->get('height') - $artwork->{'y'}, $cprefs->get('width') - $artwork->{'x'});
  48. my $params = { force => shift || 0 };
  49. my $path = 'music/current/cover_' . $s . 'x' . $s . '_o.jpg';
  50. my $body = Slim::Web::Graphics::artworkRequest($client, $path, $params, \&send_artwork, undef, HTTP::Response->new);
  51. send_artwork($client, undef, \$body) if $body;
  52. }
  53. sub send_artwork {
  54. my ($client, $params, $dataref) = @_;
  55. # I'm not sure why we are called so often, so only send when needed
  56. my $md5 = md5($$dataref);
  57. return if $client->pluginData('artwork_md5') eq $md5 && !$params->{'force'};
  58. $client->pluginData('artwork', $dataref);
  59. $client->pluginData('artwork_md5', $md5);
  60. my $artwork = $prefs->client($client)->get('artwork') || {};
  61. my $length = length $$dataref;
  62. my $offset = 0;
  63. $log->info("got resized artwork (length: ", length $$dataref, ")");
  64. my $header = pack('Nnn', $length, $artwork->{'x'}, $artwork->{'y'});
  65. while ($length > 0) {
  66. $length = 1280 if $length > 1280;
  67. $log->info("sending grfa $length");
  68. my $data = $header . pack('N', $offset) . substr( $$dataref, 0, $length, '' );
  69. $client->sendFrame( grfa => \$data );
  70. $offset += $length;
  71. $length = length $$dataref;
  72. }
  73. }
  74. sub clear_artwork {
  75. my ($client, $request) = @_;
  76. my $artwork = $prefs->client($client)->get('artwork');
  77. if ($artwork && $artwork->{'enable'}) {
  78. main::INFOLOG && $log->is_info && $log->info("artwork stop/clear " . $request->getRequestString());
  79. $client->pluginData('artwork_md5', '');
  80. }
  81. }
  82. sub config_artwork {
  83. my ($client) = @_;
  84. if ( my $artwork = $prefs->client($client)->get('artwork') ) {
  85. my $header = pack('Nnn', $artwork->{'enable'}, $artwork->{'x'}, $artwork->{'y'});
  86. $client->sendFrame( grfa => \$header );
  87. }
  88. }
  89. sub reconnect {
  90. my $client = shift;
  91. $client->pluginData('artwork_md5', '');
  92. $client->SUPER::reconnect(@_);
  93. }
  94. 1;