From 5682ab44e3ecca0a18404885bcfbf97b8762a89b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Sat, 2 May 2015 09:53:00 +0200 Subject: Make mga-bg-res a systemd service that uses monitor-probe Now runs as a systemd service with root privileges, therefore the mga-bg-res C binary is no longer needed. Uses monitor-probe to find the optimal resolution when X is not yet started, and handles cases where monitor-probe fails by defaulting to 4:3 aspect ratio. --- mga-bg-res/NEWS | 4 +++ mga-bg-res/mga-bg-res.c | 82 ------------------------------------------- mga-bg-res/mga-bg-res.service | 9 +++++ mga-bg-res/mga-bg-res.sh | 47 +++++++++++++++++++++++++ mga-bg-res/mga-bg-res.xinit | 41 ---------------------- 5 files changed, 60 insertions(+), 123 deletions(-) delete mode 100644 mga-bg-res/mga-bg-res.c create mode 100644 mga-bg-res/mga-bg-res.service create mode 100755 mga-bg-res/mga-bg-res.sh delete mode 100755 mga-bg-res/mga-bg-res.xinit (limited to 'mga-bg-res') diff --git a/mga-bg-res/NEWS b/mga-bg-res/NEWS index 0211c74..ead83b4 100644 --- a/mga-bg-res/NEWS +++ b/mga-bg-res/NEWS @@ -1,3 +1,7 @@ +- Make mga-bg-res a systemd service +- Uses monitor-probe to find the optimal resolution when X is not yet started +- Handle cases where monitor-probe fails by defaulting to 4:3 aspect ratio + Version 0.6 (2015-04-22): - xinit script: Work also when default.jpg symlink is missing diff --git a/mga-bg-res/mga-bg-res.c b/mga-bg-res/mga-bg-res.c deleted file mode 100644 index 6061587..0000000 --- a/mga-bg-res/mga-bg-res.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include - -#define PATH "/usr/share/mga/backgrounds/" -#define DSTLINK PATH"default.jpg" -#define SRCLINK_PREFIX PATH"Mageia-Default-" -#define SRCLINK_SUFFIX ".jpg" -#define SRCLINK SRCLINK_PREFIX"%s"SRCLINK_SUFFIX -#define RESSIZE 20 -#define SRCSIZE (RESSIZE+strlen(SRCLINK_PREFIX)+strlen(SRCLINK_SUFFIX) + 1) -#define ALLOWCHARS "0123456789x" - -int main(int argc, char* argv[]) { - char src[SRCSIZE]; - char res[RESSIZE]; - int l; - - // check argument - if (argc != 2) { - fprintf(stderr, "Need a resolution argument, eg: '1920x1080'.\n"); - return 1; - } - - // get the res (max RESSIZE) - l = snprintf(res, RESSIZE, argv[1]); - if (l < 5) { - fprintf(stderr, "Malformed resolution argument, eg: '1920x1080'.\n"); - return 2; - } - - // to be sure, set the last char as 0 - if (l < RESSIZE) - res[l] = 0; - else - res[RESSIZE-1] = 0; - - // check if the resolution contains acceptable chars - l = strspn(res, ALLOWCHARS); - if (l < 5) { - fprintf(stderr, "Malformed resolution argument, eg: '1920x1080'.\n"); - return 2; - } - - // to be sure, set the last char as 0 - if (l < RESSIZE) - res[l] = 0; - else - res[RESSIZE-1] = 0; - - // create target string - if (snprintf(src, SRCSIZE, SRCLINK, res) < 0) { - fprintf(stderr, "Unknown error determining symlink target.\n"); - return 3; - } - - // check if symlink target exists - if (access(src, F_OK) < 0) { - fprintf(stderr, "No image for this resolution.\n"); - return 0; - } - - // set symlink - if (symlink(src, DSTLINK) < 0) { - if (errno != EEXIST) { - fprintf(stderr, "Symlink could not be set: %s.\n", strerror(errno)); - return 4; - } - - // since the destination exists, remove and try again - if (unlink(DSTLINK) < 0) { - fprintf(stderr, "Symlink could not be forced: %s.\n", strerror(errno)); - return 5; - } - if (symlink(src, DSTLINK) < 0) { - fprintf(stderr, "Unknown error during forced symlink: %s.\n", strerror(errno)); - return 6; - } - } - return 0; -} diff --git a/mga-bg-res/mga-bg-res.service b/mga-bg-res/mga-bg-res.service new file mode 100644 index 0000000..821fb9f --- /dev/null +++ b/mga-bg-res/mga-bg-res.service @@ -0,0 +1,9 @@ +[Unit] +Description=Adapt Mageia theme to the monitor resolution + +[Service] +Type=oneshot +ExecStart=/usr/libexec/mga-bg-res + +[Install] +WantedBy=multi-user.target diff --git a/mga-bg-res/mga-bg-res.sh b/mga-bg-res/mga-bg-res.sh new file mode 100755 index 0000000..db937cb --- /dev/null +++ b/mga-bg-res/mga-bg-res.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +bgpath="/usr/share/mga/backgrounds" +theme="Mageia-Default" +curlink=$(readlink $bgpath/default.jpg) + +# If the following tests bail out, default to 4:3 aspect ratio (e.g. 1024x768) +res="1600x1200" + +# Search for the optimal background resolution according to the driver +DRIVER=$(grep -A 6 'Device' /etc/X11/xorg.conf | grep 'Driver') # => Driver "DriverName" +DRIVER=$(expr "$DRIVER" : '.*"\(.*\)"') # isolate driver's name +if [ ! -z "$DRIVER" -a -e /usr/sbin/monitor-probe ]; then + res=$(exec /usr/sbin/monitor-probe $DRIVER | grep -m 1 ModeLine) # => ModeLine "1920x1080" ... + res=$(expr "$res" : '.*"\(.*\)".*') # isolate the resolution +fi + +# Check if the symlink is already good +if [ "$curlink" = "$bgpath/$theme-$res.jpg" ]; then + exit 1 +fi + +# Check if this is a supported resolution, if not, find the background with a similar aspect ratio +if [ ! -e "$bgpath/$theme-$res.jpg" ]; then + width=$(echo $res | cut -f1 -d"x") + height=$(echo $res | cut -f2 -d"x") + # Bash only does integer arithmetic, we multiply everything by 1000 to workaround this + ratio=$((1000*$width/$height)) + declare -A refRatios=( ["1250"]="1280x1024" ["1333"]="1600x1200" ["1600"]="1920x1200" + ["1667"]="1280x768" ["1707"]="1024x600" ["1778"]="1920x1080" ) + for key in "${!refRatios[@]}"; do + if [ $(($ratio % $key)) -lt 25 -o $(($key % $ratio)) -lt 25 ]; then + res=${refRatios[$key]} + break + fi + done +fi + +# Check again if the symlink does not already point to this new resolution +# If not, create a new symlink +if [ "$curlink" != "$bgpath/$theme-$res.jpg" ]; then + if [ -e "$bgpath/$theme-$res.jpg" ]; then + ln -sf $bgpath/$theme-$res.jpg $bgpath/default.jpg + else + echo -e "Could not find this file: $bgpath/$theme-$res.jpg.\nPlease check that the mageia-theme-Default package is properly installed, or disable the mga-bg-res systemd service if you do not want to force using the Mageia theme." + fi +fi diff --git a/mga-bg-res/mga-bg-res.xinit b/mga-bg-res/mga-bg-res.xinit deleted file mode 100755 index 42d79da..0000000 --- a/mga-bg-res/mga-bg-res.xinit +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -# Find currently used resolution -res=$( xrandr | awk '/\sconnected/{i=split(substr($0,0,index($0,"+")-1),a);if($0~/\sprimary\s/){if(p==""){p=a[i]}}else{if(d==""){d=a[i]}}}END{if(p==""){print d}else{print p}}' ) - -bgpath="/usr/share/mga/backgrounds" -if [ "$(readlink $bgpath/default.jpg)" != "$bgpath/Mageia-Default-$res.jpg" ]; then - # Check if this is a supported resolution, if not, find the background with the same aspect ratio - if [ ! -e "$bgpath/Mageia-Default-$res.jpg" ]; then - width=$(echo $res | cut -f1 -d"x") - height=$(echo $res | cut -f2 -d"x") - # Bash only does integer arithmetic, we multiply everything by 1000 to workaround this - ratio=$((1000*$width/$height)) - - found=false - declare -A refRatios=( ["1250"]="1280x1024" ["1333"]="1600x1200" ["1600"]="1920x1200" - ["1667"]="1280x768" ["1707"]="1024x600" ["1778"]="1920x1080" ) - for key in "${!refRatios[@]}"; do - if [ $(($ratio % $key)) -lt 25 -o $(($key % $ratio)) -lt 25 ]; then - res=${refRatios[$key]} - ratio=$key - found=true - break - fi - done - - if ! $found; then - echo "mga-bg-res: Your aspect ratio does not seem to be supported, keeping default background resolution." - exit 1 - fi - fi - - if [ "$(readlink $bgpath/default.jpg)" != "$bgpath/Mageia-Default-$res.jpg" ]; then - echo "mga-bg-res: Changing the background symlink to point to Mageia-Default-$res.jpg (aspect ratio ${ratio:0:1}.${ratio:1:3})" - /usr/bin/mga-bg-res $res - else - echo "mga-bg-res: Nothing to do, the background symlink is already correct." - fi -else - echo "mga-bg-res: Nothing to do, the background symlink is already correct." -fi -- cgit v1.2.1