Guard against null pointer

This commit is contained in:
Endolf 2018-05-12 18:59:54 +01:00
parent c8996c0581
commit 64ec6fe8e4

View file

@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2003 Jeremy Booth (jeremy@newdawnsoftware.com)
*
* Redistribution and use in source and binary forms, with or without
@ -63,11 +63,11 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
public final Object run() {
String lib_path = System.getProperty("net.java.games.input.librarypath");
try {
if (lib_path != null)
if(lib_path != null)
System.load(lib_path + File.separator + System.mapLibraryName(lib_name));
else
System.loadLibrary(lib_name);
} catch (UnsatisfiedLinkError e) {
} catch(UnsatisfiedLinkError e) {
logln("Failed to load library: " + e.getMessage());
e.printStackTrace();
supported = false;
@ -78,7 +78,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
}
static String getPrivilegedProperty(final String property) {
return (String)AccessController.doPrivileged(new PrivilegedAction() {
return (String) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(property);
}
@ -87,7 +87,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
static String getPrivilegedProperty(final String property, final String default_value) {
return (String)AccessController.doPrivileged(new PrivilegedAction() {
return (String) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(property, default_value);
}
@ -138,13 +138,13 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
private final static Component[] createComponents(List event_components, LinuxEventDevice device) {
LinuxEventComponent[][] povs = new LinuxEventComponent[4][2];
List components = new ArrayList();
for (int i = 0; i < event_components.size(); i++) {
LinuxEventComponent event_component = (LinuxEventComponent)event_components.get(i);
for(int i = 0; i < event_components.size(); i++) {
LinuxEventComponent event_component = (LinuxEventComponent) event_components.get(i);
Component.Identifier identifier = event_component.getIdentifier();
if (identifier == Component.Identifier.Axis.POV) {
if(identifier == Component.Identifier.Axis.POV) {
int native_code = event_component.getDescriptor().getCode();
switch (native_code) {
switch(native_code) {
case NativeDefinitions.ABS_HAT0X:
povs[0][0] = event_component;
break;
@ -173,16 +173,16 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
logln("Unknown POV instance: " + native_code);
break;
}
} else if (identifier != null) {
} else if(identifier != null) {
LinuxComponent component = new LinuxComponent(event_component);
components.add(component);
device.registerComponent(event_component.getDescriptor(), component);
}
}
for (int i = 0; i < povs.length; i++) {
for(int i = 0; i < povs.length; i++) {
LinuxEventComponent x = povs[i][0];
LinuxEventComponent y = povs[i][1];
if (x != null && y != null) {
if(x != null && y != null) {
LinuxComponent controller_component = new LinuxPOV(x, y);
components.add(controller_component);
device.registerComponent(x.getDescriptor(), controller_component);
@ -196,7 +196,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
private final static Mouse createMouseFromDevice(LinuxEventDevice device, Component[] components) throws IOException {
Mouse mouse = new LinuxMouse(device, components, new Controller[]{}, device.getRumblers());
if (mouse.getX() != null && mouse.getY() != null && mouse.getPrimaryButton() != null)
if(mouse.getX() != null && mouse.getY() != null && mouse.getPrimaryButton() != null)
return mouse;
else
return null;
@ -217,11 +217,11 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
Component[] components = createComponents(event_components, device);
Controller.Type type = device.getType();
if (type == Controller.Type.MOUSE) {
if(type == Controller.Type.MOUSE) {
return createMouseFromDevice(device, components);
} else if (type == Controller.Type.KEYBOARD) {
} else if(type == Controller.Type.KEYBOARD) {
return createKeyboardFromDevice(device, components);
} else if (type == Controller.Type.STICK || type == Controller.Type.GAMEPAD) {
} else if(type == Controller.Type.STICK || type == Controller.Type.GAMEPAD) {
return createJoystickFromDevice(device, components, type);
} else
return null;
@ -234,8 +234,8 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
enumerateEventControllers(eventControllers);
enumerateJoystickControllers(jsControllers);
for(int i=0;i<eventControllers.size();i++) {
for(int j=0;j<jsControllers.size();j++) {
for(int i = 0; i < eventControllers.size(); i++) {
for(int j = 0; j < jsControllers.size(); j++) {
Controller evController = (Controller) eventControllers.get(i);
Controller jsController = (Controller) jsControllers.get(j);
@ -245,10 +245,10 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
// Check they have the same component count
Component[] evComponents = evController.getComponents();
Component[] jsComponents = jsController.getComponents();
if(evComponents.length==jsComponents.length) {
if(evComponents.length == jsComponents.length) {
boolean foundADifference = false;
// check the component pairs are of the same type
for(int k=0;k<evComponents.length;k++) {
for(int k = 0; k < evComponents.length; k++) {
// Check the type of the component is the same
if(!(evComponents[k].getIdentifier() == jsComponents[k].getIdentifier())) {
foundADifference = true;
@ -256,7 +256,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
}
if(!foundADifference) {
controllers.add(new LinuxCombinedController((LinuxAbstractController)eventControllers.remove(i), (LinuxJoystickAbstractController)jsControllers.remove(j)));
controllers.add(new LinuxCombinedController((LinuxAbstractController) eventControllers.remove(i), (LinuxJoystickAbstractController) jsControllers.remove(j)));
i--;
j--;
break;
@ -274,7 +274,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
}
private final static Component.Identifier.Button getButtonIdentifier(int index) {
switch (index) {
switch(index) {
case 0:
return Component.Identifier.Button._0;
case 1:
@ -350,69 +350,69 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
char[] buttonMap = device.getButtonMap();
LinuxJoystickAxis[] hatBits = new LinuxJoystickAxis[6];
for (int i = 0; i < device.getNumButtons(); i++) {
Component.Identifier button_id = (Component.Identifier)LinuxNativeTypesMap.getButtonID(buttonMap[i]);
if (button_id != null) {
for(int i = 0; i < device.getNumButtons(); i++) {
Component.Identifier button_id = (Component.Identifier) LinuxNativeTypesMap.getButtonID(buttonMap[i]);
if(button_id != null) {
LinuxJoystickButton button = new LinuxJoystickButton(button_id);
device.registerButton(i, button);
components.add(button);
}
}
for (int i = 0; i < device.getNumAxes(); i++) {
for(int i = 0; i < device.getNumAxes(); i++) {
Component.Identifier.Axis axis_id;
axis_id = (Component.Identifier.Axis) LinuxNativeTypesMap.getAbsAxisID(axisMap[i]);
LinuxJoystickAxis axis = new LinuxJoystickAxis(axis_id);
device.registerAxis(i, axis);
if(axisMap[i]==NativeDefinitions.ABS_HAT0X) {
if(axisMap[i] == NativeDefinitions.ABS_HAT0X) {
hatBits[0] = axis;
} else if(axisMap[i]==NativeDefinitions.ABS_HAT0Y) {
} else if(axisMap[i] == NativeDefinitions.ABS_HAT0Y) {
hatBits[1] = axis;
axis = new LinuxJoystickPOV(Component.Identifier.Axis.POV, hatBits[0], hatBits[1]);
device.registerPOV((LinuxJoystickPOV)axis);
device.registerPOV((LinuxJoystickPOV) axis);
components.add(axis);
} else if(axisMap[i]==NativeDefinitions.ABS_HAT1X) {
} else if(axisMap[i] == NativeDefinitions.ABS_HAT1X) {
hatBits[2] = axis;
} else if(axisMap[i]==NativeDefinitions.ABS_HAT1Y) {
} else if(axisMap[i] == NativeDefinitions.ABS_HAT1Y) {
hatBits[3] = axis;
axis = new LinuxJoystickPOV(Component.Identifier.Axis.POV, hatBits[2], hatBits[3]);
device.registerPOV((LinuxJoystickPOV)axis);
device.registerPOV((LinuxJoystickPOV) axis);
components.add(axis);
} else if(axisMap[i]==NativeDefinitions.ABS_HAT2X) {
} else if(axisMap[i] == NativeDefinitions.ABS_HAT2X) {
hatBits[4] = axis;
} else if(axisMap[i]==NativeDefinitions.ABS_HAT2Y) {
} else if(axisMap[i] == NativeDefinitions.ABS_HAT2Y) {
hatBits[5] = axis;
axis = new LinuxJoystickPOV(Component.Identifier.Axis.POV, hatBits[4], hatBits[5]);
device.registerPOV((LinuxJoystickPOV)axis);
device.registerPOV((LinuxJoystickPOV) axis);
components.add(axis);
} else {
components.add(axis);
}
}
return new LinuxJoystickAbstractController(device, (Component[])components.toArray(new Component[]{}), new Controller[]{}, new Rumbler[]{});
return new LinuxJoystickAbstractController(device, (Component[]) components.toArray(new Component[]{}), new Controller[]{}, new Rumbler[]{});
}
private final void enumerateJoystickControllers(List controllers) {
File[] joystick_device_files = enumerateJoystickDeviceFiles("/dev/input");
if (joystick_device_files == null || joystick_device_files.length == 0) {
if(joystick_device_files == null || joystick_device_files.length == 0) {
joystick_device_files = enumerateJoystickDeviceFiles("/dev");
if (joystick_device_files == null)
if(joystick_device_files == null)
return;
}
for (int i = 0; i < joystick_device_files.length; i++) {
for(int i = 0; i < joystick_device_files.length; i++) {
File event_file = joystick_device_files[i];
try {
String path = getAbsolutePathPrivileged(event_file);
LinuxJoystickDevice device = new LinuxJoystickDevice(path);
Controller controller = createJoystickFromJoystickDevice(device);
if (controller != null) {
if(controller != null) {
controllers.add(controller);
devices.add(device);
} else
device.close();
} catch (IOException e) {
} catch(IOException e) {
logln("Failed to open device (" + event_file + "): " + e.getMessage());
}
}
@ -428,7 +428,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
}
private static String getAbsolutePathPrivileged(final File file) {
return (String)AccessController.doPrivileged(new PrivilegedAction() {
return (String) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return file.getAbsolutePath();
}
@ -436,14 +436,19 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
}
private static File[] listFilesPrivileged(final File dir, final FilenameFilter filter) {
return (File[])AccessController.doPrivileged(new PrivilegedAction() {
return (File[]) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
File[] files = dir.listFiles(filter);
Arrays.sort(files, new Comparator(){
if(files == null) {
logln("dir " + dir.getName() + " exists: " + dir.exists() + ", is writable: " + dir.isDirectory());
files = new File[]{};
} else {
Arrays.sort(files, new Comparator() {
public int compare(Object f1, Object f2) {
return ((File)f1).getName().compareTo(((File)f2).getName());
return ((File) f1).getName().compareTo(((File) f2).getName());
}
});
}
return files;
}
});
@ -456,25 +461,25 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
return name.startsWith("event");
}
});
if (event_device_files == null)
if(event_device_files == null)
return;
for (int i = 0; i < event_device_files.length; i++) {
for(int i = 0; i < event_device_files.length; i++) {
File event_file = event_device_files[i];
try {
String path = getAbsolutePathPrivileged(event_file);
LinuxEventDevice device = new LinuxEventDevice(path);
try {
Controller controller = createControllerFromDevice(device);
if (controller != null) {
if(controller != null) {
controllers.add(controller);
devices.add(device);
} else
device.close();
} catch (IOException e) {
} catch(IOException e) {
logln("Failed to create Controller: " + e.getMessage());
device.close();
}
} catch (IOException e) {
} catch(IOException e) {
logln("Failed to open device (" + event_file + "): " + e.getMessage());
}
}
@ -482,11 +487,11 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
private final class ShutdownHook extends Thread {
public final void run() {
for (int i = 0; i < devices.size(); i++) {
for(int i = 0; i < devices.size(); i++) {
try {
LinuxDevice device = (LinuxDevice)devices.get(i);
LinuxDevice device = (LinuxDevice) devices.get(i);
device.close();
} catch (IOException e) {
} catch(IOException e) {
logln("Failed to close device: " + e.getMessage());
}
}