File: gunze-setup

package info (click to toggle)
gpm 1.20.7-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,960 kB
  • sloc: ansic: 8,702; yacc: 1,054; sh: 1,034; makefile: 387; cpp: 241; lisp: 222; awk: 82; sed: 6
file content (315 lines) | stat: -rwxr-xr-x 7,718 bytes parent folder | download | duplicates (11)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/sh
#
# gunze-setup, copied and adapted from microtouch-setup.
# Most of this code is duplicated, this is definitely not good.
#
# This program is execute by tclsh if we are in the console, and by wish
# otherwise (assuming we are under a graphic environment

#the following is a comment for tcl \
if [ $TERM = "linux" ] ; then exec tclsh $0 $* ; fi

#the following is another comment for tcl \
exec wish $0 $*

########### That's Tcl...

# The Gunze touchscreens don't adapt their output to calibration, so
# we must save calibration information somewhere...
set datafile /etc/gpm-calibration

puts "Calibration program for Gunze touch-screens"

# check generic permission
set who [exec whoami]
if [string compare $who root] {
    puts stderr "you must be root to be able to run this program"
}

# check if we can access this file
if [catch {set F [open $datafile a]} err] {
    puts stderr "$argv0: Can't open $datafile: $err"
    exit 1
}
close $F

# Look for /dev/touchscreen or /dev/mouse
if [file exists /dev/mouse] {
    set dev /dev/mouse
}
if [file exists /dev/touchscreen] {
    set dev /dev/touchscreen
}
if ![info exists dev] {
    puts stderr "$argv0: need to access /dev/touchscreen or /dev/mouse"
    exit 1
}

# horrible, for the linux console
proc goto {x y} {
    set x [expr $x]
    set y [expr $y]
    puts -nonewline [format "\x1B\[%03i;%03iH" $y $x]
}

# an abort procedure, and one to read touchscreen data
proc do_abort {msg} {
    if ![string compare $env(TERM) linux] {
	goto 0 $env(LINES)-2
    } else {
	wm withdraw .
    }
    puts stderr $msg
    killall -CONT gpm
    exit 1
}

proc get_position file {
    global dev
    while 1 {
	gets $file string
	if [scan $string %c%d,%d  cmd x y]!=3 {
	    do_abort  "Received wrong data \"$string\" from $dev"
	}
	# If release ('R') it's done
	if $cmd==0x52 {
	    return "$sx $sy"
	}
	# If a touch event ('T'), save it
	if $cmd==0x54 {
	    set sx $x; set sy $y
	} else {
	    do_abort "Received wrong data \"$string\" from $dev"
	}
    }
}



if ![string compare $env(TERM) linux] {

    # Text-mode calibration

    if {![info exists env(COLUMNS)] || ![info exists env(LINES)]} {
	puts stderr "Please set COLUMNS and LINES in your environment"
	puts stderr " for example, you can issue"
	puts stderr "        eval `resize`"
	puts stderr " in your shell before running this program"
	exit 1
    }
    fconfigure stdout -buffering none
    exec clear > /dev/tty


    # To calibrate, draw targets at 1/8 and 7/8 x-y
    set x [expr $env(COLUMNS)/8]
    set y [expr $env(LINES)/8]
    set X [expr $env(COLUMNS) - $x]
    set Y [expr $env(LINES) - $y]

    # half x and y
    set hx [expr $env(COLUMNS)/2]
    set hy [expr $env(LINES)/2]

    goto $hx-10 $hy-1; puts -nonewline "Please touch the lower"
    goto $hx-10 $hy-0; puts -nonewline "left target, then the"
    goto $hx-10 $hy+1; puts -nonewline "upper right target."

    exec killall -STOP gpm
    set F [open $dev r+]; # $dev is /dev/touchscreen or /dev/mouse
    fconfigure $F -buffering none -translation auto

    # Draw the target
    goto $x $Y-1; puts -nonewline "|"
    goto $x-1 $Y; puts -nonewline "-+-"
    goto $x $Y+1; puts -nonewline "|"

    # Read coordinates
    set first [get_position $F]

    #remove the target, draw the other
    goto $x $Y-1; puts -nonewline " "
    goto $x-1 $Y; puts -nonewline "   "
    goto $x $Y+1; puts -nonewline " "

    goto $X $y-1; puts -nonewline "|"
    goto $X-1 $y; puts -nonewline "-+-"
    goto $X $y+1; puts -nonewline "|"

    # Read coordinates
    set second [get_position $F]

    # remove the target
    goto $X $y-1; puts -nonewline " "
    goto $X-1 $y; puts -nonewline "   "
    goto $X $y+1; puts -nonewline " "
    close $F

    exec killall -CONT gpm
    goto 0 $env(LINES)-2

    # Now save the coordinates
    set F [open $datafile w]
    puts $F "# Calibration coordinates for Gunze device"
    puts $F "$first $second"
    close $F

    puts "Done"
    exit 0

}

# X calibration. This is Tk, so we can use the canvas items.
# However, there is uncertainty about the absolute X and Y...

set wid [winfo screenwidth .]
set hei [winfo screenheight .]
wm geometry . ${wid}x${hei}+0+0

set x [expr $wid/8]
set y [expr $hei/8]
set X [expr $wid - $x]
set Y [expr $hei - $y]
set hx [expr $wid/2]
set hy [expr $hei/2]
set cwid [expr 2*$x]
set chei [expr 2*$y]

# The offset variables account for window manager borders etc
set xoff1 0
set yoff1 0
set xoff2 0
set yoff2 0

proc recanvas {} {
    global x y X Y xoff1 xoff2 yoff1 yoff2
    catch {.l.c delete line}
    catch {.r.c delete line}
    
    set x1 [expr $x-$xoff1]
    set y1 [expr $Y-$yoff1]
    set x2 [expr $X-$xoff2]
    set y2 [expr $y-$yoff2]
    
    .l.c create line [expr $x1-50] $y1 [expr $x1+50] $y1 \
	    -width 3 -fill red -tag line
    .l.c create line $x1 [expr $y1-50] $x1 [expr $y1+50] \
	    -width 3 -fill red -tag line
    
    .r.c create line [expr $x2-50] $y2 [expr $x2+50] $y2 \
	    -width 3 -fill red -tag line
    .r.c create line $x2 [expr $y2-50] $x2 [expr $y2+50] \
	    -width 3 -fill red -tag line
}


pack [frame .l] -side left -expand true -fill both 
pack [frame .m] -side left -expand true -fill both 
pack [frame .r] -side left -expand true -fill both 
pack [canvas .l.c -width $cwid -hei $chei -scrollregion "0 0 $cwid $chei" \
	] -side bottom 
pack [frame .l.f] -expand true -fill both; #filler
pack [canvas .r.c -width $cwid -hei $chei -scrollregion "0 0 $cwid $chei" \
	] -side top
pack [frame .r.f] -expand true -fill both; #filler

pack [label .m.t -text "Gunze Calibrator" -foreground blue] -pady 40
pack [label .m.l -bd 5 -relief raised] -expand true -fill both
pack [label .m.s] -expand true -fill both

bind .l.c <Enter> {
    set xoff1 [expr %X - %x]
    set yoff1 [expr %Y - %y]
    set x1 [expr $x-$xoff1]
    set y1 [expr $Y-$yoff1]

    catch {.l.c delete line}
    
    .l.c create line [expr $x1-50] $y1 [expr $x1+50] $y1 \
	    -width 3 -fill red -tag line
    .l.c create line $x1 [expr $y1-50] $x1 [expr $y1+50] \
	    -width 3 -fill red -tag line
    
    set done1 1
    update
}
bind .r.c <Enter> {
    set xoff2 [expr %X - %x]
    set yoff2 [expr %Y - %y]
    set x2 [expr $X-$xoff2]
    set y2 [expr $y-$yoff2]

    catch {.r.c delete line}

    .r.c create line [expr $x2-50] $y2 [expr $x2+50] $y2 \
	    -width 3 -fill red -tag line
    .r.c create line $x2 [expr $y2-50] $x2 [expr $y2+50] \
	    -width 3 -fill red -tag line

    set done2 1
    update
}

set done1 0; set done2 0

update

.m.l config -text "Please move the mouse towards\n\
	the lower left corner, until a cross\n\
	appears (there's no need to click)"
vwait done1

update

if !$done2 {
    .m.l config -text "Please move the mouse towards\n\
	    the upper right corner, until a cross\n\
	    appears (there's no need to click)"
    vwait done2
}

.m.l config -text "\nWait....\n"
after 1000

.m.l config -text "Please touch the lower-left taget\n\
	and then the upper-right target\n\
	to calibrate the touch screen"
update

exec killall -STOP gpm
set F [open $dev r+]; # $dev is /dev/touchscreen or /dev/mouse
fconfigure $F -buffering none -translation auto

.m.s configure -text "Calibration started"
update

# Read coordinates
set first [get_position $F]

.l.c itemco line -fill green
.m.s configure -text "First target ok"
update

# Read coordinates
set second [get_position $F]

.r.c itemco line -fill green
.m.s configure -text "Second target ok"
update

close $F

exec killall -CONT gpm

# Now save the coordinates
set F [open $datafile w]
puts $F "# Calibration coordinates for Gunze device"
puts $F "$first $second"
close $F

after 1000
.m.s configure -text ""
pack [button .m.b -text Ok -command exit]

exit 0