1# mtdev - Multitouch Protocol Translation Library 2 3The mtdev is a stand-alone library which transforms all variants of kernel MT events to the slotted type B protocol. The events put into mtdev may be from any MT device, specifically type A without contact tracking, type A with contact tracking, or type B with contact tracking. 4 5The bulk of the mtdev code has been out there since 2008, as part of the Multitouch X Driver. With this package, finger tracking and seamless MT protocol handling is available under a free license. 6 7 8## Directory structure 9 10``` 11README.md English description 12README_zh.md Chinese description 13include/ API Definition 14src/ Encapsulation layer implementation 15README.OpenSource Open Source Description 16``` 17 18## How OpenHarmony integrates mtdev 19### 1.Header file import 20```c 21#include <mtdev.h> 22``` 23### 2.BUILD.gn add Reference 24```c 25public_deps += ["//third_party/mtdev:libmtdev"] 26``` 27### 3.Example of calling mtdev function 28```c 29//year-proof millisecond event time 30typedef uint64_t mstime_t; 31 32static int use_event(const struct input_event *ev) 33{ 34#if 0 35 return ev->type == EV_ABS && mtdev_is_absmt(ev->code); 36#else 37 return 1; 38#endif 39} 40 41//print events 42static void print_event(const struct input_event *ev) 43{ 44 static const mstime_t ms = 1000; 45 static int slot; 46 mstime_t evtime = ev->input_event_usec / ms + ev->input_event_sec * ms; 47 if (ev->type == EV_ABS && ev->code == ABS_MT_SLOT) 48 slot = ev->value; 49 fprintf(stderr, "%012llx %02d %01d %04x %d\n", 50 evtime, slot, ev->type, ev->code, ev->value); 51} 52 53#define CHECK(dev, name) \ 54 if (mtdev_has_mt_event(dev, name)) \ 55 fprintf(stderr, " %s\n", #name) 56 57//check prop 58static void show_props(const struct mtdev *dev) 59{ 60 fprintf(stderr, "supported mt events:\n"); 61 CHECK(dev, ABS_MT_SLOT); 62 CHECK(dev, ABS_MT_TOUCH_MAJOR); 63 CHECK(dev, ABS_MT_TOUCH_MINOR); 64 CHECK(dev, ABS_MT_WIDTH_MAJOR); 65 CHECK(dev, ABS_MT_WIDTH_MINOR); 66 CHECK(dev, ABS_MT_ORIENTATION); 67 CHECK(dev, ABS_MT_POSITION_X); 68 CHECK(dev, ABS_MT_POSITION_Y); 69 CHECK(dev, ABS_MT_TOOL_TYPE); 70 CHECK(dev, ABS_MT_BLOB_ID); 71 CHECK(dev, ABS_MT_TRACKING_ID); 72 CHECK(dev, ABS_MT_PRESSURE); 73 CHECK(dev, ABS_MT_DISTANCE); 74} 75 76//Create mtdev, check prop, get and print mt events 77static void loop_device(int fd) 78{ 79 struct mtdev dev; 80 struct input_event ev; 81 int ret = mtdev_open(&dev, fd); 82 if (ret) { 83 fprintf(stderr, "error: could not open device: %d\n", ret); 84 return; 85 } 86 show_props(&dev); 87 //while the device has not been inactive for five seconds 88 while (!mtdev_idle(&dev, fd, 5000)) { 89 //extract all available processed events 90 while (mtdev_get(&dev, fd, &ev, 1) > 0) { 91 if (use_event(&ev)) 92 print_event(&ev); 93 } 94 } 95 mtdev_close(&dev); 96} 97 98//main function 99int main(int argc, char *argv[]) 100{ 101 int fd; 102 if (argc < 2) { 103 fprintf(stderr, "Usage: mtdev <device>\n"); 104 return -1; 105 } 106 fd = open(argv[1], O_RDONLY | O_NONBLOCK); 107 if (fd < 0) { 108 fprintf(stderr, "error: could not open device\n"); 109 return -1; 110 } 111 if (ioctl(fd, EVIOCGRAB, 1)) { 112 fprintf(stderr, "error: could not grab the device\n"); 113 return -1; 114 } 115 loop_device(fd); 116 ioctl(fd, EVIOCGRAB, 0); 117 close(fd); 118 return 0; 119} 120``` 121 122## mtdev Working with documents 123 124 125Official API http://bitmath.org/code/mtdev/ 126 127 128## License 129 130见 [LICENSE](MIT(X11)License). 131 132--- 133 134The mtdev library was released under the MIT licence in collaboration with Canonical, Ltd.