shithub: lola

ref: f42ae8d3b918c57f5c82d192aef0b9feccd7a313
dir: /inc.h/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <keyboard.h>
#include <mouse.h>
#include <cursor.h>
#include <frame.h>
#include <fcall.h>
#include <9p.h>
#include <complete.h>
#include <plumb.h>

typedef uchar bool;
enum {
	FALSE = 0,
	TRUE = 1
};

#define ALT(c, v, t) (Alt){ c, v, t, nil, nil, 0 };

#define CTRL(c) ((c)&0x1F)

extern Rune *snarf;
extern int nsnarf;
extern int snarfversion;
extern int snarffd;
enum { MAXSNARF = 100*1024 };
void putsnarf(void);
void getsnarf(void);
void setsnarf(char *s, int ns);

typedef struct Text Text;
struct Text
{
	Frame;
	Rectangle scrollr, lastsr;
	Image *i;
	Rune *r;
	uint nr;
	uint maxr;
	uint org;	/* start of Frame's text */
	uint q0, q1;	/* selection */
	uint qh;	/* host point, output here */

	/* not entirely happy with this in here */
	bool rawmode;
	Rune *raw;
	int nraw;

	int posx;
};

void xinit(Text *x, Rectangle textr, Rectangle scrollr, Font *ft, Image *b, Image **cols);
void xsetrects(Text *x, Rectangle textr, Rectangle scrollr);
void xclear(Text *x);
void xredraw(Text *x);
uint xinsert(Text *x, Rune *r, int n, uint q0);
void xfill(Text *x);
void xdelete(Text *x, uint q0, uint q1);
void xsetselect(Text *x, uint q0, uint q1);
void xselect(Text *x, Mousectl *mc);
void xscrdraw(Text *x);
void xscroll(Text *x, Mousectl *mc, int but);
void xscrolln(Text *x, int n);
void xtickupdn(Text *x, int d);
void xshow(Text *x, uint q0);
void xplacetick(Text *x, uint q);
void xtype(Text *x, Rune r);
int xninput(Text *x);
void xaddraw(Text *x, Rune *r, int nr);
void xlook(Text *x);
void xsnarf(Text *x);
void xcut(Text *x);
void xpaste(Text *x);
void xsend(Text *x);
int xplumb(Text *w, char *dir, int maxsize);

enum
{
	// NCOL is defined by libframe, add more after it
	TITLE = NCOL,
	LTITLE,
	TITLEHOLD,
	LTITLEHOLD,

	PALETEXT,
	HOLDTEXT,
	PALEHOLDTEXT,

	NumColors
};

extern Image *colors[NumColors];
extern Screen *wscreen;
extern Mousectl *mctl;
extern int scrolling;
extern char *startdir;
extern int shiftdown;
extern int gotscreen;
extern int servekbd;

extern Cursor whitearrow;
extern Cursor *cursor;
void setcursoroverride(Cursor *c, int ov);
void setcursornormal(Cursor *c);


typedef struct RuneConvBuf RuneConvBuf;
struct RuneConvBuf
{
	char *buf;
	int maxbuf;	// allocated size
	int nb;		// size
	int n;		// filled
};

typedef struct Stringpair Stringpair;
struct Stringpair	/* rune and nrune or byte and nbyte */
{
	void		*s;
	int		ns;
};

typedef struct Mousestate Mousestate;
struct Mousestate
{
	Mouse;
	ulong	counter;	/* serial no. of mouse event */
};

typedef struct Mousequeue Mousequeue;
struct Mousequeue
{
	Mousestate	q[16];
	int	ri;	/* read index into queue */
	int	wi;	/* write index */
	ulong	counter;	/* serial no. of last mouse event we received */
	ulong	lastcounter;	/* serial no. of last mouse event sent to client */
	int	lastb;	/* last button state we received */
	bool	full;	/* filled the queue; no more recording until client comes back */	
};

typedef struct Kbdqueue Kbdqueue;
struct Kbdqueue
{
	char *q[32];
	int ri;
	int wi;
	bool full;
};

enum
{
	Closed,
	Reshaped,
	Deleted,
	Refresh,
	Holdon,
	Holdoff,
	Rawon,
	Rawoff,
	Wakeup
};

typedef struct Wctlmesg Wctlmesg;
struct Wctlmesg
{
	int type;
	Rectangle r;
	void *p;
};

typedef struct Window Window;
struct Window
{
	Ref;
	bool deleted;
	bool hidden;
	Window *lower;
	Window *higher;
	Image *img;
	int id;
	char name[32];
	int namecount;
	char *label;
	bool noborder;
	Rectangle contrect;
	int notefd;
	char *dir;

	Text text;
	Rectangle scrollr;
	Rectangle textr;
	bool scrolling;
	int holdmode;

	Mousectl mc;
	Mousequeue mq;
	int mouseopen;
	int resized;

	Cursor *cursorp;
	Cursor cursor;

	Channel *kbd;
	Kbdqueue kq;
	bool consctlopen;
	bool kbdopen;

	Channel *gone;		// window gone
	Channel *ctl;		// Wctlmesg
	/* channels to xfids */
	Channel *conswrite;
	Channel *consread;
	Channel *kbdread;
	Channel *mouseread;
	Channel *complete;
};

extern Window *bottomwin, *topwin;
extern Window *windows[1000];	// TMP
extern int nwindows;
extern Window *hidden[1000];
extern int nhidden;
extern Window *focused, *cursorwin;

void wdecor(Window *w);
void wresize(Window *w, Rectangle r);
Window *wcreate(Rectangle r);
int wrelease(Window *w);
void wsendmsg(Window *w, int type, Rectangle r, void *p);
Window *wfind(int id);
Window *wpointto(Point pt);
void wsetcursor(Window *w);
void wsetlabel(Window *w, char *label);
void wmove(Window *w, Point pos);
void wrmove(Window *w, Point delta);
void wrmovescreen(Point delta);
void wraise(Window *w);
void wlower(Window *w);
void wfocus(Window *w);
void whide(Window *w);
void wunhide(Window *w);
void wsethold(Window *w, int hold);
void wmovemouse(Window *w, Point pt);
void wtype(Window *w, Rune r);
void wsetname(Window *w);
void wsetpid(Window *w, int pid, int dolabel);
void winshell(void *args);


Rectangle newrect(void);

extern Srv fsys;
void fs(void);
int fsmount(int id);

#define	runemalloc(n)		malloc((n)*sizeof(Rune))
#define	runerealloc(a, n)	realloc(a, (n)*sizeof(Rune))
#define	runemove(a, b, n)	memmove(a, b, (n)*sizeof(Rune))
#define min(a, b)	((a) < (b) ? (a) : (b))
#define max(a, b)	((a) > (b) ? (a) : (b))

void panic(char *s);
void *emalloc(ulong size);
void *erealloc(void *p, ulong size);
char *estrdup(char *s);
int handlebs(Stringpair *pair);
void cnvsize(RuneConvBuf *cnv, int nb);
int r2bfill(RuneConvBuf *cnv, Rune *rp, int nr);
void r2bfinish(RuneConvBuf *cnv, Stringpair *pair);
Stringpair b2r(RuneConvBuf *cnv);


typedef struct Timer Timer;
struct Timer
{
	int		dt;
	int		cancel;
	Channel	*c;	/* chan(int) */
	Timer	*next;
};
void timerinit(void);
Timer *timerstart(int dt);
void timerstop(Timer *t);
void timercancel(Timer *t);