1 # Copyright (C) 2008, Parrot Foundation.
6 xlibtest.pir - Test NCI with libX11
10 This is an initial version, be careful and not expect too much.
12 Compile Xlib.pir to Xlib.pbc before usage:
14 ../../parrot -o Xlib.pbc Xlib.pir [ filename ]
16 Press Escape key to exit.
18 Press S to save to filename
22 .include 'Xlibconstants.pir'
24 # Key for JSON line data
25 .const string JKEY_LINES = 'lines'
30 .local string filename
34 load_bytecode 'Xlib.pbc'
36 # Initialize and print some information
40 .local pmc openDisplay
41 openDisplay = get_global ['Xlib'], 'OpenDisplay'
44 display = openDisplay('')
48 print 'DefaultScreen: '
49 $I0 = display.'DefaultScreen'()
54 black = display.'BlackPixel'()
59 white = display.'WhitePixel'(0)
62 # Create and map the window
65 rw = display.'RootWindow'()
68 w = display.'CreateSimpleWindow'(rw, 0, 0, 600, 400, 0, 0, white)
70 bor $I0, ButtonPressMask
71 bor $I0, ButtonReleaseMask
72 bor $I0, PointerMotionMask
73 bor $I0, StructureNotifyMask
76 w.'StoreName'('Hello, parrot')
79 # Get keycode of the Escape key to check in key press event
81 .local pmc KeysymToString
82 KeysymToString = get_global ['Xlib'], 'KeysymToString'
83 .local pmc StringToKeysym
84 StringToKeysym = get_global ['Xlib'], 'StringToKeysym'
87 $I0 = StringToKeysym('Escape')
91 codeEscape = display.'KeysymToKeycode'($I0)
94 $I0 = StringToKeysym('S')
98 codeS = display.'KeysymToKeycode'($I0)
102 listline = load(filename)
111 newEvent = get_global ['Xlib'], 'newEvent'
125 $I0 = display.'NextEvent'(event)
129 eq $I1, KeyPress, keypress
130 eq $I1, KeyRelease, loop
131 eq $I1, ButtonPress, press
132 eq $I1, ButtonRelease, release
133 eq $I1, MotionNotify, paint
134 eq $I1, Expose, expose
135 eq $I1, ClientMessage, message
136 eq $I1, DestroyNotify, finish
138 # Unhandled event type
143 $I0 = event.'keycode'()
147 # $I1 = display.KeycodeToKeysym($I0)
149 # $S0 = KeysymToString($I1)
151 # eq $S0, 'Escape', finish
153 if $I0 == codeEscape goto handleescape
154 if $I0 == codeS goto handleS
162 save(filename, listline)
169 w.'DrawPoint'(lastpx, lastpy)
170 line = new [ 'ResizableIntegerArray' ]
181 unless pressed goto loop
184 # $I0 = event.serial()
197 eq lastpx, px, checky
202 w.'DrawLine'(lastpx, lastpy, px, py)
213 $I0 = elements listline
227 unless $I2 goto nextline
234 w.'DrawPoint'(lastpx, lastpy)
236 eq $I3, $I2, nextline
241 w.'DrawLine'(lastpx, lastpy, px, py)
253 # End. Close window and display, and exit.
261 #-----------------------------------------------------------------------
262 .sub show_display_name
263 print 'DisplayName: "'
264 .local pmc DisplayName
265 DisplayName = get_global ['Xlib'], 'DisplayName'
267 dname = DisplayName()
272 #-----------------------------------------------------------------------
274 .param string filename
276 if null filename goto newfile
280 handle = open filename, 'r'
284 .local string jsonfile
285 jsonfile = handle.'readall'()
288 load_bytecode 'compilers/json/JSON.pbc'
290 json = compreg 'JSON'
291 .local pmc jsonobject
292 jsonobject = json(jsonfile)
293 listline = jsonobject [JKEY_LINES]
298 .get_results(exception)
300 print "\nERROR LOADING FILE: "
305 listline = new [ 'ResizablePMCArray' ]
310 #-----------------------------------------------------------------------
312 .param string filename
316 load_bytecode 'JSON.pbc'
319 jsondata = new [ 'Hash' ]
320 jsondata [JKEY_LINES] = listline
321 .local string jsonfile
322 jsonfile = _json(jsondata)
324 handle = open filename, 'w'
325 print handle, jsonfile
331 .get_results(exception)
333 print "\nERROR SAVING FILE: "
338 #-----------------------------------------------------------------------
343 # vim: expandtab shiftwidth=4 ft=pir: