better support for interfaces

This commit is contained in:
Stefan Salewski 2017-09-27 20:53:08 +02:00
commit 3d99d2cc50
8 changed files with 98 additions and 45 deletions

View file

@ -4,7 +4,7 @@ import gintro/[gtk, glib, gobject]
import gintro/gio except Application, newApplication # we want to use GTK application import gintro/gio except Application, newApplication # we want to use GTK application
#from gintro/gio import ApplicationFlags, scActivate, run #from gintro/gio import ApplicationFlags, scActivate, run
proc activate(app: Application) = proc appActivate(app: Application) =
let window = newApplicationWindow(app) let window = newApplicationWindow(app)
window.title = "GTK3 & Nim" window.title = "GTK3 & Nim"
window.defaultSize = (200, 200) window.defaultSize = (200, 200)
@ -12,7 +12,7 @@ proc activate(app: Application) =
proc main = proc main =
let app = newApplication("org.gtk.example") let app = newApplication("org.gtk.example")
connect(app, "activate", activate) connect(app, "activate", appActivate)
discard run(app) discard run(app)
main() main()

View file

@ -5,7 +5,7 @@ import gintro/gio except Application, newApplication
proc buttonClicked (button: Button) = proc buttonClicked (button: Button) =
button.label = utf8Strreverse(button.label, -1) button.label = utf8Strreverse(button.label, -1)
proc activate (app: Application) = proc appActivate (app: Application) =
let window = newApplicationWindow(app) let window = newApplicationWindow(app)
window.title = "GNOME Button" window.title = "GNOME Button"
window.defaultSize = (250, 50) window.defaultSize = (250, 50)
@ -16,7 +16,7 @@ proc activate (app: Application) =
proc main = proc main =
let app = newApplication("org.gtk.example") let app = newApplication("org.gtk.example")
connect(app, "activate", activate) connect(app, "activate", appActivate)
discard app.run discard app.run
main() main()

View file

@ -25,7 +25,7 @@ proc drawingAreaDrawCb(widget: DrawingArea; context: Context): bool =
inc(redrawNumber) inc(redrawNumber)
return true # TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further. return true # TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
proc activate(app: Application) = proc appActivate(app: Application) =
let window = newApplicationWindow(app) let window = newApplicationWindow(app)
window.title = "Drawing example" window.title = "Drawing example"
window.defaultSize = (400, 400) window.defaultSize = (400, 400)
@ -37,7 +37,7 @@ proc activate(app: Application) =
proc main = proc main =
let app = newApplication("org.gtk.example") let app = newApplication("org.gtk.example")
connect(app, "activate", activate) connect(app, "activate", appActivate)
discard run(app) discard run(app)
main() main()

View file

@ -21,7 +21,7 @@ proc b4Callback(button: Button; w: ApplicationWindow) =
else: else:
w.title = "Nim with GTK3" w.title = "Nim with GTK3"
proc activate (app: Application) = proc appActivate (app: Application) =
var o: O var o: O
var r: ref O var r: ref O
new r new r
@ -47,7 +47,7 @@ proc activate (app: Application) =
proc main = proc main =
let app = newApplication("org.gtk.example") let app = newApplication("org.gtk.example")
connect(app, "activate", activate) connect(app, "activate", appActivate)
discard app.run discard app.run
main() main()

View file

@ -11,7 +11,7 @@ proc buttonClicked (button: CountButton; decrement: int) =
button.label = "Counter: " & $button.counter button.label = "Counter: " & $button.counter
echo "Counter is now: ", button.counter echo "Counter is now: ", button.counter
proc activate (app: Application) = proc appActivate (app: Application) =
var button: CountButton var button: CountButton
let window = newApplicationWindow(app) let window = newApplicationWindow(app)
window.title = "Count Button" window.title = "Count Button"
@ -23,7 +23,7 @@ proc activate (app: Application) =
proc main = proc main =
let app = newApplication("org.gtk.example") let app = newApplication("org.gtk.example")
connect(app, "activate", activate) connect(app, "activate", appActivate)
discard app.run discard app.run
main() main()

View file

@ -2,7 +2,7 @@
import gintro/[gtk, glib, gobject] import gintro/[gtk, glib, gobject]
import gintro/gio except Application, newApplication import gintro/gio except Application, newApplication
proc activate(app: Application) = proc appActivate(app: Application) =
let window = newApplicationWindow(app) let window = newApplicationWindow(app)
let label = newLabel("Yellow text on green background") let label = newLabel("Yellow text on green background")
let cssProvider = newCssProvider() let cssProvider = newCssProvider()
@ -17,7 +17,7 @@ proc activate(app: Application) =
proc main = proc main =
let app = newApplication("org.gtk.example") let app = newApplication("org.gtk.example")
connect(app, "activate", activate) connect(app, "activate", appActivate)
discard run(app) discard run(app)
main() main()

View file

@ -14,9 +14,18 @@ proc ct5nt(s: string): string =
proc findSignal(name, obj: NimNode): string = proc findSignal(name, obj: NimNode): string =
let str = ($name).replace("-", "_") & RecSep let str = ($name).replace("-", "_") & RecSep
var ipros: seq[string]
for i in SCA: for i in SCA:
if i.startsWith(str): if i.startsWith(str):
let t = i.split(RecSep, 2)[1] let h1 = i.split(RecSep)[3]
let h2 = h1.split(": ")[1]
let h = h2.split({';', ')'})[0]
if h.contains("|"):
ipros = h.split(" | ")
else:
ipros = @[h]
assert(ipros[0] == i.split(RecSep, 2)[1])
for t in ipros:
var n = obj var n = obj
while true: while true:
if n.kind != nnkBracketExpr: break if n.kind != nnkBracketExpr: break
@ -73,7 +82,7 @@ proc $1$2 {.cdecl.} =
if hargs[i].contains("|"): if hargs[i].contains("|"):
ipos = i ipos = i
break break
assert(ipos >= 0) # indeed assert(ipos > 0) as first parameter should be plain widget/gobject assert(ipos >= 0)
let ipronode = p.symbol.getImpl().params[ipos + 1][1] # the actual data type for interface providers let ipronode = p.symbol.getImpl().params[ipos + 1][1] # the actual data type for interface providers
if ipronode.isNil: if ipronode.isNil:
quit("Error: Signal-Handler has too few parameters: " & $(p.symbol.getImpl().name.toStrLit) & $(p.symbol.getImpl().params.toStrLit)) quit("Error: Signal-Handler has too few parameters: " & $(p.symbol.getImpl().name.toStrLit) & $(p.symbol.getImpl().params.toStrLit))
@ -92,6 +101,7 @@ proc $1$2 {.cdecl.} =
if ahl.contains("): "): if ahl.contains("): "):
resl = all.rsplit("): ", 1)[1] resl = all.rsplit("): ", 1)[1]
var resu = " $3(cast[$4](h)" var resu = " $3(cast[$4](h)"
if all.find(";") > 0: # more than one argument if all.find(";") > 0: # more than one argument
var largs = all.split("; ") var largs = all.split("; ")
largs.delete(0) largs.delete(0)
@ -109,7 +119,10 @@ proc $1$2 {.cdecl.} =
if h.len > 0: if h.len > 0:
a1 = h & "(" & a1 & ")" a1 = h & "(" & a1 & ")"
names[i] = a1 names[i] = a1
if ipro != nil: types[ipos - 1] = ipro
if ipos > 0:
types[ipos - 1] = ipro
for i in 0 .. largs.high: for i in 0 .. largs.high:
if types[i] == nil: if types[i] == nil:
resu.add(", " & names[i]) resu.add(", " & names[i])
@ -118,6 +131,7 @@ proc $1$2 {.cdecl.} =
r1s.add(" var " & names[i] & "1: " & types[i] & "\n") r1s.add(" var " & names[i] & "1: " & types[i] & "\n")
r1s.add(" new " & names[i] & "1" & "\n") r1s.add(" new " & names[i] & "1" & "\n")
r1s.add(" " & names[i] & "1.impl = " & names[i] & "\n") r1s.add(" " & names[i] & "1.impl = " & names[i] & "\n")
if not ignoreArg.boolVal: if not ignoreArg.boolVal:
resu.add(", cast[$5](data)") resu.add(", cast[$5](data)")
resu.add(")") resu.add(")")
@ -134,6 +148,7 @@ proc $1$2 {.cdecl.} =
ahl = "(self: " & wts & ";" & ahl.split(";", 1)[1] ahl = "(self: " & wts & ";" & ahl.split(";", 1)[1]
else: else:
ahl = "(self: " & wts & ")" & ahl.split(")", 1)[1] ahl = "(self: " & wts & ")" & ahl.split(")", 1)[1]
let r2s = let r2s =
if ignoreArg.boolVal: if ignoreArg.boolVal:
""" """

View file

@ -1,5 +1,5 @@
# High level gobject-introspection based GTK3 bindings for the Nim programming language # High level gobject-introspection based GTK3 bindings for the Nim programming language
# v 0.2 2017-SEP-19 # v 0.2 2017-SEP-27
# (c) S. Salewski 2017 # (c) S. Salewski 2017
# https://wiki.gnome.org/Projects/GObjectIntrospection # https://wiki.gnome.org/Projects/GObjectIntrospection
@ -240,6 +240,7 @@ proc mangleType(s: cstring): string =
var output: StringStream var output: StringStream
var supmod: StringStream var supmod: StringStream
var methodBuffer: StringStream var methodBuffer: StringStream
var signalBuffer: StringStream
proc genP(info: GICallableInfo; genProxy = false; binfo: GIBaseInfo = nil): (string, string, TableRef[string, string]) proc genP(info: GICallableInfo; genProxy = false; binfo: GIBaseInfo = nil): (string, string, TableRef[string, string])
@ -355,6 +356,12 @@ proc genP(info: GICallableInfo; genProxy = false; binfo: GIBaseInfo = nil): (str
arglist = "(cast[ptr " & manglename(gBaseInfoGetName(binfo)) & "00](" & "self.impl)" arglist = "(cast[ptr " & manglename(gBaseInfoGetName(binfo)) & "00](" & "self.impl)"
if genProxy: if genProxy:
var h = manglename(gBaseInfoGetName(binfo)) var h = manglename(gBaseInfoGetName(binfo))
if interfaceProvider.contains(h):
let provider = interfaceProvider[h]
for i in provider: discard mangleType(i)
h = h & " | " & provider.join(" | ")
if (sym.startsWith("gdk_events_get_") or sym.startsWith("gdk_event_get_")) and h == "Event": if (sym.startsWith("gdk_events_get_") or sym.startsWith("gdk_event_get_")) and h == "Event":
h = "SomeEvent" h = "SomeEvent"
resul = "(self: " & h resul = "(self: " & h
@ -481,6 +488,7 @@ proc writeMethod(info: GIBaseInfo; minfo: GIFunctionInfo; genProxy = false) =
var replist: TableRef[string, string] var replist: TableRef[string, string]
let p = methodBuffer.getPosition let p = methodBuffer.getPosition
let sym = $gFunctionInfoGetSymbol(mInfo) let sym = $gFunctionInfoGetSymbol(mInfo)
if sym[^1] == '_': return if sym[^1] == '_': return
if sym.contains("__"): return if sym.contains("__"): return
if processedFunctions.contains(sym): return if processedFunctions.contains(sym): return
@ -494,6 +502,7 @@ proc writeMethod(info: GIBaseInfo; minfo: GIFunctionInfo; genProxy = false) =
if sym == "g_iconv": if sym == "g_iconv":
return return
try: try:
#if sym == "g_value_get_string": echo "g_value_get_string"; quit()
(plist, arglist, replist) = genP(mInfo, genProxy, info) (plist, arglist, replist) = genP(mInfo, genProxy, info)
methodBuffer.write("\nproc " & sym & EM & plist) methodBuffer.write("\nproc " & sym & EM & plist)
methodBuffer.writeLine(" {.\n importc: \"", sym, "\", ", libprag, ".}") methodBuffer.writeLine(" {.\n importc: \"", sym, "\", ", libprag, ".}")
@ -506,6 +515,7 @@ proc writeMethod(info: GIBaseInfo; minfo: GIFunctionInfo; genProxy = false) =
asym.add($manglename(gBaseInfoGetName(info))) asym.add($manglename(gBaseInfoGetName(info)))
if keywords.split.contains(asym) or nims.split.contains(asym): asym.add('P') if keywords.split.contains(asym) or nims.split.contains(asym): asym.add('P')
var ret2 = gCallableInfoGetReturnType(minfo) var ret2 = gCallableInfoGetReturnType(minfo)
#if sym == "g_value_get_string": echo isString(ret2); quit()
var ret22 = gCallableInfoGetReturnType(minfo) var ret22 = gCallableInfoGetReturnType(minfo)
for run in 0 .. 1: for run in 0 .. 1:
if run == 1: if run == 1:
@ -835,7 +845,7 @@ proc writeInterface(info: GIInterfaceInfo) =
################################ ################################
let numsig = info.gInterfaceInfoGetNSignals let numsig = info.gInterfaceInfoGetNSignals
if numsig > 0: output.writeLine("") if numsig > 0: signalbuffer.writeLine("")
for j in 0 .. <numsig: for j in 0 .. <numsig:
let signalInfo = gInterfaceInfoGetSignal(info, j) let signalInfo = gInterfaceInfoGetSignal(info, j)
#let c = gSignalInfoGetClassClosure(signalInfo) #let c = gSignalInfoGetClassClosure(signalInfo)
@ -872,14 +882,24 @@ proc writeInterface(info: GIInterfaceInfo) =
else: else:
h = h.replace(")", "xdata: pointer)") h = h.replace(")", "xdata: pointer)")
var xxx = mangleName(gBaseInfoGetName(info)) var xxx = mangleName(gBaseInfoGetName(info))
output.write("proc " & mangleName("sc_" & $gBaseInfoGetName(signalInfo)) & EM & "(self: " & xxx & "; ")
#var h = manglename(gBaseInfoGetName(binfo))
let yyy = xxx
if interfaceProvider.contains(xxx):
let provider = interfaceProvider[xxx]
for i in provider: discard mangleType(i)
xxx = xxx & " | " & provider.join(" | ")
signalbuffer.write("proc " & mangleName("sc_" & $gBaseInfoGetName(signalInfo)) & EM & "(self: " & xxx & "; ")
if gCallableInfoGetNArgs(signalInfo) > 0 or gTypeInfoGetTag(zzzu) != GITypeTag.VOID: if gCallableInfoGetNArgs(signalInfo) > 0 or gTypeInfoGetTag(zzzu) != GITypeTag.VOID:
output.writeLine(" p: proc (self: ptr " & xxx & "00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =") signalbuffer.writeLine(" p: proc (self: ptr " & yyy & "00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =")
else: else:
output.writeLine(" p: proc (self: ptr gobject.Object00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =") signalbuffer.writeLine(" p: proc (self: ptr gobject.Object00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =")
output.write(" g_signal_connect_data(self.impl, \"") signalbuffer.write(" g_signal_connect_data(self.impl, \"")
output.write($gBaseInfoGetName(signalInfo)) signalbuffer.write($gBaseInfoGetName(signalInfo))
output.writeLine("\", cast[GCallback](p), xdata, nil, cast[ConnectFlags](0))") signalbuffer.writeLine("\", cast[GCallback](p), xdata, nil, cast[ConnectFlags](0))")
################################ ################################
@ -1099,7 +1119,7 @@ proc writeObj(info: GIObjectInfo) =
output.writeLine(" g_object_remove_toggle_ref(o.impl, toggleNotify, addr(o[]))") output.writeLine(" g_object_remove_toggle_ref(o.impl, toggleNotify, addr(o[]))")
let numsig = info.gObjectInfoGetNSignals let numsig = info.gObjectInfoGetNSignals
if numsig > 0: output.writeLine("") if numsig > 0: signalbuffer.writeLine("")
for j in 0 .. <numsig: for j in 0 .. <numsig:
let signalInfo = gObjectInfoGetSignal(info, j) let signalInfo = gObjectInfoGetSignal(info, j)
let c = gSignalInfoGetClassClosure(signalInfo) let c = gSignalInfoGetClassClosure(signalInfo)
@ -1136,14 +1156,21 @@ proc writeObj(info: GIObjectInfo) =
else: else:
h = h.replace(")", "xdata: pointer)") h = h.replace(")", "xdata: pointer)")
var xxx = mangleName(gBaseInfoGetName(info)) var xxx = mangleName(gBaseInfoGetName(info))
output.write("proc " & mangleName("sc_" & $gBaseInfoGetName(signalInfo)) & EM & "(self: " & xxx & "; ")
let yyy = xxx
if interfaceProvider.contains(xxx):
let provider = interfaceProvider[xxx]
for i in provider: discard mangleType(i)
xxx = xxx & " | " & provider.join(" | ")
signalbuffer.write("proc " & mangleName("sc_" & $gBaseInfoGetName(signalInfo)) & EM & "(self: " & xxx & "; ")
if gCallableInfoGetNArgs(signalInfo) > 0 or gTypeInfoGetTag(zzzu) != GITypeTag.VOID: if gCallableInfoGetNArgs(signalInfo) > 0 or gTypeInfoGetTag(zzzu) != GITypeTag.VOID:
output.writeLine(" p: proc (self: ptr " & xxx & "00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =") signalbuffer.writeLine(" p: proc (self: ptr " & yyy & "00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =")
else: else:
output.writeLine(" p: proc (self: ptr gobject.Object00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =") signalbuffer.writeLine(" p: proc (self: ptr gobject.Object00; " & h & " {.cdecl.}, xdata: pointer = nil): culong =")
output.write(" g_signal_connect_data(self.impl, \"") signalbuffer.write(" g_signal_connect_data(self.impl, \"")
output.write($gBaseInfoGetName(signalInfo)) signalbuffer.write($gBaseInfoGetName(signalInfo))
output.writeLine("\", cast[GCallback](p), xdata, nil, cast[ConnectFlags](0))") signalbuffer.writeLine("\", cast[GCallback](p), xdata, nil, cast[ConnectFlags](0))")
let nMethods = gObjectInfoGetNMethods(info) let nMethods = gObjectInfoGetNMethods(info)
for j in 0 .. <nMethods: for j in 0 .. <nMethods:
let mInfo = gObjectInfoGetMethod(info, j) let mInfo = gObjectInfoGetMethod(info, j)
@ -1495,7 +1522,9 @@ proc main(namespace: string) =
var i: GIBaseInfo var i: GIBaseInfo
var unp = newSeq[GIBaseInfo]() var unp = newSeq[GIBaseInfo]()
reverse(s) reverse(s)
while s.len > 0: var more_runs = 50
while s.len > 0 or more_runs > 0:
if s.len == 0: dec(more_runs)
if namespace == "GObject" and not knownSyms.contains("g_signal_connect_data"): if namespace == "GObject" and not knownSyms.contains("g_signal_connect_data"):
if knownSyms.contains("ClosureNotify") and knownSyms.contains("ConnectFlags"): if knownSyms.contains("ClosureNotify") and knownSyms.contains("ConnectFlags"):
knownSyms.incl("g_signal_connect_data") knownSyms.incl("g_signal_connect_data")
@ -1516,6 +1545,7 @@ proc main(namespace: string) =
s.add(s[k]) s.add(s[k])
s.delete(k) s.delete(k)
if s.len > 0:
i = s.pop i = s.pop
delayedSyms.insert(i) delayedSyms.insert(i)
if classList.len > 0: if classList.len > 0:
@ -1525,9 +1555,12 @@ proc main(namespace: string) =
var pos = output.getPosition var pos = output.getPosition
var supmodpos = supmod.getPosition var supmodpos = supmod.getPosition
try: try:
echo "try process ", mangleName(gBaseInfoGetName(k))
knownSyms.incl(mangleName(gBaseInfoGetName(k))) knownSyms.incl(mangleName(gBaseInfoGetName(k)))
methodBuffer = newStringStream() methodBuffer = newStringStream()
signalBuffer = newStringStream()
processInfo(k) processInfo(k)
output.write(signalBuffer.data)
output.write(methodBuffer.data) output.write(methodBuffer.data)
echo "----------", gBaseInfoGetName(k) echo "----------", gBaseInfoGetName(k)
except UndefEx: except UndefEx:
@ -1541,19 +1574,22 @@ proc main(namespace: string) =
if delayedSyms.len > a: if delayedSyms.len > a:
suppressType = true suppressType = true
methodBuffer = newStringStream() methodBuffer = newStringStream()
signalBuffer = newStringStream()
unp = delayedSyms unp = delayedSyms
for t in unp.combinations(a + 1): for t in unp.combinations(a + 1):
var pos = output.getPosition var pos = output.getPosition
var supmodpos = supmod.getPosition var supmodpos = supmod.getPosition
methodBuffer = newStringStream() methodBuffer = newStringStream()
signalBuffer = newStringStream()
let x = delayedMethods.len let x = delayedMethods.len
try: try:
var h = false var h = false
for k in t: for k in t:
if gBaseInfoGetType(k) != GIInfoType.OBJECT and gBaseInfoGetType(k) != GIInfoType.STRUCT and gBaseInfoGetType(k) != GIInfoType.UNION and not isCallbackInfo(k): if gBaseInfoGetType(k) != GIInfoType.INTERFACE and gBaseInfoGetType(k) != GIInfoType.OBJECT and gBaseInfoGetType(k) != GIInfoType.STRUCT and gBaseInfoGetType(k) != GIInfoType.UNION and not isCallbackInfo(k):
h = true h = true
if h: continue if h: continue
output.writeLine("type") output.writeLine("type")
for k in t: for k in t:
knownSyms.incl(mangleName(gBaseInfoGetName(k))) knownSyms.incl(mangleName(gBaseInfoGetName(k)))
for k in t: for k in t:
@ -1561,7 +1597,9 @@ proc main(namespace: string) =
for k in t: for k in t:
delayedSyms.keepItIf(mangleName(gBaseInfoGetName(k)) != mangleName(gBaseInfoGetName(it))) delayedSyms.keepItIf(mangleName(gBaseInfoGetName(k)) != mangleName(gBaseInfoGetName(it)))
suppressType = false suppressType = false
output.write(signalBuffer.data)
output.write(methodBuffer.data) output.write(methodBuffer.data)
break myb break myb
except UndefEx: except UndefEx:
for k in t: for k in t:
@ -1633,4 +1671,4 @@ o.close()
supmod.close supmod.close
#for i in callerAllocCollector: echo i #for i in callerAllocCollector: echo i
# 1530 lines # 1660 lines