Lot of fixes...

This commit is contained in:
Johannes Findeisen 2026-07-16 00:03:03 +02:00
commit 22c991cd26
3 changed files with 1749 additions and 621 deletions

View file

@ -2,29 +2,15 @@
* SpaceAPI based tray applet for BSD* / Linux / UNIX etc. using GTK2
*
* Author: hanez <you@hanez.org>
* Website: https://hanez.org/project/spacetray/
* Website: https://git.xw3.org/hanez/spacetray
*
* License (MIT License):
*
* TODO:
* - Add door open/close date to notification and status
* - Fix: not show notification twice at start when status "open"
* Licensed under the terms of the MIT License.
*/
#include <curl/curl.h>
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#define GDK_PIXBUF_ENABLE_BACKWARDS_COMPATIBILITY
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixdata.h>
#include <json.h>
#ifdef G_GNUC_BEGIN_IGNORE_DEPRECATIONS
#define IGNORE_DEPRECATIONS_START G_GNUC_BEGIN_IGNORE_DEPRECATIONS
#define IGNORE_DEPRECATIONS_END G_GNUC_END_IGNORE_DEPRECATIONS
#else
#define IGNORE_DEPRECATIONS_START
#define IGNORE_DEPRECATIONS_END
#endif
#if LIBNOTIFY
#include <libnotify/notify.h>
#endif
@ -35,23 +21,45 @@
#include <pthread.h>
#include <unistd.h>
#include "themes/lock.h"
#include "themes/lock_new.h"
struct string {
char *ptr;
size_t len;
};
static char *agent = "Spacetray/0.42";
static char *name = "SpaceAPI widget for UNIX";
static char *statusurl = NULL;
static char *agent = "Spacetray/0.42";
// The delay for polling the dooris service. Adjust this before compiling
static int delay = 1000; // ms. aka 15 minutes
static int delay = 300; // ms. aka 15 minutes
bool door_open = false;
bool old_door_open = false;
int door_last_change = 0;
char space_name[128] = "Unknown Space";
void on_quit_menu_item_activated(GtkMenuItem *item, gpointer data) {
gtk_main_quit();
}
void on_popup_menu(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer data) {
GtkWidget *menu, *quit_item;
menu = gtk_menu_new();
quit_item = gtk_menu_item_new_with_label("Quit");
g_signal_connect(G_OBJECT(quit_item), "activate",
G_CALLBACK(on_quit_menu_item_activated), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), quit_item);
gtk_widget_show_all(menu);
gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
gtk_status_icon_position_menu, status_icon,
button, activate_time);
}
void do_it();
void invoke_notification();
@ -146,6 +154,15 @@ void get_bouncer_data() {
leaf_json_object = json_object_object_get(door_json_object, "lastchange");
door_last_change = json_object_get_int(leaf_json_object);
leaf_json_object = json_object_object_get(response_json_object, "space");
if (leaf_json_object != NULL) {
const char *s_name = json_object_get_string(leaf_json_object);
if (s_name != NULL) {
strncpy(space_name, s_name, sizeof(space_name) - 1);
space_name[sizeof(space_name) - 1] = '\0';
}
}
// free response object
json_object_put(response_json_object);
@ -159,8 +176,15 @@ void get_bouncer_data() {
}
}
void invoke_notification() {
char door[128];
GdkPixbuf *icon_open = NULL;
GdkPixbuf *icon_closed = NULL;
static GdkPixbuf *pixbuf_new_from_data(const guint8 *data) {
return gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, 8, 52, 48, 208, NULL, NULL);
}
void update_tooltip() {
char door[256];
char date_str[64];
time_t last_change_time = (time_t)door_last_change;
struct tm *tm_info = localtime(&last_change_time);
@ -168,33 +192,37 @@ void invoke_notification() {
strftime(date_str, sizeof(date_str), "%Y-%m-%d %H:%M:%S", tm_info);
if (door_open == true) {
snprintf(door, sizeof(door), "Door open since %s", date_str);
snprintf(door, sizeof(door), "%s: Door open since %s", space_name, date_str);
} else {
snprintf(door, sizeof(door), "Door closed since %s", date_str);
snprintf(door, sizeof(door), "%s: Door closed since %s", space_name, date_str);
}
gtk_status_icon_set_tooltip(tray_icon, door);
}
void invoke_notification() {
char door[256];
char date_str[64];
time_t last_change_time = (time_t)door_last_change;
struct tm *tm_info = localtime(&last_change_time);
strftime(date_str, sizeof(date_str), "%Y-%m-%d %H:%M:%S", tm_info);
if (door_open == true) {
snprintf(door, sizeof(door), "%s: Door open since %s", space_name, date_str);
} else {
snprintf(door, sizeof(door), "%s: Door closed since %s", space_name, date_str);
}
#if LIBNOTIFY
NotifyNotification *n;
n = notify_notification_new(name, door, NULL);
//notify_notification_set_timeout(n, 10000);
if (door_open == true) {
IGNORE_DEPRECATIONS_START
notify_notification_set_icon_from_pixbuf(n, gdk_pixbuf_from_pixdata(
&icon_open_pixbuf,
true,
NULL));
IGNORE_DEPRECATIONS_END
notify_notification_set_icon_from_pixbuf(n, icon_open);
} else {
IGNORE_DEPRECATIONS_START
notify_notification_set_icon_from_pixbuf(n, gdk_pixbuf_from_pixdata(
&icon_closed_pixbuf,
true,
NULL));
IGNORE_DEPRECATIONS_END
notify_notification_set_icon_from_pixbuf(n, icon_closed);
}
notify_notification_show(n, NULL);
g_object_unref(G_OBJECT(n));
@ -204,20 +232,10 @@ void invoke_notification() {
void set_status() {
if (door_open == true) {
printf("Set to status: open\n");
IGNORE_DEPRECATIONS_START
gtk_status_icon_set_from_pixbuf(tray_icon,
gdk_pixbuf_from_pixdata(&icon_open_pixbuf,
true,
NULL));
IGNORE_DEPRECATIONS_END
gtk_status_icon_set_from_pixbuf(tray_icon, icon_open);
} else {
printf("Set to status: closed\n");
IGNORE_DEPRECATIONS_START
gtk_status_icon_set_from_pixbuf(tray_icon,
gdk_pixbuf_from_pixdata(&icon_closed_pixbuf,
true,
NULL));
IGNORE_DEPRECATIONS_END
gtk_status_icon_set_from_pixbuf(tray_icon, icon_closed);
}
}
@ -225,6 +243,7 @@ void do_it() {
static bool first_run = true;
get_bouncer_data();
set_status();
update_tooltip();
if (!first_run && door_open != old_door_open) {
invoke_notification();
@ -244,6 +263,9 @@ int main(int argc, char **argv) {
gtk_init(&argc, &argv);
icon_open = pixbuf_new_from_data(icon_open_data);
icon_closed = pixbuf_new_from_data(icon_closed_data);
#if LIBNOTIFY
notify_init(name);
#endif
@ -251,21 +273,13 @@ int main(int argc, char **argv) {
tray_icon = gtk_status_icon_new();
g_signal_connect(G_OBJECT(tray_icon), "activate",
G_CALLBACK(do_it), NULL);
g_signal_connect(G_OBJECT(tray_icon), "popup-menu",
G_CALLBACK(on_popup_menu), NULL);
gtk_status_icon_set_visible(tray_icon, true);
do_it();
// DEBUG STUFF
//door_open = true;
//set_status();
// END DEBUG STUFF
//invoke_notification();
//gtk_timeout_add(delay, (GtkFunction)do_it, (gpointer)NULL);
// guint xxx = g_timeout_add_seconds(1000, (GSourceFunc)do_it, (gpointer)NULL);
if (pthread_create(&thread_id, NULL, status_update_thread, NULL) != 0) {
fprintf(stderr, "Error creating thread\n");
return 1;
@ -275,4 +289,3 @@ int main(int argc, char **argv) {
return 0;
}