Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Wednesday, September 29, 2010

Disable touchpad when USB Mouse connected on Ubuntu (Lucid)

Update: 7/11/2010 Fixed a bug that happened with my laptop. The xinput was reset periodically for some reason.

Update: 2/11/2010 Fixed it a bit to make it works better. Now it don't need fix device name.

I wrote a short script to disable touchpad when mouse attached in Jaunty, which never works for me anymore after Jaunty. The reason is that gconftool and synclient could not correctly set touchpad state (it works for a very short while, until Gnome or X enable it back again). I just found the solution for Lucid (and probably for Maverick). It's

#!/usr/bin/python


import dbus # needed to do anything
from dbus.exceptions import DBusException
import dbus.decorators # needed to receive messages
import dbus.glib # needed to receive messages
from dbus.mainloop.glib import DBusGMainLoop
import gobject # needed to loop & monitor
import os, re, time, sys

global add_action, remove_action, bus, hal_manager

_debug = True
device_added = []
touchpad_enabled = True
device_cap = 'input.mouse'

add_action = 'sleep 1s && xinput --list --short | grep -i touchpad | sed "s/^.*[[:space:]]*id=\\([0-9]*\\)[[:space:]]*.*$/\\1/g" | xargs -I id xinput --set-prop id --type=int --format=8 "Device Enabled" 0'
remove_action = 'xinput --list --short | grep -i touchpad | sed "s/^.*[[:space:]]*id=\\([0-9]*\\)[[:space:]]*.*$/\\1/g" | xargs -I id xinput --set-prop id --type=int --format=8 "Device Enabled" 1'
check_action = 'xinput --list --short | grep -i touchpad | sed "s/^.*[[:space:]]*id=\\([0-9]*\\)[[:space:]]*.*$/\\1/g" | xargs -I id xinput --list-props id | grep -qi "device.*enabled.*:[[:space:]]*0$"'

exclude_devices = ['usb_device_a5c_4503_noserial_if0_logicaldev_input']

def dprint(msg) :
    global _debug
    if _debug :
        sys.stderr.write(msg + '\n')
        sys.stderr.flush()

#@dbus.decorators.explicitly_pass_message
def add_device(*args, **keywords):
    check_and_run_add_action(args[0])

def check_and_run_add_action(device_path) :
    global device_added

    retval = False
    Path = device_path.split('/')
    dprint('device added == %s' % Path)

    if Path[-1] in exclude_devices :
        return retval

    device_obj = bus.get_object('org.freedesktop.Hal', device_path)
    device = dbus.Interface(device_obj, dbus_interface = "org.freedesktop.Hal.Device")
    cap = device.QueryCapability(device_cap)

    dprint('Device capability == %s' % cap)
    try :
        prop = device.GetPropertyString('info.subsystem')
    except DBusException :
        prop = None

    dprint('Property String == %s' % prop)

    if cap and prop and (prop == 'input') :
        device_added.append(Path[-1])
        dprint('Executing add_action')
        os.system(add_action)
        retval = True
    return retval
       
#@dbus.decorators.explicitly_pass_message
def remove_device(*args, **keywords):
    global bus, _debug, device_added

    Path = args[0].split('/')
    dprint('device removed == %s' % Path)

    try :
        if device_added :
            if Path[-1] in device_added :
                device_added.remove(Path[-1])
                dprint('Excuting remove_action')
                os.system(remove_action)
            else :
                # other device removed, run add action again to make sure
                # this workaround a bug in Lucid that device was periodically removed will reset enable flag
                os.system(add_action)
    except :
        dprint('Exception while removing device')
        if _debug :
            import traceback
            traceback.print_exc()

DBusGMainLoop(set_as_default = True)
bus = dbus.SystemBus()  # connect to system bus
hal_manager_obj = bus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
hal_manager = dbus.Interface(hal_manager_obj, 'org.freedesktop.Hal.Manager')

# Add listeners for all devices being added or removed
bus.add_signal_receiver(add_device, 'DeviceAdded', 'org.freedesktop.Hal.Manager',
                        'org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
bus.add_signal_receiver(remove_device, 'DeviceRemoved', 'org.freedesktop.Hal.Manager',
                        'org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')

# Run remove action once to enable touchpad
dprint('Running removaction once')
os.system(remove_action)

# Find mouse first
dprint('Finding mouse first')
udis = hal_manager.FindDeviceByCapability(device_cap)
for udi in udis :
    dprint('Found device == %s' % udi)
    Path = udi.split('/')
    if check_and_run_add_action(udi) :
        break

# monitor
dprint('Start Mainloop')
loop = gobject.MainLoop()
try :
    loop.run()
except SystemExit, e :
    dprint('Got SystemExit exception %s' % e)
    raise e
except Exception, e :
    dprint('Got Exception from the loop')
    if _debug :
        import traceback
        traceback.print_exc()
    loop.quit()
    sys.exit(255)
The trick is to use xinput instead of gconftool/synclient!

Wednesday, September 02, 2009

Disable touchpad when USB Mouse connected on Ubuntu (Jaunty)

Do you have problem with Synaptics touchpad Palm detection? Do you accidentally touch the touchpad while typing? On Windows Vista, there is an option to disable touchpad when USB Mouse connected, but how to do that on Ubuntu? Here's how I 'properly' did it on my Ubuntu 9.04 (Jaunty).
  • Create a Dbus signal handler script using Python. Here's the script
#!/usr/bin/python

global DeviceName, AddAction, RemoveAction, bus, hal_manager

#DeviceName = 'usb_device_15ca_c3_noserial_if0_logicaldev_input'
DevicePattern = r'usb_device.*_input'
DeviceCap = 'input.mouse'
AddAction = '/usr/bin/gconftool --set --type=bool /desktop/gnome/peripherals/mouse/touchpad_enabled false'
RemoveAction = '/usr/bin/gconftool --set --type=bool /desktop/gnome/peripherals/mouse/touchpad_enabled true'

import dbus # needed to do anything
import dbus.decorators # needed to receive messages
import dbus.glib # needed to receive messages
import gobject # needed to loop & monitor
import os # needed to
import re, time

DeviceRe = re.compile(DevicePattern, re.IGNORECASE | re.DOTALL)

#@dbus.decorators.explicitly_pass_message
def add_device(*args, **keywords):
Path = args[0].split('/')
device_obj = bus.get_object('org.freedesktop.Hal', args[0])
device = dbus.Interface(device_obj, dbus_interface = "org.freedesktop.Hal.Device")

cap = device.QueryCapability(DeviceCap)

if cap and DeviceRe.match(Path[-1]) :
os.system(AddAction)

#@dbus.decorators.explicitly_pass_message
def remove_device(*args, **keywords):
Path = args[0].split('/')
try :
device_obj = bus.get_object('org.freedesktop.Hal', args[0])
device = dbus.Interface(device_obj, dbus_interface = "org.freedesktop.Hal.Device")
cap = device.QueryCapability(DeviceCap)
except :
# assume true, since device might be already removed
cap = True

if cap and DeviceRe.match(Path[-1]) : # Device found
os.system(RemoveAction)

bus = dbus.SystemBus() # connect to system bus
hal_manager_obj = bus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
hal_manager = dbus.Interface(hal_manager_obj, 'org.freedesktop.Hal.Manager')

# Add listeners for all devices being added or removed
bus.add_signal_receiver(add_device, 'DeviceAdded', 'org.freedesktop.Hal.Manager',
'org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
bus.add_signal_receiver(remove_device, 'DeviceRemoved', 'org.freedesktop.Hal.Manager',
'org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')

# Run remove action once to enable touchpad
os.system(RemoveAction)
time.sleep(1)

# Find mouse first
udis = hal_manager.FindDeviceByCapability('input.mouse')
for udi in udis :
Path = udi.split('/')
if DeviceRe.match(Path[-1]) : # Check if this is our prefer mouse
os.system(AddAction)
break # no need to keep looking

# monitor
loop = gobject.MainLoop()
loop.run()
  • If you wonder how I got the HAL path of USB mouse, try connecting your mouse and do "lshal", look for "Mouse" and see how your path looks like. Change the path properly if the default one is not match.
  • Add the script to run when log-in by going to System->Start up Applications
  • Log-out and re-Log-in again. That's it.
Another way to do this is to handle it in UDEV. However, I found that GNOME will always try to enable it back that way. Besides, UDEV is run by root, but touchpad setting is per-user. Disabling Touchpad permanently (in Mouse setting) and use synclient to control touchpad is another way to go.

Wednesday, December 10, 2008

Faxing in Ubuntu

Today, I have a mission to send out as many fax as possible to attract customer with my laptop and modem. I don't have much choice, Windows used to have good faxing support but that's gone from Vista (fax only available in Vista Business or Ultimate), so Hardy is my only quick choice now (not mentioning other commercial software for Windows).

efax command line works well and can send fax, but I have some problem on converting postscript printed file into tiffg3, because all page is not being converted, some part is being cropped. And I don't want to use command line all the time. Anyways I will document how to do that here.
  • Have your file ready in PS format, easiest way is to use "print to file" option in Print menu of Oo.O.
  • Use ghostscript to convert from PS to Tiffg3, format accepted by efax program
    gs -q -sDEVICE=tiffg3 -dNOPAUSE -sOutputFile=letter-%03d.tiff invitation_cmu.ps < /dev/null
  • You'll get letter-001.tiff - letter-00x.tiff, counted by number of pages in the PS file. Last, use efax to send it out
    efax -t  file...
I search again and found that gfax might be my helper. Anyways, gfax is not working with my mode, although I told gfax to use efax. I don't sure why, but from quick search it seems that some initialize modem command might not compatible with my modem. And there is no debugging or customize initialize command configuration available. So I try efax-gtk next. And efax-gtk works quite well on my Dell XPS M1330 Modem!
  • efax-gtk can send out single/multiple PS file by itself. I just need to change modem initialize command a bit. The key is
    .... M1L3
    Command in the latter part of Modem Init command dialog box. Also, the modem reset command is not working well, so I replaced it with hang-up command
    H
    Note that all command will always being prefixed with "AT", as normal modem command. Ah... I miss the AT command
  • Next, I don't want to just use efax-gtk. I want to print form OO.O directly to efax-gtk!. That can be easily done. First, add a new printer through system-config-printer GUI utility. Select "New Printer" -> "AppSocket/JetDirect" and specify "localhost" and port "9900" in the diaglog box (Yes, efax-gtk is also a network server)
  • Next printer type is "Generic", and model is "Raw Queue", which means we'll just get PS file from printing. After that, name the printer anything meaningfull.
  • Now open efax-gtk and leave it run in tray, then print directly to the new printer. After a while, a efax-gtk dialog will appear to ask for telephone number. It works!

Sunday, December 07, 2008

Office2007 on my hardy




Just want to mention that the instruction here is really working, even with my Office 2007 Thai Edition (btw, licensed to my organization).

(The transparent floating windows in the middle is Compiz effect on gimp whole screenshot acquire)

Wednesday, September 24, 2008

Windows Vista, Ubuntu Linux, and Media Direct on DELL XPS M1330

My laptop, DELL XPS M1330, was broken two days ago! The machine refused to boot, even the bios screen was not coming up. Thanks to the quick and decent services from Dell, they quickly replace my laptop motherboard the next business day and my laptop is back to work. I think this is the good side of Dell as many suggest, their services. But I wonder why my laptop was broken, although I just purchased it less than a month ago.

Anyways, I just found out that my media direct (small "home" button) stop working. It just goes directly to the same GFXGrub boot screen that I installed for my Ubuntu + Windows dual boot. At first, I thought that this is the doing of replaced motherboard, some internal nvram or something might be changed. So I follow many guides spread in the internet to use "rmbr" command supplied with MediaDirect CD to fix the problem.

The rmbr tool is actually a tool to instruct the machine to boot from a partition when power button is pressed, and boot from another partition when mediadirect button is pressed. The command is like
rmbr DELL 2 4
Where "DELL" is my manufacturer model, 2 is the number of partition for power button, and 4 is number of partition for mediadirect button. I believe what it really does is, modify MBR to boot from partition #2, and put "something" in some place to boot from #4 when mediadirect is pressed. (I think the "something" is actually goes into MBR as well after some experiments below, and also according to many web sites in the internet.)

I expermented a bit with rmbr. It really crashed the whole partition table if the number of partition exceed 4 (aka. use it on non-primary partition). I had to use testdisk to recover my partition. Somehow testdisk recovered the wrong order of my partition, which was because the odd structure of my partition due to MediaDirect partition. But testdisk can also change the type of any partition from logical to primary as well. So now my last partition is MediaDirect although the numbering is 4 (I got 7 partitions).

Because of the lost of partitions, I have to modify my Grub menu.lst, fix Windows using restore DVD supplied with my Notebook (using "Repair" functionality, a small link on the bottom of the screen that show "Install NOW"), then manually fix boot.ini in MediaDirect partition (it was actually FAT32, with partition type set to 0xc to hide it).

After a bit of experments, I found that the culprit is actually GFXGrub. I managed to make everything back to works as it should without grub. However, once GFXGrub was installed, media direct will boot to Grub instead of MediaDirect. So I just add a new menu option to my Grub to boot to MediaDirect. It works now, although not neatly.

I believe that once my laptop can boot to Grub from power button, and boot to MediaDirect directly using mediadirect button. Don't sure if that happened when I use normal Grub, or after I replace it with GFXGrub. Anyways my everydays works are in Vista|Ubuntu, not MediaDirect, so the priority goes to that.

Thursday, May 22, 2008

Getting issuer hash from a root CA

I forgot this all the time
openssl x509 -issuer_hash -noout -in cacert.org.crt