Pārlūkot izejas kodu

Re-enable flash, refactor to use libmegapixels

Kristian Vos 5 mēneši atpakaļ
vecāks
revīzija
b1c46f7800
7 mainītis faili ar 43 papildinājumiem un 106 dzēšanām
  1. 21 84
      src/flash.c
  2. 3 8
      src/flash.h
  3. 9 10
      src/io_pipeline.c
  4. 4 1
      src/main.c
  5. 4 1
      src/process_pipeline.c
  6. 0 1
      src/process_pipeline.h
  7. 2 1
      src/state.h

+ 21 - 84
src/flash.c

@@ -5,48 +5,7 @@
 #include <inttypes.h>
 #include <stdio.h>
 #include <unistd.h>
-
-typedef enum {
-        FLASH_TYPE_LED,
-        FLASH_TYPE_DISPLAY,
-} FlashType;
-
-typedef struct {
-        char path[260];
-        int fd;
-} MPLEDFlash;
-
-typedef struct {
-} MPDisplayFlash;
-
-struct _MPFlash {
-        FlashType type;
-
-        union {
-                MPLEDFlash led;
-                MPDisplayFlash display;
-        };
-};
-
-MPFlash *
-mp_led_flash_from_path(const char *path)
-{
-        MPFlash *flash = malloc(sizeof(MPFlash));
-        flash->type = FLASH_TYPE_LED;
-
-        strncpy(flash->led.path, path, 259);
-
-        char mpath[275];
-        snprintf(mpath, 275, "%s/flash_strobe", path);
-        flash->led.fd = open(mpath, O_WRONLY);
-        if (flash->led.fd == -1) {
-                g_printerr("Failed to open %s\n", mpath);
-                free(flash);
-                return NULL;
-        }
-
-        return flash;
-}
+#include <libmegapixels.h>
 
 static GtkWidget *flash_window = NULL;
 static GDBusProxy *dbus_brightness_proxy = NULL;
@@ -95,29 +54,6 @@ mp_flash_gtk_clean()
         g_object_unref(dbus_brightness_proxy);
 }
 
-MPFlash *
-mp_create_display_flash()
-{
-        MPFlash *flash = malloc(sizeof(MPFlash));
-        flash->type = FLASH_TYPE_DISPLAY;
-
-        return flash;
-}
-
-void
-mp_flash_free(MPFlash *flash)
-{
-        switch (flash->type) {
-        case FLASH_TYPE_LED:
-                close(flash->led.fd);
-                break;
-        case FLASH_TYPE_DISPLAY:
-                break;
-        }
-
-        free(flash);
-}
-
 static void
 set_display_brightness(int brightness)
 {
@@ -155,7 +91,7 @@ brightness_received(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
 }
 
 static bool
-show_display_flash(MPFlash *flash)
+show_display_flash(libmegapixels_camera *camera)
 {
         if (!flash_window)
                 return false;
@@ -183,21 +119,21 @@ show_display_flash(MPFlash *flash)
 }
 
 void
-mp_flash_enable(MPFlash *flash)
+mp_flash_enable(libmegapixels_camera *camera)
 {
-        switch (flash->type) {
-        case FLASH_TYPE_LED:
-                lseek(flash->led.fd, 0, SEEK_SET);
-                dprintf(flash->led.fd, "1\n");
-                break;
-        case FLASH_TYPE_DISPLAY:
-                g_main_context_invoke(NULL, (GSourceFunc)show_display_flash, flash);
-                break;
+        switch (camera->flash_type) {
+                case LIBMEGAPIXELS_FLASH_V4L:
+                case LIBMEGAPIXELS_FLASH_LED:
+                        libmegapixels_flash_on(camera);
+                        break;
+                case LIBMEGAPIXELS_FLASH_SCREEN:
+                        g_main_context_invoke(NULL, (GSourceFunc)show_display_flash, camera);
+                        break;
         }
 }
 
 static bool
-hide_display_flash(MPFlash *flash)
+hide_display_flash(libmegapixels_camera *camera)
 {
         if (!flash_window)
                 return false;
@@ -209,14 +145,15 @@ hide_display_flash(MPFlash *flash)
 }
 
 void
-mp_flash_disable(MPFlash *flash)
+mp_flash_disable(libmegapixels_camera *camera)
 {
-        switch (flash->type) {
-        case FLASH_TYPE_LED:
-                // Flash gets reset automatically
-                break;
-        case FLASH_TYPE_DISPLAY:
-                g_main_context_invoke(NULL, (GSourceFunc)hide_display_flash, flash);
-                break;
+        switch (camera->flash_type) {
+                case LIBMEGAPIXELS_FLASH_V4L:
+                case LIBMEGAPIXELS_FLASH_LED:
+                        libmegapixels_flash_off(camera);
+                        break;
+                case LIBMEGAPIXELS_FLASH_SCREEN:
+                        g_main_context_invoke(NULL, (GSourceFunc)hide_display_flash, camera);
+                        break;
         }
 }

+ 3 - 8
src/flash.h

@@ -1,13 +1,8 @@
 #include "gio/gio.h"
-
-typedef struct _MPFlash MPFlash;
+#include <libmegapixels.h>
 
 void mp_flash_gtk_init(GDBusConnection *conn);
 void mp_flash_gtk_clean();
 
-MPFlash *mp_led_flash_from_path(const char *path);
-MPFlash *mp_create_display_flash();
-void mp_flash_free(MPFlash *flash);
-
-void mp_flash_enable(MPFlash *flash);
-void mp_flash_disable(MPFlash *flash);
+void mp_flash_enable(libmegapixels_camera *camera);
+void mp_flash_disable(libmegapixels_camera *camera);

+ 9 - 10
src/io_pipeline.c

@@ -113,6 +113,8 @@ update_process_pipeline()
                 .focus.manual = state_io.focus.manual,
 
                 .balance = { balance_red, 1.0f, balance_blue },
+
+                .flash_enabled = state_io.flash_enabled,
         };
 
         mp_process_pipeline_update_state(&new_state);
@@ -179,11 +181,9 @@ capture(MPPipeline *pipeline, const void *data)
         mp_camera_start_capture(mpcamera);
 
         // Enable flash
-        /* TODO: implement
-        if (info->flash && flash_enabled) {
-                mp_flash_enable(info->flash);
+        if (state_io.flash_enabled) {
+                mp_flash_enable(state_io.camera);
         }
-         */
 
         update_process_pipeline();
 
@@ -335,8 +335,9 @@ do_aaa()
 static void
 on_frame(MPBuffer buffer, void *_data)
 {
-        // Don't process frame when the window is not active
-        if (!check_window_active()) {
+        // Don't process frame when the window is not active, unless we're capturing an image,
+        // in which case the flash window may be active instead of this window
+        if (!check_window_active() && state_io.captures_remaining == 0) {
                 return;
         }
 
@@ -405,11 +406,9 @@ on_frame(MPBuffer buffer, void *_data)
                         mp_camera_start_capture(mpcamera);
 
                         // Disable flash
-                        /* TODO: implement
-                        if (info->flash && flash_enabled) {
-                                mp_flash_disable(info->flash);
+                        if (state_io.flash_enabled) {
+                                mp_flash_disable(state_io.camera);
                         }
-                         */
 
                         update_process_pipeline();
                 }

+ 4 - 1
src/main.c

@@ -147,6 +147,8 @@ update_io_pipeline()
                 .stats.temp = state.stats.temp,
                 .stats.tint = state.stats.tint,
                 .stats.focus = state.stats.focus,
+
+                .flash_enabled = state.flash_enabled,
         };
         mp_io_pipeline_update_state(&new_state);
 }
@@ -197,7 +199,8 @@ update_state(const mp_state_main *new_state)
         state.stats.focus = new_state->stats.focus;
 
         // Make the right settings available for the camera
-        gtk_widget_set_visible(flash_button, state.control_flash);
+        // Even if there's no flash led/v4l, it'll just default to using the screen as flash, so always enable this button
+        gtk_widget_set_visible(flash_button, true);
         gtk_widget_set_visible(iso_button, state.gain.control != 0);
         gtk_widget_set_visible(shutter_button, state.exposure.control != 0);
 

+ 4 - 1
src/process_pipeline.c

@@ -1315,6 +1315,8 @@ update_state(MPPipeline *pipeline, const mp_state_proc *new_state)
         state_proc.balance[1] = new_state->balance[1];
         state_proc.balance[2] = new_state->balance[2];
 
+        state_proc.flash_enabled = new_state->flash_enabled;
+
         if (output_changed) {
                 state_proc.camera_rotation = mod(
                         state_proc.mode->rotation - state_proc.device_rotation, 360);
@@ -1337,7 +1339,6 @@ update_state(MPPipeline *pipeline, const mp_state_proc *new_state)
                 .has_auto_focus_start = false,
                 .preview_buffer_width = output_buffer_width,
                 .preview_buffer_height = output_buffer_height,
-                .control_flash = false,
 
                 .gain.control = state_proc.gain.control,
                 .gain.auto_control = state_proc.gain.auto_control,
@@ -1363,6 +1364,8 @@ update_state(MPPipeline *pipeline, const mp_state_proc *new_state)
                 .stats.temp = state_proc.stats.temp,
                 .stats.tint = state_proc.stats.tint,
                 .stats.focus = state_proc.stats.focus,
+
+                .flash_enabled = state_proc.flash_enabled,
         };
         mp_main_update_state(&new_main);
 }

+ 0 - 1
src/process_pipeline.h

@@ -34,7 +34,6 @@ struct mp_process_pipeline_state {
 
         bool control_gain;
         bool control_exposure;
-        bool control_flash;
         bool control_focus;
 };
 

+ 2 - 1
src/state.h

@@ -31,7 +31,6 @@ typedef struct state_main {
         int burst_length;
 
         // Control state
-        bool control_flash;
         bool flash_enabled;
         controlstate gain;
         controlstate exposure;
@@ -109,4 +108,6 @@ typedef struct state_proc {
         int mode_balance;
         int mode_exposure;
         int mode_focus;
+
+        bool flash_enabled;
 } mp_state_proc;