@@ -260,26 +260,33 @@ IOReturn VoodooI2CGoodixTouchDriver::goodix_end_cmd() {
260260
261261/* Ported from goodix.c */
262262IOReturn VoodooI2CGoodixTouchDriver::goodix_process_events () {
263- UInt8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
263+ // Allocate enough space for the status byte, all touches, and the extra button byte
264+ UInt8 data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS + 1 ];
264265
265- numTouches = goodix_ts_read_input_report (point_data );
266+ numTouches = goodix_ts_read_input_report (data );
266267 if (numTouches <= 0 ) {
267268 return kIOReturnSuccess ;
268269 }
269270
270- /*
271- * Bit 4 of the first byte reports the status of the capacitive
272- * Windows/Home button.
273- */
274- // bool home_pressed = point_data[0] & BIT(4);
271+ UInt8 keys = data[1 + numTouches * GOODIX_CONTACT_SIZE];
272+ if (GOODIX_KEYDOWN_EVENT (keys)) {
273+ stylusButton1 = GOODIX_IS_STYLUS_BTN_DOWN (keys, GOODIX_STYLUS_BTN1);
274+ stylusButton2 = GOODIX_IS_STYLUS_BTN_DOWN (keys, GOODIX_STYLUS_BTN2);
275+ }
276+ else {
277+ stylusButton1 = false ;
278+ stylusButton2 = false ;
279+ }
275280
281+ UInt8 *point_data;
276282 for (int i = 0 ; i < numTouches; i++) {
277- goodix_ts_store_touch (&point_data[1 + GOODIX_CONTACT_SIZE * i]);
283+ point_data = &data[1 + i * GOODIX_CONTACT_SIZE];
284+ goodix_ts_store_touch (point_data);
278285 }
279286
280287 if (numTouches > 0 ) {
281288 // send the event into the event driver
282- event_driver->reportTouches (touches, numTouches);
289+ event_driver->reportTouches (touches, numTouches, stylusButton1, stylusButton2 );
283290 }
284291
285292 return kIOReturnSuccess ;
@@ -306,7 +313,8 @@ int VoodooI2CGoodixTouchDriver::goodix_ts_read_input_report(UInt8 *data) {
306313 clock_get_uptime (×tamp);
307314 absolutetime_to_nanoseconds (timestamp, ×tamp_ns);
308315
309- retVal = goodix_read_reg (GOODIX_READ_COOR_ADDR, data, GOODIX_CONTACT_SIZE + 1 );
316+ // On the intial read, get the status byte, the first touch, and the pen buttons
317+ retVal = goodix_read_reg (GOODIX_READ_COOR_ADDR, data, 1 + GOODIX_CONTACT_SIZE + 1 );
310318 if (retVal != kIOReturnSuccess ) {
311319 IOLog (" %s::I2C transfer error starting coordinate read: %d\n " , getName (), retVal);
312320 return -1 ;
@@ -320,7 +328,8 @@ int VoodooI2CGoodixTouchDriver::goodix_ts_read_input_report(UInt8 *data) {
320328
321329 if (touch_num > 1 ) {
322330 data += 1 + GOODIX_CONTACT_SIZE;
323- retVal = goodix_read_reg (GOODIX_READ_COOR_ADDR + 1 + GOODIX_CONTACT_SIZE, data, GOODIX_CONTACT_SIZE * (touch_num - 1 ));
331+ // Read all touches, and 1 additional byte for the pen buttons
332+ retVal = goodix_read_reg (GOODIX_READ_COOR_ADDR + 1 + GOODIX_CONTACT_SIZE, data, GOODIX_CONTACT_SIZE * (touch_num - 1 ) + 1 );
324333 if (retVal != kIOReturnSuccess ) {
325334 IOLog (" %s::I2C transfer error during coordinate read: %d\n " , getName (), retVal);
326335 return -1 ;
@@ -346,6 +355,7 @@ void VoodooI2CGoodixTouchDriver::goodix_ts_store_touch(UInt8 *coor_data) {
346355 int input_x = get_unaligned_le16 (&coor_data[1 ]);
347356 int input_y = get_unaligned_le16 (&coor_data[3 ]);
348357 int input_w = get_unaligned_le16 (&coor_data[5 ]);
358+ bool type = GOODIX_TOOL_TYPE (coor_data[0 ]) == GOODIX_TOOL_PEN;
349359
350360 // Inversions have to happen before axis swapping
351361 if (ts->inverted_x )
@@ -357,12 +367,15 @@ void VoodooI2CGoodixTouchDriver::goodix_ts_store_touch(UInt8 *coor_data) {
357367 swap (input_x, input_y);
358368 }
359369
360- // IOLog("%s::Touch %d with width %d at %d,%d\n", getName(), id, input_w, input_x, input_y);
370+ #ifndef GOODIX_TOUCH_DRIVER_DEBUG
371+ IOLog (" %s::%s %d with width %d at %d,%d\n " , getName (), type ? " Stylus" : " Touch" , id, input_w, input_x, input_y);
372+ #endif
361373
362374 // Store touch information
363375 touches[id].x = input_x;
364376 touches[id].y = input_y;
365377 touches[id].width = input_w;
378+ touches[id].type = type;
366379}
367380
368381void VoodooI2CGoodixTouchDriver::stop (IOService* provider) {
@@ -374,7 +387,7 @@ void VoodooI2CGoodixTouchDriver::stop(IOService* provider) {
374387}
375388
376389IOReturn VoodooI2CGoodixTouchDriver::setPowerState (unsigned long powerState, IOService* whatDevice) {
377- #ifndef GOODIX_EVENT_DRIVER_DEBUG
390+ #ifndef GOODIX_TOUCH_DRIVER_DEBUG
378391 if (powerState == 0 ) {
379392 if (awake) {
380393 awake = false ;
0 commit comments