Skip to content

Instantly share code, notes, and snippets.

@efruchter
Last active December 31, 2015 02:29
Show Gist options
  • Save efruchter/7921084 to your computer and use it in GitHub Desktop.
Save efruchter/7921084 to your computer and use it in GitHub Desktop.
Rotate the screen using this GTK taskbar icon
#!/usr/bin/python
# -*- coding: utf-8 -*-
# [SNIPPET_NAME: Screen Rotate]
# [SNIPPET_DESCRIPTION: Shows a widget to rotate screen]
# [SNIPPET_AUTHOR: Eric Fruchter <[email protected]>]
# [SNIPPET_LICENSE: GPL]
#
import gtk
import subprocess
import os
GET_ORIENT_COMMAND = "xrandr -q --verbose|grep LVDS1|cut -d ' ' -f6"
SET_ORIENT_COMMAND = lambda o : "xrandr -o " + o
ORIENTATIONS = ['normal', 'left', 'inverted', 'right']
NORMAL = 'normal'
TABLET = 'right'
class RotateScreenWidget:
def __init__(self):
self.tray = gtk.StatusIcon()
self.tray.set_from_stock(gtk.STOCK_REFRESH)
self.tray.connect('button_press_event', self.on_right_click)
self.tray.set_tooltip(('Screen Rotate'))
def on_right_click(self, widget, event):
if event.button == 1:
self.make_menu(event)
def make_menu(self, event):
menu = gtk.Menu()
# Normal
rotMenu = gtk.MenuItem("Normal")
rotMenu.show()
menu.append(rotMenu)
rotMenu.connect('activate', self.default_orientation)
# Tablet
rotMenu = gtk.MenuItem("Tablet")
rotMenu.show()
menu.append(rotMenu)
rotMenu.connect('activate', self.tablet_orientation)
# Rotate
rotMenu = gtk.MenuItem("Clockwise")
rotMenu.show()
menu.append(rotMenu)
rotMenu.connect('activate', self.rotate)
# Divider
div = gtk.SeparatorMenuItem()
div.show()
menu.append(div)
# show about dialog
about = gtk.MenuItem("About")
about.show()
menu.append(about)
about.connect('activate', self.show_about_dialog)
# add quit item
quit = gtk.MenuItem("Quit")
quit.show()
menu.append(quit)
quit.connect('activate', gtk.main_quit)
menu.popup(None, None, gtk.status_icon_position_menu,
event.button, event.time, self.tray)
def get_orientation(self):
proc = subprocess.Popen([GET_ORIENT_COMMAND], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
return out.replace('\n', '')
def rotate(self, widget=None):
nrot = (ORIENTATIONS.index(self.get_orientation()) + 1) % len(ORIENTATIONS)
self.set_rotate(ORIENTATIONS[nrot])
def set_rotate(self, orientation):
os.system('xrandr -o ' + orientation)
def default_orientation(self, widget):
self.set_rotate(NORMAL)
def tablet_orientation(self, widget):
self.set_rotate(TABLET)
def show_about_dialog(self, widget):
about_dialog = gtk.AboutDialog()
about_dialog.set_destroy_with_parent (True)
about_dialog.set_icon_name ("Rotate Screen")
about_dialog.set_name('Rotate Screen')
about_dialog.set_version('1.0')
about_dialog.set_copyright("(C) 2013 Eric Fruchter")
about_dialog.set_comments(("Program to flip the screen using xrandr"))
about_dialog.set_authors(['Eric Fruchter <[email protected]>'])
about_dialog.run()
about_dialog.destroy()
if __name__ == "__main__":
RotateScreenWidget()
gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment