File: blackhole.c

package info (click to toggle)
xjokes 1.0-14
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 240 kB
  • ctags: 110
  • sloc: ansic: 1,194; makefile: 39
file content (170 lines) | stat: -rw-r--r-- 3,218 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include	<X11/Xlib.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<unistd.h>

#define	N 10

Display *dpy;
Window win;
GC copygc, fillgc;
int screen;

void
usage (char *filename)
{
  fprintf (stderr, "Usage: %s [-display <displayname>]\n", filename);
  exit (1);
}

extern void do_all ();

int
main (int argc, char **argv)
{
  char *display = NULL;
  unsigned long vmask;
  XSetWindowAttributes xswat;
  XGCValues gcvals;


  switch (argc)
    {
    case 1:
      break;
    case 3:
      display = argv[2];
      break;
    default:
      usage (argv[0]);
      exit (1);
    }

  if ((dpy = XOpenDisplay (display)) == NULL)
    {
      fprintf (stderr, "can't open display\n");
      exit (0);
    }

  screen = DefaultScreen (dpy);

  xswat.override_redirect = True;
  xswat.do_not_propagate_mask =
    KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
  vmask = CWOverrideRedirect | CWDontPropagate;
  win = XCreateWindow (dpy, RootWindow (dpy, screen), 0, 0,
		       DisplayWidth (dpy, screen), DisplayHeight (dpy,
								  screen), 0,
		       CopyFromParent, CopyFromParent, CopyFromParent, vmask,
		       &xswat);
  XMapWindow (dpy, win);

  gcvals.graphics_exposures = False;

  gcvals.foreground = 1;
  gcvals.background = 0;
  copygc = XCreateGC (dpy, win,
		      GCForeground | GCBackground | GCGraphicsExposures,
		      &gcvals);

  gcvals.foreground = BlackPixel (dpy, screen);
  fillgc = XCreateGC (dpy, win, GCForeground, &gcvals);

  XSync (dpy, 0);

  do_all ();

  exit (0);
}

typedef struct
{
  int x, y;
  int d;
} mask_type;
int
comp (const void *ii, const void *jj)
{
  const mask_type *i = ii;
  const mask_type *j = jj;
  return i->d - j->d;
}

mask_type *mask;

inline int
isqr (int a)
{
  return a * a;
}

void
mov (int fw, int tw, int width, int height)
{
  int w, h;
  int wx, hy, fox, foy, tox, toy, fh, th, i;
  mask_type *ma;

  th = height * tw / (width);
  w = tw / N;
  h = th / N;

  fh = height * fw / (width);
  wx = fw / N;
  hy = fh / N;

  fox = (width - fw) / 2;
  foy = (height - fh) / 2;

  tox = (width - tw) / 2;
  toy = (height - th) / 2;

  ma = mask;
  for (i = 0; i < N * N; i++)
    {
      XCopyArea (dpy, win, win, copygc,
		 fox + wx * ma->x, foy + hy * ma->y,
		 w, h, tox + w * ma->x, toy + h * ma->y);
      ma++;
    }
  XFillRectangle (dpy, win, fillgc, fox, foy, fw, toy - foy);
  XFillRectangle (dpy, win, fillgc, fox, foy, tox - fox, fh);
  XFillRectangle (dpy, win, fillgc, fox, foy + hy * N, fw, toy - foy + 5);
  XFillRectangle (dpy, win, fillgc, fox + wx * N, foy, tox - fox + 5, fh + 5);

}

void
do_all ()
{
  int height, width;
  int w;
  int x, y;
  mask_type *ma;
  width = DisplayWidth (dpy, screen);
  height = DisplayHeight (dpy, screen);

  ma = mask = (mask_type *) malloc (sizeof (mask_type) * N * N);
  for (y = 0; y < N; y++)
    {
      for (x = 0; x < N; x++)
	{
	  ma->x = x;
	  ma->y = y;
	  ma->d = isqr ((x + 1) * 2 - N) + isqr ((y + 1) * 2 - N);
	  ma++;
	}
    }
  qsort (mask, N * N, sizeof (mask_type), comp);

  for (w = width; w > 30; w -= 10)
    {
      mov (w, w - 10, width, height);
    }

  XFillRectangle (dpy, win, fillgc, 0, 0, width, height);

  XSync (dpy, 0);
  sleep (1);
  exit (1);
}