diff options
-rw-r--r-- | spred.c | 328 |
1 files changed, 149 insertions, 179 deletions
@@ -4,11 +4,16 @@ #include <X11/Xatom.h> #include <X11/keysym.h> +#include <stdlib.h> #include <stdio.h> #include <string.h> -#include <fcntl.h> #include <math.h> +#include <unistd.h> +#include <fcntl.h> + +#include <arpa/inet.h> + #include "Xbit.h" #define WINX 270 @@ -23,12 +28,12 @@ #define ABS(x) ((x)<0 ? (-(x)) : (x)) #define SGN(x) ((x) ? ((x)/ABS(x)) : (0)) -XSizeHints size_hints; -int black,white; -Display *disp; -Window window; -Colormap cm; -XFontStruct *font; +static XSizeHints size_hints; +static ulong black,white; +static Display *disp; +static Window window; +static Colormap cm; +static XFontStruct *font; #define CMAP_R 4 @@ -86,28 +91,39 @@ XFontStruct *font; #define RND_MIN 255 -typedef struct +typedef struct Button { int x,y,w,h; char *text; - int (*redraw)(); - int (*callback)(); + void (*redraw)(struct Button *b); + void (*callback)(void); } Button; -int RdCmapBut(),CmapBut(); -int RdSprSaveBut(),SprSaveBut(); -int RdSprLoadBut(),SprLoadBut(); -int RdSetCol(),SetCol(); -int RdSpreadBut(),SpreadBut(); -int RdLoadCmapBut(),LoadCmapBut(); -int RdDrawMode(),DrawMode(); -int RdClearToBut(),ClearToBut(); -int RdClearBut(),ClearBut(); -int RdSetSprBut(),SetSprBut(); -int RdQuitBut(),QuitBut(); +static void RdCmapBut(Button *b); +static void CmapBut(); +static void RdSprSaveBut(Button *b); +static void SprSaveBut(); +static void RdSprLoadBut(Button *b); +static void SprLoadBut(); +static void RdSetCol(Button *b); +static void SetCol(); +static void RdSpreadBut(Button *b); +static void SpreadBut(); +static void RdLoadCmapBut(Button *b); +static void LoadCmapBut(); +static void RdDrawMode(Button *b); +static void DrawMode(); +static void RdClearToBut(Button *b); +static void ClearToBut(); +static void RdClearBut(Button *b); +static void ClearBut(); +static void RdSetSprBut(Button *b); +static void SetSprBut(); +static void RdQuitBut(Button *b); +static void QuitBut(); #define NO_BUT 11 -Button butlist[NO_BUT]= +static Button butlist[NO_BUT]= { {1,10,90,20, "Save Cmap", @@ -155,27 +171,45 @@ Button butlist[NO_BUT]= QuitBut} }; -#define MAXSTRLEN 64 +static void SetCmFade(int c1, int c2, XColor xc[], int fl); +static void MainLoop(int argc, char *argv[]); +static void NullStr(void); +static void RedrawAll(void); +static void RedrawButs(void); +static void RedrawCmap(void); +static void RedrawSel(void); +static void RedrawStr(void); +static void RedrawSpr(void); +static void SpritePress(int x, int y, int b); +static void SprCircle(int x, int y, int rx, int ry, int c); +static void SprLine(int p1x,int p1y,int p2x,int p2y,int c); +static void SprSet(int x,int y,int c); +static void SprPlot(int x,int y,int c); +static void SprFill(int x, int y, int c, int bc); +static void RangeCols(int a, int b); +static void ButtonPoll(Button *b, int n, int x, int y); +static void ButtonText(const Button *b); +static void ButtonRedraw(Button *b); +static int ColPress(int x,int y, int *c); -int quit=False; -int selcol; -int range=False; -int width=1; -int drawmode=PLOT; -int last_x=-1,last_y=-1; -XColor xc[256]; -int namelen=0; -char name[MAXSTRLEN]; -int SPR_W=MAX_SPR_W; -int SPR_H=MAX_SPR_H; -int GRID_W=SETGW(MAX_SPR_W,MAX_SPR_H); -unsigned char sprite[MAX_SPR_W][MAX_SPR_H]; - - -int main(argc,argv) -int argc; -char *argv[]; +#define MAXSTRLEN 64 +static int quit=False; +static int selcol; +static int range=False; +static int width=1; +static int drawmode=PLOT; +static int last_x=-1,last_y=-1; +static XColor xc[256]; +static int namelen=0; +static char name[MAXSTRLEN]; +static int SPR_W=MAX_SPR_W; +static int SPR_H=MAX_SPR_H; +static int GRID_W=SETGW(MAX_SPR_W,MAX_SPR_H); +static unsigned char sprite[MAX_SPR_W][MAX_SPR_H]; + + +int main(int argc, char *argv[]) { XEvent event; unsigned long evmask; @@ -201,11 +235,10 @@ char *argv[]; disp=GetDisplay(); - cm=XCreateColormap(disp,window,DefaultVisual(disp,0),AllocAll); - XSetWindowColormap(disp,window,cm); - font=XUseFont("fixed"); + cm = DefaultColormap(disp, DefaultScreen(disp)); + SetCmFade(W_MAX,W_MIN,xc,DoRed|DoBlue|DoGreen); SetCmFade(R_MAX,R_MIN,xc,DoRed); SetCmFade(B_MAX,B_MIN,xc,DoBlue); @@ -221,31 +254,26 @@ char *argv[]; xc[f].red=RND(0xff)*0xff; xc[f].green=RND(0xff)*0xff; xc[f].blue=RND(0xff)*0xff; + XAllocColor(disp, cm, &xc[f]); } - black=1; - white=0; - selcol=white; + selcol=1; - xc[black].pixel=black; - xc[black].flags=DoRed|DoBlue|DoGreen; - xc[black].red=xc[black].blue=xc[black].green=0; + xc[0].pixel=black; + xc[0].flags=DoRed|DoBlue|DoGreen; + xc[0].red=xc[0].blue=xc[0].green=0; - xc[white].pixel=white; - xc[white].flags=DoRed|DoBlue|DoGreen; - xc[white].red=xc[white].blue=xc[white].green=0xffff; + xc[1].pixel=white; + xc[1].flags=DoRed|DoBlue|DoGreen; + xc[1].red=xc[1].blue=xc[1].green=0xffff; - XStoreColors(disp,cm,xc,256); + /* XStoreColors(disp,cm,xc,256); */ MainLoop(argc,argv); } -SetCmFade(c1,c2,xc,fl) -int c1,c2; -XColor xc[]; -int fl; - +static void SetCmFade(int c1, int c2, XColor xc[], int fl) { int f,n,r; @@ -254,7 +282,6 @@ int fl; for(f=c1,r=0xff;f<=c2;f++,r-=n) { - xc[f].pixel=f; xc[f].flags=DoRed|DoBlue|DoGreen; if (fl&DoRed) @@ -272,22 +299,20 @@ int fl; else xc[f].blue=0; + XAllocColor(disp, cm, &xc[f]); } } /* Main Loop */ -MainLoop(argc,argv) -int argc; -char *argv[]; - +static void MainLoop(int argc, char *argv[]) { XEvent e; KeySym ks; char keybuf[10]; int f,x,y; - name[0]=NULL; + name[0]=0; for(x=0;x<SPR_W;x++) for(y=0;y<SPR_H;y++) @@ -315,7 +340,7 @@ char *argv[]; switch(e.type) { case KeyPress: - XLookupString(&e,keybuf,10,&ks,NULL); + XLookupString((XKeyEvent *)&e,keybuf,10,&ks,NULL); switch(ks) { @@ -323,7 +348,7 @@ char *argv[]; case XK_BackSpace: if (namelen) { - name[--namelen]=NULL; + name[--namelen]=0; RedrawStr(); Redraw(0,0); } @@ -333,7 +358,7 @@ char *argv[]; switch(keybuf[0]) { case '': - name[namelen=0]=NULL; + name[namelen=0]=0; RedrawStr(); Redraw(0,0); break; @@ -347,7 +372,7 @@ char *argv[]; if (namelen<MAXSTRLEN-1) { name[namelen++]=keybuf[0]; - name[namelen]=NULL; + name[namelen]=0; RedrawStr(); Redraw(0,0); } @@ -402,8 +427,7 @@ char *argv[]; /* Main Loop routines */ -NullStr() - +static void NullStr(void) { strcpy(name,""); namelen=0; @@ -411,8 +435,7 @@ NullStr() } -RedrawAll() - +static void RedrawAll(void) { XCls(black); RedrawButs(); @@ -423,8 +446,7 @@ RedrawAll() Redraw(0,0); } -RedrawButs() - +static void RedrawButs(void) { int f; @@ -435,8 +457,7 @@ RedrawButs() } -RedrawCmap() - +static void RedrawCmap(void) { int x,y,f; @@ -450,12 +471,12 @@ RedrawCmap() y+=CMAP_H; x=CMAP_X; } - XFillBox(x,y,CMAP_W,CMAP_H,f); + XFillBox(x,y,CMAP_W,CMAP_H,xc[f].pixel); } } -RedrawSel() +static void RedrawSel(void) { XFillBox @@ -468,8 +489,7 @@ RedrawSel() } -RedrawStr() - +static void RedrawStr(void) { char s[MAXSTRLEN+1]; int f; @@ -488,8 +508,7 @@ RedrawStr() } -RedrawSpr() - +static void RedrawSpr(void) { int x,y,f,r; @@ -505,9 +524,7 @@ RedrawSpr() } -SpritePress(x,y,b) -int x,y,b; - +static void SpritePress(int x, int y, int b) { int dx,dy; int c; @@ -674,8 +691,7 @@ int x,y,b; } -SprCircle(x,y,rx,ry,c) -int x,y,rx,ry,c; +static void SprCircle(int x, int y, int rx, int ry, int c) { int odx,ody,dy,dx,a; @@ -698,9 +714,7 @@ int x,y,rx,ry,c; } -SprLine(p1x,p1y,p2x,p2y,c) -int p1x,p1y,p2x,p2y,c; - +static void SprLine(int p1x,int p1y,int p2x,int p2y,int c) { int f; int dx,dy,ix,iy,incrE,incrNE,d,x,y,ymode; @@ -771,9 +785,7 @@ int p1x,p1y,p2x,p2y,c; } -SprSet(x,y,c) -int x,y,c; - +static void SprSet(int x,int y,int c) { if ((x<0)||(x>=SPR_W)||(y<0)||(y>=SPR_H)) return; @@ -783,22 +795,18 @@ int x,y,c; } -SprPlot(x,y,c) -int x,y,c; - +static void SprPlot(int x,int y,int c) { - XPlot(SPR_X+x,SPR_Y+y,c); + XPlot(SPR_X+x,SPR_Y+y,xc[c].pixel); x=GRID_X+(x*GRID_W)+1; y=GRID_Y+(y*GRID_W)+1; - XFillBox(x,y,GRID_W-1,GRID_W-1,c); + XFillBox(x,y,GRID_W-1,GRID_W-1,xc[c].pixel); } -SprFill(x,y,c,bc) -int x,y,c,bc; - +static void SprFill(int x, int y, int c, int bc) { if ((x<0)||(x>=SPR_W)||(y<0)||(y>=SPR_H)||(sprite[x][y]==c)|| (sprite[x][y]!=bc)) @@ -813,9 +821,7 @@ int x,y,c,bc; } -ColPress(x,y,c) -int x,y,*c; - +static int ColPress(int x,int y, int *c) { int lc; @@ -836,9 +842,7 @@ int x,y,*c; } -RangeCols(a,b) -int a,b; - +static void RangeCols(int a, int b) { int f,n,rc,gc,bc; @@ -860,16 +864,16 @@ int a,b; xc[a+f].red=xc[a].red+(f*rc); xc[a+f].green=xc[a].green+(f*gc); xc[a+f].blue=xc[a].blue+(f*bc); + XAllocColor(disp, cm, &xc[a+f]); } - XStoreColors(disp,cm,xc,256); + + /* XStoreColors(disp,cm,xc,256); */ } /* Button Routines */ -isin(x,y,bx,by,bw,bh) -int x,y,bx,by,bw,bh; - +static int isin(int x, int y, int bx, int by, int bw, int bh) { if ((x>=bx)&&(x<=bx+bw)&&(y>=by)&&(y<=by+bh)) return(True); @@ -877,11 +881,7 @@ int x,y,bx,by,bw,bh; return(False); } -ButtonPoll(b,n,x,y) -Button b[]; -int n; -int x,y; - +static void ButtonPoll(Button *b, int n, int x, int y) { int f; @@ -894,9 +894,7 @@ int x,y; } } -ButtonText(b) -Button *b; - +static void ButtonText(const Button *b) { XFillBox(b->x,b->y,b->w,b->h,black); XBox(b->x,b->y,b->w,b->h,white); @@ -907,9 +905,7 @@ Button *b; } -ButtonRedraw(b) -Button *b; - +static void ButtonRedraw(Button *b) { XBox(b->x,b->y,b->w,b->h,white); @@ -919,15 +915,13 @@ Button *b; /* Button Callbacks and redraws */ -int RdCmapBut(b) -Button *b; - +static void RdCmapBut(Button *b) { ButtonText(b); } -int CmapBut() +static void CmapBut(void) { unsigned short us; @@ -953,16 +947,13 @@ int CmapBut() } -int RdLoadCmapBut(b) -Button *b; - +static void RdLoadCmapBut(Button *b) { ButtonText(b); } -int LoadCmapBut() - +static void LoadCmapBut(void) { unsigned short us; int fd,f; @@ -981,24 +972,23 @@ int LoadCmapBut() xc[f].green=ntohs(us); read(fd,&us,sizeof(unsigned short)); xc[f].blue=ntohs(us); + XAllocColor(disp, cm, &xc[f]); } close(fd); - XStoreColors(disp,cm,xc,256); + /* XStoreColors(disp,cm,xc,256); */ + RedrawAll(); } -int RdSprSaveBut(b) -Button *b; - +static void RdSprSaveBut(Button *b) { ButtonText(b); } -int SprSaveBut() - +static void SprSaveBut(void) { int fd,x,y; unsigned short s; @@ -1025,16 +1015,13 @@ int SprSaveBut() } -int RdSprLoadBut(b) -Button *b; - +static void RdSprLoadBut(Button *b) { ButtonText(b); } -int SprLoadBut() - +static void SprLoadBut(void) { int fd,x,y; unsigned short s; @@ -1071,16 +1058,13 @@ int SprLoadBut() } -int RdSetCol(b) -Button *b; - +static void RdSetCol(Button *b) { ButtonText(b); } -int SetCol() - +static void SetCol(void) { static char *dum="0"; char *r,*g,*b,s[MAXSTRLEN]; @@ -1103,15 +1087,17 @@ int SetCol() xc[selcol].green=(int)strtol(g,NULL,0)*0xff; xc[selcol].blue=(int)strtol(b,NULL,0)*0xff; + XAllocColor(disp, cm, &xc[selcol]); + NullStr(); - XStoreColors(disp,cm,xc,256); -} + RedrawAll(); + /* XStoreColors(disp,cm,xc,256); */ +} -int RdDrawMode(b) -Button *b; +static void RdDrawMode(Button *b) { switch(drawmode) { @@ -1151,8 +1137,7 @@ Button *b; } -int DrawMode() - +static void DrawMode(void) { if(++drawmode==ENDMODE) drawmode=PLOT; @@ -1164,31 +1149,25 @@ int DrawMode() } -int RdSpreadBut(b) -Button *b; - +static void RdSpreadBut(Button *b) { ButtonText(b); } -int SpreadBut() - +static void SpreadBut(void) { range=True; } -int RdClearToBut(b) -Button *b; - +static void RdClearToBut(Button *b) { ButtonText(b); } -int ClearToBut() - +static void ClearToBut(void) { int x,y; @@ -1198,16 +1177,13 @@ int ClearToBut() } -int RdClearBut(b) -Button *b; - +static void RdClearBut(Button *b) { ButtonText(b); } -int ClearBut() - +static void ClearBut(void) { int x,y; @@ -1217,16 +1193,13 @@ int ClearBut() } -int RdSetSprBut(b) -Button *b; - +static void RdSetSprBut(Button *b) { ButtonText(b); } -int SetSprBut() - +static void SetSprBut(void) { int n; @@ -1249,16 +1222,13 @@ int SetSprBut() } -int RdQuitBut(b) -Button *b; - +static void RdQuitBut(Button *b) { ButtonText(b); } -int QuitBut() - +static void QuitBut(void) { quit=True; } |