Personal

Show Only ...
Maps - Photos - Videos

GTK+ New Years Eve Countdown App

I’ve been educating myself about GTK+ programming lately. So I wrote this semi-short GTK app to countdown the time to New Years Day to learn more about GTK+, Cairo drawing, and Pango font library.

#include <gtk/gtk.h>
#include <time.h>
#include <glib/gprintf.h>

static char *
nye_countdown_str()
{
    struct tm nyd = {0}; /* inits struct members to 0 */
    nyd.tm_year = 2025 - 1900;
    nyd.tm_mday = 1;

    time_t nyd_time = mktime(&nyd);
    time_t diff = difftime(nyd_time, time(NULL));

    GString *buff = g_string_new("");
    if ((int)(diff / 86400))
        g_string_append_printf(buff, " %d %s\n", (int)(diff / 86400), "days");
    if ((int)((diff % 86400) / 3600))
        g_string_append_printf(buff, "%d %s\n", (int)(diff % 86400) / 3600, "hours");
    if ((int)(diff % 3600) / 60)
        g_string_append_printf(buff, "%d %s\n", (int)(diff % 3600) / 60, "minutes");
    if ((int)(diff % 60))
        g_string_append_printf(buff, "%d %s\n", (int)(diff % 60), "seconds");

    return buff->str;
}

static void
draw_countdown(GtkDrawingArea *area, cairo_t *cr, int width, int height, gpointer user_data)
{
    // make the ball drop one pixel every second through the top of minute
    time_t now = time(NULL);
    struct tm *currentTime = localtime(&now);

    cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
    cairo_paint(cr);

    cairo_set_source_rgb(cr, 0, 0, 0);
    cairo_rectangle(cr, width / 2 - 20, (width - 500) / 2 + 20, 40, height);
    cairo_fill(cr);

    float colorShift = currentTime->tm_sec / 60 + 0.5;
    if (colorShift > 1)
        colorShift = 0.85;

    cairo_set_source_rgb(cr, 1, 1, colorShift);

    cairo_arc(cr, width / 2, 80 + currentTime->tm_sec, 200 / 2, 0,
              2 * 3.14159);

    cairo_fill(cr);

    cairo_set_source_rgb(cr, 1, 1, 1);

    PangoLayout *pangLayout;
    PangoFontDescription *pangDesc;

    pangLayout = pango_cairo_create_layout(cr);
    pango_layout_set_text(pangLayout, nye_countdown_str(), -1);
    pangDesc = pango_font_description_from_string("American Typewriter 40");
    pango_layout_set_font_description(pangLayout, pangDesc);
    pango_font_description_free(pangDesc);

    pango_layout_set_alignment(pangLayout, PANGO_ALIGN_CENTER);

    PangoRectangle pLExtents = {0};
    pango_layout_get_extents(pangLayout, NULL, &pLExtents);
    pango_extents_to_pixels(NULL, &pLExtents);

    cairo_move_to(cr, (width / 2) - (pLExtents.width / 2), 300);
    pango_cairo_update_layout(cr, pangLayout);
    pango_cairo_show_layout(cr, pangLayout);

    pangLayout = pango_cairo_create_layout(cr);
    pango_layout_set_text(pangLayout, "2025", -1);
    pangDesc = pango_font_description_from_string("American Typewriter Bold 50");
    pango_layout_set_font_description(pangLayout, pangDesc);
    pango_font_description_free(pangDesc);

    pango_layout_set_alignment(pangLayout, PANGO_ALIGN_CENTER);
    pango_layout_get_extents(pangLayout, NULL, &pLExtents);
    pango_extents_to_pixels(NULL, &pLExtents);

    cairo_move_to(cr, 0, 120);
    cairo_rotate(cr, -45 * (M_PI / 180));
 
    pango_cairo_update_layout(cr, pangLayout);
    pango_cairo_show_layout(cr, pangLayout);
}

static gboolean
time_handler(GtkWidget *widget)
{
    gtk_widget_queue_draw(widget);

    return TRUE;
}

static void
app_activate(GApplication *app, gpointer user_data)
{
    GtkWidget *win, *countdown, *box, *creator_labl;
    int curWindowHeight;

    win = gtk_application_window_new(GTK_APPLICATION(app));
    gtk_window_set_title(GTK_WINDOW(win), "Countdown to 2025");
    gtk_window_set_default_size(GTK_WINDOW(win), 600, 600);

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_window_set_child(GTK_WINDOW(win), box);

    countdown = gtk_drawing_area_new();
    gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(countdown), draw_countdown, NULL, NULL);

    gtk_window_get_default_size(GTK_WINDOW(win), NULL, &curWindowHeight);

    gtk_drawing_area_set_content_height(GTK_DRAWING_AREA(countdown), hgcurWindowHeight);
    gtk_box_append(GTK_BOX(box), countdown);

    creator_labl = gtk_label_new("Created by Andy Arthur, December 29, 2024");
    gtk_box_append(GTK_BOX(box), creator_labl);

    g_timeout_add(1000,  (GSourceFunc)time_handler, (gpointer)countdown);
    gtk_window_present(GTK_WINDOW(win));
}

static void
app_open(GApplication *app, GFile **files, gint n_files, gchar *hint, gpointer user_data)
{
    app_activate(app, user_data);
}

int main(int argc, char **argv)
{
    GtkApplication *app;
    int stat;

    app = gtk_application_new("org.andyarthur.nye", G_APPLICATION_HANDLES_OPEN);
    g_signal_connect(app, "activate", G_CALLBACK(app_activate), NULL);
    g_signal_connect(app, "open", G_CALLBACK(app_open), NULL);
    stat = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return stat;
}

How I Closed Out 2024 … With the COVID ! 😷

Give me that holiday cheer! A good ol’ “FISH CHEER“… It’s not ducking COVID until the test says it was. I can’t believe I got the COVID as much as denial I was about it until I finally googled my symptoms, and they aren’t off a cold, and as soon as I dusted off that 2-year old expired COVID test, rammed that swab up my nose, and barely had time enough to it stuck in the test kit, and both lines were as clear as day.

I guess at some point I was going to have dumb luck of getting the COVID after five years of avoiding it. πŸ‘Ύ At this point, it’s really not considered that much of menace to society, similar to the common cold and flu. That doesn’t mean it won’t make you ducking miserable for days, even if you have been religious about getting the latest and greatest jabs from the pharmacy. πŸ’‰ Heck, for most people these days they don’t recommend anything beyond staying home, resting up and not returning to work until 24 hours after the last of the symptoms.

Truth is I am feeling a lot better, πŸ˜ƒ though I have moments of up and down. My energy levels are at 50-80% what they were pre-COVID and I still have a bit of a sore throat. My nose is still congested, don’t have as much dizziness and my hearing is coming back. πŸ‘‚ But I also feel so euphoric at times, after being so sick. Sleeping basically for 36 hours straight from Christmas Eve until essentially that Friday after Christmas, πŸŽ… bar maybe glancing at my phone and running downstairs to grab an orange 🍊 — the only thing I could eat as I was puking up bananas. And even Friday, when I in my stubbornness I was still pretty sick even though I ended up having to go to laundromat and shopping. The weekend was more mixed, both days I really didn’t have the wherewithal to go anywhere, though today I finally got to car wash and got my pantry restocked — that is wearing the muzzle. πŸ›’ Friday I had some gotten some groceries, but I was hardly well stocked. Then I went for my evening walk. 🚢 Damn nice day outside, but I have COVID.

I mean the COVID-test could wrong. πŸ§ͺ But the exhaustion, sore throat, zero energy, and just staying indoors for so long without going anywhere is totally not me — it really points to COVID. It was far worse then any cold I have had in the past, even those sinus infections I had when mold was bad in my apartment, though I still kind of feel shitty at times. πŸ’© I get these times when I’m feeling I can run a mile, and then I just collapse. I don’t think I’ll have long COVID or brain fog, though I’m worried about getting my hearing back. πŸ‘‚ Did I mention I discovered today, I have no sense of smell πŸ‘ƒ and no taste either? Funny thing about it was I didn’t notice that until I did the COVID test today and it came back positive. But what would there to be to smell, I mean it wasn’t like I had enough energy to go outside with the cold 😀 and food for past few days has been more about filling that emptiness in my stomach then enjoying rich tastes. Plus I don’t eat processed foods, and maybe I’m not the greatest chef πŸ‘¨β€πŸ³ though I do try to always it healthy.

Knowing I have COVID at least I feel better knowing it’s not a cold that lingering round. 🩺 Mom was suggesting I go to doctor if I didn’t recover soon, as colds don’t usually last this long, and when they do they are bacterial and not a virus. The thing is I didn’t think it was bacterial as I didn’t have much soreness, my throat was raw like smoking too much pot around a smokey campfire, but never sore. The diziness was the big thing. Symptoms are changing, I’ve had more achiness and tightness in the throat now, but at least I’m getting my hearing back and I’m not as dizzy and tired as I was. It’s just been a wild ride. 🎒

It’s hard to believe that it was five years ago now, when the first news headlines would be coming out about the COVID πŸ‘Ύ and we would be only 45 days away from the lockdown, when everything would close down, at all work would be remote. πŸ’» Such a weird time — I’ve never had internet at home! Still don’t. I worked from the libary parking lot in my pickup. The whole was kind of a weird time, people didn’t know anything about the virus, though it was very dangerous as there was no vaccine available at the time, the treatment regiments weren’t well established, and there was so many unknowns. Five years later, it’s little more then the cold or the flu, especially for the vaccinated. I conceded I probably shouldn’t have gone out on Friday, but I didn’t know I had COVID. πŸ‘Ή Truth is though I needed clean clothes and groceries.

It’s impossible to know if all those shots were worth it, πŸ’‰ as just like seatbelts or safety features in an automobile, or the cop that drives by on his beat, you can’t prove causation at an individual level. Sure the COVID-vaccine clearly has saved lives, but did it keep me from getting even sicker? There are plenty of people who have cars with airbags and wear seatbelts πŸ’Ί yet are killed in car crashes. Plenty of people get mugged even with the cops. Would anyone person be safer without them? Truth is though, I will get over COVID, and it’s much better to the Wu Flu five years after the start of the pandemic.

Slowly I think I’m feeling better 😷

I would rather not bring a cold that I got before Christmas that had me laid in bed for several days into the New Year. But it’s been a struggle, I have not been outside or anywhere really beyond my bedroom and bath since except for that one brief trip to laundromat and the supermarket when I felt like I was dying on Friday.

Truth is I think I’m getting over the cold. πŸ‘ƒ While nobody wants to cough or have a runny nose, I think that’s progress. Never had a fever but my sore throat has pretty much gone away, it’s just the fluid in the ear canals that makes things hard to hear and a bit dizzy at times. No fever, πŸ€’ and I don’t feel like I have strep, and I didn’t do a COVID test, as I didn’t think that would change anything. Maybe it was finally COVID. Actually, I could see it being that, though I usually didn’t think of COVID as being a nose thing, and without a fever, but who knows. I could have done a COVID test, and maybe even gone to the doctor’s office on Thursday or Friday to beg for COVID medicine, but whatever. I just had how everybody blames it on the COVID or the flu, when most of the time it’s just an ordinary cold.

It’s been good, in the sense it’s allowed me a lot of time to read and study, πŸ“š and I’ve really started to get a good grip on real computer programming, especially things like C libraries, linkers, NCurses library and GTK+/Pango/Cairo libraries for GUI software writing. Also been doing a lot of studying of bash shell scripting, which will help me out with work. I’ve gotten to the point where I can fairly easily just look at a header file and sometimes implementing code in either C or C++ and figure out what it actually does. πŸ’» And the things I’m learning in the

Still I am kind of sad I missed Christmas with the family, πŸŽ„and I didn’t get to do any trips. Maybe Tuesday if I feel better I’ll do a hike somewhere, or at least get out for a ride. Tomorrow, I’m hoping to at least get to the store and maybe down to the library. πŸͺ Maybe if it’s decent do some riding. πŸš΄β€β™‚οΈ Second Christmas in the row I missed out with my family, last year it was Mom that was sick. Sucks, you don’t know how many more Christmas are left with the family, they’re getting up there in years. πŸŽ… That said, I’m no big lover of the whole holiday season.