# mouse_backend.py - backend code for mouse configuration
#
# Copyright (C) 2002, 2003 Red Hat, Inc.
# Copyright (C) 2002, 2003 Brent Fox <bfox@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


import sys
import os

class MouseBackend:

    def writeMouseFile(self, name, gpm, Xemu3, xprotocol, iprotocol, device, doDebug=None):
        if doDebug:
            #If we're in debug mode, just print what we would otherwise write to the file
            print ('FULLNAME="%s"' %name)
            print ('MOUSETYPE="%s"' %gpm)
            print ('XEMU3="%s"' %Xemu3)
            print ('XMOUSETYPE="%s"' %xprotocol)
	    if iprotocol:
	    	print ('IMOUSETYPE="%s"' %iprotocol)
            print ('DEVICE=/dev/mouse')
        else:
            #This is the real thing, so write the file
            fd = open('/etc/sysconfig/mouse', 'w')
            fd.write('FULLNAME="%s"\n' %name)
            fd.write('MOUSETYPE="%s"\n' %gpm)
            fd.write('XEMU3="%s"\n' %Xemu3)
            fd.write('XMOUSETYPE="%s"\n' %xprotocol)
	    if iprotocol:
	    	fd.write('IMOUSETYPE="%s"\n' %iprotocol)
            fd.write('DEVICE=/dev/%s\n' %device)
            fd.close()

    def modifyXconfig(self, xprotocol, device, Xemu3):
	return
        if os.access("/etc/X11/XF86Config", os.W_OK):

            #import xf86config and make the necessary changes
            import xf86config
            (xconfig, xconfigpath) = xf86config.readConfigFile()
            Xmouse = xf86config.getCorePointer(xconfig)

            foundZAxis = None

            for o in Xmouse.options:
                if o.name == "Protocol":
                    o.val = xprotocol
                if o.name == "Device":
                    o.val = "/dev/%s" % device
                if o.name == "Emulate3Buttons":
                    o.val = Xemu3
                if o.name == "ZAxisMapping":
                    foundZAxis = 1

            #If there is no ZAxisMapping line, let's make one.  This will make wheel mice work
            #and non-wheel mice should ignore the line
            if not foundZAxis:
                option = xf86config.XF86Option("ZAxisMapping\" \"4 5")
                Xmouse.options.insert(option)

            #Write out the modified Xconfig file
            xconfig.write(xconfigpath)

    def restartGpm(self):
        os.system("/sbin/service gpm condrestart")
        
    
