2 #define I3__FILE__ "key_press.c"
13 #include <sys/types.h>
16 #include <sys/socket.h>
25 static int version_string(
void *ctx,
const unsigned char *val,
size_t len) {
27 static int version_string(
void *ctx,
const unsigned char *val,
unsigned int len) {
35 static int version_map_key(
void *ctx,
const unsigned char *stringval,
size_t stringlen) {
37 static int version_map_key(
void *ctx,
const unsigned char *stringval,
unsigned int stringlen) {
40 strncmp((
const char*)stringval,
"human_readable", strlen(
"human_readable")) == 0);
72 if (socket_path == NULL)
76 if (pid_from_atom == NULL) {
78 printf(
"\nRunning version: < 4.2-200\n");
84 printf(
"(Getting version from running i3, press ctrl-c to abort…)");
88 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
90 err(EXIT_FAILURE,
"Could not create socket");
92 struct sockaddr_un addr;
93 memset(&addr, 0,
sizeof(
struct sockaddr_un));
94 addr.sun_family = AF_LOCAL;
95 strncpy(addr.sun_path, socket_path,
sizeof(addr.sun_path) - 1);
96 if (connect(sockfd, (
const struct sockaddr*)&addr,
sizeof(
struct sockaddr_un)) < 0)
97 err(EXIT_FAILURE,
"Could not connect to i3");
101 err(EXIT_FAILURE,
"IPC: write()");
103 uint32_t reply_length;
107 if ((ret =
ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
109 err(EXIT_FAILURE,
"IPC: read()");
113 if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
114 errx(EXIT_FAILURE,
"Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
117 yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL);
119 yajl_parser_config parse_conf = { 0, 0 };
121 yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, NULL);
124 yajl_status
state = yajl_parse(handle, (
const unsigned char*)reply, (
int)reply_length);
125 if (state != yajl_status_ok)
126 errx(EXIT_FAILURE,
"Could not parse my own reply. That's weird. reply is %.*s", (
int)reply_length, reply);
131 char exepath[PATH_MAX],
135 snprintf(exepath,
sizeof(exepath),
"/proc/%d/exe", getpid());
137 if ((linksize = readlink(exepath, destpath,
sizeof(destpath))) == -1)
138 err(EXIT_FAILURE,
"readlink(%s)", exepath);
141 destpath[linksize] =
'\0';
144 printf(
"The i3 binary you just called: %s\n", destpath);
146 snprintf(exepath,
sizeof(exepath),
"/proc/%s/exe", pid_from_atom);
148 if ((linksize = readlink(exepath, destpath,
sizeof(destpath))) == -1)
149 err(EXIT_FAILURE,
"readlink(%s)", exepath);
152 destpath[linksize] =
'\0';
156 if (strstr(destpath,
"(deleted)") != NULL)
157 printf(
"RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
162 snprintf(exepath,
sizeof(exepath),
"/proc/%s/cmdline", pid_from_atom);
165 if ((fd = open(exepath, O_RDONLY)) == -1)
166 err(EXIT_FAILURE,
"open(%s)", exepath);
167 if (read(fd, destpath,
sizeof(destpath)) == -1)
168 err(EXIT_FAILURE,
"read(%s)", exepath);
171 printf(
"The i3 binary you are running: %s\n", destpath);
static int version_string(void *ctx, const unsigned char *val, unsigned int len)
int ipc_send_message(int sockfd, const uint32_t message_size, const uint32_t message_type, const uint8_t *payload)
Formats a message (payload) of the given size and type and sends it to i3 via the given socket file d...
static int version_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen)
static char * human_readable_version
char * root_atom_contents(const char *atomname)
Try to get the contents of the given atom (for example I3_SOCKET_PATH) from the X11 root window and r...
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
int ipc_recv_message(int sockfd, uint32_t *message_type, uint32_t *reply_length, uint8_t **reply)
Reads a message from the given socket file descriptor and stores its length (reply_length) as well as...
void display_running_version(void)
Connects to i3 to find out the currently running version.
static yajl_callbacks version_callbacks
static bool human_readable_key