[
  {
    "path": "LICENSE",
    "content": "Copyright (c) 2016 Janne Heß\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "CXX = g++\nCPPFLAGS = -Wall -Wextra\nLDFLAGS = -O3 -shared -fPIC -static-libgcc -lX11\n\nall: spotifywm\n\nspotifywm: spotifywm.so\n\nspotifywm.so: spotifywm.cpp\n\t$(CXX) $(CPPFLAGS) $(LDFLAGS) -DSONAME=\"$@\" -o $@ $^\n\nclean:\n\trm spotifywm.so\n"
  },
  {
    "path": "README.md",
    "content": "# spotifywm\n\nMakes spotify more friendly to window managers by settings a class name before opening the window.\nThis allows WMs like i3 to correctly discover the window and fit it into a prepared layout.\n\nInspired by [steamwm](https://github.com/dscharrer/steamwm).\n\n# Installation\n\n```\n$ make\n```\n\n# Usage\n\n```\nLD_PRELOAD=/path/to/spotifywm.so /path/to/spotify/binary\n```\n\nUnder Arch Linux, do not run the wrapper script in /usr/bin, it will override `LD_PRELOAD`.\nUse `/usr/share/spotify/spotify` instead.\n"
  },
  {
    "path": "spotifywm.cpp",
    "content": "#ifndef _GNU_SOURCE\n#define _GNU_SOURCE\n#endif /* _GNU_SOURCE */\n\n#include <stdlib.h>\n#include <string.h>\n#include <stdio.h>\n#include <dlfcn.h>\n#include <X11/Xlib.h>\n#include <X11/Xutil.h>\n#include <X11/Xproto.h>\t\t/* to declare xEvent */\n#include <xcb/xproto.h>\n#include <xcb/xcb.h>\n#include <xcb/xcbext.h>\n\n#define STR_(x) # x\n#define STR(x)  STR_(x)\n\nextern \"C\" {\n\textern char * program_invocation_short_name; // provided by glibc\n}\n\nvoid spotifywm_init(void) __attribute__((constructor));\nvoid spotifywm_init(void) {\n\t// Prevent spotifywm.so from being attached to processes started by steam\n\tconst char *envname = \"LD_PRELOAD\";\n\tconst char *oldenv = getenv(envname);\n\tif (oldenv && false) {\n\t\tchar *env = strdup(oldenv);\n\t\tchar *pos = strstr(env, STR(SONAME));\n\t\tif (pos) {\n\t\t\tsize_t len1 = strlen(STR(SONAME));\n\t\t\tsize_t len2 = strlen(pos + len1);\n\t\t\tmemmove(pos, pos + len1, len2);\n\t\t\t*(pos + len2) = '\\0';\n\t\t\tsetenv(envname, env, 1);\n\t\t}\n\t\tfree(env);\n\t}\n\tfprintf(stderr, \"[spotifywm] attached to spotify\\n\");\n}\n\n#define BASE_NAME(SymbolName) base_ ## SymbolName\n#define TYPE_NAME(SymbolName) SymbolName ## _t\n#define INTERCEPT(ReturnType, SymbolName, ...) \\\n\t\ttypedef ReturnType (*TYPE_NAME(SymbolName))(__VA_ARGS__); \\\n\tstatic void * const BASE_NAME(SymbolName) = dlsym(RTLD_NEXT, STR(SymbolName)); \\\n\textern \"C\" ReturnType SymbolName(__VA_ARGS__)\n#define BASE(SymbolName) ((TYPE_NAME(SymbolName))BASE_NAME(SymbolName))\n\nINTERCEPT(int, XMapWindow,\n\tDisplay * dpy,\n\tWindow    w\n) {\n\tXClassHint* classHint;\n\n\tfprintf(stderr, \"[spotifywm] spotify window %lx found\\n\", w);\n\tclassHint = XAllocClassHint();\n\tif (classHint) {\n\t\tclassHint->res_name = (char*)\"spotify\";\n\t\tclassHint->res_class = (char*)\"Spotify\";\n\t\tXSetClassHint(dpy, w, classHint);\n\t\tXFree(classHint);\n\t}\n\treturn BASE(XMapWindow)(dpy, w);\n}\n\nINTERCEPT(unsigned int, xcb_send_request,\n\txcb_connection_t *c,\n\tint flags,\n\tstruct iovec *vector,\n\tconst xcb_protocol_request_t *request \n) {\n\t// Check if we are sending a MapWindow request\n\t// This code is called from chromium's source code:\n\t// ui/gfx/x/generated_protos/xproto.cc in XProto::MapWindow\n\tif(request->count >= 1 && vector[0].iov_len >= 8 && ((uint8_t*)vector[0].iov_base)[0] == 8) {\n\t\tuint32_t window_id = ((uint32_t*)vector[0].iov_base)[1];\n\n\t\tfprintf(stderr, \"[spotifywm] spotify window %x found\\n\", window_id);\n\t\t\n\t\t// Don't use the same XCB connection as spotify is checking the returned number\n\t\tDisplay* dpy = XOpenDisplay(NULL);\n\t\tXClassHint* classHint = XAllocClassHint();\n\t\tif (classHint) {\n\t\t\tclassHint->res_name = (char*)\"spotify\";\n\t\t\tclassHint->res_class = (char*)\"Spotify\";\n\t\t\tXSetClassHint(dpy, window_id, classHint);\n\t\t\tXFree(classHint);\n\t\t}\n\t\tXCloseDisplay(dpy);\n\t}\n\t\n\treturn BASE(xcb_send_request)(c, flags, vector, request);\n}\n"
  }
]