File: aperture-demo-window.c

package info (click to toggle)
libaperture-0 0.1.0%2Bgit20221220-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: ansic: 2,318; xml: 63; sh: 32; makefile: 9
file content (234 lines) | stat: -rw-r--r-- 7,260 bytes parent folder | download | duplicates (2)
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
/* aperture-demo-window.c
 *
 * Copyright 2020 James Westman <james@flyingpimonster.net>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This file 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */


#include "aperture-demo-window.h"

struct _ApertureDemoWindow
{
  GtkApplicationWindow parent_instance;

  GtkStack *stack;

  GtkWidget *controls;
  ApertureViewfinder *viewfinder;
  GtkStack *controls_stack;
  GtkStack *no_cameras_stack;
  GtkButton *switch_camera;

  gboolean recording;
  gboolean taking_picture;
};

G_DEFINE_TYPE (ApertureDemoWindow, aperture_demo_window, GTK_TYPE_APPLICATION_WINDOW)


static char *
get_file (GUserDirectory user_dir, gchar *extension)
{
  /* Get a file path for an image or video. The filename is based on the
   * date and time. If that file already exists, a number will be added to
   * the end so that a new file is created. */

  g_autoptr(GDateTime) date = g_date_time_new_now_local ();
  const char *dir = g_get_user_special_dir (user_dir);
  g_autofree char *filename = g_date_time_format (date, "%F_%T");
  g_autofree char *basename = g_strdup_printf ("%s.%s", filename, extension);
  char *path = g_build_filename (dir, basename, NULL);

  for (int i = 1; g_file_test (path, G_FILE_TEST_EXISTS); i ++) {
    g_free (path);
    g_free (basename);

    basename = g_strdup_printf ("%s_%d.%s", filename, i, extension);
    path = g_build_filename (dir, basename, NULL);
  }

  return path;
}


static void
update_ui (ApertureDemoWindow *self)
{
  /* Update the UI depending on the state of the viewfinder and the current
   * operation.
   *
   * If it's recording, show the stop button, otherwise show the regular
   * controls. If it's not in the READY state, make the controls insensitive
   * so they can't be used. If there are no cameras, show that screen
   * instead. */

  ApertureViewfinderState state = aperture_viewfinder_get_state (self->viewfinder);
  gtk_widget_set_sensitive (self->controls, state == APERTURE_VIEWFINDER_STATE_READY && !(self->recording || self->taking_picture));

  gtk_stack_set_visible_child_name (self->controls_stack, self->recording ? "video" : "main");

  gtk_stack_set_visible_child_name (self->no_cameras_stack,
                                    state == APERTURE_VIEWFINDER_STATE_NO_CAMERAS
                                      ? "no_cameras"
                                      : "main");
}


static void
on_viewfinder_state_notify (ApertureDemoWindow *self)
{
  update_ui (self);
}


static void
on_photo_taken (ApertureViewfinder *source, GAsyncResult *res, ApertureDemoWindow *self)
{
  g_autofree char *file = get_file (G_USER_DIRECTORY_PICTURES, "jpg");
  g_autoptr(GError) err = NULL;
  g_autoptr(GdkPixbuf) pixbuf = aperture_viewfinder_take_picture_finish (source, res, &err);

  if (err) {
    g_critical ("%s", err->message);
  } else {
    gdk_pixbuf_save (pixbuf, file, "jpeg", &err, NULL);
    if (err) {
      g_critical ("%s", err->message);
    } else {
      g_debug ("Saved picture to %s", (char *) file);
    }
  }

  self->taking_picture = FALSE;
  update_ui (self);
}


static void
on_take_photo_clicked (GtkButton *button, ApertureDemoWindow *self)
{
  self->taking_picture = TRUE;
  update_ui (self);
  aperture_viewfinder_take_picture_async (self->viewfinder, NULL, (GAsyncReadyCallback) on_photo_taken, self);
}


static void
on_take_video_clicked (GtkButton *button, ApertureDemoWindow *self)
{
  g_autofree char *file = get_file (G_USER_DIRECTORY_VIDEOS, "mp4");
  g_autoptr(GError) err = NULL;

  self->recording = TRUE;
  update_ui (self);

  aperture_viewfinder_start_recording_to_file (self->viewfinder, file, &err);

  if (err) {
    g_critical ("%s", err->message);
  }
}


static void
on_video_done (ApertureViewfinder *source, GAsyncResult *res, ApertureDemoWindow *self)
{
  self->recording = FALSE;
  update_ui (self);
}


static void
on_stop_video_clicked (GtkButton *button, ApertureDemoWindow *self)
{
  aperture_viewfinder_stop_recording_async (self->viewfinder, NULL, (GAsyncReadyCallback) on_video_done, self);
}


static void
on_barcode_detected (ApertureViewfinder *viewfinder, ApertureBarcode barcode_type, const char *data)
{
  const char *type = g_enum_to_string (APERTURE_TYPE_BARCODE, barcode_type);
  g_print ("Barcode detected (%s): %s\n", type, data);
}


static void
on_switch_camera_clicked (GtkButton *button, ApertureDemoWindow *self)
{
  /* When the "switch camera" button is clicked, use
   * aperture_device_manager_next_camera() to find the next camera, and
   * start using that. */

  g_autoptr(ApertureCamera) camera = NULL;
  g_autoptr(ApertureDeviceManager) manager = aperture_device_manager_get_instance ();
  g_autoptr(GError) err = NULL;

  camera = aperture_device_manager_next_camera (manager, aperture_viewfinder_get_camera (self->viewfinder));
  aperture_viewfinder_set_camera (self->viewfinder, camera, &err);

  if (err) {
    g_critical ("%s", err->message);
  }
}


/* INIT */


static void
aperture_demo_window_class_init (ApertureDemoWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/io/gnome/Aperture/Demo/ui/demo-window.ui");
  gtk_widget_class_bind_template_child (widget_class, ApertureDemoWindow, viewfinder);
  gtk_widget_class_bind_template_child (widget_class, ApertureDemoWindow, controls);
  gtk_widget_class_bind_template_child (widget_class, ApertureDemoWindow, stack);
  gtk_widget_class_bind_template_child (widget_class, ApertureDemoWindow, controls_stack);
  gtk_widget_class_bind_template_child (widget_class, ApertureDemoWindow, switch_camera);
  gtk_widget_class_bind_template_child (widget_class, ApertureDemoWindow, no_cameras_stack);
  gtk_widget_class_bind_template_callback (widget_class, on_barcode_detected);
  gtk_widget_class_bind_template_callback (widget_class, on_stop_video_clicked);
  gtk_widget_class_bind_template_callback (widget_class, on_switch_camera_clicked);
  gtk_widget_class_bind_template_callback (widget_class, on_take_photo_clicked);
  gtk_widget_class_bind_template_callback (widget_class, on_take_video_clicked);
  gtk_widget_class_bind_template_callback (widget_class, on_viewfinder_state_notify);
}


static void
aperture_demo_window_init (ApertureDemoWindow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));

  /* Make sure the UI is in the correct state */
  on_viewfinder_state_notify (self);
}


/* PUBLIC */


ApertureDemoWindow *
aperture_demo_window_new (GtkApplication *app)
{
  g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);

  return g_object_new (APERTURE_TYPE_DEMO_WINDOW, "application", app, NULL);
}