2017-04-15 06:37:43 +10:00
|
|
|
#define _XOPEN_SOURCE 500
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2016-01-24 12:59:58 +11:00
|
|
|
#include <math.h>
|
2016-09-01 22:18:37 +10:00
|
|
|
#include <stdint.h>
|
2016-06-10 21:08:59 +10:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-01 22:18:37 +10:00
|
|
|
#include <string.h>
|
2017-03-11 15:41:24 +11:00
|
|
|
#include <strings.h>
|
2016-09-01 22:18:37 +10:00
|
|
|
#include <wlc/wlc.h>
|
|
|
|
#include <xkbcommon/xkbcommon-names.h>
|
|
|
|
#include "log.h"
|
2016-06-10 21:08:59 +10:00
|
|
|
#include "readline.h"
|
2015-08-26 03:53:59 +10:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-08-25 23:17:18 +10:00
|
|
|
int wrap(int i, int max) {
|
|
|
|
return ((i % max) + max) % max;
|
|
|
|
}
|
2015-12-15 03:07:31 +11:00
|
|
|
|
2016-01-24 08:35:32 +11:00
|
|
|
int numlen(int n) {
|
2016-01-24 12:59:58 +11:00
|
|
|
if (n == 0) {
|
2016-01-24 13:02:51 +11:00
|
|
|
return 1;
|
2016-01-24 12:59:58 +11:00
|
|
|
}
|
|
|
|
return log10(n) + 1;
|
2016-01-24 08:35:32 +11:00
|
|
|
}
|
|
|
|
|
2016-01-06 04:07:43 +11:00
|
|
|
static struct modifier_key {
|
|
|
|
char *name;
|
|
|
|
uint32_t mod;
|
|
|
|
} modifiers[] = {
|
|
|
|
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
|
|
|
|
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
|
|
|
|
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
|
|
|
|
{ "Ctrl", WLC_BIT_MOD_CTRL },
|
|
|
|
{ XKB_MOD_NAME_ALT, WLC_BIT_MOD_ALT },
|
|
|
|
{ "Alt", WLC_BIT_MOD_ALT },
|
|
|
|
{ XKB_MOD_NAME_NUM, WLC_BIT_MOD_MOD2 },
|
|
|
|
{ "Mod3", WLC_BIT_MOD_MOD3 },
|
|
|
|
{ XKB_MOD_NAME_LOGO, WLC_BIT_MOD_LOGO },
|
|
|
|
{ "Mod5", WLC_BIT_MOD_MOD5 },
|
|
|
|
};
|
|
|
|
|
|
|
|
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-07 02:59:54 +11: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 21:08:59 +10:00
|
|
|
|
|
|
|
pid_t get_parent_pid(pid_t child) {
|
2016-06-12 06:29:04 +10:00
|
|
|
pid_t parent = -1;
|
2016-06-10 21:08:59 +10:00
|
|
|
char file_name[100];
|
|
|
|
char *buffer = NULL;
|
|
|
|
char *token = NULL;
|
2016-06-12 03:43:34 +10:00
|
|
|
const char *sep = " ";
|
2016-06-10 21:08:59 +10:00
|
|
|
FILE *stat = NULL;
|
|
|
|
|
|
|
|
sprintf(file_name, "/proc/%d/stat", child);
|
|
|
|
|
2016-06-12 06:29:04 +10:00
|
|
|
if ((stat = fopen(file_name, "r"))) {
|
|
|
|
if ((buffer = read_line(stat))) {
|
|
|
|
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);
|
|
|
|
}
|
2016-06-10 21:08:59 +10:00
|
|
|
|
2016-06-12 06:29:04 +10:00
|
|
|
fclose(stat);
|
|
|
|
}
|
2016-06-10 21:08:59 +10:00
|
|
|
|
2016-06-12 06:29:04 +10:00
|
|
|
if (parent) {
|
2016-06-12 03:43:34 +10:00
|
|
|
return (parent == child) ? -1 : parent;
|
2016-06-10 21:08:59 +10:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:43:34 +10:00
|
|
|
return -1;
|
2016-06-10 21:08:59 +10:00
|
|
|
}
|
2016-07-31 09:50:13 +10:00
|
|
|
|
|
|
|
uint32_t parse_color(const char *color) {
|
2017-02-22 06:49:22 +11:00
|
|
|
if (color[0] == '#') {
|
|
|
|
++color;
|
|
|
|
}
|
|
|
|
|
2016-07-31 09:50:13 +10:00
|
|
|
int len = strlen(color);
|
2017-02-22 06:49:22 +11:00
|
|
|
if (len != 6 && len != 8) {
|
2016-07-31 09:50:13 +10:00
|
|
|
sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color);
|
|
|
|
return 0xFFFFFFFF;
|
|
|
|
}
|
2017-02-22 06:49:22 +11:00
|
|
|
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
|
|
|
|
if (strlen(color) == 6) {
|
2016-07-31 09:50:13 +10:00
|
|
|
res = (res << 8) | 0xFF;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2017-04-15 06:37:43 +10:00
|
|
|
|
|
|
|
char* resolve_path(const char* path) {
|
|
|
|
struct stat sb;
|
|
|
|
ssize_t r;
|
|
|
|
int i;
|
|
|
|
char *current = NULL;
|
|
|
|
char *resolved = NULL;
|
|
|
|
|
|
|
|
if(!(current = strdup(path))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
if (lstat(current, &sb) == -1) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
if((sb.st_mode & S_IFMT) != S_IFLNK) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
if (!(resolved = malloc(sb.st_size + 1))) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
r = readlink(current, resolved, sb.st_size);
|
|
|
|
if (r == -1 || r > sb.st_size) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
resolved[r] = '\0';
|
|
|
|
free(current);
|
|
|
|
current = strdup(resolved);
|
|
|
|
free(resolved);
|
|
|
|
resolved = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
failed:
|
|
|
|
free(resolved);
|
|
|
|
free(current);
|
|
|
|
return NULL;
|
|
|
|
}
|