2016-01-24 02:59:58 +01:00
|
|
|
#include <math.h>
|
2016-06-10 13:08:59 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "readline.h"
|
2015-08-25 19:53:59 +02:00
|
|
|
#include "util.h"
|
2016-06-10 13:08:59 +02:00
|
|
|
#include "log.h"
|
2015-08-25 19:53:59 +02:00
|
|
|
|
2015-08-25 15:17:18 +02:00
|
|
|
int wrap(int i, int max) {
|
|
|
|
return ((i % max) + max) % max;
|
|
|
|
}
|
2015-12-14 17:07:31 +01:00
|
|
|
|
2016-01-23 22:35:32 +01:00
|
|
|
int numlen(int n) {
|
2016-01-24 02:59:58 +01:00
|
|
|
if (n == 0) {
|
2016-01-24 03:02:51 +01:00
|
|
|
return 1;
|
2016-01-24 02:59:58 +01:00
|
|
|
}
|
|
|
|
return log10(n) + 1;
|
2016-01-23 22:35:32 +01:00
|
|
|
}
|
|
|
|
|
2016-01-05 18:07:43 +01: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-06 16:59:54 +01: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 13:08:59 +02:00
|
|
|
|
|
|
|
pid_t get_parent_pid(pid_t child) {
|
|
|
|
pid_t parent;
|
|
|
|
char file_name[100];
|
|
|
|
char *buffer = NULL;
|
|
|
|
char *token = NULL;
|
2016-06-11 19:43:34 +02:00
|
|
|
const char *sep = " ";
|
2016-06-10 13:08:59 +02:00
|
|
|
FILE *stat = NULL;
|
|
|
|
|
|
|
|
sprintf(file_name, "/proc/%d/stat", child);
|
|
|
|
|
2016-06-11 19:43:34 +02:00
|
|
|
if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) {
|
|
|
|
fclose(stat);
|
2016-06-10 13:08:59 +02:00
|
|
|
|
2016-06-11 19:43:34 +02:00
|
|
|
token = strtok(buffer, sep); // pid
|
|
|
|
token = strtok(NULL, sep); // executable name
|
|
|
|
token = strtok(NULL, sep); // state
|
|
|
|
token = strtok(NULL, sep); // parent pid
|
2016-06-10 13:08:59 +02:00
|
|
|
|
2016-06-11 19:43:34 +02:00
|
|
|
parent = strtol(token, NULL, 10);
|
|
|
|
return (parent == child) ? -1 : parent;
|
2016-06-10 13:08:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:43:34 +02:00
|
|
|
return -1;
|
2016-06-10 13:08:59 +02:00
|
|
|
}
|