Skip to content

Commit

Permalink
Add linked list implementation
Browse files Browse the repository at this point in the history
Allows to use common code for list manipulations.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Aug 17, 2024
1 parent aeff45d commit 3dde4de
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 261 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ sources = [
'src/keybind.c',
'src/loader.c',
'src/main.c',
'src/memdata.c',
'src/pixmap.c',
'src/str.c',
'src/sway.c',
'src/ui.c',
'src/viewer.c',
Expand Down
2 changes: 1 addition & 1 deletion src/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "action.h"

#include "str.h"
#include "memdata.h"

#include <ctype.h>
#include <stdlib.h>
Expand Down
67 changes: 26 additions & 41 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include "application.h"

#include "buildcfg.h"
#include "config.h"
#include "font.h"
#include "gallery.h"
#include "imagelist.h"
#include "info.h"
#include "loader.h"
#include "str.h"
#include "sway.h"
#include "ui.h"
#include "viewer.h"
Expand Down Expand Up @@ -47,9 +47,9 @@ struct watchfd {
};

/* Application event queue (list). */
struct event_entry {
struct event_queue {
struct list list;
struct event event;
struct event_entry* next;
};

/** Application context */
Expand All @@ -59,7 +59,7 @@ struct application {
struct watchfd* wfds; ///< FD polling descriptors
size_t wfds_num; ///< Number of polling FD

struct event_entry* events; ///< Event queue
struct event_queue* events; ///< Event queue
pthread_mutex_t events_lock; ///< Event queue lock
int event_signal; ///< Queue change notification

Expand Down Expand Up @@ -122,15 +122,17 @@ static void handle_event_queue(__attribute__((unused)) void* data)
notification_reset(ctx.event_signal);

while (ctx.events && ctx.state == loop_run) {
struct event_entry* entry;
struct event_queue* entry = NULL;
pthread_mutex_lock(&ctx.events_lock);
entry = ctx.events;
if (ctx.events) {
ctx.events = ctx.events->next;
entry = ctx.events;
ctx.events = list_remove(entry);
}
pthread_mutex_unlock(&ctx.events_lock);
ctx.ehandler(&entry->event);
free(entry);
if (entry) {
ctx.ehandler(&entry->event);
free(entry);
}
}
}

Expand All @@ -140,27 +142,18 @@ static void handle_event_queue(__attribute__((unused)) void* data)
*/
static void append_event(const struct event* event)
{
struct event_entry* entry;
struct event_queue* entry;

// create new entry
entry = malloc(sizeof(*entry));
if (!entry) {
return;
}
memcpy(&entry->event, event, sizeof(entry->event));
entry->next = NULL;

// add to queue tail
pthread_mutex_lock(&ctx.events_lock);
if (ctx.events) {
struct event_entry* last = ctx.events;
while (last->next) {
last = last->next;
}
last->next = entry;
} else {
ctx.events = entry;
}
ctx.events = list_append(ctx.events, entry);
pthread_mutex_unlock(&ctx.events_lock);

notification_raise(ctx.event_signal);
Expand Down Expand Up @@ -394,13 +387,11 @@ void app_destroy(void)
}
free(ctx.wfds);

while (ctx.events) {
struct event_entry* entry = ctx.events;
ctx.events = ctx.events->next;
if (entry->event.type == event_load) {
image_free(entry->event.param.load.image);
list_for_each(ctx.events, struct event_queue, it) {
if (it->event.type == event_load) {
image_free(it->event.param.load.image);
}
free(entry);
free(it);
}
if (ctx.event_signal != -1) {
notification_free(ctx.event_signal);
Expand Down Expand Up @@ -608,24 +599,17 @@ void app_redraw(void)
const struct event event = {
.type = event_redraw,
};
struct event_entry* prev = NULL;
struct event_entry* it = ctx.events;

// remove the same event to append the new one to tail
while (it) {
struct event_entry* next = it->next;
// remove the same event to append new one to tail
pthread_mutex_lock(&ctx.events_lock);
list_for_each(ctx.events, struct event_queue, it) {
if (it->event.type == event_redraw) {
if (prev) {
prev->next = next;
} else {
ctx.events = next;
}
ctx.events = list_remove(it);
free(it);
break;
}
prev = it;
it = next;
}
pthread_mutex_unlock(&ctx.events_lock);

append_event(&event);
}
Expand Down Expand Up @@ -661,17 +645,18 @@ void app_on_drag(int dx, int dy)
const struct event event = { .type = event_drag,
.param.drag.dx = dx,
.param.drag.dy = dy };
struct event_entry* it = ctx.events;

// merge with existing event
while (it) {
pthread_mutex_lock(&ctx.events_lock);
list_for_each(ctx.events, struct event_queue, it) {
if (it->event.type == event_drag) {
it->event.param.drag.dx += dx;
it->event.param.drag.dy += dy;
pthread_mutex_unlock(&ctx.events_lock);
return;
}
it = it->next;
}
pthread_mutex_unlock(&ctx.events_lock);

append_event(&event);
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "config.h"

#include "str.h"
#include "memdata.h"

#include <ctype.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "font.h"

#include "config.h"
#include "str.h"
#include "memdata.h"

// font realted
#include <fontconfig/fontconfig.h>
Expand Down
45 changes: 15 additions & 30 deletions src/gallery.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "gallery.h"

#include "application.h"
#include "config.h"
#include "imagelist.h"
#include "info.h"
#include "loader.h"
#include "str.h"
#include "ui.h"

#include <stdlib.h>
Expand All @@ -19,9 +19,9 @@

/** List of thumbnails. */
struct thumbnail {
struct image* image; ///< Preview image
size_t width, height; ///< Real image size
struct thumbnail* next; ///< Next entry
struct list list; ///< Links to prev/next entry
struct image* image; ///< Preview image
size_t width, height; ///< Real image size
};

/** Gallery context. */
Expand All @@ -48,12 +48,11 @@ static struct gallery ctx;
*/
static void reset_thumbnails(void)
{
while (ctx.thumbs) {
struct thumbnail* entry = ctx.thumbs;
ctx.thumbs = ctx.thumbs->next;
image_free(entry->image);
free(entry);
list_for_each(ctx.thumbs, struct thumbnail, it) {
image_free(it->image);
free(it);
}
ctx.thumbs = NULL;
app_redraw();
}

Expand All @@ -64,17 +63,14 @@ static void reset_thumbnails(void)
static void add_thumbnail(struct image* image)
{
struct thumbnail* entry = malloc(sizeof(*entry));
if (entry) {
if (!entry) {
image_free(image);
} else {
entry->width = image->frames[0].pm.width;
entry->height = image->frames[0].pm.height;
entry->image = image;

// create thumbnail from image
image_thumbnail(image, ctx.thumb_size, ctx.thumb_aa);

// add to the list
entry->next = ctx.thumbs;
ctx.thumbs = entry;
ctx.thumbs = list_append(ctx.thumbs, entry);
}
}

Expand All @@ -85,12 +81,10 @@ static void add_thumbnail(struct image* image)
*/
static struct thumbnail* get_thumbnail(size_t index)
{
struct thumbnail* it = ctx.thumbs;
while (it) {
list_for_each(ctx.thumbs, struct thumbnail, it) {
if (it->image->index == index) {
return it;
}
it = it->next;
}
return NULL;
}
Expand All @@ -101,21 +95,12 @@ static struct thumbnail* get_thumbnail(size_t index)
*/
static void remove_thumbnail(size_t index)
{
struct thumbnail* it = ctx.thumbs;
struct thumbnail* prev = NULL;
while (it) {
list_for_each(ctx.thumbs, struct thumbnail, it) {
if (it->image->index == index) {
if (prev) {
prev->next = it->next;
} else {
ctx.thumbs = it->next;
}
ctx.thumbs = list_remove(it);
image_free(it->image);
free(it);
return;
}
prev = it;
it = it->next;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/imagelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "config.h"
#include "loader.h"
#include "str.h"
#include "memdata.h"
#include "ui.h"
#include "viewer.h"

Expand Down
22 changes: 7 additions & 15 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "imagelist.h"
#include "keybind.h"
#include "loader.h"
#include "str.h"
#include "ui.h"

#include <stdarg.h>
Expand Down Expand Up @@ -575,7 +574,6 @@ void info_switch(const char* mode)

void info_switch_help(void)
{

if (ctx.help) {
for (size_t i = 0; i < ctx.help_num; i++) {
free(ctx.help[i].data);
Expand All @@ -584,33 +582,27 @@ void info_switch_help(void)
ctx.help = NULL;
ctx.help_num = 0;
} else {
const struct keybind* kb = keybind_get();
size_t num = 0;

// get number of bindings
while (kb) {
if (kb->help) {
size_t num = 0;
list_for_each(keybind_get(), struct keybind, it) {
if (it->help) {
++num;
}
kb = kb->next;
}
if (num == 0) {
return;
}

// create help layer
// create help layer in reverse order
ctx.help = calloc(1, num * sizeof(*ctx.help));
if (!ctx.help) {
return;
}
ctx.help_num = num;
// fill keybinds help (they are stored in reverse order)
kb = keybind_get();
while (kb) {
if (kb->help) {
font_render(kb->help, &ctx.help[--num]);
list_for_each(keybind_get(), struct keybind, it) {
if (it->help) {
font_render(it->help, &ctx.help[--num]);
}
kb = kb->next;
}
}
}
Expand Down
Loading

0 comments on commit 3dde4de

Please sign in to comment.