Skip to content

Commit

Permalink
Use pid instead of app_id for sway window rules
Browse files Browse the repository at this point in the history
Removes confusing app_id usage.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Aug 13, 2024
1 parent edf57ab commit edbfb26
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 53 deletions.
2 changes: 1 addition & 1 deletion extra/swayimg.1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -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
.\" ****************************************************************************
Expand Down
28 changes: 6 additions & 22 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

// Special ids for windows size and position
Expand Down Expand Up @@ -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)
};

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 0 additions & 14 deletions src/pixmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions src/sway.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
17 changes: 12 additions & 5 deletions src/sway.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

#pragma once

#include "pixmap.h"

#include <stdbool.h>
#include <stddef.h>
#include <sys/types.h>

#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
Expand All @@ -29,19 +37,18 @@ 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:
* 1. Enable floating mode;
* 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);

0 comments on commit edbfb26

Please sign in to comment.