Skip to content

Commit

Permalink
Implemented log window (Fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-poidevin committed May 1, 2017
1 parent 28443c4 commit 2413c57
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 4 deletions.
6 changes: 4 additions & 2 deletions MBPMid2010-GPU-Fix.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ DEFINES += QT_DEPRECATED_WARNINGS


SOURCES += main.cpp\
mainwindow.cpp
mainwindow.cpp \
logger.cpp

HEADERS += mainwindow.h
HEADERS += mainwindow.h \
logger.h

FORMS += mainwindow.ui
42 changes: 42 additions & 0 deletions logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "logger.h"

Logger::Logger(QObject *parent, QString fileName, QPlainTextEdit *editor) : QObject(parent) {

m_editor = editor;
m_showDate = false;
if (!fileName.isEmpty()) {
file = new QFile;
file->setFileName(fileName);
file->open(QIODevice::Append | QIODevice::Text);
}
}

void Logger::write(const QString &value) {

QString text = value + " ";
if (m_showDate)
text = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss ") + text;
QTextStream out(file);
out.setCodec("UTF-8");
if (file != 0) {
out << text;
}
if (m_editor != 0)
{
QTextCursor prev_cursor = m_editor->textCursor();
m_editor->moveCursor (QTextCursor::End);
m_editor->insertPlainText (text);
m_editor->setTextCursor(prev_cursor);
}
}

void Logger::setShowDateTime(bool value) {

m_showDate = value;
}

Logger::~Logger() {

if (file != 0)
file->close();
}
30 changes: 30 additions & 0 deletions logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QPlainTextEdit>
#include <QFile>
#include <QTextStream>
#include <QDateTime>

class Logger : public QObject {

Q_OBJECT
public:

explicit Logger(QObject *parent, QString fileName, QPlainTextEdit *editor = 0);
~Logger();
void setShowDateTime(bool value);
private:

QFile *file;
QPlainTextEdit *m_editor;
bool m_showDate;
signals:

public slots:

void write(const QString &value);
};

#endif // LOGGER_H
50 changes: 49 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ void MainWindow::on_patchButton_clicked()
//if (answer == QMessageBox::Yes)
if(1)
{
logger->write(" ********** Starting MBP GPU Fix **********\n");
patchKernelExtensionFile(&kernelFile);
loadKernelExtension(&kernelFile);
}
else
{
logger->write(" ********** Discarded MBP GPU Fix **********n");
return;
}
}
Expand Down Expand Up @@ -69,16 +71,25 @@ void MainWindow::on_restoreButton_clicked()
bool MainWindow::init()
{
bool isInitOk = true;

MainWindow::setWindowTitle (APP_NAME);

//Initialize logger
if (QFile::exists("log.txt"))
{
QFile::remove("log.txt");
}
QString fileName = "log.txt";
logger = new Logger(this, fileName, this->ui->logWindow);

//Search for compatibility
if(isCompatibleVersion(getMBPModelVersion()))
{
isInitOk = true;
logger->write("➔ Compatibility : OK ✓\n");
}
else
{
logger->write("➔ Compatibility : NOK ✗\n");
QMessageBox::information(this,"Mac not compatible","Sorry, your Mac is not compatible.\nThe application will close");
isInitOk = false;
}
Expand Down Expand Up @@ -113,6 +124,8 @@ QString MainWindow::getMBPModelVersion()
QString MBPModelVersion;
QProcess process;

logger->write(" | Checking compatibility\n");

//Execute commande line
process.start("sysctl -n hw.model");

Expand All @@ -125,6 +138,8 @@ QString MainWindow::getMBPModelVersion()
//Remove carriage return ("\n") from string
MBPModelVersion = MBPModelVersion.simplified();

logger->write("MBPModelVersion : " + MBPModelVersion);

return MBPModelVersion;
}

Expand All @@ -133,6 +148,8 @@ bool MainWindow::isSIPEnabled(void)
QString SIPStatus;
QProcess process;

logger->write(" | Checking SIP Status\n");

//Execute commande line
process.start("csrutil status");

Expand All @@ -145,10 +162,12 @@ bool MainWindow::isSIPEnabled(void)
#ifndef WINDOWS
if(SIPStatus.contains("disable"))
{
logger->write("SIP Disabled\n");
return false;
}
else
{
logger->write("SIP Enabled\n");
return true;
}
#else
Expand Down Expand Up @@ -185,6 +204,8 @@ bool MainWindow::searchKernelExtensionFile(QFile* kernelExtensionFile)
QDir::NoSymLinks | QDir::Files,
QDirIterator::Subdirectories);

logger->write(" | Searching for AppleGraphicsPowerManagement.kext\n");

//Check if the file was found
if(it.hasNext())
{
Expand All @@ -204,12 +225,14 @@ bool MainWindow::searchKernelExtensionFile(QFile* kernelExtensionFile)
if(listOfFiles.length() <= 1 && listOfFiles.length() > 0)
{
//qDebug() << "Moins de 1";
logger->write("AppleGraphicsPowerManagement.kext found\n");
kernelExtensionFile->setFileName(listOfFiles.at(0));
isFileFound = true;
}
else
{
//qDebug () << "No file was found...";
logger->write("AppleGraphicsPowerManagement.kext not found\n");
isFileFound = false;
}

Expand Down Expand Up @@ -290,11 +313,13 @@ void MainWindow::patchKernelExtensionFile(QFile *kernelFile)
#define PATCHED_FILE_PATH "C:/temp/PatchedInfo.plist"
#endif

logger->write("Copying Info.plist file\n");

//Remove file if already exists
if (QFile::exists(PATCHED_FILE_PATH))
{
QFile::remove(PATCHED_FILE_PATH);
logger->write("Previous Info.plist file removed\n");
}

//Copy file in tmp dir for patch
Expand All @@ -303,6 +328,7 @@ void MainWindow::patchKernelExtensionFile(QFile *kernelFile)
QFile tmpFile(PATCHED_FILE_PATH);
if(!tmpFile.open(QIODevice::ReadWrite | QIODevice::Text))
{
logger->write("Could not open Info.plist file\n");
qDebug() << "Could not open tmp File";
return;
}
Expand Down Expand Up @@ -348,6 +374,8 @@ void MainWindow::patchKernelExtensionFile(QFile *kernelFile)
{"" , {0,0,4,200} , FillArray }
};

logger->write("Patching Info.plist\n");

QDomElement currentNode = xmlBOM.firstChildElement("plist");
QDomElement nextNode;

Expand All @@ -358,21 +386,25 @@ void MainWindow::patchKernelExtensionFile(QFile *kernelFile)
case FindChild:
nextNode = findElementChild(currentNode,confTree.at(i).nodeName);
qDebug() << "FindChild - " << nextNode.tagName() << "|" << nextNode.text();
logger->write(" - FindChild - " + nextNode.tagName() + "|" + nextNode.text() + "\n");
break;

case FindSibling:
nextNode = findElementSibling(currentNode,confTree.at(i).nodeName);
qDebug() << "FindSibling - " << nextNode.tagName() << "|" << nextNode.text();
logger->write(" - FindSibling - " + nextNode.tagName() + "|" + nextNode.text() + "\n");
break;

case NextSibling:
nextNode = currentNode.nextSiblingElement(confTree.at(i).nodeName);
qDebug() << "NextSibling - " << nextNode.tagName();
logger->write(" - NextSibling - " + nextNode.tagName() + "\n");
break;

case FirstChild:
nextNode = currentNode.firstChildElement(confTree.at(i).nodeName);
qDebug() << "FirstChild - " << nextNode.tagName();
logger->write(" - FirstChild - " + nextNode.tagName() + "\n");
break;

case FillArray:
Expand All @@ -395,6 +427,8 @@ void MainWindow::patchKernelExtensionFile(QFile *kernelFile)
currentNode = nextNode;
}

logger->write("Info.plist successfully patched\n");

// Write changes to same file
tmpFile.resize(0);
QTextStream stream;
Expand All @@ -411,19 +445,25 @@ int MainWindow::loadKernelExtension(QFile *kernelFile)

//Disable srcutils: https://derflounder.wordpress.com/2015/10/05/configuring-system-integrity-protection-without-booting-to-recovery-hd/

logger->write(" | Loading Kernel Extension\n");

/* Copy real kext into tmp file */
QProcess process;
QString command = "cp";
QStringList arguments;
QDir kextDir(kernelFile->fileName());
kextDir.cdUp();

logger->write("Copying actuel kext into tmp\n");

arguments << "-rf" << kextDir.absolutePath() << "/tmp/AppleGraphicsPowerManagement.kext";
//Execute commande line
process.start(command,arguments);
//Wait forever until finished
process.waitForFinished(-1);

logger->write("Copying patched Info.plist into kext\n");

/*** Copy patched file into kext ***/
command = "cp";
arguments.clear();
Expand All @@ -433,6 +473,8 @@ int MainWindow::loadKernelExtension(QFile *kernelFile)
//Wait forever until finished
process.waitForFinished(-1);

logger->write("Changing permission of kext\n");

/*** Change permission of modified kext File ***/
//TODO find a way to execute process as root
command = "chown";
Expand All @@ -444,6 +486,8 @@ int MainWindow::loadKernelExtension(QFile *kernelFile)
process.waitForFinished(-1);
qDebug() << process.readAllStandardError();

logger->write("Unloading previous kext\n");

/*** Unload previous kext file ***/
//TODO find a way to execute process as root
command = "kextunload";
Expand All @@ -455,6 +499,8 @@ int MainWindow::loadKernelExtension(QFile *kernelFile)
process.waitForFinished(-1);
qDebug() << process.readAllStandardError();

logger->write("Loading modified kext\n");

/*** Finally load kext file ***/
//TODO find a way to execute process as root
command = "kextload";
Expand All @@ -469,6 +515,8 @@ int MainWindow::loadKernelExtension(QFile *kernelFile)
//See here : http://osxdaily.com/2015/06/24/load-unload-kernel-extensions-mac-os-x/
int Status = 0;

if(Status == 0) logger->write("********************* MBP GPU Fixed Successfully *********************\n");

return Status;
}

Expand Down
4 changes: 4 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <QList>
#include <QtXml>
#include <QDesktopServices>
#include <QLibrary>

#include "logger.h"

using namespace std; // Indique quel espace de noms on va utiliser

Expand Down Expand Up @@ -50,6 +53,7 @@ private slots:
Ui::MainWindow *ui;

QFile kernelFile;
Logger *logger;

QString getMBPModelVersion(void);
bool isCompatibleVersion(QString modelVersion);
Expand Down
13 changes: 12 additions & 1 deletion mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<string>MBPMid2010-GPU-Fix</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPlainTextEdit" name="plainTextEdit">
<widget class="QPlainTextEdit" name="logWindow">
<property name="geometry">
<rect>
<x>10</x>
Expand All @@ -23,6 +23,17 @@
<height>111</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
Expand Down

0 comments on commit 2413c57

Please sign in to comment.