Proof of concept joystick patch

A nice (keyboard based) front end for Arcade / Console game emulators.

Moderator: battlecat

Proof of concept joystick patch

Postby zerodiv » Tue Dec 11, 2007 2:08 am

I wanted to see if I could add joystick support to Wah!Cade, and this is what I came up with. It polls the joystick using pygame, and sends key-press-events to the focused window. It isn't really usable yet, but after adding something like initial key repeat delay, and KEY_RELEASE events, I think it should be usable.

I'll keep working on it, but I'm not that great with python/pygtk so if anyone wants to help or take over, please feel free :-)

Code: Select all
--- win_main.py.orig   2007-12-10 19:33:24.675231312 -0500
+++ win_main.py   2007-12-10 19:36:31.646707641 -0500
@@ -62,6 +62,11 @@
 #set gettext function
 _ = gettext.gettext
 
+#pygame joystick
+import pygame
+pygame.init()
+pygame.joystick.init()
+
 #main window class
 class WinMain(WahCade):
     """Wah!Cade Main Window"""
@@ -288,6 +293,53 @@
             self.winMain.fullscreen()
         #hide splash
         self.splash.destroy()
+       
+        # initialize the joystick and polling
+        self.joystick = pygame.joystick.Joystick(0)
+        self.joystick.init()
+        gobject.timeout_add(100,self.poll_joystick)
+
+    def poll_joystick(self):       
+        joy_keys = [["button",0,"1"],
+                    ["button",1,"2"],
+                    ["axis",1,"Up","Down"],
+                    ["axis",2,"Left","Right"]]
+       
+        # give pygame a chance to do its magic with the joystick
+        pygame.event.pump()
+       
+        # get the focused window or return
+        w = None
+        windows = gtk.window_list_toplevels()
+        for window in windows:
+            if window.is_active():
+                w = window
+        if w == None: return 1
+       
+        # check if any of our defined keys were pressed
+        e_keys= []
+        for k in joy_keys:
+            if k[0] == "button":
+                if self.joystick.get_button(k[1]):
+                    e_keys.append(k[2])
+            if k[0] == "axis":
+                a = self.joystick.get_axis(k[1])
+                if abs(a) > 0.2:
+                    if a < 0:
+                        e_keys.append(k[2])
+                    else:
+                        e_keys.append(k[3])
+       
+        # send key-press events
+        for e_key in e_keys:
+            e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
+            e.state = 0
+            e.keyval = int(gtk.gdk.keyval_from_name(e_key))
+            e.window = w.window
+            e.time = 0
+            w.emit("key-press-event", e)
+
+        return 1
 
     def on_winMain_destroy(self, *args):
         """done, quit the application"""
zerodiv
 
Posts: 8
Joined: Tue Dec 11, 2007 1:23 am

Postby zerodiv » Wed Dec 12, 2007 4:12 am

Well, after adding the key repeat delay stuff, KEY_RELEASE events, and a big splash of ugly, it is very usable. :-)

Code: Select all
--- win_main.py.orig   2007-12-10 19:33:24.675231312 -0500
+++ win_main.py   2007-12-11 22:02:06.799356368 -0500
@@ -62,6 +62,11 @@
 #set gettext function
 _ = gettext.gettext
 
+#pygame joystick
+import pygame
+pygame.init()
+pygame.joystick.init()
+
 #main window class
 class WinMain(WahCade):
     """Wah!Cade Main Window"""
@@ -288,6 +293,87 @@
             self.winMain.fullscreen()
         #hide splash
         self.splash.destroy()
+       
+        # initialize the joystick and polling
+        self.joystick = pygame.joystick.Joystick(0)
+        self.joystick.init()
+        gobject.timeout_add(10,self.poll_joystick)
+        self.joy_keys = {"button_0"   : "1",
+                         "button_1"   : "2",
+                         "axis_1_Neg" : "Up",
+                         "axis_1_Pos" : "Down",
+                         "axis_0_Neg" : "Left",
+                         "axis_0_Pos" : "Right"}
+        self.joy_state = {}
+        for k in self.joy_keys.iterkeys():
+            self.joy_state[k] = 0;
+
+    def poll_joystick(self):
+        initial_repeat_delay = 50
+        repeat_delay = 5
+        # give pygame a chance to do its magic with the joystick
+        pygame.event.pump()
+       
+        # get the focused window or return
+        w = None
+        windows = gtk.window_list_toplevels()
+        for window in windows:
+            if window.is_active():
+                w = window
+        if w == None: return 1
+       
+        # check if any of our defined keys were pressed
+        e_keys= []
+        for k, v in self.joy_keys.iteritems():
+            (type, number) = (k.split("_",1))
+            if type == "axis":
+                (type, number, position) = (k.split("_",2))
+           
+            if type == "button":
+                if self.joystick.get_button(int(number)):
+                    self.joy_state[k]+=1
+                    e_keys.append(k)
+                elif self.joy_state[k] > 0:
+                    self.joy_state[k]=0
+                    e_keys.append(k)
+            if type == "axis":
+                a = self.joystick.get_axis(int(number))
+                if position=="Neg":
+                    if a < -0.5:
+                        self.joy_state[k]+=1
+                        e_keys.append(k)
+                    elif self.joy_state[k] > 0:
+                        self.joy_state[k]=0
+                        e_keys.append(k)
+                elif position=="Pos":
+                    if a > 0.5:
+                        self.joy_state[k]+=1
+                        e_keys.append(k)
+                    elif self.joy_state[k] > 0:
+                        self.joy_state[k]=0
+                        e_keys.append(k)
+                       
+        # send key-press events
+        for e_key in e_keys:
+            if self.joy_state[e_key]==0:
+                e = gtk.gdk.Event(gtk.gdk.KEY_RELEASE)
+                e.state = 0
+                e.keyval = int(gtk.gdk.keyval_from_name(self.joy_keys[e_key]))
+                e.window = w.window
+                e.time = 0
+                w.emit("key-release-event", e)
+            elif self.joy_state[e_key]==1 \
+              or self.joy_state[e_key]==initial_repeat_delay \
+              or (self.joy_state[e_key]>initial_repeat_delay and \
+                  self.joy_state[e_key]%repeat_delay==0):
+                e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
+                e.state = 0
+                e.keyval = int(gtk.gdk.keyval_from_name(self.joy_keys[e_key]))
+                e.window = w.window
+                e.time = 0
+                w.emit("key-press-event", e)
+
+        return 1
 
     def on_winMain_destroy(self, *args):
         """done, quit the application"""
zerodiv
 
Posts: 8
Joined: Tue Dec 11, 2007 1:23 am

Postby zerodiv » Thu Dec 13, 2007 10:05 pm

Is anyone interested in joystick support?
zerodiv
 
Posts: 8
Joined: Tue Dec 11, 2007 1:23 am

Postby SeTTleR » Fri Dec 14, 2007 8:07 pm

Hey, thanks for your work, but I could not try this out because I have no time at the moment. But I'm really interested in this. For now I am using Qjoypad, which works but is not a really good choice, because it maps the Joypadkeys to keys on the keyboard and it does this even if I am in the emulator. I know it's just a matter of configuration, but your solution looks nice so that I can circumvent this :-)
So keep up the working! ;-)
SeTTleR
 
Posts: 35
Joined: Sat Jul 28, 2007 8:36 pm

Postby balki » Sun Dec 16, 2007 11:04 am

Hi,

Sorry for not replying sooner - I'll definitely be getting round to trying this out soon :roll: I'll try and include this in the next release or two...

Cheers,
Andy
User avatar
balki
Site Admin
 
Posts: 425
Joined: Thu Jan 01, 1970 1:00 am
Location: Huddersfield, UK

Postby zerodiv » Mon Dec 17, 2007 7:50 am

Just a quick update.

I have joystick bindings from the ctrlr ini file working now.
These are the controls that are currently supported:

'Joystick Inputs (?=device no.):
JOY?_LEFT
JOY?_RIGHT
JOY?_UP
JOY?_DOWN
JOY?_BUTTON0-31

My next step is to add joystick support to wahcade-setup. Then i'll clean it up as much as possible and post the patch here.
zerodiv
 
Posts: 8
Joined: Tue Dec 11, 2007 1:23 am

Postby zerodiv » Tue Dec 18, 2007 3:49 am

This patch works with wahcade-setup.py now, and It shouldn't do silly things like throw errors if you don't have your joystick plugged in. If anyone gets a chance to test it out, I would appreciate some feedback.

btw, sorry for posting these huge diffs in the forum. I have free hosting, but its picky about what kind of files I can upload.

Code: Select all
diff -ruN wahcade-old/joystick.py wahcade/joystick.py
--- wahcade-old/joystick.py   1969-12-31 19:00:00.000000000 -0500
+++ wahcade/joystick.py   2007-12-17 21:26:34.335582411 -0500
@@ -0,0 +1,114 @@
+import gtk
+import pygame
+
+class joystick:
+    def __init__(self):
+        self.state = {}
+        self.devices = {}
+        pygame.init()
+        pygame.joystick.init()
+   
+    def use_ini_controls(self,ctrlr_ini):
+        for mw_keys in ctrlr_ini.ini_dict.itervalues():
+            mw_keys = mw_keys.strip(' "').split(' | ')
+            for mw_key in mw_keys:
+                if mw_key[:3] == "JOY":
+                    self.state[mw_key]=0
+                    (dev_num,control) = mw_key.split("_",1)
+                    dev_num=dev_num[3:]
+                    self.devices[int(dev_num)] = None
+       
+        num_joysticks = pygame.joystick.get_count()
+        for dev_num in self.devices.iterkeys():
+            if dev_num < num_joysticks:
+                self.devices[dev_num] = pygame.joystick.Joystick(dev_num)
+                self.devices[dev_num].init()
+       
+    def use_all_controls(self):
+        num_joysticks = pygame.joystick.get_count()
+        for dev_num in range(num_joysticks):
+            self.devices[dev_num]=pygame.joystick.Joystick(dev_num)
+            self.devices[dev_num].init()
+            num_buttons = self.devices[dev_num].get_numbuttons()
+            for button_num in range(num_buttons):
+                mw_key = "JOY%s_BUTTON%s" % (dev_num,button_num)
+                self.state[mw_key]=0
+            if self.devices[dev_num].get_numaxes() > 2:
+                mw_key = "JOY%s_" % (dev_num)
+                self.state[mw_key+"UP"]=0
+                self.state[mw_key+"DOWN"]=0
+                self.state[mw_key+"LEFT"]=0
+                self.state[mw_key+"RIGHT"]=0
+       
+    def poll(self,call,initial_repeat_delay=40):
+        repeat_delay = 4
+        # give pygame a chance to do its magic with the joystick
+        pygame.event.pump()
+       
+        # end polling if no joysticks are found
+        num_joysticks = pygame.joystick.get_count()
+        if num_joysticks == 0:
+            return 0
+       
+        # get the focused window or return
+        active_window = None
+        windows = gtk.window_list_toplevels()
+        for window in windows:
+            if window.is_active():
+                active_window = window
+        if active_window == None: return 1
+
+        # check if any of our defined controlls were pressed
+        mw_key_events=[]
+        for mw_key in self.state.iterkeys():
+            (dev_num, type) = (mw_key.split("_",1))
+            dev_num = int(dev_num[3:])
+            # if binding is for an unfound joystick, continue to next binding
+            if dev_num > num_joysticks-1:
+                continue
+            if type[:6]=="BUTTON":
+                button_num = type[6:]
+                if self.devices[dev_num].get_button(int(button_num)):
+                    self.state[mw_key]+=1
+                    mw_key_events.append(mw_key)
+                elif self.state[mw_key] > 0:
+                    self.state[mw_key]=0
+                    mw_key_events.append(mw_key)
+            elif type in ["LEFT","RIGHT","UP","DOWN"]:
+                if type in ["LEFT","RIGHT"]:
+                    axis_num = 0
+                else:
+                    axis_num = 1
+                axis_value = self.devices[dev_num].get_axis(axis_num)
+                if type in ["UP","LEFT"]:
+                    if axis_value < -0.5:
+                        self.state[mw_key]+=1
+                        mw_key_events.append(mw_key)
+                    elif self.state[mw_key] > 0:
+                        self.state[mw_key]=0
+                        mw_key_events.append(mw_key)
+                else:
+                    if axis_value > 0.5:
+                        self.state[mw_key]+=1
+                        mw_key_events.append(mw_key)
+                    elif self.state[mw_key] > 0:
+                        self.state[mw_key]=0
+                        mw_key_events.append(mw_key)
+                       
+        # send fake key-press events
+        for mw_key in mw_key_events:
+            if self.state[mw_key]==0:
+                e = gtk.gdk.Event(gtk.gdk.KEY_RELEASE)
+                #e.keyval = int(gtk.gdk.keyval_from_name("j"))
+                e.window = active_window.window
+                call(active_window,e,"JOYSTICK",mw_key)
+            elif self.state[mw_key]==1 \
+              or self.state[mw_key]==initial_repeat_delay \
+              or (self.state[mw_key]>initial_repeat_delay and \
+                   (self.state[mw_key]-initial_repeat_delay)%repeat_delay==0):
+                e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
+                #e.keyval = int(gtk.gdk.keyval_from_name("j"))
+                e.window = active_window.window
+                call(active_window,e,"JOYSTICK",mw_key)
+        return 1
+
diff -ruN wahcade-old/wc_setup.py wahcade/wc_setup.py
--- wahcade-old/wc_setup.py   2007-10-14 09:49:03.000000000 -0400
+++ wahcade/wc_setup.py   2007-12-17 20:54:39.127759372 -0500
@@ -35,6 +35,8 @@
 from win_filter import WinFilter, DlgWait
 _ = gettext.gettext
 
+import joystick
+
 _mouse_ctrls = {
     'Mouse Button 1': ['MOUSE_BUTTON0'],
     'Mouse Button 2': ['MOUSE_BUTTON1'],
@@ -118,7 +120,9 @@
         self.key_changed_cb = key_changed_cb
         self.tvw_path = tvw_path
         self.rev_mouse_ctrls = self.invert_dictionary_with_lists(_mouse_ctrls)
-       
+        global joy
+        self.event_source = gobject.timeout_add(10,joy.poll,self.on_dlgKeyPress_key_press,100000)
+
     def on_btnClear_clicked(self, *args):
         """clear clicked"""
         self.dlgKeyPress.destroy()
@@ -131,7 +135,16 @@
     def on_dlgKeyPress_key_press(self, widget, event, *args):
         """a key has been pressed"""
         mw_keys = []
-        if event.type == gtk.gdk.KEY_PRESS:
+        joystick_key = None
+        if len(args) > 1:
+            if args[0] == "JOYSTICK":
+                joystick_key = args[1]
+               
+        if event.type == gtk.gdk.KEY_PRESS and joystick_key:
+            mw_keys = [joystick_key]
+            keyname = joystick_key
+            gobject.source_remove(self.event_source)
+        elif event.type == gtk.gdk.KEY_PRESS:
             #keyboard pressed, get gtk keyname
             keyname = gtk.gdk.keyval_name(event.keyval).lower()
             #get mamewah keyname
@@ -234,7 +247,12 @@
         settings = gtk.settings_get_default()
         settings.set_string_property('gtk-icon-sizes', 'gtk-button=16,16', '')
         #settings.set_long_property('gtk-toolbar-icon-size', gtk.ICON_SIZE_MENU, '')
-
+       
+        global joy
+        joy = joystick.joystick()
+        joy.use_all_controls()
+       
+       
     def on_winSetup_delete_event(self, *args):
         """done, quit the application"""
         self.save_setups()
@@ -862,6 +880,7 @@
             lv = cv.strip(' "').split(' | ')
             kb_vals = [v for v in lv if v.startswith('DIK_')]
             mouse_vals = [v for v in lv if v.startswith('MOUSE_')]
+            joystick_vals = [v for v in lv if v.startswith('JOY')]
             key_desc = ''
             #get key descriptions
             for keyval in kb_vals:
@@ -879,6 +898,12 @@
                         key_desc = '%s or %s' % (key_desc, mousename[0].upper())
                     else:
                         key_desc = '%s' % (mousename[0].upper())
+            #get joystick descriptions
+            for joyval in joystick_vals:
+                if key_desc != '':
+                    key_desc = '%s or %s' % (key_desc, joyval)
+                else:
+                    key_desc = joyval
             if ck.isupper():
                 self.lsKeys.append((ck, key_desc))
                 
diff -ruN wahcade-old/win_main.py wahcade/win_main.py
--- wahcade-old/win_main.py   2007-10-10 12:44:01.000000000 -0400
+++ wahcade/win_main.py   2007-12-17 20:58:23.093591980 -0500
@@ -62,6 +62,8 @@
 #set gettext function
 _ = gettext.gettext
 
+import joystick
+
 #main window class
 class WinMain(WahCade):
     """Wah!Cade Main Window"""
@@ -286,6 +288,10 @@
             pass
         elif self.wahcade_ini.getint('fullscreen', 0):
             self.winMain.fullscreen()
+        #joystick setup
+        self.joy = joystick.joystick()
+        self.joy.use_ini_controls(self.ctrlr_ini);
+        gobject.timeout_add(10,self.joy.poll,self.on_winMain_key_press)
         #hide splash
         self.splash.destroy()
 
@@ -310,6 +316,10 @@
         mw_key = ''
         mw_func = ''
         mw_functions = []
+        joystick_key = None
+        if len(args) > 1:
+            if args[0] == "JOYSTICK":
+                joystick_key = args[1]
         #reset screen saver time (and turn off if necessary)
         self.scrsave_time = time.time()
         if self.scrsaver.running:
@@ -332,14 +342,17 @@
         elif event.type == gtk.gdk.KEY_PRESS:
             self.keypress_count += 1
             #keyboard pressed, get gtk keyname
-            keyname = gtk.gdk.keyval_name(event.keyval).lower()
-            #got something?
-            if not mamewah_keys.has_key(keyname):
-                return
-            #get mamewah keyname
-            mw_keys = mamewah_keys[keyname]
-            if mw_keys == []:
-                return
+            if joystick_key:
+                mw_keys = [joystick_key]
+            else:
+                keyname = gtk.gdk.keyval_name(event.keyval).lower()
+                #got something?
+                if not mamewah_keys.has_key(keyname):
+                    return
+                #get mamewah keyname
+                mw_keys = mamewah_keys[keyname]
+                if mw_keys == []:
+                    return
         elif event.type == gtk.gdk.KEY_RELEASE:
             self.keypress_count = 0
             #keyboard released, update labels, images, etc
zerodiv
 
Posts: 8
Joined: Tue Dec 11, 2007 1:23 am

Postby balki » Tue Dec 18, 2007 9:05 am

Hi zerodiv,

That looks excellent! :P
I'll get this patched into the upcoming release and send you a version to test in the next day or two.

Cheers again,
Andy
User avatar
balki
Site Admin
 
Posts: 425
Joined: Thu Jan 01, 1970 1:00 am
Location: Huddersfield, UK

Postby zerodiv » Wed Dec 19, 2007 11:22 pm

One of my joysticks just died, so I plugged in a different one that had less buttons, and came up with a few bugs.

This new patch should fix those bugs. It also makes pygame optional, so if you don't have it installed, everything should still work.

Code: Select all
diff -ruN wahcade-old/joystick.py wahcade/joystick.py
--- wahcade-old/joystick.py   1969-12-31 19:00:00.000000000 -0500
+++ wahcade/joystick.py   2007-12-19 15:43:52.217834212 -0500
@@ -0,0 +1,152 @@
+import gtk
+try:
+    import pygame
+except ImportError:
+    supported = False
+else:
+    supported = True
+
+class joystick:
+    def __init__(self):
+        self.state = {}
+        self.devices = {}
+        pygame.init()
+        pygame.joystick.init()
+        print "Joystick initialized"
+   
+    def use_ini_controls(self,ctrlr_ini):
+        # grab controls from the ini file
+        for mw_keys in ctrlr_ini.ini_dict.itervalues():
+            mw_keys = mw_keys.strip(' "').split(' | ')
+            for mw_key in mw_keys:
+                if mw_key[:3] == "JOY":
+                    self.state[mw_key]=0
+                    (dev_num,control) = mw_key.split("_",1)
+                    dev_num=dev_num[3:]
+                    self.devices[int(dev_num)] = None
+
+        # init all joysticks
+        num_joysticks = pygame.joystick.get_count()
+        for dev_num in self.devices.iterkeys():
+            if dev_num < num_joysticks:
+                self.devices[dev_num] = pygame.joystick.Joystick(dev_num)
+                self.devices[dev_num].init()
+               
+        # make a list of all the controls we cant use
+        remove_mw_key = []
+        for mw_key in self.state.iterkeys():
+            (dev_num, type) = (mw_key.split("_",1))
+            dev_num = int(dev_num[3:])
+            # non-existant joystick
+            if dev_num > num_joysticks-1:
+                remove_mw_key.append(mw_key)
+                continue
+            # button doesn't exist
+            if type[:6]=="BUTTON":
+                button_num = int(type[6:])
+                num_buttons = int(self.devices[dev_num].get_numbuttons())
+                if button_num > num_buttons-1:
+                    remove_mw_key.append(mw_key)
+            # axis doesn't exist
+            elif type in ["LEFT","RIGHT","UP","DOWN"]:
+                num_axis = self.devices[dev_num].get_numaxes()
+                if type in ["LEFT","RIGHT"]:
+                    if num_axis < 1:
+                        remove_mw_key.append(mw_key)
+                elif type in ["UP","DOWN"]:
+                    if num_axis < 2:
+                        remove_mw_key.append(mw_key)
+            # unsupported control
+            else:
+                remove_mw_key.append(mw_key)
+               
+        # remove unusable controls
+        for mw_key in remove_mw_key:
+            del self.state[mw_key]
+
+       
+    def use_all_controls(self):
+        num_joysticks = pygame.joystick.get_count()
+        for dev_num in range(num_joysticks):
+            self.devices[dev_num]=pygame.joystick.Joystick(dev_num)
+            self.devices[dev_num].init()
+            num_buttons = self.devices[dev_num].get_numbuttons()
+            for button_num in range(num_buttons):
+                mw_key = "JOY%s_BUTTON%s" % (dev_num,button_num)
+                self.state[mw_key]=0
+            if self.devices[dev_num].get_numaxes() > 1:
+                mw_key = "JOY%s_" % (dev_num)
+                self.state[mw_key+"UP"]=0
+                self.state[mw_key+"DOWN"]=0
+                self.state[mw_key+"LEFT"]=0
+                self.state[mw_key+"RIGHT"]=0
+       
+    def poll(self,call,initial_repeat_delay=40):
+        repeat_delay = 4
+        # give pygame a chance to do its magic with the joystick
+        pygame.event.pump()
+       
+        # end polling if no joysticks are found
+        num_joysticks = pygame.joystick.get_count()
+        if num_joysticks == 0:
+            return 0
+       
+        # get the focused window or return
+        active_window = None
+        windows = gtk.window_list_toplevels()
+        for window in windows:
+            if window.is_active():
+                active_window = window
+        if active_window == None: return 1
+
+        # check if any of our defined controls were pressed
+        mw_key_events=[]
+        for mw_key in self.state.iterkeys():
+            (dev_num, type) = (mw_key.split("_",1))
+            dev_num = int(dev_num[3:])
+            if type[:6]=="BUTTON":
+                button_num = type[6:]
+                if self.devices[dev_num].get_button(int(button_num)):
+                    self.state[mw_key]+=1
+                    mw_key_events.append(mw_key)
+                elif self.state[mw_key] > 0:
+                    self.state[mw_key]=0
+                    mw_key_events.append(mw_key)
+            elif type in ["LEFT","RIGHT","UP","DOWN"]:
+                if type in ["LEFT","RIGHT"]:
+                    axis_num = 0
+                else:
+                    axis_num = 1
+                axis_value = self.devices[dev_num].get_axis(axis_num)
+                if type in ["UP","LEFT"]:
+                    if axis_value < -0.5:
+                        self.state[mw_key]+=1
+                        mw_key_events.append(mw_key)
+                    elif self.state[mw_key] > 0:
+                        self.state[mw_key]=0
+                        mw_key_events.append(mw_key)
+                else:
+                    if axis_value > 0.5:
+                        self.state[mw_key]+=1
+                        mw_key_events.append(mw_key)
+                    elif self.state[mw_key] > 0:
+                        self.state[mw_key]=0
+                        mw_key_events.append(mw_key)
+                       
+        # send fake key-press events
+        for mw_key in mw_key_events:
+            if self.state[mw_key]==0:
+                e = gtk.gdk.Event(gtk.gdk.KEY_RELEASE)
+                #e.keyval = int(gtk.gdk.keyval_from_name("j"))
+                e.window = active_window.window
+                call(active_window,e,"JOYSTICK",mw_key)
+            elif self.state[mw_key]==1 \
+              or self.state[mw_key]==initial_repeat_delay \
+              or (self.state[mw_key]>initial_repeat_delay and \
+                   (self.state[mw_key]-initial_repeat_delay)%repeat_delay==0):
+                e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
+                #e.keyval = int(gtk.gdk.keyval_from_name("j"))
+                e.window = active_window.window
+                call(active_window,e,"JOYSTICK",mw_key)
+        return 1
+
diff -ruN wahcade-old/wc_setup.py wahcade/wc_setup.py
--- wahcade-old/wc_setup.py   2007-10-14 09:49:03.000000000 -0400
+++ wahcade/wc_setup.py   2007-12-19 15:37:41.774347689 -0500
@@ -35,6 +35,8 @@
 from win_filter import WinFilter, DlgWait
 _ = gettext.gettext
 
+import joystick
+
 _mouse_ctrls = {
     'Mouse Button 1': ['MOUSE_BUTTON0'],
     'Mouse Button 2': ['MOUSE_BUTTON1'],
@@ -118,7 +120,10 @@
         self.key_changed_cb = key_changed_cb
         self.tvw_path = tvw_path
         self.rev_mouse_ctrls = self.invert_dictionary_with_lists(_mouse_ctrls)
-       
+        if joystick.supported:
+            global joy
+            self.event_source = gobject.timeout_add(10,joy.poll,self.on_dlgKeyPress_key_press,100000)
+
     def on_btnClear_clicked(self, *args):
         """clear clicked"""
         self.dlgKeyPress.destroy()
@@ -131,7 +136,15 @@
     def on_dlgKeyPress_key_press(self, widget, event, *args):
         """a key has been pressed"""
         mw_keys = []
-        if event.type == gtk.gdk.KEY_PRESS:
+        joystick_key = None
+        if len(args) > 1:
+            if args[0] == "JOYSTICK":
+                joystick_key = args[1]
+        if event.type == gtk.gdk.KEY_PRESS and joystick_key:
+            mw_keys = [joystick_key]
+            keyname = joystick_key
+            gobject.source_remove(self.event_source)
+        elif event.type == gtk.gdk.KEY_PRESS:
             #keyboard pressed, get gtk keyname
             keyname = gtk.gdk.keyval_name(event.keyval).lower()
             #get mamewah keyname
@@ -234,7 +247,13 @@
         settings = gtk.settings_get_default()
         settings.set_string_property('gtk-icon-sizes', 'gtk-button=16,16', '')
         #settings.set_long_property('gtk-toolbar-icon-size', gtk.ICON_SIZE_MENU, '')
-
+       
+        if joystick.supported:
+            global joy
+            joy = joystick.joystick()
+            joy.use_all_controls()
+       
+       
     def on_winSetup_delete_event(self, *args):
         """done, quit the application"""
         self.save_setups()
@@ -862,6 +881,7 @@
             lv = cv.strip(' "').split(' | ')
             kb_vals = [v for v in lv if v.startswith('DIK_')]
             mouse_vals = [v for v in lv if v.startswith('MOUSE_')]
+            joystick_vals = [v for v in lv if v.startswith('JOY')]
             key_desc = ''
             #get key descriptions
             for keyval in kb_vals:
@@ -879,6 +899,12 @@
                         key_desc = '%s or %s' % (key_desc, mousename[0].upper())
                     else:
                         key_desc = '%s' % (mousename[0].upper())
+            #get joystick descriptions
+            for joyval in joystick_vals:
+                if key_desc != '':
+                    key_desc = '%s or %s' % (key_desc, joyval)
+                else:
+                    key_desc = joyval
             if ck.isupper():
                 self.lsKeys.append((ck, key_desc))
                 
diff -ruN wahcade-old/win_main.py wahcade/win_main.py
--- wahcade-old/win_main.py   2007-10-10 12:44:01.000000000 -0400
+++ wahcade/win_main.py   2007-12-19 15:35:19.296083641 -0500
@@ -62,6 +62,8 @@
 #set gettext function
 _ = gettext.gettext
 
+import joystick
+
 #main window class
 class WinMain(WahCade):
     """Wah!Cade Main Window"""
@@ -286,6 +288,11 @@
             pass
         elif self.wahcade_ini.getint('fullscreen', 0):
             self.winMain.fullscreen()
+        #joystick setup
+        if joystick.supported:
+            self.joy = joystick.joystick()
+            self.joy.use_ini_controls(self.ctrlr_ini);
+            gobject.timeout_add(10,self.joy.poll,self.on_winMain_key_press)
         #hide splash
         self.splash.destroy()
 
@@ -310,6 +317,10 @@
         mw_key = ''
         mw_func = ''
         mw_functions = []
+        joystick_key = None
+        if len(args) > 1:
+            if args[0] == "JOYSTICK":
+                joystick_key = args[1]
         #reset screen saver time (and turn off if necessary)
         self.scrsave_time = time.time()
         if self.scrsaver.running:
@@ -332,14 +343,17 @@
         elif event.type == gtk.gdk.KEY_PRESS:
             self.keypress_count += 1
             #keyboard pressed, get gtk keyname
-            keyname = gtk.gdk.keyval_name(event.keyval).lower()
-            #got something?
-            if not mamewah_keys.has_key(keyname):
-                return
-            #get mamewah keyname
-            mw_keys = mamewah_keys[keyname]
-            if mw_keys == []:
-                return
+            if joystick_key:
+                mw_keys = [joystick_key]
+            else:
+                keyname = gtk.gdk.keyval_name(event.keyval).lower()
+                #got something?
+                if not mamewah_keys.has_key(keyname):
+                    return
+                #get mamewah keyname
+                mw_keys = mamewah_keys[keyname]
+                if mw_keys == []:
+                    return
         elif event.type == gtk.gdk.KEY_RELEASE:
             self.keypress_count = 0
             #keyboard released, update labels, images, etc
zerodiv
 
Posts: 8
Joined: Tue Dec 11, 2007 1:23 am


Return to Wah!Cade

Who is online

Users browsing this forum: Bing [Bot] and 5 guests

cron