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