HEAD
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 {9NotifyNotification *notification;10gboolean running;11char *summary;12gboolean close_on_exit;13int close_delay;14gboolean is_closed;15gchar read_buf[READ_BUF_SIZE];16gsize read_buf_pos;17GString *notification_contents;18gsize max_notification_contents_len;19};2021static void notail_init(struct notail *state) {22state->running = FALSE;23state->summary = "notail";24state->close_on_exit = FALSE;25state->close_delay = 0;26state->is_closed = FALSE;27state->max_notification_contents_len = 5000;28state->read_buf_pos = 0;29state->notification_contents = g_string_new(NULL);30}3132static void stop_main_loop(struct notail *state) {33state->running = FALSE;34g_main_context_wakeup(NULL);35}3637static gboolean fd_callback(GIOChannel* source, GIOCondition condition, gpointer user_data) {38struct notail *state = (struct notail *) user_data;3940if (!(condition & G_IO_IN)) {41stop_main_loop(state);4243return FALSE;44}4546gsize n;47GIOStatus status = g_io_channel_read_chars(source, state->read_buf + state->read_buf_pos, READ_BUF_SIZE - state->read_buf_pos - 1, &n, NULL);48state->read_buf_pos += n;49state->read_buf[state->read_buf_pos] = '\0';5051switch (status) {52case G_IO_STATUS_EOF:53case G_IO_STATUS_ERROR:54stop_main_loop(state);5556return FALSE;57case G_IO_STATUS_AGAIN:58return TRUE;59case G_IO_STATUS_NORMAL:60state->read_buf_pos = 0;61break;62}6364g_string_append(state->notification_contents, state->read_buf);6566gsize notification_contents_len = g_utf8_strlen(state->notification_contents->str, -1);67if (notification_contents_len > state->max_notification_contents_len) {68GString *new_notification_contents = g_string_new(g_utf8_substring(state->notification_contents->str, notification_contents_len - state->max_notification_contents_len, -1));69g_string_free(state->notification_contents, TRUE);70state->notification_contents = new_notification_contents;71}7273notify_notification_update(state->notification, state->summary, state->notification_contents->str, NULL);7475return TRUE;76}7778static void notification_closed_callback(NotifyNotification* notification, gpointer user_data)79{80struct notail *state = (struct notail *) user_data;8182state->is_closed = TRUE;8384stop_main_loop(state);85}8687static gboolean sigint_callback(gpointer user_data)88{89struct notail *state = (struct notail *) user_data;9091stop_main_loop(state);9293return FALSE;94}9596static gboolean show_notification(gpointer user_data) {97struct notail *state = (struct notail *) user_data;9899notify_notification_show(state->notification, NULL);100101return TRUE;102}103104static int notail_run(struct notail *state) {105notify_init("notail");106107state->notification = notify_notification_new(state->summary, " ", NULL);108109g_unix_signal_add (SIGINT, sigint_callback, state);110g_signal_connect(G_OBJECT(state->notification),111"closed",112G_CALLBACK(notification_closed_callback),113state);114115notify_notification_set_timeout(state->notification, 0);116117GIOChannel *io_channel = g_io_channel_unix_new(0);118g_io_channel_set_encoding(io_channel, "UTF-8", NULL);119g_io_channel_set_flags(io_channel, G_IO_FLAG_NONBLOCK, NULL);120g_io_add_watch(io_channel, G_IO_IN | G_IO_HUP | G_IO_ERR, fd_callback, state);121122g_timeout_add(100, show_notification, state);123124state->running = TRUE;125while (state->running) {126g_main_context_iteration(NULL, TRUE);127}128129g_io_channel_shutdown(io_channel, FALSE, NULL);130g_io_channel_unref(io_channel);131132if (!state->is_closed) {133// Make sure the notification is displayed at least once before the program ends.134notify_notification_show(state->notification, NULL);135136if (state->close_on_exit) {137if (state->close_delay != 0) {138notify_notification_set_timeout(state->notification, state->close_delay);139notify_notification_show(state->notification, NULL);140} else {141notify_notification_close(state->notification, NULL);142}143}144}145146g_object_unref(G_OBJECT(state->notification));147notify_uninit();148149return 0;150}151152void print_version()153{154#ifdef VERSION155printf("notail version %s\n", VERSION);156#else157printf("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{172printf("%s", help);173}174175int main(int argc, char *argv[])176{177int c;178int option_index = 0;179struct notail state = {0};180181notail_init(&state);182183while (1) {184static 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};193194c = getopt_long(argc, argv, "hvcd:s:C:", long_options, &option_index);195196if (c == -1) {197break;198}199200switch (c) {201case 'h':202print_help();203return EXIT_SUCCESS;204case 'v':205print_version();206return EXIT_SUCCESS;207case 'c':208state.close_on_exit = TRUE;209break;210case 'd':211state.close_delay = atoi(optarg);212break;213case 's':214state.summary = optarg;215break;216case 'C':217state.max_notification_contents_len = atoi(optarg);218break;219}220}221222if(notail_run(&state)) {223return EXIT_FAILURE;224}225226return EXIT_SUCCESS;227}228