X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fdisplay_version.c;h=da11bff33587b772f273edf9dca591a2ff13ae3e;hb=b479dc1a0f1b535f918445c4224373d8d172d7a2;hp=73bc54d11084c74c8fad8f8380b3f74d86e44ec7;hpb=2314f107784196d8fc7ee500645dbdf548f91386;p=i3%2Fi3 diff --git a/src/display_version.c b/src/display_version.c index 73bc54d1..da11bff3 100644 --- a/src/display_version.c +++ b/src/display_version.c @@ -1,58 +1,45 @@ -#undef I3__FILE__ -#define I3__FILE__ "key_press.c" /* * vim:ts=4:sw=4:expandtab * * i3 - an improved dynamic tiling window manager - * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE) + * © 2009 Michael Stapelberg and contributors (see also: LICENSE) * * display_version.c: displays the running i3 version, runs as part of * i3 --moreversion. * */ +#include "all.h" + #include #include #include #include #include #include -#include "all.h" +#include -static bool human_readable_key; -static char *human_readable_version; +static bool human_readable_key, loaded_config_file_name_key; +static char *human_readable_version, *loaded_config_file_name; -#if YAJL_MAJOR >= 2 static int version_string(void *ctx, const unsigned char *val, size_t len) { -#else -static int version_string(void *ctx, const unsigned char *val, unsigned int len) { -#endif if (human_readable_key) sasprintf(&human_readable_version, "%.*s", (int)len, val); + if (loaded_config_file_name_key) + sasprintf(&loaded_config_file_name, "%.*s", (int)len, val); return 1; } -#if YAJL_MAJOR >= 2 static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) { -#else -static int version_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) { -#endif human_readable_key = (stringlen == strlen("human_readable") && - strncmp((const char*)stringval, "human_readable", strlen("human_readable")) == 0); + strncmp((const char *)stringval, "human_readable", strlen("human_readable")) == 0); + loaded_config_file_name_key = (stringlen == strlen("loaded_config_file_name") && + strncmp((const char *)stringval, "loaded_config_file_name", strlen("loaded_config_file_name")) == 0); return 1; } static yajl_callbacks version_callbacks = { - NULL, /* null */ - NULL, /* boolean */ - NULL, /* integer */ - NULL, /* double */ - NULL, /* number */ - &version_string, - NULL, /* start_map */ - &version_map_key, - NULL, /* end_map */ - NULL, /* start_array */ - NULL /* end_array */ + .yajl_string = version_string, + .yajl_map_key = version_map_key, }; /* @@ -68,9 +55,12 @@ static yajl_callbacks version_callbacks = { * */ void display_running_version(void) { - char *socket_path = root_atom_contents("I3_SOCKET_PATH", conn, conn_screen); - if (socket_path == NULL) - exit(EXIT_SUCCESS); + if (getenv("DISPLAY") == NULL) { + fprintf(stderr, "\nYour DISPLAY environment variable is not set.\n"); + fprintf(stderr, "Are you running i3 via SSH or on a virtual console?\n"); + fprintf(stderr, "Try DISPLAY=:0 i3 --moreversion\n"); + exit(EXIT_FAILURE); + } char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen); if (pid_from_atom == NULL) { @@ -84,20 +74,9 @@ void display_running_version(void) { printf("(Getting version from running i3, press ctrl-c to abort…)"); fflush(stdout); - /* TODO: refactor this with the code for sending commands */ - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - if (sockfd == -1) - err(EXIT_FAILURE, "Could not create socket"); - - struct sockaddr_un addr; - memset(&addr, 0, sizeof(struct sockaddr_un)); - addr.sun_family = AF_LOCAL; - strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); - if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) - err(EXIT_FAILURE, "Could not connect to i3"); - + int sockfd = ipc_connect(NULL); if (ipc_send_message(sockfd, 0, I3_IPC_MESSAGE_TYPE_GET_VERSION, - (uint8_t*)"") == -1) + (uint8_t *)"") == -1) err(EXIT_FAILURE, "IPC: write()"); uint32_t reply_length; @@ -113,20 +92,29 @@ void display_running_version(void) { if (reply_type != I3_IPC_MESSAGE_TYPE_GET_VERSION) errx(EXIT_FAILURE, "Got reply type %d, but expected %d (GET_VERSION)", reply_type, I3_IPC_MESSAGE_TYPE_GET_VERSION); -#if YAJL_MAJOR >= 2 yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL); -#else - yajl_parser_config parse_conf = { 0, 0 }; - - yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, NULL); -#endif - yajl_status state = yajl_parse(handle, (const unsigned char*)reply, (int)reply_length); + yajl_status state = yajl_parse(handle, (const unsigned char *)reply, (int)reply_length); if (state != yajl_status_ok) errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply); printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom); + if (loaded_config_file_name) { + struct stat sb; + time_t now; + char mtime[64]; + printf("Loaded i3 config: %s", loaded_config_file_name); + if (stat(loaded_config_file_name, &sb) == -1) { + printf("\n"); + ELOG("Cannot stat config file \"%s\"\n", loaded_config_file_name); + } else { + strftime(mtime, sizeof(mtime), "%c", localtime(&(sb.st_mtime))); + time(&now); + printf(" (Last modified: %s, %.f seconds ago)\n", mtime, difftime(now, sb.st_mtime)); + } + } + #ifdef __linux__ size_t destpath_size = 1024; ssize_t linksize; @@ -136,8 +124,8 @@ void display_running_version(void) { sasprintf(&exepath, "/proc/%d/exe", getpid()); while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) { - destpath_size = destpath_size * 2; - destpath = srealloc(destpath, destpath_size); + destpath_size = destpath_size * 2; + destpath = srealloc(destpath, destpath_size); } if (linksize == -1) err(EXIT_FAILURE, "readlink(%s)", exepath); @@ -186,4 +174,6 @@ void display_running_version(void) { #endif yajl_free(handle); + free(reply); + free(pid_from_atom); }