Friday, December 20, 2013

Fedora 20: Touchscreen doesn't work after lid closed

Fedora 20 was released recently and finally I've got the balls to install it in my new touchscreen laptop (which is preinstalled with Windows 8). The installation step is rather easy, takes me only few steps and I was like "that's it? It's installed?"

It looks nice, but there're some issues with my touchscreen. It appears that touchscreen will not work after I close my laptop lid, or probably when the laptop is put to sleep. So my early assumption is that whatever thing that should be handling the input from the touchscreen is not restarted. I'm almost correct, but the actual reason is that the touchscreen device was not mapped to the output device (i.e. the screen).

To solve this, first you need to know the name of your output device using the following command:
$ xrandr
Screen 0: minimum 320 x 200, current 1366 x 768, maximum 8192 x 8192
LVDS1 connected primary 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
   1366x768       60.0*+
   1024x768       60.0 
   800x600        60.3     56.2 
   640x480        59.9 
VGA1 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
DP1 disconnected (normal left inverted right x axis y axis)


in my case, LVDS1 is my laptop screen. Then you need to know your touchscreen ID using the following command:
$ xinput list
⎡ Virtual core pointer                        id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                  id=4    [slave  pointer  (2)]
⎜   ↳ Atmel Atmel maXTouch Digitizer              id=11    [slave  pointer  (2)]
⎜   ↳ ETPS/2 Elantech Touchpad                    id=14    [slave  pointer  (2)]
⎣ Virtual core keyboard                       id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard                 id=5    [slave  keyboard (3)]
    ↳ Power Button                                id=6    [slave  keyboard (3)]
    ↳ Video Bus                                   id=7    [slave  keyboard (3)]
    ↳ Video Bus                                   id=8    [slave  keyboard (3)]
    ↳ Sleep Button                                id=9    [slave  keyboard (3)]
    ↳ USB Camera                                  id=10    [slave  keyboard (3)]
    ↳ Asus WMI hotkeys                            id=12    [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard                id=13    [slave  keyboard (3)]


from the output, my touchscreen would be Atmel maxTouch with id 11.

Now that you have both my output and input id, you can now map the input to the out using the following command:
$ xinput --map-to-output 11 LVDS1

Now test your touchscreen, it should've work after you execute the command.

Hope this helps. :)

Monday, August 5, 2013

Java RMI: Client timeout

Creating a timeout for Java RMI client is important  since LocateRegistry.getRegistry would make your application unresponsive when the Java RMI server is not available. To create a timeout at the client-side, I suggest you to use ExecutorService. The following codes is an example how to use it:

Suppose you have your localhost listening on port 1099 for RMI and your server serve an interface

public interface RMIService() {
  public String sayHello throws RemoteException;
}

Then at your client app, you do this

ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new Callable<Object>() {
 

  @Override
  public Object call() throws Exception {
    String host = "localhost";
    int port = 1099;
    RMIService service = (RMIService) LocateRegistry

      .getRegistry(host, port).lookup("RMIService");
    System.err.println(service.sayHello());
    return null;
  }


});
 

try {
  future.get(5, TimeUnit.SECONDS);

} catch (InterruptedException | TimeoutException |
    ExecutionException e) {
  System.err.println(e.getMessage());

}

The above code will try executing the remote method and will throw exception if  the lookup time exceeds 5 seconds.

Hope this helps. :)

Monday, February 18, 2013

Java EE: Persists entity beans with generated primary key (SQL Server)

In developing an enterprise application in Java EE, sometimes you would come across a database table with auto-generated primary key. If you're using NetBeans to generante entity beans from a database, you would get something like this:

@Entity
@Table(name = "your_table_name")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "YourTableName.findByTheId", ... )})
public class YourTableName implements Serializable {
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "the_id")
    private Integer theId;


    ...
}

But if you try to persists an instance of the entity bean, you would get an error and you would not be able to alter your database. This is because when you try to persist the instance you would do this

YourTableName yourTableName = new YourTableName();
yourTableName.setFooBarField("foobar"); //example field in table

em.persists(yourTableName);

You could not set the yourself, since it's suppose to be autogenerated by the database. So, in order to solve this, you have to edit the generated entity beans, by adding GeneratedValue annotation to the primary key variable, and also remove the NotNull annotation, like so:

@Id
@Basic(optional = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "the_id")
private Integer theId;

You have to remove @NotNull since, during persisting, you does not set the value for the primary key. Not removing this would throw an error (at least in my case).

Hope this would help. :) 

Tuesday, February 5, 2013

Java: Base64 Encode

I used this function to encode a string using base64 encoding for my java card applet.

public static String base64Encode(String s) {
    String table="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            + "abcdefghijklmnopqrstuvwxyz0123456789+/";
    String b="";
    String r="";
    int pad;
       
    for (int c : s.toCharArray()) {
        b+=rjust(Integer.toBinaryString(c), 8, "0");
    }
           
    for (int i=0; i<b.length(); i+=6) {
        r+=table.charAt(Integer.parseInt(ljust(b.substring(i, i+6), 6, "0"), 2));
        pad=6-b.substring(i, i+6).length();
        switch (pad) {
            case 0: r+=""; break;
            case 4: r+="=="; break;
            case 2: r+="="; break; }                  
    }
    return r;

}

Both rjust and ljust that you can find in the function above are custom functions similar to python's rjust and ljust.

Monday, January 28, 2013

SQL Server notes

Rename a field (or column) in a table:
EXEC sp_rename 'table_name.old_name', 'new_name', 'COLUMN';

sp_rename is a SQL Server function name.