d43fbcc0a751f68982170150462504028f98a103 — Gregory Mullen 1 year, 1 month ago 426c076
Fix realloc bug
13 files changed, 172 insertions(+), 77 deletions(-)

M Makefile
M audio.c
M audio.h
M audio_search.c
M audio_search.h
R gui/{gps.c => gps_gui.c}
R gui/{gps.h => gps_gui.h}
M gui/music_tracks.c
M gui/onscreenkeys.h
M gui/root.c
M hud.c
M wl/draw.c
M wl/ui.c
M Makefile => Makefile +19 -11
@@ 1,10 1,11 @@
 CC = arm-linux-gnueabihf-gcc
 LD = arm-linux-gnueabihf-ld
-
 # CC = arm-linux-gnueabi-gcc
 # LD = arm-linux-gnueabi-ld
 # CC = arm-linux-musleabi-gcc
 # LD = arm-linux-mulleabi-ld
+# CC = clang
+# LD = clang
 
 OBJ = hud.o
 OBJ += wayland.o wl/info.o wl/keyboard.o wl/touch.o wl/draw.o wl/ui.o wl/text.o wl/ivi.o wl/seat.o


@@ 13,27 14,34 @@ OBJ += audio.o audio_search.o
 
 
 OBJ += gui/root.o gui/nav.o gui/notifier.o gui/onscreenkeys.o
-OBJ += gui/gps.o
+OBJ += gui/gps_gui.o
 OBJ += gui/music.o gui/music_tracks.o gui/music_buttons.o
 
 
+
 CFLAGS := -Wall -Wextra -Werror $(CFLAGS)
+CFLAGS += -std=c11
+CFLAGS += -O0
 CFLAGS += -Wno-sign-compare
+CFLAGS += -Wno-deprecated-declarations  # Needed for legacy version of ffmpeg
+LDFLAGS =
 
-CFLAGS += -Wno-deprecated-declarations
 
-CFLAGS += -std=c11
-CFLAGS += -O0
+ifeq (${CC}, clang)
+	CFLAGS += --target=arm-linux-gnueabi
+	CFLAGS += --sysroot=/usr/arm-linux-gnueabi
+else
+	CFLAGS += -mcpu=arm710
+endif
 
-CFLAGS += -mcpu=arm710
 CFLAGS += -mtune=arm710
 CFLAGS += -mfpu=vfpv3
 CFLAGS += -mfloat-abi=soft
 
-CFLAGS += -isystem=/include/freetype2
+CFLAGS += -isystem/usr/arm-linux-gnueabi/include/freetype2
 
-LDFLAGS = -Wl,-rpath-link=/home/grayhatter/mazda/libs/lib/
-LDFLAGS = -Wl,-rpath-link=/home/grayhatter/mazda/libs/usr/lib/
+LDFLAGS += -Wl,-rpath-link=/home/grayhatter/mazda/libs/lib
+LDFLAGS += -Wl,-rpath-link=/home/grayhatter/mazda/libs/usr/lib
 
 LIBS   +=  -lfreetype -lz
 LIBS   +=  -lwayland-ivi-shell-client -lwayland-ivi-client


@@ 43,8 51,8 @@ LIBS   +=  -lgps
 LIBS   +=  -lffi -ldl -lrt -lpthread -lm
 
 %.o: %.c %.h
-	@# $(CC) -v -g -c -o $@ $(CFLAGS) $<
-	@$(CC) -c -o $@ $(CFLAGS) $<
+	@# $(CC) -v -g -c $(CFLAGS) $< -o $@
+	@$(CC) -c  $(CFLAGS) $< -o $@
 
 
 hudtds: $(OBJ)

M audio.c => audio.c +86 -21
@@ 10,10 10,6 @@
 #include <string.h>
 #include <time.h>
 
-#define u_int8_t uint8_t
-#define u_int16_t uint16_t
-#define u_int32_t uint32_t
-#define u_int64_t uint64_t
 #include <alsa/asoundlib.h>
 
 #include <libavutil/common.h>


@@ 148,7 144,6 @@ static AVFormatContext *open_audio_track(char *filename, bool debug, int *stream
 
 static AVCodecContext *init_codec(AVCodecContext *ctx, AVDictionary **metadata)
 {
-
     LOG_D("getting codec\n");
     AVCodec *codec = avcodec_find_decoder(ctx->codec_id);
     // AVCodec *codec = avcodec_find_decoder(CODEC_ID_MP3);


@@ 294,6 289,7 @@ static void *playback_thread(void *d)
     return NULL;
 }
 
+
 static void *audio_thread(void *p)
 {
     (void) p;


@@ 410,14 406,85 @@ static void *audio_thread(void *p)
 }
 
 
+static struct artist_db *artist_db = NULL;
+
+
+static int push_artist(const char *name)
+{
+    // TODO check for unused locations so array can shrink with deletions
+
+    if (!name) {
+        return -2;
+    }
+
+    int name_length = strlen(name);
+    if (name_length == 0) {
+        LOG_E("artist push Name length shouldn't be 0\n");
+        return -2;
+    }
+
+    for (int i = 0; i < artist_db->count; i++) {
+        if (artist_db->data[i].length != name_length) {
+            continue;
+        }
+
+        if (strcmp(name, artist_db->data[i].name) == 0) {
+            return i;
+        }
+    }
+
+    if (artist_db->count >= artist_db->capacity) {
+        struct artist_data *next = realloc(artist_db->data, sizeof(struct artist_data) * (artist_db->capacity + 10));
+        if (!next) {
+            LOG_F("Realloc failed for push artist %s", name);
+            exit(2);
+        }
+        artist_db->data = next;
+        artist_db->capacity += 10;
+    }
+
+    artist_db->data[artist_db->count].length = name_length;
+    artist_db->data[artist_db->count].name = strdup(name);
+    if (!artist_db->data[artist_db->count].name) {
+        LOG_E("Strdup failed in push_artist\n");
+        exit(2);
+    }
+
+    artist_db->count++;
+    return artist_db->count - 1;
+
+    return -1;
+}
+
+
+const char *track_artist_get(const int id)
+{
+    if (id < 0 || id >= artist_db->count) {
+        return NULL;
+    }
+
+    return artist_db->data[id].name;
+}
+
+
 int audio_track_add_metadata(struct audio_track *track)
 {
-    LOG_T("Trying to add metadata for track\n");
+    LOG_T("Trying to add metadata for track ");
+    if (!track) {
+        LOG_W("Track give was null\n");
+        return 0;
+    }
 
     if (!track->dirname || !track->filename) {
         return -1;
     }
+    LOG_T("dirname %s, filename %s\n", track->dirname, track->filename);
+
     char *dirname  = expand_dirname(track->dirname, track->filename);
+    if (!dirname) {
+        LOG_F("Unable to expand dirname for track %s\n", track->filename);
+        exit(2);
+    }
 
     AVFormatContext *file = avformat_alloc_context();
     if (avformat_open_input(&file, dirname, NULL, NULL) < 0) {


@@ 428,18 495,19 @@ int audio_track_add_metadata(struct audio_track *track)
 
     AVDictionaryEntry *dict = NULL;
     if ((dict = av_dict_get(file->metadata, "album_artist", NULL, 0))) {
-        track->md_album_artist = strdup(dict->value);
+        track->album_artist_id = push_artist(dict->value);
     } else if ((dict = av_dict_get(file->metadata, "artist", NULL, 0))) {
-        track->md_album_artist = strdup(dict->value);
+        track->album_artist_id = push_artist(dict->value);
     }
 
     if ((dict = av_dict_get(file->metadata, "artist", NULL, 0))) {
-        track->md_artist = strdup(dict->value);
+        track->artist_id = push_artist(dict->value);
     } else {
-        track->md_artist = track->md_album_artist;
+        track->artist_id = track->album_artist_id;
     }
 
     if ((dict = av_dict_get(file->metadata, "title", NULL, 0))) {
+        LOG_E("%s title %s\n", track->filename, dict->value);
         track->md_title = strdup(dict->value);
     }
 


@@ 455,23 523,14 @@ int audio_track_add_metadata(struct audio_track *track)
 
     avformat_close_input(&file);
     avformat_free_context(file);
+    free(dirname);
+    LOG_T("metadata complete\n");
     return 0;
 }
 
 
 void audio_track_free_metadata(struct audio_track *track)
 {
-    if (track->md_album_artist) {
-        if (track->md_album_artist == track->md_artist) {
-            track->md_artist = NULL;
-        } else if (track->md_artist) {
-            free(track->md_artist);
-            track->md_artist = NULL;
-        }
-        free(track->md_album_artist);
-        track->md_album_artist = NULL;
-    }
-
     if (track->md_title) {
         free(track->md_title);
         track->md_title = NULL;


@@ 516,6 575,12 @@ static void audio_init(void)
 
     LOG_T("init pcm\n");
     init_pcm();
+
+    artist_db = calloc(1, sizeof (struct artist_db));
+    if (!artist_db) {
+        LOG_F("artist_db Calloc failed\n");
+        exit(2);
+    }
 }
 
 

M audio.h => audio.h +17 -2
@@ 25,14 25,26 @@ struct audio_track {
     char *filename;
     char *dirname;
 
-    char *md_album_artist;
-    char *md_artist;
+    int artist_id;
+    int album_artist_id;
     char *md_title;
     char *md_album;
     char *md_genre;
 };
 
 
+struct artist_data {
+    int length;
+    char *name;
+};
+
+struct artist_db {
+    int count;
+    int capacity;
+    struct artist_data *data;
+};
+
+
 void postmsg_audio(AUDIO_MSG msg, void *data);
 
 void audio_thread_start(void);


@@ 40,6 52,9 @@ void audio_thread_start(void);
 struct audio_track *audio_track_get_current(void);
 struct music_db *audio_db_get(void);
 
+const char *track_artist_get(const int id);
+
+
 int audio_track_add_metadata(struct audio_track *track);
 void audio_track_free_metadata(struct audio_track *track);
 

M audio_search.c => audio_search.c +14 -10
@@ 102,13 102,15 @@ static struct music_dir *search_dir(const char *dirname)
                             return NULL;
                         }
                     } else {
-                        struct audio_track *next = realloc(thisdir->tracks, sizeof (struct audio_track) * (thisdir->track_count + 1));
+                        struct audio_track *next = realloc(thisdir->tracks,
+                            sizeof (struct audio_track) * (thisdir->track_count + 1));
                         if (!next) {
                             free(filename);
                             return NULL;
                         }
                         thisdir->tracks = next;
                     }
+                    memset(&thisdir->tracks[thisdir->track_count], 0, sizeof (struct audio_track));
                     thisdir->tracks[thisdir->track_count].filename = strdup(entry->d_name);
                     thisdir->tracks[thisdir->track_count++].dirname = strdup(dirname);
                     thisdir->total_track_count++;


@@ 119,7 121,8 @@ static struct music_dir *search_dir(const char *dirname)
                 if (child) {
                     thisdir = ensure_dir(thisdir);
                     if (child->dir_count || child->track_count) {
-                        struct music_dir *next = realloc(thisdir->subdirs, sizeof (struct music_dir) * (thisdir->dir_count + 1));
+                        struct music_dir *next = realloc(thisdir->subdirs,
+                            sizeof (struct music_dir) * (thisdir->dir_count + 1));
                         if (!next) {
                             free(filename);
                             return NULL;


@@ 183,19 186,20 @@ void *find_files_thread(void *db_)
     LOG_D("subdir count %i\n", db->dirs[0].dir_count);
     LOG_D("subdir track count %i\n", db->dirs[0].track_count);
     LOG_D("subdir track 0 name %s\n", db->dirs[0].tracks[0].filename);
-    LOG_D("subdir track 0 location %s\n", expand_dirname(db->dirs[0].dirname, db->dirs[0].tracks[0].filename));
+    // LOG_D("subdir track 0 location %s\n", expand_dirname(db->dirs[0].dirname, db->dirs[0].tracks[0].filename));
 
 
     db->search_done = true;
     postmsg_audio(AMSG_TRACK_SCAN_DONE, db);
 
-    struct audio_track *track;
-    uint32_t pos = 0;
-    while ((track = find_track(pos++, db->dirs))) {
-        audio_track_add_metadata(track);
-        struct timespec __ts_nanosleep = { .tv_nsec = _10_mSECS };
-        nanosleep(&__ts_nanosleep, NULL);
-    }
+    // struct timespec __ts_nanosleep = { .tv_nsec = _10_mSECS };
+    // struct audio_track *track;
+    // uint32_t pos = 0;
+
+    // while ((track = find_track(pos++, db->dirs))) {
+    //     audio_track_add_metadata(track);
+    //     nanosleep(&__ts_nanosleep, NULL);
+    // }
     return NULL;
 }
 

M audio_search.h => audio_search.h +3 -3
@@ 1,5 1,5 @@
-#ifndef _HUD_AUDIO_SEARCH_H_
-#define _HUD_AUDIO_SEARCH_H_
+#ifndef _HUDTDS_AUDIO_SEARCH_H_
+#define _HUDTDS_AUDIO_SEARCH_H_
 
 #include <stdint.h>
 #include <stdbool.h>


@@ 40,4 40,4 @@ void *find_files_thread(void *db);
 struct audio_track *find_track(uint32_t pos, struct music_dir *dir);
 
 
-#endif // _HUD_AUDIO_SEARCH_H_
+#endif // _HUDTDS_AUDIO_SEARCH_H_

R gui/gps.c => gui/gps_gui.c +7 -7
@@ 1,4 1,4 @@
-#include "gps.h"
+#include "gps_gui.h"
 
 #include "../wl/ui.h"
 #include "../wl/text.h"


@@ 45,16 45,16 @@ static void draw_gps_data(struct ui_panel *p, int32_t x, int32_t y, int32_t w, i
     text_draw_string(str, x + 2, y + 30);
 
     snprintf(str, sizeof str, "ALT: %lf", data->alt);
-    text_draw_string(str, x + 2, y + 40);
+    text_draw_string(str, x + 2, y + 50);
 
-    snprintf(str, sizeof str, "Ground Speed: %lf", data->gnd_speed);
-    text_draw_string(str, x + 2, y + 80);
+    snprintf(str, sizeof str, "Ground Speed: %.2lf", data->gnd_speed);
+    text_draw_string(str, x + 2, y + 90);
 
-    snprintf(str, sizeof str, "Climb Speed: %lf", data->vert_speed);
-    text_draw_string(str, x + 2, y + 100);
+    snprintf(str, sizeof str, "Climb Speed: %.2lf", data->vert_speed);
+    text_draw_string(str, x + 2, y + 120);
 
     snprintf(str, sizeof str, "Satellites: %i (%i)", data->satellites_visible, data->satellites_used);
-    text_draw_string(str, x + 2, y + 130);
+    text_draw_string(str, x + 2, y + 150);
 
     free(data);
 }

R gui/gps.h => gui/gps_gui.h +0 -1
@@ 3,5 3,4 @@
 
 extern struct ui_panel gps_frame;
 
-
 #endif // _HUDTDS_GUI_GPS_

M gui/music_tracks.c => gui/music_tracks.c +6 -4
@@ 45,14 45,16 @@ static const char *entry_text(struct audio_track *t)
         audio_track_add_metadata(t);
     }
 
-    if (t->md_album_artist) {
-        pos += snprintf(str, 2048, "%s - ", t->md_album_artist);
-    } else if (t->md_artist) {
-        pos += snprintf(str, 2048, "%s - ", t->md_artist);
+    if (t->album_artist_id >= 0) {
+        pos += snprintf(str, 2048, "%s - ", track_artist_get(t->album_artist_id));
+    } else if (t->artist_id >= 0) {
+        pos += snprintf(str, 2048, "%s - ", track_artist_get(t->artist_id));
     }
 
     if (t->md_title) {
         pos += snprintf(str + pos, 2048 - pos, "%s", t->md_title);
+    } else {
+        pos += snprintf(str + pos, 2048 - pos, "%s", t->filename);
     }
 
     if (pos == 0) {

M gui/onscreenkeys.h => gui/onscreenkeys.h +3 -3
@@ 1,7 1,7 @@
-#ifndef _HUD_GUI_ONSCRKEYS_H_
-#define _HUD_GUI_ONSCRKEYS_H_
+#ifndef _HUDTDS_GUI_ONSCRKEYS_H_
+#define _HUDTDS_GUI_ONSCRKEYS_H_
 
 extern struct ui_panel onscreenkey_frame;
 
 
-#endif // _HUD_GUI_ONSCRKEYS_H_
+#endif // _HUDTDS_GUI_ONSCRKEYS_H_

M gui/root.c => gui/root.c +1 -1
@@ 3,7 3,7 @@
 #include "notifier.h"
 #include "music.h"
 #include "nav.h"
-#include "gps.h"
+#include "gps_gui.h"
 // #include "onscreenkeys.h"
 
 #include "../wl/keyboard.h"

M hud.c => hud.c +2 -2
@@ 35,8 35,8 @@ int main(void)
     init_wayland();
     LOG_D("wl init done\n");
 
-    // LOG_D("Starting Audio Thread\n");
-    // audio_thread_start();
+    LOG_D("Starting Audio Thread\n");
+    audio_thread_start();
 
     LOG_D("Starting GPS Thread\n");
     gps_thread_start();

M wl/draw.c => wl/draw.c +13 -12
@@ 92,7 92,7 @@ static inline uint32_t *get_pixel(int32_t x, int32_t y)
 }
 
 
-static uint8_t calculate_mix_8(uint8_t old, uint8_t new, float mix)
+static uint8_t calculate_mix_8(uint8_t old, uint8_t new, double mix)
 {
     if (old > new) {
         return old - ((old - new) * (1.0 - mix));


@@ 102,7 102,7 @@ static uint8_t calculate_mix_8(uint8_t old, uint8_t new, float mix)
 }
 
 
-static uint32_t calculate_mix_32(uint32_t old, uint32_t new, float mix)
+static uint32_t calculate_mix_32(uint32_t old, uint32_t new, double mix)
 {
     return 0xff000000
           | calculate_mix_8(old >> 16, new >> 16, mix) << 16


@@ 130,7 130,7 @@ bool draw_pixel(int32_t x, int32_t y, uint32_t c)
 }
 
 
-bool draw_pixel_mix(int32_t x, int32_t y, float mix, uint32_t c)
+bool draw_pixel_mix(int32_t x, int32_t y, double mix, uint32_t c)
 {
     if ((clip_box.set
             && (x < clip_box.x


@@ 249,12 249,12 @@ void draw_circle_c(int32_t x, int32_t y, int32_t w, uint32_t c)
 {
     LOG_D("draw circle %i %i %i \n", x, y, w);
 
-    float hw = (w / 2.0) - 0.5;
+    double hw = (w / 2.0) - 0.5;
     for (int i = 0; i < w; i++) {
         for (int j = 0; j < w; j++) {
-            float di = (i - hw);
-            float dj = (j - hw);
-            float mix = sqrtf(di * di + dj * dj) - hw + 0.5;
+            double di = (i - hw);
+            double dj = (j - hw);
+            double mix = sqrtf(di * di + dj * dj) - hw + 0.5;
             draw_pixel_mix(i + x, j + y, mix, c);
         }
     }


@@ 267,16 267,17 @@ void draw_circle(int32_t x, int32_t y, int32_t w)
 }
 
 
-void draw_circle_radius_c(int32_t x, int32_t y, int32_t r, uint32_t c)
+void draw_circle_radius_c(int32_t x, int32_t y, int32_t radius, uint32_t c)
 {
-    LOG_D("draw circle %i %i %i \n", x, y, r);
+    LOG_D("draw circle %i %i %i \n", x, y, radius);
+    double r = radius - 0.5;
     int32_t ax = x - r;
     int32_t ay = y - r;
     for (int i = 0; i < r * 2; i++) {
         for (int j = 0; j < r * 2; j++) {
-            float di = (i - r - 0.5);
-            float dj = (j - r - 0.5);
-            float mix = sqrtf(di * di + dj * dj) - r ;
+            double di = i - r;
+            double dj = j - r;
+            double mix = sqrtf(di * di + dj * dj) - r + 0.5;
             draw_pixel_mix(i + ax, j + ay, mix, c);
         }
     }

M wl/ui.c => wl/ui.c +1 -0
@@ 188,6 188,7 @@ bool ui_panel_draw(struct ui_panel *panel, int32_t x, int32_t y, int32_t w, int3
         }
     }
 
+    LOG_T("ui panel draw done %s\n", panel->name);
     return redraw;
 }