aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--comet.c8
-rw-r--r--draw.c57
-rw-r--r--draw.h20
3 files changed, 55 insertions, 30 deletions
diff --git a/comet.c b/comet.c
index 825dff0..4707337 100644
--- a/comet.c
+++ b/comet.c
@@ -17,7 +17,7 @@ static gboolean mainloop_quit(gpointer data)
static gboolean mainloop_do(gpointer data)
{
- draw(data);
+ draw_paint(data);
return G_SOURCE_REMOVE;
}
@@ -40,12 +40,12 @@ int main(int argc, char **argv)
log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding);
- draw_init("Hack 12", height, x_padding, y_padding);
+ Drawable *draw = draw_create(win, "Hack 12", height, x_padding, x_padding, y_padding, 0.99);
guint source_term = g_unix_signal_add(SIGTERM, mainloop_quit, mainloop);
guint source_int = g_unix_signal_add(SIGINT, mainloop_quit, mainloop);
- guint id = g_timeout_add(100, mainloop_do, win);
+ guint id = g_timeout_add(100, mainloop_do, draw);
log_debug("Starting main loop");
g_main_loop_run(mainloop);
@@ -56,7 +56,9 @@ int main(int argc, char **argv)
g_source_remove(source_term);
g_source_remove(source_int);
+ draw_destroy(draw);
window_destroy(win);
+
return 0;
}
diff --git a/draw.c b/draw.c
index 1dfa773..b0c32ba 100644
--- a/draw.c
+++ b/draw.c
@@ -1,3 +1,4 @@
+#include <glib.h>
#include <math.h>
#include <pango/pangocairo.h>
#include <pango/pango-font.h>
@@ -9,26 +10,29 @@
// Idea: Either make a to_draw queue where we put things we schedule to redraw
// (this will also work for animations in the future)
// or use some flags to trigger drawing
-//
-// Anyway, a drawing context struct is required
-// Make this a state passed to draw
-int draw_height, draw_x_padding, draw_y_padding;
-PangoFontDescription *desc;
-
-void draw_init(const char *font, int height, int x_padding, int y_padding)
+Drawable *draw_create(Window *win, const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha)
{
+ Drawable *draw = g_malloc(sizeof(Drawable));
+ g_assert_nonnull(draw);
+
log_debug("Pango loading font description '%s'", font);
- desc = pango_font_description_from_string(font);
- log_debug("Pango found matching font '%s'", pango_font_description_get_family(desc));
+ draw->desc = pango_font_description_from_string(font);
+ log_debug("Pango found matching font '%s'", pango_font_description_get_family(draw->desc));
- draw_height = height;
- draw_x_padding = x_padding;
- draw_y_padding = y_padding;
+ draw->win = win;
+ draw->height = height;
+ draw->left_pad = left_pad;
+ draw->right_pad = right_pad;
+ draw->top_pad = top_pad;
+ draw->alpha = alpha;
+
+ return draw;
}
-void draw(Window *win)
+void draw_paint(Drawable *draw)
{
+ Window *win = draw->win;
// FIXME: Does not work for scale != 1
//double scale = window_get_scale(win);
double scale = 1;
@@ -36,9 +40,9 @@ void draw(Window *win)
int screen_width, screen_height;
window_get_screen_size(win, &screen_width, &screen_height);
- int width0 = screen_width - 2 * draw_x_padding;
+ int width0 = screen_width - draw->right_pad - draw->left_pad;
int width = round(width0 * scale);
- int height = round(draw_height * scale);
+ int height = round(draw->height * scale);
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
@@ -49,10 +53,7 @@ void draw(Window *win)
int radius = height / 2;
double degree = M_PI / 180.0;
- // FIXME: Alpha transparency is not working
- double alpha = 0.8;
-
- cairo_set_source_rgba(cr, 0.3, 0.3, 0.3, alpha);
+ cairo_set_source_rgba(cr, 0.3, 0.3, 0.3, draw->alpha);
// TODO: Here we should paint the shape of the bar, however there is a problem with the surface
// painted in the pixmap to mask the window shape in window_paint_corners.
@@ -71,16 +72,16 @@ void draw(Window *win)
for (int i = 0; i < 9; i++) {
PangoLayout *layout = pango_cairo_create_layout (cr);
- pango_layout_set_font_description(layout, desc);
+ pango_layout_set_font_description(layout, draw->desc);
int x = (height + cairo_get_line_width(cr) * scale * 2) * i;
// purple
- cairo_set_source_rgba(cr, 0.502, 0.168, 0.886, alpha);
+ cairo_set_source_rgba(cr, 0.502, 0.168, 0.886, 1);
cairo_arc(cr, x + radius, radius, radius, 0 * degree, 360 * degree);
cairo_fill(cr);
- cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, alpha);
+ cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
cairo_arc(cr, x + radius, radius, radius - 1, 0 * degree, 360 * degree);
cairo_stroke(cr);
@@ -105,15 +106,21 @@ void draw(Window *win)
cairo_destroy(cr);
- int x = draw_x_padding;
- int y = draw_y_padding;
+ int x = draw->left_pad;
+ int y = draw->top_pad;
// TODO: Move these somewhere else
window_move(win, x, y);
- window_resize(win, width0, draw_height);
+ window_resize(win, width0, draw->height);
window_paint_surface(win, surface, width, height);
cairo_surface_destroy(surface);
}
+void draw_destroy(Drawable *draw)
+{
+ pango_font_description_free(draw->desc);
+ g_free(draw);
+}
+
// vim: ts=4 sw=4 et
diff --git a/draw.h b/draw.h
index e352ad5..1d299ce 100644
--- a/draw.h
+++ b/draw.h
@@ -1,11 +1,27 @@
#ifndef COMET_DRAW_H
#define COMET_DRAW_H
+#include <pango/pango-font.h>
+#include <pango/pango-types.h>
+
#include "window.h"
-void draw_init(const char *font, int height, int x_padding, int y_padding);
+// TODO: Make opaque
+typedef struct {
+ Window *win;
+ PangoFontDescription *desc;
+ int height;
+ int left_pad;
+ int right_pad;
+ int top_pad;
+ double alpha;
+} Drawable;
+
+Drawable *draw_create(Window *win, const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha);
+
+void draw_paint(Drawable *draw);
-void draw(Window *win);
+void draw_destroy(Drawable *draw);
#endif