-
Notifications
You must be signed in to change notification settings - Fork 16
/
upnp-display.cc
299 lines (267 loc) · 9.99 KB
/
upnp-display.cc
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
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// This file is part of UPnP LCD Display
//
// Copyright (C) 2013 Henner Zeller <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "upnp-display.h"
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include "printer.h"
#include "renderer-state.h"
#include "scroller.h"
#include "utf8.h"
// Time between display updates.
// This influences scroll speed and 'pause' blinking.
// Note, too fast scrolling looks blurry on cheap displays.
static const int kDisplayUpdateMillis = 400;
// Number of periods, a changed volume flashes up.
static const int kVolumeFlashTime = 3;
// We do the signal receiving the classic static way, as creating callbacks to
// c functions is more readable than with c++ methods :)
volatile bool signal_received = false;
static void SigReceiver(int) {
signal_received = true;
}
#define STOP_SYMBOL "\u2b1b" // ⬛
#define PLAY_SYMBOL "\u25b6" // ▶
#define PAUSE_SYMBOL "][" // TODO: add symbol in private unicode range.
UPnPDisplay::UPnPDisplay(const std::string &friendly_name, Printer *printer,
int screensave_timeout, FILE *logstream)
: player_match_name_(friendly_name),
printer_(printer), logstream_(logstream),
screensave_timeout_(screensave_timeout),
current_state_(NULL) {
pthread_mutex_init(&mutex_, NULL);
signal(SIGTERM, &SigReceiver);
signal(SIGINT, &SigReceiver);
}
void UPnPDisplay::Loop() {
std::string player_name;
std::string title, composer, artist, album;
std::string play_state = "STOPPED";
std::string volume, previous_volume;
time_t last_update = 0;
int track_time = 0;
int relative_time = 0;
Scroller first_line_scroller(" - ");
Scroller second_line_scroller(" - ");
unsigned char blink_time = 0;
int volume_countdown = 0;
signal_received = false;
while (!signal_received) {
usleep(kDisplayUpdateMillis * 1000);
const time_t now = time(NULL);
last_update = 0;
bool renderer_available = false;
bool muted = false;
pthread_mutex_lock(&mutex_);
if (current_state_ != NULL) {
renderer_available = true;
player_name = current_state_->friendly_name();
title = current_state_->GetVar("Meta_Title");
composer = current_state_->GetVar("Meta_Composer");
artist = current_state_->GetVar("Meta_Artist");
std::string creator = current_state_->GetVar("Meta_Creator");
if (artist == composer && !creator.empty() && creator != artist) {
artist = creator;
}
album = current_state_->GetVar("Meta_Album");
play_state = current_state_->GetVar("TransportState");
// "RelativeTimePosition" var is not evented by default.
// TODO: query actively.
relative_time = 0;
// for now, we just show the duration.
track_time = parseTime(current_state_->GetVar("CurrentTrackDuration"));
volume = current_state_->GetVar("Volume");
muted = current_state_->GetVar("Mute") == "1";
last_update = current_state_->last_event_update();
}
pthread_mutex_unlock(&mutex_);
if (screensave_timeout_ > 0 && last_update > 0 &&
(now - last_update) > screensave_timeout_) {
printer_->SaveScreen();
continue;
}
if (!renderer_available) {
printer_->Print(0, "Waiting for");
std::string to_print = (player_match_name_.empty()
? "any Renderer"
: player_match_name_);
CenterAlign(&to_print, printer_->width());
printer_->Print(1, to_print);
continue;
}
// First line is "[composer: ]Title"
std::string print_line = composer;
if (!print_line.empty()) print_line.append(": ");
print_line.append(title);
const bool no_title_to_display = (print_line.empty() && album.empty());
if (no_title_to_display) {
// No title, so show at least player name.
print_line = player_name;
CenterAlign(&print_line, printer_->width());
printer_->Print(0, print_line);
}
// Second line: Show volume related things if relevant.
// Either we're muted, or there was a volume change that we display
// for kVolumeFlashTime
if (muted) {
print_line = "[Muted]";
CenterAlign(&print_line, printer_->width());
printer_->Print(1, print_line);
continue;
}
else if (volume != previous_volume || volume_countdown > 0) {
if (!previous_volume.empty()) {
if (volume != previous_volume) {
volume_countdown = kVolumeFlashTime;
} else {
--volume_countdown;
}
std::string volume_line = "Volume " + volume;
CenterAlign(&volume_line, printer_->width());
printer_->Print(1, volume_line);
}
previous_volume = volume;
continue;
}
if (no_title_to_display) {
// Nothing really to display ? Show play-state.
print_line = play_state;
if (play_state == "STOPPED")
print_line = STOP_SYMBOL " [Stopped]";
else if (play_state == "PAUSED_PLAYBACK")
print_line = PAUSE_SYMBOL" [Paused]";
else if (play_state == "PLAYING")
print_line = PLAY_SYMBOL " [Playing]";
CenterAlign(&print_line, printer_->width());
printer_->Print(1, print_line);
continue;
}
// Alright, we have a title. If short enough, center, otherwise scroll.
CenterAlign(&print_line, printer_->width());
first_line_scroller.SetValue(print_line, printer_->width());
printer_->Print(0, first_line_scroller.GetScrolledContent());
std::string formatted_time;
if (play_state == "STOPPED") {
formatted_time = " " STOP_SYMBOL " ";
} else {
// relative time might not be evented. In that case, show track time.
const int show_time = relative_time ? relative_time : track_time;
formatted_time = formatTime(show_time);
// 'Blinking' time when paused.
if (play_state == "PAUSED_PLAYBACK" && blink_time % 2 == 0) {
formatted_time = std::string(formatted_time.size(), ' ');
}
}
const int remaining_len = printer_->width() - formatted_time.length() - 1;
// Assemble second line from album. Add artist, but only if we wouldn't
// exceed length (or, if we already exceed length, also append).
print_line = album;
std::string artist_addition;
if (!artist.empty() && artist != album) {
if (!print_line.empty()) artist_addition.append("/");
artist_addition.append(artist);
}
// Only append it if we'd stay within allocated screen-width. Unless the
// Album name is already so long that we'd exceed the length anyway. In
// that case, we have to scroll no matter what and including the artist
// does less harm.
if (utf8_len(print_line + artist_addition) <= remaining_len
|| utf8_len(print_line) > remaining_len) {
print_line += artist_addition;
}
// Show album/artist right aligned in space next to time. Or scroll if long.
RightAlign(&print_line, remaining_len);
second_line_scroller.SetValue(print_line, remaining_len);
printer_->Print(1, formatted_time + " "
+ second_line_scroller.GetScrolledContent());
blink_time++;
first_line_scroller.NextTick();
second_line_scroller.NextTick();
}
std::string msg = "Goodbye!";
CenterAlign(&msg, printer_->width());
printer_->Print(0, msg);
// Show off unicode :)
msg = "\u2192 \u266a\u266b\u266a\u2669 \u2190"; // → ♪♫♩ ←
CenterAlign(&msg, printer_->width());
printer_->Print(1, msg);
}
void UPnPDisplay::AddRenderer(const std::string &uuid,
const RendererState *state) {
// Not to LCD, different thread
fprintf(logstream_, "%s: connected (name='%s')\n",
uuid.c_str(), state->friendly_name().c_str());
pthread_mutex_lock(&mutex_);
if (current_state_ == NULL
&& (player_match_name_.empty()
|| player_match_name_ == uuid
|| player_match_name_ == state->friendly_name())) {
uuid_ = uuid;
current_state_ = state;
}
pthread_mutex_unlock(&mutex_);
}
void UPnPDisplay::RemoveRenderer(const std::string &uuid) {
fprintf(logstream_, "disconnect (uuid=%s)\n", uuid.c_str());
pthread_mutex_lock(&mutex_);
if (current_state_ != NULL && uuid == uuid_) {
current_state_ = NULL;
}
pthread_mutex_unlock(&mutex_);
}
int UPnPDisplay::parseTime(const std::string &upnp_time) {
int hour = 0;
int minute = 0;
int second = 0;
if (sscanf(upnp_time.c_str(), "%d:%02d:%02d", &hour, &minute, &second) == 3)
return hour * 3600 + minute * 60 + second;
return 0;
}
std::string UPnPDisplay::formatTime(int time) {
const bool is_neg = (time < 0);
time = abs(time);
const int hour = time / 3600; time %= 3600;
const int minute = time / 60; time %= 60;
const int second = time;
char buf[32];
char *pos = buf;
if (is_neg) *pos++ = '-';
if (hour > 0) {
snprintf(pos, sizeof(buf)-1, "%dh%02d:%02d", hour, minute, second);
} else {
snprintf(pos, sizeof(buf)-1, "%d:%02d", minute, second);
}
return buf;
}
void UPnPDisplay::CenterAlign(std::string *to_print, int width) {
const int len = utf8_len(*to_print);
if (len < width) {
to_print->insert(0, std::string((width - len) / 2, ' '));
}
}
void UPnPDisplay::RightAlign(std::string *to_print, int width) {
const int len = utf8_len(*to_print);
if (len < width) {
to_print->insert(0, std::string(width - len, ' '));
}
}