2018-11-17 16:11:28 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2018-10-31 22:29:45 +00:00
|
|
|
#include <assert.h>
|
2017-04-14 20:37:43 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2018-11-17 19:31:33 +00:00
|
|
|
#include <float.h>
|
2016-01-24 01:59:58 +00:00
|
|
|
#include <math.h>
|
2016-09-01 12:18:37 +00:00
|
|
|
#include <stdint.h>
|
2016-06-10 11:08:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-01 12:18:37 +00:00
|
|
|
#include <string.h>
|
2017-03-11 04:41:24 +00:00
|
|
|
#include <strings.h>
|
2016-09-01 12:18:37 +00:00
|
|
|
#include <xkbcommon/xkbcommon-names.h>
|
2017-11-22 23:23:37 +00:00
|
|
|
#include <wlr/types/wlr_keyboard.h>
|
2016-09-01 12:18:37 +00:00
|
|
|
#include "log.h"
|
2015-08-25 17:53:59 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-08-25 13:17:18 +00:00
|
|
|
int wrap(int i, int max) {
|
|
|
|
return ((i % max) + max) % max;
|
|
|
|
}
|
2015-12-14 16:07:31 +00:00
|
|
|
|
2016-01-23 21:35:32 +00:00
|
|
|
int numlen(int n) {
|
2016-01-24 01:59:58 +00:00
|
|
|
if (n == 0) {
|
2016-01-24 02:02:51 +00:00
|
|
|
return 1;
|
2016-01-24 01:59:58 +00:00
|
|
|
}
|
2018-11-17 16:11:28 +00:00
|
|
|
// Account for the '-' in negative numbers.
|
|
|
|
return log10(abs(n)) + (n > 0 ? 1 : 2);
|
2016-01-23 21:35:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:07:43 +00:00
|
|
|
static struct modifier_key {
|
|
|
|
char *name;
|
|
|
|
uint32_t mod;
|
|
|
|
} modifiers[] = {
|
2017-11-22 23:23:37 +00:00
|
|
|
{ XKB_MOD_NAME_SHIFT, WLR_MODIFIER_SHIFT },
|
|
|
|
{ XKB_MOD_NAME_CAPS, WLR_MODIFIER_CAPS },
|
|
|
|
{ XKB_MOD_NAME_CTRL, WLR_MODIFIER_CTRL },
|
|
|
|
{ "Ctrl", WLR_MODIFIER_CTRL },
|
|
|
|
{ XKB_MOD_NAME_ALT, WLR_MODIFIER_ALT },
|
|
|
|
{ "Alt", WLR_MODIFIER_ALT },
|
|
|
|
{ XKB_MOD_NAME_NUM, WLR_MODIFIER_MOD2 },
|
|
|
|
{ "Mod3", WLR_MODIFIER_MOD3 },
|
|
|
|
{ XKB_MOD_NAME_LOGO, WLR_MODIFIER_LOGO },
|
|
|
|
{ "Mod5", WLR_MODIFIER_MOD5 },
|
2016-01-05 17:07:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t get_modifier_mask_by_name(const char *name) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
|
|
|
|
if (strcasecmp(modifiers[i].name, name) == 0) {
|
|
|
|
return modifiers[i].mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *get_modifier_name_by_mask(uint32_t modifier) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
|
|
|
|
if (modifiers[i].mod == modifier) {
|
|
|
|
return modifiers[i].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-06 15:59:54 +00:00
|
|
|
|
|
|
|
int get_modifier_names(const char **names, uint32_t modifier_masks) {
|
|
|
|
int length = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
|
|
|
|
if ((modifier_masks & modifiers[i].mod) != 0) {
|
|
|
|
names[length] = modifiers[i].name;
|
|
|
|
++length;
|
|
|
|
modifier_masks ^= modifiers[i].mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
2016-06-10 11:08:59 +00:00
|
|
|
|
|
|
|
pid_t get_parent_pid(pid_t child) {
|
2016-06-11 20:29:04 +00:00
|
|
|
pid_t parent = -1;
|
2016-06-10 11:08:59 +00:00
|
|
|
char file_name[100];
|
|
|
|
char *buffer = NULL;
|
|
|
|
char *token = NULL;
|
2016-06-11 17:43:34 +00:00
|
|
|
const char *sep = " ";
|
2016-06-10 11:08:59 +00:00
|
|
|
FILE *stat = NULL;
|
2018-12-09 12:09:11 +00:00
|
|
|
size_t buf_size = 0;
|
2016-06-10 11:08:59 +00:00
|
|
|
|
|
|
|
sprintf(file_name, "/proc/%d/stat", child);
|
|
|
|
|
2016-06-11 20:29:04 +00:00
|
|
|
if ((stat = fopen(file_name, "r"))) {
|
2018-12-09 12:09:11 +00:00
|
|
|
if (getline(&buffer, &buf_size, stat) != -1) {
|
2016-06-11 20:29:04 +00:00
|
|
|
token = strtok(buffer, sep); // pid
|
|
|
|
token = strtok(NULL, sep); // executable name
|
|
|
|
token = strtok(NULL, sep); // state
|
|
|
|
token = strtok(NULL, sep); // parent pid
|
|
|
|
parent = strtol(token, NULL, 10);
|
|
|
|
}
|
2018-07-01 14:06:40 +00:00
|
|
|
free(buffer);
|
2016-06-11 20:29:04 +00:00
|
|
|
fclose(stat);
|
|
|
|
}
|
2016-06-10 11:08:59 +00:00
|
|
|
|
2016-06-11 20:29:04 +00:00
|
|
|
if (parent) {
|
2016-06-11 17:43:34 +00:00
|
|
|
return (parent == child) ? -1 : parent;
|
2016-06-10 11:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 17:43:34 +00:00
|
|
|
return -1;
|
2016-06-10 11:08:59 +00:00
|
|
|
}
|
2016-07-30 23:50:13 +00:00
|
|
|
|
|
|
|
uint32_t parse_color(const char *color) {
|
2017-02-21 19:49:22 +00:00
|
|
|
if (color[0] == '#') {
|
|
|
|
++color;
|
|
|
|
}
|
|
|
|
|
2016-07-30 23:50:13 +00:00
|
|
|
int len = strlen(color);
|
2017-02-21 19:49:22 +00:00
|
|
|
if (len != 6 && len != 8) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color);
|
2016-07-30 23:50:13 +00:00
|
|
|
return 0xFFFFFFFF;
|
|
|
|
}
|
2017-02-21 19:49:22 +00:00
|
|
|
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
|
|
|
|
if (strlen(color) == 6) {
|
2016-07-30 23:50:13 +00:00
|
|
|
res = (res << 8) | 0xFF;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2017-04-14 20:37:43 +00:00
|
|
|
|
2018-07-23 20:22:09 +00:00
|
|
|
bool parse_boolean(const char *boolean, bool current) {
|
2018-07-24 01:37:53 +00:00
|
|
|
if (strcasecmp(boolean, "1") == 0
|
|
|
|
|| strcasecmp(boolean, "yes") == 0
|
|
|
|
|| strcasecmp(boolean, "on") == 0
|
|
|
|
|| strcasecmp(boolean, "true") == 0
|
|
|
|
|| strcasecmp(boolean, "enable") == 0
|
|
|
|
|| strcasecmp(boolean, "enabled") == 0
|
|
|
|
|| strcasecmp(boolean, "active") == 0) {
|
2018-07-23 19:04:46 +00:00
|
|
|
return true;
|
2018-07-24 01:37:53 +00:00
|
|
|
} else if (strcasecmp(boolean, "toggle") == 0) {
|
2018-07-23 19:04:46 +00:00
|
|
|
return !current;
|
|
|
|
}
|
2018-07-24 01:37:53 +00:00
|
|
|
// All other values are false to match i3
|
2018-07-23 19:04:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-31 22:29:45 +00:00
|
|
|
|
2018-11-17 19:31:33 +00:00
|
|
|
float parse_float(const char *value) {
|
|
|
|
errno = 0;
|
|
|
|
char *end;
|
|
|
|
float flt = strtof(value, &end);
|
|
|
|
if (*end || errno) {
|
|
|
|
wlr_log(WLR_DEBUG, "Invalid float value '%s', defaulting to NAN", value);
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
return flt;
|
|
|
|
}
|
|
|
|
|
2018-10-31 22:29:45 +00:00
|
|
|
enum wlr_direction opposite_direction(enum wlr_direction d) {
|
|
|
|
switch (d) {
|
|
|
|
case WLR_DIRECTION_UP:
|
|
|
|
return WLR_DIRECTION_DOWN;
|
|
|
|
case WLR_DIRECTION_DOWN:
|
|
|
|
return WLR_DIRECTION_UP;
|
|
|
|
case WLR_DIRECTION_RIGHT:
|
|
|
|
return WLR_DIRECTION_LEFT;
|
|
|
|
case WLR_DIRECTION_LEFT:
|
|
|
|
return WLR_DIRECTION_RIGHT;
|
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|