Skip to content

Commit

Permalink
Support for time strings
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Apr 5, 2024
1 parent 3f02b8f commit 9ce6792
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ Supported fonts (sourced from /usr/share/fonts/truetype/) can render Unicode cha
curl 192.168.1.17:9000/api/osd/0?text=Entrée
```

Empty strings are used to clear the regions:
```
curl 192.168.1.17:9000/api/osd/1?text=
```

Specifiers starting with a dollar sign are used to represent real-time statistics:
```
curl "192.168.1.17:9000/api/osd/1?text=$B C:$C M:$M"
curl "192.168.1.17:9000/api/osd/1?text=$B%20C:$C%20M:$M"
```
N.B. Spaces have to be escaped with %20 in URL syntaxes

Empty strings are used to clear the regions:
Showing the time and customizing the time format is done this way:
```
curl 192.168.1.17:9000/api/osd/2?text=
curl 192.168.1.17:9000/api/time?fmt=%25Y/%25m/%25d%20%25H:%25M:%25S
curl 192.168.1.17:9000/api/osd/2?text=$t&posy=120
```

UTC date and time can be set using Unix timestamps:
Expand Down
1 change: 1 addition & 0 deletions osd/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define DEF_POSX 16
#define DEF_POSY 16
#define DEF_SIZE 32.0f
#define DEF_TIMEFMT "%Y/%m/%d %H:%M:%S"
#define MAX_CONN 16
#define MAX_OSD 8
#define PORT "9000"
Expand Down
17 changes: 15 additions & 2 deletions osd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

void *io_map;
struct osd *osds;
char timefmt[32] = DEF_TIMEFMT;

static void fill(char* str)
{
Expand Down Expand Up @@ -78,6 +79,16 @@ static void fill(char* str)
strcat(out, m);
j += strlen(m);
}
else if (str[i + 1] == 't')
{
i++;
char s[64];
time_t t = time(NULL);
struct tm *tm = gmtime(&t);
strftime(s, 64, timefmt, tm);
strcat(out, s);
j += strlen(s);
}
else if (str[i + 1] == 'T')
{
i++;
Expand Down Expand Up @@ -192,7 +203,9 @@ void route()
for (char *item; (item = extract_pair(&input));) {
char *value = item;
char *key = extract_key(&value);
if (!strcmp(key, "ts")) {
if (!strcmp(key, "fmt"))
strncpy(timefmt, value, 32);
else if (!strcmp(key, "ts")) {
long result = strtol(value, &remain, 10);
if (remain == value) continue;
t.tv_sec = result;
Expand All @@ -208,7 +221,7 @@ void route()
"Connection: close\r\n" \
"\r\n" \
);
printf("{\"ts\":%d}", t.tv_sec);
printf("{\"fmt\":\"%s\",\"ts\":%d}", timefmt, t.tv_sec);
return;
}

Expand Down
1 change: 1 addition & 0 deletions osd/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C"
void serve_forever();
char *extract_key(char **pair);
char *extract_pair(char **input);
char *request_header(const char *name);

void callback();
void route();
Expand Down

0 comments on commit 9ce6792

Please sign in to comment.