# HG changeset patch # User Guido Berhoerster # Date 1532808144 -7200 # Node ID f0accfc74f7bf0b3a7323bf08dc987b2a8f814c3 # Parent 4a53309794338e497b757af14e6fe03bdf893c09 Port to GLib version 2.48 or later Use GApplication instead of the custom XWritedUnique object for uniqueness. Use GNotification from GIO instead of libnotify. Remove help and debug options from documentation. Replace intltool with GNU gettext (version 0.19 or later required). diff -r 4a5330979433 -r f0accfc74f7b Makefile --- a/Makefile Wed Sep 14 09:46:59 2016 +0200 +++ b/Makefile Sat Jul 28 22:02:24 2018 +0200 @@ -1,5 +1,5 @@ # -# Copyright (C) 2016 Guido Berhoerster +# Copyright (C) 2018 Guido Berhoerster # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -22,9 +22,10 @@ # PACKAGE = xwrited -APP_NAME = org.guido-berhoerster.code.xwrited +APPLICATION_ID = org.guido-berhoerster.code.xwrited VERSION = 2 DISTNAME := $(PACKAGE)-$(VERSION) +BUG_ADDRESS = guido+xwrited@berhoerster.name # gcc, clang, icc, Sun/Solaris Studio CC := $(CC) -std=c99 @@ -35,18 +36,35 @@ #MAKEDEPEND.c = $(CC) -xM1 $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) # X makedepend #MAKEDEPEND.c = makedepend -f- -Y -- $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) -- +LINK.c = $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(XCPPFLAGS) $(LDFLAGS) $(XLDFLAGS) $(TARGET_ARCH) +LINK.o = $(CC) $(LDFLAGS) $(XLDFLAGS) $(TARGET_ARCH) +CP := cp INSTALL := install INSTALL.exec := $(INSTALL) -D -m 0755 INSTALL.data := $(INSTALL) -D -m 0644 +INSTALL.link := $(CP) -f -P PAX := pax GZIP := gzip SED := sed -PASTE := paste -MSGFMT := msgfmt -INTLTOOL_UPDATE := intltool-update -INTLTOOL_MERGE := intltool-merge XSLTPROC := xsltproc DOCBOOK5_MANPAGES_STYLESHEET = http://docbook.sourceforge.net/release/xsl-ns/current/manpages/docbook.xsl +DOCBOOK5_MANPAGES_FLAGS = --stringparam man.authors.section.enabled 0 \ + --stringparam man.copyright.section.enabled 0 +MSGFMT = msgfmt +MSGMERGE = msgmerge +XGETTEXT = xgettext +XGETTEXT_OPTIONS = --copyright-holder "$(AUTHOR)" \ + --package-name '$(PACKAGE)' \ + --package-version '$(VERSION)' \ + --msgid-bugs-address '$(BUG_ADDRESS)' \ + --default-domain '$(PACKAGE)' \ + --from-code UTF-8 \ + --keyword=_ \ + --keyword=N_ \ + --keyword=C_:1c,2 \ + --keyword=NC_:1c,2 \ + --keyword=g_dngettext:2,3 \ + --add-comments define generate-manpage-rule = %.$(1): %.$(1).xml @@ -54,7 +72,7 @@ --xinclude \ --stringparam package $$(PACKAGE) \ --stringparam version $$(VERSION)\ - data/docbook-update-source-data.xsl $$< | \ + docbook-update-source-data.xsl $$< | \ $$(XSLTPROC) \ --xinclude \ $$(DOCBOOK5_MANPAGES_FLAGS) \ @@ -75,95 +93,94 @@ OS_NAME := $(shell uname -s) OS_RELEASE := $(shell uname -r) -ifeq ($(shell pkg-config --exists 'glib-2.0 >= 2.25.5' && printf "true"),true) - HAVE_GLIB_GDBUS = 1 +OBJS = xwrited.o \ + xwd-application.o +ifneq ($(findstring $(OS_NAME),Linux FreeBSD),) + OBJS += xwd-utmp-utempter.o else - HAVE_GLIB_GDBUS = 0 + OBJS += xwd-utmp-utmpx.o endif -PKGCONFIG_LIBS := dbus-1 glib-2.0 libnotify -ifeq ($(HAVE_GLIB_GDBUS),1) - PKGCONFIG_LIBS += dbus-glib-1 -endif -OBJS = main.o xwrited-debug.o xwrited-unique.o -ifneq ($(findstring $(OS_NAME),Linux FreeBSD),) - OBJS += xwrited-utmp-utempter.o -else - OBJS += xwrited-utmp-utmpx.o -endif -MANPAGES = data/$(PACKAGE).1 -AUTOSTART_FILE = data/$(PACKAGE).desktop -MOFILES := $(patsubst %.po,%.mo,$(wildcard po/*.po)) +MANPAGES = $(PACKAGE).1 + +AUTOSTART_FILE = $(APPLICATION_ID).desktop + +LINGUAS := $(shell sed 's/\#.*//' po/LINGUAS) +MOFILES := $(patsubst %,po/%.mo,$(LINGUAS)) +POTFILES_IN := $(shell sed 's/\#.*//' po/POTFILES.in) POTFILE = po/$(PACKAGE).pot -POSRCS := $(shell $(SED) -e 's/\#.*//' -e '/^[ \t]*$$/d' \ - -e 's/^\[[^]]*\]//' po/POTFILES.in | $(PASTE) -s -d ' ') -DOCBOOK5_MANPAGES_FLAGS = --stringparam man.authors.section.enabled 0 \ - --stringparam man.copyright.section.enabled 0 .DEFAULT_TARGET = all -.PHONY: all clean clobber dist install +.PHONY: all pot update-po clean clobber dist install -all: $(PACKAGE) $(MANPAGES) $(MOFILES) $(AUTOSTART_FILE) +all: $(PACKAGE) $(AUTOSTART_FILE) $(MOFILES) $(MANPAGES) -$(PACKAGE): CPPFLAGS := $(shell pkg-config --cflags $(PKGCONFIG_LIBS)) \ - -D_XOPEN_SOURCE=600 \ - -DPACKAGE="\"$(PACKAGE)\"" \ - -DAPP_NAME=\"$(APP_NAME)\" \ +$(PACKAGE): XCFLAGS = $(shell pkg-config --cflags gobject-2.0 gio-2.0 \ + glib-2.0) \ + -DPACKAGE=\"$(PACKAGE)\" \ + -DAPPLICATION_ID=\"$(APPLICATION_ID)\" \ -DVERSION=\"$(VERSION)\" \ - -DLOCALEDIR="\"$(localedir)\"" \ - -DG_LOG_DOMAIN=\"$(PACKAGE)\" -$(PACKAGE): LDLIBS := $(shell pkg-config --libs $(PKGCONFIG_LIBS)) -ifeq ($(HAVE_GLIB_GDBUS),1) - $(PACKAGE): XCPPFLAGS += -DHAVE_GLIB_GDBUS -endif + -DG_LOG_DOMAIN=\"$(PACKAGE)\" \ + -DPACKAGE_LOCALE_DIR="\"$(localedir)\"" \ + -DGETTEXT_PACKAGE=\"$(PACKAGE)\" +$(PACKAGE): LDLIBS = $(shell pkg-config --libs gobject-2.0 gio-2.0 \ + glib-2.0) + ifeq ($(OS_NAME),Linux) + $(PACKAGE): XCPPFLAGS += -D_XOPEN_SOURCE=600 $(PACKAGE): LDLIBS += -lutempter else ifeq ($(OS_NAME),FreeBSD) $(PACKAGE): LDLIBS += -lulog else ifeq ($(OS_NAME),SunOS) - $(PACKAGE): XCPPFLAGS += -I/usr/xpg4/include + $(PACKAGE): XCPPFLAGS += -D_XOPEN_SOURCE=600 -I/usr/xpg4/include $(PACKAGE): XLDFLAGS += -L/usr/xpg4/lib -R/usr/xpg4/lib endif + $(PACKAGE): $(OBJS) $(LINK.o) $^ $(LDLIBS) -o $@ -$(POTFILE): po/POTFILES.in $(POSRCS) - cd po/ && $(INTLTOOL_UPDATE) --pot --gettext-package="$(PACKAGE)" +$(POTFILE): po/POTFILES.in $(POTFILES_IN) + $(XGETTEXT) $(XGETTEXT_OPTIONS) --files-from $< --output $@ pot: $(POTFILE) update-po: $(POTFILE) - cd po/ && for lang in $(patsubst po/%.mo,%,$(MOFILES)); do \ - $(INTLTOOL_UPDATE) --dist --gettext-package="$(PACKAGE)" \ - $${lang}; \ + for pofile in $(patsubst %.mo,%.po,$(MOFILES)); do \ + $(MSGMERGE) --update --backup off $$pofile $<; \ done +%.mo: %.po + $(MSGFMT) --output $@ $< + +%.desktop: %.desktop.in + $(MSGFMT) --desktop --template $< -d po --output $@ + +$(foreach section,1 2 3 4 5 6 7 8 9,$(eval $(call generate-manpage-rule,$(section)))) + +%.mo: %.po + $(MSGFMT) -o $@ $< + %.o: %.c $(MAKEDEPEND.c) $< | $(SED) -f deps.sed >$*.d $(COMPILE.c) -o $@ $< -$(foreach section,1 2 3 4 5 6 7 8 9,$(eval $(call generate-manpage-rule,$(section)))) - -%.desktop: %.desktop.in $(MOFILES) - $(INTLTOOL_MERGE) --desktop-style --utf8 po $< $@ - -%.mo: %.po - $(MSGFMT) -o $@ $< - -install: +install: all $(INSTALL.exec) $(PACKAGE) "$(DESTDIR)$(bindir)/$(PACKAGE)" - for lang in $(patsubst po/%.mo,%,$(MOFILES)); do \ - $(INSTALL.data) po/$${lang}.mo \ - "$(DESTDIR)$(localedir)/$${lang}/LC_MESSAGES/$(PACKAGE).mo"; \ + $(INSTALL.data) $(AUTOSTART_FILE) \ + $(DESTDIR)$(xdgautostartdir)/$(AUTOSTART_FILE) + for lang in $(LINGUAS); do \ + $(INSTALL.data) po/$${lang}.mo \ + $(DESTDIR)$(localedir)/$${lang}/LC_MESSAGES/$(PACKAGE).mo; \ done - $(INSTALL.data) $(AUTOSTART_FILE) \ - "$(DESTDIR)$(xdgautostartdir)/$(notdir $(AUTOSTART_FILE))" - $(INSTALL.data) data/$(PACKAGE).1 \ - "$(DESTDIR)$(mandir)/man1/$(PACKAGE).1" + for manpage in $(MANPAGES); do \ + $(INSTALL.data) $${manpage} \ + "$(DESTDIR)$(mandir)/man$${manpage##*.}/$${manpage##*/}"; \ + done clean: - rm -f $(PACKAGE) $(OBJS) $(POTFILE) $(MOFILES) $(MANPAGES) $(AUTOSTART_FILE) + rm -f $(PACKAGE) $(OBJS) $(AUTOSTART_FILE) $(POTFILE) $(MOFILES) \ + $(MANPAGES) clobber: clean rm -f $(patsubst %.o,%.d,$(OBJS)) @@ -173,4 +190,6 @@ -s ',^\.$$,,' -s ',\./,$(DISTNAME)/,' . | \ $(GZIP) > $(DISTNAME).tar.gz +-include local.mk + -include $(patsubst %.o,%.d,$(OBJS)) diff -r 4a5330979433 -r f0accfc74f7b README --- a/README Wed Sep 14 09:46:59 2016 +0200 +++ b/README Sat Jul 28 22:02:24 2018 +0200 @@ -14,13 +14,12 @@ Illumos-derived distributions. The following tools and shared libraries are required to build xwrited: -- GNU make >= 3.81 +- GNU make version 3.81 or later - GNU or BSD install -- GLib version 2.26 or later -- the D-Bus GLib bindings for GLib < 2.25.5 +- GNU gettext 0.19 or later +- GLib version 2.48 or later - libutempter on Linux - -Rebuilding the man pages additionally requires the xsltproc tool from libxml2. +- the xsltproc tool from libxml2 Before building xwrited check the commented macros in the Makefile for any macros you may need to override depending on the used toolchain and operating @@ -78,10 +77,10 @@ License ------- -Except otherwise noted, all files are Copyright (C) 2014 Guido Berhoerster and +Except otherwise noted, all files are Copyright (C) 2018 Guido Berhoerster and distributed under the following license terms: -Copyright (C) 2014 Guido Berhoerster +Copyright (C) 2018 Guido Berhoerster Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff -r 4a5330979433 -r f0accfc74f7b data/docbook-update-source-data.xsl --- a/data/docbook-update-source-data.xsl Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff -r 4a5330979433 -r f0accfc74f7b data/xwrited.1.xml --- a/data/xwrited.1.xml Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,159 +0,0 @@ - - - - - - - Guido - Berhoerster - - guido+xwrited@berhoerster.name - - - 27 April, 2014 - - - xwrited - 1 - - - User Commands - - - xwrited - display write and wall messages as desktop - notifications - - - - xwrited - - - - - - - - - - - - - - - - - - - - - - - - - Description - The xwrited utility displays - write1 - and - wall1 - messages as desktop notifications. A notification daemon - compliant to the freedesktop.org Desktop Notification Specification draft - needs to be running in order to display the notifications. - - - Options - The following options are supported: - - - - - - - - - - - - - Print a summary of all command line options and exit. - - - - - - - - - - - Enable debugging output. - - - - - - - - - - - Print the version number and exit. - - - - - - Exit Status - The following exit values are returned: - - - 0 - - Command successfully executed. - - - - > 0 - - An error has occured. - - - - - - See Also - write - 1, - wall1 - - - - Notes - xwrited assumes that messages are encoded in UTF-8 because there is - no way for it to determine the character set encoding of the received - data. - - diff -r 4a5330979433 -r f0accfc74f7b data/xwrited.desktop.in --- a/data/xwrited.desktop.in Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -[Desktop Entry] -Encoding=UTF-8 -_Name=xwrited -_Comment=Display write and wall messages as desktop notifications -Exec=xwrited -Terminal=false -Type=Application -Categories=System;Monitor; diff -r 4a5330979433 -r f0accfc74f7b docbook-update-source-data.xsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docbook-update-source-data.xsl Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + diff -r 4a5330979433 -r f0accfc74f7b main.c --- a/main.c Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,507 +0,0 @@ -/* - * Copyright (C) 2015 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "xwrited-debug.h" -#include "xwrited-unique.h" -#include "xwrited-utmp.h" - -#define BUFFER_TIMEOUT (250) - -enum { - PIPE_R_FD = 0, - PIPE_W_FD -}; - -static int signal_pipe_fd[2] = { -1, -1 }; -static guint notify_timeout_id; -static GMainLoop *loop; -static GString *buffer; - -static void -on_signal(int signo) -{ - int old_errno = errno; - ssize_t n; - sigset_t sigset; - - /* try to read unread signals from the pipe and add the new one to it */ - n = read(signal_pipe_fd[PIPE_R_FD], &sigset, sizeof (sigset)); - if ((n == -1) || ((size_t)n < sizeof (sigset))) { - sigemptyset(&sigset); - } - sigaddset(&sigset, signo); - write(signal_pipe_fd[PIPE_W_FD], &sigset, sizeof (sigset)); - - errno = old_errno; -} - -static gboolean -signal_read_cb(GIOChannel *source, GIOCondition cond, gpointer user_data) -{ - sigset_t sigset; - sigset_t old_sigset; - GIOStatus status; - gsize n; - GError *error = NULL; - - /* - * deal with pending signals previously received in the signal handler, - * try to read a sigset from the pipe, avoid partial reads by blocking - * all signals during the read operation - */ - sigfillset(&sigset); - sigprocmask(SIG_BLOCK, &sigset, &old_sigset); - status = g_io_channel_read_chars(source, (gchar *)&sigset, - sizeof (sigset), &n, &error); - sigprocmask(SIG_SETMASK, &old_sigset, NULL); - if (status != G_IO_STATUS_NORMAL) { - if (status != G_IO_STATUS_AGAIN) { - if (error != NULL) { - g_critical("failed to read from signal pipe: " - "%s", error->message); - g_error_free(error); - g_main_loop_quit(loop); - } else { - g_critical("failed to read from signal pipe"); - g_main_loop_quit(loop); - } - } - } else if (n != sizeof (sigset)) { - g_critical("short read from signal pipe"); - g_main_loop_quit(loop); - } else { - if ((sigismember(&sigset, SIGINT) == 1) || - (sigismember(&sigset, SIGTERM) == 1) || - (sigismember(&sigset, SIGQUIT) == 1) || - (sigismember(&sigset, SIGHUP) == 1)) { - g_debug("received signal, exiting"); - g_main_loop_quit(loop); - } - } - - return (TRUE); -} - -static gboolean -send_notification(void) -{ - gboolean retval = FALSE; - GString *utf8_str = NULL; - gchar *startp = buffer->str; - gchar *endp; - GRegex *regex = NULL; - GError *error = NULL; - gchar *body = NULL; - GList *capabilities = NULL; - gchar *tmp; - NotifyNotification *notification = NULL; - - utf8_str = g_string_sized_new(buffer->len); - while (!g_utf8_validate(startp, buffer->str + buffer->len - - startp, (const gchar **)&endp)) { - g_string_append_len(utf8_str, startp, endp - startp); - /* - * replace each byte that does not belong to a UTF-8-encoded - * character with the Unicode REPLACEMENT CHARACTER (U+FFFD) - */ - g_string_append(utf8_str, "\357\277\275"); - - startp = endp + ((endp < buffer->str + buffer->len) ? 1 : 0); - } - g_string_append_len(utf8_str, startp, buffer->str + buffer->len - - startp); - - /* remove any CR, BEL and trailing space and tabs */ - regex = g_regex_new("([\r\a]+|[ \t\r\a]+$)", G_REGEX_MULTILINE, 0, - &error); - if (error != NULL) { - g_critical("failed to create regex object: %s", - error->message); - g_error_free(error); - goto out; - } - body = g_regex_replace_literal(regex, utf8_str->str, -1, 0, "", 0, - &error); - if (error != NULL) { - g_critical("failed to replace control and space characters: " - "%s", error->message); - g_error_free(error); - goto out; - } - - /* - * skip empty messages or messages only consisting of whitespace and - * control characters - */ - if ((strlen(body) == 0) || - !g_regex_match_simple("[^[:space:][:cntrl:]]", body, 0, 0)) { - retval = TRUE; - goto out; - } - - /* - * if the notification daemon supports markup the message needs to be - * escaped - */ - capabilities = notify_get_server_caps(); - if (g_list_find_custom(capabilities, "body-markup", - (GCompareFunc)strcmp) != NULL) { - tmp = g_markup_escape_text(body, -1); - g_free(body); - body = tmp; - } - - /* show notification */ - notification = notify_notification_new(_("Message received"), - body, "utilities-terminal" -#if !defined(NOTIFY_VERSION_MINOR) || \ - (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7) - , NULL -#endif - ); - if (notification == NULL) { - g_critical("failed to create a notification object"); - g_main_loop_quit(loop); - goto out; - } - notify_notification_set_timeout(notification, NOTIFY_EXPIRES_NEVER); - retval = notify_notification_show(notification, NULL); - -out: - if (notification != NULL) { - g_object_unref(G_OBJECT(notification)); - } - if (capabilities != NULL) { - g_list_free_full(capabilities, g_free); - } - g_free(body); - if (regex != NULL) { - g_regex_unref(regex); - } - if (utf8_str != NULL) { - g_string_free(utf8_str, TRUE); - } - /* prevent a permanently large buffer */ - g_string_free(buffer, FALSE); - buffer = g_string_sized_new(BUFSIZ); - - return (retval); -} - -static gboolean -notify_timeout_cb(gpointer user_data) -{ - if (!send_notification()) { - g_warning("failed to send notification"); - } - - notify_timeout_id = 0; - - return (FALSE); -} - -static gboolean -master_pty_read_cb(GIOChannel *source, GIOCondition cond, gpointer user_data) -{ - gchar buf[BUFSIZ]; - GIOStatus status; - gsize buf_len; - GError *error = NULL; - - if ((cond & G_IO_IN) || (cond & G_IO_PRI)) { - /* read message from master pty */ - while ((status = g_io_channel_read_chars(source, buf, BUFSIZ, - &buf_len, &error)) == G_IO_STATUS_NORMAL) { - if (buf_len > 0) { - g_debug("read %" G_GSSIZE_FORMAT " bytes from " - "master pty", buf_len); - g_string_append_len(buffer, buf, - (gssize)buf_len); - } - } - if (error != NULL) { - g_critical("failed to read from master pty: %s", - error->message); - g_error_free(error); - g_main_loop_quit(loop); - return (FALSE); - } - - /* - * schedule a timeout for sending a notification with the - * buffered message - */ - if (notify_timeout_id <= 0) { - notify_timeout_id = g_timeout_add(BUFFER_TIMEOUT, - notify_timeout_cb, NULL); - } - } - - if ((cond & G_IO_ERR) || (cond & G_IO_HUP)) { - g_critical("connection to master pty broken"); - g_main_loop_quit(loop); - return (FALSE); - } - - return (TRUE); -} - -int -main(int argc, char *argv[]) -{ - int status = EXIT_FAILURE; - GError *error = NULL; - XWritedUnique *app = NULL; - GOptionContext *context = NULL; - struct sigaction sigact; - GIOChannel *signal_channel = NULL; - GIOChannel *master_pty_channel = NULL; - int masterfd = -1; - int slavefd = -1; - char *slave_name = NULL; - gboolean vflag = FALSE; - gboolean dflag = FALSE; - const GOptionEntry options[] = { - { "debug", 'd', 0, G_OPTION_ARG_NONE, &dflag, - N_("Show extra debugging information"), NULL }, - { "version", 'V', 0, G_OPTION_ARG_NONE, &vflag, - N_("Print the current version and exit"), NULL }, - { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, 0 } - }; - - setlocale(LC_ALL, ""); - bindtextdomain(PACKAGE, LOCALEDIR); - bind_textdomain_codeset(PACKAGE, "UTF-8"); - textdomain(PACKAGE); - -#if !GLIB_CHECK_VERSION(2, 35, 0) - /* deprecated in glib >= 2.35 */ - g_type_init(); -#endif - - context = g_option_context_new("- display write and wall messages as " - "desktop notifications"); - g_option_context_add_main_entries(context, options, PACKAGE); - g_option_context_set_translation_domain(context, PACKAGE); - g_option_context_parse(context, &argc, &argv, &error); - if (error != NULL) { - g_printerr("%s.\n", error->message); - g_error_free(error); - goto out; - } - - xwrited_debug_init(dflag); - - if (vflag) { - g_print("%s %s\n", PACKAGE, VERSION); - status = EXIT_SUCCESS; - goto out; - } - - app = xwrited_unique_new("org.guido-berhoerster.code.xwrited"); - if (app == NULL) { - g_critical("failed to initialize application"); - goto out; - } - if (!xwrited_unique_is_unique(app)) { - g_printerr(_("xwrited is already running in this session.\n")); - goto out; - } - - if (!notify_init(APP_NAME)) { - g_critical("failed to initialize libnotify"); - goto out; - } - - loop = g_main_loop_new(NULL, FALSE); - if (loop == NULL) { - g_critical("failed to create main loop"); - goto out; - } - - buffer = g_string_sized_new(BUFSIZ); - - /* open master pty */ - masterfd = posix_openpt(O_RDWR | O_NOCTTY); - if (masterfd == -1) { - g_critical("failed to open master pty: %s", g_strerror(errno)); - goto out; - } - - /* create slave pty */ - if ((grantpt(masterfd) == -1) || (unlockpt(masterfd) == -1)) { - g_critical("failed to create slave pty: %s", g_strerror(errno)); - goto out; - } - slave_name = ptsname(masterfd); - if (slave_name == NULL) { - g_critical("failed to obtain name of slave pty"); - goto out; - } - - /* - * keep an open fd around order to prevent closing the master fd when - * receiving an EOF - */ - slavefd = open(slave_name, O_RDWR); - if (slavefd == -1) { - g_critical("failed to open slave pty: %s", g_strerror(errno)); - goto out; - } - - /* create a GIOChannel for monitoring the master pty for messages */ - master_pty_channel = g_io_channel_unix_new(masterfd); - g_io_channel_set_flags(master_pty_channel, - g_io_channel_get_flags(master_pty_channel) | G_IO_FLAG_NONBLOCK, - &error); - if (error != NULL) { - g_critical("failed set flags on the master pty channel: %s", - error->message); - g_error_free(error); - goto out; - } - if (!g_io_add_watch(master_pty_channel, G_IO_IN | G_IO_PRI | G_IO_HUP | - G_IO_ERR, master_pty_read_cb, NULL)) { - g_critical("failed to add watch on signal channel"); - goto out; - } - - /* create pipe for delivering signals to a listener in the main loop */ - if (pipe(signal_pipe_fd) == -1) { - g_critical("failed to create signal pipe: %s", - g_strerror(errno)); - goto out; - } - if (fcntl(signal_pipe_fd[PIPE_W_FD], F_SETFL, O_NONBLOCK) == -1) { - g_critical("failed to set flags on signal pipe: %s", - g_strerror(errno)); - goto out; - } - - /* create GIO channel for reading from the signal_pipe */ - signal_channel = g_io_channel_unix_new(signal_pipe_fd[PIPE_R_FD]); - g_io_channel_set_encoding(signal_channel, NULL, &error); - if (error != NULL) { - g_critical("failed to set binary encoding for signal channel: " - "%s", error->message); - g_error_free(error); - goto out; - } - g_io_channel_set_buffered(signal_channel, FALSE); - g_io_channel_set_flags(signal_channel, - g_io_channel_get_flags(signal_channel) | G_IO_FLAG_NONBLOCK, - &error); - if (error != NULL) { - g_critical("failed set flags on signal channel: %s", - error->message); - g_error_free(error); - goto out; - } - if (g_io_add_watch(signal_channel, G_IO_IN | G_IO_PRI | G_IO_HUP | - G_IO_ERR, signal_read_cb, NULL) == 0) { - g_critical("failed to add watch on the signal channel"); - goto out; - } - - /* set up signal handler */ - sigact.sa_handler = on_signal; - sigact.sa_flags = SA_RESTART; - sigemptyset(&sigact.sa_mask); - if ((sigaction(SIGINT, &sigact, NULL) < 0) || - (sigaction(SIGTERM, &sigact, NULL) < 0) || - (sigaction(SIGQUIT, &sigact, NULL) < 0) || - (sigaction(SIGHUP, &sigact, NULL) < 0)) { - g_critical("failed to set up signal handler"); - goto out; - } - - xwrited_utmp_add_entry(masterfd); - - /* main loop */ - g_main_loop_run(loop); - - xwrited_utmp_remove_entry(masterfd); - - status = EXIT_SUCCESS; - -out: - if (context != NULL) { - g_option_context_free(context); - } - - if (signal_channel != NULL) { - g_io_channel_shutdown(signal_channel, FALSE, NULL); - g_io_channel_unref(signal_channel); - } - - if (signal_pipe_fd[PIPE_R_FD] != -1) { - close(signal_pipe_fd[PIPE_R_FD]); - } - if (signal_pipe_fd[PIPE_W_FD] != -1) { - close(signal_pipe_fd[PIPE_W_FD]); - } - - if (master_pty_channel != NULL) { - g_io_channel_shutdown(master_pty_channel, FALSE, NULL); - g_io_channel_unref(master_pty_channel); - } - - if (slavefd != -1) { - close(slavefd); - } - - if (masterfd != -1) { - close(masterfd); - } - - if (buffer != NULL) { - g_string_free(buffer, FALSE); - } - - if (app != NULL) { - g_object_unref(app); - } - - if (loop != NULL) { - g_main_loop_unref(loop); - } - - if (notify_is_initted()) { - notify_uninit(); - } - - exit(status); -} diff -r 4a5330979433 -r f0accfc74f7b org.guido-berhoerster.code.xwrited.desktop.in --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/org.guido-berhoerster.code.xwrited.desktop.in Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,8 @@ +[Desktop Entry] +Encoding=UTF-8 +Name=xwrited +Comment=Display write and wall messages as desktop notifications +Exec=xwrited +Terminal=false +Type=Application +Categories=System;Monitor; diff -r 4a5330979433 -r f0accfc74f7b po/LINGUAS --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/po/LINGUAS Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,2 @@ +de +id diff -r 4a5330979433 -r f0accfc74f7b po/POTFILES.in --- a/po/POTFILES.in Wed Sep 14 09:46:59 2016 +0200 +++ b/po/POTFILES.in Sat Jul 28 22:02:24 2018 +0200 @@ -1,6 +1,5 @@ -data/xwrited.desktop.in -main.c -xwrited-debug.c -xwrited-unique.c -xwrited-utmp-utempter.c -xwrited-utmp-utmpx.c +org.guido-berhoerster.code.xwrited.desktop.in +xwd-application.c +xwd-utmp-utempter.c +xwd-utmp-utmpx.c +xwrited.c diff -r 4a5330979433 -r f0accfc74f7b po/de.po --- a/po/de.po Wed Sep 14 09:46:59 2016 +0200 +++ b/po/de.po Sat Jul 28 22:02:24 2018 +0200 @@ -1,14 +1,14 @@ # German translations for xwrited package # German messages for xwrited. -# Copyright (C) 2011 Guido Berhoerster. +# Copyright (C) 2018 Guido Berhoerster. # This file is distributed under the same license as the xwrited package. -# Guido Berhoerster , 2011. +# Guido Berhoerster , 2018. # msgid "" msgstr "" "Project-Id-Version: xwrited 1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2011-05-20 16:48+0200\n" +"POT-Creation-Date: 2018-06-29 10:46+0000\n" "PO-Revision-Date: 2010-07-28 13:00+0200\n" "Last-Translator: Guido Berhoerster \n" "Language-Team: German\n" @@ -18,28 +18,26 @@ "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -#: ../data/xwrited.desktop.in.h:1 +#: org.guido-berhoerster.code.xwrited.desktop.in:4 msgid "xwrited" msgstr "xwrited" -#: ../data/xwrited.desktop.in.h:2 +#: org.guido-berhoerster.code.xwrited.desktop.in:5 msgid "Display write and wall messages as desktop notifications" msgstr "Zeigt write und wall Nachrichten as Desktop-Benachrichtigungen an" -#. show notification -#: ../main.c:179 +#: xwd-application.c:52 +msgid "Enable debugging messages" +msgstr "" + +#: xwd-application.c:54 +msgid "Quit running instance of xwrited" +msgstr "Beende laufende Instanz von xwrited" + +#: xwd-application.c:56 +msgid "Print the version number and quit" +msgstr "Aktuelle Version zeigen und beenden" + +#: xwd-application.c:252 msgid "Message received" msgstr "Nachricht erhalten" - -#: ../main.c:280 -msgid "Show extra debugging information" -msgstr "Zusätzliche Debugging-Informationen anzeigen" - -#: ../main.c:282 -msgid "Print the current version and exit" -msgstr "Aktuelle Version zeigen und beenden" - -#: ../main.c:321 -#, c-format -msgid "xwrited is already running in this session.\n" -msgstr "xwrited läuft bereits in dieser Sitzung.\n" diff -r 4a5330979433 -r f0accfc74f7b po/id.po --- a/po/id.po Wed Sep 14 09:46:59 2016 +0200 +++ b/po/id.po Sat Jul 28 22:02:24 2018 +0200 @@ -1,13 +1,13 @@ # Indonesian translations for xwrited package. -# Copyright (C) 2014 Guido Berhoerster +# Copyright (C) 2018 Guido Berhoerster # This file is distributed under the same license as the xwrited package. -# Guido Berhoerster , 2014. +# Guido Berhoerster , 2018. # msgid "" msgstr "" "Project-Id-Version: xwrited 1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2014-04-27 23:04+0200\n" +"POT-Creation-Date: 2018-06-29 10:46+0000\n" "PO-Revision-Date: 2014-04-27 21:01+0200\n" "Last-Translator: Guido Berhoerster \n" "Language-Team: Indonesian\n" @@ -16,28 +16,26 @@ "Content-Type: text/plain; charset=ASCII\n" "Content-Transfer-Encoding: 8bit\n" -#: ../data/xwrited.desktop.in.h:1 +#: org.guido-berhoerster.code.xwrited.desktop.in:4 msgid "xwrited" msgstr "xwrited" -#: ../data/xwrited.desktop.in.h:2 +#: org.guido-berhoerster.code.xwrited.desktop.in:5 msgid "Display write and wall messages as desktop notifications" msgstr "Tampilkan pesanan write dan wall sebagai notifikasi desktop" -#. show notification -#: ../main.c:179 +#: xwd-application.c:52 +msgid "Enable debugging messages" +msgstr "" + +#: xwd-application.c:54 +msgid "Quit running instance of xwrited" +msgstr "" + +#: xwd-application.c:56 +msgid "Print the version number and quit" +msgstr "Tampilkan versi kini dan keluar" + +#: xwd-application.c:252 msgid "Message received" msgstr "Pesanan diterima" - -#: ../main.c:280 -msgid "Show extra debugging information" -msgstr "Tampilkan informasi debug tambahan" - -#: ../main.c:282 -msgid "Print the current version and exit" -msgstr "Tampilkan versi kini dan keluar" - -#: ../main.c:321 -#, c-format -msgid "xwrited is already running in this session.\n" -msgstr "xwrited sudah berjalan pada sesi ini.\n" diff -r 4a5330979433 -r f0accfc74f7b xwd-application.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xwd-application.c Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,567 @@ +/* + * Copyright (C) 2018 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "xwd-application.h" +#include "xwd-utmp.h" + +#define BUFFER_TIMEOUT 500 /* ms */ + +struct _XwdApplication { + GApplication parent_instance; + gint masterfd; + gint slavefd; + GIOChannel *master_pty_chan; + guint buffer_timeout_id; + GString *message_buf; +}; + +G_DEFINE_TYPE(XwdApplication, xwd_application, G_TYPE_APPLICATION) + +static void xwd_application_quit(GSimpleAction *, GVariant *, gpointer); + +static const GOptionEntry cmd_options[] = { + { "debug", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, + N_("Enable debugging messages"), NULL }, + { "quit", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, + N_("Quit running instance of xwrited"), NULL }, + { "version", 'V', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, + N_("Print the version number and quit"), NULL }, + { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, NULL, NULL, NULL }, + { NULL } +}; + +static const GActionEntry xwd_application_actions[] = { + { "quit", xwd_application_quit } +}; + +static void +xwd_application_quit(GSimpleAction *simple, GVariant *parameter, + gpointer user_data) +{ + XwdApplication *self = user_data; + + g_application_quit(G_APPLICATION(self)); +} + +static gboolean +on_signal(gpointer user_data) +{ + XwdApplication *self = user_data; + + g_debug("received signal, exiting"); + g_action_group_activate_action(G_ACTION_GROUP(G_APPLICATION(self)), + "quit", NULL); + + return (TRUE); +} + +static GString * +string_copy(GString *dest, GString *src) +{ + g_string_truncate(dest, 0); + g_string_append(dest, src->str); + + return (dest); +} + +static void +string_to_valid_utf8(GString *string) +{ + GString *result; + gchar *start = string->str; + gchar *end; + gsize remaining = string->len; + + result = g_string_sized_new(string->len); + + while (remaining > 0) { + if (g_utf8_validate(start, remaining, (const gchar **)&end)) { + /* remaining part is valid */ + g_string_append_len(result, start, remaining); + break; + } + + /* append valid part */ + g_string_append_len(result, start, end - start); + /* + * replace first invalid byte with Unicode "REPLACEMENT + * CHARACTER" (U+FFFD) + */ + g_string_append(result, "\357\277\275"); + remaining -= (end - start) + 1; + start = end + 1; + } + + string_copy(string, result); + g_string_free(result, TRUE); +} + +static void +string_trim_lines(GString *string) +{ + GString *result; + gchar *p = string->str; + gchar *q; + gsize line_len; + gsize remaining = string->len; + + result = g_string_sized_new(string->len); + + while (remaining > 0) { + q = memchr(p, '\n', remaining); + if (q == NULL) { + g_string_append_len(result, p, remaining); + break; + } + line_len = q - p - 1; + /* convert \r\n to \n */ + if ((line_len > 0) && (p[line_len - 1] == '\r')) { + line_len--; + } + /* trim spaces on the right */ + while ((line_len > 0) && (p[line_len - 1] == ' ')) { + line_len--; + } + g_string_append_len(result, p, line_len); + g_string_append_c(result, '\n'); + remaining -= q + 1 - p; + p = q + 1; + } + + string_copy(string, result); + g_string_free(result, TRUE); +} + +static void +string_filter_nonprintable(GString *string) +{ + GString *result; + const gchar *p; + gunichar c; + + result = g_string_sized_new(string->len); + + for (p = string->str; *p != '\0'; p = g_utf8_next_char(p)) { + c = g_utf8_get_char(p); + if (g_unichar_isprint(c) || g_unichar_isspace(c)) { + g_string_append_unichar(result, c); + } + } + + string_copy(string, result); + g_string_free(result, TRUE); +} + +static gchar * +hexdumpa(const void *mem, gsize n) +{ + const guchar *bytes = mem; + GString *string; + gsize i; + gsize j; + + string = g_string_sized_new((n / 16 + (n % 16 > 0)) * 76); + + for (i = 0; i < n; i += 16) { + g_string_append_printf(string, "%08zx ", i); + + for (j = 0; (i + j < n) && (j < 16); j++) { + g_string_append_printf(string, " %02x", bytes[i + j]); + } + for (; j < 16; j++) { + g_string_append(string, " "); + } + + g_string_append(string, " "); + for (j = 0; (i + j < n) && (j < 16); j++) { + g_string_append_printf(string, "%c", + g_ascii_isprint(bytes[i + j]) ? bytes[i + j] : '.'); + } + + g_string_append_c(string, '\n'); + } + + return (g_string_free(string, FALSE)); +} + +static void +display_message(XwdApplication *self) +{ + gboolean enable_debug_logging; + gchar *message_dump; + GString *message; + GIcon *icon; + GNotification *notification; + + if (self->message_buf->len == 0) { + return; + } + + enable_debug_logging = (g_getenv("G_MESSAGES_DEBUG") != NULL); + if (enable_debug_logging) { + message_dump = hexdumpa(self->message_buf->str, + self->message_buf->len); + g_debug("raw message:\n%s", message_dump); + g_free(message_dump); + } + + /* + * There is no reliable way to determine the character encoding of the + * received message which, depending on the locale of the sender, may + * even differ for different messages. A user could even send binary + * data. It is thus assumed that messages are in UTF-8 encoding which + * should be the most common case on modern systems. Any invalid + * sequences are replaced with the Unicode "REPLACEMENT CHARACTER" + * (U+FFFD) and non-printable characters are removed. Additionally, + * padding typically added by wall(1) implementations is removed in + * order to improve readability. + */ + message = g_string_new_len(self->message_buf->str, + self->message_buf->len); + string_to_valid_utf8(message); + string_filter_nonprintable(message); + string_trim_lines(message); + + if (enable_debug_logging) { + message_dump = hexdumpa(message->str, message->len); + g_debug("message:\n%s", message_dump); + g_free(message_dump); + } + + icon = g_themed_icon_new("utilities-terminal"); + + notification = g_notification_new(_("Message received")); + g_notification_set_icon(notification, icon); + g_notification_set_body(notification, message->str); + g_application_send_notification(G_APPLICATION(self), NULL, + notification); + + g_object_unref(notification); + g_object_unref(icon); + g_string_free(message, TRUE); + g_string_truncate(self->message_buf, 0); +} + +static gboolean +on_buffer_timeout(gpointer user_data) +{ + XwdApplication *self = user_data; + + display_message(self); + + self->buffer_timeout_id = 0; + + return (FALSE); +} + +static gboolean +on_master_pty_readable(GIOChannel *source, GIOCondition cond, + gpointer user_data) +{ + XwdApplication *self = user_data; + GIOStatus status; + gchar buf[BUFSIZ]; + gsize buf_len = 0; + GError *error = NULL; + + if (cond & G_IO_IN) { + /* read message data from master pty */ + memset(buf, 0, sizeof (buf)); + while ((status = g_io_channel_read_chars(source, (gchar *)&buf, + sizeof (buf), &buf_len, &error)) == G_IO_STATUS_NORMAL) { + if (buf_len > 0) { + g_debug("read %" G_GSSIZE_FORMAT " bytes from " + "master pty", buf_len); + g_string_append_len(self->message_buf, buf, + buf_len); + } + } + if (error != NULL) { + g_critical("failed to read from master pty: %s", + error->message); + g_error_free(error); + return (FALSE); + } + + /* + * a message might be read in several parts and it is not + * possible to reliably detect the beginning or end of a + * message, so buffer read data until a short timeout is + * reached as this works well for a single message which should + * be the most common case + */ + if (self->buffer_timeout_id == 0) { + self->buffer_timeout_id = g_timeout_add(BUFFER_TIMEOUT, + on_buffer_timeout, self); + } + } + + if (cond & (G_IO_HUP | G_IO_ERR)) { + g_critical("connection to master pty broken"); + return (FALSE); + } + + return (TRUE); +} + +static void +xwd_application_startup(GApplication *application) +{ + XwdApplication *self = XWD_APPLICATION(application); + gchar *slave_name; + GIOFlags flags; + GError *error = NULL; + + G_APPLICATION_CLASS(xwd_application_parent_class)->startup(application); + + /* create actions */ + g_action_map_add_action_entries(G_ACTION_MAP(self), + xwd_application_actions, G_N_ELEMENTS(xwd_application_actions), + self); + + /* create signal watchers */ + g_unix_signal_add(SIGINT, on_signal, self); + g_unix_signal_add(SIGTERM, on_signal, self); + g_unix_signal_add(SIGHUP, on_signal, self); + + /* open master pty */ + self->masterfd = posix_openpt(O_RDWR | O_NOCTTY); + if (self->masterfd == -1) { + g_critical("failed to open master pty: %s", g_strerror(errno)); + return; + } + + /* create slave pty */ + if ((grantpt(self->masterfd) == -1) || + (unlockpt(self->masterfd) == -1)) { + g_critical("failed to create slave pty: %s", g_strerror(errno)); + return; + } + slave_name = ptsname(self->masterfd); + if (slave_name == NULL) { + g_critical("failed to obtain name of slave pty"); + return; + } + + /* + * keep an open fd around order to prevent closing the master fd when + * receiving an EOF + */ + self->slavefd = open(slave_name, O_RDWR); + if (self->slavefd == -1) { + g_critical("failed to open slave pty: %s", g_strerror(errno)); + return; + } + + /* create a GIOChannel for monitoring the master pty for messages */ + self->master_pty_chan = g_io_channel_unix_new(self->masterfd); + /* make it non-blocking */ + flags = g_io_channel_get_flags(self->master_pty_chan); + if (g_io_channel_set_flags(self->master_pty_chan, + flags | G_IO_FLAG_NONBLOCK, &error) != G_IO_STATUS_NORMAL) { + g_critical("failed set flags on the master pty channel: %s", + error->message); + g_error_free(error); + return; + } + /* make the channel safe for encodings other than UTF-8 */ + if (g_io_channel_set_encoding(self->master_pty_chan, NULL, &error) != + G_IO_STATUS_NORMAL) { + g_critical("failed set encoding on the master pty channel: %s", + error->message); + g_error_free(error); + return; + } + if (!g_io_add_watch(self->master_pty_chan, + G_IO_IN | G_IO_HUP | G_IO_ERR, on_master_pty_readable, self)) { + g_critical("failed to add watch on master pty channel"); + return; + } + + xwd_utmp_add_entry(self->masterfd); + + /* keep GApplication running */ + g_application_hold(application); +} + +static void +xwd_application_shutdown(GApplication *application) +{ + XwdApplication *self = XWD_APPLICATION(application); + GApplicationClass *application_class = + G_APPLICATION_CLASS(xwd_application_parent_class); + + /* display any buffered data before exiting */ + display_message(self); + + if (self->master_pty_chan != NULL) { + g_io_channel_shutdown(self->master_pty_chan, FALSE, NULL); + g_clear_pointer(&self->master_pty_chan, + (GDestroyNotify)g_io_channel_unref); + } + + if (self->slavefd != -1) { + close(self->slavefd); + self->slavefd = -1; + } + + if (self->masterfd != -1) { + close(self->masterfd); + self->masterfd = -1; + } + + xwd_utmp_remove_entry(self->masterfd); + + /* remove signal watches and buffer timeout */ + while (g_source_remove_by_user_data(self)) { + continue; + } + self->buffer_timeout_id = 0; + + application_class->shutdown(application); +} + +static gint +xwd_application_handle_local_options(GApplication *application, + GVariantDict *options) +{ + gchar **args = NULL; + gchar *messages_debug; + GError *error = NULL; + + /* filename arguments are not allowed */ + if (g_variant_dict_lookup(options, G_OPTION_REMAINING, "^a&ay", + &args)) { + g_printerr("invalid argument: \"%s\"\n", args[0]); + g_free(args); + return (1); + } + + if (g_variant_dict_contains(options, "version")) { + g_print("%s %s\n", PACKAGE, VERSION); + + /* quit */ + return (0); + } + + if (g_variant_dict_contains(options, "debug")) { + /* enable debug logging */ + messages_debug = g_strjoin(":", G_LOG_DOMAIN, + g_getenv("G_MESSAGES_DEBUG"), NULL); + g_setenv("G_MESSAGES_DEBUG", messages_debug, TRUE); + g_free(messages_debug); + } + + /* + * register with the session bus so that it is possible to discern + * between remote and primary instance and that remote actions can be + * invoked, this causes the startup signal to be emitted which, in case + * of the primary instance, starts to instantiate the + * backend with the given values + */ + if (!g_application_register(application, NULL, &error)) { + g_critical("g_application_register: %s", error->message); + g_error_free(error); + return (1); + } + + if (g_variant_dict_contains(options, "quit")) { + /* only valid if a remote instance is running */ + if (!g_application_get_is_remote(application)) { + g_printerr("%s is not running\n", g_get_prgname()); + return (1); + } + + /* signal remote instance to quit */ + g_action_group_activate_action(G_ACTION_GROUP(application), + "quit", NULL); + + /* quit local instance */ + return (0); + } + + /* proceed with default command line processing */ + return (-1); +} + +static void +xwd_application_activate(GApplication *application) { + GApplicationClass *application_class = + G_APPLICATION_CLASS(xwd_application_parent_class); + + /* do nothing, implementation required by GApplication */ + + application_class->activate(application); +} + +static void +xwd_application_finalize(GObject *object) +{ + XwdApplication *self = XWD_APPLICATION(object); + + g_string_free(self->message_buf, TRUE); + + G_OBJECT_CLASS(xwd_application_parent_class)->finalize(object); +} + +static void +xwd_application_class_init(XwdApplicationClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS(klass); + GApplicationClass *application_class = G_APPLICATION_CLASS(klass); + + object_class->finalize = xwd_application_finalize; + + application_class->startup = xwd_application_startup; + application_class->shutdown = xwd_application_shutdown; + application_class->handle_local_options = + xwd_application_handle_local_options; + application_class->activate = xwd_application_activate; +} + +static void +xwd_application_init(XwdApplication *self) +{ + g_application_add_main_option_entries(G_APPLICATION(self), + cmd_options); + + self->masterfd = -1; + self->slavefd = -1; + + self->message_buf = g_string_sized_new(BUFSIZ); +} + +XwdApplication * +xwd_application_new(void) +{ + return (g_object_new(XWD_TYPE_APPLICATION, "application-id", + APPLICATION_ID, NULL)); +} diff -r 4a5330979433 -r f0accfc74f7b xwd-application.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xwd-application.h Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2018 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef XWD_APPLICATION_H +#define XWD_APPLICATION_H + +#include +#include + +G_BEGIN_DECLS + +#define XWD_TYPE_APPLICATION (xwd_application_get_type()) + +G_DECLARE_FINAL_TYPE(XwdApplication, xwd_application, XWD, + APPLICATION, GApplication) + +XwdApplication * xwd_application_new(void); + +G_END_DECLS + +#endif /* !XWD_APPLICATION_H */ diff -r 4a5330979433 -r f0accfc74f7b xwd-utmp-utempter.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xwd-utmp-utempter.c Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2010 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +void +xwd_utmp_add_entry(int fd) +{ + char *pty_name; + + pty_name = ptsname(fd); + addToUtmp(pty_name, NULL, fd); +} + +void +xwd_utmp_remove_entry(int fd) +{ + char *pty_name; + + pty_name = ptsname(fd); + removeLineFromUtmp(pty_name, fd); +} diff -r 4a5330979433 -r f0accfc74f7b xwd-utmp-utmpx.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xwd-utmp-utmpx.c Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2010 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef DEV_PREFIX +#define DEV_PREFIX "/dev/" +#endif /* !DEV_PREFIX */ + +static void +utmp_write_entry(int fd, gboolean add) +{ + struct utmpx utmpx; + char *line = NULL; + size_t line_len; + char *id; + struct passwd *pwd; + + line = ptsname(fd); + if (line == NULL) { + g_critical("failed to obtain slave pty name"); + return; + } + if (g_str_has_prefix(line, DEV_PREFIX)) { + line += strlen(DEV_PREFIX); + } + + line_len = strlen(line); + id = (line_len >= sizeof (utmpx.ut_pid)) ? + line + (line_len - sizeof (utmpx.ut_pid)) : + line; + + pwd = getpwuid(getuid()); + if (pwd == NULL) { + g_critical("failed to get username: %s", g_strerror(errno)); + return; + } + + memset(&utmpx, 0, sizeof (utmpx)); + strncpy(utmpx.ut_name, pwd->pw_name, sizeof (utmpx.ut_name)); + strncpy(utmpx.ut_id, id, sizeof (utmpx.ut_id)); + strncpy(utmpx.ut_line, line, sizeof (utmpx.ut_line)); + utmpx.ut_pid = getpid(); + utmpx.ut_type = add ? USER_PROCESS : DEAD_PROCESS; + gettimeofday(&utmpx.ut_tv, NULL); + + setutxent(); + if (pututxline(&utmpx) == NULL) { + g_critical("failed to write to utmpx database: %s", + g_strerror(errno)); + return; + } + endutxent(); +} + +void +xwd_utmp_add_entry(int fd) +{ + utmp_write_entry(fd, TRUE); +} + +void +xwd_utmp_remove_entry(int fd) +{ + utmp_write_entry(fd, FALSE); +} diff -r 4a5330979433 -r f0accfc74f7b xwd-utmp.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xwd-utmp.h Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2010 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef XWD_UTMP_H +#define XWD_UTMP_H + +#include + +G_BEGIN_DECLS + +void xwd_utmp_add_entry(int); +void xwd_utmp_remove_entry(int); + +G_END_DECLS + +#endif /* XWD_UTMP_H */ diff -r 4a5330979433 -r f0accfc74f7b xwrited-debug.c --- a/xwrited-debug.c Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2014 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include -#include - -#include "xwrited-debug.h" - -#if !GLIB_CHECK_VERSION(2, 32, 0) -static void -dummy_log_handler(const gchar *log_domain, GLogLevelFlags log_level, - const gchar *message, gpointer data) -{ - /* drop all messages */ -} -#endif /* !GLIB_CHECK_VERSION (2,32,0) */ - -void -xwrited_debug_init(gboolean debug_mode) -{ - /* - * glib >= 2.32 only shows debug messages if the G_MESSAGES_DEBUG - * environment variable contains the log domain or "all", earlier glib - * version always show debugging output - */ -#if GLIB_CHECK_VERSION(2, 32, 0) - const gchar *debug_env; - gchar *debug_env_new; - - if (debug_mode) { - debug_env = g_getenv("G_MESSAGES_DEBUG"); - - if (debug_env == NULL) { - g_setenv("G_MESSAGES_DEBUG", G_LOG_DOMAIN, TRUE); - } else if (strstr(debug_env, G_LOG_DOMAIN) == NULL) { - debug_env_new = g_strdup_printf("%s %s", debug_env, - G_LOG_DOMAIN); - g_setenv("G_MESSAGES_DEBUG", debug_env_new, TRUE); - g_free(debug_env_new); - } - } -#else - if (!debug_mode) { - g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, - dummy_log_handler, NULL); - } -#endif /* GLIB_CHECK_VERSION (2,32,0) */ -} diff -r 4a5330979433 -r f0accfc74f7b xwrited-debug.h --- a/xwrited-debug.h Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2011 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef XWRITED_DEBUG_H -#define XWRITED_DEBUG_H - -#include - -G_BEGIN_DECLS - -void xwrited_debug_init(gboolean); - -G_END_DECLS - -#endif /* XWRITED_DEBUG_H */ diff -r 4a5330979433 -r f0accfc74f7b xwrited-unique.c --- a/xwrited-unique.c Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,288 +0,0 @@ -/* - * Copyright (C) 2014 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#ifdef HAVE_GLIB_GDBUS -#include -#else -#include -#endif /* HAVE_GLIB_GDBUS */ -#include - -#include "xwrited-unique.h" - -G_DEFINE_TYPE(XWritedUnique, xwrited_unique, G_TYPE_OBJECT) - -#define XWRITED_UNIQUE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ - XWRITED_TYPE_UNIQUE, XWritedUniquePrivate)) - -struct _XWritedUniquePrivate { -#ifdef HAVE_GLIB_GDBUS - GDBusProxy *session_bus_proxy; -#else - DBusGConnection *session_bus; - DBusGProxy *session_bus_proxy; -#endif /* HAVE_GLIB_GDBUS */ - gchar *name; - gboolean is_unique; -}; - -enum { - PROP_0, - PROP_NAME, - PROP_IS_XWRITED_UNIQUE -}; - -static gboolean -request_name(XWritedUnique *self) -{ - guint32 request_name_response; - GError *error = NULL; -#ifdef HAVE_GLIB_GDBUS - GVariant *result; - - g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE); - - result = g_dbus_proxy_call_sync(self->priv->session_bus_proxy, - "RequestName", g_variant_new("(su)", self->priv->name, - DBUS_NAME_FLAG_DO_NOT_QUEUE), G_DBUS_CALL_FLAGS_NONE, -1, NULL, - &error); - if (result == NULL) { - g_warning("failed to acquire service name \"%s\": %s", - self->priv->name, error->message); - g_error_free(error); - return (FALSE); - } - - g_variant_get(result, "(u)", &request_name_response); - g_variant_unref(result); -#else - - g_return_val_if_fail(self->priv->session_bus != NULL, FALSE); - g_return_val_if_fail(self->priv->session_bus_proxy != NULL, FALSE); - if (dbus_g_proxy_call(self->priv->session_bus_proxy, "RequestName", - &error, G_TYPE_STRING, self->priv->name, G_TYPE_UINT, - DBUS_NAME_FLAG_DO_NOT_QUEUE, G_TYPE_INVALID, G_TYPE_UINT, - &request_name_response, G_TYPE_INVALID) == 0) { - g_warning("failed to acquire service name \"%s\": %s", - self->priv->name, error->message); - g_error_free(error); - return (FALSE); - } -#endif /* HAVE_GLIB_GDBUS */ - - switch (request_name_response) { - case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: - return (TRUE); - case DBUS_REQUEST_NAME_REPLY_EXISTS: - break; - default: - g_warning("failed to acquire service name \"%s\"", - self->priv->name); - } - - return (FALSE); -} - -static void -xwrited_unique_get_property(GObject *gobject, guint property_id, GValue *value, - GParamSpec *pspec) -{ - XWritedUnique *app = XWRITED_UNIQUE(gobject); - - switch (property_id) { - case PROP_NAME: - g_value_set_string(value, app->priv->name); - break; - case PROP_IS_XWRITED_UNIQUE: - g_value_set_boolean(value, app->priv->is_unique); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, - pspec); - } -} - -static void -xwrited_unique_set_property(GObject *gobject, guint property_id, - const GValue *value, GParamSpec *pspec) -{ - XWritedUnique *app = XWRITED_UNIQUE(gobject); - - switch (property_id) { - case PROP_NAME: - app->priv->name = g_strdup(g_value_get_string(value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, - pspec); - } -} - -static GObject * -xwrited_unique_constructor(GType gtype, guint n_params, - GObjectConstructParam *params) -{ - GObjectClass *parent_class; - GObject *gobject; - XWritedUnique *app; - GError *error = NULL; - - parent_class = G_OBJECT_CLASS(xwrited_unique_parent_class); - gobject = parent_class->constructor(gtype, n_params, params); - app = XWRITED_UNIQUE(gobject); - -#ifdef HAVE_GLIB_GDBUS - app->priv->session_bus_proxy = - g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, - G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | - G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, DBUS_SERVICE_DBUS, - DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, NULL, &error); - if (app->priv->session_bus_proxy == NULL) { - g_warning("failed to create DBus proxy: %s", error->message); - g_error_free(error); - goto out; - } -#else - app->priv->session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); - if (app->priv->session_bus == NULL) { - g_warning("failed to connect to DBus session bus: %s", - error->message); - g_error_free(error); - goto out; - } - - app->priv->session_bus_proxy = - dbus_g_proxy_new_for_name(app->priv->session_bus, - DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); - if (app->priv->session_bus_proxy == NULL) { - g_warning("failed to create DBus proxy"); - goto out; - } -#endif /* HAVE_GLIB_GDBUS */ - - if (request_name(app)) { - app->priv->is_unique = TRUE; - } - -out: - return (gobject); -} - -static void -xwrited_unique_dispose(GObject *gobject) -{ - XWritedUnique *self = XWRITED_UNIQUE(gobject); - - if (self->priv->session_bus_proxy != NULL) { - g_object_unref(self->priv->session_bus_proxy); - self->priv->session_bus_proxy = NULL; - } - -#ifndef HAVE_GLIB_GDBUS - if (self->priv->session_bus != NULL) { - dbus_g_connection_unref(self->priv->session_bus); - self->priv->session_bus = NULL; - } - -#endif /* !HAVE_GLIB_GDBUS */ - G_OBJECT_CLASS(xwrited_unique_parent_class)->dispose(gobject); -} - -static void -xwrited_unique_finalize(GObject *gobject) -{ - XWritedUnique *self = XWRITED_UNIQUE(gobject); - - g_free(self->priv->name); - - G_OBJECT_CLASS(xwrited_unique_parent_class)->finalize(gobject); -} - -static void -xwrited_unique_class_init(XWritedUniqueClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS(klass); - GParamSpec *pspec; - - gobject_class->constructor = xwrited_unique_constructor; - gobject_class->get_property = xwrited_unique_get_property; - gobject_class->set_property = xwrited_unique_set_property; - gobject_class->dispose = xwrited_unique_dispose; - gobject_class->finalize = xwrited_unique_finalize; - - pspec = g_param_spec_string("name", "Name", - "The unique name of the application", NULL, G_PARAM_READABLE | - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | - G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB); - g_object_class_install_property(gobject_class, PROP_NAME, pspec); - - pspec = g_param_spec_boolean("is-unique", "Is unique", - "Whether the current application instance is unique", FALSE, - G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | - G_PARAM_STATIC_BLURB); - g_object_class_install_property(gobject_class, PROP_IS_XWRITED_UNIQUE, - pspec); - - g_type_class_add_private(klass, sizeof (XWritedUniquePrivate)); -} - -static void -xwrited_unique_init(XWritedUnique *self) -{ - self->priv = XWRITED_UNIQUE_GET_PRIVATE(self); - - self->priv->is_unique = FALSE; -#ifndef HAVE_GLIB_GDBUS - self->priv->session_bus = NULL; -#endif /* !HAVE_GLIB_GDBUS */ - self->priv->session_bus_proxy = NULL; -} - -XWritedUnique * -xwrited_unique_new(const gchar *name) -{ - XWritedUnique *app; - - g_return_val_if_fail(name != NULL, NULL); - - app = g_object_new(XWRITED_TYPE_UNIQUE, "name", name, NULL); - if ( -#ifndef HAVE_GLIB_GDBUS - app->priv->session_bus == NULL || -#endif /* !HAVE_GLIB_GDBUS */ - app->priv->session_bus_proxy == NULL) { - g_object_unref(app); - return (NULL); - } - - return (app); -} - -gboolean -xwrited_unique_is_unique(XWritedUnique *self) -{ - g_return_val_if_fail(XWRITED_IS_UNIQUE(self), FALSE); - - return (self->priv->is_unique); -} diff -r 4a5330979433 -r f0accfc74f7b xwrited-unique.h --- a/xwrited-unique.h Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2011 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef XWRITED_UNIQUE_H -#define XWRITED_UNIQUE_H - -#include -#include - -G_BEGIN_DECLS - -#define XWRITED_TYPE_UNIQUE (xwrited_unique_get_type()) -#define XWRITED_UNIQUE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \ - XWRITED_TYPE_UNIQUE, XWritedUnique)) -#define XWRITED_IS_UNIQUE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ - XWRITED_TYPE_UNIQUE)) -#define XWRITED_UNIQUE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), \ - XWRITED_TYPE_UNIQUE, XWritedUniqueClass)) -#define XWRITED_IS_UNIQUE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), \ - XWRITED_TYPE_UNIQUE)) -#define XWRITED_UNIQUE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \ - XWRITED_TYPE_UNIQUE, XWritedUniqueClass)) - -typedef struct _XWritedUnique XWritedUnique; -typedef struct _XWritedUniqueClass XWritedUniqueClass; -typedef struct _XWritedUniquePrivate XWritedUniquePrivate; - -struct _XWritedUnique { - GObject parent_instance; - XWritedUniquePrivate *priv; -}; - -struct _XWritedUniqueClass { - GObjectClass parent_class; -}; - -GType xwrited_unique_get_type(void) G_GNUC_CONST; -gboolean xwrited_unique_is_unique(XWritedUnique *); -XWritedUnique * xwrited_unique_new(const gchar *); - -G_END_DECLS - -#endif /* XWRITED_UNIQUE_H */ diff -r 4a5330979433 -r f0accfc74f7b xwrited-utmp-utempter.c --- a/xwrited-utmp-utempter.c Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2010 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include - -void -xwrited_utmp_add_entry(int fd) -{ - char *pty_name; - - pty_name = ptsname(fd); - addToUtmp(pty_name, NULL, fd); -} - -void -xwrited_utmp_remove_entry(int fd) -{ - char *pty_name; - - pty_name = ptsname(fd); - removeLineFromUtmp(pty_name, fd); -} diff -r 4a5330979433 -r f0accfc74f7b xwrited-utmp-utmpx.c --- a/xwrited-utmp-utmpx.c Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2010 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef DEV_PREFIX -#define DEV_PREFIX "/dev/" -#endif /* !DEV_PREFIX */ - -static void -utmp_write_entry(int fd, gboolean add) -{ - struct utmpx utmpx; - char *line = NULL; - size_t line_len; - char *id; - struct passwd *pwd; - - line = ptsname(fd); - if (line == NULL) { - g_critical("failed to obtain slave pty name"); - return; - } - if (g_str_has_prefix(line, DEV_PREFIX)) { - line += strlen(DEV_PREFIX); - } - - line_len = strlen(line); - id = (line_len >= sizeof (utmpx.ut_pid)) ? - line + (line_len - sizeof (utmpx.ut_pid)) : - line; - - pwd = getpwuid(getuid()); - if (pwd == NULL) { - g_critical("failed to get username: %s", g_strerror(errno)); - return; - } - - memset(&utmpx, 0, sizeof (utmpx)); - strncpy(utmpx.ut_name, pwd->pw_name, sizeof (utmpx.ut_name)); - strncpy(utmpx.ut_id, id, sizeof (utmpx.ut_id)); - strncpy(utmpx.ut_line, line, sizeof (utmpx.ut_line)); - utmpx.ut_pid = getpid(); - utmpx.ut_type = add ? USER_PROCESS : DEAD_PROCESS; - gettimeofday(&utmpx.ut_tv, NULL); - - setutxent(); - if (pututxline(&utmpx) == NULL) { - g_critical("failed to write to utmpx database: %s", - g_strerror(errno)); - return; - } - endutxent(); -} - -void -xwrited_utmp_add_entry(int fd) -{ - utmp_write_entry(fd, TRUE); -} - -void -xwrited_utmp_remove_entry(int fd) -{ - utmp_write_entry(fd, FALSE); -} diff -r 4a5330979433 -r f0accfc74f7b xwrited-utmp.h --- a/xwrited-utmp.h Wed Sep 14 09:46:59 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2010 Guido Berhoerster - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef XWRITED_UTMP_H -#define XWRITED_UTMP_H - -#include - -G_BEGIN_DECLS - -void xwrited_utmp_add_entry(int); -void xwrited_utmp_remove_entry(int); - -G_END_DECLS - -#endif /* XWRITED_UTMP_H */ diff -r 4a5330979433 -r f0accfc74f7b xwrited.1.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xwrited.1.xml Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,141 @@ + + + + + + + Guido + Berhoerster + + guido+xwrited@berhoerster.name + + + 29 June, 2018 + + + xwrited + 1 + + + User Commands + + + xwrited + display write and wall messages as desktop + notifications + + + + xwrited + + + + + + + + + + + + + + + + + + + + Description + The xwrited utility displays + write1 + and + wall1 + messages as desktop notifications. A notification daemon + compliant to the freedesktop.org Desktop Notification Specification draft + needs to be running in order to display the notifications. + + + Options + The following options are supported: + + + + + + + + + + Quit the running instance of + xwrited. + + + + + + + + + + + Print the version number and exit. + + + + + + Exit Status + The following exit values are returned: + + + 0 + + Command successfully executed. + + + + > 0 + + An error has occured. + + + + + + See Also + write + 1, + wall1 + + + + Notes + xwrited assumes that messages are encoded in UTF-8 because there is + no way for it to determine the character set encoding of the received + data. + + diff -r 4a5330979433 -r f0accfc74f7b xwrited.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xwrited.c Sat Jul 28 22:02:24 2018 +0200 @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 Guido Berhoerster + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include + +#include "xwd-application.h" + +int +main(int argc, char *argv[]) +{ + int status; + XwdApplication *application; + + bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR); + setlocale(LC_ALL, ""); + bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); + textdomain(GETTEXT_PACKAGE); + + application = xwd_application_new(); + status = g_application_run(G_APPLICATION(application), argc, argv); + g_object_unref(application); + + exit(status); +}