Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Copyright (C) 2010 Damjan Dimitrioski <damjandimitrioski at gmail dot 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 3 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.
- #
- # quick priority changer - program that will change the priority of "any"
- # (almost) program by clicking on it's xwindow.
- # USAGE:
- # add a global shortcut in your favourite DE or add 2 new desktop entri-
- # es.
- #
- # bugs:
- # not tested on remote xwindows.
- # Returns the name of the program.
- # arg $1 = process pid.
- function progamName(){
- echo $(ps -p "$1" -o comm --no-headers);
- }
- # If the Desktop Session is unknown, invokes a custom gui dialog, made
- # with zenity
- # arg $1 = command to execute.
- # arg $2 = command arguments.
- function zcustomSudo(){
- echo "customSudo $1";
- cmd="$1 $2";
- zenity --entry --hide-text --title="Password" --text="enter sudo pasword for user: $USER." | sudo -S $cmd;
- sudo -k;
- }
- # Gets the priority of program by it's pid.
- # arg $1 = process pid.
- function getPriority(){
- echo $(ps -p "$1" -o nice --no-headers);
- }
- # Sets the priority of the program, if the niceness <0, than it invokes
- # the sudo gui dialog.
- # arg $1 = programs pid.
- # arg $2 = new priority.
- function setPriority(){
- #echo "the pid:"$1;
- if [[ $1 -gt -1 ]]; then
- #echo "new priority $2.";
- renice -n "$2" "$1";
- else
- $su "renice -n $2" "$1";
- fi;
- }
- # Sets the gui sudo dialog based on the running Desktop Session, if any.
- function setSudoDialog(){
- case "$DESKTOP_SESSION" in
- kde)
- #if [[ -e "`which kdesu`" ]]; then alias su="kdesu"; fi;
- if [[ -e "`which kdesu`" ]]; then su="kdesu"; fi;
- ;;
- GNOME)
- #if [[ -e "`which gksudo`" ]]; then alias su="gksudo"; fi;
- if [[ -e "`which gksudo`" ]]; then su="gksudo"; fi;
- ;;
- *)
- su="zcustomSudo";
- esac
- }
- # Removes spaces from a given string.
- # arg $1 = string to trim.
- function trim(){
- echo $1 | sed -e 's/ //'
- }
- # Used to inform the user of an error.
- # arg $1 = the case to jump to.
- function inform(){
- case "$1" in
- *)
- zenity --error --title "Under construction" --text "Under construction.";
- esac
- }
- # Opens some sort of a gui dialog which ask's the user to choose a number
- # from a range, defined by a minimum and a maximum.
- # arg $1 = programs name.
- # arg $2 = programs pid.
- # TODO: A case switch that will open input prompt for the current DE.
- function numberPrompt(){
- msg="Set a new priority to $1[$2], current [$value]"
- msgt="Set priority"
- zscale='--scale --min-value=-19 --max-value=19 '"--value=$value"
- empty=" " # to separate the strings :D.
- title="--title=$msgt"
- txtmsg="--text=$msg"
- # On cancel exits the script.
- echo $(zenity $zscale "$title" $empty "$txtmsg" || exit);
- }
- setSudoDialog # Sets the default gui sudo dialog.
- # Gets the pid of the desired program.
- pid="`xprop | grep "_NET_WM_PID(CARDINAL)" |\
- awk -F= '{print $2}' | sed -e 's/ //'`" || exit; # TODO: if the xwindow
- #coudn't be identified, than shows an error gui dialog.
- pname=$(progamName $pid); # The name of the focused program.
- value="`getPriority $pid`" # The current priority of the program.
- # opens a gui dialog in which the user chooses a number
- # for the new priority of the selected program.
- priority=$(numberPrompt $pname $pid);
- # Changes the programs priority to the number.
- setPriority "$pid" "$priority"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement