Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to map #1

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 19 additions & 67 deletions src/kbdd.cpp
Original file line number Diff line number Diff line change
@@ -1,81 +1,40 @@
#include <stdlib.h>
#include <map>
#include <xkbcommon/xkbcommon.h>
#include <wayfire/plugin.hpp>
#include <wayfire/output.hpp>
#include <wayfire/util/log.hpp>
#include <wayfire/signal-definitions.hpp>
#include <wayfire/nonstd/wlroots-full.hpp>

struct layout {
int view_id;
int layout_id;
};

int prev_view_id = -1;
struct layout *layouts;
int layouts_size;
class kbdd_plugin : public wf::plugin_interface_t
{

struct layout *find_layout(int view_id) {
for (int i = 0; i < layouts_size; ++i) {
if (layouts[i].view_id == view_id) {
return &layouts[i];
}
}
return NULL;
}

int get_layout(int view_id) {
struct layout *l = find_layout(view_id);
return l ? l->layout_id : -1;
}

void put_layout(int view_id, int layout_id) {
struct layout *existing = find_layout(view_id);
if (existing) {
if (layout_id == -1) {
existing->view_id = -1;
} else {
existing->layout_id = layout_id;
}
return;
}
std::map<int, int> views;

if (layout_id == -1) {
return;
}
int prev_view_id = -1;

for (int i=0; i < layouts_size; ++i) {
struct layout *l = &layouts[i];
if (l->view_id == -1) {
l->view_id = view_id;
l->layout_id = layout_id;
return;
const int default_layout_id = 0;


int get_layout(int view_id) {
if (views.find(view_id) == views.end()) {
return default_layout_id;
}
return views.at(view_id);
}

// realloc
int old_size = layouts_size;
layouts_size += 8;
layouts = (layout *)realloc(layouts, layouts_size * sizeof(struct layout));
for (int i = old_size; i < layouts_size; ++i) {
struct layout *l = &layouts[i];
if (i == old_size) {
l->view_id = view_id;
l->layout_id = layout_id;
} else {
l->view_id = -1;
}

void put_layout(int view_id, int layout_id) {
views[view_id] = layout_id;
}
}

class kbdd_plugin : public wf::plugin_interface_t
{

wf::signal_callback_t on_focus_changed = [=] (wf::signal_data_t *data)
{
wlr_seat *seat = wf::get_core().get_current_seat();
wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
if (!keyboard) {
LOGI("==> keyboard is null");
return;
}

Expand All @@ -89,17 +48,7 @@ class kbdd_plugin : public wf::plugin_interface_t
int view_id = view->get_id();
prev_view_id = view_id;

LOGI("==> on_focus_changed");
LOGI("==> view: ", view_id);

int layout_id = get_layout(view_id);
if (layout_id < 0) {

layout_id = xkb_state_serialize_layout(keyboard->xkb_state,
XKB_STATE_LAYOUT_LOCKED);

LOGI("==> stashed layout ID: ", layout_id);
}

wlr_keyboard_notify_modifiers(keyboard,
keyboard->modifiers.depressed,
Expand All @@ -108,9 +57,12 @@ class kbdd_plugin : public wf::plugin_interface_t
layout_id);
};


public:

void init() override
{
LOGI("kbdd plugin init");
output->connect_signal("view-focused", &on_focus_changed);
}

Expand Down