cleaning up code
This commit is contained in:
parent
7d4aecffc2
commit
355873d2da
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
|||||||
CFILES=xcwd.c
|
CFILES=xcwd.c
|
||||||
CC=gcc
|
CC=gcc
|
||||||
CFLAGS=-Wall -Werror -std=gnu89 #-DDEBUG -g
|
CFLAGS=-Wall -Werror -std=gnu99
|
||||||
LDFLAGS=-lX11
|
LDFLAGS=-lX11
|
||||||
EXE="xcwd"
|
EXE="xcwd"
|
||||||
O=${CFILES:.c=.o}
|
O=${CFILES:.c=.o}
|
||||||
|
86
xcwd.c
86
xcwd.c
@ -12,10 +12,15 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
|
|
||||||
|
#define DEBUG 1
|
||||||
|
|
||||||
#define XA_STRING (XInternAtom(dpy, "STRING", 0))
|
#define XA_STRING (XInternAtom(dpy, "STRING", 0))
|
||||||
#define XA_CARDINAL (XInternAtom(dpy, "CARDINAL", 0))
|
#define XA_CARDINAL (XInternAtom(dpy, "CARDINAL", 0))
|
||||||
#define XA_WM_STATE (XInternAtom(dpy, "WM_STATE", 0))
|
#define XA_WM_STATE (XInternAtom(dpy, "WM_STATE", 0))
|
||||||
|
|
||||||
|
#define LOG(fmt, ...) \
|
||||||
|
do { if (DEBUG) fprintf(stderr, fmt, __VA_ARGS__); } while (0)
|
||||||
|
|
||||||
Display *dpy;
|
Display *dpy;
|
||||||
|
|
||||||
typedef struct processes_s *processes_t;
|
typedef struct processes_s *processes_t;
|
||||||
@ -41,13 +46,11 @@ int ppidCmp(const void *p1, const void *p2)
|
|||||||
|
|
||||||
static Window focusedWindow()
|
static Window focusedWindow()
|
||||||
{
|
{
|
||||||
Window focuswin, root;
|
Atom type;
|
||||||
int focusrevert;
|
Window focuswin, root, *children;
|
||||||
int format, status;
|
int format, status, focusrevert;
|
||||||
unsigned long nitems, after;
|
unsigned long nitems, after;
|
||||||
unsigned char *data;
|
unsigned char *data;
|
||||||
Atom type;
|
|
||||||
Window* children;
|
|
||||||
unsigned int nchildren;
|
unsigned int nchildren;
|
||||||
|
|
||||||
dpy = XOpenDisplay (NULL);
|
dpy = XOpenDisplay (NULL);
|
||||||
@ -57,19 +60,17 @@ static Window focusedWindow()
|
|||||||
root = XDefaultRootWindow(dpy);
|
root = XDefaultRootWindow(dpy);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
status = XGetWindowProperty(dpy, focuswin, XA_WM_STATE, 0, 65536, 0,
|
status = XGetWindowProperty(dpy, focuswin, XA_WM_STATE, 0, 1024, 0,
|
||||||
XA_WM_STATE, &type, &format, &nitems, &after, &data);
|
XA_WM_STATE, &type, &format, &nitems, &after, &data);
|
||||||
if (status == Success && data) {
|
if (status == Success && data) {
|
||||||
XFree(data);
|
XFree(data);
|
||||||
#ifdef DEBUG
|
LOG("Window ID = %lu\n", focuswin);
|
||||||
fprintf(stderr, "Window ID = %lu\n", focuswin);
|
|
||||||
#endif
|
|
||||||
return focuswin;
|
return focuswin;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
XQueryTree(dpy, focuswin, &root, &focuswin, &children, &nchildren);
|
XQueryTree(dpy, focuswin, &root, &focuswin, &children, &nchildren);
|
||||||
#ifdef DEBUG
|
LOG("%s", "Current window does not have WM_STATE, getting parent\n");
|
||||||
fprintf(stderr, "Current window do not have WM_STATE, getting parent\n");
|
|
||||||
#endif
|
|
||||||
} while(focuswin != root);
|
} while(focuswin != root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -84,20 +85,15 @@ static long windowPid(Window focuswin)
|
|||||||
unsigned long nitems, after;
|
unsigned long nitems, after;
|
||||||
unsigned char *data;
|
unsigned char *data;
|
||||||
|
|
||||||
status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 65536, 0,
|
status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0,
|
||||||
XA_CARDINAL, &type, &format, &nitems, &after, &data);
|
XA_CARDINAL, &type, &format, &nitems, &after, &data);
|
||||||
if(status == Success) {
|
if (status == Success && data) {
|
||||||
if (data) {
|
|
||||||
pid = *((long*)data);
|
pid = *((long*)data);
|
||||||
XFree(data);
|
XFree(data);
|
||||||
|
LOG("_NET_WM_PID = %lu\n", pid);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
|
||||||
if(pid == -1)
|
|
||||||
fprintf(stderr, "_NET_WM_PID not found\n");
|
|
||||||
else
|
else
|
||||||
fprintf(stderr, "_NET_WM_PID = %lu\n", pid);
|
LOG("%s", "_NET_WM_PID not found\n");
|
||||||
#endif
|
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,25 +112,18 @@ static char* windowStrings(Window focuswin, size_t *size, char* hint)
|
|||||||
if (data) {
|
if (data) {
|
||||||
if (type == XA_STRING) {
|
if (type == XA_STRING) {
|
||||||
ret = malloc(sizeof(char) * *size);
|
ret = malloc(sizeof(char) * *size);
|
||||||
#ifdef DEBUG
|
LOG("%s = ", hint);
|
||||||
fprintf(stderr, "%s = ", hint);
|
|
||||||
#endif
|
|
||||||
for (i = 0; i < *size; ++i) {
|
for (i = 0; i < *size; ++i) {
|
||||||
#ifdef DEBUG
|
LOG("%c", data[i] == 0 ? ' ' : data[i]);
|
||||||
fprintf(stderr, "%c", data[i] == 0 ? ' ' : data[i]);
|
|
||||||
#endif
|
|
||||||
ret[i] = data[i];
|
ret[i] = data[i];
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
#endif
|
LOG("%s", "\n");
|
||||||
}
|
}
|
||||||
XFree(data);
|
XFree(data);
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
|
||||||
else
|
else
|
||||||
fprintf(stderr, "%s not found\n", hint);
|
LOG("%s not found\n", hint);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -155,9 +144,7 @@ static processes_t getProcesses(void)
|
|||||||
p = malloc(sizeof(struct processes_s));
|
p = malloc(sizeof(struct processes_s));
|
||||||
p->ps = malloc(globbuf.gl_pathc * sizeof(struct proc_s));
|
p->ps = malloc(globbuf.gl_pathc * sizeof(struct proc_s));
|
||||||
|
|
||||||
#ifdef DEBUG
|
LOG("Found %ld processes\n", globbuf.gl_pathc);
|
||||||
fprintf(stderr, "Found %ld processes\n", globbuf.gl_pathc);
|
|
||||||
#endif
|
|
||||||
for (i = j = 0; i < globbuf.gl_pathc; i++) {
|
for (i = j = 0; i < globbuf.gl_pathc; i++) {
|
||||||
char name[32];
|
char name[32];
|
||||||
FILE *tn;
|
FILE *tn;
|
||||||
@ -170,10 +157,8 @@ static processes_t getProcesses(void)
|
|||||||
continue;
|
continue;
|
||||||
fscanf(tn, "%ld (%32[^)] %*3c %ld", &p->ps[j].pid, p->ps[j].name,
|
fscanf(tn, "%ld (%32[^)] %*3c %ld", &p->ps[j].pid, p->ps[j].name,
|
||||||
&p->ps[j].ppid);
|
&p->ps[j].ppid);
|
||||||
#ifdef DEBUG
|
LOG("\t%-20s\tpid=%6ld\tppid=%6ld\n", p->ps[j].name, p->ps[j].pid,
|
||||||
fprintf(stderr, "\t%-20s\tpid=%6ld\tppid=%6ld\n", p->ps[j].name,
|
p->ps[j].ppid);
|
||||||
p->ps[j].pid, p->ps[j].ppid);
|
|
||||||
#endif
|
|
||||||
fclose(tn);
|
fclose(tn);
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
@ -199,11 +184,12 @@ static void readPath(long pid)
|
|||||||
{
|
{
|
||||||
char buf[255];
|
char buf[255];
|
||||||
char path[64];
|
char path[64];
|
||||||
|
ssize_t len;
|
||||||
|
|
||||||
snprintf(path, sizeof(path), "/proc/%ld/cwd", pid);
|
snprintf(path, sizeof(path), "/proc/%ld/cwd", pid);
|
||||||
#ifdef DEBUG
|
LOG("Read %s\n", path);
|
||||||
fprintf(stderr, "Read %s\n", path);
|
if ((len = readlink(path, buf, 255)) != -1)
|
||||||
#endif
|
buf[len] = '\0';
|
||||||
readlink(path, buf, 255);
|
|
||||||
fprintf(stdout, "%s\n", buf);
|
fprintf(stdout, "%s\n", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,7 +205,7 @@ int main(int argc, const char *argv[])
|
|||||||
|
|
||||||
pid = windowPid(w);
|
pid = windowPid(w);
|
||||||
p = getProcesses();
|
p = getProcesses();
|
||||||
if(pid != -1) { // WM_NET_PID
|
if (pid != -1) {
|
||||||
qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp);
|
qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -230,9 +216,7 @@ int main(int argc, const char *argv[])
|
|||||||
qsort(p->ps, p->n, sizeof(struct proc_s), nameCmp);
|
qsort(p->ps, p->n, sizeof(struct proc_s), nameCmp);
|
||||||
strings = windowStrings(w, &size, "WM_CLASS");
|
strings = windowStrings(w, &size, "WM_CLASS");
|
||||||
for(i = 0; i < size; i += strlen(strings + i) + 1) {
|
for(i = 0; i < size; i += strlen(strings + i) + 1) {
|
||||||
#ifdef DEBUG
|
LOG("pidof %s\n", strings + i);
|
||||||
fprintf(stderr, "pidof %s\n", strings + i);
|
|
||||||
#endif
|
|
||||||
strcpy(key.name, strings + i);
|
strcpy(key.name, strings + i);
|
||||||
res = (struct proc_s *)bsearch(&key, p->ps, p->n,
|
res = (struct proc_s *)bsearch(&key, p->ps, p->n,
|
||||||
sizeof(struct proc_s), nameCmp);
|
sizeof(struct proc_s), nameCmp);
|
||||||
@ -241,9 +225,7 @@ int main(int argc, const char *argv[])
|
|||||||
}
|
}
|
||||||
if (res) {
|
if (res) {
|
||||||
pid = res->pid;
|
pid = res->pid;
|
||||||
#ifdef DEBUG
|
LOG("Found %s (%ld)\n", res->name, res->pid);
|
||||||
fprintf(stderr, "Found %s (%ld)\n", res->name, res->pid);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
if (size != 0)
|
if (size != 0)
|
||||||
free(strings);
|
free(strings);
|
||||||
@ -253,9 +235,7 @@ int main(int argc, const char *argv[])
|
|||||||
readPath(pid);
|
readPath(pid);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef DEBUG
|
LOG("%s", "getenv $HOME...\n");
|
||||||
fprintf(stderr, "getenv $HOME...\n");
|
|
||||||
#endif
|
|
||||||
fprintf(stdout, "%s\n", getenv("HOME"));
|
fprintf(stdout, "%s\n", getenv("HOME"));
|
||||||
}
|
}
|
||||||
freeProcesses(p);
|
freeProcesses(p);
|
||||||
|
Loading…
Reference in New Issue
Block a user