Skip to content

Commit

Permalink
fix: apply screen rotation from framebuffer, fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
lazd committed Jan 9, 2020
1 parent 9c3a0c0 commit 50b24ba
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 38 deletions.
2 changes: 2 additions & 0 deletions VoodooI2CGoodix/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<string>1.4</string>
<key>com.apple.iokit.IOHIDFamily</key>
<string>2.0</string>
<key>com.apple.iokit.IOGraphicsFamily</key>
<string>1.0.0b1</string>
<key>com.apple.kpi.iokit</key>
<string>14</string>
<key>com.apple.kpi.libkern</key>
Expand Down
64 changes: 62 additions & 2 deletions VoodooI2CGoodix/VoodooI2CGoodixEventDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// Copyright © 2020 lazd. All rights reserved.
//

#include "VoodooI2CGoodixEventDriver.hpp"

#include "VoodooI2CGoodixEventDriver.hpp"
#include <IOKit/hid/IOHIDInterface.h>
#include <IOKit/IOLib.h>
Expand All @@ -23,6 +21,8 @@ void VoodooI2CGoodixEventDriver::dispatchDigitizerEvent(int logicalX, int logica
IOFixed x = ((logicalX * 1.0f) / multitouch_interface->logical_max_x) * 65535;
IOFixed y = ((logicalY * 1.0f) / multitouch_interface->logical_max_y) * 65535;

checkRotation(&x, &y);

// Dispatch the actual event
dispatchDigitizerEventWithTiltOrientation(timestamp, 0, kDigitiserTransducerFinger, 0x1, clickType, x, y);

Expand Down Expand Up @@ -66,6 +66,15 @@ void VoodooI2CGoodixEventDriver::fingerLift() {
}

void VoodooI2CGoodixEventDriver::reportTouches(struct Touch touches[], int numTouches) {
if (!active_framebuffer) {
active_framebuffer = getFramebuffer();
}

if (active_framebuffer) {
OSNumber* number = OSDynamicCast(OSNumber, active_framebuffer->getProperty(kIOFBTransformKey));
current_rotation = number->unsigned8BitValue() / 0x10;
}

if (numTouches == 1) {
Touch touch = touches[0];

Expand Down Expand Up @@ -111,6 +120,9 @@ void VoodooI2CGoodixEventDriver::reportTouches(struct Touch touches[], int numTo
scheduleLift();
}
else {
// Set rotation for gestures
multitouch_interface->setProperty(kIOFBTransformKey, current_rotation, 8);

// Our finger event is multitouch, so we're not clicking
click_tick = 0;

Expand Down Expand Up @@ -168,6 +180,8 @@ bool VoodooI2CGoodixEventDriver::handleStart(IOService* provider) {

timer_source = IOTimerEventSource::timerEventSource(this, OSMemberFunctionCast(IOTimerEventSource::Action, this, &VoodooI2CGoodixEventDriver::fingerLift));

active_framebuffer = getFramebuffer();

if (!timer_source || work_loop->addEventSource(timer_source) != kIOReturnSuccess) {
IOLog("%s::Could not add timer source to work loop\n", getName());
return false;
Expand Down Expand Up @@ -196,6 +210,8 @@ void VoodooI2CGoodixEventDriver::handleStop(IOService* provider) {

OSSafeReleaseNULL(work_loop);

// OSSafeReleaseNULL(active_framebuffer); // Todo: do we need to do this?

super::handleStop(provider);
}

Expand Down Expand Up @@ -292,3 +308,47 @@ bool VoodooI2CGoodixEventDriver::start(IOService* provider) {

return true;
}

IOFramebuffer* VoodooI2CGoodixEventDriver::getFramebuffer() {
IODisplay* display = NULL;
IOFramebuffer* framebuffer = NULL;

OSDictionary *match = serviceMatching("IODisplay");
OSIterator *iterator = getMatchingServices(match);

if (iterator) {
display = OSDynamicCast(IODisplay, iterator->getNextObject());

if (display) {
IOLog("%s::Got active display\n", getName());

framebuffer = OSDynamicCast(IOFramebuffer, display->getParentEntry(gIOServicePlane)->getParentEntry(gIOServicePlane));

if (framebuffer) {
IOLog("%s::Got active framebuffer\n", getName());
}
}

OSSafeReleaseNULL(iterator);
}

OSSafeReleaseNULL(match);

return framebuffer;
}

void VoodooI2CGoodixEventDriver::checkRotation(IOFixed* x, IOFixed* y) {
if (active_framebuffer) {
if (current_rotation & kIOFBSwapAxes) {
IOFixed old_x = *x;
*x = *y;
*y = old_x;
}
if (current_rotation & kIOFBInvertX) {
*x = 65535 - *x;
}
if (current_rotation & kIOFBInvertY) {
*y = 65535 - *y;
}
}
}
21 changes: 20 additions & 1 deletion VoodooI2CGoodix/VoodooI2CGoodixEventDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include <IOKit/IOLib.h>
#include <IOKit/IOService.h>
#include <IOKit/IOWorkLoop.h>
#include <IOKit/IOTimerEventSource.h>

#include <IOKit/hidevent/IOHIDEventService.h>
#include <IOKit/hidsystem/IOHIDTypes.h>

#include <IOKit/graphics/IOFramebuffer.h>
#include <IOKit/graphics/IODisplay.h>

#include "../../../Multitouch Support/VoodooI2CDigitiserStylus.hpp"
#include "../../../Multitouch Support/VoodooI2CMultitouchInterface.hpp"
#include "../../../Multitouch Support/MultitouchHelpers.hpp"
Expand Down Expand Up @@ -144,9 +148,24 @@ class EXPORT VoodooI2CGoodixEventDriver : public IOHIDEventService {
*/
void scheduleLift();

private:
/* Get the active framebuffer
*/
IOFramebuffer* getFramebuffer();

/* Rotate coordinates to match current framebuffer's rotation
*
* @x A pointer to the X coordinate
* @y A pointer to the Y coordinate
*/

void checkRotation(IOFixed* x, IOFixed* y);

private:
IOWorkLoop *work_loop;
IOTimerEventSource *timer_source;
IOFramebuffer* active_framebuffer = NULL;

UInt8 current_rotation;

IOFixed last_x = 0;
IOFixed last_y = 0;
Expand Down
44 changes: 9 additions & 35 deletions VoodooI2CGoodix/VoodooI2CGoodixTouchDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ struct goodix_ts_data {
bool swapped_x_y;
bool inverted_x;
bool inverted_y;
bool swap_x_y_scale;
bool swapped_x_y_values;
unsigned int max_touch_num;
unsigned int int_trigger_type;
UInt16 id;
Expand Down Expand Up @@ -185,7 +183,6 @@ bool VoodooI2CGoodixTouchDriver::start(IOService* provider) {
IOLog("%s::VoodooI2CGoodixTouchDriver has started\n", getName());

// Instantiate the event driver
// Todo: how to properly attach to this service?
event_driver = OSTypeAlloc(VoodooI2CGoodixEventDriver);
if (!event_driver
|| !event_driver->init()
Expand Down Expand Up @@ -340,36 +337,17 @@ void VoodooI2CGoodixTouchDriver::goodix_ts_report_touch(UInt8 *coor_data, Touch

// IOLog("%s::raw: %d,%d\n", getName(), input_x, input_y);

// Scale swapping has to happen before everything
if (ts->swap_x_y_scale) {
input_x = (int)(((float)input_x / ts->abs_x_max) * ts->abs_y_max);
input_y = (int)(((float)input_y / ts->abs_y_max) * ts->abs_x_max);
// Inversions have to happen before axis swapping
if (ts->inverted_x)
input_x = ts->abs_x_max - input_x;
if (ts->inverted_y)
input_y = ts->abs_y_max - input_y;

// IOLog("%s::scl: %d,%d\n", getName(), input_x, input_y);

// Inversions have to happen before axis swapping
if (ts->inverted_x)
input_x = ts->abs_y_max - input_x;
if (ts->inverted_y)
input_y = ts->abs_x_max - input_y;

if (ts->inverted_x || ts->inverted_y) {
// IOLog("%s::inv: %d,%d\n", getName(), input_x, input_y);
}
}
else {
// Inversions have to happen before axis swapping
if (ts->inverted_x)
input_x = ts->abs_x_max - input_x;
if (ts->inverted_y)
input_y = ts->abs_y_max - input_y;

if (ts->inverted_x || ts->inverted_y) {
// IOLog("%s::inv: %d,%d\n", getName(), input_x, input_y);
}
if (ts->inverted_x || ts->inverted_y) {
// IOLog("%s::inv: %d,%d\n", getName(), input_x, input_y);
}

if (ts->swapped_x_y || ts->swapped_x_y_values) {
if (ts->swapped_x_y) {
swap(input_x, input_y);
// IOLog("%s::swp: %d,%d\n", getName(), input_x, input_y);
}
Expand Down Expand Up @@ -536,19 +514,15 @@ void VoodooI2CGoodixTouchDriver::goodix_read_config() {

IOLog("%s::ts->abs_x_max = %d\n", getName(), ts->abs_x_max);
IOLog("%s::ts->abs_y_max = %d\n", getName(), ts->abs_y_max);
IOLog("%s::ts->int_trigger_type = %d\n", getName(), ts->int_trigger_type);
IOLog("%s::ts->max_touch_num = %d\n", getName(), ts->max_touch_num);
}

IOReturn VoodooI2CGoodixTouchDriver::goodix_configure_dev() {
IOReturn retVal = kIOReturnSuccess;

// Hardcoded values for Chuwi Minibook 8
ts->swapped_x_y = false;
ts->inverted_x = true;
ts->inverted_x = false;
ts->inverted_y = false;
ts->swap_x_y_scale = true;
ts->swapped_x_y_values = true;

goodix_read_config();

Expand Down

0 comments on commit 50b24ba

Please sign in to comment.