* src/hildon-remote-texture.c * src/hildon-remote-texture.h * src/hildon-remote-textu...
[hildon] / examples / hildon-remote-texture-example.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <glib.h>
4 #include <gtk/gtk.h>
5 #include <sys/types.h>
6 #include <sys/shm.h>
7 #include <string.h>
8 #include <math.h>
9 #include "hildon.h"
10
11 #define W 800
12 #define H 480
13
14 /* Press and drag to pan around, or tap to move between zoom levels.
15  * A file must be specified on the command-line, and it must be > 640x480.
16  *
17  * This file will be loaded and placed into a shared memory area, which will
18  * be read by hildon-desktop.
19  * An area 640x480 in the top-left corner will be updated with an animation
20  * to show how to modify the area. */
21
22 #define DO_ANIM 1 /* whether to animate the area defined below */
23
24 #define ANIM_X 0
25 #define ANIM_Y 0
26 #define ANIM_WIDTH 640
27 #define ANIM_HEIGHT 480
28 #define ANIM_FRAMES 64
29
30 /* this could come from ftol, but we hardcode it for this example */
31 static key_t shm_key = 0xCAFEBEEF;
32
33 static gint  bpp, width, height;
34 static guchar *shm = 0; /* shared mem area */
35 static int anim_frame = 0;
36
37 static double scale = 1;
38 static double scale_smooth = 1;
39 static double x = 0;
40 static double y = 0;
41 static double x_inc = 0;
42 static double y_inc = 0;
43 static double cursor_move = 0;
44
45 static int last_x, last_y, pressed = 0;
46
47 static void timeout_cb (void *obj)
48 {
49     HildonRemoteTexture *actor = HILDON_REMOTE_TEXTURE (obj);
50
51     if (shm && DO_ANIM) {
52       gint x,y;
53
54       for (y=0;y<ANIM_HEIGHT;y++)
55         {
56           guchar *anim = &shm[(width*(y+ANIM_Y) + ANIM_X)*bpp];
57           for (x=0;x<ANIM_WIDTH;x++)
58             {
59               *anim = ((x+anim_frame)&(ANIM_FRAMES-1)) * 256 / ANIM_FRAMES;
60               anim += bpp;
61             }
62         }
63
64       hildon_remote_texture_update_area(actor,
65           ANIM_X, ANIM_Y, ANIM_WIDTH, ANIM_HEIGHT);
66       anim_frame++;
67     }
68
69     if (!pressed)
70       {
71         if (fabs(x_inc)>0.01 || fabs(y_inc)>0.01)
72           {
73             x += x_inc;
74             y += y_inc;
75             x_inc = x_inc * 0.8;
76             y_inc = y_inc * 0.8;
77             hildon_remote_texture_set_offset (actor, x, y);
78           }
79
80         if (fabs(scale - scale_smooth)>0.01)
81           {
82             scale_smooth = scale_smooth*0.9 + scale*0.1;
83             hildon_remote_texture_set_scale (actor, scale_smooth, scale_smooth);
84           }
85       }
86
87 }
88
89 static void press_cb (GtkWidget *widget,
90                GdkEventButton *event, gpointer data)
91 {
92   pressed = TRUE;
93   last_x = event->x;
94   last_y = event->y;
95   cursor_move = 0;
96 }
97
98 static void release_cb (GtkWidget *widget,
99                GdkEventButton *event, gpointer obj)
100 {
101   pressed = FALSE;
102   last_x = event->x;
103   last_y = event->y;
104
105   if (cursor_move < 40)
106     {
107       scale = scale * 1.5;
108       if (scale>8) scale=0.125;
109       /* scale smooth will actually do the update */
110     }
111 }
112
113 static void motion_cb (GtkWidget *widget,
114                 GdkEventMotion *event, gpointer data)
115 {
116   HildonRemoteTexture *actor = HILDON_REMOTE_TEXTURE(data);
117
118   if (pressed)
119     {
120       x_inc = event->x - last_x;
121       y_inc = event->y - last_y;
122       cursor_move += fabs(x_inc) + fabs(y_inc);
123
124       x_inc /= scale;
125       y_inc /= scale;
126
127       x += x_inc;
128       y += y_inc;
129       hildon_remote_texture_set_offset (actor, x, y);
130     }
131
132   last_x = event->x;
133   last_y = event->y;
134 }
135
136 int
137 main (int argc, char **argv)
138 {
139     gtk_init (&argc, &argv);
140
141     GdkPixbuf        *pixbuf;
142     GError           *error = 0;
143     guchar           *gpixels;
144
145     if (argc!=2)
146       {
147         printf("USAGE: hildon-remote-texture-example image.png\n");
148         printf("NB. Image should be 640x480 in size or greater\n");
149         return EXIT_FAILURE;
150       }
151
152     g_debug("Loading Image %s...", argv[1]);
153     pixbuf          = gdk_pixbuf_new_from_file (argv[1], &error);
154     width           = gdk_pixbuf_get_width (pixbuf);
155     height          = gdk_pixbuf_get_height (pixbuf);
156     bpp             = gdk_pixbuf_get_n_channels (pixbuf); /* assume 8 bit */
157     gpixels         = gdk_pixbuf_get_pixels (pixbuf);
158     g_debug("Creating Shared Memory");
159     size_t shm_size = width*height*bpp;
160     int shmid;
161
162     /*
163      * Create the segment, attach it to our data space, and copy in the
164      * texture we loaded
165      */
166     if ((shmid = shmget(shm_key, shm_size, IPC_CREAT | 0666)) < 0) {
167         perror("shmget");
168         exit(1);
169     }
170
171     if ((shm = shmat(shmid, NULL, 0)) == (guchar *) -1) {
172         perror("shmat");
173         exit(1);
174     }
175
176     memcpy(shm, gpixels, shm_size);
177     g_debug("Done.");
178
179     /* Craete the program with the remote texture */
180     HildonProgram *program = hildon_program_get_instance ();
181     HildonWindow *window = HILDON_WINDOW(
182         hildon_window_new());
183     hildon_program_add_window (program, window);
184     gtk_widget_show (GTK_WIDGET(window));
185
186     HildonRemoteTexture *actor = HILDON_REMOTE_TEXTURE
187         (hildon_remote_texture_new ());
188
189     g_set_application_name ("Animation");
190
191     g_signal_connect (G_OBJECT (window),
192                       "delete_event",
193                       G_CALLBACK (gtk_main_quit), NULL);
194     g_signal_connect (G_OBJECT (window),
195                               "button-press-event",
196                               G_CALLBACK (press_cb),
197                               actor);
198     g_signal_connect (G_OBJECT (window),
199                                   "button-release-event",
200                                   G_CALLBACK (release_cb),
201                                   actor);
202     g_signal_connect (G_OBJECT (window),
203                       "motion-notify-event",
204                       G_CALLBACK (motion_cb),
205                       actor);
206     gtk_widget_add_events (GTK_WIDGET(window),
207                            GDK_BUTTON_PRESS_MASK|
208                            GDK_BUTTON_MOTION_MASK|
209                            GDK_BUTTON_RELEASE_MASK);
210
211
212     gtk_widget_show_all (GTK_WIDGET (actor));
213     gdk_flush ();
214
215     /* vital: add this remote texture to a proper window */
216     hildon_remote_texture_set_parent(actor, GTK_WINDOW(window));
217     /* Set the actual data we'll be displaying, via the shm key */
218     hildon_remote_texture_set_image(actor, shm_key, width, height, bpp);
219     /* Set the actual position on the screen */
220     hildon_remote_texture_set_position (actor, 0, 56, W, H-56);
221     hildon_remote_texture_set_show (actor, 1);
222
223     g_timeout_add (25, (GSourceFunc)timeout_cb, actor);
224
225     printf ("going to gtk_main ()\n");
226     gtk_main ();
227     return 0;
228 }