Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

""" Runner for multi-consoles (8bit mainly) """ 

 

import os 

import subprocess 

 

from lutris.runners.runner import Runner 

from lutris.util.display import get_current_resolution 

from lutris.util.log import logger 

 

 

# pylint: disable=C0103 

class mednafen(Runner): 

    """Mednafen is a multi-platform emulator, including NES, GB/A, PC Engine""" 

    def __init__(self, settings=None): 

        super(mednafen, self).__init__() 

        self.executable = "mednafen" 

        self.platform = """ 

* Atari Lynx 

* GameBoy 

* GameBoy Color 

* GameBoy Advance 

* NES 

* PC Engine (TurboGrafx 16) 

* PC-FX 

* SuperGrafx 

* NeoGeo Pocket, NeoGeo Pocket Color 

* WonderSwan 

""" 

        self.package = "mednafen" 

        machine_choices = ( 

            ("NES", "nes"), 

            ("PC Engine", "pce"), 

            ('Game Boy', 'gb'), 

            ('Game Boy Advance', 'gba') 

        ) 

        self.game_options = [ 

            { 

                "option": "main_file", 

                "type": "file", 

                "label": "Rom file" 

            }, 

            { 

                "option": "machine", 

                "type": "one_choice", 

                "label": "Machine type", 

                "choices": machine_choices 

            } 

        ] 

        self.runner_options = [ 

            { 

                "option": "fs", 

                "type": "bool", 

                "label": "Fullscreen", 

                "default": True, 

            } 

        ] 

        self.settings = settings 

 

    def find_joysticks(self): 

        """ Detect connected joysticks and return their ids """ 

        joy_ids = [] 

        if not self.is_installed: 

            return [] 

        output = subprocess.Popen(["mednafen", "dummy"], 

                                  stdout=subprocess.PIPE).communicate()[0] 

        ouput = str.split(output, "\n") 

        found = False 

        joy_list = [] 

        for line in ouput: 

            if found and "Joystick" in line: 

                joy_list.append(line) 

            else: 

                found = False 

            if "Initializing joysticks" in line: 

                found = True 

 

        for joy in joy_list: 

            index = joy.find("Unique ID:") 

            joy_id = joy[index + 11:] 

            logger.debug('Joystick found id %s ' % joy_id) 

            joy_ids.append(joy_id) 

        return joy_ids 

 

    def set_joystick_controls(self, joy_ids): 

        """ Setup joystick mappings per machine """ 

        nes_controls = [ 

            "-nes.input.port1.gamepad.a", 

            "\"joystick " + joy_ids[0] + " 00000001\"", 

            "-nes.input.port1.gamepad.b", 

            "\"joystick " + joy_ids[0] + " 00000002\"", 

            "-nes.input.port1.gamepad.start", 

            "\"joystick " + joy_ids[0] + " 00000009\"", 

            "-nes.input.port1.gamepad.select", 

            "\"joystick " + joy_ids[0] + " 00000008\"", 

            "-nes.input.port1.gamepad.up", 

            "\"joystick " + joy_ids[0] + " 0000c001\"", 

            "-nes.input.port1.gamepad.down", 

            "\"joystick " + joy_ids[0] + " 00008001\"", 

            "-nes.input.port1.gamepad.left", 

            "\"joystick " + joy_ids[0] + " 0000c000\"", 

            "-nes.input.port1.gamepad.right", 

            "\"joystick " + joy_ids[0] + " 00008000\"" 

        ] 

 

        gba_controls = [ 

            "-gba.input.builtin.gamepad.a", 

            "\"joystick " + joy_ids[0] + " 00000001\"", 

            "-gba.input.builtin.gamepad.b", 

            "\"joystick " + joy_ids[0] + " 00000002\"", 

            "-gba.input.builtin.gamepad.shoulder_r", 

            "\"joystick " + joy_ids[0] + " 00000007\"", 

            "-gba.input.builtin.gamepad.shoulder_l", 

            "\"joystick " + joy_ids[0] + " 00000006\"", 

            "-gba.input.builtin.gamepad.start", 

            "\"joystick " + joy_ids[0] + " 00000009\"", 

            "-gba.input.builtin.gamepad.select", 

            "\"joystick " + joy_ids[0] + " 00000008\"", 

            "-gba.input.builtin.gamepad.up", 

            "\"joystick " + joy_ids[0] + " 0000c001\"", 

            "-gba.input.builtin.gamepad.down", 

            "\"joystick " + joy_ids[0] + " 00008001\"", 

            "-gba.input.builtin.gamepad.left", 

            "\"joystick " + joy_ids[0] + " 0000c000\"", 

            "-gba.input.builtin.gamepad.right", 

            "\"joystick " + joy_ids[0] + " 00008000\"" 

        ] 

 

        gb_controls = [ 

            "-gb.input.builtin.gamepad.a", 

            "\"joystick " + joy_ids[0] + " 00000001\"", 

            "-gb.input.builtin.gamepad.b", 

            "\"joystick " + joy_ids[0] + " 00000002\"", 

            "-gb.input.builtin.gamepad.start", 

            "\"joystick " + joy_ids[0] + " 00000009\"", 

            "-gb.input.builtin.gamepad.select", 

            "\"joystick " + joy_ids[0] + " 00000008\"", 

            "-gb.input.builtin.gamepad.up", 

            "\"joystick " + joy_ids[0] + " 0000c001\"", 

            "-gb.input.builtin.gamepad.down", 

            "\"joystick " + joy_ids[0] + " 00008001\"", 

            "-gb.input.builtin.gamepad.left", 

            "\"joystick " + joy_ids[0] + " 0000c000\"", 

            "-gb.input.builtin.gamepad.right", 

            "\"joystick " + joy_ids[0] + " 00008000\"" 

        ] 

 

        pce_controls = [ 

            "-pce.input.port1.gamepad.i", 

            "\"joystick " + joy_ids[0] + " 00000001\"", 

            "-pce.input.port1.gamepad.ii", 

            "\"joystick " + joy_ids[0] + " 00000002\"", 

            "-pce.input.port1.gamepad.run", 

            "\"joystick " + joy_ids[0] + " 00000009\"", 

            "-pce.input.port1.gamepad.select", 

            "\"joystick " + joy_ids[0] + " 00000008\"", 

            "-pce.input.port1.gamepad.up", 

            "\"joystick " + joy_ids[0] + " 0000c001\"", 

            "-pce.input.port1.gamepad.down", 

            "\"joystick " + joy_ids[0] + " 00008001\"", 

            "-pce.input.port1.gamepad.left", 

            "\"joystick " + joy_ids[0] + " 0000c000\"", 

            "-pce.input.port1.gamepad.right", 

            "\"joystick " + joy_ids[0] + " 00008000\"" 

        ] 

 

        if self.machine == "pce": 

            controls = pce_controls 

        elif self.machine == "nes": 

            controls = nes_controls 

        elif self.machine == "gba": 

            controls = gba_controls 

        elif self.machine == "gb": 

            controls = gb_controls 

        else: 

            controls = [] 

        return controls 

 

    def play(self): 

        """Runs the game""" 

        rom = self.settings["game"]["main_file"] 

        machine = self.settings["game"]["machine"] 

        #Defaults 

        fullscreen = "1" 

 

        if "mednafen" in self.settings.config: 

            if "fs" in self.settings.config["mednafen"]: 

                if not self.settings.config["mednafen"]["fs"]: 

                    fullscreen = "0" 

        resolution = get_current_resolution() 

        (resolutionx, resolutiony) = resolution.split("x") 

        xres = str(resolutionx) 

        yres = str(resolutiony) 

        options = ["-fs", fullscreen, 

                   "-" + machine + ".xres", xres, 

                   "-" + machine + ".yres", yres, 

                   "-" + machine + ".stretch", "1", 

                   "-" + machine + ".special", "hq4x", 

                   "-" + machine + ".videoip", "1"] 

        joy_ids = self.find_joysticks() 

        if len(joy_ids) > 0: 

            controls = self.set_joystick_controls(joy_ids) 

            for control in controls: 

                options.append(control) 

        else: 

            logger.debug("No Joystick found") 

 

        if not os.path.exists(rom): 

            return {'error': 'FILE_NOT_FOUND', 'file': rom} 

 

        command = [self.executable] 

        for option in options: 

            command.append(option) 

        command.append("\"%s\"" % rom) 

        return {'command': command}