2 #define I3__FILE__ "key_press.c"
13 #include <sys/types.h>
16 #include <sys/socket.h>
35 strncmp((
const char *)stringval,
"human_readable", strlen(
"human_readable")) == 0);
37 strncmp((
const char *)stringval,
"loaded_config_file_name", strlen(
"loaded_config_file_name")) == 0);
60 if (socket_path == NULL)
64 if (pid_from_atom == NULL) {
66 printf(
"\nRunning version: < 4.2-200\n");
72 printf(
"(Getting version from running i3, press ctrl-c to abort…)");
76 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
78 err(EXIT_FAILURE,
"Could not create socket");
80 struct sockaddr_un addr;
81 memset(&addr, 0,
sizeof(
struct sockaddr_un));
82 addr.sun_family = AF_LOCAL;
83 strncpy(addr.sun_path, socket_path,
sizeof(addr.sun_path) - 1);
84 if (connect(sockfd, (
const struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0)
85 err(EXIT_FAILURE,
"Could not connect to i3");
89 err(EXIT_FAILURE,
"IPC: write()");
91 uint32_t reply_length;
95 if ((ret =
ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
97 err(EXIT_FAILURE,
"IPC: read()");
101 if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION)
102 errx(EXIT_FAILURE,
"Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION);
106 yajl_status
state = yajl_parse(handle, (
const unsigned char *)reply, (
int)reply_length);
107 if (state != yajl_status_ok)
108 errx(EXIT_FAILURE,
"Could not parse my own reply. That's weird. reply is %.*s", (
int)reply_length, reply);
121 strftime(mtime,
sizeof(mtime),
"%c", localtime(&(sb.st_mtime)));
123 printf(
" (Last modified: %s, %.f seconds ago)\n", mtime, difftime(now, sb.st_mtime));
128 size_t destpath_size = 1024;
131 char *destpath =
smalloc(destpath_size);
133 sasprintf(&exepath,
"/proc/%d/exe", getpid());
135 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
136 destpath_size = destpath_size * 2;
137 destpath =
srealloc(destpath, destpath_size);
140 err(EXIT_FAILURE,
"readlink(%s)", exepath);
143 destpath[linksize] =
'\0';
146 printf(
"The i3 binary you just called: %s\n", destpath);
149 sasprintf(&exepath,
"/proc/%s/exe", pid_from_atom);
151 while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
152 destpath_size = destpath_size * 2;
153 destpath =
srealloc(destpath, destpath_size);
156 err(EXIT_FAILURE,
"readlink(%s)", exepath);
159 destpath[linksize] =
'\0';
163 if (strstr(destpath,
"(deleted)") != NULL)
164 printf(
"RUNNING BINARY DIFFERENT FROM BINARY ON DISK!\n");
170 sasprintf(&exepath,
"/proc/%s/cmdline", pid_from_atom);
173 if ((fd = open(exepath, O_RDONLY)) == -1)
174 err(EXIT_FAILURE,
"open(%s)", exepath);
175 if (read(fd, destpath,
sizeof(destpath)) == -1)
176 err(EXIT_FAILURE,
"read(%s)", exepath);
179 printf(
"The i3 binary you are running: %s\n", destpath);
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.
char * root_atom_contents(const char *atomname, xcb_connection_t *provided_conn, int screen)
Try to get the contents of the given atom (for example I3_SOCKET_PATH) from the X11 root window and r...
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
xcb_connection_t * conn
XCB connection and root screen.
static char * loaded_config_file_name
static bool loaded_config_file_name_key
static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen)
static xcb_cursor_context_t * ctx
static yajl_callbacks version_callbacks
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
static int version_string(void *ctx, const unsigned char *val, size_t len)
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...
static bool human_readable_key
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 char * human_readable_version