Skip to content

Commit

Permalink
Keyboard LED visualizations!
Browse files Browse the repository at this point in the history
  • Loading branch information
flithm committed May 10, 2007
1 parent 4c41fa4 commit ab31f99
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 4 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
TODO:

- internal script "restart"
- macro recorder
3 changes: 2 additions & 1 deletion gizmod/GizmoDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,12 @@ BOOST_PYTHON_MODULE(GizmoDaemon) {
.def("updateProcessTree", &Processes::updateProcessTree)
.staticmethod("updateProcessTree")
;

/// X11FocusWatcher Python Class Export
class_<X11FocusWatcher>("X11FocusWatcher")
.def("isApplicationRunning", &X11FocusWatcher::isApplicationRunning)
;


/// GizmoUtils Python Class Export
class_<GizmoUtils>("GizmoUtils")
Expand Down
2 changes: 1 addition & 1 deletion gizmod/GizmoDaemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
class GizmoDaemon :
public H::FileEventWatcher,
private H::SignalHandler,
public X11FocusWatcher,
public X11FocusWatcher,
public Alsa,
public Processes,
public CPUUsage,
Expand Down
2 changes: 0 additions & 2 deletions libGizmod/X11FocusWatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <string>
#include <boost/tuple/tuple.hpp>
#include <boost/archive/text_oarchive.hpp>
Expand Down
191 changes: 191 additions & 0 deletions scripts/modules.d/001-KeyboardLED-Visualizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#***
#*********************************************************************
#*************************************************************************
#***
#*** GizmoDaemon Config Script
#*** Keyboard Visualization config
#***
#*****************************************
#*****************************************
#***

############################
# Imports
##########################

from GizmoDaemon import *
import sys

ENABLED = True
VERSION_NEEDED = 3.0
INTERRUPT_LENGTH = 10
LEDS = [0, 1, 2]

############################
# Visualization Class definition
##########################

class VisualizationType:
"""
Types of visualizations
"""

CPUUsage = "CPUUsage"
SoundVisualization = "SoundVisualization"
Volume = "Volume"

############################
# KeyboardVisualizer Class definition
##########################

class KeyboardVisualizer:
"""
Keyboard Visualization Handler
"""

############################
# Public Functions
##########################

def applyVisualization(self):
"""
Apply the current visualization type to the Keyboard's LED
"""

if self.Visualization == VisualizationType.Volume:
self.__applyVisualizationVolume()

def onEvent(self, Event, Gizmo = None):
"""
See GizmodDispatcher.onEvent documention for an explanation of this function
"""

# take care of the interrupt count
if Event.Class == GizmoEventClass.CPUUsage:
if self.InterruptCount:
self.InterruptCount -= 1
if self.SoundVisCount:
self.SoundVisCount -= 1
if not self.SoundVisCount:
self.Visualization = self.LastVis

# take care of the interrupt count
if Event.Class == GizmoEventClass.SoundVisualization:
if Event.Type == SoundVisualizationEventType.Connect:
self.LastVis = self.Visualization
self.Visualization = VisualizationType.SoundVisualization
self.SoundVisCount = INTERRUPT_LENGTH

# check for mixer events
if (self.Visualization == VisualizationType.Volume or self.VolumeInterruptsOthers) \
and Event.Class == GizmoEventClass.SoundCard \
and Event.Type == AlsaEventType.MixerElementChange:
# if we're an interruption, mark the occasion
self.InterruptCount = INTERRUPT_LENGTH

# check for volume changed
if Event.VolumePlaybackChanged \
and Gizmod.DefaultMixerVolume \
and Event.Mixer.Name == Gizmod.DefaultMixerVolume.Name:
self.__applyVisualizationVolume()

# check for switch changed
if Event.SwitchPlaybackChanged \
and Gizmod.DefaultMixerSwitch \
and Event.Mixer.Name == Gizmod.DefaultMixerSwitch.Name:
self.__applyVisualizationVolume()

# check for CPUUsage events
elif self.Visualization == VisualizationType.CPUUsage \
and Event.Class == GizmoEventClass.CPUUsage\
and not (Gizmod.DefaultMixerSwitch and (not Gizmod.DefaultMixerSwitch.SwitchPlayback)):
if not self.InterruptCount:
self.__applyVisualizationCPUUsage(Event)

# check for Sound Visualization events
elif self.Visualization == VisualizationType.SoundVisualization \
and Event.Class == GizmoEventClass.SoundVisualization \
and not (Gizmod.DefaultMixerSwitch and (not Gizmod.DefaultMixerSwitch.SwitchPlayback)):
if not self.InterruptCount:
self.__applyVisualizationSound(Event)

return False

############################
# Private Functions
##########################

def __setLEDsPercent(self, Percent):
"""
Set the keyboard LEDs to a percentage
"""

CurVal = 0
for i in range(len(LEDS)):
if Percent >= CurVal and Percent >= 5.0:
Gizmod.Keyboards[0].createEvent(GizmoEventType.EV_LED, i, 1)
else:
Gizmod.Keyboards[0].createEvent(GizmoEventType.EV_LED, i, 0)
CurVal += self.RangePerLED

def __applyVisualizationCPUUsage(self, Event):
"""
Set the Keyboards' LEDs to the current system CPU Usage
"""

self.__setLEDsPercent(Event.getCPUUsageAvg(0))

def __applyVisualizationVolume(self):
"""
Set the Keyboards' LEDs to the Default playback volume mixer's level
"""

# make sure there's a mixer available
if not Gizmod.DefaultMixerVolume:
return

# update the Keyboards' LEDs
if Gizmod.DefaultMixerSwitch.SwitchPlayback:
# if not muted set LED to volume level
self.__setLEDsPercent(Gizmod.DefaultMixerVolume.VolumePlaybackPercent)
else:
# if muted pulse the led
for i in range(len(LEDS)):
Gizmod.Keyboards[0].createEvent(GizmoEventType.EV_LED, i, 0)

def __applyVisualizationSound(self, Event):
"""
Set the Keyboards' LEDs to the sound level
"""

self.__setLEDsPercent(Event.VUCombined * 100.0)

def __init__(self):
"""
Default Constructor
"""

# print informative text
Gizmod.printNiceScriptInit(1, self.__class__.__name__, self.__class__.__doc__, str(len(Gizmod.Keyboards)) + " Keyboards")

# initialize member variables
self.Visualization = VisualizationType.CPUUsage # Current LED visualizer
self.LastVis = self.Visualization # Last known visualizer
self.VolumeInterruptsOthers = True # Set to True if volume changes should interrupt other visualizations
self.InterruptCount = 0 # Length of time to remain interrupted
self.SoundVisCount = 0 # Amarok is buggy and doesn't notify on exit, so this is the workaround
self.RangePerLED = 100.0 / float(len(LEDS))

# apply the initial visualization
self.applyVisualization()

############################
# KeyboardVisualization class end
##########################

# register the user script
if ENABLED:
if not Gizmod.checkVersion(VERSION_NEEDED, False):
Gizmod.printNiceScriptInit(1, " * KeyboardVisualizer", "NOT LOADED", "Version Needed: " + str(VERSION_NEEDED))
else:
Gizmod.Dispatcher.userScripts.append(KeyboardVisualizer())

0 comments on commit ab31f99

Please sign in to comment.