notail

HEAD
notail.c
0#include <glib-2.0/glib.h>1#include <glib-2.0/glib-unix.h>2#include <libnotify/notification.h>3#include <libnotify/notify.h>4#include <getopt.h>56#define READ_BUF_SIZE 409678struct notail {9	NotifyNotification *notification;10	gboolean running;11	char *summary;12	gboolean close_on_exit;13	int close_delay;14	gboolean is_closed;15	gchar read_buf[READ_BUF_SIZE];16	gsize read_buf_pos;17	GString *notification_contents;18	gsize max_notification_contents_len;19};2021static void notail_init(struct notail *state) {22	state->running = FALSE;23	state->summary = "notail";24	state->close_on_exit = FALSE;25	state->close_delay = 0;26	state->is_closed = FALSE;27	state->max_notification_contents_len = 5000;28	state->read_buf_pos = 0;29	state->notification_contents = g_string_new(NULL);30}31 32static void stop_main_loop(struct notail *state) {33	state->running = FALSE;34	g_main_context_wakeup(NULL);35}3637static gboolean fd_callback(GIOChannel* source, GIOCondition condition, gpointer user_data) {38	struct notail *state = (struct notail *) user_data;3940	if (!(condition & G_IO_IN)) {41		stop_main_loop(state);4243		return FALSE;44	}4546	gsize n;47	GIOStatus status = g_io_channel_read_chars(source, state->read_buf + state->read_buf_pos, READ_BUF_SIZE - state->read_buf_pos - 1, &n, NULL);48	state->read_buf_pos += n;49	state->read_buf[state->read_buf_pos] = '\0';5051	switch (status) {52		case G_IO_STATUS_EOF:53		case G_IO_STATUS_ERROR:54			stop_main_loop(state);5556			return FALSE;57		case G_IO_STATUS_AGAIN:58			return TRUE;59		case G_IO_STATUS_NORMAL:60			state->read_buf_pos = 0;61			break;62	}6364	g_string_append(state->notification_contents, state->read_buf);6566	gsize notification_contents_len = g_utf8_strlen(state->notification_contents->str, -1);67	if (notification_contents_len > state->max_notification_contents_len) {68		GString *new_notification_contents = g_string_new(g_utf8_substring(state->notification_contents->str, notification_contents_len - state->max_notification_contents_len, -1));69		g_string_free(state->notification_contents, TRUE);70		state->notification_contents = new_notification_contents;71	}7273	notify_notification_update(state->notification, state->summary, state->notification_contents->str, NULL);7475	return TRUE;76}7778static void notification_closed_callback(NotifyNotification* notification, gpointer user_data)79{80	struct notail *state = (struct notail *) user_data;8182	state->is_closed = TRUE;8384	stop_main_loop(state);85}8687static gboolean sigint_callback(gpointer user_data)88{89	struct notail *state = (struct notail *) user_data;9091	stop_main_loop(state);9293	return FALSE;94}9596static gboolean show_notification(gpointer user_data) {97	struct notail *state = (struct notail *) user_data;9899	notify_notification_show(state->notification, NULL);100101	return TRUE;102}103104static int notail_run(struct notail *state) {105	notify_init("notail");106107	state->notification = notify_notification_new(state->summary, " ", NULL);108109	g_unix_signal_add (SIGINT, sigint_callback, state);110	g_signal_connect(G_OBJECT(state->notification),111			"closed",112			G_CALLBACK(notification_closed_callback),113			state);114115	notify_notification_set_timeout(state->notification, 0);116117	GIOChannel *io_channel = g_io_channel_unix_new(0);118	g_io_channel_set_encoding(io_channel, "UTF-8", NULL);119	g_io_channel_set_flags(io_channel, G_IO_FLAG_NONBLOCK, NULL);120	g_io_add_watch(io_channel, G_IO_IN | G_IO_HUP | G_IO_ERR, fd_callback, state);121122	g_timeout_add(100, show_notification, state);123124	state->running = TRUE;125	while (state->running) {126		g_main_context_iteration(NULL, TRUE);127	}128129	g_io_channel_shutdown(io_channel, FALSE, NULL);130	g_io_channel_unref(io_channel);131132	if (!state->is_closed) {133		// Make sure the notification is displayed at least once before the program ends.134		notify_notification_show(state->notification, NULL);135136		if (state->close_on_exit) {137			if (state->close_delay != 0) {138				notify_notification_set_timeout(state->notification, state->close_delay);139				notify_notification_show(state->notification, NULL);140			} else {141				notify_notification_close(state->notification, NULL);142			}143		}144	}145146	g_object_unref(G_OBJECT(state->notification));147	notify_uninit();148149	return 0;150}151152void print_version()153{154#ifdef VERSION155       printf("notail version %s\n", VERSION);156#else157       printf("notail (unknown version)\n");158#endif159}160161char help[] =162		"usage: notail [options] [command]\n"163		"  -h, --help                    Display this help text.\n"164		"  -v, --version                 Display version information.\n"165		"  -c, --close-on-exit           Close the notification on command exit.\n"166		"  -d, --close-delay <delay ms>  Delay in milliseconds before closing the notification after the program quits. Only has effect if used together with -c.\n"167		"  -s, --summary <summary>       Set the summary of the notification.\n"168		"  -C  --num-chars <num>         Output the last num characters of the output in the notification\n";169170void print_help()171{172	printf("%s", help);173}174175int main(int argc, char *argv[])176{177	int c;178	int option_index = 0;179	struct notail state = {0};180181	notail_init(&state);182183	while (1) {184		static struct option long_options[] = {185			{"help", no_argument, 0, 'h'},186			{"version", no_argument, 0, 'v'},187			{"close-on-exit", no_argument, 0, 'c'},188			{"close-delay", required_argument, 0, 'd'},189			{"summary", required_argument, 0, 's'},190			{"num-chars", required_argument, 0, 'C'},191			{0, 0, 0, 0},192		};193194		c = getopt_long(argc, argv, "hvcd:s:C:", long_options, &option_index);195196		if (c == -1) {197			break;198		}199200		switch (c) {201		case 'h':202			print_help();203			return EXIT_SUCCESS;204		case 'v':205			print_version();206			return EXIT_SUCCESS;207		case 'c':208			state.close_on_exit = TRUE;209			break;210		case 'd':211			state.close_delay = atoi(optarg);212			break;213		case 's':214			state.summary = optarg;215			break;216		case 'C':217			state.max_notification_contents_len = atoi(optarg);218			break;219		}220	}221222	if(notail_run(&state)) {223		return EXIT_FAILURE;224	}225226	return EXIT_SUCCESS;227}228
srcg
version 69be645