#!/bin/sh ####################################################################### # # Set up application windows the way I like, across my 9 Gnome workspaces. # I use http://svn.red-bean.com/repos/kfogel/trunk/.local/share/\ # gnome-shell/extensions/kfogel-desktop-setup@kfogel.red-bean.com/ to # launch this script from an icon on my Gnome title bar (which I think # is the name for the bar along the top of the screen that shows the # date, the network connection status, battery power, etc, and at its # right has that dropdown menu with, you know, the stuff). # # https://unix.stackexchange.com/questions/27050/\ # how-to-start-an-application-on-a-different-workspace # ####################################################################### # # Copyright (C) 2018, 2019 Karl Fogel # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ####################################################################### XTERM=/usr/bin/xterm # Pass "b" argument to launch browser only. BROWSER_ONLY="no" if [ "${1}" = "b" ]; then BROWSER_ONLY="yes" elif [ "${1}x" != "x" ]; then echo "WARNING: '${1}' unrecognized (use 'b' for browser-only)" >&2 exit 1 fi # Add one level of indirection for wmctrl because there are divergent # forks (https://github.com/Conservatory/wmctrl lists a couple of them # in its README) and we might want to experiment with some of them some # day. That would involve using a locally-built wmctrl instead of the # one in the Debian package repository, hence this indirection step. WMCTRL=/usr/bin/wmctrl cd ${HOME} ${HOME}/bin/xfix ${WMCTRL} -n 9 # Gnome names workspaces starting from 0, but I name them starting # from 1 so they match the function keys I bind them to. To avoid # off-by-one confusion, the symbolic names below match my naming, # while the values match Gnome's (we pass the value as the parameter # to the '-t' option of 'wmctrl'). WKSPC_1="0" WKSPC_2="1" WKSPC_3="2" WKSPC_4="3" WKSPC_5="4" WKSPC_6="5" WKSPC_7="6" WKSPC_8="7" WKSPC_9="8" # Parameters that control window dimensions and placements. SXW="80" # My xterms have the same width on all machines. SXH="24" # Standard xterm height is the same on all machines. TXH="UNINITIALIZED" # Tall xterm height varies by machine. O_X="UNINITIALIZED" # Other left X (b/c workspace 7 has tall xterm too) R_X="UNINITIALIZED" # Right X coordinate (workspaces 1, 2) L_X="UNINITIALIZED" # Left X coordinate (workspace 2) B_Y="UNINITIALIZED" # Bottom Y coordinate (workspaces 1, 2) T_Y="UNINITIALIZED" # Top Y coordinate (workspace 2) C_X="UNINITIALIZED" # Center X coordinate (workspaces 3, 8, 9) C_Y="UNINITIALIZED" # Center Y coordinate (workspaces 3, 8, 9) FFOX_W="UNINITIALIZED" # Firefox width FFOX_H="UNINITIALIZED" # Firefox height # Initialize all the uninitialized parameters. I determined the first # few of these by experimentation, but eventually (starting with the # "mused" entry) had enough information to calculate the parameters # arithmetically. So all the entries could now be done arithmetically # with dynamic code, based on the output of 'xwininfo -root'. Doing # so would future-proof the code for whatever new machines might get # added, and maybe I'll get around to it some day, but right now the # hardcoded numbers work for all the machines I currently own anyway. # # And even before having a closed form, one can just use the # dimensions as keys to know which monitor we're on. ROOT_W=`xwininfo -root | grep -E "^ +Width: " | cut -d " " -f 4` ROOT_H=`xwininfo -root | grep -E "^ +Height: " | cut -d " " -f 4` if [ "${ROOT_W}x${ROOT_H}" = "1280x1024" ]; then # former kwork external monitor TXH="072" O_X="200" R_X="788" L_X="010" B_Y="664" T_Y="035" C_X="397" C_Y="374" FFOX_W="1261" FFOX_H="0944" elif [ "${ROOT_W}x${ROOT_H}" = "1920x1080" ]; then # home large monitor (floss), and apparently now kwork external too TXH="076" O_X="200" R_X="1424" L_X="012" B_Y="717" T_Y="039" C_X="757" C_Y="404" FFOX_W="1896" # The large large monitor at home (floss) and the large monitor at # work (kwork) have the same dimensions, but somehow firefox's # vertical dimension behaves differently on them. HOSTNAME=`hostname -s` if [ ${HOSTNAME} = "kwork" ]; then FFOX_H="994" else # Default to the floss height, for no particular reason. FFOX_H="1031" fi elif [ "${ROOT_W}x${ROOT_H}" = "1366x768" ]; then # klen native screen TXH="053" O_X="200" R_X="876" L_X="006" B_Y="409" T_Y="033" C_X="438" C_Y="230" FFOX_W="1354" FFOX_H="0727" elif [ "${ROOT_W}x${ROOT_H}" = "1280x800" ]; then # klib native screen TXH="055" O_X="200" R_X="788" L_X="010" B_Y="440" T_Y="035" C_X="397" C_Y="262" FFOX_W="1261" FFOX_H="0720" elif [ "${ROOT_W}x${ROOT_H}" = "1024x768" -o "default" = "default" ]; then # home small monitor (mused, et al) TXH="052" O_X="24" R_X="532" L_X="010" B_Y="408" T_Y="035" C_X="269" C_Y="246" FFOX_W="1005" FFOX_H="0688" fi ### Set up all the xterms, unless browser-only. ### if [ "${BROWSER_ONLY}" = "no" ]; then # Launch the xterms. ${XTERM} -geometry ${SXW}x${SXH}+${R_X}+${B_Y} -n w1-br & ${XTERM} -geometry ${SXW}x${SXH}+${R_X}+${B_Y} -n w2-br & ${XTERM} -geometry ${SXW}x${SXH}+${L_X}+${B_Y} -n w2-bl & ${XTERM} -geometry ${SXW}x${SXH}+${L_X}+${T_Y} -n w2-ul & ${XTERM} -geometry ${SXW}x${SXH}+${R_X}+${T_Y} -n w2-ur & ${XTERM} -geometry ${SXW}x${SXH}+${C_X}+${C_Y} -n w3-center & ${XTERM} -geometry ${SXW}x${SXH}+${O_X}+${C_Y} -n w7-center & ${XTERM} -geometry ${SXW}x${TXH}+${R_X}+${T_Y} -n w7-right & ${XTERM} -geometry ${SXW}x${SXH}+${C_X}+${C_Y} -n w8-center & ${XTERM} -geometry ${SXW}x${SXH}+${C_X}+${C_Y} -n w9-center & # Send them to the right workspaces. sleep 1 ${WMCTRL} -r w1-br -t ${WKSPC_1} sleep 1 ${WMCTRL} -r w2-br -t ${WKSPC_2} sleep 1 ${WMCTRL} -r w2-bl -t ${WKSPC_2} sleep 1 ${WMCTRL} -r w2-ul -t ${WKSPC_2} sleep 1 ${WMCTRL} -r w2-ur -t ${WKSPC_2} sleep 1 ${WMCTRL} -r w3-center -t ${WKSPC_3} sleep 1 ${WMCTRL} -r w7-center -t ${WKSPC_7} sleep 1 ${WMCTRL} -r w7-right -t ${WKSPC_7} sleep 1 ${WMCTRL} -r w8-center -t ${WKSPC_8} sleep 1 ${WMCTRL} -r w9-center -t ${WKSPC_9} sleep 1 fi ### Set up Emacs, unless browser-only. ### if [ "${BROWSER_ONLY}" = "no" ]; then /usr/local/bin/emacs --geometry=+${L_X}+${T_Y} & sleep 5 # Emacs takes a while to initialize on startup. ${WMCTRL} -r emacs -t ${WKSPC_1} sleep 1 fi ### Set up Firefox. ### # # Firefox doesn't take standard X Windows options -geometry and -n. # Also, if you ask the Internet how to open Firefoxen on specified # workspaces at a specified position with specified width and height, # you will quickly disappear into a maze of rumor, speculation, and # half-tested solutions. However, the recipe below seems to work. # # (Another solution I played with was preserving a particular session # store -- see r7870 in private repository -- and manually slipping it # into place just before starting Firefox: # # FFOX_SESS_DIR=.mozilla/firefox/erbg2usa.default # cp ${HOME}/${FFOX_SESS_DIR}/sessionstore.js \ # ${HOME}/${FFOX_SESS_DIR}/sessionstore-backups/orig-sessionstore.js # cp ${HOME}/private/${FFOX_SESS_DIR}/`hostname -s`-sessionstore.js \ # ${HOME}/${FFOX_SESS_DIR}/sessionstore.js # # Maybe it would work, but it feels brittle and I'd rather not go there.) for num in 1 2 3; do # Start small enough to avoid triggering full-screen behavior, # from which wmctrl apparently is unable to pull Firefox back. # # The -height and -width options seem to work, although as of # 2018-09-08 the only documentation of them I could find was at # https://www-archive.mozilla.org/docs/command-line-args.html, # not at any of Mozilla's more official-looking sites. firefox -height 200 -width 200 & # Wait long enough for Firefox to do its morning toilette: sleep 3 # Rename the Firefox window according to its destination workspace. # (By default, Firefox seems to always launch with the window name # "Mozilla Firefox". If that ever changes, this code will break. # See https://unix.stackexchange.com/questions/43106/\ # how-to-set-window-size-and-location-of-an-application-on-screen-\ # via-command-line for some ideas on how to deal with that.) ${WMCTRL} -T "FFox ${num}" -r "Mozilla Firefox"; sleep 2; # Send it to the right workspace. ${WMCTRL} -t `dc -e "${num} 2 + p"` -r "FFox ${num}"; sleep 1; # Resize it there. ${WMCTRL} -r "FFox ${num}" -e "0,${L_X},${T_Y},${FFOX_W},${FFOX_H}" done