示例#1
0
文件: wmtime.c 项目: cneira/dockapps
void wmtime_routine(int argc, char **argv) {

    rckeys		wmtime_keys[] = {
        { "left", &left_action },
        { "right", &right_action },
        { "middle", &middle_action },
        { NULL, NULL }
    };

    int			i;
    XEvent		Event;
    int			but_stat = -1;

    struct tm	*time_struct;

    long		starttime;
    long		curtime;

    char		*conffile = NULL;

    /* Scan through ~/.wmtimerc for the mouse button actions. */
    if (default_left_action) left_action = strdup(default_left_action);
    if (default_middle_action) middle_action = strdup(default_middle_action);
    if (default_right_action) right_action = strdup(default_right_action);

    /* Scan through the .rc files */
    if (asprintf(&conffile, "/etc/wmtimerc") >= 0) {
        parse_rcfile(conffile, wmtime_keys);
        free(conffile);
    }

    if (asprintf(&conffile, "%s/.wmtimerc", getenv("HOME")) >= 0) {
        parse_rcfile(conffile, wmtime_keys);
        free(conffile);
    }

    if (asprintf(&conffile, "/etc/wmtimerc.fixed") >= 0) {
        parse_rcfile(conffile, wmtime_keys);
        free(conffile);
    }

    /* set user-defined colors */
    if (color[0] != 0) {
        Window	Root;
        XColor col;
        XWindowAttributes attributes;
        int screen;
        Pixel pixel;
#define NUMSYMBOLS 10
        XpmColorSymbol user_color[NUMSYMBOLS] = {
            {NULL, "#2081B2CAAEBA", 0}, /* O */
            {NULL, "#000049244103", 0}, /* + */
            {NULL, "#00007DF771C6", 0}, /* @ */
            {NULL, "#18618A288617", 0}, /* # */
            {NULL, "#18619A699658", 0}, /* ; */
            {NULL, "#0820861779E7", 0}, /* : */
            {NULL, "#000071C66185", 0}, /* > */
            {NULL, "#000061855144", 0}, /* , */
            {NULL, "#00004D344103", 0}, /* < */
            {NULL, "#10407DF779E7", 0}  /* 1 */
        };


        /* code based on GetColor() from wmgeneral.c */
        /* we need a temporary display to parse the color */
        display = XOpenDisplay(NULL);
        screen = DefaultScreen(display);
        Root = RootWindow(display, screen);
        XGetWindowAttributes(display, Root, &attributes);

        col.pixel = 0;
        if (!XParseColor(display, attributes.colormap, color, &col)) {
            fprintf(stderr, "wmtime: can't parse %s.\n", color);
            goto draw_window;
        } else if (!XAllocColor(display, attributes.colormap, &col)) {
            fprintf(stderr, "wmtime: can't allocate %s.\n", color);
            goto draw_window;
        }

        pixel = col.pixel;

        /* replace colors from wmtime-master.xpm */
        user_color[0].pixel = pixel;
        user_color[1].pixel = scale_pixel(pixel, .4);
        user_color[2].pixel = scale_pixel(pixel, .7);
        user_color[3].pixel = scale_pixel(pixel, .8);
        user_color[4].pixel = scale_pixel(pixel, .9);
        user_color[5].pixel = scale_pixel(pixel, .8);
        user_color[6].pixel = scale_pixel(pixel, .6);
        user_color[7].pixel = scale_pixel(pixel, .5);
        user_color[8].pixel = scale_pixel(pixel, .4);
        user_color[9].pixel = scale_pixel(pixel, .7);

        wmgen.attributes.valuemask |= XpmColorSymbols;
        wmgen.attributes.numsymbols = NUMSYMBOLS;
        wmgen.attributes.colorsymbols = user_color;

        XCloseDisplay(display);
    }

draw_window:
    openXwindow(argc, argv, wmtime_master_xpm, (char*)wmtime_mask_bits, 128, 64);

    /* Mask out the right parts of the clock */
    copyXPMArea(0, 0, 128, 64, 0, 98);   /* Draw the borders */
    copyXPMArea(0, 0, 64, 64, 64, 0);    /* Draw the clock face */
    copyXPMArea(64, 98, 64, 64, 0, 0);   /* Draw the LCD background */
    setMaskXY(0, 0);

    /* add mouse region */
    AddMouseRegion(0, 5, 48, 58, 60);
    AddMouseRegion(1, 5, 5, 58, 46);

    starttime = time(0);

    curtime = time(0);
    time_struct = localtime(&curtime);

    while (1) {
        curtime = time(0);

        waitpid(0, NULL, WNOHANG);

        time_struct = localtime(&curtime);


        if (curtime >= starttime) {
            if (!digital) {
                /* Now to update the seconds */

                DrawWijzer(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);

                DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);

            } else {

                DrawTime(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);

                DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);
            }
            RedrawWindow();
        }


        while (XPending(display)) {
            XNextEvent(display, &Event);
            switch (Event.type) {
            case Expose:
                RedrawWindow();
                break;
            case DestroyNotify:
                XCloseDisplay(display);
                exit(0);
                break;
            case ButtonPress:
                but_stat = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
                break;
            case ButtonRelease:
                i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
                if (but_stat == i && but_stat >= 0) {
                    switch (but_stat) {
                    case 0:
                        digital = 1-digital;

                        if (digital) {
                            copyXPMArea(64, 98, 64, 64, 0, 0);
                            DrawTime(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
                            DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);
                        } else {
                            copyXPMArea(0, 98, 64, 64, 0, 0);
                            DrawWijzer(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
                            DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);
                        }
                        RedrawWindow();
                        break;
                    case 1:
                        switch (Event.xbutton.button) {
                        case 1:
                            if (left_action)
                                execCommand(left_action);
                            break;
                        case 2:
                            if (middle_action)
                                execCommand(middle_action);
                            break;
                        case 3:
                            if (right_action)
                                execCommand(right_action);
                            break;
                        }
                    }
                }
                break;
            }
        }

        /* Sleep 0.3 seconds */
        usleep(300000L);
    }
}
示例#2
0
void wmifs_routine(int argc, char **argv)
{

	rckeys	wmifs_keys[] = {
		{ "left", &left_action },
		{ "middle", &middle_action },
		{ "right", &right_action },
		{ NULL, NULL }
	};


	int			i, j;
	XEvent		Event;
	int			but_stat = -1;

	int			stat_online;
	int			stat_current;
	int			first_time = 1;

	unsigned int	curtime;
	unsigned int	nexttime;
	struct timeval	tv, tv2;

	long		ipacket, opacket, istat, ostat;

	char		temp[BUFFER_SIZE];
	char		*p;

	for (i = 0; i < MAX_STAT_DEVICES; i++) {
		stat_devices[i].name[0] = 0;
		for (j = 0; j < 48; j++) {
			stat_devices[i].his[j][0] = 0;
			stat_devices[i].his[j][1] = 0;
		}
	}

	stat_online = checknetdevs();

	stat_current = 0;
	if (active_interface) {
		int isauto = !strcmp(active_interface, "auto");
		for (i = 0; i < stat_online; i++) {
			if ((isauto && stillonline(stat_devices[i].name)) ||
			    !strcmp(stat_devices[i].name, active_interface)) {
				stat_current = i;
				break;
			}
		}
	}

#ifdef LEFT_ACTION
	left_action = strdup(LEFT_ACTION);
#endif
#ifdef MIDDLE_ACTION
	middle_action = strdup(MIDDLE_ACTION);
#endif
#ifdef RIGHT_ACTION
	right_action = strdup(RIGHT_ACTION);
#endif

	/* Scan throught the .rc files */
	parse_rcfile(CONF"/wmifsrc", wmifs_keys);

	p = getenv("HOME");
	if (p == NULL || *p == 0) {
		fprintf(stderr, "Unknown $HOME directory, please check your environment\n");
		return;
	}
	strncpy(temp, p, BUFFER_SIZE - 10);
	strcat(temp, "/.wmifsrc");
	parse_rcfile(temp, wmifs_keys);

	parse_rcfile(CONF"/wmifsrc.fixed", wmifs_keys);

       /* set user-defined colors */
       if (color[0] != 0) {
               Window  Root;
               XColor col;
               XWindowAttributes attributes;
               int screen;
               Pixel pixel;
#define NUMSYMBOLS 4
               XpmColorSymbol user_color[NUMSYMBOLS] = {
                       {NULL, "#2081B2CAAEBA", 0}, /* + */
                       {NULL, "#28A23CF338E3", 0}, /* O */
                       {NULL, "#000049244103", 0}, /* @ */
                       {NULL, "#18618A288617", 0}, /* # */
                        };


               /* code based on GetColor() from wmgeneral.c */
               /* we need a temporary display to parse the color */
               display = XOpenDisplay(NULL);
               screen = DefaultScreen(display);
               Root = RootWindow(display, screen);
               XGetWindowAttributes(display, Root, &attributes);

               col.pixel = 0;
               if (!XParseColor(display, attributes.colormap, color, &col)) {
                       fprintf(stderr, "wmtime: can't parse %s.\n", color);
                       goto draw_window;
               } else if (!XAllocColor(display, attributes.colormap, &col)) {
                       fprintf(stderr, "wmtime: can't allocate %s.\n", color);
                       goto draw_window;
               }

               pixel = col.pixel;

               /* replace colors from wmtime-master.xpm */
               user_color[0].pixel = pixel;
               user_color[1].pixel = scale_pixel(pixel, .3);
               user_color[2].pixel = scale_pixel(pixel, .4);
               user_color[3].pixel = scale_pixel(pixel, .8);

               wmgen.attributes.valuemask |= XpmColorSymbols;
               wmgen.attributes.numsymbols = NUMSYMBOLS;
               wmgen.attributes.colorsymbols = user_color;

               XCloseDisplay(display);
       }

draw_window:
	openXwindow(argc, argv, wmifs_master_xpm, (char*)wmifs_mask_bits, wmifs_mask_width, wmifs_mask_height);

	/* > Button */
	AddMouseRegion(0, 5, 5, 35, 15);
	AddMouseRegion(1, 5, 20, 58, 58);

	gettimeofday(&tv2, NULL);
	nexttime = ScrollSpeed;

	DrawActiveIFS(stat_devices[stat_current].name);

	while (1) {
		struct timespec ts;

		gettimeofday(&tv, NULL);
		curtime = (tv.tv_sec - tv2.tv_sec) * 1000
			+ (tv.tv_usec - tv2.tv_usec) / 1000;

		waitpid(0, NULL, WNOHANG);

		for (i = 0; i < stat_online; i++) {
			get_statistics(stat_devices[i].name, &ipacket, &opacket, &istat, &ostat);

			if (first_time) {
				first_time = 0;
			} else {
				stat_devices[i].his[53][0] += istat - stat_devices[i].istatlast;
				stat_devices[i].his[53][1] += ostat - stat_devices[i].ostatlast;
			}

			if (i == stat_current) {
				if (!stillonline(stat_devices[i].name))
					SetErrLED(LED_NET_POWER);
				else
					SetOnLED(LED_NET_POWER);

				if (stat_devices[i].istatlast == istat)
					SetOffLED(LED_NET_RX);
				else
					SetOnLED(LED_NET_RX);

				if (stat_devices[i].ostatlast == ostat)
					SetOffLED(LED_NET_TX);
				else
					SetOnLED(LED_NET_TX);
			}

			stat_devices[i].istatlast = istat;
			stat_devices[i].ostatlast = ostat;
		}
		RedrawWindow();

		if (curtime >= nexttime) {
			nexttime = curtime + ScrollSpeed;

			DrawStats(&stat_devices[stat_current].his[0][0], 54, 40, 5, 58);
			for (i = 0; i < stat_online; i++) {
				if (stillonline(stat_devices[i].name)) {
					for (j = 1; j < 54; j++) {
						stat_devices[i].his[j-1][0] = stat_devices[i].his[j][0];
						stat_devices[i].his[j-1][1] = stat_devices[i].his[j][1];
					}
					stat_devices[i].his[53][0] = 0;
					stat_devices[i].his[53][1] = 0;
				}
			}
			RedrawWindow();
		}

		while (XPending(display)) {
			XNextEvent(display, &Event);
			switch (Event.type) {
			case Expose:
				RedrawWindow();
				break;
			case DestroyNotify:
				XCloseDisplay(display);
				exit(0);
				break;
			case ButtonPress:
				but_stat = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
				break;
			case ButtonRelease:
				i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);

				if (but_stat == i && but_stat >= 0) {
					switch (but_stat) {
					case 0:
						/* re-read the table */
						strcpy(temp, stat_devices[stat_current].name);
						stat_online = checknetdevs();
						stat_current = 0;
						for (i = 0; i < stat_online; i++) {
							if (!strcmp(temp, stat_devices[i].name))
								stat_current = i;
						}

						stat_current++;
						if (stat_current == stat_online)
							stat_current = 0;

						DrawActiveIFS(stat_devices[stat_current].name);

						DrawStats(&stat_devices[stat_current].his[0][0], 54, 40, 5, 58);
						break;
					case 1:
						switch (Event.xbutton.button) {
						case 1:
							if (left_action)
								execCommand(left_action);
							break;
						case 2:
							if (middle_action)
								execCommand(middle_action);
							break;
						case 3:
							if (right_action)
								execCommand(right_action);
							break;
						}
						break;

					}
				}
				but_stat = -1;
				RedrawWindow();
				break;
			}
		}
		ts.tv_sec = 0;
		ts.tv_nsec = SampleInt * 1000000;
		nanosleep(&ts, NULL);
	}
}