from libqtile import bar, hook, layout, widget
from libqtile.command import lazy
from libqtile.config import Click, Drag, Group, Key, Screen

import subprocess
import os


@hook.subscribe.startup_once
def autostart():
    home = os.path.expanduser('~/.config/qtile/autostart.sh')
    subprocess.call([home])

@hook.subscribe.startup
def dbus_register():
        x = os.environ['DESKTOP_AUTOSTART_ID']
        subprocess.Popen(['dbus-send',
                          '--session',
                          '--print-reply=string',
                          '--dest=org.gnome.SessionManager',
                          '/org/gnome/SessionManager',
                          'org.gnome.SessionManager.RegisterClient',
                          'string:qtile',
                          'string:' + x])

wmname = 'qtile'
mod = 'mod4'

# Key bindings
keys = [
# multiple screens
    Key([mod], "z", lazy.to_screen(0)),
    Key([mod], "e", lazy.to_screen(1)),
    
# Window manager controls
    Key([mod, 'control'], 'r', lazy.restart()),
    Key([mod, 'control'], 'q', lazy.shutdown()),
    Key([mod], 'r', lazy.spawncmd()),
    Key([mod], 'Return', lazy.spawn('terminator')),
    Key([mod], 'w',      lazy.window.kill()),

    #Key([mod], 'Tab', lazy.layout.next()),
    Key([mod], 'Left', lazy.screen.prevgroup()),
    Key([mod], 'Right', lazy.screen.nextgroup()),

    # Layout modification
    Key([mod, 'control'], 'space', lazy.layout.toggle_split()),
    #Key([mod, 'control'], 'space', lazy.window.toggle_floating()),
    # Switch between windows in current stack pane
    Key([mod], 'k', lazy.layout.down()),
    Key([mod], 'j', lazy.layout.up()),

    # Move windows up or down in current stack
    Key([mod, 'control'], 'k', lazy.layout.shuffle_down()),
    Key([mod, 'control'], 'j', lazy.layout.shuffle_up()),

    # Switch window focus to other pane(s) of stack
    Key([mod], 'space', lazy.layout.next()),

    # Toggle between different layouts as defined below
    Key([mod], 'Tab',    lazy.next_layout()),

    Key([], "XF86AudioRaiseVolume",
                    lazy.spawn("amixer sset Master 5%+")),
    Key([], "XF86AudioLowerVolume",
                lazy.spawn("amixer sset Master 5%-")),
]

# Mouse bindings and options
mouse = (
    Drag([mod], 'Button1', lazy.window.set_position_floating(),
        start=lazy.window.get_position()),
    Drag([mod], 'Button3', lazy.window.set_size_floating(),
        start=lazy.window.get_size()),
)

bring_front_click = True
cursor_warp = False
follow_mouse_focus = False
autosplit = True

# Groups
groups = [
    Group('a'),
    Group('s'),
    Group('d'),
    Group('f'),
    Group('u'),
    Group('i'),
    Group('o'),
    Group('p'),
]
for i in groups:
    # mod + letter of group = switch to group
    keys.append(Key([mod], i.name, lazy.group[i.name].toscreen()))

    # mod + shift + letter of group = switch to & move focused window to group
    keys.append(Key([mod, 'shift'], i.name, lazy.window.togroup(i.name)))

dgroups_key_binder = None
dgroups_app_rules = []

# Layouts
layouts = [
    layout.Max(),
#    layout.Stack(num_stacks=2),
    layout.Tile(),
    layout.RatioTile(),
    layout.Matrix(),
]

floating_layout = layout.Floating()

widget_defaults = dict(
#    font='Monospace',
    font='DejaVu Sans Mono',
#    font='Andika',
    fontsize=11,
    padding=3,
    )

# Screens and widget options
screens = [
    Screen(
        top=bar.Bar(
            widgets=[
                widget.GroupBox(
                    highlight_method='block',
                    inactive='999999',
		    fontsize=11
                ),
                widget.Prompt(),
                widget.Sep(),
                widget.WindowName(),
                widget.Sep(),
                widget.CurrentLayout(),
                widget.Net(interface='eth0'),
                widget.Systray(),
                widget.Volume(),
                widget.Clock(format='%a %d %b %H:%M:%S %p'), 
            ],
            size=24,
            background='#000000',
        ),
    ),
        Screen(
           top=bar.Bar(
              widgets=[
                widget.GroupBox(
                    highlight_method='block',
                    inactive='999999',
		    fontsize=11
                ),
                widget.Sep(),
                widget.WindowName(),
               ],
	    size=24,
            background='#000000',
        ),
    ),
]