📄 lockscreen_default.c
字号:
#ifndef lint#ifdef sccsstatic char sccsid[] = "@(#)lockscreen_default.c 1.1 92/07/30";#endif#endif/* * lockscreen_default.c: Life for lockscreen * Sun Microsystems, Inc. */#include <stdio.h>#include <ctype.h>#include <sunwindow/cms.h>#include <suntool/tool_hs.h>#include <suntool/gfxsw.h>#ifdef STANDALONE#define EXIT(n) exit(n)#else#define EXIT(n) return(n)#endifstatic struct gfxsubwindow *gfx;static struct rect rectcreate;#define STARTGLIDER 40#define CLEARSCREEN 290static int startglider = STARTGLIDER;static int clearscreen = CLEARSCREEN;static int life_count;static int pause_var; /* Set at the end of a cycle */static struct timeval sectimer = {1, 0};static struct timeval tooltimer = {1, 0};static struct timeval pausetimer = {3, 0};static colormonitor; /* true if running on color monitor */struct pixrect *logopixrect; /* FIXME */static gfx_selected();/* arrays which get malloc'd to reduce BSS load in a toolmerge */#define MAXPIXELS 2048 /* maximum pixel width of any screen */#define SPACING 33#define HASHSIZE 32static char (*board)[MAXPIXELS/SPACING][MAXPIXELS/SPACING];static int *addx, *addy, *xarr, *yarr;struct xy { int x,y; short cnt; struct xy *nxt; /* next in this bucket */ struct xy *chain; /* for chaining nonzero entries together */};static struct xy *(*hash)[HASHSIZE][HASHSIZE];static void alloc_memory();long random();char *malloc(), *sprintf();#ifdef STANDALONEmain(argc, argv)#elseint lockscreen_default_main(argc, argv)#endif int argc; char **argv;{ char *progname; Cursor cursor; fd_set ibits, obits, ebits; progname = argv[0]; argv++; argc--; alloc_memory(); while (argc > 0 && **argv == '-') { if (**argv == '-') switch (argv[0][1]) { case 's': if (argc > 1 && isdigit(argv[1][0])) { int k = atoi(argv[1]); sectimer.tv_sec = k/10; sectimer.tv_usec = (k % 10) * 100000; } else { (void)fprintf(stderr, "%s -s needs integer argument\n", progname); goto default_case; } argv++; argc--; break; case 'c': if (argc > 1 && isdigit(argv[1][0])) { int k = atoi(argv[1]); clearscreen = k; } else { (void)fprintf(stderr, "%s-c needs integer argument\n", progname); goto default_case; } argv++; argc--; break; case 'g': if (argc > 1 && isdigit(argv[1][0])) { int k = atoi(argv[1]); startglider = k; } else { (void)fprintf(stderr, "%s -g needs integer argument\n", progname); goto default_case; } argv++; argc--; break; default_case: default: (void)fprintf(stderr, "%s -s tenths-of-seconds -c number-of-cycles -g glider-after-cycle-n\n", progname); EXIT(1); } argv++; argc--; } gfx = gfxsw_init(NULL, argv); cursor = cursor_create(CURSOR_SHOW_CURSOR, 0, 0); (void)win_setcursor(gfx->gfx_windowfd, cursor); initcolor(); initlife(); FD_ZERO(&ibits); FD_ZERO(&obits); FD_ZERO(&ebits); /* Wait for input */ (void)gfxsw_select(gfx, gfx_selected, ibits, obits, ebits, &tooltimer); EXIT(0);}staticgfx_selected(gfx_local, ibits, obits, ebits, timer) struct gfxsubwindow *gfx_local; int *ibits, *obits, *ebits; struct timeval **timer;{ if (*timer && ((*timer)->tv_sec == 0) && ((*timer)->tv_usec == 0)) { drawlife(); } else if (*ibits) { (void)gfxsw_selectdone(gfx_local); } if (gfx_local->gfx_flags & GFX_DAMAGED) { (void)gfxsw_handlesigwinch(gfx_local); } if (gfx_local->gfx_flags & GFX_RESTART) { gfx_local->gfx_flags &= ~GFX_RESTART; initlife(); } *ibits = *obits = *ebits = 0; tooltimer = pause_var ? pausetimer : sectimer; *timer = &tooltimer;}#define CMSIZE 32staticinitcolor(){ char cmsname[CMS_NAMESIZE]; u_char red[CMSIZE]; u_char green[CMSIZE]; u_char blue[CMSIZE]; int i; (void)sprintf(cmsname, "lockscreen%d", getpid()); (void)pw_setcmsname(gfx->gfx_pixwin, cmsname); for(i = 1; i <= CMSIZE-2; i++) hsv_to_rgb(((i-1)*(360-225))/(CMSIZE-3) + 225, 255, 255, &red[i], &green[i], &blue[i]); red[0] = green[0] = blue[0] = 0; red[CMSIZE-1] = green[CMSIZE-1] = blue[CMSIZE-1] = 255; (void)pw_putcolormap(gfx->gfx_pixwin, 0, CMSIZE, red, green, blue); colormonitor = gfx->gfx_pixwin->pw_pixrect->pr_depth > 1;}/* * Convert hue/saturation/value to red/green/blue. */hsv_to_rgb(h, s, v, rp, gp, bp) int h, s, v; u_char *rp, *gp, *bp;{ int i, f; u_char p, q, t; if (s == 0) *rp = *gp = *bp = v; else { if (h == 360) h = 0; h = h * 256 / 60; i = h / 256 * 256; f = h - i; p = v * (256 - s) / 256; q = v * (256 - (s*f)/256) / 256; t = v * (256 - (s * (256 - f))/256) / 256; switch (i/256) { case 0: *rp = v; *gp = t; *bp = p; break; case 1: *rp = q; *gp = v; *bp = p; break; case 2: *rp = p; *gp = v; *bp = t; break; case 3: *rp = p; *gp = q; *bp = v; break; case 4: *rp = t; *gp = p; *bp = v; break; case 5: *rp = v; *gp = p; *bp = q; break; } }}/* * stuff for life */#define PAUSE 10#define INITCOLOR 1#define NUMPATTERNS 9#define EIGHT 0#define PULSAR 1#define BARBER 2#define TUMBLER 3#define HERTZ 4#define PERIOD4 5#define PERIOD5 6#define PERIOD6 7#define PINWHEEL 8#define STARTSIZE 13static struct { int x,y;}startpos[STARTSIZE] = { {0,0}, {0,1}, {0,2}, {0,3}, {0,4}, {0,5}, {0,6}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0}};static int rightedge;static int bottomedge;static short logo_data[256]={#include <images/sun30.icon>};static mpr_static(logo_mpr, 64, 64, 1, logo_data);staticinitlife(){ /* * Initialize life variables */ life_count = 0; logopixrect = &logo_mpr; srandom(time((time_t *)0)); (void)win_getrect(gfx->gfx_windowfd, &rectcreate); rightedge = rectcreate.r_width/SPACING; bottomedge = rectcreate.r_height/SPACING; clearpicture();}staticdrawlife(){ int x, y, z; static int dullcnt = 0; static int oldsums[3]; int newptr, oldptr; static int ptr; pause_var = 0; if (life_count == 0) { dullcnt = 0; ptr = 0; oldsums[0] = 1; oldsums[1] = 2; oldsums[2] = 3; drawpattern((int)(random()%NUMPATTERNS)); } else if (life_count == clearscreen || (life_count > startglider && dullcnt >= PAUSE)) { clearpicture(); pause_var = 1; life_count = 0; return; } else if (life_count == startglider) { dullcnt = 0; z = random()%STARTSIZE; x = startpos[z].x; y = startpos[z].y; simplepaint(x+3,y+2); simplepaint(x+4,y+2); simplepaint(x+5,y+2); simplepaint(x+4,y); simplepaint(x+5,y+1); } newgen(); if (ptr == 2) newptr = 0; else newptr = ptr+1; if (ptr == 0) oldptr = 2; else oldptr = ptr-1; oldsums[newptr] = checksum(); if (oldsums[ptr] == oldsums[newptr]) { dullcnt++; } else if (oldsums[oldptr] == oldsums[newptr]) { dullcnt++; } else dullcnt = 0; ptr = newptr; life_count++;}staticlock(){ (void)pw_lock(gfx->gfx_pixwin, &rectcreate);}staticunlock(){ (void)pw_unlock(gfx->gfx_pixwin);}staticboard_init(){ register int i,j; (void)pw_writebackground(gfx->gfx_pixwin, 0, 0, rectcreate.r_width, rectcreate.r_height, PIX_CLR); for (i = 0; i < rightedge; i++) { for (j = 0; j < bottomedge; j++) { if ((*board)[i][j]) paint_stone(i, j, (*board)[i][j]); } }}staticclearpicture(){ int i,j; for(i = 0; i < rightedge; i++) for(j = 0; j < bottomedge; j++) (*board)[i][j] = 0; zerolist(); refreshboard(); board_init();}staticpaint_stone(i, j, color){ int diameter; int op = (colormonitor)? PIX_SRC|PIX_COLOR(color): PIX_SRC ^ PIX_DST; diameter = SPACING - 3; (void)pw_write(gfx->gfx_pixwin, i*SPACING+2, j*SPACING+2, diameter, diameter, op, logopixrect, 0, 0); (*board)[i][j] = color;} staticerase_stone(i, j){ int diameter; diameter = SPACING - 3; (void)pw_writebackground(gfx->gfx_pixwin, i*SPACING+2, j*SPACING+2, diameter, diameter, PIX_CLR); (*board)[i][j] = 0;} staticsimplepaint(x,y){ paint_stone(x,y,INITCOLOR); addpoint(x,y);}#define MAXINT 0x7fffffff#define MININT 0x80000000#define MIN(a,b) ((a) < (b) ? (a) : (b))#define MAX(a,b) ((a) > (b) ? (a) : (b))#define STACK 2000 /* max number of points added at any one step */static int stack;struct point { unsigned char p_color; int p_y; /* x from row pointer */ short p_delete; struct point *p_nxt;};staticstruct row { int r_x; struct point *r_point; struct row *r_nxt;} *row;staticaddpoint(x,y){ struct row *rw, *oldrw, *newrw; if (row == NULL) { row = (struct row *)(LINT_CAST(malloc(sizeof(struct row)))); row->r_x = x; row->r_point = NULL; addtorow(row, y); row->r_nxt = NULL; return; } if (x < row->r_x) { rw = (struct row *)(LINT_CAST(malloc(sizeof(struct row)))); rw->r_x = x; rw->r_point = NULL; addtorow(rw, y); rw->r_nxt = row; row = rw; return; } for(rw = row, oldrw = row; rw != NULL; oldrw = rw, rw = rw->r_nxt) { if (rw->r_x == x) { addtorow(rw, y); return; } if (x < rw->r_x) break; } newrw = (struct row *)(LINT_CAST(malloc(sizeof(struct row)))); newrw->r_x = x; newrw->r_point = NULL; addtorow(newrw, y); newrw->r_nxt = rw; oldrw->r_nxt = newrw;}staticaddtorow(rw, y) struct row *rw;{ struct point *pt, *oldpt, *newpt; if (rw->r_point == NULL) { pt = (struct point *)(LINT_CAST(malloc(sizeof(struct point)))); pt->p_color = INITCOLOR; pt->p_y = y; pt->p_delete = 0; pt->p_nxt = NULL; rw->r_point = pt; return; } if (y < rw->r_point->p_y) { pt = (struct point *)(LINT_CAST(malloc(sizeof(struct point)))); pt->p_color = INITCOLOR; pt->p_y = y; pt->p_delete = 0; pt->p_nxt = rw->r_point; rw->r_point = pt; return; } for(pt = rw->r_point, oldpt = pt; pt != NULL; oldpt = pt, pt = pt->p_nxt) { if (pt->p_y == y) return; if (y < pt->p_y) break; } newpt = (struct point *)(LINT_CAST(malloc(sizeof(struct point)))); newpt->p_color = INITCOLOR; newpt->p_y = y; newpt->p_delete = 0; newpt->p_nxt = pt; oldpt->p_nxt = newpt;}staticdeletepoint(x,y){ struct row *rw, *oldrw; struct point *pt, *oldpt; for (rw = row, oldrw = row; rw != NULL; oldrw = rw, rw = rw->r_nxt) { if (rw->r_x == x) break; } if (rw == NULL) { (void)fprintf(stderr, "tried to delete nonexistent point\n"); exit(1); } for (pt = rw->r_point, oldpt = pt; pt != NULL; oldpt = pt, pt = pt->p_nxt) { if (pt->p_y == y) break; } if (pt == NULL) { (void)fprintf(stderr, "tried to delete nonexistent point\n"); exit(1); } if (oldpt == pt) { rw->r_point = pt->p_nxt; if (rw->r_point == NULL) { deleterow(oldrw, rw); } } else oldpt->p_nxt = pt->p_nxt; free((char *)(LINT_CAST(pt)));}staticdeleterow(oldrw, rw) struct row *oldrw, *rw;{ if (oldrw == rw) { if (rw->r_nxt == NULL) row = NULL; else row = rw->r_nxt; } else oldrw->r_nxt = rw->r_nxt; free((char *)(LINT_CAST(rw)));}staticzerolist(){ struct row *rw; struct point *pt; for (rw = row; rw != NULL; rw = rw->r_nxt) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -