546N/A/*
546N/A * Copyright (c) 1988-91 by Patrick J. Naughton.
546N/A *
546N/A * Permission to use, copy, modify, and distribute this software and its
546N/A * documentation for any purpose and without fee is hereby granted,
546N/A * provided that the above copyright notice appear in all copies and that
546N/A * both that copyright notice and this permission notice appear in
546N/A * supporting documentation.
546N/A *
546N/A * This file is provided AS IS with no warranties of any kind. The author
546N/A * shall have no liability with respect to the infringement of copyrights,
546N/A * trade secrets or any patents by this file or any part thereof. In no
546N/A * event will the author be liable for any lost revenue or profits or
546N/A * other special, indirect and consequential damages.
546N/A */
546N/A
546N/A/*
1513N/A * Copyright (c) 1990, 2015, Oracle and/or its affiliates. All rights reserved.
546N/A *
546N/A * Permission is hereby granted, free of charge, to any person obtaining a
919N/A * copy of this software and associated documentation files (the "Software"),
919N/A * to deal in the Software without restriction, including without limitation
919N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
919N/A * and/or sell copies of the Software, and to permit persons to whom the
919N/A * Software is furnished to do so, subject to the following conditions:
546N/A *
919N/A * The above copyright notice and this permission notice (including the next
919N/A * paragraph) shall be included in all copies or substantial portions of the
919N/A * Software.
546N/A *
919N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
919N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
919N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
919N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
919N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
919N/A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
919N/A * DEALINGS IN THE SOFTWARE.
546N/A */
546N/A/*-
546N/A * rotor.c - A swirly rotor for xlock, the X Window System lockscreen.
546N/A *
546N/A * Copyright (c) 1991 by Patrick J. Naughton.
546N/A *
546N/A * See xlock.c for copying information.
546N/A *
546N/A * Revision History:
546N/A * 11-Nov-90: put into xlock (by Steve Zellers, zellers@sun.com)
546N/A * 16-Oct-90: Received from Tom Lawrence (tcl@cs.brown.edu: 'flight' simulator)
546N/A */
546N/A
546N/A/*
546N/A * A 'batchcount' of 3 or 4 works best!
546N/A */
546N/A
546N/A#include <stdio.h>
546N/A#include <math.h>
546N/A#include "xlock.h"
546N/A
546N/A#define SAVE 100 /* this is a good constant to tweak */
546N/A#define REPS 50
546N/A
546N/A#define MAXANGLE 10000.0 /* irrectangular */
546N/A#define DEFAULTCOUNT 3
546N/A
546N/Atypedef unsigned char Boolean;
546N/A
546N/A#define IDENT(X) X
546N/A#ifdef __STDC__
546N/A#define CAT(X,Y) X##Y
546N/A#else
546N/A#define CAT(X,Y) IDENT(X)Y
546N/A#endif
546N/A
546N/Astruct elem {
546N/A float angle;
546N/A float radius;
546N/A float start_radius;
546N/A float end_radius;
546N/A float radius_drift_max;
546N/A float radius_drift_now;
546N/A
546N/A float ratio;
546N/A float start_ratio;
546N/A float end_ratio;
546N/A float ratio_drift_max;
546N/A float ratio_drift_now;
546N/A};
546N/A
546N/Atypedef struct flightstruct {
546N/A struct elem *elements;
546N/A int pix;
546N/A int lastx,
546N/A lasty;
546N/A int num,
546N/A rotor,
546N/A prev;
546N/A int savex[SAVE],
546N/A savey[SAVE];
546N/A float angle;
546N/A int centerx,
546N/A centery;
546N/A Boolean firsttime;
546N/A Boolean smallscreen; /* for iconified view */
546N/A Boolean forward;
546N/A Boolean unused;
546N/A} flightstruct;
546N/A
546N/A
546N/Astatic flightstruct flights[MAXSCREENS];
546N/A
546N/Avoid
1513N/Ainitrotor(Window win)
546N/A{
546N/A flightstruct *fs = &flights[screen];
546N/A XWindowAttributes xgwa;
546N/A int x;
546N/A struct elem *pelem;
546N/A Boolean wassmall;
546N/A
546N/A XGetWindowAttributes(dsp, win, &xgwa);
546N/A fs->centerx = xgwa.width / 2;
546N/A fs->centery = xgwa.height / 2;
546N/A
546N/A /*
546N/A * sometimes, you go into small view, only to see a really whizzy pattern
546N/A * that you would like to look more closely at. Normally, clicking in the
546N/A * icon reinitializes everything - but I don't, cuz I'm that kind of guy.
546N/A * HENCE, the wassmall stuff you see here.
546N/A */
546N/A
546N/A wassmall = fs->smallscreen;
1513N/A fs->smallscreen = (Boolean) (xgwa.width < 100);
546N/A
546N/A if (wassmall && !fs->smallscreen)
546N/A fs->firsttime = True;
546N/A else {
1513N/A if ((batchcount < 1) || (batchcount > 12))
546N/A batchcount = DEFAULTCOUNT;
546N/A fs->num = batchcount;
546N/A
546N/A if (fs->elements == NULL) {
1513N/A fs->elements = calloc(fs->num, sizeof(struct elem));
1513N/A if (fs->elements == NULL)
1513N/A error("allocation failed, unable to motor our rotor\n");
546N/A }
546N/A memset(fs->savex, 0, sizeof(fs->savex));
546N/A
546N/A pelem = fs->elements;
546N/A
546N/A for (x = fs->num; --x >= 0; pelem++) {
546N/A pelem->radius_drift_max = 1.0;
546N/A pelem->radius_drift_now = 1.0;
546N/A
546N/A pelem->end_radius = 100.0;
546N/A
546N/A pelem->ratio_drift_max = 1.0;
546N/A pelem->ratio_drift_now = 1.0;
546N/A pelem->end_ratio = 10.0;
546N/A }
546N/A
546N/A fs->rotor = 0;
546N/A fs->prev = 1;
546N/A fs->lastx = fs->centerx;
546N/A fs->lasty = fs->centery;
546N/A fs->angle = (random() % (long) MAXANGLE) / 3;
546N/A fs->forward = fs->firsttime = True;
546N/A }
546N/A XSetForeground(dsp, Scr[screen].gc, ssblack[screen].pixel);
546N/A XFillRectangle(dsp, win, Scr[screen].gc, 0, 0, xgwa.width, xgwa.height);
546N/A}
546N/A
546N/Avoid
1513N/Adrawrotor(Window win)
546N/A{
546N/A register flightstruct *fs = &flights[screen];
546N/A register struct elem *pelem;
546N/A int thisx,
546N/A thisy;
546N/A int i,
546N/A rp;
546N/A int x1,
546N/A y1,
546N/A x2,
546N/A y2;
546N/A
546N/A
546N/A#define SCALE(W,N) CAT(W,N)/=12; CAT(W,N)+=(CAT(fs->center,W)-2)
546N/A#define SCALEIFSMALL() if (fs->smallscreen) { \
546N/A SCALE(x,1); SCALE(x,2); \
546N/A SCALE(y,1); SCALE(y,2); \
546N/A }
546N/A
546N/A for (rp = 0; rp < REPS; rp++) {
546N/A thisx = fs->centerx;
546N/A thisy = fs->centery;
546N/A
546N/A for (i = fs->num, pelem = fs->elements; --i >= 0; pelem++) {
546N/A if (pelem->radius_drift_max <= pelem->radius_drift_now) {
546N/A pelem->start_radius = pelem->end_radius;
546N/A pelem->end_radius =
546N/A (float) (random() % 40000) / 100.0 - 200.0;
546N/A pelem->radius_drift_max =
546N/A (float) (random() % 100000) + 10000.0;
546N/A pelem->radius_drift_now = 0.0;
546N/A }
546N/A if (pelem->ratio_drift_max <= pelem->ratio_drift_now) {
546N/A pelem->start_ratio = pelem->end_ratio;
546N/A pelem->end_ratio =
546N/A (float) (random() % 2000) / 100.0 - 10.0;
546N/A pelem->ratio_drift_max =
546N/A (float) (random() % 100000) + 10000.0;
546N/A pelem->ratio_drift_now = 0.0;
546N/A }
546N/A pelem->ratio = pelem->start_ratio +
546N/A (pelem->end_ratio - pelem->start_ratio) /
546N/A pelem->ratio_drift_max * pelem->ratio_drift_now;
546N/A pelem->angle = fs->angle * pelem->ratio;
546N/A pelem->radius = pelem->start_radius +
546N/A (pelem->end_radius - pelem->start_radius) /
546N/A pelem->radius_drift_max * pelem->radius_drift_now;
546N/A
546N/A thisx += (int) (cos(pelem->angle) * pelem->radius);
546N/A thisy += (int) (sin(pelem->angle) * pelem->radius);
546N/A
546N/A pelem->ratio_drift_now += 1.0;
546N/A pelem->radius_drift_now += 1.0;
546N/A }
546N/A if (fs->firsttime)
546N/A fs->firsttime = False;
546N/A else {
546N/A XSetForeground(dsp, Scr[screen].gc, ssblack[screen].pixel);
546N/A
546N/A x1 = (int) fs->savex[fs->rotor];
546N/A y1 = (int) fs->savey[fs->rotor];
546N/A x2 = (int) fs->savex[fs->prev];
546N/A y2 = (int) fs->savey[fs->prev];
546N/A
546N/A SCALEIFSMALL();
546N/A
546N/A XDrawLine(dsp, win, Scr[screen].gc, x1, y1, x2, y2);
546N/A
546N/A if (!mono && Scr[screen].npixels > 2) {
546N/A XSetForeground(dsp, Scr[screen].gc,
546N/A Scr[screen].pixels[fs->pix]);
546N/A if (++fs->pix >= Scr[screen].npixels)
546N/A fs->pix = 0;
546N/A } else
546N/A XSetForeground(dsp, Scr[screen].gc, sswhite[screen].pixel);
546N/A
546N/A x1 = fs->lastx;
546N/A y1 = fs->lasty;
546N/A x2 = thisx;
546N/A y2 = thisy;
546N/A
546N/A SCALEIFSMALL();
546N/A
546N/A XDrawLine(dsp, win, Scr[screen].gc, x1, y1, x2, y2);
546N/A }
546N/A fs->savex[fs->rotor] = fs->lastx = thisx;
546N/A fs->savey[fs->rotor] = fs->lasty = thisy;
546N/A
546N/A ++fs->rotor;
546N/A fs->rotor %= SAVE;
546N/A ++fs->prev;
546N/A fs->prev %= SAVE;
546N/A if (fs->forward) {
546N/A fs->angle += 0.01;
546N/A if (fs->angle >= MAXANGLE) {
546N/A fs->angle = MAXANGLE;
546N/A fs->forward = False;
546N/A }
546N/A } else {
546N/A fs->angle -= 0.1;
546N/A if (fs->angle <= 0) {
546N/A fs->angle = 0.0;
546N/A fs->forward = True;
546N/A }
546N/A }
546N/A }
546N/A}