Scriptel ScripTouch ProScript C Library This documentation contains information about the ScripTouch ProScript C Library.

example-image-gdiplus.cpp

This is a simple example of how to connect to a ScripTouch device and output a PNG image using the GDI+ drawing library included in Windows.

#include <stdio.h>
#include <Windows.h>
#include <Gdiplus.h>
extern "C" {
#include "scriptel-proscript.h"
}
scriptel_hid_feature_coordinate_range range;
ULONG_PTR gdiplusToken;
CLSID pngClsid;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::Bitmap* image;
Gdiplus::Graphics* graphics;
Gdiplus::Pen* pen;
double lastX = -1;
double lastY = -1;
//Finds the CLSID of a Gdiplus output format type by mime type.
//Example taken directly from MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
UINT num = 0;
UINT size = 0;
Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
Gdiplus::GetImageEncodersSize(&num, &size);
if(size == 0) {
return -1;
}
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL) {
return -1;
}
Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j) {
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) {
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
void input_callback(scriptel_device* device, scriptel_input_report* report, unsigned char report_id) {
if(report_id == SCRIPTEL_INPUT_EXTENDEDCOORDINATE) {
//We got pen input, lets adjust the coordinates into screen aspect.
double x = device->display_info.width * ((report->extended_coordinate.x - range.x_min) / (double)(range.x_max - range.x_min));
double y = device->display_info.height * ((report->extended_coordinate.y - range.y_min) / (double)(range.y_max - range.y_min));
printf("Got a coordinate from the device: %4.2f, %4.2f\n", x, y);
if(report->extended_coordinate.sample_info.pen.tip_switch) {
if(lastX < 0) {
//Pen Down
Gdiplus::PointF pt((float)x,(float)y);
graphics->DrawLine(pen, pt, pt);
} else {
//Pen Move
Gdiplus::PointF pt1((float)lastX,(float)lastY);
Gdiplus::PointF pt2((float)x,(float)y);
graphics->DrawLine(pen, pt1, pt2);
}
lastX = x;
lastY = y;
} else {
//Pen Up
Gdiplus::PointF pt1((float)lastX,(float)lastY);
Gdiplus::PointF pt2((float)x,(float)y);
graphics->DrawLine(pen, pt1, pt2);
lastX = -1;
lastY = -1;
}
} else if(report_id == SCRIPTEL_INPUT_BUTTONPRESS) {
//A button was pressed, if it's the OK button we'll close the device.
char* caption = report->button_press.region->button.caption;
printf("Button pressed: %s\n", caption);
if(strcmp(caption, "OK") == 0) {
image->Save(L"signature.png", &pngClsid, NULL);
//The OK button was hit, close the device.
scriptel_close_device(device);
} else {
//The cancel button was hit, clear the image.
graphics->Clear(Gdiplus::Color::Transparent);
}
}
}
int main() {
scriptel_device* device;
//Initialize the library.
scriptel_init();
//List the devices.
if(scriptel_list_devices(&device) == SCRIPTEL_CODE_SUCCESS) {
//Test to see if we have more than zero devices.
if(device != NULL) {
printf("Found %i devices.\n", scriptel_count_device_list(device));
//Lets open the first device and get some information from it.
if(scriptel_open_device(device) == SCRIPTEL_CODE_SUCCESS) {
printf("Successfully connected to %s - %s at: %s\n", device->manufacturer, device->model, device->path);
//Register an input callback, this will be called when we get input from the pad.
scriptel_register_input_callback(device, input_callback);
//Get the coordinate scaling information we'll need later.
range = scriptel_get_coordinate_range(device);
//Start up GDI+
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
image = new Gdiplus::Bitmap(device->display_info.width, device->display_info.height, PixelFormat32bppARGB);
graphics = Gdiplus::Graphics::FromImage(image);
pen = new Gdiplus::Pen(Gdiplus::Color::Black, 1);
graphics->Clear(Gdiplus::Color::Transparent);
graphics->SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
GetEncoderClsid(L"image/png", &pngClsid);
//Wait for input from the pad.
while(device->open) {
scriptel_device_read(device);
}
//Free GDI+
delete graphics;
delete image;
delete pen;
Gdiplus::GdiplusShutdown(gdiplusToken);
scriptel_free_device_list(device);
} else {
printf("Problem opening device: %s\n", scriptel_get_last_error());
return 1;
}
} else {
printf("No devices found!\n");
return 2;
}
} else {
printf("Problem enumerating devices: %s\n", scriptel_get_last_error());
return 3;
}
}
SCRIPTEL_INPUT_BUTTONPRESS
@ SCRIPTEL_INPUT_BUTTONPRESS
The report identifier of the button press input report.
Definition: scriptel-hid-reports.h:297
SCRIPTEL_INPUT_EXTENDEDCOORDINATE
@ SCRIPTEL_INPUT_EXTENDEDCOORDINATE
The report identifier of the extended coordinate input report.
Definition: scriptel-hid-reports.h:234
scriptel-proscript.h
This file contains the headers for the core of the Scriptel ScripTouch ProScript library.
scriptel_init
void scriptel_init(void)
This function should be called prior to using any other library functions.
scriptel_count_device_list
unsigned int scriptel_count_device_list(scriptel_device *list)
This function counts the number of devices in a given device list.
scriptel_get_coordinate_range
scriptel_hid_feature_coordinate_range scriptel_get_coordinate_range(scriptel_device *device)
This function gets the logical coordinate range that will be returned from the device when points are...
scriptel_list_devices
scriptel_error_code scriptel_list_devices(scriptel_device **start)
This function takes a pointer and will attempt to enumerate any attached ScripTouch devices.
scriptel_close_device
scriptel_error_code scriptel_close_device(scriptel_device *device)
This function attempts to close a local device that was previously opened.
scriptel_open_device
scriptel_error_code scriptel_open_device(scriptel_device *device)
This function attempts to open a local device returned by scriptel_list_devices().
scriptel_get_last_error
char * scriptel_get_last_error(void)
This function gets the last error message generated by the library.
scriptel_free_device_list
void scriptel_free_device_list(scriptel_device *device_list)
This function crawls a scriptel_device list and attempts to free any library managed memory associate...
SCRIPTEL_CODE_SUCCESS
@ SCRIPTEL_CODE_SUCCESS
This code indicates that the function did succeed.
Definition: scriptel-proscript.h:96
scriptel_device_read
scriptel_error_code scriptel_device_read(scriptel_device *device)
This function attempts to read from the passed in device.
scriptel_register_input_callback
void scriptel_register_input_callback(scriptel_device *device, scriptel_input_callback callback)
This function registers an input callback for a device.
scriptel_device_region_button::caption
char caption[100]
Caption of the button.
Definition: scriptel-hid-reports.h:2585
scriptel_device
This structure represents a physically attached Scriptel ScripTouch device.
Definition: scriptel-proscript.h:284
scriptel_device::path
char * path
The path to the device.
Definition: scriptel-proscript.h:290
scriptel_device::open
unsigned char open
Indicates whether or not the device is in the open state.
Definition: scriptel-proscript.h:319
scriptel_device::display_info
scriptel_hid_feature_display_info display_info
Cached display information from the device at open time.
Definition: scriptel-proscript.h:333
scriptel_device::model
char * model
The textual name of the device as returned by the operating system's HID driver.
Definition: scriptel-proscript.h:305
scriptel_device::manufacturer
char * manufacturer
The textual name of the manufacturer as returned by the operating system's HID driver.
Definition: scriptel-proscript.h:310
scriptel_hid_feature_coordinate_range
This report is capable of getting the coordinate range from the device.
Definition: scriptel-hid-reports.h:1582
scriptel_hid_feature_coordinate_range::x_max
unsigned short x_max
Maximum logical value in the horizontal direction to be returned by the device.
Definition: scriptel-hid-reports.h:1594
scriptel_hid_feature_coordinate_range::y_max
unsigned short y_max
Maximum logical value in the vertical direction to be returned by the device.
Definition: scriptel-hid-reports.h:1602
scriptel_hid_feature_coordinate_range::y_min
unsigned short y_min
Minimum logical value in the vertical direction to be returned by the device.
Definition: scriptel-hid-reports.h:1598
scriptel_hid_feature_coordinate_range::x_min
unsigned short x_min
Minimum logical value in the horizontal direction to be returned by the device.
Definition: scriptel-hid-reports.h:1590
scriptel_hid_feature_display_info::width
unsigned short width
Width of the on board display in pixels.
Definition: scriptel-hid-reports.h:2341
scriptel_hid_feature_display_info::height
unsigned short height
Height of the on board display in pixels.
Definition: scriptel-hid-reports.h:2347
scriptel_hid_input_button_press::region
scriptel_device_region_info * region
Button that was pressed.
Definition: scriptel-hid-reports.h:3712
scriptel_hid_input_extended_coordinate::x
unsigned short x
Horizontal component of the sample.
Definition: scriptel-hid-reports.h:897
scriptel_hid_input_extended_coordinate::y
unsigned short y
Vertical component of the sample.
Definition: scriptel-hid-reports.h:901
scriptel_hid_input_extended_coordinate::sample_info
scriptel_sample_info sample_info
Information about the sample that produced this coordinate.
Definition: scriptel-hid-reports.h:893
scriptel_sample_info_pen::tip_switch
unsigned char tip_switch
This will be true if the pen is currently against the screen.
Definition: scriptel-hid-reports.h:799
scriptel_device_region_info::button
scriptel_device_region_button button
Button region information.
Definition: scriptel-hid-reports.h:2625
scriptel_input_report
This union contains all valid input report types.
Definition: scriptel-hid-reports.h:3728
scriptel_input_report::extended_coordinate
scriptel_hid_input_extended_coordinate extended_coordinate
Extended coordinate report.
Definition: scriptel-hid-reports.h:3740
scriptel_input_report::button_press
scriptel_hid_input_button_press button_press
Button press report.
Definition: scriptel-hid-reports.h:3768
scriptel_sample_info::pen
scriptel_sample_info_pen pen
Sample information relevant to only pen input.
Definition: scriptel-hid-reports.h:853