diff options
| -rw-r--r-- | flake.nix | 2 | ||||
| -rw-r--r-- | meson_options.txt | 2 | ||||
| -rw-r--r-- | src/compfy.modulemap | 6 | ||||
| -rw-r--r-- | src/meson.build | 7 | ||||
| -rw-r--r-- | src/options.c | 18 | ||||
| -rw-r--r-- | src/update.c | 89 |
6 files changed, 120 insertions, 4 deletions
@@ -14,7 +14,7 @@ system: let pkgs = nixpkgs.legacyPackages.${system}; nativeBuildInputs = with pkgs; []; - buildInputs = with pkgs; [ pcre2 ]; + buildInputs = with pkgs; [ pcre2 json_c curl ]; in { devShells.default = pkgs.mkShell {inherit nativeBuildInputs buildInputs;}; diff --git a/meson_options.txt b/meson_options.txt index 70c15d0..e625bc0 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -11,6 +11,8 @@ option('xrescheck', type: 'boolean', value: false, description: 'Enable X resour option('with_docs', type: 'boolean', value: true, description: 'Build documentation and man pages') +option('update_checks', type: 'boolean', value: false, description: 'Gives access to --check-for-updates option') + option('modularize', type: 'boolean', value: false, description: 'Build with clang\'s module system') option('unittest', type: 'boolean', value: false, description: 'Enable unittests in the code') diff --git a/src/compfy.modulemap b/src/compfy.modulemap index 621ad81..78d7d94 100644 --- a/src/compfy.modulemap +++ b/src/compfy.modulemap @@ -23,6 +23,12 @@ module region { module compfy { header "compfy.h" } +module curl { + header "curl.h" +} +module json-c { + header "json.h" +} module types { header "types.h" } diff --git a/src/meson.build b/src/meson.build index 48b212f..60d1893 100644 --- a/src/meson.build +++ b/src/meson.build @@ -9,7 +9,7 @@ base_deps = [ srcs = [ files('compfy.c', 'win.c', 'c2.c', 'x.c', 'config.c', 'vsync.c', 'utils.c', 'diagnostic.c', 'string_utils.c', 'render.c', 'kernel.c', 'log.c', - 'options.c', 'event.c', 'cache.c', 'atom.c', 'file_watch.c') ] + 'options.c', 'event.c', 'cache.c', 'atom.c', 'file_watch.c', 'update.c') ] compfy_inc = include_directories('.') cflags = [] @@ -37,6 +37,11 @@ endif deps = [] +if get_option('update_checks') + cflags += ['-DCONFIG_UPDATES'] + deps += [dependency('json-c', required: true), dependency('libcurl', required: true)] +endif + if get_option('config_file') deps += [dependency('libconfig', version: '>=1.4', required: true)] diff --git a/src/options.c b/src/options.c index 098e25f..d270ee9 100644 --- a/src/options.c +++ b/src/options.c @@ -191,6 +191,9 @@ static const struct compfy_option compfy_options[] = { {"active-opacity-exclude", required_argument, 817, NULL, "Exclude windows from being affected by active opacity"}, {"inactive-exclude", required_argument, 818, NULL, "Exclude windows from being affected by inactive opacity"}, {"blur-whitelist", no_argument, 819, NULL, "Toggles Blur Whitelisting"}, +#ifdef CONFIG_UPDATES + {"check-for-updates", no_argument,'cfu', NULL, "Check for updates"}, +#endif }; // clang-format on @@ -336,8 +339,14 @@ bool get_early_config(int argc, char *const *argv, char **config_file, bool *all } else if (o == 'h') { usage(argv[0], 0); return true; - - } else if (o == 'b') { + } +#ifdef CONFIG_UPDATES + else if (o == 'cfu') { + check_for_updates(); + return true; + } +#endif + else if (o == 'b') { *fork = true; } else if (o == 314) { *all_xerrors = true; @@ -412,6 +421,11 @@ bool get_cfg(options_t *opt, int argc, char *const *argv, bool shadow_enable, // so assert(false) here assert(false); break; +#ifdef CONFIG_UPDATES + case 'cfu': + assert(false); + break; +#endif case 'b': case 314: case 320: diff --git a/src/update.c b/src/update.c new file mode 100644 index 0000000..e05f465 --- /dev/null +++ b/src/update.c @@ -0,0 +1,89 @@ +#ifdef CONFIG_UPDATES +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <curl/curl.h> +#include <json-c/json.h> + +// Callback function to handle the response data +size_t write_callback_updater(void* contents, size_t size, size_t nmemb, void* user_data) { + size_t real_size = size * nmemb; + ((char*)user_data)[real_size] = '\0'; // Null-terminate the string + strcat(user_data, (const char*)contents); + return real_size; +} + +// Function to fetch the latest release tag from a GitHub repository +char* get_latest_release_tag(const char* owner, const char* repo) { + CURL* curl; + CURLcode res; + + // Initialize libcurl + curl_global_init(CURL_GLOBAL_DEFAULT); + curl = curl_easy_init(); + + if (curl) { + // Set the GitHub API URL with a User-Agent header + char url[100]; + snprintf(url, sizeof(url), "https://api.github.com/repos/%s/%s/releases/latest", owner, repo); + curl_easy_setopt(curl, CURLOPT_URL, url); + + // Add a User-Agent header to the request + curl_easy_setopt(curl, CURLOPT_USERAGENT, "Compfy_Fetch_Version"); + + // Set up a buffer to store the response data + char response_buffer[4096] = {0}; + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_updater); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, response_buffer); + + // Perform the HTTP request + res = curl_easy_perform(curl); + + // Check for errors + if (res != CURLE_OK) { + fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); + } else { + // Print the response for debugging + // printf("Response: %s\n", response_buffer); + + // Parse the JSON response + struct json_object* json = json_tokener_parse(response_buffer); + if (json == NULL) { + fprintf(stderr, "Error parsing JSON response\n"); + } else { + struct json_object* tag_name; + json_object_object_get_ex(json, "tag_name", &tag_name); + + // Clean up libcurl + curl_easy_cleanup(curl); + + // Return the latest release tag + char* tag = strdup(json_object_get_string(tag_name)); + // printf("Tag: %s\n", tag); + return tag; + } + } + } + + // Clean up libcurl in case of failure + curl_easy_cleanup(curl); + return NULL; +} + +int check_for_updates() { + const char* owner = "allusive-dev"; + const char* repo = "compfy"; + + char* latest_tag = get_latest_release_tag(owner, repo); + + if (latest_tag) { + printf("Your current version is: %s\n", COMPFY_VERSION); + printf("The latest version is: %s\n", latest_tag); + free(latest_tag); + } else { + printf("Unable to fetch latest release.\n"); + } + + return 0; +} +#endif
\ No newline at end of file |