Skip to content

Examples

Izzatbek Mukhanov edited this page Jun 17, 2016 · 2 revisions

You can find more examples in apps or bindings folder

C++ example

#include <pointing/pointing.h>

#include <iomanip>
#include <stdexcept>

using namespace pointing ;

TransferFunction *func = 0 ;
TimeStamp::inttime last_time = 0 ;
bool button_pressed = false ;

void
pointingCallback(void * /*context*/, TimeStamp::inttime timestamp,
		 int input_dx, int input_dy, int buttons) {
  if (!func) return ;

  int output_dx=0, output_dy=0 ;
  func->applyi(input_dx, input_dy, &output_dx, &output_dy, timestamp) ;
  double delta = (double)(timestamp - last_time)/TimeStamp::one_millisecond ;
  std::cout << timestamp << " ns (" << TimeStamp::createAsStringFrom(timestamp) << "), " 
	    << std::setw(7) << delta << " ms later (" << std::setw(7) << (1000.0/delta) << " Hz), "
	    << "(" << std::setw(3) << input_dx << ", " << std::setw(3) << input_dy << ") counts"
	    << " -> (" << std::setw(3) << output_dx << ", " << std::setw(3) << output_dy << ") pixels, "
	    << "buttons: " << buttons << std::endl ;
  last_time = timestamp;
  button_pressed = buttons&PointingDevice::BUTTON_2 ;
}

int
main(int argc, char** argv) {
  try {

    if (argc < 3)
      std::cout 
	<< "Usage: " << argv[0] << " [inputdeviceURI [outputdeviceURI [transferfunctionURI]]]" << std::endl 
	<< "Using default values for some parameters" << std::endl ;

    // --- Pointing device ----------------------------------------------------

    PointingDevice *input = PointingDevice::create(argc>1?argv[1]:"any:?debugLevel=1") ;
    for (TimeStamp reftime, now;
	 !input->isActive() && now-reftime<15*TimeStamp::one_second; 
	 now.refresh())
      PointingDevice::idle(500) ;

    std::cout << std::endl << "Pointing device" << std::endl ;
    std::cout << "  " << input->getURI(true).asString() << std::endl
	      << "  " << input->getResolution() << " CPI, " 
	      << input->getUpdateFrequency() << " Hz" << std::endl 
	      << "  device is " << (input->isActive()?"":"not ") << "active" << std::endl 
	      << std::endl ;

    // --- Display device -----------------------------------------------------

    DisplayDevice *output = DisplayDevice::create(argc>2?argv[2]:"any:?debugLevel=1") ;

    double hdpi, vdpi;
    output->getResolution(&hdpi, &vdpi) ;
    DisplayDevice::Size size = output->getSize() ;
    DisplayDevice::Bounds bounds = output->getBounds() ;
    std::cout << std::endl << "Display device" << std::endl
	      << "  " << output->getURI(true).asString() << std::endl
	      << "  " << bounds.size.width << " x " << bounds.size.height << " pixels, "
	      << size.width << " x " << size.height << " mm" << std::endl
	      << "  " << hdpi << " x " << vdpi << " PPI, "
	      << output->getRefreshRate() << " Hz" << std::endl ;

    // --- Transfer function --------------------------------------------------

    func = TransferFunction::create(argc>3?argv[3]:"sigmoid:?debugLevel=2", input, output) ;

    std::cout << std::endl << "Transfer function" << std::endl
	      << "  " << func->getURI(true).asString() << std::endl
	      << std::endl ;

    // --- Ready to go --------------------------------------------------------

    input->setPointingCallback(pointingCallback) ;
    while (!button_pressed || 1)
      PointingDevice::idle(100) ; // milliseconds

    // --- Done ---------------------------------------------------------------

    delete input ;
    delete output ;
    delete func ;

  } catch (std::runtime_error e) {
    std::cerr << "Runtime error: " << e.what() << std::endl ;
  } catch (std::exception e) {
    std::cerr << "Exception: " << e.what() << std::endl ;
  }

  return 0 ;
}

Java console example

import org.libpointing.*;
import java.awt.geom.Point2D;
import java.awt.Dimension;
import java.awt.Rectangle;
import org.libpointing.event.PointingDeviceEvent;
import org.libpointing.event.PointingDeviceListener;

public class ConsoleExample {
	
	static class MyPointingDeviceListener implements PointingDeviceListener {
	    TransferFunction func;

	    public void setTransferFunction(TransferFunction tf) {
		   func = tf;
	    }

		@Override
		public void callback(PointingDeviceEvent e) {
			System.out.println("Callback from pointing device: " + ((PointingDevice)e.getSource()).getURI());
	        System.out.println("Mouse moved at " +
	        		e.getTimeStamp() + " nano s, dx = " +
	        		e.getDx() + " mickeys, dy = " +
	        		e.getDy() + " mickeys, buttons = " + e.getButtons());
	        Point2D.Double p = func.applyd(e.getDx(), e.getDy(), e.getTimeStamp());
	        System.out.println("Corresponding cursor movement using transfer function : dx = " + p.x + " pixels dy = " + p.y + " pixels");
		} 
	}
	
    public static void main(String args[]) {
	    DisplayDevice output = new DisplayDevice("any:");
	    System.out.println(output.getURI() + ", resolution = " + output.getResolution() + " PPI");

	    // Pointing device
	    PointingDevice input = new PointingDevice("any:?debugLevel=1");
	    System.out.println(input.getURI());

	    // Transfer function
	    TransferFunction func = new TransferFunction("sigmoid:", input, output);
	    System.out.println("Transfer function URI: " + func.getURI());
	 
	    MyPointingDeviceListener listener = new MyPointingDeviceListener();
	    listener.setTransferFunction(func);
	    input.addPointingDeviceListener(listener);
		while (true) {
			PointingDevice.idle(100);
	  	}
	}
}

Java manager example

import java.util.*;

import org.libpointing.*;
import org.libpointing.event.*;

public class ManagerExample {
	
	static class MyPointingDeviceManagerListener implements PointingDeviceManagerListener {

	    public void deviceAdded(PointingDeviceDescriptor desc)
	    {
	        //System.out.println(Arrays.toString(getDeviceList()));
	        System.out.println("Device " + desc.vendor + " - " + desc.product);
	        System.out.println("  was added with URI: " + desc.devUri);
	        System.out.println("  with vendor Id: " + desc.vendorID + " and product Id: " + desc.productID + "\n");
	    }

	    public void deviceRemoved(PointingDeviceDescriptor desc)
	    {
	        //System.out.println(Arrays.toString(getDeviceList()));
	        System.out.println("Device " + desc.vendor + " - " + desc.product);
	        System.out.print("  was removed with URI: " + desc.devUri + "\n");
	    }
	}
	
  public static void main(String args[]) {

    PointingDevice input = new PointingDevice("any:");
    
    PointingDeviceManager manager = PointingDeviceManager.getInstance();
    manager.addPointingDeviceManagerListener(new MyPointingDeviceManagerListener());
    // It is better to use callbacks, not getDeviceList
    // Because devices are found with delay, not immediately
    for (PointingDeviceDescriptor desc : manager.getDeviceList()) {
        System.out.println(desc.vendor + " - " + desc.product);
    }
    //System.out.println(Arrays.toString(manager.getDeviceList()));

	while (true) {
		PointingDevice.idle(100);
	}
  }
}

Python console example

from pylibpointing import PointingDevice, DisplayDevice, TransferFunction
from pylibpointing import PointingDeviceManager, PointingDeviceDescriptor

def cb_man(desc, wasAdded):
	print desc
	print "was added" if wasAdded else "was removed"

pm = PointingDeviceManager()
PointingDevice.idle(100)
pm.addDeviceUpdateCallback(cb_man)

for desc in pm:
	print desc

pdev = PointingDevice("any:")
ddev = DisplayDevice.create("any:")
tfct = TransferFunction("sigmoid:", pdev, ddev)

def cb_fct(timestamp, dx, dy, button):
    rx,ry=tfct.applyd(dx, dy, timestamp)
    print("%s: %d %d %d -> %d %d"%(str(timestamp), dx, dy, button, rx, ry ))

pdev.setCallback(cb_fct)
print("Move the mouse of Press CTRL+C to exit")
for i in range(0, 100000):
    PointingDevice.idle(10)

Node.js console example

var pointing = require('libpointing');

if (process.argv.length < 5)
    console.log("Usage: node", process.argv[1], "[inputdeviceURI [outputdeviceURI [transferfunctionURI]]]")

var input = pointing.PointingDevice((process.argv[2]) ? process.argv[2] : "any:")
var output = pointing.DisplayDevice((process.argv[3]) ? process.argv[3] : "any:");
var tFunc = pointing.TransferFunction((process.argv[4]) ? process.argv[4] : "system:", input, output);

input.setPointingCallback(function(timestamp, dx, dy, buttons) {
    var pixels = tFunc.applyi(dx, dy, timestamp);
    console.log(timestamp, dx, dy, buttons, " -> ", pixels.dx, pixels.dy);
});

var manager = pointing.PointingDeviceManager().addDeviceUpdateCallback(
    function(deviceDescriptor, wasAdded) {
        console.log(deviceDescriptor, wasAdded);
    }
);