SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-06-30 17:23:43

IamFuzzles
Member
Registered: 2013-02-16
Posts: 32

[Solved: sort-of] Automatically detect second monitor

I'm sure there's a way to do this, but I have no idea where to really start. Whenever I plug in my second monitor to my laptop I have to use arandr to activate it, then deactivate it when I unplug. Surely there's an automated way? Thanks in advance

Last edited by IamFuzzles (2013-06-30 21:10:55)

Offline

Help fund CrunchBang, donate to the project!

#2 2013-06-30 17:55:00

th3pun15h3r
#! Die Hard
Registered: 2011-11-22
Posts: 564

Re: [Solved: sort-of] Automatically detect second monitor

Is your video card on your laptop amd or nvidia?  If so load the proprietary drivers then it will automatically detect monitors and set them up as separate with native resolutions.  Otherwise you can do this:

Open arandr and activate your monitors and set your resolutions.  Then save the configuration (ex. default.sh).  Then add it as the first thing you load up in your autostart.sh file like so:

##Dual Monitor Configuration
sh -c '~/.screenlayout/./default.sh' &

Now every time you turn on your computer it will automatically load your configuration.

Offline

#3 2013-06-30 18:05:22

IamFuzzles
Member
Registered: 2013-02-16
Posts: 32

Re: [Solved: sort-of] Automatically detect second monitor

Fantastic idea, thank you! Question though, is there anyway to get this to run when my laptop is already on? Something that will sit and wait for me to plug in the monitor, then run the script?

EDIT: Instead I decided to make a script to execute manually. I'll include it below, let me know what you think if you can, thank you!

#!/bin/sh
#use xrandr to see what display is connected
#grep the display I'm interested in, then awk the relevant field

vgaActiveStatus=` xrandr -q | grep 'VGA1' | awk ' { print $2 }'`

#if the display is connected, run the multimonitor script to activate the second monitor
#otherwise run the default script to deactivate it
#For the scripts I simply used arandr to save the settings once I got it the way I wanted

if [ "$vgaActiveStatus" = "connected" ]
then
	~/.screenlayout/./multimonitor.sh
else
	~/.screenlayout/./default.sh
fi

Last edited by IamFuzzles (2013-06-30 21:10:21)

Offline

#4 2013-07-01 19:43:39

Alefa
Member
From: Freiburg, Germany
Registered: 2013-03-29
Posts: 42

Re: [Solved: sort-of] Automatically detect second monitor

I use a similar script to switch my laptop screen off and the external monitor on when the latter is connected. This is the code:

#!/bin/bash

myvar="$(xrandr -q)"
if [[ $myvar == *"HDMI3 connected"* ]]; then
    xrandr --output LVDS1 --off --output HDMI3 --mode 1920x1200
else
    xrandr --output LVDS1 --auto
fi

As you can see, it uses xrandr to change the monitor settings. I've added this script to autostart. Then I have another script to switch between internal and external monitor:

#!/bin/bash
# Toggles between laptop and external display devices. Sets laptop screen if external device goes missing.
# From http://www.pclinuxos.com/forum/index.php?topic=111155.0

xStatus=`xrandr`
connectedOutputs=$(echo "$xStatus" | grep " connected" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
disconnectedOutputs=$(echo "$xStatus" | grep " disconnected" | sed -e "s/\([A-Z0-9]\+\) disconnected.*/\1/")
activeOutput=$(echo "$xStatus" | grep -e " connected [^(]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
connectionCount=$(echo $connectedOutputs | wc -w)
command="xrandr "

if [[ "$connectionCount" -gt 1 ]]; then
  for display in $connectedOutputs; do
    if [[ $display = $activeOutput ]]; then
      command+="--output $display --off "
    else
      command+="--output $display --auto "
    fi
  done
else
  command+="--output $connectedOutputs --auto "
  for display in $disconnectedOutputs; do
    command+="--output $display --off "
  done
fi

$command

I found this script on the PCLinuxOS forums (never used that distro, but the script has been very useful). I've bound it to a keyboard shortcut. Very convenient if you're using a docking station with an external monitor at work, and only the laptop screen at home. You might have to modify it a bit depending on the names of your displays and whether or not you want to switch your laptop screen off when using the external.

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo