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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * CAFE panel x stuff
 *
 * Copyright (C) 2000, 2001 Eazel, Inc.
 *               2002 Sun Microsystems Inc.
 *
 * Authors: George Lebl <jirka@5z.com>
 *          Mark McLoughlin <mark@skynet.ie>
 *
 *  Contains code from the Window Maker window manager
 *
 *  Copyright (c) 1997-2002 Alfredo K. Kojima

 */

#include <config.h>

#ifndef HAVE_X11
#error file should only be built when HAVE_X11 is enabled
#endif

#include <string.h>
#include <unistd.h>

#include <cdk/cdk.h>
#include <cdk/cdkx.h>
#include <ctk/ctk.h>

#include <X11/Xlib.h>

#include "panel-enums.h"
#include "xstuff.h"

static int (* xstuff_old_xio_error_handler) (Display *) = NULL;
static int (* xstuff_old_x_error_handler)   (Display *, XErrorEvent *);
static gboolean xstuff_display_is_dead = FALSE;

/* Zoom animation */
#define MINIATURIZE_ANIMATION_FRAMES_Z   1
#define MINIATURIZE_ANIMATION_STEPS_Z    6
/* the delay per draw */
#define MINIATURIZE_ANIMATION_DELAY_Z    10

/* zoom factor, steps and delay if composited (factor must be odd) */
#define ZOOM_FACTOR 5
#define ZOOM_STEPS  14
#define ZOOM_DELAY 10

gboolean is_using_x11 ()
{
	return CDK_IS_X11_DISPLAY (cdk_display_get_default ());
}

typedef struct {
	int size;
	int size_start;
	int size_end;
	PanelOrientation orientation;
	double opacity;
	GdkPixbuf *pixbuf;
	guint timeout_id;
} CompositedZoomData;

static gboolean
zoom_timeout (CtkWidget *window)
{
	ctk_widget_queue_draw (window);
	return TRUE;
}

static gboolean
idle_destroy (gpointer data)
{
	ctk_widget_destroy (CTK_WIDGET (data));

	return FALSE;
}

static gboolean
zoom_draw (CtkWidget *widget,
	     cairo_t *cr,
	     gpointer        user_data)
{
	CompositedZoomData *zoom;

	zoom = user_data;

	if (zoom->size >= zoom->size_end) {
		if (zoom->timeout_id)
			g_source_remove (zoom->timeout_id);
		zoom->timeout_id = 0;

		ctk_widget_hide (widget);
		g_idle_add (idle_destroy, widget);

		g_object_unref (zoom->pixbuf);
		zoom->pixbuf = NULL;

		g_slice_free (CompositedZoomData, zoom);
	} else {
		GdkPixbuf *scaled;
		int width, height;
		int x = 0, y = 0;

		ctk_window_get_size (CTK_WINDOW (widget), &width, &height);

		zoom->size += MAX ((zoom->size_end - zoom->size_start) / ZOOM_STEPS, 1);
		zoom->opacity -= 1.0 / ((double) ZOOM_STEPS + 1);

		scaled = gdk_pixbuf_scale_simple (zoom->pixbuf,
						  zoom->size, zoom->size,
						  GDK_INTERP_BILINEAR);

		switch (zoom->orientation) {
		case PANEL_ORIENTATION_TOP:
			x = (width - gdk_pixbuf_get_width (scaled)) / 2;
			y = 0;
			break;

		case PANEL_ORIENTATION_RIGHT:
			x = width - gdk_pixbuf_get_width (scaled);
			y = (height - gdk_pixbuf_get_height (scaled)) / 2;
			break;

		case PANEL_ORIENTATION_BOTTOM:
			x = (width - gdk_pixbuf_get_width (scaled)) / 2;
			y = height - gdk_pixbuf_get_height (scaled);
			break;

		case PANEL_ORIENTATION_LEFT:
			x = 0;
			y = (height - gdk_pixbuf_get_height (scaled)) / 2;
			break;
		}

		cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
		cairo_set_source_rgba (cr, 0, 0, 0, 0.0);
		cairo_rectangle (cr, 0, 0, width, height);
		cairo_fill (cr);

		cdk_cairo_set_source_pixbuf (cr, scaled, x, y);
		cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
		cairo_paint_with_alpha (cr, MAX (zoom->opacity, 0));

		g_object_unref (scaled);
	}

	return FALSE;
}

static void
draw_zoom_animation_composited (CdkScreen *gscreen,
				int x, int y, int w, int h,
				GdkPixbuf *pixbuf,
				PanelOrientation orientation)
{
	CtkWidget *win;
	CompositedZoomData *zoom;
	int wx = 0, wy = 0;

	w += 2;
	h += 2;

	zoom = g_slice_new (CompositedZoomData);
	zoom->size = w;
	zoom->size_start = w;
	zoom->size_end = w * ZOOM_FACTOR;
	zoom->orientation = orientation;
	zoom->opacity = 1.0;
	zoom->pixbuf = g_object_ref (pixbuf);
	zoom->timeout_id = 0;

	win = ctk_window_new (CTK_WINDOW_POPUP);

	ctk_window_set_screen (CTK_WINDOW (win), gscreen);
	ctk_window_set_keep_above (CTK_WINDOW (win), TRUE);
	ctk_window_set_decorated (CTK_WINDOW (win), FALSE);
	ctk_widget_set_app_paintable(win, TRUE);
	ctk_widget_set_visual (win, cdk_screen_get_rgba_visual (gscreen));

	ctk_window_set_gravity (CTK_WINDOW (win), CDK_GRAVITY_STATIC);
	ctk_window_set_default_size (CTK_WINDOW (win),
				     w * ZOOM_FACTOR, h * ZOOM_FACTOR);

	switch (zoom->orientation) {
	case PANEL_ORIENTATION_TOP:
		wx = x - w * (ZOOM_FACTOR / 2);
		wy = y;
		break;

	case PANEL_ORIENTATION_RIGHT:
		wx = x - w * (ZOOM_FACTOR - 1);
		wy = y - h * (ZOOM_FACTOR / 2);
		break;

	case PANEL_ORIENTATION_BOTTOM:
		wx = x - w * (ZOOM_FACTOR / 2);
		wy = y - h * (ZOOM_FACTOR - 1);
		break;

	case PANEL_ORIENTATION_LEFT:
		wx = x;
		wy = y - h * (ZOOM_FACTOR / 2);
		break;
	}

	ctk_window_move (CTK_WINDOW (win), wx, wy);

	g_signal_connect (G_OBJECT (win), "draw",
			 G_CALLBACK (zoom_draw), zoom);

	/* see doc for ctk_widget_set_app_paintable() */
	ctk_widget_realize (win);
	cdk_window_set_background_pattern (ctk_widget_get_window (win), NULL);
	ctk_widget_show (win);

	zoom->timeout_id = g_timeout_add (ZOOM_DELAY,
					  (GSourceFunc) zoom_timeout,
					  win);
}

static void
draw_zoom_animation (CdkScreen *gscreen,
		     int x, int y, int w, int h,
		     int fx, int fy, int fw, int fh,
		     int steps)
{
#define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
	float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
	int cxi[FRAMES], cyi[FRAMES], cwi[FRAMES], chi[FRAMES];
	float xstep, ystep, wstep, hstep;
	int i, j;
	GC frame_gc;
	XGCValues gcv;
	CdkColor color = { 65535, 65535, 65535 };
	Display *dpy;
	Window root_win;
	int screen;
	int depth;

	dpy = cdk_x11_display_get_xdisplay (cdk_screen_get_display (gscreen));
	root_win = CDK_WINDOW_XID (cdk_screen_get_root_window (gscreen));
	screen = cdk_x11_screen_get_screen_number (gscreen);
	depth = DefaultDepth(dpy,screen);

	/* frame GC */
	gcv.function = GXxor;
	/* this will raise the probability of the XORed color being different
	 * of the original color in PseudoColor when not all color cells are
	 * initialized */
	if (DefaultVisual(dpy, screen)->class==PseudoColor)
		gcv.plane_mask = (1<<(depth-1))|1;
	else
		gcv.plane_mask = AllPlanes;
	gcv.foreground = color.pixel;
	if (gcv.foreground == 0)
		gcv.foreground = 1;
	gcv.line_width = 1;
	gcv.subwindow_mode = IncludeInferiors;
	gcv.graphics_exposures = False;

	frame_gc = XCreateGC(dpy, root_win, GCForeground|GCGraphicsExposures
			     |GCFunction|GCSubwindowMode|GCLineWidth
			     |GCPlaneMask, &gcv);

	xstep = (float)(fx-x)/steps;
	ystep = (float)(fy-y)/steps;
	wstep = (float)(fw-w)/steps;
	hstep = (float)(fh-h)/steps;

	for (j=0; j<FRAMES; j++) {
		cx[j] = (float)x;
		cy[j] = (float)y;
		cw[j] = (float)w;
		ch[j] = (float)h;
		cxi[j] = (int)cx[j];
		cyi[j] = (int)cy[j];
		cwi[j] = (int)cw[j];
		chi[j] = (int)ch[j];
	}
	XGrabServer(dpy);
	for (i=0; i<steps; i++) {
		for (j=0; j<FRAMES; j++) {
			XDrawRectangle(dpy, root_win, frame_gc, cxi[j], cyi[j], cwi[j], chi[j]);
		}
		XFlush(dpy);
#if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
		usleep(MINIATURIZE_ANIMATION_DELAY_Z);
#else
		usleep(10);
#endif
		for (j=0; j<FRAMES; j++) {
			XDrawRectangle(dpy, root_win, frame_gc,
				       cxi[j], cyi[j], cwi[j], chi[j]);
			if (j<FRAMES-1) {<--- Assuming that condition 'j<1-1' is not redundant<--- Assuming that condition 'j<1-1' is not redundant<--- Assuming that condition 'j<1-1' is not redundant<--- Assuming that condition 'j<1-1' is not redundant<--- Assuming that condition 'j<1-1' is not redundant<--- Assuming that condition 'j<1-1' is not redundant<--- Assuming that condition 'j<1-1' is not redundant<--- Assuming that condition 'j<1-1' is not redundant
				cx[j]=cx[j+1];<--- Negative array index
				cy[j]=cy[j+1];<--- Negative array index
				cw[j]=cw[j+1];<--- Negative array index
				ch[j]=ch[j+1];<--- Negative array index

				cxi[j]=cxi[j+1];<--- Negative array index
				cyi[j]=cyi[j+1];<--- Negative array index
				cwi[j]=cwi[j+1];<--- Negative array index
				chi[j]=chi[j+1];<--- Negative array index

			} else {
				cx[j]+=xstep;
				cy[j]+=ystep;
				cw[j]+=wstep;
				ch[j]+=hstep;

				cxi[j] = (int)cx[j];
				cyi[j] = (int)cy[j];
				cwi[j] = (int)cw[j];
				chi[j] = (int)ch[j];
			}
		}
	}

	for (j=0; j<FRAMES; j++) {
		XDrawRectangle(dpy, root_win, frame_gc,
				       cxi[j], cyi[j], cwi[j], chi[j]);
	}
	XFlush(dpy);
#if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
	usleep(MINIATURIZE_ANIMATION_DELAY_Z);
#else
	usleep(10);
#endif
	for (j=0; j<FRAMES; j++) {
		XDrawRectangle(dpy, root_win, frame_gc,
				       cxi[j], cyi[j], cwi[j], chi[j]);
	}

	XUngrabServer(dpy);
	XFreeGC (dpy, frame_gc);
}
#undef FRAMES

void
xstuff_zoom_anicafe (CtkWidget *widget,
		     cairo_surface_t *surface,
		     PanelOrientation orientation,
		     CdkRectangle *opt_rect)<--- Parameter 'opt_rect' can be declared as pointer to const
{
	CdkScreen *gscreen;
	CdkRectangle rect, dest;
	CtkAllocation allocation;
	CdkMonitor *monitor;
	CdkDisplay *display;

	if (opt_rect)
		rect = *opt_rect;
	else {
		cdk_window_get_origin (ctk_widget_get_window (widget), &rect.x, &rect.y);
		ctk_widget_get_allocation (widget, &allocation);
		if (!ctk_widget_get_has_window (widget)) {
			rect.x += allocation.x;
			rect.y += allocation.y;
		}
		rect.height = allocation.height;
		rect.width = allocation.width;
	}

	gscreen = ctk_widget_get_screen (widget);

	if (cdk_screen_is_composited (gscreen) && surface) {
		GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface (surface,
				0, 0,
				cairo_image_surface_get_width (surface),
				cairo_image_surface_get_height (surface));
		draw_zoom_animation_composited (gscreen,
				rect.x, rect.y,
				rect.width, rect.height,
				pixbuf, orientation);
		g_object_unref (pixbuf);
	} else {
		display = cdk_screen_get_display (gscreen);
		monitor = cdk_display_get_monitor_at_window (display,
							     ctk_widget_get_window (widget));
		cdk_monitor_get_geometry (monitor, &dest);

		draw_zoom_animation (gscreen,
				     rect.x, rect.y, rect.width, rect.height,
				     dest.x, dest.y, dest.width, dest.height,
				     MINIATURIZE_ANIMATION_STEPS_Z);
	}
}

static int
xstuff_xio_error_handler (Display *display)
{
	xstuff_display_is_dead = TRUE;

	if (xstuff_old_xio_error_handler)
		return xstuff_old_xio_error_handler (display);

	return 0;
}

gboolean
xstuff_is_display_dead (void)
{
	return xstuff_display_is_dead;
}

static int
xstuff_x_error_handler (Display *display, XErrorEvent *error)
{
	if (!error->error_code)
		return 0;

	/* If we got a BadDrawable or a BadWindow, we ignore it for now.
	 * FIXME: We need to somehow distinguish real errors from
	 * X-server-induced errors. Keeping a list of windows for which we will
	 * ignore BadDrawables would be a good idea.  */
	if (error->error_code == BadDrawable ||
	    error->error_code == BadWindow)
		return 0;

	return xstuff_old_x_error_handler (display, error);
}

void
xstuff_init (void)
{
	xstuff_old_xio_error_handler = XSetIOErrorHandler (xstuff_xio_error_handler);
	xstuff_old_x_error_handler = XSetErrorHandler (xstuff_x_error_handler);
}