/* Copyright (c) 1999 Decklin Foster <fosterd@hartwick.edu>
   Copyright (c) 2000 Linus Nilsson <d96ln@efd.lth.se> */

#include "yawm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <X11/cursorfont.h>

Display *dpy;
Display *child_dpy;
int screen;
int current_desk = 1;
Window root;
Window taskbar;
XFontStruct *font;
int iheight;
Client *head_client;
Atom xa_wm_state;
Atom xa_wm_change_state;
Atom xa_wm_protos;
Atom xa_wm_delete;
Atom xa_wm_cmapwins;
Cursor move_curs;
Cursor resize_curs;
Client *focused_client;
#ifdef SHAPE
Bool shape;
int shape_event;
#endif
int opt_bw = DEF_BW;
char *opt_display = "";
char *opt_font = DEF_FONT;
char *opt_new1 = DEF_NEW1;

// Color variables
GC invert_gc;
GC string_gc;
GC focus_string_gc;
GC hidden_string_gc;
GC normal_string_gc;
GC border_gc;
GC focus_bg_gc;
GC focus_fg_gc;
GC taskbar_focus_fg_gc;
GC taskbar_bg_gc;
XColor fg, bg, bd, focus_bg, focus_fg, black, hidden_fg;
XColor normal_fg, taskbar_focus_fg, taskbar_bg;
char *opt_fg = TITLE_FG;
char *opt_bg = TITLE_BG;
char *opt_bd = BORDER_COLOR;
char *opt_focus_bg = TITLE_FOCUS_BG;
char *opt_focus_fg = TITLE_FOCUS_FG;
char *opt_taskbar_bg = TASKBAR_BG;
char *opt_taskbar_focus_fg = TASKBAR_FOCUS_FG;
char *opt_hidden_fg = TASKBAR_HIDDEN_FG;
char *opt_normal_fg = TASKBAR_NORMAL_FG;
char *opt_black = "black";

int main(int argc, char *argv[]) {
	setup_display();
	scan_windows();
	if (USE_TASKBAR) {
		start_taskbar();
	}
	draw_tasks();
	do_event_loop();
	exit(0); 
}

void sighandle(int dummy)
{
	 fprintf( stderr, "Catched Signal\n");
}

void setsighandle( int signl )
{
        if( signal(signl,SIG_IGN) != SIG_IGN )
                signal( signl, sighandle );
}


void setup_display() {
	XGCValues gv;
	XSetWindowAttributes attr;
	XColor dummy;
#ifdef SHAPE
	int dummy2;
#endif

	XSetErrorHandler(handle_xerror);
        setsighandle( SIGINT );
        setsighandle( SIGHUP );
        setsighandle( SIGQUIT );
        setsighandle( SIGTERM );
        signal( SIGSEGV, quit_nicely );
	

	dpy = XOpenDisplay(opt_display);

	if (!dpy) { 
		err("can't open display %s", opt_display);
		exit(1);
	}

	screen = DefaultScreen(dpy);
	root = RootWindow(dpy, screen);

	xa_wm_state = XInternAtom(dpy, "WM_STATE", False);
	xa_wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
	xa_wm_protos = XInternAtom(dpy, "WM_PROTOCOLS", False);
	xa_wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
	xa_wm_cmapwins = XInternAtom(dpy, "WM_COLORMAP_WINDOWS", False);

	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_fg, &fg, 
			 &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_bg, &bg, 
			 &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_bd, &bd, 
			 &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_focus_fg, 
			 &focus_fg, &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_focus_bg, 
			 &focus_bg, &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_black, 
			 &black, &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_hidden_fg, 
			 &hidden_fg, &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_normal_fg, 
			 &normal_fg, &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), 
			 opt_taskbar_focus_fg, 
			 &taskbar_focus_fg, &dummy);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_taskbar_bg, 
			 &taskbar_bg, &dummy);

	font = XLoadQueryFont(dpy, opt_font);
	if (!font) font = XLoadQueryFont(dpy, DEF_FONT);
	iheight = font->ascent + font->descent + 2*SPACE;

#ifdef SHAPE
	shape = XShapeQueryExtension(dpy, &shape_event, &dummy2);
#endif
	move_curs = XCreateFontCursor(dpy, XC_fleur);
	resize_curs = XCreateFontCursor(dpy, XC_plus);

	gv.function = GXcopy;
	gv.foreground = fg.pixel;
	gv.font = font->fid;
	gv.line_width = opt_bw;

	string_gc = XCreateGC(dpy, root, GCLineWidth | GCFunction |
			      GCForeground | GCFont, &gv);

	gv.foreground = focus_fg.pixel;
	focus_string_gc = XCreateGC(dpy, root, GCLineWidth | GCFunction | 
				    GCForeground | GCFont, &gv);
	
	gv.foreground = hidden_fg.pixel;
	hidden_string_gc = XCreateGC(dpy, root, GCLineWidth | GCFunction | 
				     GCForeground | GCFont, &gv);
	
	gv.foreground = normal_fg.pixel;
	normal_string_gc = XCreateGC(dpy, root, GCLineWidth | GCFunction | 
				     GCForeground | GCFont, &gv);
	
	gv.foreground = taskbar_focus_fg.pixel;
	taskbar_focus_fg_gc = XCreateGC(dpy, root, GCLineWidth | GCFunction | 
					GCForeground | GCFont, &gv);

	gv.foreground = bd.pixel;
	border_gc = XCreateGC(dpy, root, GCFunction | GCForeground | 
			      GCLineWidth, &gv);
	
	gv.foreground = focus_bg.pixel;
	focus_bg_gc = XCreateGC(dpy, root, GCFunction | GCForeground | 
				GCLineWidth, &gv);

	gv.function = GXinvert;
	gv.subwindow_mode = IncludeInferiors;
	invert_gc = XCreateGC(dpy, root, GCFunction | GCSubwindowMode | 
			      GCLineWidth | GCFont, &gv);
	
	attr.event_mask = ChildMask | ColormapChangeMask | ButtonMask | 
		PropertyChangeMask | KeyPressMask;
	XChangeWindowAttributes(dpy, root, CWEventMask, &attr);
}

void scan_windows() {
	unsigned int i, nwins;
	Window dw1, dw2, *wins;
	XWindowAttributes attr;

	XQueryTree(dpy, root, &dw1, &dw2, &wins, &nwins);
	for (i = 0; i < nwins; i++) {
		XGetWindowAttributes(dpy, wins[i], &attr);
		if (!attr.override_redirect && attr.map_state == IsViewable)
			if (wins[i] != root) {
				make_new_client(wins[i]);
			}
	}
	XFree(wins);
}


syntax highlighted by Code2HTML, v. 0.9.1