Linux

Random gnome-terminal profiles (themes) in Ubuntu

Introduction

Does it ever confuse you if you have too many terminals open at once that look alike? Perhaps you’re just looking to express your personality or tickle your brain. In any case, if you’re using the terminal in ubuntu a lot, you may be interested in having random profiles (colors / settings).

The concept of the method is pretty simple, define a hotkey that launches a script that picks a random profile you’ve created and then open the terminal with that profile as a parameter.

Prerequisites

- Compiz or other hotkey script that will allow you to link to a .sh file
- gnome-terminal
- bash

Getting Started

You can figure out what Profiles you have by going to Edit > Profiles in gnome-terminal. You likely only have one, “Default”, unless you’re already actively using terminal profiles. If you only have one, you should create a few, maybe 3 or 4 right now and play with the colors a bit. Important, don’t include spaces in the names of the profiles

The Script

Create a file in your scripts folder (or create a directory if you don’t have one):

mkdir ~/scripts
touch ~/scripts/gnome-terminal.sh && chmod +x ~/scripts/gnome-terminal.sh
gedit ~/scripts/gnome-terminal.sh

Paste the following replacing the Profile names with those of your own (delimited by spaces) and change the number 4 to that of the :

#!/bin/bash
p=( Default Delta Psi Sigma )
gnome-terminal --window-with-profile ${p[$((RANDOM%${#p[@]}))]}

That ugly looking bit right here is a calculation between a random number (echo RANDOM) and the size of the array (${#p[@]}), “random” % “length of array”. Where % means mod, or remainder of the division. (examples: 7%4 = 3; 6%4 = 2; 5%4 = 1; 4 % 4 = 0; 4 % 3 = 1; 321%321= 0).

To illustrate more, play with this code:

r=$RANDOM; echo $r; echo $((r % 4))

This is how we get a random index value for the array. This value is nested inside the array ${p[r]}, where r is the random, within bounds, array index. That array then corresponds with a name of our profile and we pass it as a paramater to gnome-terminal with “–window-with-profile”. So using my define array above, if the random index were “1″, “Delta” would be echoed. If the index were “0″, Default would be.

The Setup

Now, I use compiz with the commands plugin, setting my “command line 0″ to ~/scripts/./gnome-terminal.sh and my “run command 0″ under my key bindings tab to ctrl+alt+t, but you can associate this script with anything you’d like to kick it off. A shortcut icon for example.

May this inspire you to understand, extend and share.

Python Multi-head X (Nvidia TwinView / Dual Monitor) Development Notes

Preface

The following development notes were written after researching the underlying handling of dual monitors in the X window system on Linux. I’ve included a code snippet that I built to help demonstrate behavior and create a proof of concept to show I can determine which monitor a window is on using only python and no statically set coordinates.

Introduction

I mentioned in my previous post that I’m using an nVidia video card with “TwinView” software that outputs my video as if it were one screen, which it technically is, one X screen. This means that the distinction between monitors is not mapped in the X tree, it’s handled by the window manager. Unlike Xinerama, which has an x session per monitor and stitches them together. Xinerama, however, has is being deprecated in favor of RandR but regardless, TwinView is my choice and is not an option for me to change to.

With all of that said, the decision to bridge python to c that interfaces with compiz is a deadend and would be better implemented based on the new 0.9.0 C++ api. It would be nice to be able to return a list of windows from a “monitor” object. However, that’s beyond my current scope. I was able to whip up some python that to show that implementing the monitor management in python using the gtk module isn’t /that/ hacky. I emphasize because I read some posts that claimed window decorations could be an issue in accurate calculations.

Some Code

Read the comment on line 2. Learn more about pygtk here

#!/usr/bin/python
# Print some information about the X environment, the monitor setup, currently active window and cursor position
import gtk.gdk

screen = gtk.gdk.screen_get_default()
print "X default screen size: %d x %d" % (screen.get_width(), screen.get_height())
print "xid of root window: %d" % screen.get_root_window().xid

monitors = int(screen.get_n_monitors())
print "== %d monitors ==" % monitors
for m in range(0, monitors):
    print " - geometry of monitor %d: %s" % (m, screen.get_monitor_geometry(m))

window = screen.get_active_window()
win_x, win_y, win_w, win_h, win_bit_depth = window.get_geometry()
print "active window on monitor: %d" % screen.get_monitor_at_point((win_x+(win_w/2)),(win_y+(win_h/2)))
print "window geometry (x,y,w,h): %d, %d, %d, %d" % (win_x,win_y,win_w,win_h)

display = gtk.gdk.display_get_default()
pointer = display.get_pointer()
print "cursor position (x, y): %d, %d" % (pointer[1], pointer[2])
print "cursor on monitor: %d" % screen.get_monitor_at_point(pointer[1],pointer[2])

thanks to those in #compiz-dev and #python on freenode who helped me come around to create this snippet. I hope it will help others looking to develop for multi-head setups in Linux. Please let me know if I missed anything or did something incorrectly, this is new territory for me.

compiz_comparison_chart

Compiz 0.9.0 Released – Completely Rewritten in C++

I was doing some in depth research / code hacking regarding the support of multi-headed output (dual monitors) on Linux. I won’t get into details but my video is being output to my monitors as “one screen” with a virtual distinctions handled by the window manager. Because of this, figuring out which of the monitors you are on isn’t as straight forward as you might think. Originally I was looking for a way to access the c functions in compiz through python but that point is now moot (likely for the better).

The first unstable release of the Compiz 0.9 series, completely rewritten in C++. As said, this “brings a whole new developer API, splits rendering into plugins, switches the buildsystem from automake to cmake and brings minor functionality improvements. This release represents the first developer and tester preview of what will eventually make the 0.10.x stable series. Please note that as such, it is not yet ready for general use as there are a number of known ssues, regressions and incomplete functionality.”

Here is a SLIGHTLY DATED graph I got from Santiance.com.

This is a really interesting turning point for the project and I’m glad I came across this while doing my research for multi-head handling in compiz. Knowing where the future lies could drastically change where I put my efforts in developing to support them.