Skip to content

Commit

Permalink
Merge branch 'feature/loadable-modules' into develop
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
Lokaltog committed Mar 8, 2014
2 parents 6746967 + bca5f00 commit 4a4c338
Show file tree
Hide file tree
Showing 23 changed files with 234 additions and 153 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ Installation instructions::

wkline

Debug/development build instructions (with relative library search path)::

git clone https://github.com/Lokaltog/wkline.git
cd wkline

waf clean configure build --debug --prefix=/ \
--libdir=`pwd`/out/lib/wkline install --destdir=out

out/bin/wkline

Configuration
-------------

Expand Down
32 changes: 10 additions & 22 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,40 @@
"theme_uri": "http://lokaltog.github.io/wkline-theme-default/",
"background": "#111",
"monitor": 0,
"widgets": {
"widgets": [
"desktops",
"window_title",
"now_playing_mpd",
"volume",
"weather",
"external_ip",
"battery"
],
"widgets_config": {
"desktops": {
"position": 0,
"enabled": true,

"names": ["I", "II", "III", "IV", "V"]
},
"now_playing_mpd": {
"position": 1,
"enabled": true,

"update_interval": 1000,
"host": "localhost",
"port": 6600,
"timeout": 5000
},
"volume": {
"position": 2,
"enabled": true,

"card": "default",
"selem": "Master"
},
"weather": {
"position": 3,
"enabled": true,

"location": "",
"unit": "c"
},
"external_ip": {
"position": 4,
"enabled": true,

"address": "http://ipv4.icanhazip.com/"
},
"battery": {
"position": 5,
"enabled": true,

"name": "BAT0"
},
"datetime": {
"position": 6,
"enabled": true,

"update_interval": 1000,
"date_format": "DDDD-MM-YY",
"time_format": "HH:mm:ss"
Expand Down
2 changes: 1 addition & 1 deletion src/util/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ wkline_get_config (struct wkline *self, const char *config_name) {
json_t*
wkline_widget_get_config (struct widget *self, const char *config_name) {
json_t *object;
object = json_object_get(self->config, "widgets");
object = json_object_get(self->config, "widgets_config");
if (!object) {
wklog("Warning: widgets block not found in config file");
}
Expand Down
76 changes: 62 additions & 14 deletions src/widgets.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "widgets.h"
#include "util/log.h"

GThread *widget_threads[LENGTH(wkline_widgets)];
static pthread_t *widget_threads;
static size_t widgets_len;

gboolean
update_widget (struct widget *widget) {
Expand Down Expand Up @@ -30,26 +31,73 @@ update_widget (struct widget *widget) {
return FALSE; /* only run once */
}

pthread_t
spawn_widget (WebKitWebView *web_view, json_t *config, const char *name) {
widget_init_func widget_init;
char libname[64];
snprintf(libname, 64, "libwidget_%s", name);
gchar *libpath = g_module_build_path(LIBDIR, libname);
GModule *lib = g_module_open(libpath, G_MODULE_BIND_LOCAL);
pthread_t return_thread;

if (!g_module_symbol(lib, "widget_init", (gpointer*)&widget_init)) {
wklog("loading of '%s' (%s) failed", libpath, name);

return 0;
}

struct widget *widget = malloc(sizeof(struct widget));

widget->config = config;
widget->web_view = web_view;
widget->name = strdup(name); /* don't forget to free this one */

wklog("spawning widget '%s'", name);

pthread_create(&return_thread, NULL, (void*)widget_init, widget);
pthread_setname_np(return_thread, name);

return return_thread;
}

void
handle_interrupt (int signal) {
unsigned short i;
if ((signal == SIGTERM) || (signal == SIGINT) || (signal == SIGHUP)) {
if (widget_threads && (widgets_len > 0)) {
wklog("handle_interrupt: stopping widget threads");
for (i = 0; i < widgets_len; i++) {
pthread_cancel(widget_threads[i]);
}
}
gtk_main_quit();
}
}

void
window_object_cleared_cb (WebKitWebView *web_view, GParamSpec *pspec, gpointer context, gpointer window_object, gpointer user_data) {
struct json_t *config = user_data;
json_t *config = user_data;
json_t *widgets_arr = json_object_get(config, "widgets");
unsigned short i;

wklog("webkit: window object cleared");
for (i = 0; i < LENGTH(wkline_widgets); i++) {
/* FIXME this is pretty bad, it should probably join and
recreate the threads instead */
if (!widget_threads[i]) {
struct widget *widget = malloc(sizeof(struct widget));

widget->config = config;
widget->web_view = web_view;
if (widget_threads && (widgets_len > 0)) {
wklog("webkit: stopping running widget threads");
for (i = 0; i < widgets_len; i++) {
/* this call may fail if the thread newer enters the
main thread loop, e.g. if it fails to connect to a
server */
pthread_cancel(widget_threads[i]);
}
}

/* Dont forget to free this one */
widget->name = strdup(wkline_widgets[i].name);
widgets_len = json_array_size(widgets_arr);
widget_threads = malloc(widgets_len * sizeof(pthread_t));

wklog("creating thread for widget '%s'", wkline_widgets[i].name);
widget_threads[i] = g_thread_new(wkline_widgets[i].name, (GThreadFunc)wkline_widgets[i].func, widget);
}
wklog("webkit: spawning new widget threads");

for (i = 0; i < widgets_len; i++) {
widget_threads[i] = spawn_widget(web_view, config, json_string_value(json_array_get(widgets_arr, i)));
}
}
46 changes: 7 additions & 39 deletions src/widgets.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#define _GNU_SOURCE
#include <gmodule.h>
#include <jansson.h>
#include <pthread.h>
#include <string.h>
#include <webkit/webkit.h>

Expand All @@ -10,48 +13,13 @@ struct widget {
WebKitWebView *web_view;
char *data;
};
struct widget_call {
void *func;
const char *name;
};

void* widget_battery ();
void* widget_desktops ();
void* widget_external_ip ();
void* widget_notifications ();
void* widget_now_playing_mpd ();
void* widget_volume ();
void* widget_weather ();
void* widget_window_title ();
typedef void (*widget_init_func)(struct widget *widget);

gboolean update_widget (struct widget *widget);
pthread_t spawn_widget (WebKitWebView *web_view, json_t *config, const char *name);
void handle_interrupt (int signal);
void window_object_cleared_cb (WebKitWebView *web_view, GParamSpec *pspec, gpointer context, gpointer window_object, gpointer user_data);

static const struct widget_call wkline_widgets[] = {
#ifndef DISABLE_WIDGET_BATTERY
{ .func = widget_battery, .name = "battery" },
#endif
#ifndef DISABLE_WIDGET_DESKTOPS
{ .func = widget_desktops, .name = "desktops" },
#endif
#ifndef DISABLE_WIDGET_EXTERNAL_IP
{ .func = widget_external_ip, .name = "external_ip" },
#endif
#ifndef DISABLE_WIDGET_NOTIFICATIONS
{ .func = widget_notifications, .name = "notifications" },
#endif
#ifndef DISABLE_WIDGET_NOW_PLAYING_MPD
{ .func = widget_now_playing_mpd, .name = "now_playing_mpd" },
#endif
#ifndef DISABLE_WIDGET_VOLUME
{ .func = widget_volume, .name = "volume" },
#endif
#ifndef DISABLE_WIDGET_WEATHER
{ .func = widget_weather, .name = "weather" },
#endif
#ifndef DISABLE_WIDGET_WINDOW_TITLE
{ .func = widget_window_title, .name = "window_title" },
#endif
};

#define MISSING_VALUE ""
#define LENGTH(X) (sizeof X / sizeof X[0])
25 changes: 17 additions & 8 deletions src/widgets/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ widget_battery_send_update (struct widget *widget, DBusGProxy *properties_proxy,
return 0;
}

static void
widget_cleanup (void *arg) {
wklog("widget cleanup: battery");
DBusGProxy **proxy_ref = arg;

if (proxy_ref[0] != NULL) {
g_object_unref(proxy_ref[0]);
}
if (proxy_ref[1] != NULL) {
g_object_unref(proxy_ref[1]);
}
}

void*
widget_battery (struct widget *widget) {
widget_init (struct widget *widget) {
DBusGConnection *conn;
DBusGProxy *proxy;
DBusGProxy *properties_proxy;
Expand Down Expand Up @@ -79,16 +92,12 @@ widget_battery (struct widget *widget) {
return 0;
}

DBusGProxy *proxy_ptr[2] = { proxy, properties_proxy };
pthread_cleanup_push(widget_cleanup, proxy_ptr);
for (;;) {
widget_battery_send_update(widget, properties_proxy, pathbuf);

sleep(20);
}

if (proxy != NULL) {
g_object_unref(proxy);
}
if (properties_proxy != NULL) {
g_object_unref(properties_proxy);
}
pthread_cleanup_pop(1);
}
1 change: 1 addition & 0 deletions src/widgets/battery.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <pthread.h>

#include "util/config.h"
#include "util/dbus_helpers.h"
16 changes: 12 additions & 4 deletions src/widgets/desktops.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,17 @@ desktops_send_update (struct widget *widget, xcb_ewmh_connection_t *ewmh, int sc
free(desktops);
}

static void
widget_cleanup (void *arg) {
wklog("widget cleanup: desktops");

xcb_ewmh_connection_t *ewmh = arg;
xcb_ewmh_connection_wipe(ewmh);
xcb_disconnect(ewmh->connection);
}

void*
widget_desktops (struct widget *widget) {
widget_init (struct widget *widget) {
xcb_connection_t *conn = xcb_connect(NULL, NULL);
if (xcb_connection_has_error(conn)) {
wklog("Could not connect to display %s.", getenv("DISPLAY"));
Expand All @@ -113,6 +122,7 @@ widget_desktops (struct widget *widget) {
return 0;
}

pthread_cleanup_push(widget_cleanup, ewmh);
desktops_send_update(widget, ewmh, screen_nbr);

for (;;) {
Expand All @@ -137,7 +147,5 @@ widget_desktops (struct widget *widget) {
}
}

xcb_ewmh_connection_wipe(ewmh);

return 0;
pthread_cleanup_pop(1);
}
1 change: 1 addition & 0 deletions src/widgets/desktops.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <pthread.h>
#include <stdbool.h>
#include <xcb/xcb_event.h>
#include <xcb/xcb_ewmh.h>
Expand Down
4 changes: 1 addition & 3 deletions src/widgets/external_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ widget_external_ip_send_update (struct widget *widget) {
}

void*
widget_external_ip (struct widget *widget) {
widget_init (struct widget *widget) {
for (;;) {
widget_external_ip_send_update(widget);

sleep(600);
}

return 0;
}
4 changes: 3 additions & 1 deletion src/widgets/external_ip.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#include <pthread.h>

#include "util/curl.h"
#include "util/config.h"
#include "util/config.h"
16 changes: 11 additions & 5 deletions src/widgets/notifications.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ widget_notifications_send_update (struct widget *widget, DBusConnection *connect
return 0;
}

static void
widget_cleanup (void *arg) {
wklog("widget cleanup: notifications");

DBusConnection *connection = arg;
dbus_connection_unref(connection);
}

void*
widget_notifications (struct widget *widget) {
widget_init (struct widget *widget) {
DBusConnection *connection;
DBusError dbus_error;
DBusError *err = &dbus_error;
Expand Down Expand Up @@ -123,6 +131,7 @@ widget_notifications (struct widget *widget) {
}
dbus_error_free(err);

pthread_cleanup_push(widget_cleanup, connection);
for (;;) {
dbus_connection_read_write(connection, -1);

Expand All @@ -144,8 +153,5 @@ widget_notifications (struct widget *widget) {
dbus_connection_flush(connection);
}
}

dbus_connection_unref(connection);

return 0;
pthread_cleanup_pop(1);
}

0 comments on commit 4a4c338

Please sign in to comment.