File: xlibtest.rb

package info (click to toggle)
parrot 6.6.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 25,164 kB
  • ctags: 16,050
  • sloc: ansic: 110,715; perl: 94,382; yacc: 1,911; lex: 1,529; lisp: 1,163; cpp: 782; python: 646; ruby: 335; sh: 140; makefile: 129; cs: 49; asm: 30
file content (111 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# xlibtest.rb - A test of Xlib.pir usage from cardinal
# 
# To run this file, execute the following command from the
# current directory:
# 
# ../../parrot ../../languages/cardinal/cardinal.pbc  xlibtest.rb
# 
# (You must have the cardinal pbc already builded).
# 
# Press Esc key to exit the program.
# 
# Parsing is very slow.  Give it a minute to start up.
require 'Xlib'

puts 'Hello'

puts 'Display: ' + Xlib::DisplayName()

display = Xlib::OpenDisplay('')

puts 'Default screen: ' + display.DefaultScreen()

display.hello()

white  = display.WhitePixel(0)
root   = display.RootWindow()
window = display.CreateSimpleWindow(root, 0, 0, 600, 400, 0, 0, white)
window.StoreName("Hello, ruby")
window.SelectInput(163919)
window.Map()

# Get Escape keycode
keysym = Xlib::StringToKeysym('Escape')
code_escape = display.KeysymToKeycode(keysym)

event = Xlib::newEvent()

type = 0

lastx = 0
lasty = 0
pressed = 0
listline = Array.new()
line = Array.new()

while type != 17
    display.NextEvent(event)
    type = event.type()
    if type == 4
        x = event.x()
        y = event.y()
        window.DrawPoint(x, y)
        lastx = x
        lasty = y
        pressed = 1
    end
    if type == 5
        newline = line
        listline.push(newline)
        line = Array.new()
        pressed = 0
    end
    if type == 6 && pressed
        x = event.x()
        y = event.y()
        if x != lastx || y != lasty
            window.DrawLine(lastx, lasty, x, y)
            lastx = x
            lasty = y
            line.push(x, y)
        end
    end
    if type == 12
        #puts 'Exposed. Lines: ', +@listline
        listline.each do |l|
            #puts 'Points ', l.elems
            if l.elems  > 0
                lx = l[0]
                ly = l[1]
                #puts lx, ' ', ly
                window.DrawPoint(lx, ly)

                i = 2
                while i < l.elems
                    x = l[i]
                    y = l[i+1]
                    window.DrawLine(lx, ly, x, y)
                    lx = x
                    ly = y
                    #puts lx, ' ', ly
                    i += 2
                end
            end
        end
    end
    if type == 2
        code = event.keycode()
        if code == code_escape
            window.Unmap()
            window.Destroy()
        end
    end
    if type == 33
        window.Unmap()
        window.Destroy()
    end
end

display.Close()

puts 'Bye'