c - Vala to Python and back again -
i stuck trying grok path vala/c python , down again. google-fu leading me in circles. want use vala write api , employ python (or perhaps gnome javascript).
using clutter example (it gtk+3 widget too) here question: how —
go there
write custom actor when clicked will:
- change colour - nb: done in vala handler. i.e vala object connected 'button-release' event. handler calls vala method: this.set_col('blue');
- have continue event python, along data — want print "i turned blue!" - need "blue" string.
in python i'd create stage , (somehow - via gi magic) create new actor. python stuff set , connect same 'button-release' event (i guess..)
a) vala handler run , python one? (in order, or @ all.)
b) have special in vala handler — return true, or perhaps emit new signal python receive?
and again
let's actor called v. how i: v.set_col('red') (in python) , have run vala set_col method, passing python string? (i suspect automagic under gi, don't know sure.)
in short
vala actor -- event --> handler (in vala) --> handler (in python) data vala method <--- method call args python
i'd appreciate links , such too, thanks.
i got it. code below works so:
- make 3 files in dir.
- "build" bash script.
- "redsquare.vala" vala become library (.so file)
- "test.py" python3 code use library.
- install these: ( apt-get bias, sorry )
- apt-get install libgirepository1.0-dev
- apt-get install gobject-introspection
- valac , company
run ./build , should grind voodoo , run test.py file.
you should see clutter window. click red square , watch console-barf.
:-d
hth.
build
#!/bin/bash # # script borrowed tal liron at: # https://github.com/tliron/pygobject-example # # did these script work: # # apt-get install libgirepository1.0-dev # apt-get install gobject-introspection # echo "cleaning..." rm -rf tmp rm -rf lib rm -rf type rm -f test mkdir tmp mkdir lib mkdir type echo "building vala library..." # note 1: ubuntu package valac: valac-0.14 # note 2: generates broken gir if --gir= has directory prefixed # note 3: -x switches gcc necessary! # note 4: generated gir include gobject-2.0. gir # included in ubuntu package: libgirepository1.0-dev valac \ --pkg clutter-1.0 \ --library=palelib \ --directory=tmp \ --gir=palelib-1.0.gir \ --output=libpalelib.so \ -x -shared \ -x -fpic \ redsquare.vala mv tmp/libpalelib.so lib mv tmp/palelib-1.0.gir type # note: cannot generate c code , compile in same call # (we don't need c code run test, curious # vala generating. resulting code in # logging.c) #valac \ #--ccode \ #redsquare.vala echo "building typelib..." # note 1: ubuntu package g-ir-compiler: gobject-introspection # note 2: --shared-library switch necessary when using # gir produced valac, because not include # 'shared-library' attribute in <namespace> tag. g-ir-compiler \ --shared-library=libpalelib.so \ --output=type/palelib-1.0.typelib \ type/palelib-1.0.gir echo "test python..." # note 1: ubuntu's default path typelib files seems be: # /usr/lib/girepository-1.0/. # note 2: possible programmatically change # gi_typelib_path environment var in python (os.environ api). # if so, make sure set before importing # gi.repository. ld_library_path=lib \ gi_typelib_path=type \ ./test.py
redsquare.vala
namespace palelib { public class redsquare : clutter.actor { //private vars private clutter.canvas _canvas; private int[] _col = { 255, 0, 0 }; //constructor - needs called explicitly python .new() public redsquare() { stdout.printf( "redsquare constructor.\n" ); _canvas = new clutter.canvas(); _canvas.set_size(300,300); this.set_size(300,300); this.set_content( _canvas ); //connect draw signal. _canvas.draw.connect(drawme); //make reactive , connect button-press-event this.set_reactive(true); this.button_press_event.connect( cleek ); } //button press signal handler private bool cleek ( clutter.buttonevent evt ) { stdout.printf("vala cleek() has run!\n"); this._col = {0,255,0}; //just change colour this.redraw("from vala"); //return true; //stops signal here. python won't it. return false; //lets signal carry on going (to python). } //draws cairo art canvas private bool drawme( cairo.context ctx, int w, int h) { stdout.printf("drawme test.\n"); ctx.set_source_rgb(this._col[0],this._col[1],this._col[2]); ctx.rectangle(0,0,300,300); ctx.fill(); return true; } //redraw - forces invalidate trips draw event //am gonna call directly python too! public void redraw(string? thing) { thing = thing ?? "from null"; //tests null or else stdout.printf( "redraw test %s.\n", thing ); this._canvas.invalidate(); } } //end redsquare class } //end namespace
test.py
#!/usr/bin/env python3 """ tests instance of our vala actor. expect see red square on white stage. (it can clicked.) """ import sys gi.repository import palelib, clutter clutter.init(sys.argv) stage = clutter.stage() stage.set_size(800, 400) stage.set_title("blah blah") stage.connect('destroy', lambda x: clutter.main_quit() ) # make our object: rs = palelib.redsquare.new() #note .new() call. yuck. print(rs) #print(dir(rs)) # see actor object. rs.set_position(100,100) stage.add_child(rs) #force rs appear. calls vala method , passes string. rs.redraw("from python") """ # crud testing: r1 = clutter.rectangle() r1.set_size(50,50) r1.set_position(0,0) damnweird = clutter.color.new(0,0,0,255) r1.set_color( damnweird ) stage.add_child(r1) """ """ let's event going python! because redsquare actor *already* listening button-press-event (in vala) second such event obey. *think* happens after vala cleek() method runs. if |return true| in cleek(), not run, implies python happening second in chain. """ def gogo( a, evt ): print ("hello gogo. %s %s" % (a,evt)) rs.connect("button_press_event", gogo) stage.show_all() clutter.main()
Comments
Post a Comment