Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Conzar (posted 2014-01-31 08:17:39, viewed 104 times)

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  
/* 
 * See COPYING in top-level directory.
 */
package com.monkygames.kbmaster.util;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.Event;
import net.java.games.input.EventQueue;
import net.java.games.input.LinuxEnvironmentPlugin;

/**
 * Scans the hardware and prints out the information.
 * @version 1.0
 */
public class ScanHardware implements Runnable{

// ============= Class variables ============== //
    private Controller[] pollControllers;
// ============= Constructors ============== //
    public ScanHardware(String deviceName, boolean doPoll){
   pollControllers = scanHardware(deviceName);
   if(doPoll){
       if(pollControllers == null){
      System.out.println("Device: "+deviceName+" not found");
      return;
       }
       System.out.println("====== Polling ====== ");
       Thread thread = new Thread(this);
       thread.start();
   }
    }
// ============= Public Methods ============== //
// ============= Protected Methods ============== //
// ============= Private Methods ============== //
    /**
     * Scans all hardware and writes out the components to standard out.
     * @param deviceName the name of the device.
     * @return the controllers specified by the devicename.
     */
    private Controller[] scanHardware(String deviceName){
   ArrayList<Controller> returnControllers = new ArrayList<>();
   Controller[] controllers = LinuxEnvironmentPlugin.getDefaultEnvironment().getControllers();
   for(Controller controller: controllers){
       // Check if this controller should be returned for polling.
       if(deviceName != null && controller.getName().equals(deviceName)){
      returnControllers.add(controller);
       }
       // if no device was specified, than print the controller.
       if(deviceName == null){
      printControllerInformation(controller);
       }else if(controller.getName().equals(deviceName)){
      // if its the controller, print it to the screen.
      printControllerInformation(controller);
       }
   }
   if(deviceName == null || returnControllers.size()== 0){
       return null;
   }
   Controller[] returnControllersArray = new Controller[returnControllers.size()];
   for(int i = 0; i < returnControllers.size(); i++){
       returnControllersArray[i] = returnControllers.get(i);
   }
   return returnControllersArray;
    }
    /**
     * Gets the component details as a string.
     * @param component gets the details from this object.
     * @return the details of the component.
     */
    private String getComponentDetails(Component component){
   String data = "Name["+component.getName()+
            "] Identifier["+component.getIdentifier().getName();
   if(component.isAnalog()){
       data += "] Analog";
   }else{
       data += "] Digital";
   }
   if(component.isRelative()){
       data += " Relative";
   }else{
       data += " Absolute";
   }
   String className = component.getIdentifier().getClass().getName();
   data += " Class["+className+"]";
   return data;
    }
    /**
     * Prints the controller information to the screen.
     * @param controller the controller to print.
     */
    private void printControllerInformation(Controller controller){
   System.out.println("Name: "+controller.getName()+" Type: "+controller.getType());
   Component[] components = controller.getComponents();
   System.out.println("=== Inputs ===");
   System.out.println("Number of Inputs: "+components.length);
   for(Component component: components){
       if(component.getName().equals("Unknown")){
      continue;
       }
       String data = getComponentDetails(component);
       System.out.println(data);
   }
   System.out.println("===============");
    }
// ============= Implemented Methods ============== //
    @Override
    public void run(){
   Event event = new Event();
   while(true){
       for(Controller controller: pollControllers){
      controller.poll();
      String out = "["+controller.getName()+":"+controller.getType()+"] ";
      String data = null;
      EventQueue queue = controller.getEventQueue();
      while(queue.getNextEvent(event)){
          Component component = event.getComponent();
          data += "\n"+(out + getComponentDetails(component));
      }
      if(data != null){
          System.out.print(out+data);
      }
       }
       try {
      // print out every 0.1 seconds
      Thread.sleep(100);
       } catch (InterruptedException ex) {
      Logger.getLogger(ScanHardware.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
    }
// ============= Extended Methods ============== //
// ============= Internal Classes ============== //
// ============= Static Methods ============== //
    public static void main(String[] args){
   boolean doPoll = true;
   String deviceName = null;
   if(args.length == 0){
       doPoll = false;
   }else{
       deviceName = args[0];
   }
   ScanHardware scanHardware = new ScanHardware(deviceName,doPoll);
    }
}
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 noexpandtab
 */





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (19 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!