Skip to content

Commit

Permalink
add function to mark file as "hot" fixes hd-zero#96
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Jan 19, 2023
1 parent de763f8 commit f054f1b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 46 deletions.
13 changes: 12 additions & 1 deletion src/core/common.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "common.hh"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdatomic.h>
#include <sys/stat.h>

#include "defines.h"
#include "common.hh"
#include "self_test.h"
#include "ui/page_common.h"

Expand Down Expand Up @@ -93,6 +96,14 @@ bool file_exists(const char* filename) {
return access(filename, F_OK) == 0;
}

long file_get_size(const char* filename) {
struct stat st;
if (stat(filename, &st) != 0) {
return 0;
}
return st.st_size;
}

///////////////////////////////////////////////////////////////////////////////
// GPIO
void gpio_init()
Expand Down
1 change: 1 addition & 0 deletions src/core/common.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ uint8_t slow_key(left_dial_t key,uint8_t* state,uint8_t* cnt);

bool file_compare(char* f1,char* f2);
bool file_exists(const char* filename);
long file_get_size(const char* filename);

void gpio_init();
void open_gpio(int port_num);
Expand Down
116 changes: 71 additions & 45 deletions src/ui/page_playback.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,6 @@ static void show_pb_item(uint8_t pos, char *label) {
lv_obj_clear_flag(pb_ui[pos]._img, LV_OBJ_FLAG_HIDDEN);
}

static int insert_to_list(char *fname, char *label, int size) {
media_file_node_t *pnode = &media_db.list[media_db.count];
strcpy(pnode->filename, fname);
strcpy(pnode->label, label);
pnode->size = size;

LOGI("%d: %s-%dMB", media_db.count, fname, size);
media_db.count++;
return 1;
}

int get_videofile_cnt() {
return media_db.count;
}
Expand All @@ -142,63 +131,76 @@ static bool get_seleteced(int seq, char *fname) {
return true;
}

static bool cmp_ignore_case(const char *lhs, const char *rhs) {
if (rhs == NULL || lhs == NULL) {
return false;
}

if (strlen(lhs) != strlen(rhs)) {
return false;
}

for (size_t i = 0; i < strlen(lhs); i++) {
if (toupper(lhs[i]) != toupper(rhs[i])) {
return false;
}
}

return true;
}

static int walk_sdcard() {
DIR *FD;
struct dirent *in_file;
FILE *entry_file;
int i, j, k, len;
char label[64], fname[128];
long int res;

media_db.count = 0;
media_db.cur_sel = 0;

/* Scanning the in directory */
DIR *FD;
if (NULL == (FD = opendir(MEDIA_FILES_DIR)))
return 0;

struct dirent *in_file;
while ((in_file = readdir(FD))) {
if (!strcmp(in_file->d_name, "."))
continue;
if (!strcmp(in_file->d_name, ".."))
continue;

// .ts or .mp4 only
len = strlen(in_file->d_name);
for (i = len - 1; i > 0; i--) {
if (in_file->d_name[i] == '.')
break;
}
if (i == 0)
const char *dot = strrchr(in_file->d_name, '.');
if(dot == NULL) {
// '.' not found
continue;
}

k = i; // save the '.' position

j = 0; // .TS and .MP4 only
while (i < len)
label[j++] = toupper(in_file->d_name[i++]);
label[j] = 0;
if ((strcmp(label, ".TS") != 0) && (strcmp(label, ".MP4") != 0))
if (!cmp_ignore_case(dot, ".ts") && !cmp_ignore_case(dot, ".mp4")) {
continue;
}

strncpy(label, in_file->d_name, k);
label[k] = 0;

char fname[128];
sprintf(fname, "%s/%s", MEDIA_FILES_DIR, in_file->d_name);
entry_file = fopen(fname, "r");
if (!entry_file)
continue;
fseek(entry_file, 0L, SEEK_END);
res = ftell(entry_file);
fclose(entry_file);
res >>= 20; // in MB
if (res < 5) // skip small files

long size = file_get_size(fname);
size >>= 20; // in MB
if (size < 5) {
// skip small files
continue;
}

if (media_db.count < MAX_VIDEO_FILES)
insert_to_list(in_file->d_name, label, (int)res);
else
if (media_db.count >= MAX_VIDEO_FILES) {
LOGI("max video file cnt reached %d,skipped", MAX_VIDEO_FILES);
continue;
}

media_file_node_t *pnode = &media_db.list[media_db.count];
strcpy(pnode->filename, in_file->d_name);
strncpy(pnode->label, in_file->d_name, dot - in_file->d_name);
pnode->size = size;

LOGI("%d: %s-%dMB", media_db.count, pnode->filename, size);

media_db.count++;
}
closedir(FD);

Expand Down Expand Up @@ -240,6 +242,22 @@ static void update_page() {
}
}

static void mark_video_file(int seq) {
media_file_node_t *pnode = get_list(seq);
if (!pnode) {
return;
}

char cmd[128];
sprintf(cmd, "mv %s/%s %s/hot_%s", MEDIA_FILES_DIR, pnode->filename, MEDIA_FILES_DIR, pnode->filename);
system(cmd);
sprintf(cmd, "mv %s/%s.jpg %s/hot_%s.jpg", MEDIA_FILES_DIR, pnode->label, MEDIA_FILES_DIR, pnode->label);
system(cmd);

walk_sdcard();
update_page();
}

static void page_playback_exit() {
clear_videofile_cnt();
update_page();
Expand All @@ -251,7 +269,7 @@ static void page_playback_enter() {

if (ret == 0) {
// no files found, back out
page_playback_exit();
submenu_exit();
}
}

Expand Down Expand Up @@ -301,6 +319,10 @@ void pb_key(uint8_t key) {
case DIAL_KEY_PRESS: // long press
page_playback_exit();
break;

case RIGHT_KEY_CLICK:
mark_video_file(media_db.cur_sel);
break;
}
done = true;
}
Expand All @@ -313,11 +335,15 @@ static void page_playback_on_click(uint8_t key, int sel) {
pb_key(key);
}

static void page_playback_on_right_button(bool is_short) {
pb_key(is_short ? RIGHT_KEY_CLICK : RIGHT_KEY_PRESS);
}

page_pack_t pp_playback = {
.create = page_playback_create,
.enter = page_playback_enter,
.exit = page_playback_exit,
.on_roller = page_playback_on_roller,
.on_click = page_playback_on_click,
.on_right_button = NULL,
.on_right_button = page_playback_on_right_button,
};

0 comments on commit f054f1b

Please sign in to comment.