#define _GNU_SOURCE
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "settings.h"
static bool has_focus(struct c8ui_widget *w) {
if (w->focused) {
return true;
}
for (int i = 0; i < w->child_count; i++) {
if (has_focus(w->children[i])) {
return true;
}
}
return false;
}
/* Startup apps */
static struct {
struct c8ui_widget *rows, *new_entry;
} startup;
static void real_gone(struct c8ui_widget *w, void *data) {
g_free(w->user_data);
}
/* A command's box. In stealth mode one naming the user shows the
* stand-in, greyed so it can't be edited, and keeps the real one to save
* (entry_value). */
static struct c8ui_widget *command_entry(const char *command,
void (*changed)(struct c8ui_widget *, void *)) {
const char *shown = c8_stealth_show(command);
struct c8ui_widget *e = c8ui_entry_new(NULL, shown, changed, NULL);
if (shown != command) {
e->user_data = g_strdup(command);
e->on_destroy = real_gone;
c8ui_widget_set_disabled(e, true);
c8ui_widget_set_tooltip(e, "Stealth mode hides this. Turn it off "
"to change it.");
}
return e;
}
static const char *entry_value(struct c8ui_widget *e) {
return e->user_data ? e->user_data : c8ui_entry_get(e);
}
static void startup_save(void) {
const char **values = calloc(startup.rows->child_count + 1, sizeof(char *));
int n = 0;
for (int i = 0; i < startup.rows->child_count; i++) {
struct c8ui_widget *entry = startup.rows->children[i]->children[0];
const char *text = entry_value(entry);
if (text[0]) {
values[n++] = text;
}
}
setting_set_list("general", "autostart", values, n);
free(values);
}
static void startup_edited(struct c8ui_widget *w, void *data) {
startup_save();
}
static void startup_remove(struct c8ui_widget *w, void *data) {
c8ui_widget_destroy(w->parent);
startup_save();
}
static void startup_add_row(const char *command) {
struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 10);
row->fill = true;
struct c8ui_widget *entry = command_entry(command, startup_edited);
entry->expand = true;
entry->fill = true;
add(row, entry);
add(row, c8ui_button_new("Remove", startup_remove, NULL));
add(startup.rows, row);
}
static void startup_fill(void) {
c8ui_widget_clear(startup.rows);
const struct entry *e;
int n = setting_all("general", "autostart", &e);
for (int i = 0; i < n; i++) {
startup_add_row(e[i].value);
}
}
static void startup_new(struct c8ui_widget *w, void *data) {
const char *text = c8ui_entry_get(startup.new_entry);
if (text[0] == '\0') {
return;
}
startup_add_row(text);
c8ui_entry_set(startup.new_entry, "");
startup_save();
}
static void startup_build(struct c8ui_widget *box) {
add(box, caption("These commands run each time you sign in, in order."));
c8ui_widget_track(c8ui_box_new(C8UI_VERTICAL, 10), &startup.rows);
startup.rows->fill = true;
add(box, startup.rows);
startup_fill();
add(box, section_title("Add a command"));
struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 10);
row->fill = true;
c8ui_widget_track(c8ui_entry_new(NULL, "", startup_new, NULL),
&startup.new_entry);
c8ui_entry_set_placeholder(startup.new_entry, "mako");
startup.new_entry->expand = true;
add(row, startup.new_entry);
struct c8ui_widget *button = c8ui_button_new("Add", startup_new, NULL);
c8ui_button_set_primary(button, true);
add(row, button);
add(box, row);
}
static void startup_refresh(void) {
if (startup.rows == NULL || has_focus(startup.rows)) {
return;
}
const struct entry *e;
int n = setting_all("general", "autostart", &e);
bool same = n == startup.rows->child_count;
for (int i = 0; same && i < n; i++) {
struct c8ui_widget *entry = startup.rows->children[i]->children[0];
same = strcmp(c8ui_entry_get(entry), e[i].value) == 0;
}
if (!same) {
startup_fill();
}
}
const struct page page_startup = {
.name = "startup",
.title = "Startup apps",
.build = startup_build,
.refresh = startup_refresh,
};
/* Keyboard shortcuts */
static const struct {
const char *name, *label;
} actions[] = {
{ "exec", "Run a command" },
{ "start", "Show Start" },
{ "apps", "All apps" },
{ "search", "Search" },
{ "switcher", "Switch apps" },
{ "app-windows", "Switch the app's windows" },
{ "close", "Close the app" },
{ "snap-left", "Snap to the left" },
{ "snap-right", "Snap to the right" },
{ "unsnap", "Unsnap" },
{ "split-left", "Move the split left" },
{ "split-right", "Move the split right" },
{ "charms", "Charms" },
{ "settings", "Settings charm" },
{ "pc-settings", "PC settings" },
{ "quit", "Sign out" },
{ "screenshot", "Copy the screen" },
{ "screenshot-window", "Copy the app in front" },
{ "screenshot-save", "Save a screenshot" },
{ "snip", "Snip part of the screen" },
{ "volume-up", "Volume up" },
{ "volume-down", "Volume down" },
{ "mute", "Mute" },
{ "mic-mute", "Mute the microphone" },
{ "brightness-up", "Brightness up" },
{ "brightness-down", "Brightness down" },
{ "play-pause", "Play or pause" },
{ "next-track", "Next track" },
{ "previous-track", "Previous track" },
{ "stop-media", "Stop playing" },
};
#define ACTION_COUNT (int)(sizeof(actions) / sizeof(actions[0]))
struct shortcut_row {
struct c8ui_widget *box, *keys, *action, *arg;
};
static struct {
struct c8ui_widget *rows;
} shortcuts;
/* The row's binding as config.ini has it; false while incomplete */
static bool row_binding(struct shortcut_row *row, char *command, size_t len) {
const char *combo = c8ui_shortcut_get(row->keys);
int a = c8ui_combo_get(row->action);
if (combo[0] == '\0' || a < 0) {
return false;
}
if (strcmp(actions[a].name, "exec") == 0) {
const char *arg = entry_value(row->arg);
if (arg[0] == '\0') {
return false;
}
snprintf(command, len, "exec %s", arg);
} else {
snprintf(command, len, "%s", actions[a].name);
}
return true;
}
static struct shortcut_row *row_of(struct c8ui_widget *w) {
while (w && w->user_data == NULL) {
w = w->parent;
}
return w ? w->user_data : NULL;
}
static void shortcuts_save(void) {
setting_queue_remove("bindings", NULL);
for (int i = 0; i < shortcuts.rows->child_count; i++) {
struct shortcut_row *row = shortcuts.rows->children[i]->user_data;
char command[1024];
if (row_binding(row, command, sizeof(command))) {
setting_queue_add("bindings", c8ui_shortcut_get(row->keys), command);
}
}
setting_commit();
}
static void shortcut_edited(struct c8ui_widget *w, void *data) {
struct shortcut_row *row = row_of(w);
if (row && w == row->action) {
int a = c8ui_combo_get(row->action);
c8ui_widget_set_hidden(row->arg,
a < 0 || strcmp(actions[a].name, "exec") != 0);
}
shortcuts_save();
}
static void shortcut_remove(struct c8ui_widget *w, void *data) {
struct shortcut_row *row = row_of(w);
if (row) {
c8ui_widget_destroy(row->box);
shortcuts_save();
}
}
static void row_gone(struct c8ui_widget *w, void *data) {
free(data);
}
static struct shortcut_row *shortcut_add_row(const char *combo,
const char *command) {
struct shortcut_row *row = calloc(1, sizeof(*row));
char *name = g_strndup(command, strcspn(command, " \t"));
const char *arg = command + strlen(name);
while (*arg == ' ' || *arg == '\t') {
arg++;
}
int selected = -1;
const char *labels[ACTION_COUNT];
for (int i = 0; i < ACTION_COUNT; i++) {
labels[i] = actions[i].label;
if (strcmp(actions[i].name, name) == 0) {
selected = i;
}
}
g_free(name);
row->box = c8ui_box_new(C8UI_VERTICAL, 8);
row->box->fill = true;
row->box->user_data = row;
row->box->on_destroy = row_gone;
struct c8ui_widget *line = c8ui_box_new(C8UI_HORIZONTAL, 10);
line->fill = true;
row->keys = c8ui_shortcut_new(combo, shortcut_edited, NULL);
row->action = c8ui_combo_new(NULL, labels, ACTION_COUNT, selected,
shortcut_edited, NULL);
row->action->fill = true;
/* The keys and the action, then Remove; narrow, the action under
* the keys, Remove still beside them */
struct c8ui_widget *what = c8ui_box_new(C8UI_HORIZONTAL, 10);
c8ui_box_set_narrow_orientation(what, C8UI_VERTICAL);
c8ui_box_set_narrow_spacing(what, 8);
what->expand = true;
row->keys->fixed_width = 200;
add(what, row->keys);
row->action->expand = true;
add(what, row->action);
add(line, what);
add(line, c8ui_button_new("Remove", shortcut_remove, NULL));
add(row->box, line);
row->arg = command_entry(arg, shortcut_edited);
row->arg->fill = true;
c8ui_entry_set_placeholder(row->arg, "The command to run");
c8ui_widget_set_hidden(row->arg, selected < 0 ||
strcmp(actions[selected].name, "exec") != 0);
add(row->box, row->arg);
add(shortcuts.rows, row->box);
return row;
}
static void shortcuts_fill(void) {
c8ui_widget_clear(shortcuts.rows);
const struct entry *e;
int n = setting_all("bindings", NULL, &e);
for (int i = 0; i < n; i++) {
shortcut_add_row(e[i].key, e[i].value);
}
}
static void shortcut_new(struct c8ui_widget *w, void *data) {
struct shortcut_row *row = shortcut_add_row("", "exec");
c8ui_widget_grab_focus(row->keys);
}
static void shortcuts_build(struct c8ui_widget *box) {
add(box, caption("Click a shortcut, then press the keys you want for it."));
c8ui_widget_track(c8ui_box_new(C8UI_VERTICAL, 16), &shortcuts.rows);
shortcuts.rows->fill = true;
add(box, shortcuts.rows);
shortcuts_fill();
struct c8ui_widget *button = c8ui_button_new("Add a shortcut", shortcut_new,
NULL);
add(box, button);
}
static void shortcuts_refresh(void) {
if (shortcuts.rows == NULL || has_focus(shortcuts.rows)) {
return;
}
const struct entry *e;
int n = setting_all("bindings", NULL, &e);
int complete = 0;
bool same = true;
for (int i = 0; i < shortcuts.rows->child_count && same; i++) {
struct shortcut_row *row = shortcuts.rows->children[i]->user_data;
char command[1024];
if (!row_binding(row, command, sizeof(command))) {
continue; /* still being written */
}
same = complete < n &&
strcmp(e[complete].key, c8ui_shortcut_get(row->keys)) == 0 &&
strcmp(e[complete].value, command) == 0;
complete++;
}
if (!same || complete != n) {
shortcuts_fill();
}
}
const struct page page_shortcuts = {
.name = "shortcuts",
.title = "Keyboard shortcuts",
.build = shortcuts_build,
.refresh = shortcuts_refresh,
};