diff options
-rw-r--r-- | dir.h | 7 | ||||
-rw-r--r-- | file.c (renamed from dir.c) | 41 | ||||
-rw-r--r-- | file.h | 13 | ||||
-rw-r--r-- | image.c | 111 | ||||
-rw-r--r-- | image.h | 26 | ||||
-rw-r--r-- | main.c | 181 | ||||
-rw-r--r-- | makefile | 2 |
7 files changed, 195 insertions, 186 deletions
@@ -1,7 +0,0 @@ -#ifndef __DIR_H -#define __DIR_H -#define _DEFAULT_SOURCE - -int scan(const char *file_name, char ***list, int *list_count); - -#endif @@ -1,4 +1,4 @@ -#include "dir.h" +#include "file.h" #include <dirent.h> #include <libgen.h> #include <stdio.h> @@ -23,32 +23,45 @@ int image_filter(const struct dirent *dir) return 0; } -int scan(const char *file_name, char ***list, int *list_count) +int scan(const char *file_name) { struct dirent **name_list; - char **files; - int curr_index; char *dir_name = dirname(strdup(file_name)); size_t dir_name_len = strlen(dir_name); - *list_count = scandir(dir_name, &name_list, image_filter, alphasort); - if (list_count < 0) { + file_list_count = scandir(dir_name, &name_list, image_filter, alphasort); + if (file_list_count < 0) { return -1; } - files = malloc(*list_count * sizeof(char *)); + file_list = malloc(file_list_count * sizeof(char *)); char *file_basename = basename(strdup(file_name)); int i = 0; - while (i < *list_count) { + while (i < file_list_count) { if (!strcmp(file_basename, name_list[i]->d_name)) - curr_index = i; - files[i] = malloc((strlen(name_list[i]->d_name) + dir_name_len + 2) * - sizeof(char)); - sprintf(files[i], "%s/%s", dir_name, name_list[i]->d_name); + curr_file_index = i; + file_list[i] = malloc( + (strlen(name_list[i]->d_name) + dir_name_len + 2) * sizeof(char *)); + sprintf(file_list[i], "%s/%s", dir_name, name_list[i]->d_name); free(name_list[i]); i++; } - *list = files; free(name_list); - return curr_index; + return 0; +} + +char *get_next_file() +{ + if (curr_file_index >= file_list_count - 1) { + return NULL; + } + return file_list[++curr_file_index]; +} + +char *get_prev_file() +{ + if (curr_file_index <= 0) { + return NULL; + } + return file_list[--curr_file_index]; } @@ -0,0 +1,13 @@ +#ifndef __FILE_H +#define __FILE_H +#define _DEFAULT_SOURCE + +char **file_list; +char *curr_file_name; +int curr_file_index, file_list_count; + +int scan(const char *file_name); +char *get_next_file(); +char *get_prev_file(); + +#endif @@ -0,0 +1,111 @@ +#include "image.h" +#include <gtk-3.0/gtk/gtk.h> + +GtkWidget* new_image() +{ + image = gtk_image_new(); + return image; +} + +int load_image(char *file_name, int win_width, int win_height) +{ + printf("loading: %s\n", file_name); + GError *error = NULL; + if (pixbuf != NULL) + g_object_unref(pixbuf); + pixbuf = gdk_pixbuf_new_from_file(file_name, &error); + if (error) + return -1; + pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); + pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); + aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; + curr_zoom = 1.0; + fit_image(win_width, win_height); + return 1; +} + +void update_pixbuf() +{ + if (curr_pixbuf != NULL) + g_object_unref(curr_pixbuf); + if (pixbuf == NULL) + return; + curr_pixbuf = gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), pixbuf_width, + pixbuf_height, GDK_INTERP_BILINEAR); + gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf)); +} + +void fit_image(int win_width, int win_height) +{ + if (pixbuf == NULL || win_width < 1 || win_height < 1) + return; + if (win_width < pixbuf_width && win_height > pixbuf_height) { + pixbuf_width = win_width; + pixbuf_height = (double)pixbuf_width / aspect_ratio; + } else if (win_width > pixbuf_width && win_height < pixbuf_height) { + pixbuf_height = win_height; + pixbuf_width = (double)pixbuf_height * aspect_ratio; + } else if (win_width < pixbuf_width && win_height < pixbuf_height) { + if (((double)win_width / (double)win_height) > aspect_ratio) { + pixbuf_height = win_height; + pixbuf_width = ((double)pixbuf_height * aspect_ratio); + } else { + pixbuf_width = win_width; + pixbuf_height = (double)pixbuf_width / aspect_ratio; + } + } else { + pixbuf_width = pixbuf_width; + pixbuf_height = pixbuf_height; + } + + curr_zoom = (double)pixbuf_width / (double)pixbuf_width; + if (pixbuf_width < 1 || pixbuf_height < 1) + return; + + update_pixbuf(); +} + +void zoom(int type) +{ + if (pixbuf == NULL) + return; + if (type == 0) { + if (curr_zoom == 1.0) + return; + curr_zoom = (double)1.0; + } else if (type < 0) { + if (curr_zoom < 0.2) + return; + curr_zoom -= (double)ZOOM_FACTOR; + } else if (type > 0) { + if (curr_zoom > 2) + return; + curr_zoom += (double)ZOOM_FACTOR; + } + pixbuf_width *= curr_zoom; + pixbuf_height += curr_zoom; + + update_pixbuf(); +} + +void print_supported_formats() +{ + GSList *list, *it; + GdkPixbufFormat *format; + gchar **extensions; + gchar *ex; + + list = gdk_pixbuf_get_formats(); + if (list != NULL) { + for (it = list; it->next != NULL; it = it->next) { + format = (GdkPixbufFormat *)it->data; + printf("%s:", gdk_pixbuf_format_get_description(format)); + extensions = gdk_pixbuf_format_get_extensions(format); + for (ex = *extensions; *ex; ex++) { + printf(" %s", ex); + } + printf("\n"); + } + g_slist_free(list); + } +} @@ -0,0 +1,26 @@ +#ifndef __IMAGE_H +#define __IMAGE_H + +#include <gtk-3.0/gtk/gtk.h> + +#define ZOOM_FACTOR 0.1 + +enum scale_mode { + fit = 0, + zoomed = 1, +} curr_scale_mod; + +GtkWidget *image; +GdkPixbuf *pixbuf; +GdkPixbuf *curr_pixbuf; +int pixbuf_width, pixbuf_height; +double aspect_ratio; +double curr_zoom; + +GtkWidget* new_image(); +int load_image(char *file_name, int win_width, int win_height); +void fit_image(int win_width, int win_height); +void zoom(int type); +void print_supported_formats(); + +#endif @@ -1,162 +1,20 @@ -#include <curses.h> +#include "file.h" +#include "image.h" #include <dirent.h> #include <gtk-3.0/gtk/gtk.h> #include <libgen.h> #include <stdlib.h> #include <string.h> -#include "dir.h" - #define TIMEOUT 20 -#define ZOOM_FACTOR 0.1 - -enum scale_mode { - fit = 0, - zoomed = 1, -}; GtkWidget *window; -GtkWidget *image; -GdkPixbuf *pixbuf; -GdkPixbuf *curr_pixbuf; -int pixbuf_width, pixbuf_height; -double aspect_ratio; -double curr_zoom = 1.0; -enum scale_mode curr_scale_mod; +gint win_width, win_height; GError *error = NULL; -char *curr_file_name; -GDir *dir; -char **file_list; -int curr_file_index, file_list_count; - -void print_supported_formats() -{ - GSList *list, *it; - GdkPixbufFormat *format; - gchar **extensions; - gchar *ex; - - list = gdk_pixbuf_get_formats(); - if (list != NULL) { - for (it = list; it->next != NULL; it = it->next) { - format = (GdkPixbufFormat *)it->data; - g_print("%s:", gdk_pixbuf_format_get_description(format)); - extensions = gdk_pixbuf_format_get_extensions(format); - for (ex = *extensions; *ex; ex++) { - g_print(" %s", ex); - } - g_print("\n"); - } - g_slist_free(list); - } -} - -void fit_image() -{ - gint win_width, win_height; - gint new_pixbuf_width, new_pixbuf_height; - gtk_window_get_size(GTK_WINDOW(window), &win_width, &win_height); - - if (win_width < pixbuf_width && win_height > pixbuf_height) { - new_pixbuf_width = win_width; - new_pixbuf_height = (double)new_pixbuf_width / aspect_ratio; - } else if (win_width > pixbuf_width && win_height < pixbuf_height) { - new_pixbuf_height = win_height; - new_pixbuf_width = (double)new_pixbuf_height * aspect_ratio; - } else if (win_width < pixbuf_width && win_height < pixbuf_height) { - if (((double)win_width / (double)win_height) > aspect_ratio) { - new_pixbuf_height = win_height; - new_pixbuf_width = ((double)new_pixbuf_height * aspect_ratio); - } else { - new_pixbuf_width = win_width; - new_pixbuf_height = (double)new_pixbuf_width / aspect_ratio; - } - } else { - new_pixbuf_width = pixbuf_width; - new_pixbuf_height = pixbuf_height; - } - - curr_zoom = (double)new_pixbuf_width / (double)pixbuf_width; - if (curr_pixbuf != NULL) - g_object_unref(curr_pixbuf); - curr_pixbuf = - gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), new_pixbuf_width, - new_pixbuf_height, GDK_INTERP_BILINEAR); - gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf)); -} - -void next() -{ - if (curr_file_index >= file_list_count - 1) { - return; - } - curr_file_index++; - curr_file_name = file_list[curr_file_index]; - g_print("qwe2: %s\n", curr_file_name); - g_object_unref(pixbuf); - pixbuf = gdk_pixbuf_new_from_file(curr_file_name, &error); - if (error) { - g_warning("gdk_pixbuf_new_from_file() failed with error: %s\n", - error->message); - g_clear_error(&error); - return; - } - pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); - pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); - aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; - curr_zoom = 1.0; - fit_image(); -} - -void prev() -{ - if (curr_file_index <= 0) { - return; - } - curr_file_index--; - curr_file_name = file_list[curr_file_index]; - g_print("qwe2: %s\n", curr_file_name); - g_object_unref(pixbuf); - pixbuf = gdk_pixbuf_new_from_file(curr_file_name, &error); - if (error) { - g_warning("gdk_pixbuf_new_from_file() failed with error: %s\n", - error->message); - g_clear_error(&error); - return; - } - pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); - pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); - aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; - curr_zoom = 1.0; - fit_image(); -} - -void zoom(int type) -{ - if (type == 0) { - if (curr_zoom == 1.0) - return; - curr_zoom = (double)1.0; - } else if (type < 0) { - if (curr_zoom < 0.2) - return; - curr_zoom -= (double)ZOOM_FACTOR; - } else if (type > 0) { - if (curr_zoom > 2) - return; - curr_zoom += (double)ZOOM_FACTOR; - } - if (curr_pixbuf != NULL) - g_object_unref(curr_pixbuf); - curr_pixbuf = - gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), pixbuf_width * curr_zoom, - pixbuf_height * curr_zoom, GDK_INTERP_BILINEAR); - gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf)); -} - static gboolean key_press(GtkWindow *window, GdkEvent *event, gpointer data) { + char *name; switch (event->key.keyval) { case GDK_KEY_q: g_application_quit( @@ -164,7 +22,7 @@ static gboolean key_press(GtkWindow *window, GdkEvent *event, gpointer data) return FALSE; case GDK_KEY_w: curr_scale_mod = fit; - fit_image(); + fit_image(win_width, win_height); return FALSE; case GDK_KEY_plus: zoom(1); @@ -177,11 +35,13 @@ static gboolean key_press(GtkWindow *window, GdkEvent *event, gpointer data) return FALSE; case GDK_KEY_n: case GDK_KEY_j: - next(); + if ((name = get_next_file()) != NULL) + load_image(name, win_width, win_height); return FALSE; case GDK_KEY_p: case GDK_KEY_k: - prev(); + if ((name = get_prev_file()) != NULL) + load_image(name, win_width, win_height); return FALSE; default: return TRUE; @@ -209,7 +69,8 @@ gboolean resize_done(gpointer data) { guint *id = data; *id = 0; - fit_image(); + gtk_window_get_size(GTK_WINDOW(window), &win_width, &win_height); + fit_image(win_width, win_height); return FALSE; } @@ -241,28 +102,20 @@ static void activate(GtkApplication *app, gpointer user_data) char *curr_filename = "/home/nirav/Downloads/Saved Pictures/bash help shortcuts.png"; - curr_file_index = scan(curr_filename, &file_list, &file_list_count); - printf("num %d %d\n", curr_file_index, file_list_count); - printf("file %s\n", file_list[curr_file_index]); - - pixbuf = gdk_pixbuf_new_from_file(curr_filename, &error); - if (error) { - g_warning("gdk_pixbuf_new_from_file() failed with error: %s\n", - error->message); - g_clear_error(&error); + int i = scan(curr_filename); + if (i < 0) { + printf("scan() error %d\n", i); return; } - pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); - pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); - aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; - image = gtk_image_new_from_pixbuf(GDK_PIXBUF(pixbuf)); GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); - gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(image)); + gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(new_image())); gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(scrolled_window)); gtk_widget_show_all(GTK_WIDGET(window)); + + load_image(curr_filename, win_width, win_height); } int main(int argc, char *argv[]) @@ -6,7 +6,7 @@ LDFLAGS=$(shell pkg-config --libs gtk+-3.0) PREFIX=/usr BINDIR=$(PREFIX)/bin -OBJECTS=main.o dir.o +OBJECTS=main.o file.o image.o all: qwe |