gf-flashspot.c 5.55 KB
Newer Older
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
/*
 * Copyright (C) 2015 Alberts Muktupāvels
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Based on code in gnome-screenshot:
 * https://git.gnome.org/browse/gnome-screenshot/tree/src/cheese-flash.c
 * Copyright (C) 2008 Alexander “weej” Jones, 2008 Thomas Perl,
 *               2009 daniel g. siegel
 */

#include "config.h"

#include <gtk/gtk.h>

#include "gf-flashspot.h"

#define FLASH_DURATION 100
#define FLASH_ANIMATION_RATE 200

struct _GfFlashspot
{
  GObject    parent;

  GtkWidget *window;

  guint      flash_id;
  guint      fade_id;
};

42
43
44
45
46
47
48
49
50
enum
{
  SIGNAL_FINISHED,

  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

51
52
53
54
55
56
57
58
59
60
61
G_DEFINE_TYPE (GfFlashspot, gf_flashspot, G_TYPE_OBJECT)

static gboolean
fade (gpointer user_data)
{
  GfFlashspot *flashspot;
  gdouble opacity;

  flashspot = GF_FLASHSPOT (user_data);
  opacity = gtk_widget_get_opacity (flashspot->window);

62
63
64
  opacity -= 0.02;

  gtk_widget_set_opacity (flashspot->window, opacity);
65
66
67
68
69

  if (opacity <= 0.01)
    {
      gtk_widget_hide (flashspot->window);

70
71
      g_signal_emit (flashspot, signals[SIGNAL_FINISHED], 0);

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
      flashspot->fade_id = 0;
      return G_SOURCE_REMOVE;
    }

  return G_SOURCE_CONTINUE;
}

static gboolean
start_fade (gpointer user_data)
{
  GfFlashspot *flashspot;
  GdkScreen *screen;

  flashspot = GF_FLASHSPOT (user_data);
  screen = gtk_widget_get_screen (flashspot->window);

  if (!gdk_screen_is_composited (screen))
    {
      gtk_widget_hide (flashspot->window);

92
93
      g_signal_emit (flashspot, signals[SIGNAL_FINISHED], 0);

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
      flashspot->flash_id = 0;
      return G_SOURCE_REMOVE;
    }

  flashspot->fade_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
                                           1000.0 / FLASH_ANIMATION_RATE,
                                           fade,
                                           g_object_ref (flashspot),
                                           g_object_unref);

  flashspot->flash_id = 0;
  return G_SOURCE_REMOVE;
}

static void
gf_flashspot_dispose (GObject *object)
{
  GfFlashspot *flashspot;

  flashspot = GF_FLASHSPOT (object);

  if (flashspot->window != NULL)
    {
      gtk_widget_destroy (flashspot->window);
      flashspot->window = NULL;
    }

  if (flashspot->flash_id > 0)
    {
      g_source_remove (flashspot->flash_id);
      flashspot->flash_id = 0;
    }

  if (flashspot->fade_id > 0)
    {
      g_source_remove (flashspot->fade_id);
      flashspot->fade_id = 0;
    }

  G_OBJECT_CLASS (gf_flashspot_parent_class)->dispose (object);
}

static void
gf_flashspot_class_init (GfFlashspotClass *flashspot_class)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (flashspot_class);

  object_class->dispose = gf_flashspot_dispose;
144
145
146
147
148
149
150

  signals[SIGNAL_FINISHED] =
    g_signal_new ("finished",
                  G_OBJECT_CLASS_TYPE (flashspot_class),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
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
}

static void
gf_flashspot_init (GfFlashspot *flashspot)
{
  GtkWindow *window;
  GdkScreen *screen;
  GdkVisual *visual;
  cairo_region_t *input_region;

  flashspot->window = gtk_window_new (GTK_WINDOW_POPUP);

  window = GTK_WINDOW (flashspot->window);
  screen = gtk_widget_get_screen (flashspot->window);
  visual = gdk_screen_get_rgba_visual (screen);

  if (visual == NULL)
    visual = gdk_screen_get_system_visual (screen);

  gtk_window_set_decorated (window, FALSE);
  gtk_window_set_skip_taskbar_hint (window, TRUE);
  gtk_window_set_skip_pager_hint (window, TRUE);
  gtk_window_set_keep_above (window, TRUE);
  gtk_window_set_type_hint (window, GDK_WINDOW_TYPE_HINT_NOTIFICATION);
  gtk_window_set_accept_focus (window, FALSE);
  gtk_window_set_focus_on_map (window, FALSE);

  gtk_widget_set_visual (flashspot->window, visual);
  gtk_widget_realize (flashspot->window);

  input_region = cairo_region_create ();
  gdk_window_input_shape_combine_region (gtk_widget_get_window (flashspot->window),
                                         input_region, 0, 0);
  cairo_region_destroy (input_region);
}

GfFlashspot *
gf_flashspot_new (void)
{
  return g_object_new (GF_TYPE_FLASHSPOT, NULL);
}

void
gf_flashspot_fire (GfFlashspot *flashspot,
                   gint         x,
                   gint         y,
                   gint         width,
                   gint         height)
{
  GtkWindow *window;

  if (flashspot->flash_id > 0)
    g_source_remove (flashspot->flash_id);

  if (flashspot->fade_id > 0)
    g_source_remove (flashspot->fade_id);

  window = GTK_WINDOW (flashspot->window);

  gtk_window_move (window, x, y);
  gtk_window_resize (window, width, height);

  gtk_widget_set_opacity (flashspot->window, 0.99);
  gtk_widget_show_all (flashspot->window);

  flashspot->flash_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
                                            FLASH_DURATION,
                                            start_fade,
                                            g_object_ref (flashspot),
                                            g_object_unref);
}