GLaDOS invades Ubuntu!

In this post, I will be including text in “quotes”. This is to indicate a special use. “Text in quotes” should be typed as directed. Formating such as “Ctrl+V” is used to indicate keyboard shortcuts. Formating such as “Edit>Undo” is used for menus. Keep in mind that everything is CASE SENSITIVE, meaning “cat” is not the same thing as “CAT”.

Thanks to BlueProximity, my computer now talks to me when my phone leaves my computer, or when it gets close to my computer. This has been the main reason I carry my phone today. Here’s how it works:

BlueProximity can be downloaded through the Ubuntu Software Center. It tracks how far away your phone is, and locks your computer if you go too far away, then unlocks your computer if you get close enough. It also lets you set such things as how far away is too far, how far away is too close, and what program to run to lock or unlock your computer.

Focus on the top two.

That last part is important! You can have it run any program, not just lock or unlock your computer! But, I don’t just want to lock my computer, I also want to play a random sound file from a folder, and maybe control a running application. Here is where shell scripts come in handy.

In Linux, you can save a text file anywhere on your computer as an executable text file. This means that the document can be edited as text, or run as a program. The general format is as follows: each line of the document acts as it would in a terminal. For every application, there is a line of text that means the same thing in the terminal. For example, the screensaver and locker application is called “gnome-screensaver”. These commands can also hold options, or in some cases, other commands that will accept the options. There is a simple way to check for these options and commands.

Most Ubuntu users access their programs through the main menu icon in the upper-left hand corner of their screen. Right clicking on it allows you to edit the menus, or at least navigate to an application in a familiar way.

Right click Ubuntu logo, then click here.

Once you’ve highlighted a program, click edit and remember the text in the field “command”, or copy it straight into a text file. Please note that if there is a space, you can almost always delete the space and everything after it. Anything after the space is an option, possibly looking for a specific file after it. There are some exceptions, and I am about to go over general rules for finding them.

At this point in time, you will need to open a terminal of your choice. For simplicity, I am going to open the default terminal located under accessories in the main menu. Advanced users can press “Ctrl+Alt+T” or “Alt+F2″ followed by the terminal of their choice, like “bash”, or “gnome-terminal”. Now type “man ” followed by the command of your choice in your terminal. “man” is the manual application for most Linux applications. In this case, the application you type is accepted as an argument, specifying which program manual you want to read. Let’s start with “gnome-screensaver”, the default Ubuntu screensaver (Ubuntu is based on Gnome).

Typing “man gnome-screensaver” into your terminal, or copying the text in quotes and pasting it into terminal through “edit>paste” or “Ctrl+Shift+V” and pressing enter will result in something like this:

the gnome-screensaver man page

To navigate this document, use the arrow keys, page up/down, and home/end on your keyboard to scroll through the document. To exit, type “:q”. (NOTE: 11.10 uses “q” instead, and displays a lovely reminder about this.)

Toward the bottom of this document, you will see something titled “SEE ALSO”. This usually contains a list of related programs, or a list of other common options, in this case GTK options. However, some applications, like the Gnome screensaver, the X screensaver, and Rhythmbox, use a different application for command line controls. As the default BlueProximity settings indicate, we actually want to read the gnome-screensaver-command man page.

Typing “man gnome-screensaver-command” into terminal should bring up something more useful.

the gnome-screensaver-command man page

The description mention “gnome-screensaver”, just as expected. At the bottom of this screen, we see the -l option that was shown in BlueProximity, but its description is cut off. Scrolling down will fix this.

the gnome-screensaver-command man page continued

Now we can see both commands of interest. “gnome-screensaver-command -l” locks the computer, and ”gnome-screensaver-command -d” unlocks the computer. (I’m ignoring the poke command, which didn’t seem to affect anything.) I recommend that at this point in time you find a folder on your computer that you won’t see everyday. and create a folder with the name “executable_text” or similar, you decide what you actually call it. I also recommend that the folder doesn’t contain any spaces (underscores and dashes are fine), as they are a pain to reference in terminal. Within this new folder, you will write your first two executable text files.

First create a text file, one whose name ends in “.txt”, not “.doc” or “.odt”. Because this doesn’t involve any sort of formating, you can use “Accessories>Text Editor” for this. Save the first document as “unlock.txt”, as we will be working on a program to unlock your computer first. The first line should read “gnome-screensaver-command -d” so it actually unlocks your computer.

To make this application play sound files, I just had to tell a music player to play a sound file in one of the lines of the text file. while the default music player, “totem”, can play files, I didn’t want to close anything I was playing in totem, or leave a window open whenever I approached my computer, so I chose “mplayer”, a music player for terminal. The line I added was “mplayer ” and a file name, with the full path, which you can probably figure out if I tell you your music folder is in “/home/(your username)/Music”.

To make everything a little more random, I copied the code from Melanie Gross’s program. I made several changes, which can be seen in the final code below. Remember to make a similar program for locking!

You may also want to set an application to do something while you’re away. I set BOINC to work on such projects as World Community Grid and SETI. To set it to automatically run faster while I was away, I checked the man for “boinc”, which showed “boinccmd” under “SEE ALSO”. I then checked for anything useful under the options for “boinccmd”. By typing “boinccmd –set_run_mode always” in the line before “gnome-screensaver-command -d”, I was able to set this to run as fast as it wanted. To reverse the effects, I typed “boinccmd –set_run_mode auto” in the line before “gnome-screensaver-command -d”.

The final code for unlock.txt is [

#set BOINC to normal priority
boinccmd --set_run_mode auto
#unlock screen
gnome-screensaver-command -d
#pick a random sound file
soundsfolder="/home/tim/Public/Files/sound/computer/Sounds/BlueProximity/unlock"
echo "sounds are in" $soundsfolder
cd $soundsfolder
pwd
echo "working in" $soundsfolder
files=(././*)
N=${#files[@]}
echo “found” $N “files”
((N=RANDOM%N))
echo “picked file” $N
randomfile=${files[$N]}
echo $randomfile
randomfile=cut –characters=5- $randomfile
echo “file name is” $randomfile
#play it
mplayer “$soundsfolder/$randomfile” -msglevel all=-1

]. The final code for lock.txt is [

#set BOINC to high priority
boinccmd --set_run_mode always
#unlock screen
gnome-screensaver-command -l
#pick a random sound file
soundsfolder="/home/tim/Public/Files/sound/computer/Sounds/BlueProximity/lock"
echo "sounds are in" $soundsfolder
cd $soundsfolder
pwd
echo "working in" $soundsfolder
files=(././*)
N=${#files[@]}
echo “found” $N “files”
((N=RANDOM%N))
echo “picked file” $N
randomfile=${files[$N]}
echo $randomfile
randomfile=cut –characters=5- $randomfile
echo “file name is” $randomfile
#play it
mplayer “$soundsfolder/$randomfile” -msglevel all=-1

]

When working with these, it might be easiest to use the default text editor, and select “View>Highlight Modes>Scripts>sh”, since this is technically a shell script, meaning you could also save this as a sh file. In any event, when you are done editing a file remember to right-click the file, click “properties”, go to the “Permissions” tab, and give the file executable permissions! Failure to do so will make the file an ordinary text file, and it won’t run as a program. Once the permission has been changed, you can double-click the file and tell it to run. If you try the lock command, you will have to enter your password.

Now you must tell BlueProximity to run the files.

Focus on the top two.

Change the top line to your new lock command, remembering to type the address. Same idea with the bottom line.

Much better.

For some reason, some of the commands used in my program won’t run unless they run through terminal. To fix this, I simply run the files through bash, the terminal. Now my computer can play sound from the folders you specified earlier.

Now you might be wondering about how GLaDOS fits in all of this. My lock folder and my unlock folder are FILLED with sound files from Portal and Portal 2, including the turrets, GLaDOS, Wheatley, Cave Johnson, Caroline, and even the announcer. When leaving my computer, I now hear GLaDOS saying “Make no further attempt to leave the testing area.”, or a turret asking “Are you still there?”. They are also set up to talk when I get back, such as GLaDOS welcoming me to the enrichment center. Now that I have shared this fun project I think it’s time to say goodbye for now.

Cave: “Say goodbye Caroline.”

Caroline: “Goodbye Caroline.”

Update #1: I figured out how to always play through the speakers, even if the default is headphones! At the last line of both programs add “ -ao alsa:device=hw=0.0″. The first number is which sound card, the second which output on that sound card. If you have digital headphones, HDMI outputs, or other digital audio outputs, they will count as a sound card, although not your primary sound card, and I could be wrong if you connect such a device to an existing sound card. The second number changes with the output on a sound card, so speakers on a computer may be 0.0, but headphones might be 0.1. I used Audacity, a professional audio editor, to check the correct output. To check, click “View>Toolbars>Device Toolbar”. Then a new toolbar will appear near the bottom, with output devices on the left, input devices on the right. Focus on the options near the top, the ones that end in (hw:#,#). Judging by the name, select a few that feature the company name of your sound card. Import a sound file through “File>Import>Audio”, or “Ctrl+Shift+I”, and play the file, listening for it to play through your desired sound output. Then copy the number from Audacity as “a,b” and paste it into my code as “a.b”. I think there is an easier way to use the option “ -ao alsa:device=hw=0.0″, but this one works for me.

Update #2: For those of you using Ubuntu since 11.04, you cannot see the command for your programs from the dash/launcher. I was still using 10.10 when I made this, but I now know how to fix this problem, at least temporarily. You can install the menu editor from 10.10 and earlier with “Main Menu”, also known as “alacarte”.

And for those of you using Android Phones (and possibly others): If your device doesn’t stay reliably connected to your computer, check the available Bluetooth channels from BlueProximity. On my Droid, Channel 7 is unavailable, but several other channels are open for communication. Each one is responsible for an action, like contacts and audio connections, and your Droid will tell you which one your computer is using from its Bluetooth settings, so pick one you feel comfortable with. For me, it was the sound system. Regardless of your choice, all BlueProximity does is ask “Are you still there?”, to which the reply is either “Yes.” or “…”, except in computer code, so nothing will really be read or changed.

About these ads

About nicknackgus

I have worked with all sorts of stuff. I built a computer from parts, installed some operating systems on it, and set up all sorts of software, and I don't have enough space here to mention any advanced settings in any area. I've also worked in science, multimedia, special effects, and robotics.
This entry was posted in Linux, Portal, Portal 2, Programs, Shell, Ubuntu. Bookmark the permalink.

2 Responses to GLaDOS invades Ubuntu!

  1. Robert Gammon says:

    For those of us new to Ubuntu, Much of what is said here is not doable as the Unity desktop and the Gnome desktop are quite different. They look and feel different and operate in a very different manner as well as the underlying model for the desktops are different. I have not used Gnome, only Unity

    • nicknackgus says:

      Yeah, I know. The program itself still works, more or less, but I need to change the setup guide to make it match post Maverick releases. As I said in my second update, the commands for each application can be found using “main men”, or “alacarte”, which is still in the Software Center. I am also beta testing Precise, but I haven’t tested this in Precise yet either. This post is also messy enough for me consider a new, cleaner post for post-2010 releases. I”m going to post a quick list of changes and fixes/comments below, let me know if I’ve forgotten anything.

      *program commands – can be found in “Main Menu” in the official software center sources. Organization does NOT match Unity organization. Unity does NOT provide commands through right-click or other actions, as far as I can tell. Is there a program that can give the program command with a simple click? Maybe “Compiz Config Settings Manager”? This isn’t recommended for most people, though.
      *Gnome Screensaver – is installed by default, and acts as the default screensaver/locker. Since its settings manager is no longer included with Ubuntu or in the repositories, we are stuck with the default black screen. This seems to be intentional, probably for power-saving reasons, but I’d still like to think of this as a regression. The commands are the same to trigger the screensaver, but setting up a REAL screensaver requires either “xscreensaver”, the Gnome screensaver options program, or some clever code changes. None of these are easy for beginners.
      *(un)lock command changed – the old arguments “-l” and “-d” may still work, but if you check the man pages, these arguments have been replaced. The man pages also say what the new commands are, so check the man page before running these programs.

      Other than that, I have made a few simplifications to the program itself and started merging the lock and unlock programs into one. Feel free to recommend other changes, in the program or post, an I’ll be sure to listen!

      Just keep in mind that I haven’t figured out how to make my computer talk to robots. Yet. Otherwise my four micro-controllers would DEFINITELY be doing something cool right about now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s