view lcdemu/main.c @ 106:18b3183611fb

target-utils/pirexplore: vibe command alias for buz
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 01 Nov 2016 01:00:43 +0000
parents e7502631a0f9
children
line wrap: on
line source

/*
 * LCDemu main module
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include "globals.h"

main(argc, argv)
	char **argv;
{
	XrmInitialize();
	process_cmdline(argc, argv);
	open_display();
	init_image_conversion();
	load_resources();
	create_our_window();
	set_initial_window_title();
	set_initial_icon_name();
	create_mainwin_gc();
	XMapWindow(mydisplay, mainwindow);
	XFlush(mydisplay);

	mainloop();
	/* NOTREACHED */
}

process_cmdline(argc, argv)
	char **argv;
{
	register char **ap, *opt;
	char *rhost, *ruser;
	int len;

	if (argc < 1) {
		fprintf(stderr, "fc-lcdemu: invalid invokation\n");
		exit(1);
	}
	opt = rindex(argv[0], '/');
	if (opt)
		progbasename = opt + 1;
	else
		progbasename = argv[0];
	proginstancename = progbasename;
	for (ap = argv+1; *ap; ) {
		if (**ap == '-')
			opt = *ap++;
		else
			break;
		if (!strcmp(opt, "-display")) {
			if (!*ap) {
argreq:				fprintf(stderr, "%s: %s requires an argument\n",
					progbasename, opt);
				exit(1);
			}
			mydisplayname = *ap++;
			continue;
		}
		if (!strcmp(opt, "-name")) {
			if (!*ap)
				goto argreq;
			proginstancename = *ap++;
			continue;
		}
		if (!strcmp(opt, "-geometry") || !strcmp(opt, "-geom")) {
			if (!*ap)
				goto argreq;
			XrmPutStringResource(&xrmdb_cmdline, "LCDemu.geometry",
						*ap++);
			continue;
		}
		if (!strcmp(opt, "-iconic")) {
			XrmPutStringResource(&xrmdb_cmdline, "LCDemu.iconic",
						"on");
			continue;
		}
		if (!strcmp(opt, "-title")) {
			if (!*ap)
				goto argreq;
			XrmPutStringResource(&xrmdb_cmdline, "LCDemu.title",
						*ap++);
			continue;
		}
		if (!strcmp(opt, "-borderwidth") || !strcmp(opt, "-bw")) {
			if (!*ap)
				goto argreq;
			XrmPutStringResource(&xrmdb_cmdline, "*borderWidth",
						*ap++);
			continue;
		}
		if (!strcmp(opt, "-bordercolor") || !strcmp(opt, "-bd")) {
			if (!*ap)
				goto argreq;
			XrmPutStringResource(&xrmdb_cmdline, "*borderColor",
						*ap++);
			continue;
		}
		if (!strcmp(opt, "-xrm")) {
			if (!*ap)
				goto argreq;
			XrmPutLineResource(&xrmdb_cmdline, *ap++);
			continue;
		}
		fprintf(stderr, "%s: %s: unrecognized option\n", progbasename,
			opt);
		exit(1);
	}
}

open_display()
{
	if (!mydisplayname)
		mydisplayname = getenv("DISPLAY");
	if (!mydisplayname) {
		fprintf(stderr, "%s: no X display available\n", progbasename);
		exit(1);
	}
	mydisplay = XOpenDisplay(mydisplayname);
	if (!mydisplay) {
		fprintf(stderr, "%s: unable to open display %s\n", progbasename,
			mydisplayname);
		exit(1);
	}
}

mainloop()
{
	register int i, cc;
	XEvent event;
	fd_set readfds;
	int maxfd;
	char buf[1024];

	maxfd = ConnectionNumber(mydisplay) + 1;
	for (;;) {
		cc = XPending(mydisplay);
		for (i = 0; i < cc; i++)
			XNextEvent(mydisplay, &event);
		XFlush(mydisplay);
		FD_ZERO(&readfds);
		FD_SET(0, &readfds);
		FD_SET(ConnectionNumber(mydisplay), &readfds);
		i = select(maxfd, &readfds, NULL, NULL, NULL);
		if (i < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			exit(1);
		}
		if (FD_ISSET(0, &readfds)) {
			cc = read(0, buf, sizeof buf);
			if (cc > 0)
				input_on_stdin(buf, cc);
			else
				exit(0);
			XFlush(mydisplay);
		}
	}
}