📄 nsaver.c
字号:
s->pid = capture_screen(state); GrSetGCForeground(state->main_gc, GR_COLOR_BLACK); GrLine(s->pid, state->main_gc, 0, 0, state->si.cols, 0); state->animate_interval = SAVER9_DELAY;}void saver9_exposure(nstate *state){ s9state *s = state->priv; GrCopyArea(state->main_window, state->main_gc, 0, 0, state->si.cols, state->si.rows, s->pid, 0, 0, 0);}void saver9_animate(nstate *state){ GR_COORD x, y; GR_SIZE w, h; s9state *s = state->priv; x = RANDRANGE(0, state->si.cols - 2); y = RANDRANGE(0, state->si.rows - 2); w = RANDRANGE(1, state->si.cols - x); h = RANDRANGE(1, state->si.rows - y); GrCopyArea(s->pid, state->main_gc, x, y + 1, w, h, s->pid, x, y, 0); GrCopyArea(state->main_window, state->main_gc, x, y + 1, w, h, s->pid, x, y, 0);}/* saver10 is quite loosely based on spotlight from xscreensaver. */void saver10_init(nstate *state){ s10state *s = my_malloc(sizeof(s10state)); state->priv = s; s->screen = capture_screen(state); s->spot = GrNewPixmap(SAVER10_SPOTDIA + SAVER10_OVERSIZE * 2, SAVER10_SPOTDIA + SAVER10_OVERSIZE * 2, NULL); s->x = RANDRANGE((SAVER10_SPOTDIA / 2) + SAVER10_OVERSIZE, state->si.cols - 1 - (SAVER10_SPOTDIA / 2) - SAVER10_OVERSIZE); s->y = RANDRANGE((SAVER10_SPOTDIA / 2) + SAVER10_OVERSIZE, state->si.rows - 1 - (SAVER10_SPOTDIA / 2) - SAVER10_OVERSIZE); s->direction = FRANDRANGE(0, 2 * PI) - PI; state->animate_interval = SAVER10_DELAY;}void saver10_exposure(nstate *state){ GrClearWindow(state->main_window, 0);}void saver10_animate(nstate *state){ int i, x, y, w, xx, yy, retry; s10state *s = state->priv; double d, newx, newy; do { newx = s->x + cos(s->direction) * SAVER10_VELOCITY; newy = s->y + sin(s->direction) * SAVER10_VELOCITY; if((newx < (SAVER10_SPOTDIA / 2) + SAVER10_OVERSIZE) || (newx > state->si.cols - (SAVER10_SPOTDIA / 2) - SAVER10_OVERSIZE) || (newy < (SAVER10_SPOTDIA / 2) + 3) || (newy > state->si.rows - (SAVER10_SPOTDIA / 2) - SAVER10_OVERSIZE)) { s->direction = FRANDRANGE(0, 2 * PI); retry = 1; } else retry = 0; } while(retry); s->x = newx; s->y = newy; y = s->y - (SAVER10_SPOTDIA / 2); yy = SAVER10_OVERSIZE; for(i = 0; i < SAVER10_SPOTDIA; i++) { d = sin(acos(((double)i/((double)SAVER10_SPOTDIA / 2.0)) - 1)) * (double)SAVER10_SPOTDIA / 2.0; w = (int) 2 * d; x = (int) s->x - d; xx = (int) (SAVER10_SPOTDIA / 2) - d + SAVER10_OVERSIZE; if(!w) continue; GrCopyArea(s->spot, state->main_gc, xx, yy, w, 1, s->screen, x, y, 0); y++; yy++; } x = s->x - (SAVER10_SPOTDIA / 2) - SAVER10_OVERSIZE; y = s->y - (SAVER10_SPOTDIA / 2) - SAVER10_OVERSIZE; if(x < 0) x = 0; if(y < 0) y = 0; GrCopyArea(state->main_window, state->main_gc, x, y, SAVER10_SPOTDIA + 2 * SAVER10_OVERSIZE, SAVER10_SPOTDIA + 2 * SAVER10_OVERSIZE, s->spot, 0, 0, 0);}void saver11_init(nstate *state){ s11state *s = my_malloc(sizeof(s11state)); state->priv = s; state->animate_interval = SAVER11_DELAY;}void saver11_exposure(nstate *state){ s11state *s = state->priv; GrClearWindow(state->main_window, 0); make_random_square(state, SAVER11_MIN_BRIGHTNESS, SAVER11_MIN_SIZE, SAVER11_MAX_SIZE, 0, &s->x, &s->y, &s->size);}void saver11_animate(nstate *state){ s11state *s = state->priv; make_random_square(state, SAVER11_MIN_BRIGHTNESS, SAVER11_MIN_SIZE, SAVER11_MAX_SIZE, 0, &s->x, &s->y, &s->size); GrFillRect(state->main_window, state->main_gc, s->x, s->y, s->size, s->size);}void saver12_new_shape(nstate *state){ s12state *s = state->priv; make_random_square(state, SAVER12_MIN_BRIGHTNESS, SAVER12_MIN_SIZE, SAVER12_MAX_SIZE, 1, &s->x, &s->y, &s->size); s->t = 0; s->kt = RANDRANGE(0, s->size);}void saver12_init(nstate *state){ s12state *s = my_malloc(sizeof(s12state)); state->priv = s; saver12_new_shape(state); state->animate_interval = SAVER12_DELAY;}void saver12_exposure(nstate *state){ GrClearWindow(state->main_window, 0); saver12_new_shape(state);}void saver12_animate(nstate *state){ int x1, y1, x2, y2; s12state *s = state->priv; if(s->t >= s->size) saver12_new_shape(state); for(x1 = 0; x1 < s->size; x1++) { y1 = (x1 ^ ((s->t + s->kt) % s->size)) % s->size; x2 = (x1 % s->size) + s->x; y2 = y1 + s->y; GrPoint(state->main_window, state->main_gc, x2, y2); } s->t++;}/* Zoom from xscreensavers was the inspiration for saver13 */void saver13_init(nstate *state){ s13state *s = my_malloc(sizeof(s13state)); state->priv = s; s->screen = my_malloc(sizeof(GR_PIXELVAL) * state->si.rows * state->si.cols); s->rows = my_malloc(sizeof(GR_PIXELVAL) * state->si.cols * SAVER13_ZOOM_FACTOR * SAVER13_ROWS_BUFFER); s->rownum = 0; s->x = RANDRANGE(0, state->si.cols - (state->si.cols / SAVER13_ZOOM_FACTOR)); s->x = RANDRANGE(0, state->si.rows - (state->si.rows / SAVER13_ZOOM_FACTOR)); s->direction = FRANDRANGE(0, 2 * PI) - PI; state->animate_interval = 1;#if CAPTURESCREEN_DELAY msleep(CAPTURESCREEN_DELAY);#endif GrReadArea(GR_ROOT_WINDOW_ID, 0, 0, state->si.cols, state->si.rows, s->screen);}void saver13_exposure(nstate *state){ s13state *s = state->priv; GrClearWindow(state->main_window, 0); s->rownum = 0;}void saver13_move_portal(nstate *state){ int retry; double newx, newy; s13state *s = state->priv; do { newx = s->x + cos(s->direction) * SAVER13_VELOCITY; newy = s->y + sin(s->direction) * SAVER13_VELOCITY; if(newx < 0 || newx > state->si.cols - (state->si.cols / SAVER13_ZOOM_FACTOR) || newy < 0 || newy > state->si.rows - (state->si.rows / SAVER13_ZOOM_FACTOR)) { s->direction = FRANDRANGE(0, 2 * PI); retry = 1; } else retry = 0; } while(retry); s->x = newx; s->y = newy;}void saver13_animate(nstate *state){ int rows, r, y, i, n; s13state *s = state->priv; GR_PIXELVAL *src, *dst; rows = (state->si.rows / SAVER13_ZOOM_FACTOR) - s->rownum; if(rows > SAVER13_ROWS_BUFFER) rows = SAVER13_ROWS_BUFFER; dst = s->rows; y = s->y + s->rownum; for(r = 0; r < rows; r++) { src = &s->screen[(y++ * state->si.cols) + (int)s->x]; for(i = 0; i < SAVER13_ZOOM_FACTOR; i++) for(n = 0; n < state->si.cols; n++) *dst++ = src[n / SAVER13_ZOOM_FACTOR]; } GrArea(state->main_window, state->main_gc, 0, s->rownum * SAVER13_ZOOM_FACTOR, state->si.cols, SAVER13_ZOOM_FACTOR * rows, s->rows, MWPF_PIXELVAL); s->rownum += rows; if(s->rownum >= state->si.rows / SAVER13_ZOOM_FACTOR) { s->rownum = 0; saver13_move_portal(state); state->animate_interval = SAVER13_DELAY; } else state->animate_interval = 1;}int init(nstate *state){ GR_WM_PROPERTIES props; GR_BITMAP cursor = 0; if(!GrOpen()) { fprintf(stderr, "Couldn't connect to Nano-X server\n"); return 3; } GrGetScreenInfo(&state->si); state->main_window = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, state->si.cols, state->si.rows, 0, GR_COLOR_BLACK, 0); GrSelectEvents(state->main_window, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_UP | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_MOUSE_MOTION | GR_EVENT_MASK_KEY_UP | GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_FOCUS_OUT | GR_EVENT_MASK_CLOSE_REQ); props.flags = GR_WM_FLAGS_PROPS; props.props = GR_WM_PROPS_NOMOVE | GR_WM_PROPS_NODECORATE | GR_WM_PROPS_NOAUTOMOVE | GR_WM_PROPS_NOAUTORESIZE; GrSetWMProperties(state->main_window, &props); state->main_gc = GrNewGC(); GrSetGCForeground(state->main_gc, GR_COLOR_WHITE); GrSetGCBackground(state->main_gc, GR_COLOR_BLACK); state->animate_interval = 0; srand(time(0)); init_functions[state->saver](state); calculate_timeout(state); GrSelectEvents(GR_ROOT_WINDOW_ID, GR_EVENT_MASK_SCREENSAVER); GrSetCursor(state->main_window, 1, 1, 1, 1, 0, 0, &cursor, &cursor); GrMapWindow(state->main_window); GrSetFocus(state->main_window); return 0;}void calculate_timeout(nstate *state){ struct timeval t; long u; gettimeofday(&t, NULL); u = t.tv_usec + (state->animate_interval * 1000); state->timeout.tv_sec = t.tv_sec + (u / 1000000); state->timeout.tv_usec = u % 1000000;}unsigned long timeout_delay(nstate *state){ struct timeval t; signed long s, m, ret; gettimeofday(&t, NULL); if(!state->animate_interval) return 0; if((t.tv_sec > state->timeout.tv_sec) || ((t.tv_sec == state->timeout.tv_sec) && t.tv_usec >= state->timeout.tv_usec)) return 1; s = state->timeout.tv_sec - t.tv_sec; m = ((state->timeout.tv_usec - t.tv_usec) / 1000); ret = (unsigned long)((1000 * s) + m); if(ret <= 0) return 1; else return ret;}void do_animate(nstate *state){ struct timeval t; if(!state->animate_interval) return; gettimeofday(&t, NULL); if((t.tv_sec > state->timeout.tv_sec) || ((t.tv_sec == state->timeout.tv_sec) && (t.tv_usec >= state->timeout.tv_usec))) { animate_functions[state->saver](state); calculate_timeout(state); }}int do_screensaver_event(nstate *state){ GR_EVENT_SCREENSAVER *event = &state->event.screensaver; if(event->activate != GR_FALSE) { fprintf(stderr, "Got a non-deactivate screensaver event\n"); return 0; } return 1;}int handle_event(nstate *state){ switch(state->event.type) { case GR_EVENT_TYPE_EXPOSURE: exposure_functions[state->saver](state); case GR_EVENT_TYPE_TIMEOUT: case GR_EVENT_TYPE_NONE: break; case GR_EVENT_TYPE_SCREENSAVER: if(do_screensaver_event(state)) return 0; break; case GR_EVENT_TYPE_CLOSE_REQ: case GR_EVENT_TYPE_BUTTON_UP: case GR_EVENT_TYPE_BUTTON_DOWN: case GR_EVENT_TYPE_MOUSE_MOTION: case GR_EVENT_TYPE_KEY_UP: case GR_EVENT_TYPE_KEY_DOWN: case GR_EVENT_TYPE_FOCUS_OUT: return 0; default: fprintf(stderr, "Got unknown event type %d\n", state->event.type); break; } do_animate(state); return(1);}int main(int argc, char *argv[]){ int ret; nstate *state = my_malloc(sizeof(nstate)); if(argc == 2) { state->saver = atoi(argv[1]) - 1; if((state->saver) < 0 || (state->saver >= NUM_SAVERS)) { fprintf(stderr, "Invalid saver number \"%s\"\n", argv[1]); return 2; } } else state->saver = 0; if((ret = init(state))) return ret; do { GrGetNextEventTimeout(&state->event, timeout_delay(state)); } while(handle_event(state)); GrClose(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -