]> git.draconx.ca Git - fvwmconf.git/commitdiff
Improve wallpaper menu generation.
authorNick Bowler <nbowler@draconx.ca>
Sat, 8 Apr 2017 01:29:30 +0000 (21:29 -0400)
committerNick Bowler <nbowler@draconx.ca>
Fri, 27 Oct 2017 01:17:04 +0000 (21:17 -0400)
Update the wallpaper menu script to be more portable and hopefully
faster, and enhance the xaspect tool to work on rotated displays.

common/wallpaper
scripts/C/xaspect.c
scripts/bgmenu.sh [new file with mode: 0755]
scripts/bgmenu.zsh [deleted file]

index 07e40cd1617f6b6d7819f26109328422e0a5ba35..1bc5002726eb65cc1e3b1c90e6010ce76c747efa 100644 (file)
@@ -15,7 +15,7 @@ DestroyFunc MakeMenuWallpaper
 AddToFunc   MakeMenuWallpaper
  + I DestroyMenu recreate MenuWallpaper
  + I AddToMenu MenuWallpaper Wallpaper Title
- + I PipeRead 'exec $[FVWM_USERDIR]/scripts/bgmenu.zsh $[BGROOT] MenuWallpaper'
+ + I PipeRead 'exec $[FVWM_USERDIR]/scripts/bgmenu.sh --menu=MenuWallpaper $[BGROOT]'
 
 DestroyFunc SetWallpaper
 AddToFunc   SetWallpaper
index 16d151fe62ba4d6b60424a7e8dce323f8889a890..0009424f24f3263d5c8fd56b31d7d87717ca59bf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2007 Nick Bowler
+ * Copyright © 2008, 2017 Nick Bowler
  *
  * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
  * This is free software: you are free to do what the fuck you want to.
 #include <inttypes.h>
 #include <xcb/xcb.h>
 
-#define MKASPECT(w, h) { (double)w/h, #w ":" #h }
-
 /* Table of common monitor aspect ratios.  Add to this as necessary. */
-static struct aspect {
-       double ratio;
-       char *name;
+static const struct aspect {
+       unsigned num, denom;
 } aspects[] = {
-       MKASPECT(16, 10),
-       MKASPECT(16, 9),
-       MKASPECT(8, 3),
-       MKASPECT(5, 4),
-       MKASPECT(4, 3)
+       { 16, 10 },
+       { 16, 9 },
+       { 8, 3 },
+       { 5, 4 },
+       { 4, 3 }
 };
 
 /*
@@ -46,24 +43,32 @@ static xcb_screen_t *getscreen(xcb_connection_t *c, int screen)
        return NULL;
 }
 
-char *testaspect(xcb_screen_t *screen)
+void find_nearest_aspect(xcb_screen_t *screen, struct aspect *out)
 {
        unsigned int w = screen->width_in_millimeters;
        unsigned int h = screen->height_in_millimeters;
 
-       double ratio = (double)w/h;
-       double diff = 0;
-       char *best = NULL;
+       double r = (double)w/h;
+       double d = HUGE_VAL;
        int i;
 
        for (i = 0; i < (sizeof aspects / sizeof aspects[0]); i++) {
-               if (!best || fabs(aspects[i].ratio - ratio) < diff) {
-                       best = aspects[i].name;
-                       diff = fabs(aspects[i].ratio - ratio);
+               double t = (double) aspects[i].num / aspects[i].denom;
+               double cmp;
+
+               cmp = fabs(r - t);
+               if (cmp < d) {
+                       *out = aspects[i];
+                       d = cmp;
                }
-       }
 
-       return best;
+               cmp = fabs(r - 1/t);
+               if (cmp < d) {
+                       out->num = aspects[i].denom;
+                       out->denom = aspects[i].num;
+                       d = cmp;
+               }
+       }
 }
 
 struct options {
@@ -91,6 +96,7 @@ struct options {
 
 int main(int argc, char **argv)
 {
+       struct aspect screenaspect;
        xcb_connection_t *display;
        xcb_screen_t     *screen;
        int screen_num;
@@ -115,7 +121,9 @@ int main(int argc, char **argv)
                printf("%" PRIu16 "x%" PRIu16 "-",
                       screen->width_in_pixels, screen->height_in_pixels);
        }
-       printf("%s\n", testaspect(screen));
+
+       find_nearest_aspect(screen, &screenaspect);
+       printf("%u:%u\n", screenaspect.num, screenaspect.denom);
 
        xcb_disconnect(display);
        return EXIT_SUCCESS;
diff --git a/scripts/bgmenu.sh b/scripts/bgmenu.sh
new file mode 100755 (executable)
index 0000000..98a2a1d
--- /dev/null
@@ -0,0 +1,47 @@
+#!/bin/sh
+#
+# Copyright © 2008, 2017 Nick Bowler
+#
+# Generates an FVWM menu for selecting the PNG images found in a given
+# directory.  Thumbnails for the images are generated on the fly using
+# the thumbnailer.zsh script.  Menu items run the specified function
+# with the filename as the first argument.
+
+thumber="$FVWM_USERDIR/scripts/thumbnail.zsh --size x160"
+xaspect=$FVWM_USERDIR/scripts/C/xaspect
+menu=MenuWallpaper
+func=SetWallpaper
+
+lastarg=
+dashdash=
+for arg; do
+  if test ${lastarg:+y}; then
+    arg=$lastarg=$arg
+    lastarg=
+  fi
+
+  case $dashdash$arg in
+  --menu=*) menu=${arg#--menu=} ;;
+  --func=*) func=${arg#--func=} ;;
+  --menu|--func) lastarg=$arg ;;
+  --) dashdash=: ;;
+  -*) printf '%s: unrecognized argument: %s\n' "$0" "$arg" 1>&2; exit 1 ;;
+  *) set x "$@" "$arg"; shift
+  esac
+
+  shift
+done
+
+case $# in
+1) dir=$1 ;;
+*) printf 'usage: %s [options] directory\n' "$0" 1>&2
+esac
+
+size=`$xaspect -dimensions`
+find $1 -maxdepth 1 -name "*$size.png" -print0 -exec $thumber {} \; |
+  awk -F '\0' '{
+    gsub(/["\\]/, "\\\\&");
+    caption = gensub(/.*\/(.*)\..*/, "\\1", 1, $1);
+    print "AddToMenu", menuname, "\"" caption "*" $2 "*\"",
+          funcname, "\"" $1 "\""
+  }' "menuname=$menu" "funcname=$func"
diff --git a/scripts/bgmenu.zsh b/scripts/bgmenu.zsh
deleted file mode 100755 (executable)
index 15bbd2a..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/usr/bin/env zsh
-# Generates an FVWM menu for selecting the PNG images found in a given
-# directory.  Thumbnails for the images are generated on the fly using convert
-# from ImageMagick, and stored in the .thumbs subdirectory.  The menu items
-# run the specified function with the filename as the first argument.
-
-menu="MenuWallpaper"
-func="SetWallpaper"
-thumber="$FVWM_USERDIR/scripts/thumbnail.zsh"
-
-if [ ! -d "$1" ]; then
-       echo "usage: $0 <directory> [menuname]" 1>&2
-       exit 1
-fi
-[ -n "$2" ] && menu="$2"
-
-for i in "$1"/*$($FVWM_USERDIR/scripts/C/xaspect).png; do
-       thumb="`$thumber --size x160 "$i"`"
-       echo "AddToMenu $menu \"`basename "${i%.png}"`*$thumb*\" $func \"$i\""
-done