From e6ad62d3c5e776f9fd3fc661f64dfa9223e102dd Mon Sep 17 00:00:00 2001 From: openKylinBot Date: Sat, 14 May 2022 00:31:31 +0800 Subject: [PATCH] Import Upstream version 0.16 --- README | 2 + laptop-detect.1 | 61 +++++++++++++++++++++ laptop-detect.in | 134 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 README create mode 100644 laptop-detect.1 create mode 100644 laptop-detect.in diff --git a/README b/README new file mode 100644 index 0000000..4d31cdf --- /dev/null +++ b/README @@ -0,0 +1,2 @@ +Package to attempt to detect whether we're on a laptop, and then do the right thing. +exits 0 if we're on a laptop, 1 otherwise. diff --git a/laptop-detect.1 b/laptop-detect.1 new file mode 100644 index 0000000..7895848 --- /dev/null +++ b/laptop-detect.1 @@ -0,0 +1,61 @@ +.\" '\" t +.\" ** The above line should force tbl to be a preprocessor ** +.\" Man page for man +.\" +.\" Copyright (C), 2006, Franklin PIAT +.\" Copyright (C), 2017, Simó Albert i Beltran +.\" +.\" This manpage is licensed under the BSD license. For further +.\" information see the copying that comes with the laptop-detect +.\" distribution. +.\" +.\" +.\" Wed Nov 1 16:43:02 CET 2006 Franklin PIAT (fpiat@bigfoot.com) +.\" +.\" .pc +.TH "LAPTOP-DETECT" "1" "March 2017" "laptop-detect 0.14" "User Commands" +.SH "NAME" +.LP +laptop\-detect \- attempt to detect a laptop +.SH "SYNOPSIS" +.LP +laptop\-detect [\fI\-h\fR|\fI\-\-help\fR|\fI\-v\fR|\fI\-\-verbose\fR|\fI\-V\fR|\fI\-\-version\fR|\fI\-D\fR|\fI\-\-debug\fR] +.SH "DESCRIPTION" +.LP +Laptop\-detect attempts to determine whether it is being run on a laptop or a desktop and appraises its caller of this. +.SH "OPTIONS" +.LP +.TP +\fB\-h\fR \fB\-\-help\fR +Output help information and exit. +.TP +\fB\-v\fR \fB\-\-verbose\fR +be verbose (messages go to STDOUT) +.TP +\fB\-V\fR \fB\-\-version\fR +Output version information and exit. +.TP +\fB\-D\fR \fB\-\-debug\fR +Debug mode. +.SH "EXIT STATUS" +.br +.nf +\fB0\fR most likely running on a laptop +\fB1\fR most likely NOT running on a laptop +\fB2\fR usage error (arguments supplied) +.SH "FILES" +.LP +\fI/proc/acpi/battery\fP +.br +\fI/proc/pmu/info\fP +.br +\fI/dev/mem\fP +.br +\fI/sys/devices/virtual/dmi/id/chassis_type\fP +.br +\fI/sys/class/power_supply/*/type\fP +.br +\fI/proc/apm\fP +.SH "SEE ALSO" +.LP +dmidecode(8) diff --git a/laptop-detect.in b/laptop-detect.in new file mode 100644 index 0000000..cd626e7 --- /dev/null +++ b/laptop-detect.in @@ -0,0 +1,134 @@ +#!/bin/sh -e + +usage () { + echo "Usage: $0 [-h|--help|-v|--verbose]" + echo "" + echo " -h | --help print this help" + echo " -v | --verbose be verbose (messages go to STDOUT)" + echo " -V | --version print version information" + echo " -D | --debug debug mode" + echo "" + echo " Possible return values:" + echo " 0 most likely running on a laptop" + echo " 1 most likely NOT running on a laptop" + echo " 2 called with unknown option, -h, --help, -V, --version, -D or --debug" +} + +PRINTIT="/bin/true" # /bin/true accepts any option but never prints anything +EXIT="exit" # replaced in debug mode by /bin/true + +# Check wether we were asked to be verbose + +if [ "$1" != "" ]; then + case "$1" in + "-v"|"--verbose") + PRINTIT="echo" + ;; + "-V"|"--version") + echo "Version: @VERSION@" + exit 2 + ;; + "-h"|"--help") + usage + exit 2 + ;; + "-D"|"--debug") + EXIT="/bin/true" + set -x + ;; + *) + echo "UNKNOWN OPTION: $1" + usage + exit 2 + ;; + esac +fi + +# Are we a mac? +if test -d /proc/pmu; then + batteries=$(grep Battery /proc/pmu/info | cut -f2 -d:) + if test "$batteries" -ne 0; then + $PRINTIT "We're a laptop (Mac: batteries found)" >&2; + $EXIT 0 + fi + exit 1 +fi + +if dmidecode="$(command -v dmidecode)" && [ -r /dev/mem -a -x "$dmidecode" ] +then + # dmidecode to grab the Chassis type + dmitype=$("$dmidecode" --string chassis-type) + + if test "$dmitype" = "Notebook" || \ + test "$dmitype" = "Portable" || \ + test "$dmitype" = "Laptop" || \ + test "$dmitype" = "Hand Held"; then + $PRINTIT "We're a laptop (dmidecode returned \"$dmitype\")" >&2 + $EXIT 0 + fi + + # turn back on for debugging + #echo "$dmitype" +fi + +if [ -e /sys/devices/virtual/dmi/id/chassis_type ] ; then + chassis_type="$(cat /sys/devices/virtual/dmi/id/chassis_type)" + case "$chassis_type" in + 8) # Portable + $PRINTIT "We're a portable (chassis_type is $chassis_type)" >&2 + $EXIT 0 + ;; + 9) # Laptop + $PRINTIT "We're a laptop (chassis_type is $chassis_type)" >&2 + $EXIT 0 + ;; + 10) # Notebook + $PRINTIT "We're a notebook (chassis_type is $chassis_type)" >&2 + $EXIT 0 + ;; + 11) # Hand Held + $PRINTIT "We're a hand held (chassis_type is $chassis_type)" >&2 + $EXIT 0 + ;; + esac +fi + +# check for non device ACPI batteries +/sbin/modprobe battery 2> /dev/null || true +sysfs_power_supply=/sys/class/power_supply +if [ -d $sysfs_power_supply ]; then + for power_supply in $(ls $sysfs_power_supply) + do + if ( + grep -q Battery $sysfs_power_supply/$power_supply/type 2>/dev/null + ) && ( + ! grep -q Device $sysfs_power_supply/$power_supply/scope 2>/dev/null + ) + then + $PRINTIT "We're a laptop (non device ACPI batteries found)" >&2 + $EXIT 0 + fi + done +fi +# old interface: +if [ -d /proc/acpi/battery ]; then + results=`find /proc/acpi/battery -mindepth 1 -type d` + if [ ! -z "$results" ]; then + $PRINTIT "We're a laptop (ACPI batteries found)" >&2 + $EXIT 0 + fi +fi + + +# check for APM batteries. This sucks, because we'll only get a valid response +# if the laptop has a battery fitted at the time +if [ -f /proc/apm ]; then + battery=`awk '{print $6}' &2 + $EXIT 0 + fi +fi + +$PRINTIT "We're not on a laptop (no relevant hint found)" >&2 +$EXIT 1