From 301a2d9987a8db11374a709241414f0c90385dd7 Mon Sep 17 00:00:00 2001 From: infeeeee Date: Wed, 1 Apr 2020 02:04:31 +0200 Subject: [PATCH] initial commit --- buttonpressed.sh | 68 ++++++++++++++++++++++++++++++++ readme.md | 43 ++++++++++++++++++++ scanner.d/plustek.conf | 60 ++++++++++++++++++++++++++++ scannerbutton.sh | 74 +++++++++++++++++++++++++++++++++++ scannerbutton.sh.1 | 8 ++++ udev-example/55-libsane.rules | 1 + 6 files changed, 254 insertions(+) create mode 100644 buttonpressed.sh create mode 100644 readme.md create mode 100644 scanner.d/plustek.conf create mode 100644 scannerbutton.sh create mode 100755 scannerbutton.sh.1 create mode 100644 udev-example/55-libsane.rules diff --git a/buttonpressed.sh b/buttonpressed.sh new file mode 100644 index 0000000..208e36c --- /dev/null +++ b/buttonpressed.sh @@ -0,0 +1,68 @@ +#!/bin/sh + +# daemon's name +DAEMON=scanbd + +# securely create temporary file to avoid race condition attacks +TMPFILE=`mktemp /tmp/$DAEMON.XXXXXX` + +# lock file +LOCKFILE="/tmp/$DAEMON.lock" + +# device name +DEVICE="$2" + +# destination of the final image file (modify to match your setup) +DESTINATION="/home/manolis/Desktop/scanned_image.jpg" + +# remove temporary file on abort +trap 'rm -f $TMPFILE' 0 1 15 + +# function: create lock file with scanbuttond's PID +mk_lock() { + pidof $DAEMON > $LOCKFILE +} + +# function: remove temporary and lock files +clean_up () { + test -e $LOCKFILE && rm -f $LOCKFILE + rm -f $TMPFILE +} + +# function: check if lock file exists and print an error message with zenity (if it's installed) +chk_lock() { + if [ -e $LOCKFILE ]; then + if [ -x /usr/bin/zenity ]; then + zenity --display :0.0 --title="Scanbuttond" --error + --text="Another scanning operation is currently in progress." + fi + exit 1 + fi +} + +# function: the actual scan command (modify to match your setup) +scan() { + scanimage --device-name "$DEVICE" --mode Color --resolution 300 + --depth 8 -x 215 -y 297 > $TMPFILE +} + +case $1 in + 1) + # button 1 (scan): scan the picture & save it as $DESTINATION + chk_lock; mk_lock; scan; convert $TMPFILE -quality 85 -quiet $DESTINATION; clean_up + ;; + 2) + # button 2 (copy): scan the picture, convert it to postscript and print it + chk_lock; mk_lock; scan; convert $TMPFILE ps:- | lpr; clean_up + ;; + 3) + # button 3 (mail): scan the picture, save it as $DESTINATION and send it as an e-mail + # attachment with Evolution + chk_lock; mk_lock; scan; convert $TMPFILE -quality 85 -quiet $DESTINATION + evolution --display=:0.0 "mailto:?attach=$DESTINATION"; clean_up + ;; + 4) + # button 4 (web): unconfigured + echo "button 4 has been pressed on $2" + ;; +esac diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5d7b6d7 --- /dev/null +++ b/readme.md @@ -0,0 +1,43 @@ +## Setup + +### Sane + +```bash +sudo apt intall sane-utils +# add user to scanner group. Logout after this command +sudo adduser zero scanner +# find scanner ids +lsusb +# copy and modify udev example file to udev folder +sudo cp udev-example/55-libsane.rules /etc/udev/rules.d/ +# restart udev service, than unplug and replug scanner +sudo service udev restart +# now this should run without sudo +scanimage -L +``` + +### Scanner button + +```bash +sudo apt install scanbd +# create script directory +sudo mkdir /etc/scanbd/scripts +# copy script +sudo cp scannerbutton.sh /etc/scanbd/scripts/ +# backup original plustek config +sudo mv /etc/scanbd/scanner.d/plustek.conf /etc/scanbd/scanner.d/plustek.conf.orig +# copy modified plustek.conf +sudo cp scanner.d/plustek.conf /etc/scanbd/scanner.d/ +# restart scanbd +sudo service scanbd restart +``` + +## Scanning + +```bash +scanimage -x 210 -y 297 --format=png --resolution 150 > /media/miniretek/scanner/scan-`date +%F-%H%M`.png +``` + +## Acknoledgements + +scannerbutton.sh based on the script found here: https://www.linux.com/news/how-configure-scanners-buttons-linux/ diff --git a/scanner.d/plustek.conf b/scanner.d/plustek.conf new file mode 100644 index 0000000..ac44cf6 --- /dev/null +++ b/scanner.d/plustek.conf @@ -0,0 +1,60 @@ +/* + * $Id: snapscan.conf 154 2013-01-06 07:24:56Z wimalopaan $ + * + * scanbd - KMUX scanner button daemon + * + * Copyright (C) 2008 - 2013 Wilhelm Meier (wilhelm.wm.meier@googlemail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +device plustek { + + # the device matching string + filter = "plustek" # regex + + # the device description + desc = "Canon N650" + + action b0 { + filter = "^button.*0.*" + numerical-trigger { + from-value = 0 + to-value = 1 + } + desc = "Button0" + script = "scannerbutton.sh" + } + action b1 { + filter = "^button.*1.*" + numerical-trigger { + from-value = 0 + to-value = 1 + } + desc = "Button1" + script = "scannerbutton.sh" + } + action b2 { + filter = "^button.*2.*" + numerical-trigger { + from-value = 0 + to-value = 1 + } + desc = "Button2" + script = "scannerbutton.sh" + } + +} + diff --git a/scannerbutton.sh b/scannerbutton.sh new file mode 100644 index 0000000..e27f0b0 --- /dev/null +++ b/scannerbutton.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +# Buttons: +# b0: E-mail +# b1: Copy +# b2: Scan + +echo $SCANBD_ACTION + +# daemon's name +#DAEMON=scanbd + +# securely create temporary file to avoid race condition attacks +TMPFILE=`mktemp /tmp/$DAEMON.XXXXXX` + +# lock file +LOCKFILE="/tmp/$DAEMON.lock" + +# device name +#DEVICE="$2" + +# destination of the final image file (modify to match your setup) +DESTINATION="media/miniretek/scanner/scan-`date +%F-%H%M`.png" + +# remove temporary file on abort +trap 'rm -f $TMPFILE' 0 1 15 + +# function: create lock file with scanbuttond's PID +mk_lock() { + pidof $DAEMON > $LOCKFILE +} + +# function: remove temporary and lock files +clean_up () { + test -e $LOCKFILE && rm -f $LOCKFILE + rm -f $TMPFILE +} + +# function: check if lock file exists and print an error message with zenity (if it's installed) +chk_lock() { + if [ -e $LOCKFILE ]; then + if [ -x /usr/bin/zenity ]; then + zenity --display :0.0 --title="Scanbuttond" --error + --text="Another scanning operation is currently in progress." + fi + exit 1 + fi +} + +# function: the actual scan command (modify to match your setup) +scan() { + #scanimage --device-name "$DEVICE" --mode Color --resolution 300 --depth 8 -x 215 -y 297 > $TMPFILE + scanimage -x 210 -y 297 --format=png --resolution 150 > $TMPFILE +} + +case $SCANBD_ACTION in + "b0") + # button 1 (scan): scan the picture & save it as $DESTINATION + #chk_lock; mk_lock; scan; convert $TMPFILE -quality 85 -quiet $DESTINATION; clean_up + echo "email button pressed" + ;; + "b1") + # button 2 (copy): scan the picture, convert it to postscript and print it + #chk_lock; mk_lock; scan; convert $TMPFILE ps:- | lpr; clean_up + echo "copy button pressed" + ;; + "b2") + # button 3 (mail): scan the picture, save it as $DESTINATION and send it as an e-mail + # attachment with Evolution +# chk_lock; mk_lock; scan; convert $TMPFILE -quality 85 -quiet $DESTINATION +# evolution --display=:0.0 "mailto:?attach=$DESTINATION"; clean_up + echo "scan button pressed" + ;; +esac diff --git a/scannerbutton.sh.1 b/scannerbutton.sh.1 new file mode 100755 index 0000000..98932a8 --- /dev/null +++ b/scannerbutton.sh.1 @@ -0,0 +1,8 @@ +#!/bin/bash + +# Buttons: +# b0: E-mail +# b1: Copy +# b2: Scan + +echo $SCANBD_ACTION diff --git a/udev-example/55-libsane.rules b/udev-example/55-libsane.rules new file mode 100644 index 0000000..eac8479 --- /dev/null +++ b/udev-example/55-libsane.rules @@ -0,0 +1 @@ +ATTRS{idVendor}=="04a9", ATTRS{idProduct}=="220d", MODE="0660", GROUP="scanner", ENV{libsane_matched}="yes"