style: remove comments from header files

This commit is contained in:
2025-12-28 04:01:46 +00:00
parent a3aea281b9
commit 3bf8fa09fa
38 changed files with 167 additions and 1481 deletions
+3 -54
View File
@@ -17,13 +17,10 @@
#include <ctype.h>
#include <pwd.h>
/* Global state */
static AppLauncherState launcher_state = {0};
/* Path to recent apps file */
static char recent_file_path[512] = {0};
/* Forward declarations */
static void scan_desktop_dir(const char *dir);
static int parse_desktop_file(const char *path, AppEntry *entry);
static void load_recent_apps(void);
@@ -32,13 +29,11 @@ static void add_to_recent(const char *desktop_id);
static char *strip_field_codes(const char *exec);
static int compare_apps(const void *a, const void *b);
/* ========== Initialization ========== */
void applauncher_init(void)
{
memset(&launcher_state, 0, sizeof(launcher_state));
/* Set up recent file path */
const char *home = getenv("HOME");
if (home == NULL) {
struct passwd *pw = getpwuid(getuid());
@@ -47,15 +42,12 @@ void applauncher_init(void)
snprintf(recent_file_path, sizeof(recent_file_path),
"%s/.local/share/dwn/recent_apps", home);
/* Ensure directory exists */
char dir_path[512];
snprintf(dir_path, sizeof(dir_path), "%s/.local/share/dwn", home);
mkdir(dir_path, 0755);
/* Load recent apps first */
load_recent_apps();
/* Scan application directories */
applauncher_refresh();
LOG_INFO("App launcher initialized with %d applications", launcher_state.app_count);
@@ -70,10 +62,8 @@ void applauncher_refresh(void)
{
launcher_state.app_count = 0;
/* Scan system applications */
scan_desktop_dir("/usr/share/applications");
/* Scan user applications (takes precedence) */
const char *home = getenv("HOME");
if (home) {
char user_apps[512];
@@ -81,14 +71,12 @@ void applauncher_refresh(void)
scan_desktop_dir(user_apps);
}
/* Sort apps alphabetically by name */
qsort(launcher_state.apps, launcher_state.app_count,
sizeof(AppEntry), compare_apps);
LOG_DEBUG("Refreshed app list: %d applications found", launcher_state.app_count);
}
/* ========== Directory Scanning ========== */
static void scan_desktop_dir(const char *dir)
{
@@ -103,17 +91,14 @@ static void scan_desktop_dir(const char *dir)
break;
}
/* Only process .desktop files */
size_t len = strlen(entry->d_name);
if (len < 9 || strcmp(entry->d_name + len - 8, ".desktop") != 0) {
continue;
}
/* Build full path */
char path[1024];
snprintf(path, sizeof(path), "%s/%s", dir, entry->d_name);
/* Check if already exists (user apps override system) */
bool exists = false;
for (int i = 0; i < launcher_state.app_count; i++) {
if (strcmp(launcher_state.apps[i].desktop_id, entry->d_name) == 0) {
@@ -125,7 +110,6 @@ static void scan_desktop_dir(const char *dir)
continue;
}
/* Parse the desktop file */
AppEntry app = {0};
if (parse_desktop_file(path, &app) == 0) {
snprintf(app.desktop_id, sizeof(app.desktop_id), "%s", entry->d_name);
@@ -136,7 +120,6 @@ static void scan_desktop_dir(const char *dir)
closedir(d);
}
/* ========== Desktop File Parsing ========== */
static int parse_desktop_file(const char *path, AppEntry *entry)
{
@@ -154,14 +137,12 @@ static int parse_desktop_file(const char *path, AppEntry *entry)
entry->terminal = false;
while (fgets(line, sizeof(line), f) != NULL) {
/* Remove trailing whitespace/newline */
size_t len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r' ||
line[len-1] == ' ' || line[len-1] == '\t')) {
line[--len] = '\0';
}
/* Check for section header */
if (line[0] == '[') {
in_desktop_entry = (strcmp(line, "[Desktop Entry]") == 0);
continue;
@@ -171,7 +152,6 @@ static int parse_desktop_file(const char *path, AppEntry *entry)
continue;
}
/* Parse key=value */
char *eq = strchr(line, '=');
if (eq == NULL) {
continue;
@@ -181,7 +161,6 @@ static int parse_desktop_file(const char *path, AppEntry *entry)
char *key = line;
char *value = eq + 1;
/* Skip localized entries (Name[en]=...) */
if (strchr(key, '[') != NULL) {
continue;
}
@@ -197,7 +176,6 @@ static int parse_desktop_file(const char *path, AppEntry *entry)
} else if (strcmp(key, "Terminal") == 0) {
entry->terminal = (strcasecmp(value, "true") == 0);
} else if (strcmp(key, "Type") == 0) {
/* Skip non-Application types */
if (strcmp(value, "Application") != 0) {
fclose(f);
return -1;
@@ -211,7 +189,6 @@ static int parse_desktop_file(const char *path, AppEntry *entry)
fclose(f);
/* Must have name and exec, and not be hidden */
if (!has_name || !has_exec || entry->hidden) {
return -1;
}
@@ -219,7 +196,6 @@ static int parse_desktop_file(const char *path, AppEntry *entry)
return 0;
}
/* ========== Recent Apps Management ========== */
static void load_recent_apps(void)
{
@@ -233,7 +209,6 @@ static void load_recent_apps(void)
char line[256];
while (fgets(line, sizeof(line), f) != NULL &&
launcher_state.recent_count < MAX_RECENT_APPS) {
/* Remove newline */
size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n') {
line[len-1] = '\0';
@@ -266,10 +241,8 @@ static void save_recent_apps(void)
static void add_to_recent(const char *desktop_id)
{
/* Remove if already in list */
for (int i = 0; i < launcher_state.recent_count; i++) {
if (strcmp(launcher_state.recent[i], desktop_id) == 0) {
/* Shift everything down */
for (int j = i; j > 0; j--) {
strcpy(launcher_state.recent[j], launcher_state.recent[j-1]);
}
@@ -279,7 +252,6 @@ static void add_to_recent(const char *desktop_id)
}
}
/* Add to front, shift others */
if (launcher_state.recent_count < MAX_RECENT_APPS) {
launcher_state.recent_count++;
}
@@ -292,7 +264,6 @@ static void add_to_recent(const char *desktop_id)
save_recent_apps();
}
/* ========== Helper Functions ========== */
static char *strip_field_codes(const char *exec)
{
@@ -301,13 +272,11 @@ static char *strip_field_codes(const char *exec)
for (size_t i = 0; exec[i] && j < sizeof(result) - 1; i++) {
if (exec[i] == '%' && exec[i+1]) {
/* Skip field codes: %f, %F, %u, %U, %d, %D, %n, %N, %i, %c, %k */
char code = exec[i+1];
if (code == 'f' || code == 'F' || code == 'u' || code == 'U' ||
code == 'd' || code == 'D' || code == 'n' || code == 'N' ||
code == 'i' || code == 'c' || code == 'k') {
i++; /* Skip the code character */
/* Also skip trailing space if any */
i++;
if (exec[i+1] == ' ') {
i++;
}
@@ -318,7 +287,6 @@ static char *strip_field_codes(const char *exec)
}
result[j] = '\0';
/* Trim trailing whitespace */
while (j > 0 && (result[j-1] == ' ' || result[j-1] == '\t')) {
result[--j] = '\0';
}
@@ -333,7 +301,6 @@ static int compare_apps(const void *a, const void *b)
return strcasecmp(app_a->name, app_b->name);
}
/* Find app by desktop_id */
static AppEntry *find_app_by_id(const char *desktop_id)
{
for (int i = 0; i < launcher_state.app_count; i++) {
@@ -344,7 +311,6 @@ static AppEntry *find_app_by_id(const char *desktop_id)
return NULL;
}
/* Find app by name */
static AppEntry *find_app_by_name(const char *name)
{
for (int i = 0; i < launcher_state.app_count; i++) {
@@ -355,7 +321,6 @@ static AppEntry *find_app_by_name(const char *name)
return NULL;
}
/* ========== Launcher Display ========== */
void applauncher_show(void)
{
@@ -364,10 +329,6 @@ void applauncher_show(void)
return;
}
/* Build the list for dmenu:
* - Recent apps first (if any)
* - Then all apps alphabetically
*/
size_t buf_size = launcher_state.app_count * 140;
char *choices = malloc(buf_size);
if (choices == NULL) {
@@ -376,7 +337,6 @@ void applauncher_show(void)
}
choices[0] = '\0';
/* Track which apps we've added (to avoid duplicates) */
bool *added = calloc(launcher_state.app_count, sizeof(bool));
if (added == NULL) {
free(choices);
@@ -385,11 +345,9 @@ void applauncher_show(void)
size_t pos = 0;
/* Add recent apps first */
for (int i = 0; i < launcher_state.recent_count; i++) {
AppEntry *app = find_app_by_id(launcher_state.recent[i]);
if (app != NULL) {
/* Mark as added */
for (int j = 0; j < launcher_state.app_count; j++) {
if (&launcher_state.apps[j] == app) {
added[j] = true;
@@ -405,7 +363,6 @@ void applauncher_show(void)
}
}
/* Add remaining apps */
for (int i = 0; i < launcher_state.app_count; i++) {
if (added[i]) {
continue;
@@ -419,12 +376,11 @@ void applauncher_show(void)
}
if (pos > 0) {
choices[pos-1] = '\0'; /* Remove trailing newline */
choices[pos-1] = '\0';
}
free(added);
/* Write choices to a temp file to avoid shell escaping issues */
char tmp_path[] = "/tmp/dwn_apps_XXXXXX";
int tmp_fd = mkstemp(tmp_path);
if (tmp_fd < 0) {
@@ -443,7 +399,6 @@ void applauncher_show(void)
return;
}
/* Run dmenu */
char cmd[1024];
snprintf(cmd, sizeof(cmd),
"cat '%s' | dmenu -i -l 10 -p 'Run:'; rm -f '%s'",
@@ -458,7 +413,6 @@ void applauncher_show(void)
char selected[256] = {0};
if (fgets(selected, sizeof(selected), p) != NULL) {
/* Remove trailing newline */
size_t len = strlen(selected);
if (len > 0 && selected[len-1] == '\n') {
selected[len-1] = '\0';
@@ -467,15 +421,13 @@ void applauncher_show(void)
pclose(p);
if (selected[0] == '\0') {
return; /* User cancelled */
return;
}
/* Find and launch the selected app */
AppEntry *app = find_app_by_name(selected);
if (app != NULL) {
applauncher_launch(app->desktop_id);
} else {
/* User typed a custom command */
spawn_async(selected);
}
}
@@ -488,10 +440,8 @@ void applauncher_launch(const char *desktop_id)
return;
}
/* Strip field codes from exec */
char *exec_cmd = strip_field_codes(app->exec);
/* Build command */
char cmd[1024];
if (app->terminal) {
const char *terminal = config_get_terminal();
@@ -503,6 +453,5 @@ void applauncher_launch(const char *desktop_id)
LOG_INFO("Launching: %s (%s)", app->name, cmd);
spawn_async(cmd);
/* Add to recent apps */
add_to_recent(desktop_id);
}