SHARE
TWEET

Untitled

a guest May 19th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #      Copyright (C) 2010 Damjan Dimitrioski <damjandimitrioski at gmail dot com>
  3. #
  4. #      This program is free software; you can redistribute it and/or modify
  5. #      it under the terms of the GNU General Public License as published by
  6. #      the Free Software Foundation; either version 3 of the License, or
  7. #      (at your option) any later version.
  8. #
  9. #      This program is distributed in the hope that it will be useful,
  10. #      but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. #      GNU General Public License for more details.
  13. #
  14. #      You should have received a copy of the GNU General Public License
  15. #      along with this program; if not, write to the Free Software
  16. #      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. #      MA 02110-1301, USA.
  18. #
  19.  
  20. # quick priority changer - program that will change the priority of "any"
  21. # (almost) program by clicking on it's xwindow.
  22.  
  23. # USAGE:
  24. #   add a global shortcut in your favourite DE or add 2 new desktop entri-
  25. #   es.
  26. #
  27. # bugs:
  28. #   not tested on remote xwindows.
  29.  
  30. # Returns the name of the program.
  31. # arg $1 = process pid.
  32. function progamName(){
  33.     echo $(ps -p "$1" -o comm --no-headers);
  34. }
  35. # If the Desktop Session is unknown, invokes a custom gui dialog, made
  36. # with zenity
  37. # arg $1 = command to execute.
  38. # arg $2 = command arguments.
  39. function zcustomSudo(){
  40.     echo "customSudo $1";
  41.     cmd="$1 $2";
  42.     zenity --entry --hide-text --title="Password" --text="enter sudo pasword for user: $USER." | sudo -S $cmd;
  43.     sudo -k;
  44. }
  45. # Gets the priority of program by it's pid.
  46. # arg $1 = process pid.
  47. function getPriority(){
  48.     echo $(ps -p "$1" -o nice --no-headers);
  49. }
  50. # Sets the priority of the program, if the niceness <0, than it invokes
  51. # the sudo gui dialog.
  52. # arg $1 = programs pid.
  53. # arg $2 = new priority.
  54. function setPriority(){
  55.     #echo "the pid:"$1;
  56.     if [[ $1 -gt -1 ]]; then
  57.     #echo "new priority $2.";
  58.     renice -n "$2" "$1";
  59.     else
  60.     $su "renice -n $2" "$1";
  61.     fi;
  62. }
  63. # Sets the gui sudo dialog based on the running Desktop Session, if any.
  64. function setSudoDialog(){
  65.     case "$DESKTOP_SESSION" in
  66.     kde)
  67.         #if [[ -e "`which kdesu`" ]]; then alias su="kdesu"; fi;
  68.         if [[ -e "`which kdesu`" ]]; then su="kdesu"; fi;
  69.     ;;
  70.     GNOME)
  71.         #if [[ -e "`which gksudo`" ]]; then alias su="gksudo"; fi;
  72.         if [[ -e "`which gksudo`" ]]; then su="gksudo"; fi;
  73.     ;;
  74.     *)
  75.         su="zcustomSudo";
  76.     esac
  77. }
  78. # Removes spaces from a given string.
  79. # arg $1 = string to trim.
  80. function trim(){
  81.     echo $1 | sed -e 's/ //'
  82. }
  83. # Used to inform the user of an error.
  84. # arg $1 = the case to jump to.
  85. function inform(){
  86.     case "$1" in
  87.     *)
  88.     zenity --error --title "Under construction" --text "Under construction.";
  89.     esac
  90. }
  91. # Opens some sort of a gui dialog which ask's the user to choose a number
  92. # from a range, defined by a minimum and a maximum.
  93. # arg $1 = programs name.
  94. # arg $2 = programs pid.
  95. # TODO: A case switch that will open input prompt for the current DE.
  96. function numberPrompt(){
  97.     msg="Set a new priority to $1[$2], current [$value]"
  98.     msgt="Set priority"
  99.  
  100.     zscale='--scale --min-value=-19 --max-value=19 '"--value=$value"
  101.     empty=" " # to separate the strings :D.
  102.     title="--title=$msgt"
  103.     txtmsg="--text=$msg"
  104.  
  105.     # On cancel exits the script.
  106.     echo $(zenity $zscale "$title" $empty "$txtmsg" || exit);
  107. }
  108.  
  109. setSudoDialog # Sets the default gui sudo dialog.
  110. # Gets the pid of the desired program.
  111. pid="`xprop | grep "_NET_WM_PID(CARDINAL)" |\
  112. awk -F= '{print $2}' | sed -e 's/ //'`" || exit; # TODO: if the xwindow
  113. #coudn't be identified, than shows an error gui dialog.
  114. pname=$(progamName $pid); # The name of the focused program.
  115. value="`getPriority $pid`" # The current priority of the program.
  116. # opens a gui dialog in which the user chooses a number
  117. # for the new priority of the selected program.
  118. priority=$(numberPrompt $pname $pid);
  119. # Changes the programs priority to the number.
  120. setPriority "$pid" "$priority"
RAW Paste Data
Top