~amavect/makeu

8a2a5c28d2be01cfe61cca07bbf2583f5e499bf0 — amavect 1 year, 2 months ago fb7a09c master
revise error exiting, allocation errors
8 files changed, 83 insertions(+), 64 deletions(-)

M color.c
M fns.h
M hsvmap.c
M makeu.c
M mkfile
M screenpick.c
M slider.c
A util.c
M color.c => color.c +1 -16
@@ 1,5 1,5 @@
/* Amavect! */
/* Common functions */
/* color functions */
#include <u.h>
#include <libc.h>
#include <draw.h>


@@ 74,21 74,6 @@ torgb(RGBd rgbd)
	return rgb;
}

/* clips the point to be the nearest point inside of r (excluding max) */
Point
ptclip(Point p, Rectangle r)
{
	if(p.x > r.max.x - 1)
		p.x = r.max.x - 1;
	else if(p.x < r.min.x)
		p.x = r.min.x;
	if(p.y > r.max.y - 1)
		p.y = r.max.y - 1;
	else if(p.y < r.min.y)
		p.y = r.min.y;
	return p;
}

/* 
 * map HSV to RGB
 * implemented off of wikipedia lol

M fns.h => fns.h +5 -2
@@ 30,7 30,10 @@ RGBd hsvdtorgbd(HSVd);
HSVd rgbdtohsvd(RGBd);
HSVd rgbtohsvd(RGB);
RGB hsvdtorgb(HSVd);
ulong strtocolor(char *str);

/* misc */
/* util */
Point ptclip(Point, Rectangle);
ulong strtocolor(char *str);
void error(char*);
Image *eallocimage(Rectangle, ulong, int, ulong);
void *emalloc(ulong);

M hsvmap.c => hsvmap.c +8 -14
@@ 159,10 159,8 @@ hsvmapinit(Elementile *e)
		h->pos = (Ratioxy){0,0};
		h->maptype = 0;
	}
	h->circ = allocimage(display, Rect(-4,-4,5,5), RGBA32, 0, DTransparent);
	h->pix = allocimage(display, Rect(0,0,1,1), RGBA32, 1, DTransparent);
	if(h->circ == nil || h->pix == nil)
		sysfatal("RIP hsvmap, your images didn't alloc: %r");
	h->circ = eallocimage(Rect(-4,-4,5,5), RGBA32, 0, DTransparent);
	h->pix = eallocimage(Rect(0,0,1,1), RGBA32, 1, DTransparent);
	ellipse(h->circ, Pt(0,0), 4, 4, 0, display->black, ZP);
	h->huex = nil;
	h->satx = nil;


@@ 195,16 193,12 @@ genmapimage(HSVmap *h)
	freeimage(h->saty);
	freeimage(h->valy);
	
	rbuf = malloc(rbs);
	cbuf = malloc(cbs);
	h->huex = allocimage(display, Rect(0,0,r.max.x,1), RGB24, 1, DNofill);
	h->satx = allocimage(display, Rect(0,0,r.max.x,1), RGBA32, 1, DNofill);
	h->saty = allocimage(display, Rect(0,0,1,r.max.y), RGBA32, 1, DNofill);
	h->valy = allocimage(display, Rect(0,0,1,r.max.y), RGBA32, 1, DNofill);
	if(rbuf == nil || cbuf == nil || 
			h->huex == nil || h->satx == nil || 
			h->saty == nil || h->valy == nil)
		sysfatal("ctorimages failed: %r");
	rbuf = emalloc(rbs);
	cbuf = emalloc(cbs);
	h->huex = eallocimage(Rect(0,0,r.max.x,1), RGB24, 1, DNofill);
	h->satx = eallocimage(Rect(0,0,r.max.x,1), RGBA32, 1, DNofill);
	h->saty = eallocimage(Rect(0,0,1,r.max.y), RGBA32, 1, DNofill);
	h->valy = eallocimage(Rect(0,0,1,r.max.y), RGBA32, 1, DNofill);
	
	for(i = 0; i < r.max.x; i++){
		pd = ratioxy(Pt(i,ymax), r);

M makeu.c => makeu.c +6 -10
@@ 10,8 10,6 @@
#include "fns.h"
#include "components.h"

extern Channel* init9pfs(char*, char*, int);

void mapevent(HSVd);
void pickevent(ulong);
void redevent(double);


@@ 300,7 298,7 @@ kbdproc(void *aux)
	kbdc = aux;
	threadsetname("kbdproc");
	if((kfd = open("/dev/kbd", OREAD)) < 0)
		sysfatal("/dev/kbd: %r");
		error("kbdproc");
	
	for(;;){
		/* could be improved with detecting incomplete


@@ 324,7 322,7 @@ void
egetwindow(void)
{
	if(getwindow(display, Refnone) < 0)
		sysfatal("Cannot reconnect to display: %r");
		error("cannot reconnect to display");
	freeimage(screenbuf);
	screenbuf = allocimage(display, screen->r, screen->chan, 0, DBlack);
}


@@ 424,9 422,9 @@ threadmain(int argc, char **argv)
	fmtinstall(L'φ', φfmt);
	
	if(initdraw(nil, nil, argv0) < 0)
		sysfatal("%r");
		error("cannot initdraw");
	if((mctl = initmouse(nil, screen)) == nil)
		sysfatal("%r");
		error("cannot initmouse");
	kbdc = chancreate(sizeof(Rune), 32);
	proccreate(kbdproc, kbdc, 0x2000);
	stdinc = chancreate(sizeof(ulong), 8);


@@ 451,9 449,7 @@ threadmain(int argc, char **argv)
	va.val = hsvd.val;
	aa.val = alpha;
	
	screenbuf = allocimage(display, screen->r, screen->chan, 0, DBlack);
	if(screenbuf == nil)
		sysfatal("%r");
	screenbuf = eallocimage(screen->r, screen->chan, 0, DBlack);
	root->init(root);
	root->resize(root, screenbuf->r);
	show();


@@ 505,7 501,7 @@ threadmain(int argc, char **argv)
			show();
			break;
		case NONE:
			print("I'm a woodchuck, not a woodchucker! (thanks for playing)\n");
			error("I'm a woodchuck, not a woodchucker! (thanks for playing)");
			break;
		}
	}

M mkfile => mkfile +1 -1
@@ 3,7 3,7 @@
MAN=/sys/man
BIN=/$objtype/bin
HFILES=fns.h components.h elementile.h
OFILES=color.$O guipart.$O makeu.$O hsvmap.$O screenpick.$O slider.$O
OFILES=color.$O guipart.$O makeu.$O hsvmap.$O screenpick.$O slider.$O util.$O
TARG=$BIN/makeu
MANFILES=$MAN/1/makeu


M screenpick.c => screenpick.c +9 -13
@@ 85,13 85,11 @@ screenpickinit(Elementile *e)
	sp->state = 0;
	sp->screenfd = open("/dev/screen", OREAD);
	if(sp->screenfd < 0)
		sysfatal("%r");
		error("screenpickinit");
	sp->screenline = nil;
	sp->linesize = 0;
	sp->linebuf = nil;
	sp->pixel = allocimage(display, Rect(0,0,1,1), RGBA32, 1, sp->color);
	if(sp->pixel == nil)
		sysfatal("download more ram plz: %r");
	sp->pixel = eallocimage(Rect(0,0,1,1), RGBA32, 1, sp->color);
	snprint(hex, sizeof(hex), "%08ulX", setalpha(sp->color, sp->color & 0xFF));
	strsize = stringsize(display->defaultfont, hex);
	strsize.y *= 4;


@@ 122,15 120,15 @@ screenpickresize(Elementile *e, Rectangle r)
	 * Need a second opinion.
	 */
	if(pread(sp->screenfd, chanstr, 12, 0) < 12)
		sysfatal("%r");
		error("bad chanstr");
	if(pread(sp->screenfd, minxstr, 12, 12) < 12)
		sysfatal("%r");
		error("bad min.x");
	if(pread(sp->screenfd, minystr, 12, 2*12) < 12)
		sysfatal("%r");
		error("bad min.y");
	if(pread(sp->screenfd, maxxstr, 12, 3*12) < 12)
		sysfatal("%r");
		error("bad max.x");
	if(pread(sp->screenfd, maxystr, 12, 4*12) < 12)
		sysfatal("%r");
		error("bad max.y");
	chanstr[11] = 0;
	minxstr[11] = 0;
	minystr[11] = 0;


@@ 142,11 140,9 @@ screenpickresize(Elementile *e, Rectangle r)
	rect.max.x = (int)strtol(maxxstr, nil, 0);
	rect.max.y = (int)strtol(maxystr, nil, 0);
	
	sp->screenline = allocimage(display, Rect(0,0,rect.max.x,1), chantype, 0, DWhite);
	sp->screenline = eallocimage(Rect(0,0,rect.max.x,1), chantype, 0, DWhite);
	sp->linesize = bytesperline(rect, chantodepth(chantype));
	sp->linebuf = malloc(sp->linesize);
	if(sp->screenline == nil || sp->linebuf == nil)
		sysfatal("download more ram plz: %r");
	sp->linebuf = emalloc(sp->linesize);
	
	redraw(sp);
}

M slider.c => slider.c +4 -8
@@ 58,10 58,8 @@ sliderinit(Elementile *e)
	Point pfont;
	char txt[32];
	sl = e->aux;
	sl->bg = allocimage(display, Rect(0,0,1,1), RGB24, 1, 0xAAAAAAFF);
	sl->fg = allocimage(display, Rect(0,0,1,1), RGB24, 1, 0x333333FF);
	if(sl->bg == nil || sl->fg == nil)
		sysfatal("get more ram dude: %r");
	sl->bg = eallocimage(Rect(0,0,1,1), RGB24, 1, 0xAAAAAAFF);
	sl->fg = eallocimage(Rect(0,0,1,1), RGB24, 1, 0x333333FF);
	sl->bka = nil;
	sl->wta = nil;
	n = snprint(txt, 64, sl->fmt, sl->val);


@@ 117,10 115,8 @@ sliderresize(Elementile *e, Rectangle r)
		rb = Rect(0, -pfont.y, pfont.x, pfont.y);
		rw = Rect(-pfont.x, -pfont.y, 0, pfont.y);
	}
	sl->bka = allocimage(display, rb, RGBA32, 0, DTransparent);
	sl->wta = allocimage(display, rw, RGBA32, 0, DTransparent);
	if(sl->bka == nil || sl->wta == nil)
		sysfatal("get more ram bruh: %r");
	sl->bka = eallocimage(rb, RGBA32, 0, DTransparent);
	sl->wta = eallocimage(rw, RGBA32, 0, DTransparent);
	fillpoly(sl->bka, ptb, nelem(ptb), 1, display->black, ZP);
	fillpoly(sl->wta, ptw, nelem(ptw), 1, display->white, ZP);
	redraw(sl);

A util.c => util.c +49 -0
@@ 0,0 1,49 @@
/* Amavect! */
/* misc functions */
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include "fns.h"

/* clips the point to be the nearest point inside of r (excluding max) */
Point
ptclip(Point p, Rectangle r)
{
	if(p.x > r.max.x - 1)
		p.x = r.max.x - 1;
	else if(p.x < r.min.x)
		p.x = r.min.x;
	if(p.y > r.max.y - 1)
		p.y = r.max.y - 1;
	else if(p.y < r.min.y)
		p.y = r.min.y;
	return p;
}

void
error(char *s)
{
	fprint(2, "%s: %s: %r\n", argv0, s);
	threadexitsall(s);
}

Image *
eallocimage(Rectangle r, ulong chan, int repl, ulong col)
{
	Image *i;
	i = allocimage(display, r, chan, repl, col);
	if(i == nil)
		error("cannot allocimage");
	return i;
}

void *
emalloc(ulong size)
{
	void *p;
	p = malloc(size);
	if(p == nil)
		error("cannot malloc");
	return p;
}