Make shifting behave like I want to
This commit is contained in:
parent
c3ef4493d6
commit
6a3c8a3edf
|
@ -7,215 +7,225 @@
|
||||||
#include "river-status-unstable-v1.h"
|
#include "river-status-unstable-v1.h"
|
||||||
#include "river-control-unstable-v1.h"
|
#include "river-control-unstable-v1.h"
|
||||||
|
|
||||||
|
const char usage[] =
|
||||||
|
"Usage: river-shiftview [OPTIONS]\n"
|
||||||
|
" --shift VALUE value by which to rotate selected tag.\n"
|
||||||
|
" --num-tags VALUE number of tags to rotate\n"
|
||||||
|
" --start VALUE First tag to rotate from.\n"
|
||||||
|
" --help print this message and exit.\n"
|
||||||
|
"\n"
|
||||||
|
"\n";
|
||||||
|
|
||||||
|
int _shift = 1;
|
||||||
|
int _num_tags = 9;
|
||||||
|
int _start_tag = 1;
|
||||||
|
size_t outputs_size = 0;
|
||||||
|
|
||||||
int ret = EXIT_SUCCESS;
|
int ret = EXIT_SUCCESS;
|
||||||
|
|
||||||
|
struct output {
|
||||||
|
struct wl_output *output;
|
||||||
|
size_t index;
|
||||||
|
};
|
||||||
|
|
||||||
struct wl_display *wl_display = NULL;
|
struct wl_display *wl_display = NULL;
|
||||||
struct wl_output *wl_output = NULL;
|
struct output *wl_outputs = NULL;
|
||||||
struct wl_seat *seat = NULL;
|
struct wl_seat *seat = NULL;
|
||||||
|
|
||||||
struct zriver_status_manager_v1 *river_status_manager = NULL;
|
struct zriver_status_manager_v1 *river_status_manager = NULL;
|
||||||
struct zriver_control_v1 *river_controller = NULL;
|
struct zriver_control_v1 *river_controller = NULL;
|
||||||
|
|
||||||
struct zriver_output_status_v1 *river_output_status = NULL;
|
struct zriver_output_status_v1 **river_output_statuses = NULL;
|
||||||
|
|
||||||
int shifts = 1;
|
unsigned int *new_tags = NULL;
|
||||||
int num_tags = 9;
|
size_t focused_output = 0;
|
||||||
int start_tag = 0;
|
|
||||||
|
|
||||||
unsigned int new_tags = 0;
|
void rotate(unsigned int tagmask, size_t index, int shift, int start_tag,
|
||||||
|
int num_tags) {
|
||||||
const char usage[] =
|
// if multiple tags, chose the lowest one
|
||||||
"Usage: river-shiftview [OPTIONS]\n"
|
int current_tag = 1;
|
||||||
" --shifts VALUE value by which to rotate selected tag.\n"
|
while (!(tagmask & 1)) {
|
||||||
" --num-tags VALUE number of tags to rotate\n"
|
tagmask = tagmask >> 1;
|
||||||
" --start VALUE First tag to rotate from.\n"
|
current_tag += 1;
|
||||||
" --help print this message and exit.\n"
|
|
||||||
"\n"
|
|
||||||
"\n";
|
|
||||||
|
|
||||||
void
|
|
||||||
rotate (
|
|
||||||
unsigned int tagmask,
|
|
||||||
int rotations,
|
|
||||||
int start_tag,
|
|
||||||
int num_tags)
|
|
||||||
{
|
|
||||||
|
|
||||||
rotations %= num_tags;
|
|
||||||
|
|
||||||
const unsigned int mask = ~(~(0U) << num_tags) << start_tag;
|
|
||||||
unsigned int to_rotate = (tagmask & mask) >> start_tag;
|
|
||||||
|
|
||||||
if (rotations < 1)
|
|
||||||
{
|
|
||||||
rotations = -rotations;
|
|
||||||
new_tags = (mask
|
|
||||||
& ((to_rotate >> rotations)
|
|
||||||
| (to_rotate << (num_tags - rotations)) << start_tag))
|
|
||||||
+ (~mask & tagmask);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
int new_tag = current_tag + shift;
|
||||||
new_tags = (mask
|
while (new_tag > num_tags) {
|
||||||
& ((to_rotate << rotations)
|
new_tag = start_tag + (new_tag - num_tags) -
|
||||||
| (to_rotate >> (num_tags - rotations)) << start_tag))
|
1; // -1 because new_tag - num_tags is always at least 1 and
|
||||||
+ (~mask & tagmask);
|
// if we overflow, we want to START at start_tag
|
||||||
}
|
}
|
||||||
|
while (new_tag < start_tag) {
|
||||||
|
new_tag = num_tags + (new_tag - start_tag) +
|
||||||
|
1; // +1 because num_tags is INCLUSIVE and new_tag - start_tag
|
||||||
|
// is always at least 1
|
||||||
|
}
|
||||||
|
new_tags[index] = 1 << (new_tag - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_tagmask(void *data,
|
||||||
|
struct zriver_output_status_v1 *output_status_v1,
|
||||||
|
unsigned int tagmask) {
|
||||||
|
rotate(tagmask, (size_t)data, _shift, _start_tag, _num_tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
print_tagmask (
|
river_status_handle_view_tags(void *data,
|
||||||
void *data,
|
struct zriver_output_status_v1 *river_status,
|
||||||
struct zriver_output_status_v1 *output_status_v1,
|
struct wl_array *tags) {}
|
||||||
unsigned int tagmask)
|
|
||||||
{
|
|
||||||
rotate (tagmask, shifts, start_tag, num_tags);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void river_status_handle_urgent_tags(
|
||||||
river_status_handle_view_tags (
|
void *data, struct zriver_output_status_v1 *river_status, uint32_t tags) {}
|
||||||
void *data,
|
|
||||||
struct zriver_output_status_v1 *river_status,
|
|
||||||
struct wl_array *tags)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
static void
|
static const struct zriver_output_status_v1_listener tag_status_listener = {
|
||||||
river_status_handle_urgent_tags (
|
.focused_tags = print_tagmask,
|
||||||
void *data,
|
.view_tags = river_status_handle_view_tags,
|
||||||
struct zriver_output_status_v1 *river_status,
|
.urgent_tags = river_status_handle_urgent_tags,
|
||||||
uint32_t tags)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
static const struct zriver_output_status_v1_listener output_status_listener = {
|
|
||||||
.focused_tags = print_tagmask,
|
|
||||||
.view_tags = river_status_handle_view_tags,
|
|
||||||
.urgent_tags = river_status_handle_urgent_tags,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
void get_focused_output(void *data,
|
||||||
global_registry_handler (
|
struct zriver_seat_status_v1 *zriver_seat_status_v1,
|
||||||
void *data,
|
struct wl_output *output) {
|
||||||
struct wl_registry *registry,
|
for (size_t i = 0; i < outputs_size; i++) {
|
||||||
uint32_t id,
|
if (wl_outputs[i].output == output) {
|
||||||
const char *interface,
|
focused_output = i;
|
||||||
uint32_t version)
|
}
|
||||||
{
|
|
||||||
if (strcmp (interface, "wl_output") == 0)
|
|
||||||
{
|
|
||||||
wl_output = wl_registry_bind (registry, id, &wl_output_interface, 1);
|
|
||||||
}
|
|
||||||
else if ( strcmp( interface, "wl_seat" ) == 0 )
|
|
||||||
{
|
|
||||||
seat = wl_registry_bind(registry, id, &wl_seat_interface, 3);
|
|
||||||
}
|
|
||||||
else if (strcmp (interface, zriver_status_manager_v1_interface.name) == 0)
|
|
||||||
{
|
|
||||||
river_status_manager = wl_registry_bind (
|
|
||||||
registry, id, &zriver_status_manager_v1_interface, 2);
|
|
||||||
}
|
|
||||||
else if (strcmp (interface, zriver_control_v1_interface.name) == 0)
|
|
||||||
{
|
|
||||||
river_controller = wl_registry_bind(
|
|
||||||
registry, id, &zriver_control_v1_interface, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void unfocused_output_empty(void *data,
|
||||||
global_registry_remover (
|
struct zriver_seat_status_v1 *zriver_seat_status_v1,
|
||||||
void *data,
|
struct wl_output *output) {}
|
||||||
struct wl_registry *registry,
|
|
||||||
uint32_t id)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
static const struct wl_registry_listener registry_listener
|
void focused_view_empty(void *data,
|
||||||
= { global_registry_handler, global_registry_remover };
|
struct zriver_seat_status_v1 *zriver_seat_status_v1,
|
||||||
|
const char *title) {}
|
||||||
|
|
||||||
int
|
static const struct zriver_seat_status_v1_listener output_status_listener = {
|
||||||
main (int argc, char *argv[])
|
.focused_output = get_focused_output,
|
||||||
{
|
.unfocused_output = unfocused_output_empty,
|
||||||
enum
|
.focused_view = focused_view_empty,
|
||||||
{
|
};
|
||||||
HELP,
|
|
||||||
SHIFT_VALUE,
|
|
||||||
START_TAG,
|
|
||||||
NUM_TAGS
|
|
||||||
};
|
|
||||||
static struct option opts[] = {
|
|
||||||
{ "help", no_argument, NULL, HELP },
|
|
||||||
{ "shifts", required_argument, NULL, SHIFT_VALUE },
|
|
||||||
{ "num-tags", required_argument, NULL, NUM_TAGS },
|
|
||||||
{ "start", required_argument, NULL, START_TAG },
|
|
||||||
{ NULL, 0, NULL, 0 },
|
|
||||||
};
|
|
||||||
|
|
||||||
int opt;
|
static void global_registry_handler(void *data, struct wl_registry *registry,
|
||||||
while ((opt = getopt_long (argc, argv, "h", opts, NULL)) != -1)
|
uint32_t id, const char *interface,
|
||||||
{
|
uint32_t version) {
|
||||||
switch (opt)
|
if (strcmp(interface, "wl_output") == 0) {
|
||||||
{
|
outputs_size++;
|
||||||
|
wl_outputs = realloc(wl_outputs, outputs_size * sizeof(struct output));
|
||||||
|
if (wl_outputs == NULL) {
|
||||||
|
fputs("REALLOC ERROR", stderr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
wl_outputs[outputs_size - 1].output =
|
||||||
|
wl_registry_bind(registry, id, &wl_output_interface, 1);
|
||||||
|
wl_outputs[outputs_size - 1].index = outputs_size - 1;
|
||||||
|
} else if (strcmp(interface, "wl_seat") == 0) {
|
||||||
|
seat = wl_registry_bind(registry, id, &wl_seat_interface, 3);
|
||||||
|
} else if (strcmp(interface, zriver_status_manager_v1_interface.name) ==
|
||||||
|
0) {
|
||||||
|
river_status_manager = wl_registry_bind(
|
||||||
|
registry, id, &zriver_status_manager_v1_interface, 2);
|
||||||
|
} else if (strcmp(interface, zriver_control_v1_interface.name) == 0) {
|
||||||
|
river_controller =
|
||||||
|
wl_registry_bind(registry, id, &zriver_control_v1_interface, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void global_registry_remover(void *data, struct wl_registry *registry,
|
||||||
|
uint32_t id) {}
|
||||||
|
|
||||||
|
static const struct wl_registry_listener registry_listener = {
|
||||||
|
global_registry_handler, global_registry_remover
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
enum { HELP, SHIFT_VALUE, START_TAG, NUM_TAGS };
|
||||||
|
static struct option opts[] = {
|
||||||
|
{ "help", no_argument, NULL, HELP },
|
||||||
|
{ "shift", required_argument, NULL, SHIFT_VALUE },
|
||||||
|
{ "num-tags", required_argument, NULL, NUM_TAGS },
|
||||||
|
{ "start", required_argument, NULL, START_TAG },
|
||||||
|
{ NULL, 0, NULL, 0 },
|
||||||
|
};
|
||||||
|
|
||||||
|
int opt;
|
||||||
|
while ((opt = getopt_long(argc, argv, "h", opts, NULL)) != -1) {
|
||||||
|
switch (opt) {
|
||||||
case HELP:
|
case HELP:
|
||||||
fputs (usage, stderr);
|
fputs(usage, stderr);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
case SHIFT_VALUE:
|
case SHIFT_VALUE:
|
||||||
shifts = atoi (optarg);
|
_shift = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case START_TAG:
|
case START_TAG:
|
||||||
start_tag = atoi (optarg);
|
_start_tag = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case NUM_TAGS:
|
case NUM_TAGS:
|
||||||
num_tags = atoi (optarg);
|
_num_tags = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *display_name = getenv ("WAYLAND_DISPLAY");
|
const char *display_name = getenv("WAYLAND_DISPLAY");
|
||||||
if (display_name == NULL)
|
if (display_name == NULL) {
|
||||||
{
|
fputs("ERROR: WAYLAND_DISPLAY is not set.\n", stderr);
|
||||||
fputs ("ERROR: WAYLAND_DISPLAY is not set.\n", stderr);
|
return EXIT_FAILURE;
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wl_display = wl_display_connect (display_name);
|
wl_display = wl_display_connect(display_name);
|
||||||
if (wl_display == NULL)
|
if (wl_display == NULL) {
|
||||||
{
|
fputs("ERROR: Can not connect to wayland display.\n", stderr);
|
||||||
fputs ("ERROR: Can not connect to wayland display.\n", stderr);
|
return EXIT_FAILURE;
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct wl_registry *registry = wl_display_get_registry (wl_display);
|
struct wl_registry *registry = wl_display_get_registry(wl_display);
|
||||||
wl_registry_add_listener (registry, ®istry_listener, NULL);
|
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
||||||
|
|
||||||
wl_display_roundtrip (wl_display);
|
wl_display_roundtrip(wl_display);
|
||||||
|
|
||||||
river_output_status = zriver_status_manager_v1_get_river_output_status (
|
river_output_statuses =
|
||||||
river_status_manager, wl_output);
|
malloc(outputs_size * sizeof(struct zriver_output_status_v1 *));
|
||||||
|
new_tags = malloc(outputs_size * sizeof(int *));
|
||||||
|
|
||||||
zriver_output_status_v1_add_listener (river_output_status,
|
struct zriver_seat_status_v1 *seat_status_manager =
|
||||||
&output_status_listener, NULL);
|
zriver_status_manager_v1_get_river_seat_status(river_status_manager,
|
||||||
|
seat);
|
||||||
|
zriver_seat_status_v1_add_listener(seat_status_manager,
|
||||||
|
&output_status_listener, NULL);
|
||||||
|
|
||||||
/* Wait till new_tags is populated */
|
for (size_t i = 0; i < outputs_size; i++) {
|
||||||
wl_display_roundtrip (wl_display);
|
river_output_statuses[i] =
|
||||||
|
zriver_status_manager_v1_get_river_output_status(
|
||||||
|
river_status_manager, wl_outputs[i].output);
|
||||||
|
|
||||||
/* Set the new focused tags */
|
zriver_output_status_v1_add_listener(river_output_statuses[i],
|
||||||
zriver_control_v1_add_argument(
|
&tag_status_listener, (void *)i);
|
||||||
river_controller,
|
new_tags[i] = 0;
|
||||||
"set-focused-tags"
|
}
|
||||||
);
|
|
||||||
|
|
||||||
char* tags_str = malloc( (size_t)snprintf( NULL, 0, "%d", new_tags ));
|
/* Wait till new_tags is populated */
|
||||||
sprintf(tags_str, "%d", new_tags);
|
wl_display_roundtrip(wl_display);
|
||||||
|
|
||||||
zriver_control_v1_add_argument(
|
/* Set the new focused tags */
|
||||||
river_controller, tags_str);
|
zriver_control_v1_add_argument(river_controller, "set-focused-tags");
|
||||||
|
|
||||||
zriver_control_v1_run_command (
|
char *tags_str =
|
||||||
river_controller, seat);
|
malloc((size_t)snprintf(NULL, 0, "%d", new_tags[focused_output]));
|
||||||
|
sprintf(tags_str, "%d", new_tags[focused_output]);
|
||||||
|
|
||||||
zriver_control_v1_destroy(river_controller);
|
zriver_control_v1_add_argument(river_controller, tags_str);
|
||||||
wl_display_roundtrip (wl_display);
|
|
||||||
|
|
||||||
return ret;
|
zriver_control_v1_run_command(river_controller, seat);
|
||||||
|
|
||||||
|
zriver_control_v1_destroy(river_controller);
|
||||||
|
wl_display_roundtrip(wl_display);
|
||||||
|
|
||||||
|
free(wl_outputs);
|
||||||
|
free(river_output_statuses);
|
||||||
|
free(new_tags);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user