Display IP address in Menu core.

This commit is contained in:
sorgelig
2017-12-27 00:29:41 +08:00
parent 3935da9951
commit 165ef2190f
2 changed files with 66 additions and 22 deletions

66
menu.c
View File

@@ -28,6 +28,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <inttypes.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include "stdio.h"
#include "string.h"
#include "file_io.h"
@@ -472,6 +477,35 @@ static uint32_t menu_key_get(void)
return(c);
}
char* getIP()
{
struct ifaddrs *ifaddr, *ifa;
int family, s;
static char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1)
{
printf("getifaddrs: error\n");
return NULL;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL) continue;
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if ((strcmp(ifa->ifa_name, "eth0") == 0) && (ifa->ifa_addr->sa_family == AF_INET))
{
freeifaddrs(ifaddr);
return host;
}
}
freeifaddrs(ifaddr);
return NULL;
}
void HandleUI(void)
{
char *p;
@@ -3139,6 +3173,38 @@ void HandleUI(void)
default:
break;
}
if (is_menu_core())
{
static unsigned long rtc_timer = 0;
if (!rtc_timer || CheckTimer(rtc_timer))
{
rtc_timer = GetTimer(1000);
char str[64] = { 0 };
sprintf(str, " MiSTer ");
if (menustate == MENU_STORAGE)
{
char *ip = getIP();
if (ip) sprintf(str + strlen(str), "IP: %s", ip);
else sprintf(str + strlen(str), " No network");
}
else
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
if (tm.tm_year >= 117)
{
strftime(str + strlen(str), sizeof(str) - 1 - strlen(str), "%Y.%m.%d %H:%M:%S", &tm);
}
}
OsdWrite(16, "", 1, 0);
OsdWrite(17, str, 1, 0);
OsdWrite(18, "", 1, 0);
}
}
}
void ScrollLongName(void)

View File

@@ -1342,28 +1342,6 @@ void user_io_poll()
DISKLED_OFF;
}
*/
if (is_menu_core())
{
if (!rtc_timer || CheckTimer(rtc_timer))
{
rtc_timer = GetTimer(1000);
time_t t = time(NULL);
struct tm tm = *localtime(&t);
char str[64];
sprintf(str, " MiSTer ");
if (tm.tm_year >= 117)
{
strftime(str + strlen(str), sizeof(str) - 1 - strlen(str), "%Y.%m.%d %H:%M:%S", &tm);
}
OsdWrite(16, "", 1, 0);
OsdWrite(17, str, 1, 0);
OsdWrite(18, "", 1, 0);
}
}
}
if (core_type == CORE_TYPE_ARCHIE) archie_poll();