#ifndef C8_WIDGET_H
#define C8_WIDGET_H

#include <glib.h>
#include <stdbool.h>
#include <stdint.h>

/*
 * c8-widget: live tiles for the cont8nuum Start screen.
 *
 * Each widget kind gathers something (the time, the processor load, the
 * weather) and describes it as live tile content: fields such as block,
 * text1 and meter, which Start lays out in one of its templates (see
 * org.cont8nuum.Tiles1.xml in c8-protocols). It never draws: Start does.
 */

/* Status colours, Windows Phone style */
#define COLOR_WARN "#fa6800"
#define COLOR_ALERT "#a20025"
#define COLOR_MUTED "#647687"

/* The fields of one update */
struct content;

struct widget;

struct widget_kind {
	const char *name;
	const char *summary;
	const char *options; // help text for the kind's own options, or NULL
	/* Called every tick_ms while Start is visible, and once when it shows */
	unsigned tick_ms;
	/* Parse the kind's own arguments and start its data sources */
	bool (*init)(struct widget *w, int argc, char **argv);
	void (*tick)(struct widget *w);
	/* Describe what the tile shows now */
	void (*content)(struct widget *w, struct content *c);
	void (*activated)(struct widget *w);
};

struct widget {
	const struct widget_kind *kind;
	void *data;
	bool visible; // Start is on screen
};

/* Send the tile new content soon; several in a row are sent once, and
 * nothing is sent if it didn't change */
void widget_update(struct widget *w);

/* Set a field; printf-style. An empty value leaves the field out. */
void set(struct content *c, const char *key, const char *fmt, ...)
	G_GNUC_PRINTF(3, 4);

/* Helpers for reading the system */
char *read_text(const char *path); // g_free the result; NULL on error
bool read_long(const char *path, long *out);
/* 1536 -> "1.5K", like conky's short human readable units */
void format_bytes(double bytes, char *buf, size_t len);
/* Run argv without waiting and hand its stdout to done(), or NULL on
 * failure. Returns false if it couldn't be started. */
typedef void (*command_done_fn)(struct widget *w, const char *out);
bool run_command(struct widget *w, const char *const *argv,
	command_done_fn done);
bool have_program(const char *name);
char *cache_path(const char *name); // ~/.cache/cont8nuum/NAME, g_free
/* [general] clock from config.ini (auto, 12 or 24), read again when the
 * file changes; for clock_format in clock.h */
const char *clock_mode(void);

/* The kinds */
extern const struct widget_kind kind_clock, kind_processor, kind_memory,
	kind_storage, kind_network, kind_battery, kind_sensors, kind_updates,
	kind_calendar, kind_clocks, kind_weather,
	kind_nowplaying, kind_volume, kind_power, kind_script;

#endif
