X-Git-Url: https://git.jaslogic.tech/photobox.git/blobdiff_plain/b8d88334e0dbc2a4a1a36a9098425ebd9a647232..HEAD:/src/photobox.c?ds=sidebyside diff --git a/src/photobox.c b/src/photobox.c index 15986e7..e30496f 100644 --- a/src/photobox.c +++ b/src/photobox.c @@ -1,3 +1,5 @@ +// Copyright (C) 2019 Jaslo Ziska + #include #include #include @@ -10,9 +12,9 @@ #include #include "photobox_photo.h" -#include "apikey.h" +#include "apikey.h" // are you looking for this? :) -// fehlt in glib?? +// missing in glib??? #define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) void pb_exit(void); @@ -47,12 +49,12 @@ static unsigned int button_pin = 26; int main(int argc, char *argv[]) { if (pb_ph_init() != 0) { - fprintf(stderr, "ERROR: pb_ph_init\n"); - return 1; + fprintf(stderr, "ERROR in pb_ph_init\n"); + return EXIT_FAILURE; } if (gpio_map() == NULL) { - fprintf(stderr, "ERROR: gpio_map\n"); - return 1; + fprintf(stderr, "ERROR in gpio_map\n"); + return EXIT_FAILURE; } gtk_init(&argc, &argv); @@ -135,12 +137,13 @@ int main(int argc, char *argv[]) gtk_main(); - return 0; + return EXIT_SUCCESS; } void pb_exit(void) { gpio_unmap(); + pb_ph_uninit(); gtk_main_quit(); } @@ -155,7 +158,7 @@ void pb_show_main(void) gtk_widget_show(button_take); - // take picture with button + // take picture with button (ugly) g_idle_add(G_SOURCE_FUNC(pb_poll_buttton), NULL); } @@ -185,17 +188,30 @@ void pb_show_qr(void) pid_t pid = fork(); if (pid == 0) { // child -> copy and upload - if (pb_cp_dp(id) < 0) { - fprintf(stderr, "Error: pb_cp_dp\n"); + if (pb_cp_dp(id) != 0) { + fprintf(stderr, "ERROR in pb_cp_dp\n"); exit(EXIT_FAILURE); } else { exit(EXIT_SUCCESS); } } else if (pid < 0) { - fprintf(stderr, "FORK ERROR!!!!!\n"); - // TODO: dont show qr + fprintf(stderr, "ERROR in fork (big problem)\n"); + + // try to at least save the picture (the ugly way) + char *cmd = "cp tmp.jpg bilder/xxxxxx.jpg"; + for (unsigned int i = 0; i < 6; ++i) { + cmd[i + 18] = id[i]; + } + if (system(cmd) != 0) + perror("ERROR in system(), picture could not be saved"); + + // go back to start, dont show qr + pb_show_main(); + return; } + // parent -> continue GUI + // url qr should point to // example: https://www.dropbox.com/sh/3pq0pwednyuig86/AACx0_vjn-liY5_pP_C3nJD8a?dl=0&preview=c1b052c5.jpg char url[] = "https://www.dropbox.com/sh/3pq0pwednyuig86/AACx0_vjn-liY5_pP_C3nJD8a?dl=0&preview=xxxxxx.jpg"; @@ -207,7 +223,8 @@ void pb_show_qr(void) QRcode *qr = QRcode_encodeString(url, 0, QR_ECLEVEL_H, QR_MODE_8, 1); if (qr == NULL) { perror("Failed to generate QR-Code"); - pb_exit(); + pb_show_main(); + return; } unsigned int size = qr->width; @@ -239,6 +256,7 @@ void pb_show_qr(void) gtk_label_set_markup(GTK_LABEL(qr_label), text); } +// not proud of this gboolean pb_poll_buttton(void) { if (gpio_tst(button_pin) == 0) { @@ -249,6 +267,7 @@ gboolean pb_poll_buttton(void) } } +// neither of this gboolean pb_countdown(void) { static int num = 0; @@ -291,18 +310,18 @@ gboolean pb_countdown(void) gtk_label_set_markup(GTK_LABEL(label_countdown), "0"); - // 0 zeigen bevor der anfängt zu rechnen + // do all pendfing events (show 0) before taking picture while (gtk_events_pending()) { gtk_main_iteration(); } - // bild aufnehmen + // take pic pb_takepic(); pb_show_send(); return G_SOURCE_REMOVE; } - // wenn das passiert ist was schief gelaufen - printf("etwas ist sehr schief gelaufen\n"); + + printf("ERROR: this should never happen?\n"); return G_SOURCE_REMOVE; } @@ -310,7 +329,7 @@ void pb_takepic(void) { char *fn = "tmp.jpg"; if (pb_ph_capture_file(fn) != 0) { - fprintf(stderr, "ERROR: pb_ph_capture_file\n"); + fprintf(stderr, "ERROR in pb_ph_capture_file\n"); gtk_main_quit(); return; } @@ -338,7 +357,7 @@ int pb_cp_dp(char *id) FILE *src_file = fopen(src, "rb"); if (src_file == NULL) { - perror("Error opening \"tmp.jpg\""); + perror("Error opening tmp.jpg"); return -1; } FILE *dest_file = fopen(dest, "wb+"); @@ -348,27 +367,33 @@ int pb_cp_dp(char *id) return -1; } + // copy file do { num_read = fread(buffer, 1, 4096, src_file); dest_fsize += num_read; if (fwrite(buffer, 1, num_read, dest_file) != num_read && ferror(dest_file) != 0) { fprintf(stderr, "Error writing to destination file"); + fclose(src_file); goto error_file; } } while(num_read == 4096); + if (ferror(src_file) != 0) { - fprintf(stderr, "Error reading from \"tmp.jpg\""); + fclose(src_file); + fprintf(stderr, "Error reading from tmp.jpg\n"); + perror("^"); goto error_file; } fclose(src_file); + rewind(dest_file); // from beginning for upload // upload curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init(); if (curl == NULL) { - fprintf(stderr, "ERROR: curl_easy_init\n"); + fprintf(stderr, "ERROR in curl_easy_init\n"); curl_global_cleanup(); goto error_file; } @@ -391,19 +416,10 @@ int pb_cp_dp(char *id) CURLcode ret = curl_easy_perform(curl); if (ret != CURLE_OK) { - fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(ret)); + fprintf(stderr, "ERROR in curl_easy_perform: %s\n", curl_easy_strerror(ret)); goto error_curl; } - curl_slist_free_all(header); - curl_easy_cleanup(curl); - - curl_global_cleanup(); - - fclose(dest_file); - - return 0; - error_curl: curl_slist_free_all(header); curl_easy_cleanup(curl); @@ -413,5 +429,5 @@ int pb_cp_dp(char *id) error_file: fclose(dest_file); - return -1; + return ret; }