Showing posts with label led. Show all posts
Showing posts with label led. Show all posts

Saturday, March 17, 2012

Mail Notification

I want to have a visual indicator for when I have unread mail in my inbox. I'm gonna use the SCROLL LOCK LED for this. For controlling the LED I'll use the ledcontrol package which consists of a daemon (ledd) and a few utilities like the ledcontrol command. These enable you to use your LEDs very effectively. There is also a ledcontrol-gtk package which is a graphical frontend for this program. It is useful for learning ledcontrol commands.

This is the code I'm currently using:
;; mail notification
(defvar *unread-mail-count* 0)
(defcommand check-mail () ()
  "Checks an email account and if there is unread mail in the inbox
makes the scroll lock led to slowly blink. Returns the number of
unread messages. Needs the ledcontrol command available and the ledd
daemon pipe file (/var/run/ledd-pipe by default) should be accessible
by normal users."
  (let ((sock (clonsigna:make-imap :host "imap.gmail.com" :port 993 :ssl-p t)))
    (unwind-protect
         (progn
           (clonsigna:cmd-connect sock)
           (clonsigna:cmd-login sock "username" "password")
           (clonsigna:cmd-select sock "inbox")

           ;; cmd-search will return something like (("* SEARCH 2461
           ;; 2465 2471")) The numbers are message ids, that's why we
           ;; split the string by spaces, count the parts and then
           ;; subtract two for the "* SEARCH" part
           (setf *unread-mail-count*
                 ( - (length
                      (cl-utilities:split-sequence
                       #\Space
                       (first
                        (clonsigna:cmd-search sock :criteria "UnSeen"))))
                     2))

           ;; if there is unread mail, have the scroll lock led blink
           ;; for one second every five seconds
           (if (zerop *unread-mail-count*)
               (run-shell-command "ledcontrol set s9 off")
               (run-shell-command "ledcontrol set s9 dutycycle 5000 1 100 20"))

           *unread-mail-count*)
      (clonsigna:cmd-close sock))))

(defvar *mail-notification-timer* (run-with-timer 1 120 'check-mail))
The check-mail function uses the clonsigna package (for IMAP).  When there is unread mail, it sets the SCROLL LOCK LED to blink for one second every five seconds. It also sets the global variable *unread-mail-count* to the number of unread messages.

StumpWM's run-with-timer function is used to have the function called every two minutes.

This simplistic approach is far from perfect. One major problem for me is that if I go to my mail program and read all the unread messages, it will take a while for the LED to stop blinking.

Another problem is that this is not how email clients show the unread mail notifications. They show the number of new unread messages. That is, if there are five unread messages, and you switch to your mail client but don't read them, the notification disappears. Next time when say, two new messages arrive, you are only notified of two new messages, not seven.

The obvious way to do this is to write a plugin for my mail client (currently Thunderbird), but I'm afraid I don't have the time (and interest) to read up on how I can do that, so I'll stick with this for now.

Wednesday, March 14, 2012

Keyboard Layouts

Today I decided to tackle the problem of keyboard. I normally have two keyboard layouts, one for US English (the default) and one for Persian. I had heard that setxkbmap can help me do this. With some digging I found running this command can help me do that:
$ setxkbmap "pc+us+ir:2+inet(evdev)+group(shifts_toggle)"
I got that line by running setxkbmap -print on my Ubuntu machine. The output had something that seemed to be what I want. And I was right! Running that, I noticed that the changes I've made with xmodmap was reset. Well, all I had to do was to put the line that runs xmodmap after setxkbmap. Like this:
(run-shell-command "setxkbmap \"pc+us+ir:2+inet(evdev)+group(shifts_toggle)\"")

;; load keyboard map
(run-shell-command "xmodmap ~/.xmodmap")
Except that didn't work. I logged out and logged back in, and I saw my alt and ctrl keys are not where they should be. A couple of times running those commands by C-x C-e from within Emacs gave me an idea what might be wrong. You see, by default, run-shell-command runs the command given to it asynchronously. I needed to make sure setxkbmap is done before I move onto xmodmap. Thankfully, run-shell-command has one last optional argument collect-output-p. It's function is to collect the program output, but in doing so it makes the execution synchronous. So adding that argument fixed the problem:
(run-shell-command "setxkbmap \"pc+us+ir:2+inet(evdev)+group(shifts_toggle)\"" t)
Voila! Now I can switch layouts like before. Of course, I have no visual indication of the current layout anymore. I'm not 100% sure I want that. I might choose to work out a way for caps lock or scroll lock LED to do that for me later.

UPDATE: If you want a visual indicator for the current keyboard layout, you can change the setxkbmap invokation to something like this:
(run-shell-command
 "setxkbmap \"pc+us+ir:2+inet(evdev)+group(shifts_toggle)\" -option grp_led:caps"
 t)
This makes the CAPS LOCK led to turn on when an alternate keyboard layout is active.

Better Console Font (in Arch)

In order to use Terminus as my console (tty) font, I simply installed the package terminus-font with pacman (includes both console and X11 ...