aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Magahern <james@magahern.com>2019-01-18 11:29:37 -0800
committerJames Magahern <james@magahern.com>2019-01-18 11:32:19 -0800
commit920f468363c58c9c5c335573e44d50371e76ddfe (patch)
treef4941fba1370f03be316c0b9a964ae768aaa256c
parentMove to meson build system. very cool (diff)
Split out to render.c
-rw-r--r--meson.build3
-rw-r--r--src/main.c143
-rw-r--r--src/render.c104
-rw-r--r--src/render.h40
4 files changed, 158 insertions, 132 deletions
diff --git a/meson.build b/meson.build
index 0f79608..023a066 100644
--- a/meson.build
+++ b/meson.build
@@ -1,7 +1,8 @@
project('buzzlocker', 'c')
sources = [
- 'src/main.c'
+ 'src/main.c',
+ 'src/render.c'
]
dependencies = [
diff --git a/src/main.c b/src/main.c
index 8f35feb..395e6ff 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,10 +4,8 @@
* Created 2019-01-16 by James Magahern <james@magahern.com>
*/
-#include <cairo/cairo.h>
-#include <cairo-xlib.h>
-#include <librsvg/rsvg.h>
-#include <pango/pangocairo.h>
+#include "render.h"
+
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -16,36 +14,17 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
-int __width, __height;
static Window __window = { 0 };
static Display *__display = NULL;
static const size_t kMaxPasswordLength = 128;
-static const double kLogoBackgroundWidth = 500.0;
-
-typedef struct {
- cairo_t *ctx;
- cairo_surface_t *surface;
-
- PangoLayout *pango_layout;
- PangoFontDescription *status_font;
-
- RsvgHandle *logo_svg_handle;
- RsvgHandle *asterisk_svg_handle;
-
- double cursor_opacity;
- double cursor_fade_direction;
-
- char *password_buffer;
- size_t password_buffer_len;
-} saver_state_t;
void window_changed_size(saver_state_t *state, XConfigureEvent *event)
{
- __width = event->width;
- __height = event->height;
+ state->canvas_width = event->width;
+ state->canvas_height = event->height;
- cairo_xlib_surface_set_size(state->surface, __width, __height);
+ cairo_xlib_surface_set_size(state->surface, event->width, event->height);
}
void handle_key_event(saver_state_t *state, XKeyEvent *event)
@@ -101,104 +80,6 @@ int poll_events(saver_state_t *state)
}
/*
- * Scene specific stuff
- */
-
-void draw_logo(saver_state_t *state)
-{
- if (state->logo_svg_handle == NULL) {
- GError *error = NULL;
- state->logo_svg_handle = rsvg_handle_new_from_file("logo.svg", &error);
- if (error != NULL) {
- fprintf(stderr, "Error loading logo SVG\n");
- return;
- }
- }
-
- cairo_t *cr = state->ctx;
-
- cairo_save(cr);
- cairo_set_source_rgb(cr, (208.0 / 255.0), (69.0 / 255.0), (255.0 / 255.0));
- cairo_rectangle(cr, 0, 0, kLogoBackgroundWidth, __height);
- cairo_fill(cr);
-
- cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
-
- // Scale and draw logo
- RsvgDimensionData dimensions;
- rsvg_handle_get_dimensions(state->logo_svg_handle, &dimensions);
-
- const double padding = 100.0;
- double scale_factor = ((kLogoBackgroundWidth - (padding * 2.0)) / dimensions.width);
- double scaled_height = (dimensions.height * scale_factor);
- double y_position = (__height - scaled_height) / 2.0;
- cairo_translate(cr, padding, y_position);
- cairo_scale(cr, scale_factor, scale_factor);
- rsvg_handle_render_cairo(state->logo_svg_handle, cr);
-
- cairo_restore(cr);
-}
-
-void draw_password_field(saver_state_t *state)
-{
- const double cursor_height = 40.0;
- const double cursor_width = 30.0;
- const double field_x = kLogoBackgroundWidth + 50.0;
- const double field_y = (__height - cursor_height) / 2.0;
- const double field_padding = 10.0;
-
- cairo_t *cr = state->ctx;
-
- // Draw status text
- cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
- pango_layout_set_font_description(state->pango_layout, state->status_font);
- pango_layout_set_text(state->pango_layout, "Password: ", -1);
-
- int t_width, t_height;
- pango_layout_get_size(state->pango_layout, &t_width, &t_height);
- double line_height = t_height / PANGO_SCALE;
-
- cairo_move_to(cr, field_x, field_y - line_height - field_padding);
- pango_cairo_show_layout(cr, state->pango_layout);
-
- // Draw password asterisks
- if (state->asterisk_svg_handle == NULL) {
- GError *error = NULL;
- state->asterisk_svg_handle = rsvg_handle_new_from_file("asterisk.svg", &error);
- if (error != NULL) {
- fprintf(stderr, "Error loading asterisk SVG\n");
- return;
- }
- }
-
- const double cursor_padding_x = 10.0;
- double cursor_offset_x = 0.0;
-
- RsvgDimensionData dimensions;
- rsvg_handle_get_dimensions(state->asterisk_svg_handle, &dimensions);
-
- double asterisk_height = cursor_height - 20.0;
- double scale_factor = (asterisk_height / dimensions.height);
- double scaled_width = (dimensions.width * scale_factor);
-
- for (unsigned i = 0; i < strlen(state->password_buffer); i++) {
- cairo_save(cr);
- cairo_translate(cr, field_x + cursor_offset_x, field_y + ((cursor_height - asterisk_height) / 2.0));
- cairo_scale(cr, scale_factor, scale_factor);
- rsvg_handle_render_cairo(state->asterisk_svg_handle, cr);
- cairo_restore(cr);
-
- cursor_offset_x += scaled_width + cursor_padding_x;
- }
-
-
- // Draw cursor
- cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, state->cursor_opacity);
- cairo_rectangle(cr, field_x + cursor_offset_x, field_y, cursor_width, cursor_height);
- cairo_fill(cr);
-}
-
-/*
* Main drawing/update routines
*/
@@ -275,8 +156,8 @@ int runloop(cairo_surface_t *surface)
int main(int argc, char **argv)
{
- __width = 800;
- __height = 600;
+ int default_width = 800;
+ int default_height = 600;
__display = XOpenDisplay(NULL);
if (__display == NULL) {
@@ -289,8 +170,8 @@ int main(int argc, char **argv)
__display, // display
root_window, // parent window
0, 0, // x, y
- __width, // width
- __height, // height
+ default_width, // width
+ default_height, // height
0, // border_width
0, // border
0 // background
@@ -309,8 +190,8 @@ int main(int argc, char **argv)
__display,
__window,
visual,
- __width,
- __height
+ default_width,
+ default_height
);
if (surface == NULL) {
@@ -319,7 +200,7 @@ int main(int argc, char **argv)
}
// Docs say this must be called whenever the size of the window changes
- cairo_xlib_surface_set_size(surface, __width, __height);
+ cairo_xlib_surface_set_size(surface, default_width, default_height);
int result = runloop(surface);
diff --git a/src/render.c b/src/render.c
new file mode 100644
index 0000000..e9a5ef3
--- /dev/null
+++ b/src/render.c
@@ -0,0 +1,104 @@
+/*
+ * render.c
+ *
+ * Created by buzzert <buzzert@buzzert.net> 2019-01-18
+ */
+
+#include "render.h"
+
+static const double kLogoBackgroundWidth = 500.0;
+
+void draw_logo(saver_state_t *state)
+{
+ if (state->logo_svg_handle == NULL) {
+ GError *error = NULL;
+ state->logo_svg_handle = rsvg_handle_new_from_file("logo.svg", &error);
+ if (error != NULL) {
+ fprintf(stderr, "Error loading logo SVG\n");
+ return;
+ }
+ }
+
+ cairo_t *cr = state->ctx;
+
+ cairo_save(cr);
+ cairo_set_source_rgb(cr, (208.0 / 255.0), (69.0 / 255.0), (255.0 / 255.0));
+ cairo_rectangle(cr, 0, 0, kLogoBackgroundWidth, state->canvas_height);
+ cairo_fill(cr);
+
+ cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
+
+ // Scale and draw logo
+ RsvgDimensionData dimensions;
+ rsvg_handle_get_dimensions(state->logo_svg_handle, &dimensions);
+
+ const double padding = 100.0;
+ double scale_factor = ((kLogoBackgroundWidth - (padding * 2.0)) / dimensions.width);
+ double scaled_height = (dimensions.height * scale_factor);
+ double y_position = (state->canvas_height - scaled_height) / 2.0;
+ cairo_translate(cr, padding, y_position);
+ cairo_scale(cr, scale_factor, scale_factor);
+ rsvg_handle_render_cairo(state->logo_svg_handle, cr);
+
+ cairo_restore(cr);
+}
+
+void draw_password_field(saver_state_t *state)
+{
+ const double cursor_height = 40.0;
+ const double cursor_width = 30.0;
+ const double field_x = kLogoBackgroundWidth + 50.0;
+ const double field_y = (state->canvas_height - cursor_height) / 2.0;
+ const double field_padding = 10.0;
+
+ cairo_t *cr = state->ctx;
+
+ // Draw status text
+ cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
+ pango_layout_set_font_description(state->pango_layout, state->status_font);
+ pango_layout_set_text(state->pango_layout, "Password: ", -1);
+
+ int t_width, t_height;
+ pango_layout_get_size(state->pango_layout, &t_width, &t_height);
+ double line_height = t_height / PANGO_SCALE;
+
+ cairo_move_to(cr, field_x, field_y - line_height - field_padding);
+ pango_cairo_show_layout(cr, state->pango_layout);
+
+ // Draw password asterisks
+ if (state->asterisk_svg_handle == NULL) {
+ GError *error = NULL;
+ state->asterisk_svg_handle = rsvg_handle_new_from_file("asterisk.svg", &error);
+ if (error != NULL) {
+ fprintf(stderr, "Error loading asterisk SVG\n");
+ return;
+ }
+ }
+
+ const double cursor_padding_x = 10.0;
+ double cursor_offset_x = 0.0;
+
+ RsvgDimensionData dimensions;
+ rsvg_handle_get_dimensions(state->asterisk_svg_handle, &dimensions);
+
+ double asterisk_height = cursor_height - 20.0;
+ double scale_factor = (asterisk_height / dimensions.height);
+ double scaled_width = (dimensions.width * scale_factor);
+
+ for (unsigned i = 0; i < strlen(state->password_buffer); i++) {
+ cairo_save(cr);
+ cairo_translate(cr, field_x + cursor_offset_x, field_y + ((cursor_height - asterisk_height) / 2.0));
+ cairo_scale(cr, scale_factor, scale_factor);
+ rsvg_handle_render_cairo(state->asterisk_svg_handle, cr);
+ cairo_restore(cr);
+
+ cursor_offset_x += scaled_width + cursor_padding_x;
+ }
+
+
+ // Draw cursor
+ cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, state->cursor_opacity);
+ cairo_rectangle(cr, field_x + cursor_offset_x, field_y, cursor_width, cursor_height);
+ cairo_fill(cr);
+}
+
diff --git a/src/render.h b/src/render.h
new file mode 100644
index 0000000..6d2c095
--- /dev/null
+++ b/src/render.h
@@ -0,0 +1,40 @@
+/*
+ * render.h
+ *
+ * Created by buzzert <buzzzer@buzzert.net> 2019-01-18
+ */
+
+#pragma once
+
+#include <cairo/cairo.h>
+#include <cairo-xlib.h>
+#include <librsvg/rsvg.h>
+#include <pango/pangocairo.h>
+
+typedef struct {
+ cairo_t *ctx;
+ cairo_surface_t *surface;
+
+ PangoLayout *pango_layout;
+ PangoFontDescription *status_font;
+
+ RsvgHandle *logo_svg_handle;
+ RsvgHandle *asterisk_svg_handle;
+
+ int canvas_width;
+ int canvas_height;
+
+ double cursor_opacity;
+ double cursor_fade_direction;
+
+ char *password_buffer;
+ size_t password_buffer_len;
+} saver_state_t;
+
+// The purple sidebar
+void draw_logo(saver_state_t *state);
+
+// The status string and paassword field
+void draw_password_field(saver_state_t *state);
+
+