+#define _XOPEN_SOURCE 700
#include <x11.h>
#include <unistd.h>
#include <stdc.h>
(const unsigned char *)value, strlen(value)+1);
}
-Window edit_file(XConf* x, Window registrar, char* path, char* addr) {
+void edit_file(XConf* x, Window registrar, char* path, char* addr) {
static char wdirbuf[32768] = {0};
char* wdir = getcwd(wdirbuf, sizeof(wdirbuf));
- if (!wdir) return None;
- Window win = XCreateSimpleWindow(x->display, x->self, 0, 0, 1, 1, 0, 0, 0);
- prop_set(x, win, "WDIR", wdir);
- prop_set(x, win, "FILE", path);
- prop_set(x, win, "ADDR", addr);
+ if (!wdir) return;
+ char* rpath = realpath(path, NULL);
+ prop_set(x, x->self, "WDIR", wdir);
+ prop_set(x, x->self, "FILE", (rpath ? rpath : path));
+ prop_set(x, x->self, "ADDR", addr);
+ free(rpath);
XChangeProperty(
x->display, registrar, XA_OPEN, XA_WINDOW, 32, PropModeAppend,
- (const unsigned char *)&win, 1);
- XSelectInput(x->display, win, 0);
- return win;
+ (const unsigned char *)&(x->self), 1);
+ /* wait for the "done" message */
+ for (XEvent e;;) {
+ XNextEvent(x->display, &e);
+ if (e.type == ClientMessage && e.xclient.message_type == XA_DONE)
+ break;
+ }
}
void xclientmsg(XConf* x, XEvent* e) {
XA_DONE = XInternAtom(x.display, "DONE", 0);
Window registrar = start_registrar(&x);
if (argc == 1) {
- spawn("tide");
+ spawn("t2");
} else {
/* Loop over files and send and OPEN message for each one. */
for (int i = 1; i < argc; i++) {
}
}
XSync(x.display, False);
- x11_event_loop(&x);
return 0;
}
char* path;
} TWindow;
-Atom XA_REGISTRAR, XA_ADD, XA_DEL, XA_OPEN, XA_DONE, XA_FILE;
+Atom XA_REGISTRAR, XA_ADD, XA_DEL, XA_OPEN, XA_DONE, XA_WDIR, XA_FILE, XA_ADDR;
TWindow* Windows = NULL;
char* readprop(XConf* x, Window win, Atom prop, size_t* length) {
ev.xclient.format = 32;
ev.xclient.data.l[0] = val;
XSendEvent(x->display, to, False, mask, &ev);
+ XFlush(x->display);
}
-void win_open(XConf* x, char* path) {
- /* parse out address for later */
- char* addr = strrchr(path, ':');
- if (addr) *addr = '\0', addr++;
- if (!addr) addr = "0";
-
+void win_open(XConf* x, char* wdir, char* path, char* addr) {
+ if (!path) return;
/* search for an existing window */
for (TWindow* win = Windows; win; win = win->next) {
if (!strcmp(win->path, path)) {
win_send(x, win->win, x->root, SubstructureRedirectMask|SubstructureNotifyMask, "_NET_ACTIVE_WINDOW", 0);
XMapRaised(x->display, win->win);
win_send(x, x->self, win->win, 0, "GOTO", strtoul(addr, NULL, 0));
- XFlush(x->display);
return;
}
}
/* if we don't find it, spawn a new one */
- if (!fork())
- exit(execvp("tide", (char*[]){"tide", "-l", addr, path, 0}));
+ if (!fork()) {
+ if (wdir) chdir(wdir);
+ exit(execvp("t2", (char*[]){"t2", "-l", addr, path, 0}));
+ }
}
void selclear(XConf* x, XEvent* e) {
x->display, x->self, XA_OPEN, 0, -1, True, XA_WINDOW,
&type, &format, &datalen, &nleft, &data);
for (Window* win = (Window*)data; datalen && win && *win; win++, datalen--) {
- printf("%#lx\n", *win);
+ char* wdir = readprop(x, *win, XA_WDIR, NULL);
+ char* file = readprop(x, *win, XA_FILE, NULL);
+ char* addr = readprop(x, *win, XA_ADDR, NULL);
+ win_open(x, wdir, file, (addr ? addr : "0"));
win_send(x, x->self, *win, 0, "DONE", 0);
+ if(wdir) XFree(wdir);
+ if(file) XFree(file);
+ if(addr) XFree(addr);
}
-
-// size_t length;
-// char* paths = readprop(x, x->self, XA_OPEN, &length);
-// char* path = paths;
-// while (length && *path) {
-// size_t sz = strlen((char*)path)+1;
-// win_open(x, (char*)path);
-// path += sz, length -= sz;
-// }
-// XFree(paths);
}
int main(int argc, char** argv) {
XA_DEL = XInternAtom(x.display, "DEL", 0);
XA_OPEN = XInternAtom(x.display, "OPEN", 0);
XA_DONE = XInternAtom(x.display, "DONE", 0);
+ XA_WDIR = XInternAtom(x.display, "WDIR", 0);
XA_FILE = XInternAtom(x.display, "FILE", 0);
+ XA_ADDR = XInternAtom(x.display, "ADDR", 0);
x.eventfns[SelectionClear] = selclear;
x.eventfns[ClientMessage] = clientmsg;
x.eventfns[PropertyNotify] = propnotify;