forked from MarkusEh/vdr-plugin-live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosd_status.cpp
182 lines (164 loc) · 5.45 KB
/
osd_status.cpp
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
#include "osd_status.h"
#include <sstream>
namespace vdrlive {
OsdStatusMonitor::OsdStatusMonitor():title(),message(),red(),green(),yellow(),blue(),text(),selected(-1),lastUpdate(0){
memset(&tabs, 0, sizeof(tabs));
}
OsdStatusMonitor::~OsdStatusMonitor() {
OsdClear();
}
void OsdStatusMonitor::OsdClear() {
title = message = text = "";
red = green = yellow = blue = "";
items.Clear();
selected = -1;
memset(&tabs, 0, sizeof(tabs));
lastUpdate= clock();
}
void OsdStatusMonitor::OsdTitle(const char *Title) {
title = Title ? Title : "";
lastUpdate= clock();
}
void OsdStatusMonitor::OsdStatusMessage(const char *Message) {
message = Message ? Message : "";
lastUpdate= clock();
}
void OsdStatusMonitor::OsdHelpKeys(const char *Red, const char *Green, const char *Yellow, const char *Blue) {
red = Red ? Red :"";
green = Green ? Green : "";
yellow = Yellow ? Yellow : "";
blue = Blue ? Blue : "";
lastUpdate= clock();
}
void OsdStatusMonitor::OsdItem(const char *Text, int Index) {
const char* tab;
const char* colStart = Text;
for (int col = 0; col < MaxTabs &&
(tab = strchr(colStart, '\t')); col++) {
int width = tab - colStart + 1;
if (width > tabs[col])
tabs[col] = width;
colStart = colStart + width;
}
items.Add(new cLiveOsdItem(Text));
lastUpdate= clock();
}
void OsdStatusMonitor::OsdCurrentItem(const char *Text) {
int i = -1;
int best = -1;
int dist = items.Count();
cLiveOsdItem * currentItem = NULL;
cLiveOsdItem *bestItem = NULL;
for (cLiveOsdItem *item = items.First(); item; item = items.Next(item)) {
if (++i == selected)
currentItem = item;
if ( item->Text().compare(Text) == 0) {
if (abs(i - selected) < dist) {
// best match is the one closest to previous position
best = i;
bestItem= item;
dist = abs(i - selected);
}
else if (selected < 0) {
// previous position unknown - take first match
best = i;
bestItem= item;
break;
}
else {
// we already have a better match, so we're done
break;
}
}
}
if (best >= 0) {
// found matching item
selected = best;
bestItem->Select(true);
if (currentItem && currentItem != bestItem){
currentItem->Select(false);
lastUpdate= clock();
}
}
else if (currentItem) {
// no match: the same item is still selected but its text changed
currentItem->Update(Text);
lastUpdate= clock();
}
}
void OsdStatusMonitor::OsdTextItem(const char *Text, bool Scroll) {
if (Text) {
text = Text;
//text= text.replace( text.begin(), text.end(), '\n', '|');
}
else {
text = "";
}
lastUpdate= clock();
}
std::string const OsdStatusMonitor::GetTitleHtml() {return !title.empty() ? "<div class=\"osdTitle\">" + EncodeHtml(title) + "</div>" : "";}
std::string const OsdStatusMonitor::GetMessageHtml() {return !message.empty() ? "<div class=\"osdMessage\">" + EncodeHtml(message) + "</div>" : "";}
std::string const OsdStatusMonitor::GetRedHtml() {return !red.empty() ? "<div class=\"osdButtonRed\">" + EncodeHtml(red) + "</div>" : "";}
std::string const OsdStatusMonitor::GetGreenHtml() {return !green.empty() ? "<div class=\"osdButtonGreen\">" + EncodeHtml(green) + "</div>" : "";}
std::string const OsdStatusMonitor::GetYellowHtml() {return !yellow.empty() ? "<div class=\"osdButtonYellow\">" + EncodeHtml(yellow) + "</div>" : "";}
std::string const OsdStatusMonitor::GetBlueHtml() {return !blue.empty() ? "<div class=\"osdButtonBlue\">" + EncodeHtml(blue) + "</div>" : "";}
std::string const OsdStatusMonitor::GetTextHtml() {return !text.empty() ? "<div class=\"osdText\">" + EncodeHtml(text) + "</div>" : "";}
std::string const OsdStatusMonitor::GetButtonsHtml() {
std::string buffer= GetRedHtml() + GetGreenHtml() + GetYellowHtml() + GetBlueHtml();
return !buffer.empty() ? "<div class=\"osdButtons\">" + buffer + "</div>" : "";
}
std::string const OsdStatusMonitor::GetItemsHtml(void){
std::string buffer= "";
for (cLiveOsdItem *item = items.First(); item; item = items.Next(item)) {
buffer += "<div class=\"osdItem";
if (item->isSelected()) buffer += " selected";
buffer += "\">";
/*
if (tabs[0] > 0) {
buffer += EncodeHtml(item->Text(), true);
const char *tab = strchr(item->Text().c_str(), '\t');
if (tab && *(tab + 1) ) {
int charsT1 = tab - item->Text().c_str();
buffer.append(std::max(1, (int)tabs[0] - charsT1), ' ');
buffer += EncodeHtml(tab + 1);
}
} else {
*/
buffer += EncodeHtml(item->Text());
// }
buffer += "</div>";
}
if (buffer.empty() ) return "";
// if (tabs[0] > 0) return std::string("<div class=\"osdItems\"><table>") + buffer + "</table></div>";
return std::string("<div class=\"osdItems\">") + buffer + "</div>";
}
std::string const OsdStatusMonitor::GetHtml(){
std::stringstream ss;
ss << lastUpdate;
return "<div class=\"osd\" data-time=\"" + ss.str() + "\">" + GetTitleHtml() + GetItemsHtml() + GetTextHtml() + GetMessageHtml() + GetButtonsHtml() + "</div>";
}
std::string const OsdStatusMonitor::EncodeHtml(const std::string& html, bool stopAtTab) {
std::stringstream ss;
std::string::const_iterator i;
for (i = html.begin(); i != html.end(); ++i) {
if (*i == '<')
ss << "<";
else if (*i == '>')
ss << ">";
else if (*i == '&')
ss << "&";
else if (*i == '"')
ss << """;
else if (*i == '\t' && stopAtTab)
break;
else
ss << static_cast<char>(*i); // Copy untranslated
}
return ss.str();
}
OsdStatusMonitor& LiveOsdStatusMonitor()
{
static OsdStatusMonitor instance;
return instance;
}
} // namespace vdrlive