aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-08-12 20:10:48 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-08-12 20:10:48 +0100
commitfcb2dbd80c8adc5581b4e0e92046d8f5b11d1147 (patch)
tree0265921f78272730c2038bdcaad9682862892bc5
parentc7cbdf0139cae2445b62df2af5f7a64b4ab925dc (diff)
downloadimv-fcb2dbd80c8adc5581b4e0e92046d8f5b11d1147.tar.gz
Add imv-msg command
-rw-r--r--Makefile21
-rw-r--r--src/imv_msg.c42
-rw-r--r--src/ipc.c17
-rw-r--r--src/ipc.h4
-rw-r--r--src/ipc_common.c14
5 files changed, 80 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index e81eb47..7b16be3 100644
--- a/Makefile
+++ b/Makefile
@@ -19,13 +19,14 @@ override LIBS := -lGL -lpthread -lxkbcommon $(shell pkg-config --libs pangocairo
BUILDDIR ?= build
TARGET_WL = $(BUILDDIR)/imv-wl
TARGET_X11 = $(BUILDDIR)/imv-x11
+TARGET_MSG = $(BUILDDIR)/imv-msg
ifeq ($(WINDOWS),wayland)
- TARGETS := $(TARGET_WL)
+ TARGETS := $(TARGET_WL) $(TARGET_MSG)
else ifeq ($(WINDOWS),x11)
- TARGETS := $(TARGET_X11)
+ TARGETS := $(TARGET_X11) $(TARGET_MSG)
else ifeq ($(WINDOWS),all)
- TARGETS := $(TARGET_WL) $(TARGET_X11)
+ TARGETS := $(TARGET_WL) $(TARGET_X11) $(TARGET_MSG)
endif
SOURCES := src/main.c
@@ -39,6 +40,7 @@ SOURCES += src/image.c
SOURCES += src/imv.c
SOURCES += src/ini.c
SOURCES += src/ipc.c
+SOURCES += src/ipc_common.c
SOURCES += src/keyboard.c
SOURCES += src/list.c
SOURCES += src/log.c
@@ -51,6 +53,9 @@ WL_LIBS = -lwayland-client -lwayland-egl -lEGL
X11_SOURCES = src/x11_window.c
X11_LIBS = -lX11 -lGL -lGLU
+MSG_SOURCES = src/imv_msg.c src/ipc_common.c
+MSG_LIBS =
+
# Add backends to build as configured
ifeq ($(BACKEND_FREEIMAGE),yes)
SOURCES += src/backend_freeimage.c
@@ -88,6 +93,7 @@ TEST_SOURCES := test/list.c test/navigator.c
OBJECTS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(SOURCES))
WL_OBJECTS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(WL_SOURCES))
X11_OBJECTS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(X11_SOURCES))
+MSG_OBJECTS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(MSG_SOURCES))
TESTS := $(patsubst test/%.c,$(BUILDDIR)/test_%,$(TEST_SOURCES))
@@ -100,12 +106,15 @@ TLIBS := $(LIBS) $(shell pkg-config --libs cmocka)
imv: $(TARGETS)
-$(BUILDDIR)/imv-wl: $(OBJECTS) $(WL_OBJECTS)
+$(TARGET_WL): $(OBJECTS) $(WL_OBJECTS)
$(CC) -o $@ $^ $(LIBS) $(WL_LIBS) $(LDFLAGS)
-$(BUILDDIR)/imv-x11: $(OBJECTS) $(X11_OBJECTS)
+$(TARGET_X11): $(OBJECTS) $(X11_OBJECTS)
$(CC) -o $@ $^ $(LIBS) $(X11_LIBS) $(LDFLAGS)
+$(TARGET_MSG): $(MSG_OBJECTS)
+ $(CC) -o $@ $^ $(MSG_LIBS) $(LDFLAGS)
+
debug: CFLAGS += -DDEBUG -g -pg
debug: $(TARGETS)
@@ -143,6 +152,7 @@ else ifeq ($(WINDOWS),all)
$(INSTALL_PROGRAM) $(TARGET_X11) $(DESTDIR)$(BINPREFIX)/imv-x11
$(INSTALL_PROGRAM) src/imv.sh $(DESTDIR)$(BINPREFIX)/imv
endif
+ $(INSTALL_PROGRAM) $(TARGET_MSG) $(DESTDIR)$(BINPREFIX)/imv-msg
mkdir -p $(DESTDIR)$(MANPREFIX)/man1
$(INSTALL_MAN) doc/imv.1 $(DESTDIR)$(MANPREFIX)/man1/imv.1
mkdir -p $(DESTDIR)$(MANPREFIX)/man5
@@ -158,6 +168,7 @@ ifeq ($(WINDOWS),all)
$(RM) $(DESTDIR)$(BINPREFIX)/imv-x11
endif
$(RM) $(DESTDIR)$(BINPREFIX)/imv
+ $(RM) $(DESTDIR)$(BINPREFIX)/imv-msg
$(RM) $(DESTDIR)$(MANPREFIX)/man1/imv.1
$(RM) $(DESTDIR)$(MANPREFIX)/man5/imv.5
$(RM) $(DESTDIR)$(DATAPREFIX)/applications/imv.desktop
diff --git a/src/imv_msg.c b/src/imv_msg.c
new file mode 100644
index 0000000..505b843
--- /dev/null
+++ b/src/imv_msg.c
@@ -0,0 +1,42 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "ipc.h"
+
+int main(int argc, char **argv)
+{
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s <pid> <command>\n", argv[0]);
+ return 0;
+ }
+
+ int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ assert(sockfd);
+
+ struct sockaddr_un desc = {
+ .sun_family = AF_UNIX
+ };
+ imv_ipc_path(desc.sun_path, sizeof desc.sun_path, atoi(argv[1]));
+
+ if (connect(sockfd, (struct sockaddr *)&desc, sizeof desc) < 0) {
+ perror("Failed to connect");
+ return 1;
+ }
+
+ char buf[4096] = {0};
+ for (int i = 2; i < argc; ++i) {
+ strncat(buf, argv[i], sizeof buf - 1);
+ if (i + 1 < argc) {
+ strncat(buf, " ", sizeof buf - 1);
+ }
+ }
+ strncat(buf, "\n", sizeof buf - 1);
+
+ write(sockfd, buf, strlen(buf));
+ close(sockfd);
+ return 0;
+}
diff --git a/src/ipc.c b/src/ipc.c
index f5f3e46..0b42e22 100644
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -19,16 +19,7 @@ struct connection {
int fd;
};
-static void get_ipc_filename(char *buf, size_t len)
-{
- const char *base = getenv("XDG_RUNTIME_DIR");
- if (!base) {
- base = "/tmp";
- }
- snprintf(buf, len, "%s/imv-%d.sock", base, getpid());
-}
-
-void *wait_for_commands(void* void_conn)
+static void *wait_for_commands(void* void_conn)
{
struct connection *conn = void_conn;
@@ -55,7 +46,7 @@ void *wait_for_commands(void* void_conn)
return NULL;
}
-void *wait_for_connections(void* void_ipc)
+static void *wait_for_connections(void* void_ipc)
{
struct imv_ipc *ipc = void_ipc;
(void)ipc;
@@ -86,7 +77,7 @@ struct imv_ipc *imv_ipc_create(void)
struct sockaddr_un desc = {
.sun_family = AF_UNIX
};
- get_ipc_filename(desc.sun_path, sizeof desc.sun_path);
+ imv_ipc_path(desc.sun_path, sizeof desc.sun_path, getpid());
unlink(desc.sun_path);
@@ -115,7 +106,7 @@ void imv_ipc_free(struct imv_ipc *ipc)
}
char ipc_filename[1024];
- get_ipc_filename(ipc_filename, sizeof ipc_filename);
+ imv_ipc_path(ipc_filename, sizeof ipc_filename, getpid());
unlink(ipc_filename);
close(ipc->fd);
diff --git a/src/ipc.h b/src/ipc.h
index 9737a8f..539b684 100644
--- a/src/ipc.h
+++ b/src/ipc.h
@@ -1,6 +1,8 @@
#ifndef IMV_IPC_H
#define IMV_IPC_H
+#include <unistd.h>
+
struct imv_ipc;
struct imv_ipc *imv_ipc_create(void);
@@ -12,4 +14,6 @@ typedef void (*imv_ipc_callback)(const char *command, void *data);
void imv_ipc_set_command_callback(struct imv_ipc *ipc,
imv_ipc_callback callback, void *data);
+void imv_ipc_path(char *buf, size_t len, int pid);
+
#endif
diff --git a/src/ipc_common.c b/src/ipc_common.c
new file mode 100644
index 0000000..9943975
--- /dev/null
+++ b/src/ipc_common.c
@@ -0,0 +1,14 @@
+#include "ipc.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void imv_ipc_path(char *buf, size_t len, int pid)
+{
+ const char *base = getenv("XDG_RUNTIME_DIR");
+ if (!base) {
+ base = "/tmp";
+ }
+ snprintf(buf, len, "%s/imv-%d.sock", base, pid);
+}
+