Skip to content

Commit

Permalink
fix blocking sync callbacks for qtbox + new 3rdparty/mpv example
Browse files Browse the repository at this point in the history
  • Loading branch information
therecipe committed Sep 1, 2020
1 parent 28d94ab commit 5f0f7e5
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/binding/files/utils-qml.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (

//needed only for interop --->
ReturnPointersAsStrings bool
SupportsSyncCallsIntoRemote bool
SupportsSyncCallsIntoRemote = true

syncCallIntoRemoteChan = make(chan string, 0)
syncCallIntoRemoteReturnChan = make(chan string, 0)
Expand Down
26 changes: 26 additions & 0 deletions internal/examples/3rdparty/mpv/demo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//source: https://github.com/mpv-player/mpv-examples/tree/master/libmpv/qml

package main

import (
"os"

"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/quick"

"github.com/therecipe/qt/internal/examples/3rdparty/mpv"
)

func main() {
gui.NewQGuiApplication(len(os.Args), os.Args)

mpv.InitMpv()

view := quick.NewQQuickView(nil)
view.SetResizeMode(quick.QQuickView__SizeRootObjectToView)
view.SetSource(core.NewQUrl3("qrc:/qml/main.qml", 0)) //qrc:/mpvtest/main.qml should work as well, since init.go is not patching out the embedding
view.Show()

gui.QGuiApplication_Exec()
}
71 changes: 71 additions & 0 deletions internal/examples/3rdparty/mpv/demo/qml/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import QtQuick 2.0
import QtQuick.Controls 1.0

import mpvtest 1.0

Item {
width: 1280
height: 720

MpvObject {
id: renderer
anchors.fill: parent

MouseArea {
anchors.fill: parent
onClicked: renderer.command(["loadfile", "test.mkv"])
}
}

Rectangle {
id: labelFrame
anchors.margins: -50
radius: 5
color: "white"
border.color: "black"
opacity: 0.8
anchors.fill: box
}

Row {
id: box
anchors.bottom: renderer.bottom
anchors.left: renderer.left
anchors.right: renderer.right
anchors.margins: 100

Text {
anchors.margins: 10
wrapMode: Text.WordWrap
text: "QtQuick and mpv are both rendering stuff.\n
Click to load test.mkv"
}

// Don't take these controls too seriously. They're for testing.
Column {
CheckBox {
id: checkbox
anchors.margins: 10
// Heavily filtered means good, right?
text: "Make video look like on a Smart TV"
onClicked: {
if (checkbox.checked) {
renderer.setProperty("sharpen", 5.0)
} else {
renderer.setProperty("sharpen", 0)
}
}
}
Slider {
id: slider
anchors.margins: 10
anchors.left: checkbox.left
anchors.right: checkbox.right
minimumValue: -100
maximumValue: 100
value: 0
onValueChanged: renderer.setProperty("gamma", slider.value | 0)
}
}
}
}
66 changes: 66 additions & 0 deletions internal/examples/3rdparty/mpv/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// +build ignore

package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
)

func main() {
if _, ok := os.LookupEnv("QT_PKG_CONFIG"); !ok {
println("please export QT_PKG_CONFIG")
os.Exit(1)
}

pwd, pwdErr := os.Getwd()
if pwdErr != nil {
println("failed to get PWD", pwdErr.Error())
os.Exit(1)
}

if _, err := os.Stat("mpv-examples"); err == nil {
println("mpv-examples already cloned")
} else {
if out, err := exec.Command("git", "clone", "--depth=1", "https://github.com/mpv-player/mpv-examples.git").CombinedOutput(); err != nil {
println("failed to clone mpv-examples", err.Error())
println(string(out))
os.Exit(1)
}
println("cloned mpv-examples")
}

//patch *.pro to build a static lib
exec.Command("bash", "-c", fmt.Sprintf("echo \"TEMPLATE = lib\" >> %v", filepath.Join(pwd, "mpv-examples", "libmpv", "qml", "mpvtest.pro"))).CombinedOutput()
exec.Command("bash", "-c", fmt.Sprintf("echo \"CONFIG += staticlib\" >> %v", filepath.Join(pwd, "mpv-examples", "libmpv", "qml", "mpvtest.pro"))).CombinedOutput()

//patch main.cpp
exec.Command("bash", "-c", fmt.Sprintf("head -n -17 %[1]v > %[2]v && mv %[2]v %[1]v", filepath.Join(pwd, "mpv-examples", "libmpv", "qml", "main.cpp"), filepath.Join(pwd, "mpv-examples", "libmpv", "qml", "copy_main.cpp"))).CombinedOutput()
exec.Command("bash", "-c", fmt.Sprintf("echo 'extern \"C\" void initMpv() { std::setlocale(LC_NUMERIC, \"C\"); qmlRegisterType<MpvObject>(\"mpvtest\", 1, 0, \"MpvObject\"); }' >> %[1]v", filepath.Join(pwd, "mpv-examples", "libmpv", "qml", "main.cpp"))).CombinedOutput()

//only tested on arch, since the libmpv version is quite old on ubuntu 16.04 and 18.04
for _, target := range []string{runtime.GOOS} {

qCmd := exec.Command("qmake", "mpvtest.pro")
qCmd.Dir = filepath.Join(pwd, "mpv-examples", "libmpv", "qml")
if out, err := qCmd.CombinedOutput(); err != nil {
println("failed to generate makefile for", target, err.Error())
println(string(out))
os.Exit(1)
}
println("generated makefile for", target)

iCmd := exec.Command("make", "-j", strconv.Itoa(runtime.NumCPU()))
iCmd.Dir = filepath.Join(pwd, "mpv-examples", "libmpv", "qml")
if out, err := iCmd.CombinedOutput(); err != nil {
println("failed to make mpv-examples for", target)
println(string(out))
os.Exit(1)
}
println("built mpv-examples for", target)
}
}
19 changes: 19 additions & 0 deletions internal/examples/3rdparty/mpv/mpv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mpv

/*
#cgo pkg-config: mpv
#cgo linux,amd64 LDFLAGS: -L ${SRCDIR}/mpv-examples/libmpv/qml -lmpvtest
void initMpv();
*/
import "C"
import (
"github.com/therecipe/qt/core"
_ "github.com/therecipe/qt/quick"
)

type stub struct{ core.QObject } //TODO: needed for linking at the moment

func InitMpv() {
C.initMpv()
}
2 changes: 1 addition & 1 deletion interop/interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (

//needed only for interop --->
ReturnPointersAsStrings bool
SupportsSyncCallsIntoRemote bool
SupportsSyncCallsIntoRemote = true

syncCallIntoRemoteChan = make(chan string, 0)
syncCallIntoRemoteReturnChan = make(chan string, 0)
Expand Down
2 changes: 1 addition & 1 deletion qml/utils-qml.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (

//needed only for interop --->
ReturnPointersAsStrings bool
SupportsSyncCallsIntoRemote bool
SupportsSyncCallsIntoRemote = true

syncCallIntoRemoteChan = make(chan string, 0)
syncCallIntoRemoteReturnChan = make(chan string, 0)
Expand Down

0 comments on commit 5f0f7e5

Please sign in to comment.