Skip to content

Commit 4968e4c

Browse files
committed
avoid crash if DLNA next folder is empty
1 parent aa8fa18 commit 4968e4c

File tree

1 file changed

+72
-38
lines changed

1 file changed

+72
-38
lines changed

src/common.h

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,7 +2643,8 @@ class uniList {
26432643
char* m_name = NULL;
26442644
char* m_buff = NULL;
26452645
char* m_txt[10] = {0};
2646-
char* m_ext[10] = {0};
2646+
char* m_ext1[10] = {0};
2647+
char* m_ext2[10] = {0};
26472648
bool m_enabled = false;
26482649

26492650
public:
@@ -2655,7 +2656,7 @@ class uniList {
26552656
~uniList(){
26562657
if(m_name){free(m_name); m_name = NULL;}
26572658
if(m_buff){free(m_buff); m_buff = NULL;}
2658-
for(int i = 0; i < 10; i++){if(m_txt[i]) free(m_txt[i]); if(m_ext[i]) free(m_ext[i]);}
2659+
for(int i = 0; i < 10; i++){if(m_txt[i]) free(m_txt[i]); if(m_ext1[i]) free(m_ext1[i]); if(m_ext2[i]) free(m_ext2[i]);}
26592660
}
26602661
void begin(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t fontSize){
26612662
m_x = x; // x pos
@@ -2669,26 +2670,39 @@ class uniList {
26692670
}
26702671
void setMode(uint8_t mode){
26712672
if(mode == RADIO) {m_mode = RADIO; m_insert = 10;}
2672-
if(mode == PLAYER) m_mode = PLAYER;
2673-
if(mode == DLNA) m_mode = DLNA;
2673+
if(mode == PLAYER) m_mode = PLAYER;
2674+
if(mode == DLNA) {m_mode = DLNA; m_insert = 10;}
26742675
}
26752676
void clearList(){
26762677
tft.fillRect(m_x, m_y, m_w, m_h, m_bgColor);
26772678
for(int i = 0; i < 10; i++){
2678-
free(m_txt[i]); m_txt[i] = strdup("");
2679-
free(m_ext[i]); m_ext[i] = strdup("");
2679+
free(m_txt[i]); m_txt[i] = NULL;
2680+
free(m_ext1[i]); m_ext1[i] = NULL;
2681+
free(m_ext2[i]); m_ext2[i] = NULL;
26802682
m_nr[i] = -1;
26812683
}
26822684
}
2683-
void drawLine(uint8_t pos, const char* txt, const char* ext, const char* color = ANSI_ESC_WHITE, int16_t nr = -1){
2685+
void drawLine(uint8_t pos, const char* txt, const char* ext1 = NULL, const char* ext2 = NULL, const char* color = ANSI_ESC_WHITE, int16_t nr = -1){
26842686
if(pos > 9) return;
2687+
if(!txt) return;
26852688
tft.setFont(m_fontSize);
26862689
if(m_mode == RADIO){
26872690
sprintf(m_buff, ANSI_ESC_YELLOW "%03d %s%s" , nr, color, txt);
2688-
if(txt){free(m_txt[pos]); m_txt[pos] = strdup(txt);}
2689-
if(ext){free(m_ext[pos]); m_ext[pos] = strdup(ext);}
2691+
if(txt ){free(m_txt[pos]); m_txt[pos] = strdup(txt);}
2692+
if(ext1){free(m_ext1[pos]); m_ext1[pos] = strdup(ext1);}
2693+
if(ext2){free(m_ext2[pos]); m_ext2[pos] = strdup(ext2);}
26902694
m_nr[pos] = nr;
26912695
}
2696+
if(m_mode == DLNA){
2697+
if(!txt) {log_e("txt is NULL"); return;}
2698+
if(!ext1) sprintf(m_buff, "%s%s", color, txt);
2699+
else if(ext1[0] == '\0') sprintf(m_buff, "%s%s", color, txt);
2700+
else sprintf(m_buff, "%s%s " ANSI_ESC_CYAN "(%s)" , color, txt, ext1);
2701+
m_insert = pos? 20 : 10;
2702+
if(txt) {free(m_txt[pos]); m_txt[pos] = strdup(txt); m_nr[pos] = 1;}
2703+
if(ext1){free(m_ext1[pos]); m_ext1[pos] = strdup(ext1);}
2704+
if(ext2){free(m_ext2[pos]); m_ext2[pos] = strdup(ext2);}
2705+
}
26922706
tft.writeText(m_buff, m_insert, m_y + pos *m_lineHight, m_w - 10, m_lineHight, TFT_ALIGN_LEFT, TFT_ALIGN_CENTER, true, true);
26932707
}
26942708
void colourLine(uint8_t pos, const char* color = ANSI_ESC_WHITE) {
@@ -2705,6 +2719,15 @@ class uniList {
27052719
int16_t getNumberByPos(uint8_t pos){
27062720
return m_nr[pos];
27072721
}
2722+
void drawTriangeUp(){
2723+
auto triangleUp = [&](int16_t x, int16_t y, uint8_t s) { tft.fillTriangle(x + s, y + 0, x + 0, y + 2 * s, x + 2 * s, y + 2 * s, TFT_RED); };
2724+
triangleUp(0, m_y + (1 * m_lineHight), m_lineHight / 3.5);
2725+
}
2726+
void drawTriangeDown(){
2727+
auto triangleDown = [&](int16_t x, int16_t y, uint8_t s) { tft.fillTriangle(x + 0, y + 0, x + 2 * s, y + 0, x + s, y + 2 * s, TFT_RED); };
2728+
triangleDown(0, m_y + (9 * m_lineHight), m_lineHight / 3.5);
2729+
}
2730+
27082731
};
27092732
extern uniList myList;
27102733
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
@@ -2746,6 +2769,8 @@ class dlnaList : public RegisterTable {
27462769
bool m_enabled = false;
27472770
bool m_clicked = false;
27482771
bool m_state = false;
2772+
bool m_isAudio = false;
2773+
bool m_isURL = false;
27492774
char* m_name = NULL;
27502775
char* m_pathBuff = NULL;
27512776
char* m_chptr = NULL;
@@ -2853,18 +2878,15 @@ class dlnaList : public RegisterTable {
28532878
private:
28542879
void dlnaItemsList(){
28552880
uint8_t pos = 0;
2856-
2857-
auto triangleUp = [&](int16_t x, int16_t y, uint8_t s) { tft.fillTriangle(x + s, y + 0, x + 0, y + 2 * s, x + 2 * s, y + 2 * s, TFT_RED); };
2858-
auto triangleDown = [&](int16_t x, int16_t y, uint8_t s) { tft.fillTriangle(x + 0, y + 0, x + 2 * s, y + 0, x + s, y + 2 * s, TFT_RED); };
2859-
2860-
tft.fillRect(m_x, m_y, m_w, m_h, m_bgColor);
2861-
tft.setFont(m_fontSize);
2862-
tft.setTextColor(TFT_ORANGE);
2863-
tft.writeText(m_dlnaHistory[*m_dlnaLevel].name, 10, m_y, m_w - 10, m_lineHight, TFT_ALIGN_LEFT, TFT_ALIGN_CENTER, true, true);
2881+
myList.setMode(DLNA);
2882+
myList.clearList();
2883+
myList.drawLine(0, m_dlnaHistory[*m_dlnaLevel].name, NULL, ANSI_ESC_ORANGE);
28642884
tft.setTextColor(TFT_WHITE);
28652885
for(pos = 1; pos < 10; pos++) {
2866-
if(pos == 1 && m_viewPoint > 0) { triangleUp(0, m_y + (pos * m_lineHight), m_lineHight / 3.5); }
2867-
if(pos == 9 && m_viewPoint + 9 < m_dlnaMaxItems - 1) { triangleDown(0, m_y + (pos * m_lineHight), m_lineHight / 3.5); }
2886+
if(pos == 1 && m_viewPoint > 0) { myList.drawTriangeUp();}
2887+
if(pos == 9 && m_viewPoint + 9 < m_dlnaMaxItems - 1) { myList.drawTriangeDown();}
2888+
if(*m_dlnaLevel == 0 && pos > m_dlnaServer.size) {/* log_e("pos too high %i", pos);*/ break;} // guard
2889+
if(*m_dlnaLevel > 0 && pos > m_srvContent.size) {/* log_e("pos too high %i", pos);*/ break;} // guard
28682890
drawItem(pos);
28692891
}
28702892
sprintf(m_buff, "%i-%i/%i", m_viewPoint + 1, m_viewPoint + (pos - 1), m_dlnaMaxItems); // shows the current items pos e.g. "30-39/210"
@@ -2878,32 +2900,40 @@ class dlnaList : public RegisterTable {
28782900
if(pos < 0 || pos > 9) {log_e("pos oor %i", pos); return;} // guard
28792901
if(*m_dlnaLevel == 0 && pos > m_dlnaServer.size) {/* log_e("pos too high %i", pos);*/ return;} // guard
28802902
if(*m_dlnaLevel > 0 && pos > m_srvContent.size) {/* log_e("pos too high %i", pos);*/ return;} // guard
2903+
char extension[15] = {0};
2904+
char dummy[] = "";
28812905
bool isAudio = false;
28822906
bool isURL = false;
2883-
const char* item = NULL; char* duration = NULL; const char* itemURL = NULL; (void)itemURL;
2884-
char color[20];
2885-
uint16_t posX = m_x + 20, posY = m_y + pos * m_lineHight, width = m_w - 20, height = m_lineHight;
2907+
const char *item = dummy, *duration = dummy, *itemURL = dummy, *color = ANSI_ESC_WHITE; (void)itemURL;
28862908
int32_t itemSize = 0;
28872909
int16_t childCount = 0;
2888-
tft.setFont(m_fontSize);
28892910
if(pos == 0){
2890-
if(pos + m_viewPoint == m_currItemNr[*m_dlnaLevel] + 1) strcpy(color, ANSI_ESC_MAGENTA); else strcpy(color, ANSI_ESC_ORANGE);
2891-
if(selectedLine) strcpy(color, ANSI_ESC_CYAN);
2892-
sprintf(m_buff, "%s%s", color, m_dlnaHistory[*m_dlnaLevel].name);
2893-
tft.writeText(m_buff, 10, posY , m_w - 10, m_lineHight, TFT_ALIGN_LEFT, TFT_ALIGN_CENTER, true, true);
2911+
if(pos + m_viewPoint == m_currItemNr[*m_dlnaLevel] + 1) color = ANSI_ESC_MAGENTA; else color = ANSI_ESC_ORANGE;
2912+
if(selectedLine) color = ANSI_ESC_CYAN;
2913+
myList.drawLine(pos, m_dlnaHistory[*m_dlnaLevel].name, "", "", color, 1);
28942914
return;
28952915
}
2896-
if(*m_dlnaLevel == 0) {item = m_dlnaServer.friendlyName[pos - 1];}
2897-
else { item = m_srvContent.title[pos - 1]; itemSize = m_srvContent.itemSize[pos - 1]; childCount = m_srvContent.childCount[pos -1]; duration = m_srvContent.duration[pos - 1];
2898-
itemURL = m_srvContent.itemURL[pos -1]; isAudio = m_srvContent.isAudio[pos - 1]; if(startsWith(m_srvContent.itemURL[pos -1], "http")) isURL = true;}
2916+
if(*m_dlnaLevel == 0) {
2917+
if(m_dlnaServer.friendlyName[pos - 1]) item = m_dlnaServer.friendlyName[pos - 1];
2918+
}
2919+
else {
2920+
if(m_srvContent.title[pos - 1]) item = m_srvContent.title[pos - 1];
2921+
itemSize = m_srvContent.itemSize[pos - 1];
2922+
childCount = m_srvContent.childCount[pos -1];
2923+
if(m_srvContent.duration[pos - 1]) duration = m_srvContent.duration[pos - 1];
2924+
isAudio = m_srvContent.isAudio[pos - 1];
2925+
if(startsWith(m_srvContent.itemURL[pos -1], "http")) {isURL = true; itemURL = m_srvContent.itemURL[pos -1];}
2926+
}
28992927

2900-
if((pos - 1) + m_viewPoint == m_currItemNr[*m_dlnaLevel]) {strcpy(color, ANSI_ESC_MAGENTA);} else if(isURL && isAudio) {strcpy(color, ANSI_ESC_YELLOW);} else {strcpy(color, ANSI_ESC_WHITE);}
2901-
if(selectedLine) strcpy(color, ANSI_ESC_CYAN);
2902-
sprintf(m_buff, "%s%s", color, item);
2903-
if(childCount) sprintf(m_buff + strlen(m_buff), ANSI_ESC_CYAN " (%i)", childCount);
2904-
if(isURL && isAudio) {if(strcmp(duration, "0:00:00") == 0) sprintf(m_buff + strlen(m_buff), ANSI_ESC_CYAN " (%li)", itemSize); else sprintf(m_buff + strlen(m_buff), ANSI_ESC_CYAN " (%s)", duration);}
2905-
if(isURL && !isAudio) sprintf(m_buff + strlen(m_buff), ANSI_ESC_CYAN " (%li)", itemSize);
2906-
tft.writeText(m_buff, posX, posY, width, height, TFT_ALIGN_LEFT, TFT_ALIGN_CENTER, true, true);
2928+
if((pos - 1) + m_viewPoint == m_currItemNr[*m_dlnaLevel]){ color = ANSI_ESC_MAGENTA;}
2929+
else if(isURL && isAudio) { color = ANSI_ESC_YELLOW;}
2930+
else { color = ANSI_ESC_WHITE;}
2931+
if(selectedLine) { color = ANSI_ESC_CYAN;}
2932+
2933+
if(childCount) {sprintf(extension, "%i", childCount);}
2934+
if(itemSize) {sprintf(extension, "%li", itemSize);}
2935+
if(duration[0] != '?'){if(strcmp(duration, "0:00:00") != 0) sprintf(extension, "%s", duration);}
2936+
myList.drawLine(pos, item, extension, itemURL, color, 1);
29072937
}
29082938

29092939
void hasReleased(uint16_t x, uint16_t y){
@@ -3134,6 +3164,10 @@ class dlnaList : public RegisterTable {
31343164
m_dlnaHistory[*m_dlnaLevel].maxItems = m_dlnaMaxItems;
31353165
// log_e("m_dlnaMaxItems %i, level %i", m_dlnaMaxItems, (*m_dlnaLevel)); // level 2, 3, 4...
31363166
dlnaItemsList();
3167+
if(!m_dlnaMaxItems) { // folder is empty
3168+
m_currItemNr[*m_dlnaLevel]--;
3169+
drawItem(m_currItemNr[*m_dlnaLevel] + 0 - m_viewPoint + 1); // make magenta
3170+
}
31373171
return NULL;
31383172
}
31393173
if(startsWith(m_srvContent.itemURL[m_currItemNr[*m_dlnaLevel] - m_viewPoint], "http") != 0){ // ---------------------------------- choose file
@@ -3680,7 +3714,7 @@ class stationsList : public RegisterTable {
36803714

36813715
m_staNameToDraw = staMgnt.getStationName(pos + m_firstStationsLineNr + 1); // the station name
36823716
m_staNrToDraw = pos + m_firstStationsLineNr + 1; // the station number
3683-
myList.drawLine(pos, m_staNameToDraw, "", m_colorToDraw, m_staNrToDraw);
3717+
myList.drawLine(pos, m_staNameToDraw, NULL, NULL, m_colorToDraw, m_staNrToDraw);
36843718
}
36853719
xSemaphoreGive(mutex_display);
36863720
}

0 commit comments

Comments
 (0)