diff --git a/extra/swayimg.1 b/extra/swayimg.1 index 44fa9c5..01560b2 100644 --- a/extra/swayimg.1 +++ b/extra/swayimg.1 @@ -74,7 +74,7 @@ Set initial size of the window: \fIWIDTH,HEIGHT\fR: absolute size of the window in pixels. .\" ---------------------------------------------------------------------------- .IP "\fB\-a\fR, \fB\-\-class\fR=\fINAME\fR" -Set a constant window class/app_id. Setting this may break the window layout. +Set a constant window class/app_id. .\" ---------------------------------------------------------------------------- .IP "\fB\-c\fR, \fB\-\-config\fR=\fISECTION.KEY=VALUE\fR" Set a configuration parameter, see swayimgrc(5) for a list of sections and its parameters. diff --git a/extra/swayimgrc b/extra/swayimgrc index b044d05..0a94291 100644 --- a/extra/swayimgrc +++ b/extra/swayimgrc @@ -26,6 +26,8 @@ size = parent sigusr1 = reload # Action performed by SIGUSR2 signal (same format as for key bindings) sigusr2 = next_file +# Application ID and window class name +app_id = swayimg ################################################################################ # Viewer mode configuration diff --git a/extra/swayimgrc.5 b/extra/swayimgrc.5 index e0928dc..8325fb3 100644 --- a/extra/swayimgrc.5 +++ b/extra/swayimgrc.5 @@ -70,7 +70,7 @@ Set the action to be performed when the SIGUSR2 signal is triggered. Default value is \fInext_file\fR. .\" ---------------------------------------------------------------------------- .IP "\fBapp_id\fR = \fINAME\fR" -Set a constant window class/app_id. Setting this may break the window positioning. +Application ID used as window class name. .\" **************************************************************************** .\" Viewer config section .\" **************************************************************************** diff --git a/src/application.c b/src/application.c index 6695b02..ff46893 100644 --- a/src/application.c +++ b/src/application.c @@ -24,7 +24,6 @@ #include #include #include -#include #include // Special ids for windows size and position @@ -68,7 +67,7 @@ struct application { struct action_seq sigusr2; ///< Actions applied by USR2 signal event_handler ehandler; ///< Event handler for the current mode - struct rect window; ///< Preferable window position and size + struct wndrect window; ///< Preferable window position and size char* app_id; ///< Application id (app_id name) }; @@ -80,7 +79,7 @@ static struct application ctx; */ static void sway_setup(void) { - struct rect parent; + struct wndrect parent; bool fullscreen; bool absolute = false; int ipc; @@ -111,21 +110,8 @@ static void sway_setup(void) ctx.window.y = parent.y; } - if (!ctx.app_id) { - // create unique application id - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { - char app_id[64]; - const uint64_t uid = ((uint64_t)ts.tv_sec << 32) | ts.tv_nsec; - snprintf(app_id, sizeof(app_id), APP_NAME "_%" PRIx64, uid); - str_dup(app_id, &ctx.app_id); - } else { - str_dup(APP_NAME, &ctx.app_id); - } - } - // set window position via sway rules - sway_add_rules(ipc, ctx.app_id, ctx.window.x, ctx.window.y, absolute); + sway_add_rules(ipc, ctx.window.x, ctx.window.y, absolute); sway_disconnect(ipc); } @@ -376,6 +362,7 @@ void app_create(void) ctx.window.width = SIZE_FROM_PARENT; ctx.window.height = SIZE_FROM_PARENT; ctx.ehandler = viewer_handle; + str_dup(APP_NAME, &ctx.app_id); action_create("reload", &ctx.sigusr1); action_create("next_file", &ctx.sigusr2); @@ -473,15 +460,12 @@ bool app_init(const char** sources, size_t num) ctx.window.height = pm->height; } - if (!ctx.app_id) { - str_dup(APP_NAME, &ctx.app_id); - } - + // connect to wayland if (!ui_init(ctx.app_id, ctx.window.width, ctx.window.height)) { return false; } - // event queue notification + // create event queue notification ctx.event_signal = notification_create(); if (ctx.event_signal != -1) { app_watch(ctx.event_signal, handle_event_queue, NULL); diff --git a/src/pixmap.h b/src/pixmap.h index 513549a..503d033 100644 --- a/src/pixmap.h +++ b/src/pixmap.h @@ -39,20 +39,6 @@ typedef uint32_t argb_t; #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) -/** Size description. */ -struct size { - size_t width; - size_t height; -}; - -/** Rectangle description. */ -struct rect { - ssize_t x; - ssize_t y; - size_t width; - size_t height; -}; - /** Pixel map. */ struct pixmap { size_t width; ///< Width (px) diff --git a/src/sway.c b/src/sway.c index ad346a6..e07007f 100644 --- a/src/sway.c +++ b/src/sway.c @@ -123,16 +123,15 @@ static struct json_object* ipc_message(int ipc, enum ipc_msg_type type, /** * Send command for specified application. * @param ipc IPC context (socket file descriptor) - * @param app application Id * @param command command to send * @return true if operation completed successfully */ -static bool ipc_command(int ipc, const char* app, const char* command) +static bool ipc_command(int ipc, const char* command) { bool rc = false; char cmd[128]; - snprintf(cmd, sizeof(cmd), "for_window [app_id=%s] %s", app, command); + snprintf(cmd, sizeof(cmd), "for_window [pid=%d] %s", getpid(), command); json_object* response = ipc_message(ipc, IPC_COMMAND, cmd); if (response) { @@ -179,7 +178,7 @@ static bool read_int(json_object* node, const char* name, int* value) * @param rect rectangle geometry * @return true if operation completed successfully */ -static bool read_rect(json_object* node, const char* name, struct rect* rect) +static bool read_rect(json_object* node, const char* name, struct wndrect* rect) { int x, y, width, height; struct json_object* rn; @@ -295,7 +294,7 @@ void sway_disconnect(int ipc) } } -bool sway_current(int ipc, struct rect* wnd, bool* fullscreen) +bool sway_current(int ipc, struct wndrect* wnd, bool* fullscreen) { bool rc = false; @@ -324,8 +323,8 @@ bool sway_current(int ipc, struct rect* wnd, bool* fullscreen) } json_object* cur_wks = current_workspace(workspaces); if (cur_wks) { - struct rect workspace; - struct rect global; + struct wndrect workspace; + struct wndrect global; rc = read_rect(cur_wks, "rect", &workspace) && read_rect(cur_wnd, "rect", &global); if (rc) { @@ -340,11 +339,10 @@ bool sway_current(int ipc, struct rect* wnd, bool* fullscreen) return rc; } -bool sway_add_rules(int ipc, const char* app, int x, int y, bool absolute) +bool sway_add_rules(int ipc, int x, int y, bool absolute) { char move[64]; snprintf(move, sizeof(move), "move %s position %i %i", absolute ? "absolute" : "", x, y); - return ipc_command(ipc, app, "floating enable") && - ipc_command(ipc, app, move); + return ipc_command(ipc, "floating enable") && ipc_command(ipc, move); } diff --git a/src/sway.h b/src/sway.h index cf3562d..19028b4 100644 --- a/src/sway.h +++ b/src/sway.h @@ -4,12 +4,20 @@ #pragma once -#include "pixmap.h" - #include +#include +#include #define INVALID_SWAY_IPC -1 +/** Position and size of a window. */ +struct wndrect { + ssize_t x; + ssize_t y; + size_t width; + size_t height; +}; + /** * Connect to Sway. * @return IPC context, INVALID_SWAY_IPC if error @@ -29,7 +37,7 @@ void sway_disconnect(int ipc); * @param fullscreen current full screen mode * @return true if operation completed successfully */ -bool sway_current(int ipc, struct rect* wnd, bool* fullscreen); +bool sway_current(int ipc, struct wndrect* wnd, bool* fullscreen); /** * Add rules for Sway for application's window: @@ -37,11 +45,10 @@ bool sway_current(int ipc, struct rect* wnd, bool* fullscreen); * 2. Set initial position. * * @param ipc IPC context - * @param app application Id * @param x horizontal window position * @param v vertical window position * @param absolute flag to use absolute position instead of relative to the * current workspace * @return true if operation completed successfully */ -bool sway_add_rules(int ipc, const char* app, int x, int y, bool absolute); +bool sway_add_rules(int ipc, int x, int y, bool absolute);