-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenLayout.cpp
189 lines (148 loc) · 3.96 KB
/
screenLayout.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
183
184
185
186
187
188
189
/*
* ------------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <ramo@goodvikings.com> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return - Ramo
* ------------------------------------------------------------------------------
*/
#include <string>
#include <string.h>
#include "screenLayout.h"
screenLayout::screenLayout()
{
lock = new pthread_mutex_t;
pthread_mutex_init(lock, NULL);
pthread_mutex_lock(lock);
screen = new char*[ROWS];
for (int i = 0; i < ROWS; i++)
{
screen[i] = new char[COLS];
}
clearScreen();
titlePos = 0;
artistPos = 0;
albumYearPos = 0;
pthread_mutex_unlock(lock);
}
screenLayout::~screenLayout()
{
pthread_mutex_lock(lock);
for (int i = 0; i < ROWS; i++)
{
delete [] screen[i];
}
delete [] screen;
pthread_mutex_unlock(lock);
pthread_mutex_destroy(lock);
delete lock;
}
/*
* Data should be a char[ROWS][COLS] sized array
*/
void screenLayout::setScreen(const char** data)
{
pthread_mutex_lock(lock);
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
screen[i][j] = data[i][j];
}
}
pthread_mutex_unlock(lock);
}
/*
* All of these must be null terminated strings. Year may be NULL.
*/
void screenLayout::setContents(const char* title, const char* artist, const char* album, const char* year, const char* elapsed, const char* duration)
{
pthread_mutex_lock(lock);
titleStr.assign(title);
artistStr.assign(artist);
albumYearStr.assign(album);
if (year != NULL && strlen(year))
{
albumYearStr += " (";
albumYearStr += year;
albumYearStr += ")";
}
unsigned long maxLen = titleStr.length() > artistStr.length() ? titleStr.length() : artistStr.length();
maxLen = maxLen > albumYearStr.length() ? maxLen : albumYearStr.length();
pad(&titleStr, maxLen);
pad(&artistStr, maxLen);
pad(&albumYearStr, maxLen);
/* setTime needs it's own locking, so we need to unlock before calling it */
pthread_mutex_lock(lock);
setTime(elapsed, duration);
/* and then unlock */
pthread_mutex_unlock(lock);
titlePos = 0;
artistPos = 0;
albumYearPos = 0;
pthread_mutex_unlock(lock);
}
void screenLayout::setTime(const char* elapsed, const char* duration)
{
pthread_mutex_lock(lock);
timeStr.assign(elapsed);
timeStr += "/";
timeStr += duration;
timeStr.insert(timeStr.begin(), COLS - timeStr.length(), ' ');
if (timeStr == " /")
{
timeStr = " ";
}
pthread_mutex_unlock(lock);
}
/*
* Scrolls the lines on the screen as necessary
*/
void screenLayout::scrollScreen()
{
pthread_mutex_lock(lock);
clearScreen();
for (unsigned int j = 0; j < COLS; j++)
{
screen[0][j] = titleStr[(j + titlePos) % titleStr.length()];
screen[1][j] = artistStr[(j + artistPos) % artistStr.length()];
screen[2][j] = albumYearStr[(j + albumYearPos) % albumYearStr.length()];
screen[3][j] = timeStr[j];
}
titlePos = titleStr.length() > COLS ? (titlePos + 1) % titleStr.length() : 0;
artistPos = artistStr.length() > COLS ? (artistPos + 1) % artistStr.length() : 0;
albumYearPos = albumYearStr.length() > COLS ? (albumYearPos + 1) % albumYearStr.length() : 0;
pthread_mutex_unlock(lock);
}
/*
* data should be allocated already as a ROWS by COLS sized array
*/
void screenLayout::getContents(char** data) const
{
pthread_mutex_lock(lock);
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
data[i][j] = screen[i][j];
}
}
pthread_mutex_unlock(lock);
}
void screenLayout::clearScreen()
{
pthread_mutex_lock(lock);
for (int i = 0; i < ROWS; i++)
{
memset(screen[i], 0, COLS);
}
pthread_mutex_unlock(lock);
}
void screenLayout::pad(std::string* str, unsigned long len)
{
unsigned long padLen = str->length() >= COLS ? (len - str->length()) + PADDING : COLS - str->length();
for (unsigned long i = 0; i < padLen; i++)
{
str->append(" ");
}
}