shithub: rio

ref: e9cfa1cdb931d4a611deac188a3f16e12603f57e
dir: /wallctl.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
#include <mouse.h>
#include <keyboard.h>
#include <frame.h>
#include <fcall.h>
#include <plumb.h>
#include "dat.h"
#include "fns.h"
#include <ctype.h>
#include <stdio.h> //TODO: remove

enum
{
	WallSet,
	WallReset,
	WallTile,
	WallFitW,
	WallFitH
};

static char *cmds[] = {
	[WallSet] = "wallset",
	[WallReset] = "wallreset",
	[WallTile] = "walltile",
	[WallFitW] = "wallfitw",
	[WallFitH] = "wallfith",
	nil
};


static
int
word(char **sp, char *tab[])
{
	char *s, *t;
	int i;

	s = *sp;
	while(isspace(*s))
		s++;
	t = s;
	while(*s!='\0' && !isspace(*s))
		s++;
	for(i=0; tab[i]!=nil; i++)
		if(strncmp(tab[i], t, strlen(tab[i])) == 0){
			*sp = s;
			return i;
	}
	return -1;
}


int
parsewallctl(char **argp, char *s, char *err)
{
	int cmd;

	cmd = word(&s, cmds);
	if(cmd < 0){
		strcpy(err, "unrecognized wctl command");
		return -1;
	}

	while(isspace(*s))
		s++;
	if(cmd!=WallSet && *s!='\0'){
		strcpy(err, "extraneous text in wctl message");
		return -1;
	}

	if(argp)
		*argp = s;
	else
		strcpy(err, "missing or bad wallctl parameter, need path");

	return cmd;
}

int
writewallctl(Xfid *x, char *err)
{
	int cmd;
	char *arg, *tok;

	x->data[x->count] = '\0';

	cmd = parsewallctl(&arg, x->data, err);

	if(cmd < 0)
		return -1;

	switch(cmd){
	case WallSet:
		tok = strchr(arg, '\n');
		if(tok != nil)
			arg[tok - arg] = '\0';
		wallpath = arg;
		resized(); //this will set the wallpaper and handle re-drawing
		return 1;
	}

	return 1;
}