#include <stdio.h>
#include <stdlib.h>
#include <err.h>
+#include <errno.h>
#include <locale.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <sys/types.h>
+#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/queue.h>
#include <sys/param.h>
+#include <sys/select.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#endif
#endif
-#define SWM_DEBUG
/* #define SWM_DEBUG */
#ifdef SWM_DEBUG
#define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
/* dialog windows */
double dialog_ratio = .6;
/* status bar */
+#define SWM_BAR_MAX (128)
+sig_atomic_t bar_alarm = 0;
int bar_enabled = 1;
int bar_height = 0;
GC bar_gc;
XGCValues bar_gcv;
XFontStruct *bar_fs;
-char bar_text[128];
char *bar_fonts[] = {
"-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
"-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
unsigned long bar_font_color;
unsigned long color_focus; /* XXX should this be per ws? */
unsigned long color_unfocus;
+ char bar_text[SWM_BAR_MAX];
};
struct swm_screen *screens;
int num_screens;
}
void
-bar_print(struct swm_region *r)
+bar_print(void)
{
time_t tmt;
struct tm tm;
-
+ struct swm_region *tmpr;
+ int i;
+ char tmp[SWM_BAR_MAX];
+
if (bar_enabled == 0)
return;
/* clear old text */
- XSetForeground(display, bar_gc, r->s->bar_color);
- XDrawString(display, r->bar_window,
- bar_gc, 4, bar_fs->ascent, bar_text, strlen(bar_text));
+ for (i = 0; i < ScreenCount(display); i++)
+ TAILQ_FOREACH(tmpr, &screens[i].rl, entry) {
+ XSetForeground(display, bar_gc, tmpr->s->bar_color);
+ XDrawString(display, tmpr->bar_window,
+ bar_gc, 4, bar_fs->ascent, tmpr->s->bar_text,
+ strlen(tmpr->s->bar_text));
+ }
/* draw new text */
time(&tmt);
localtime_r(&tmt, &tm);
- strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
- XSetForeground(display, bar_gc, r->s->bar_font_color);
- XDrawString(display, r->bar_window, bar_gc, 4,
- bar_fs->ascent, bar_text, strlen(bar_text));
- XSync(display, False);
+ strftime(tmp, sizeof tmp, "%a %b %d %R %Z %Y", &tm);
+ for (i = 0; i < ScreenCount(display); i++)
+ TAILQ_FOREACH(tmpr, &screens[i].rl, entry) {
+ XSetForeground(display, bar_gc,
+ tmpr->s->bar_font_color);
+ snprintf(tmpr->s->bar_text, sizeof tmpr->s->bar_text,
+ "%s %d", tmp, i);
+ XDrawString(display, tmpr->bar_window, bar_gc, 4,
+ bar_fs->ascent, tmpr->s->bar_text,
+ strlen(tmpr->s->bar_text));
+ }
+ XSync(display, False);
alarm(60);
}
void
bar_signal(int sig)
{
- /* XXX yeah yeah byte me */
- if (cur_focus)
- bar_print(cur_focus->ws->r);
+ bar_alarm = 1;
}
void
bar_toggle(struct swm_region *r, union arg *args)
{
- struct swm_region *tmpr;
- int i, j;
+ struct swm_region *tmpr;
+ int i, j;
DNPRINTF(SWM_D_MISC, "bar_toggle\n");
/* must be after stack */
for (i = 0; i < ScreenCount(display); i++)
TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
- bar_print(tmpr);
+ bar_print();
}
void
if (signal(SIGALRM, bar_signal) == SIG_ERR)
err(1, "could not install bar_signal");
- bar_print(r);
+ bar_print();
}
void
{
DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
+ /* disable alarm because the following code may not be interrupted */
+ alarm(0);
+ if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
+ errx(1, "can't disable alarm");
+
XCloseDisplay(display);
execvp(start_argv[0], start_argv);
fprintf(stderr, "execvp failed\n");
return;
other_r = new_ws->r;
- fprintf(stderr, "switchws: other_r %p\n", other_r);
if (!other_r) {
/* if the other workspace is hidden, switch windows */
if (TAILQ_EMPTY(&screens[k].rl))
continue;
XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
- for(i = 0; i < LENGTH(keys); i++) {
- if((code = XKeysymToKeycode(display, keys[i].keysym)))
- for(j = 0; j < LENGTH(modifiers); j++)
+ for (i = 0; i < LENGTH(keys); i++) {
+ if ((code = XKeysymToKeycode(display, keys[i].keysym)))
+ for (j = 0; j < LENGTH(modifiers); j++)
XGrabKey(display, code,
keys[i].mod | modifiers[j],
screens[k].root, True,
set_win_state(win, WithdrawnState);
free(win);
}
-
stack();
}
DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
- if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
+ if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
ev->window != ev->root)
return;
if (ignore_enter) {
for (i = 0; i < ScreenCount(display); i++)
TAILQ_FOREACH(r, &screens[i].rl, entry)
if (e->xvisibility.window == r->bar_window)
- bar_print(r);
+ bar_print();
}
void (*handler[LASTEvent])(XEvent *) = {
setup_screens(void)
{
#ifdef SWM_XRR_HAS_CRTC
- XRRCrtcInfo *crtc_info;
- XRRScreenResources *res;
+ XRRCrtcInfo *ci;
+ XRRScreenResources *sr;
int c;
#endif /* SWM_XRR_HAS_CRTC */
Window d1, d2, *wins = NULL;
XWindowAttributes wa;
struct swm_region *r;
- unsigned int num;
+ unsigned int no;
int errorbase, major, minor;
- int ncrtc, w = 0;
+ int ncrtc = 0, w = 0;
int i, j, k;
struct workspace *ws;
ws->r = NULL;
TAILQ_INIT(&ws->winlist);
- for (k = 0; layouts[k].l_stack != NULL; k++) {
+ for (k = 0; layouts[k].l_stack != NULL; k++)
if (layouts[k].l_init != NULL)
layouts[k].l_init(ws);
- }
ws->cur_layout = &layouts[0];
}
#endif
/* grab existing windows (before we build the bars)*/
- if (!XQueryTree(display, screens[i].root,
- &d1, &d2, &wins, &num)) {
- if (wins) /* not sure if this is necessary */
- XFree(wins);
+ if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
continue;
- }
#ifdef SWM_XRR_HAS_CRTC
- res = XRRGetScreenResources(display, screens[i].root);
- if (res == NULL) {
- ncrtc = 0;
+ sr = XRRGetScreenResources(display, screens[i].root);
+ if (sr == NULL)
new_region(&screens[i], &screens[i].ws[w],
0, 0, DisplayWidth(display, i),
DisplayHeight(display, i));
- } else
- ncrtc = res->ncrtc;
+ else
+ ncrtc = sr->ncrtc;
for (c = 0; c < ncrtc; c++) {
- crtc_info = XRRGetCrtcInfo(display, res, res->crtcs[c]);
- if (crtc_info->noutput == 0)
+ ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
+ if (ci->noutput == 0)
continue;
- if (crtc_info != NULL && crtc_info->mode == None)
- new_region(&screens[i], &screens[i].ws[w],
- 0, 0, DisplayWidth(display, i),
+ if (ci != NULL && ci->mode == None)
+ new_region(&screens[i], &screens[i].ws[w], 0, 0,
+ DisplayWidth(display, i),
DisplayHeight(display, i));
else
new_region(&screens[i], &screens[i].ws[w],
- crtc_info->x, crtc_info->y,
- crtc_info->width, crtc_info->height);
+ ci->x, ci->y, ci->width, ci->height);
w++;
}
+ XRRFreeCrtcInfo(ci);
+ XRRFreeScreenResources(sr);
#else
- new_region(&screens[i], &screens[i].ws[w],
- 0, 0, DisplayWidth(display, i),
+ new_region(&screens[i], &screens[i].ws[w], 0, 0,
+ DisplayWidth(display, i),
DisplayHeight(display, i));
#endif /* SWM_XRR_HAS_CRTC */
if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
errx(1, "no regions on screen %d", i);
- for (i = 0; i < num; i++) {
+ for (i = 0; i < no; i++) {
XGetWindowAttributes(display, wins[i], &wa);
if (!XGetWindowAttributes(display, wins[i], &wa) ||
wa.override_redirect ||
XGetTransientForHint(display, wins[i], &d1))
continue;
+
if (wa.map_state == IsViewable ||
getstate(wins[i]) == NormalState)
manage_window(wins[i], r->ws);
}
/* transient windows */
- for (i = 0; i < num; i++) {
+ for (i = 0; i < no; i++) {
if (!XGetWindowAttributes(display, wins[i], &wa))
continue;
+
if (XGetTransientForHint(display, wins[i], &d1) &&
(wa.map_state == IsViewable || getstate(wins[i]) ==
NormalState))
manage_window(wins[i], r->ws);
}
- if (wins)
+ if (wins) {
XFree(wins);
+ wins = NULL;
+ }
}
}
char conf[PATH_MAX], *cfile = NULL;
struct stat sb;
XEvent e;
+ int xfd;
+ fd_set rd;
start_argv = argv;
fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
setup_screens();
- //ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
+ /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
grabkeys();
stack();
+ xfd = ConnectionNumber(display);
while (running) {
- XNextEvent(display, &e);
- if (handler[e.type])
- handler[e.type](&e);
+ FD_SET(xfd, &rd);
+ if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
+ if (errno != EINTR)
+ errx(1, "select failed");
+ if (bar_alarm) {
+ bar_alarm = 0;
+ bar_print();
+ }
+ while(XPending(display)) {
+ XNextEvent(display, &e);
+ if (handler[e.type])
+ handler[e.type](&e);
+ }
}
XCloseDisplay(display);