A lot of modernizing.

This commit is contained in:
Johannes Findeisen 2026-07-15 23:18:48 +02:00
commit 5f4ce7f655
8 changed files with 69 additions and 254 deletions

View file

@ -3,7 +3,6 @@
*
* Author: hanez <you@hanez.org>
* Website: https://hanez.org/project/spacetray/
* Contributors: atari, beh, eisbaer and Haeger
*
* License (MIT License):
*
@ -13,9 +12,19 @@
*/
#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
@ -23,6 +32,8 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include "themes/lock.h"
@ -32,7 +43,7 @@ struct string {
};
static char *name = "SpaceAPI widget for UNIX";
static char *statusurl = "https://www.hamburg.ccc.de/dooris/status.json";
static char *statusurl = NULL;
static char *agent = "Spacetray/0.42";
// The delay for polling the dooris service. Adjust this before compiling
@ -40,10 +51,24 @@ static int delay = 1000; // ms. aka 15 minutes
bool door_open = false;
bool old_door_open = false;
int door_last_change = 0;
void do_it();
void invoke_notification();
gboolean do_it_wrapper(gpointer data) {
do_it();
return FALSE;
}
void *status_update_thread(void *arg) {
while (1) {
sleep(delay);
g_idle_add(do_it_wrapper, NULL);
}
return NULL;
}
GtkStatusIcon *tray_icon;
void init_string(struct string *s) {
@ -79,8 +104,6 @@ void get_bouncer_data() {
struct json_object *leaf_json_object;
bool door_status;
int door_last_change;
struct string s;
printf("Called get_bouncer_data()\n");
@ -123,14 +146,8 @@ 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);
// free door object
json_object_put(door_json_object);
// free leaf object
//json_object_put(leaf_json_object);
// free response object
// json_object_put(response_json_object);
json_object_put(response_json_object);
printf("Door status: = %d\n", door_status);
printf("Door last change: = %d\n", door_last_change);
@ -143,12 +160,17 @@ void get_bouncer_data() {
}
void invoke_notification() {
char door[20];
char door[128];
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, 20, "Door open...");
snprintf(door, sizeof(door), "Door open since %s", date_str);
} else {
snprintf(door, 20, "Door closed...");
snprintf(door, sizeof(door), "Door closed since %s", date_str);
}
gtk_status_icon_set_tooltip(tray_icon, door);
@ -156,20 +178,23 @@ void invoke_notification() {
#if LIBNOTIFY
NotifyNotification *n;
notify_init(name);
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_closed_pixbuf,
&icon_open_pixbuf,
true,
NULL));
IGNORE_DEPRECATIONS_END
} 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_show(n, NULL);
g_object_unref(G_OBJECT(n));
@ -179,31 +204,50 @@ 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
} 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
}
}
void do_it() {
static bool first_run = true;
get_bouncer_data();
set_status();
if (door_open != old_door_open) {
if (!first_run && door_open != old_door_open) {
invoke_notification();
}
first_run = false;
}
int main(int argc, char **argv) {
pthread_t thread_id;
if (argc != 2) {
fprintf(stderr, "Usage: %s <statusurl>\n", argv[0]);
return 1;
}
statusurl = argv[1];
gtk_init(&argc, &argv);
#if LIBNOTIFY
notify_init(name);
#endif
tray_icon = gtk_status_icon_new();
g_signal_connect(G_OBJECT(tray_icon), "activate",
G_CALLBACK(do_it), NULL);
@ -220,7 +264,12 @@ int main(int argc, char **argv) {
//invoke_notification();
//gtk_timeout_add(delay, (GtkFunction)do_it, (gpointer)NULL);
guint xxx = g_timeout_add_seconds(1000, (GSourceFunc)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;
}
gtk_main();