[gnome-panel/wip/free-the-fish: 7/8] Free-the-fish: Cleanup code
- From: Sebastian Geiger <segeiger src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-panel/wip/free-the-fish: 7/8] Free-the-fish: Cleanup code
- Date: Tue, 2 Jun 2015 11:03:14 +0000 (UTC)
commit 67c9304c2f9773e6ee880a25ae04138db88ab8cb
Author: Sebastian Geiger <sbastig gmx net>
Date: Mon Jun 1 18:46:20 2015 +0200
Free-the-fish: Cleanup code
gnome-panel/free-the-fish.c | 87 ++++++++++++++++++++++++------------------
1 files changed, 50 insertions(+), 37 deletions(-)
---
diff --git a/gnome-panel/free-the-fish.c b/gnome-panel/free-the-fish.c
index ec0e0df..0b39d0c 100644
--- a/gnome-panel/free-the-fish.c
+++ b/gnome-panel/free-the-fish.c
@@ -20,9 +20,11 @@
/* Some important code copied from PonG */
typedef struct _FreeTheFish FreeTheFish;
struct _FreeTheFish {
- GdkWindow *win;
+ GdkWindow *window;
+ /* If the fish is about hide it self by moving out of the screen area. */
gboolean hide_mode;
int state;
+ /* x,y indicate position, xs, ys indicate speed.*/
int x, y, xs, ys;
int handler;
cairo_pattern_t *fish_pattern[FISH_FRAMES];
@@ -30,6 +32,7 @@ struct _FreeTheFish {
cairo_region_t *fish_shape;
cairo_region_t *fish_shape_reverse;
};
+
static FreeTheFish fish = {NULL};
static void
@@ -54,12 +57,13 @@ fish_kill (void)
cairo_region_destroy (fish.fish_shape_reverse);
fish.fish_shape_reverse = NULL;
- gdk_window_destroy (fish.win);
+ gdk_window_destroy (fish.window);
g_source_remove ((guint) fish.handler);
memset (&fish, 0, sizeof (FreeTheFish));
+ /* We need to remove our fish_handle_event function and restore the original gtk function. */
gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
}
@@ -75,16 +79,16 @@ fish_draw (int orient, int frame)
region = orient ? fish.fish_shape : fish.fish_shape_reverse;
shape_offset = orient ? -frame : frame + 1 - FISH_FRAMES;
- cr = gdk_cairo_create (fish.win);
+ cr = gdk_cairo_create (fish.window);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source (cr, pattern);
cairo_paint (cr);
cairo_destroy (cr);
/* Yes. Don't ask. */
- gdk_window_set_background_pattern (fish.win, pattern);
+ gdk_window_set_background_pattern (fish.window, pattern);
- gdk_window_shape_combine_region (fish.win, region,
+ gdk_window_shape_combine_region (fish.window, region,
shape_offset * FISH_WIDTH, 0);
}
@@ -106,7 +110,7 @@ fish_move (gpointer data)
g_random_int() % (fish.hide_mode?10:50) == 0)
fish.ys = -fish.ys;
- fish.state ++;
+ fish.state++;
if (fish.hide_mode) {
fish.state ++;
if (fish.state >= 2* FISH_FRAMES)
@@ -124,16 +128,34 @@ fish_move (gpointer data)
if (change)
fish_draw (orient, frame);
- gdk_window_move (fish.win, fish.x, fish.y);
- gdk_window_raise (fish.win);
+ gdk_window_move (fish.window, fish.x, fish.y);
+ gdk_window_raise (fish.window);
return TRUE;
}
+/* Starts Wandas hide mode. Wanda will move fast and it will move towards the
+ * side of the screen that is closer to it.*/
+static void fish_start_hide_mode() {
+ g_source_remove ((guint) fish.handler);
+ fish.handler = g_timeout_add (FISH_HIDE_TIMEOUT,
+ fish_move, NULL);
+ fish.xs *= FISH_XS_HIDE_FACTOR;
+ fish.ys *= FISH_YS_HIDE_FACTOR;
+ fish.hide_mode = TRUE;
+ if (fish.xs > 0) {
+ if (fish.x < (gdk_screen_width () / 2))
+ fish.xs *= -1;
+ } else {
+ if (fish.x > (gdk_screen_width () / 2))
+ fish.xs *= -1;
+ }
+}
+
static void
fish_handle_event (GdkEvent *event)
{
- if (event->any.window != fish.win)
+ if (event->any.window != fish.window)
goto out;
if (fish.hide_mode)
@@ -143,21 +165,10 @@ fish_handle_event (GdkEvent *event)
case GDK_SCROLL:
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
- case GDK_3BUTTON_PRESS:
- g_source_remove ((guint) fish.handler);
- fish.handler = g_timeout_add (FISH_HIDE_TIMEOUT,
- fish_move, NULL);
- fish.xs *= FISH_XS_HIDE_FACTOR;
- fish.ys *= FISH_YS_HIDE_FACTOR;
- fish.hide_mode = TRUE;
- if (fish.xs > 0) {
- if (fish.x < (gdk_screen_width () / 2))
- fish.xs *= -1;
- } else {
- if (fish.x > (gdk_screen_width () / 2))
- fish.xs *= -1;
- }
+ case GDK_3BUTTON_PRESS: {
+ fish_start_hide_mode();
break;
+ };
default:
break;
}
@@ -169,6 +180,8 @@ out:
static GdkPixbuf *
get_fish_pixbuf (void)
{
+ /* See panel.gresource.xml
+ * This uses ../applets/fish/wanda.png */
return gdk_pixbuf_new_from_resource ("/org/gnome/panel/anim/wanda.png", NULL);
}
@@ -178,18 +191,18 @@ fish_unsea (cairo_surface_t *surface)
guchar *pixels = cairo_image_surface_get_data (surface);
int rs = cairo_image_surface_get_stride (surface);
int w = cairo_image_surface_get_width (surface);
- int h = cairo_image_surface_get_height (surface);
- int x, y;
+ int height = cairo_image_surface_get_height (surface);
+ int x, row;
guint32 *p;
guchar a, r, g, b;
- for (y = 0; y < h; y++, pixels += rs) {
+ for (row = 0; row < height; row++, pixels += rs) {
p = (guint32 *) pixels;
for (x = 0; x < w; x++, p++) {
- a = ((*p)>>24);
- r = ((*p)>>16)&0xff;
- g = ((*p)>> 8)&0xff;
- b = ((*p)>> 0)&0xff;
+ a = (guchar) ((*p)>>24);
+ r = (guchar) (((*p)>>16)&0xff);
+ g = (guchar) (((*p)>> 8)&0xff);
+ b = (guchar) (((*p)>> 0)&0xff);
if (FISH_PIXEL_STORE_MOVE(a, r, g, b))
*p = 0;
}
@@ -281,7 +294,7 @@ get_fish_shape_reverse (cairo_surface_t *surface)
/* this checks the screen */
static void
-check_screen (void)
+fish_init (void)
{
GdkWindowAttr attributes;
GdkPixbuf *fish_pixbuf;
@@ -291,7 +304,7 @@ check_screen (void)
int orient;
int i;
- if (fish.win != NULL)
+ if (fish.window != NULL)
return;
fish_pixbuf = get_fish_pixbuf ();
@@ -341,12 +354,12 @@ check_screen (void)
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.event_mask = GDK_BUTTON_PRESS_MASK;
- fish.win = gdk_window_new (NULL, &attributes,
+ fish.window = gdk_window_new (NULL, &attributes,
GDK_WA_X | GDK_WA_Y);
for (i = 0; i < FISH_FRAMES; i++) {
- fish.fish_pattern[i] = get_fish_frame (fish.win, surface, i);
- fish.fish_pattern_reverse[i] = get_fish_frame_reverse (fish.win, surface, i);
+ fish.fish_pattern[i] = get_fish_frame (fish.window, surface, i);
+ fish.fish_pattern_reverse[i] = get_fish_frame_reverse (fish.window, surface, i);
}
fish.fish_shape = get_fish_shape (surface);
@@ -355,7 +368,7 @@ check_screen (void)
cairo_surface_destroy (surface);
fish_draw (0, 0);
- gdk_window_show (fish.win);
+ gdk_window_show (fish.window);
gdk_event_handler_set ((GdkEventFunc)fish_handle_event, NULL, NULL);
@@ -369,7 +382,7 @@ check_screen_timeout (gpointer data)
{
screen_check_id = 0;
- check_screen ();
+ fish_init();
screen_check_id = g_timeout_add (FISH_CHECK_TIMEOUT,
check_screen_timeout, NULL);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]