Debian Codegraph @

src/callbacks.c

raw
1 /*
2 * Copyright (C) Pietro Pilolli 2010 <[email protected]>
3 *
4 * Curtain is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Curtain is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include "callbacks.h"
23 #include "utils.h"
24
25
26 /* The main cairo context; will used to draw the window. */
27 static cairo_t *cr = (cairo_t *) NULL;
28
29 /* The surface will have the same dimension of the screen and will contain the curtain. */
30 static cairo_surface_t *scaled_surface = (cairo_surface_t *) NULL;
31
32 /* Variables where will store the coordinates of the press point. */
33 static gdouble press_event_x = 0.0;
34 static gdouble press_event_y = 0.0;
35
36 /* These points identify the area with the painted curtain. */
37 static gdouble min_x = 0.0;
38 static gdouble max_x = 0.0;
39 static gdouble min_y = 0.0;
40 static gdouble max_y = 0.0;
41
42 static gint screen_width = 0;
43 static gint screen_height = 0;
44
45 /* The cursor to be displayed. */
46 static GdkCursor *cursor = (GdkCursor *) NULL;
47
48 /* Region use to shape input region. */
49 static cairo_region_t *shape_reg = (cairo_region_t *) NULL;;
50
51 /* Quit the program. */
52 static void
53 quit ()
54 {
55 if (cursor)
56 {
57 g_object_unref (cursor);
58 }
59 if (cr)
60 {
61 cairo_destroy (cr);
62 }
63 if (scaled_surface)
64 {
65 cairo_surface_destroy (scaled_surface);
66 }
67 gtk_main_quit ();
68 exit (1);
69 }
70
71
72 /* Paint the white cross to be clicked in order to close the program. */
73 static void
74 paint_cross ()
75 {
76 if (cr)
77 {
78 gint delta = 25;
79 gchar *color = "FFFFFFFF";
80
81 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
82 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
83 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
84 cairo_set_source_color_from_string (cr, color);
85 cairo_rectangle (cr, (max_x - delta), min_y, delta, delta);
86 cairo_move_to (cr, (max_x - delta), min_y);
87 cairo_line_to (cr, max_x, (min_y + delta));
88 cairo_move_to (cr, max_x, min_y);
89 cairo_line_to (cr, (max_x - delta), (min_y + delta));
90 cairo_stroke (cr);
91 }
92 }
93
94
95 /* Paint the curtain in the window. */
96 static void
97 paint_curtain ()
98 {
99 if (cr)
100 {
101 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
102 cairo_set_source_surface (cr, scaled_surface, 0, 0);
103 cairo_rectangle (cr, min_x, min_y, max_x, max_y);
104 cairo_fill (cr);
105 cairo_stroke (cr);
106 }
107 paint_cross ();
108 }
109
110
111 /* Paint a transparent rectangle on the window. */
112 static void
113 paint_transparent_rectangle (GtkWidget *widget,
114 gdouble x,
115 gdouble y,
116 gdouble width,
117 gdouble height)
118 {
119 if (cr)
120 {
121 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
122 cairo_set_source_rgba (cr, 0, 0, 0, 0);
123 cairo_rectangle (cr, x, y, width, height);
124 cairo_fill (cr);
125 cairo_stroke (cr);
126 }
127
128 const cairo_rectangle_int_t transparent_rect = {(gint) x,
129 (gint) y,
130 (gint) width,
131 (gint) height };
132
133 cairo_region_t *transparent_reg = cairo_region_create_rectangle (&transparent_rect);
134
135 cairo_region_subtract (shape_reg, transparent_reg);
136
137 gtk_widget_input_shape_combine_region (widget, shape_reg);
138 cairo_region_destroy (transparent_reg);
139 }
140
141
142 /* Destroy window callback. */
143 G_MODULE_EXPORT gboolean
144 destroy (GtkWidget *widget, gpointer data)
145 {
146 quit ();
147 return TRUE;
148 }
149
150 /* On configure event. */
151 G_MODULE_EXPORT gboolean
152 on_window_configure (GtkWidget *widget,
153 GdkEventExpose *event,
154 gpointer user_data)
155 {
156 GdkWindowState state = gdk_window_get_state (gtk_widget_get_window (widget));
157 gint is_fullscreen = state & GDK_WINDOW_STATE_FULLSCREEN;
158
159 if (!is_fullscreen)
160 {
161 return FALSE;
162 }
163 if (! scaled_surface)
164 {
165 cursor = gdk_cursor_new (GDK_HAND1);
166 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
167 cairo_surface_t *surface = cairo_image_surface_create_from_png (CURTAIN_FILE);
168 gtk_window_set_opacity (GTK_WINDOW (widget), 1.0);
169 screen_width = gdk_screen_width ();
170 screen_height = gdk_screen_height ();
171 max_x = screen_width;
172 max_y = screen_height;
173 cr = gdk_cairo_create (gtk_widget_get_window (widget));
174
175 scaled_surface = scale_surface (surface, screen_width, screen_height);
176 cairo_surface_destroy (surface);
177 paint_curtain ();
178 }
179 return TRUE;
180 }
181
182 /* Expose event: this occurs when the windows is exposed. */
183 G_MODULE_EXPORT gboolean
184 on_window_expose_event (GtkWidget *widget,
185 cairo_t *cr,
186 gpointer user_data)
187 {
188 //paint_curtain ();
189
190 return TRUE;
191 }
192
193
194 /* Destroy callback. */
195 G_MODULE_EXPORT gboolean
196 on_window_destroy_event (GtkWindow *self,
197 GdkEvent *event,
198 gpointer user_data)
199 {
200 quit ();
201 return TRUE;
202 }
203
204
205 /* On screen changed. */
206 G_MODULE_EXPORT void
207 on_window_screen_changed (GtkWidget *widget,
208 GdkScreen *previous_screen,
209 gpointer user_data)
210 {
211 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (widget));
212 GdkVisual *visual = gdk_screen_get_rgba_visual (screen);
213
214 if (visual == NULL)
215 {
216 visual = gdk_screen_get_system_visual (screen);
217 }
218
219 gtk_widget_set_visual (widget, visual);
220 }
221
222
223 /* On press callback. */
224 G_MODULE_EXPORT gboolean
225 on_window_button_press_event (GtkWidget *widget,
226 GdkEventButton *event,
227 gpointer data)
228 {
229 /* only button1 allowed */
230 if (event->button != 1)
231 {
232 return FALSE;
233 }
234
235 /* Is inside the close pseudo button. */
236 if (((event->x) > (max_x - 25)) &&
237 ((event->y) < (min_y + 25)))
238 {
239 quit ();
240 }
241
242 gtk_widget_input_shape_combine_region (widget, NULL);
243 const cairo_rectangle_int_t shape_rect = { 0,
244 0,
245 gdk_screen_width (),
246 gdk_screen_height () };
247
248 cairo_region_destroy(shape_reg);
249 shape_reg = cairo_region_create_rectangle (&shape_rect);
250
251 press_event_x = event->x;
252 press_event_y = event->y;
253 return TRUE;
254 }
255
256
257 /* Release callback. */
258 G_MODULE_EXPORT gboolean
259 on_window_button_release_event (GtkWidget *widget,
260 GdkEventButton *event,
261 gpointer data)
262 {
263 gdouble delta_x = fabs (press_event_x-event->x);
264 gdouble delta_y = fabs (press_event_y-event->y);
265
266 gint min_curtain_width = 25;
267 gint min_curtain_height = 25;
268
269 /* only button1 allowed and ignore null event */
270 if ((! event) || (event->button != 1))
271 {
272 return FALSE;
273 }
274
275 if (delta_y <= delta_x)
276 {
277 /* move on x axis */
278 if (event->x >= press_event_x)
279 {
280 /* Right gesture */
281 if ((press_event_x < (max_x + min_x)/2) && ( event->x <= max_x))
282 {
283 /* sx to the curtain ->| */
284 if (event->x > min_x)
285 {
286 /* move right of delta_x */
287 //printf("->|\n");
288 min_x = event->x;
289 if (min_x > max_x-min_curtain_width)
290 {
291 min_x = max_x-min_curtain_width;
292 }
293
294 paint_transparent_rectangle (widget,
295 0,
296 0,
297 min_x,
298 screen_height);
299
300 paint_transparent_rectangle (widget,
301 max_x,
302 0,
303 screen_width-max_x,
304 screen_height);
305
306 }
307 }
308 else
309 {
310 /* dx to the curtain |-> */
311 if (event->x > max_x)
312 {
313 //printf("|->\n");
314 paint_transparent_rectangle (widget,
315 event->x,
316 0,
317 screen_width-event->x,
318 screen_height);
319
320 max_x = event->x;
321 if (max_x > screen_width)
322 {
323 max_x = screen_width;
324 }
325
326 paint_transparent_rectangle (widget,
327 0,
328 0,
329 min_x,
330 screen_height);
331
332 }
333 }
334 }
335 else
336 {
337 /* Left gesture */
338 if ((press_event_x > (max_x + min_x)/2) && (event->x >= min_x))
339 {
340 /* dx to curtain |<- */
341 if (event->x < max_x)
342 {
343 /* move left */
344 //printf("|<-\n");
345 max_x = event->x;
346 if(max_x-min_x < min_curtain_width)
347 {
348 max_x = min_x + min_curtain_width;
349 }
350
351 paint_transparent_rectangle (widget, max_x,
352 0,
353 (screen_width - max_x),
354 screen_height);
355
356 paint_transparent_rectangle (widget,
357 0,
358 0,
359 min_x,
360 screen_height);
361
362 }
363 }
364 else
365 {
366 /* sx to curtain <-| */
367 if (event->x < min_x)
368 {
369 //printf(" <-|\n");
370
371 paint_transparent_rectangle (widget,
372 0,
373 0,
374 event->x,
375 screen_height);
376
377 min_x = event->x;
378 if ((max_x - min_x) < min_curtain_width)
379 {
380 max_x = min_x + min_curtain_width;
381 }
382
383 paint_transparent_rectangle (widget,
384 max_x,
385 0,
386 (screen_width - max_x),
387 screen_height);
388
389 }
390 }
391 }
392 if (min_y > 0)
393 {
394
395 paint_transparent_rectangle (widget,
396 0,
397 0,
398 screen_width,
399 min_y);
400
401 }
402 if (max_y < screen_height)
403 {
404
405 paint_transparent_rectangle (widget,
406 0,
407 max_y,
408 screen_width,
409 (screen_height - max_y));
410
411 }
412 }
413 else
414 {
415 /* move on y axis */
416 if (event->y >= press_event_y)
417 {
418 /* down gesture */
419 if ((press_event_y < (max_y + min_y)/2) && (event->y <= max_y))
420 {
421 if (event->y > min_y)
422 {
423 /* up half */
424 min_y = event->y;
425 if (min_y > (max_y - min_curtain_height))
426 {
427 min_y = max_y - min_curtain_height;
428 }
429
430 paint_transparent_rectangle (widget,
431 0,
432 0,
433 screen_width,
434 min_y);
435
436 paint_transparent_rectangle (widget,
437 0,
438 max_y,
439 screen_width,
440 (screen_height - max_y));
441
442 }
443 }
444 else
445 {
446 if (event->y > max_y)
447 {
448 /* down half */
449
450 paint_transparent_rectangle (widget,
451 0,
452 event->y,
453 screen_width,
454 (screen_height - event->y));
455
456 max_y = event->y;
457 if (max_y > screen_height)
458 {
459 max_y = screen_height;
460 }
461
462 paint_transparent_rectangle (widget,
463 0,
464 0,
465 screen_width, min_y);
466
467 }
468 }
469 }
470 else
471 {
472 /* up gesture */
473 if ((press_event_y < (max_y + min_y)/2) || (event->y < min_y))
474 {
475 if (event->y < min_y)
476 {
477 /* up half */
478 //printf("down gesture in first half\n");
479 min_y = event->y;
480 if (min_y > (max_y - min_curtain_height))
481 {
482 min_y = max_y - min_curtain_height;
483 }
484
485 paint_transparent_rectangle (widget,
486 0,
487 0,
488 screen_width, min_y);
489
490 paint_transparent_rectangle (widget,
491 0,
492 max_y,
493 screen_width,
494 screen_height - max_y);
495
496 }
497 }
498 else
499 {
500 if (event->y < max_y)
501 {
502 /* down half */
503 //printf("down gesture in second half\n");
504 max_y = event->y;
505 if((max_y - min_y) < min_curtain_height)
506 {
507 max_y = min_y + min_curtain_height;
508 }
509
510 paint_transparent_rectangle (widget,
511 0,
512 max_y,
513 screen_width,
514 (screen_height - max_y));
515
516 paint_transparent_rectangle (widget,
517 0,
518 0,
519 screen_width, min_y);
520
521 }
522 }
523 }
524 if (min_x > 0)
525 {
526
527 paint_transparent_rectangle (widget,
528 0,
529 0,
530 min_x,
531 screen_height);
532
533 }
534 if (max_x < screen_width)
535 {
536
537 paint_transparent_rectangle (widget,
538 max_x,
539 0,
540 (screen_width - max_x),
541 screen_height);
542
543 }
544 }
545 ungrab_pointer (gdk_display_get_default ());
546 paint_curtain ();
547 return TRUE;
548 }
549
550