Skip to content

Commit ca31843

Browse files
Merge pull request #266 from CarterLi/master
Improve performance of PublicIP; add module Weather; fix compiling for Linux
2 parents b8656d5 + 8a52836 commit ca31843

File tree

15 files changed

+211
-28
lines changed

15 files changed

+211
-28
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ set(LIBFASTFETCH_SRC
235235
src/modules/vulkan.c
236236
src/modules/localip.c
237237
src/modules/publicip.c
238+
src/modules/weather.c
238239
src/modules/player.c
239240
src/modules/song.c
240241
src/modules/datetime.c
@@ -333,6 +334,8 @@ add_library(libfastfetch OBJECT
333334
${LIBFASTFETCH_SRC}
334335
)
335336

337+
target_compile_definitions(libfastfetch PUBLIC _GNU_SOURCE)
338+
336339
CHECK_INCLUDE_FILE("sys/sysinfo.h" HAVE_SYSINFO_H)
337340
if(HAVE_SYSINFO_H)
338341
# needs to be public, because changes fastfech.h ABI

completions/bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ __fastfetch_completion()
217217
"--set"
218218
"--set-keyless"
219219
"--player-name"
220+
"--public-ip-url"
220221
"--public-ip-timeout"
222+
"--weather-output-format"
223+
"--weather-timeout"
221224
"--os-key"
222225
"--os-format"
223226
"--os-error"
@@ -302,6 +305,9 @@ __fastfetch_completion()
302305
"--public-ip-key"
303306
"--public-ip-format"
304307
"--public-ip-error"
308+
"--weather-key"
309+
"--weather-format"
310+
"--weather-error"
305311
"--player-key"
306312
"--player-format"
307313
"--player-error"

presets/all

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--structure Title:Separator:OS:Host:Kernel:Uptime:Processes:Packages:Shell:Resolution:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Song:PublicIP:LocalIP:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Break:Colors
1+
--structure Title:Separator:OS:Host:Kernel:Uptime:Processes:Packages:Shell:Resolution:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Song:PublicIP:LocalIP:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Weather:Break:Colors

src/common/init.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static void defaultConfig(FFinstance* instance)
188188
initModuleArg(&instance->config.locale);
189189
initModuleArg(&instance->config.localIP);
190190
initModuleArg(&instance->config.publicIP);
191+
initModuleArg(&instance->config.weather);
191192
initModuleArg(&instance->config.player);
192193
initModuleArg(&instance->config.song);
193194
initModuleArg(&instance->config.dateTime);
@@ -243,6 +244,10 @@ static void defaultConfig(FFinstance* instance)
243244
ffStrbufInit(&instance->config.localIpNamePrefix);
244245

245246
instance->config.publicIpTimeout = 0;
247+
ffStrbufInit(&instance->config.publicIpUrl);
248+
249+
instance->config.weatherTimeout = 0;
250+
ffStrbufInitS(&instance->config.weatherOutputFormat, "%t+-+%C+(%l)");
246251

247252
ffStrbufInitA(&instance->config.osFile, 0);
248253

@@ -358,8 +363,6 @@ static void exitSignalHandler(int signal)
358363

359364
void ffStart(FFinstance* instance)
360365
{
361-
ffPrepareCPUUsage();
362-
363366
if(instance->config.multithreading)
364367
startDetectionThreads(instance);
365368

src/common/networking.c

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,38 @@
66
#include <sys/socket.h>
77
#include <netdb.h>
88

9-
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer)
9+
int ffNetworkingSendHttpRequest(const char* host, const char* path, const char* headers, uint32_t timeout)
1010
{
11-
struct addrinfo hints = {0};
12-
hints.ai_family = AF_INET;
13-
hints.ai_socktype = SOCK_STREAM;
11+
struct addrinfo hints = {
12+
.ai_family = AF_INET,
13+
.ai_socktype = SOCK_STREAM,
14+
};
1415

1516
struct addrinfo* addr;
1617

1718
if(getaddrinfo(host, "80", &hints, &addr) != 0)
18-
return;
19+
return -1;
1920

20-
int sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
21-
if(sock == -1)
21+
int sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
22+
if(sockfd == -1)
2223
{
2324
freeaddrinfo(addr);
24-
return;
25+
return -1;
2526
}
2627

2728
if(timeout > 0)
2829
{
2930
struct timeval timev;
3031
timev.tv_sec = 0;
3132
timev.tv_usec = (__typeof__(timev.tv_usec)) (timeout * 1000); //milliseconds to microseconds
32-
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev));
33+
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev));
3334
}
3435

35-
if(connect(sock, addr->ai_addr, addr->ai_addrlen) == -1)
36+
if(connect(sockfd, addr->ai_addr, addr->ai_addrlen) == -1)
3637
{
37-
close(sock);
38+
close(sockfd);
3839
freeaddrinfo(addr);
39-
return;
40+
return -1;
4041
}
4142

4243
freeaddrinfo(addr);
@@ -47,23 +48,36 @@ void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, F
4748
ffStrbufAppendS(&command, path);
4849
ffStrbufAppendS(&command, " HTTP/1.1\nHost: ");
4950
ffStrbufAppendS(&command, host);
50-
ffStrbufAppendS(&command, "\r\n\r\n");
51+
ffStrbufAppendS(&command, "\r\n");
52+
ffStrbufAppendS(&command, headers);
53+
ffStrbufAppendS(&command, "\r\n");
5154

52-
if(send(sock, command.chars, command.length, 0) == -1)
55+
if(send(sockfd, command.chars, command.length, 0) == -1)
5356
{
5457
ffStrbufDestroy(&command);
55-
close(sock);
56-
return;
58+
close(sockfd);
59+
return -1;
5760
}
61+
ffStrbufDestroy(&command);
62+
return sockfd;
63+
}
5864

59-
ssize_t received = recv(sock, buffer->chars + buffer->length, ffStrbufGetFree(buffer), 0);
65+
void ffNetworkingRecvHttpResponse(int sockfd, FFstrbuf* buffer)
66+
{
67+
ssize_t received = recv(sockfd, buffer->chars + buffer->length, ffStrbufGetFree(buffer), 0);
6068

6169
if(received > 0)
6270
{
6371
buffer->length += (uint32_t) received;
6472
buffer->chars[buffer->length] = '\0';
6573
}
6674

67-
ffStrbufDestroy(&command);
68-
close(sock);
75+
close(sockfd);
76+
}
77+
78+
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, const char* headers, FFstrbuf* buffer)
79+
{
80+
int sockfd = ffNetworkingSendHttpRequest(host, path, headers, timeout);
81+
if(sockfd > 0)
82+
ffNetworkingRecvHttpResponse(sockfd, buffer);
6983
}

src/common/networking.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "util/FFstrbuf.h"
77

8-
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer);
8+
int ffNetworkingSendHttpRequest(const char* host, const char* path, const char* headers, uint32_t timeout);
9+
void ffNetworkingRecvHttpResponse(int sock, FFstrbuf* buffer);
10+
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, const char* headers, FFstrbuf* buffer);
911

1012
#endif

src/data/config_user.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,30 @@
151151
# Default is "-"
152152
#--separator-string -
153153

154+
# Public IP URL option:
155+
# Sets the URL of public IP detection server to be used.
156+
# Only HTTP protocol is supported, and the value should not contain "http://" prefix.
157+
# Default is "ipinfo.io/ip".
158+
#--public-ip-url "ipinfo.io/ip"
159+
154160
# Public IP timeout option:
155161
# Sets the time to wait for the public ip server to respond.
156162
# Must be a positive integer.
157163
# Default is 0 (disabled).
158164
#--public-ip-timeout 0
159165

166+
# Weather output format option:
167+
# Sets the weather format to be used. It must be URI encoded.
168+
# See: https://github.com/chubin/wttr.in#one-line-output
169+
# Default is "%t+-+%C+(%l)".
170+
#--weather-output-format "%t+-+%C+(%l)"
171+
172+
# Weather timeout option:
173+
# Sets the time to wait for the weather server (wttr.in) to respond.
174+
# Must be a positive integer.
175+
# Default is 0 (disabled).
176+
#--weather-timeout 0
177+
160178
# OS file option
161179
# Sets the path to the file containing the operating system information.
162180
# Should be a valid path to an existing file.
@@ -213,6 +231,7 @@
213231
#--locale-key Locale
214232
#--local-ip-key Local IP ({1})
215233
#--public-ip-key Public IP
234+
#--weather-key Weather
216235
#--player-key Media Player
217236
#--song-key Song
218237
#--datetime-key Date Time
@@ -254,6 +273,7 @@
254273
#--locale-format
255274
#--local-ip-format
256275
#--public-ip-format
276+
#--weather-format
257277
#--player-format
258278
#--song-format
259279
#--datetime-format
@@ -295,6 +315,7 @@
295315
#--locale-error
296316
#--local-ip-error
297317
#--public-ip-error
318+
#--weather-error
298319
#--player-error
299320
#--song-error
300321
#--datetime-error

src/data/help.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ Module specific options:
103103
--localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false
104104
--localip-name-prefix <str>: Show ips with given name prefix only. Default is empty
105105
--public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
106+
--public-ip-url: The URL of public IP detection server to be used.
107+
--weather-timeout: Time in milliseconds to wait for the weather server to respond. Default is disabled (0)
108+
--weather-output-format: The output weather format to be used. It must be URI encoded.
106109
--player-name: The name of the player to use
107110
--gl <value>: Sets the opengl context creation library to use. Must be auto, egl, glx or osmesa. Default is auto
108111

src/detection/displayserver/linux/wayland.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#define _GNU_SOURCE //required for struct ucred
2-
31
#include "displayserver_linux.h"
42

53
#include <stdlib.h>

src/fastfetch.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,12 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
11511151
optionParseString(key, value, &instance->config.publicIP.outputFormat);
11521152
else if(strcasecmp(key, "--public-ip-error") == 0)
11531153
optionParseString(key, value, &instance->config.publicIP.errorFormat);
1154+
else if(strcasecmp(key, "--weather-key") == 0)
1155+
optionParseString(key, value, &instance->config.weather.key);
1156+
else if(strcasecmp(key, "--weather-format") == 0)
1157+
optionParseString(key, value, &instance->config.weather.outputFormat);
1158+
else if(strcasecmp(key, "--weather-error") == 0)
1159+
optionParseString(key, value, &instance->config.weather.errorFormat);
11541160
else if(strcasecmp(key, "--player-key") == 0)
11551161
optionParseString(key, value, &instance->config.player.key);
11561162
else if(strcasecmp(key, "--player-format") == 0)
@@ -1291,8 +1297,14 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
12911297
optionParseString(key, value, &instance->config.osFile);
12921298
else if(strcasecmp(key, "--player-name") == 0)
12931299
optionParseString(key, value, &instance->config.playerName);
1300+
else if(strcasecmp(key, "--public-ip-url") == 0)
1301+
optionParseString(key, value, &instance->config.publicIpUrl);
12941302
else if(strcasecmp(key, "--public-ip-timeout") == 0)
12951303
instance->config.publicIpTimeout = optionParseUInt32(key, value);
1304+
else if(strcasecmp(key, "--weather-output-format") == 0)
1305+
optionParseString(key, value, &instance->config.weatherOutputFormat);
1306+
else if(strcasecmp(key, "--weather-timeout") == 0)
1307+
instance->config.weatherTimeout = optionParseUInt32(key, value);
12961308
else if(strcasecmp(key, "--gl") == 0)
12971309
{
12981310
optionParseEnum(key, value, &instance->config.glType,
@@ -1428,6 +1440,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
14281440
ffPrintLocalIp(instance);
14291441
else if(strcasecmp(line, "publicip") == 0)
14301442
ffPrintPublicIp(instance);
1443+
else if(strcasecmp(line, "weather") == 0)
1444+
ffPrintWeather(instance);
14311445
else if(strcasecmp(line, "player") == 0)
14321446
ffPrintPlayer(instance);
14331447
else if(strcasecmp(line, "media") == 0 || strcasecmp(line, "song") == 0)
@@ -1471,6 +1485,19 @@ int main(int argc, const char** argv)
14711485
if(data.structure.length == 0)
14721486
ffStrbufAppendS(&data.structure, FASTFETCH_DATATEXT_STRUCTURE);
14731487

1488+
#define FF_CONTAINS_MODULE_NAME(moduleName)\
1489+
ffStrbufContainIgnCaseS(&data.structure, ":" #moduleName ":") ||\
1490+
ffStrbufStartsWithIgnCaseS(&data.structure, #moduleName ":") ||\
1491+
ffStrbufEndsWithIgnCaseS(&data.structure, ":" #moduleName)
1492+
1493+
if(FF_CONTAINS_MODULE_NAME(CPUUsage))
1494+
ffPrepareCPUUsage();
1495+
if(FF_CONTAINS_MODULE_NAME(PublicIp))
1496+
ffPreparePublicIp(&instance);
1497+
if(FF_CONTAINS_MODULE_NAME(Weather))
1498+
ffPrepareWeather(&instance);
1499+
#undef FF_CONTAINS_MODULE_NAME
1500+
14741501
ffStart(&instance);
14751502

14761503
//Parse the structure and call the modules

0 commit comments

Comments
 (0)