Linux I/O For Java

A JNA based library providing access to some low-level Linux subsystems, including :- * UInput. Create virtual devices such as keyboards, mice and touchpads and emit events from them as if they were real. * Evdev. Read events from various devices such as keyboards, mice, touchpads, and all other devices exposed by Linux. * Frame Buffer. Write directly to the Linux framebuffer. Allows graphical user interfaces without X being present. As from version 2.0, this library is now in uk.co.bithatch namespace. Version 2.1 and above support Java 8, and module Java 9 and above.

License

License

GroupId

GroupId

uk.co.bithatch
ArtifactId

ArtifactId

linuxio4j
Last Version

Last Version

2.1
Release Date

Release Date

Type

Type

jar
Description

Description

Linux I/O For Java
A JNA based library providing access to some low-level Linux subsystems, including :- * UInput. Create virtual devices such as keyboards, mice and touchpads and emit events from them as if they were real. * Evdev. Read events from various devices such as keyboards, mice, touchpads, and all other devices exposed by Linux. * Frame Buffer. Write directly to the Linux framebuffer. Allows graphical user interfaces without X being present. As from version 2.0, this library is now in uk.co.bithatch namespace. Version 2.1 and above support Java 8, and module Java 9 and above.
Project URL

Project URL

https://github.com/bithatch/linuxio4j
Project Organization

Project Organization

Bithatch
Source Code Management

Source Code Management

https://github.com/bithatch/linuxio4j

Download linuxio4j

How to add to project

<!-- https://jarcasting.com/artifacts/uk.co.bithatch/linuxio4j/ -->
<dependency>
    <groupId>uk.co.bithatch</groupId>
    <artifactId>linuxio4j</artifactId>
    <version>2.1</version>
</dependency>
// https://jarcasting.com/artifacts/uk.co.bithatch/linuxio4j/
implementation 'uk.co.bithatch:linuxio4j:2.1'
// https://jarcasting.com/artifacts/uk.co.bithatch/linuxio4j/
implementation ("uk.co.bithatch:linuxio4j:2.1")
'uk.co.bithatch:linuxio4j:jar:2.1'
<dependency org="uk.co.bithatch" name="linuxio4j" rev="2.1">
  <artifact name="linuxio4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='uk.co.bithatch', module='linuxio4j', version='2.1')
)
libraryDependencies += "uk.co.bithatch" % "linuxio4j" % "2.1"
[uk.co.bithatch/linuxio4j "2.1"]

Dependencies

compile (2)

Group / Artifact Type Version
net.java.dev.jna : jna jar 5.3.1
net.java.dev.jna : jna-platform jar 5.3.1

Project Modules

There are no modules declared in this project.

linuxio4j

Java Interface to parts of the Linux I/O system, specifically UInput/Evdev and the Framebuffer.

Configuring your project

The library is available in Maven Central, so configure your project according to the build system you use.

Version 2.0 was released as Java 9 or higher only, but as from version 2.1-SNAPSHOT, Java 8 compatibility is restored through the use of a multi release jar (MRJAR). So for Java 9 modularity support, use anything from version 2.0. For Java 8, use any version except version 2.0. Be aware though, that Java 8 compatibility may be completely removed at some future version.

Maven

	<dependency>
		<groupId>uk.co.bithatch</groupId>
		<artifactId>linuxio4j</artifactId>
		<version>2.1</version>
	</dependency>

Or for the current development version (will be occasionally updated between releases).

	<dependency>
		<groupId>uk.co.bithatch</groupId>
		<artifactId>linuxio4j</artifactId>
		<version>2.1</version>
	</dependency>

Try It

You can run the test application from the command line (requires Maven).

mvn compile exec:java

If all is well, you'll see a simple menu.

1. FB List GraphicsDevice
2. FB random colours (full speed)
3. FB noise (direct to buffer)
4. FB Test Card
5. UINPUT Keyboard
6. UINPUT Pointer
7. UINPUT All
8. UINPUT Virtual Device

Usage

To integrate with your own project, here are some basics.

Framebuffer

To find all the frame buffer devices :-

List<FrameBuffer> fbs = FrameBuffer.getFrameBuffers();

To get the resolution of a buffer device :-

	try(FrameBuffer fb = FrameBuffer.getFrameBuffer()) {
		int xres = fb.getVariableScreenInfo().xres;
		int yres = fb.getVariableScreenInfo().xres;
		System.out.println("The buffer is " + xres + " x " + yres);
	}

To write a whole screen of random noise directly to the display :-

	try(FrameBuffer fb = FrameBuffer.getFrameBuffer()) {
	
		/* Get a whole page of random numbers */
		byte[] rnd = new byte[fb.getVariableScreenInfo().yres * fb.getVariableScreenInfo().xres * Math.max(1, fb.getVariableScreenInfo().bits_per_pixel / 8)];
		new Random().nextBytes(rnd);
		
		/* Write the noise */
		fb.getBuffer().put(rnd);
	}
	

To get a Graphics to draw on :-

	try(FrameBuffer fb = FrameBuffer.getFrameBuffer()) {
		Graphics2D g = fb.getGraphics();
		g.setColor(Color.RED);
		g.drawRect(100, 100, 400, 400);
		fb.commit();
	}
	

UInput

To grab and read mouse events :-

   try(InputDevice mouse = InputDevice.getFirstPointerDevice()) {
		 mouse.open();
        mouse.grab();
        while (true) {
			Event ev = mouse.nextEvent();
			if (ev == null) {
				break;
			}
			System.out.println(ev);
		}
	}

To create a new virtual keyboard device and emit some keys :-

try (InputDevice dev = new InputDevice("LinuxIO Test", (short) 0x1234, (short) 0x5678)) {

	dev.getCapabilities().put(
		Type.EV_KEY, new LinkedHashSet<>(Arrays.asList(
			EventCode.KEY_H,
			EventCode.KEY_E, 
			EventCode.KEY_L, 
			EventCode.KEY_O, 
			EventCode.KEY_W, 
			EventCode.KEY_R, 
			EventCode.KEY_D, 
			EventCode.KEY_ENTER)));
	dev.open();
	dev.typeKeys(
		EventCode.KEY_H, EventCode.KEY_E, EventCode.KEY_L, EventCode.KEY_L, 	EventCode.KEY_O,
		EventCode.KEY_W, EventCode.KEY_O, EventCode.KEY_R, EventCode.KEY_L, EventCode.KEY_D,
		EventCode.KEY_ENTER);
}

Non-blocking monitoring of multiple devices (internally a single thread is created).

		for (InputDevice device : InputDevice.getAllPointerDevices()) {
			device.open();
			device.grab();
			InputController.getInstance().add(device, (d, e) -> {
				System.err.println(d + " = " + e);
			});
		}

History

2.1-SNAPSHOT

  • Added support for creating virtual devices.
  • Restored Java8 compatibility
  • InputEventCode renamed to EventCode and turned into an enum. More convenience methods for typing keys.
  • UInputDevice renamed to InputDevice.
  • UInputController renamed to InputController.
  • Added supported for properties and absolute value.

2.0

  • Modularised for Java9+
uk.co.bithatch

Bithatch

Open source libraries and applications

Versions

Version
2.1
2.0