chiark / gitweb /
changelog: start 1.0-2
[chiark-tcl-applet.git] / applet.tcl
1 # General purpose code for being a tray applet
2
3 # Copyright 2016,2020 Ian Jackson
4 # SPDX-License-Identifier: GPL-3.0-or-later
5 # There is NO WARRANTY.
6
7 package require Tclx
8 package require tktray
9
10 #----- general machinery -----
11
12 # Interface:
13 #
14 #  tk::tktray widget is called .i
15 #
16 # Tooltip:
17 #
18 #   Caller may call
19 #      applet::setup-tooltip ON-VISIBLE ON-INVISIBLE
20 #   to make applet have a tooltip.
21 #
22 #   ON-VISIBLE and ON-INVISIBLE will be globally eval'd
23 #   when the tooltip becomes visible and invisible.
24 #
25 #   Caller should call
26 #      applet::tooltip-set TEXT-MAYBE-MULTILINE
27 #   whenever they like.
28
29 # Button presses
30 #
31 #    Caller may bind .i.i <ButtonPress-$b>
32 #
33 #    Alternatively caller may call  applet::setup-button-menu $b
34 #    which will generate a menu .m$b which the user can configure
35 #    and which will automatically be posted and unposted etc.
36 #    In this case the caller should arrange that all of their
37 #    menus, when an item is selected, call
38 #      applet::msel
39 #
40 # Icon:
41 #
42 #  Caller should call:
43 #      applet::setimage IMAGE
44 #  as necessary.
45 #
46 # Alternatively of icon, it may provide other arrangements for
47 # using the provided subwindow.  Such a caller should call
48 #      applet::setup-subwindow ON-DESTROYING ON-READY
49 #  Then the main code will call ON-DESTROYING just before
50 #  destroying the inner window and recreating it, and
51 #  [concat ON-READY [list ORIENTATION]]
52 #  just after.  The inner window to use is called .i.i.b.
53 #
54 #  This uses variables, in the applet namespace,
55 #      w h border_colour border_width deforient
56 #  These should be set before setup-subwindow is called and not
57 #  modified thereafter.
58 #
59 #  The user of the subwindow machinery may call
60 #      applet::subwindow-need-recreate
61 #  if for any reason the inner window should be destroyed and recreated.
62 #
63 # Alternatively, it may request that a subprocess be spawned
64 # repeatedly with the xid of a suitable window.
65 #      applet::setup-subproc GET-CMDLINE
66 #  Then the main code will call [concat GET_CMDLINE [list XID ORIENTATION]]
67 #  to get the command line to run.
68 #
69 #  This also uses the same variables as setup-subwindow.
70
71 namespace eval applet {
72
73 proc become {} {
74     wm withdraw .
75
76     tktray::icon .i -class example
77     .i configure -docked 1
78
79     fconfigure stdout -buffering none
80     fconfigure stderr -buffering none
81 }
82
83 # used by both menus and tooltips
84 variable posted 0
85 variable tooltip_offset {9 9}
86
87 #----- menus -----
88
89 proc setup-button-menu {b} {
90     bind .i.i <ButtonPress> { applet::menubuttonpressed %b %X %Y }
91     menu .m$b -tearoff 0
92 }
93
94 proc menubuttonpressed {b x y} {
95     variable posted
96     tooltip-cancel
97     if {$posted == $b} {
98         debug::debug "unpost $posted toggle"
99         .m$posted unpost
100         set posted 0
101     } elseif {[winfo exists .m$b]} {
102         if {$posted} {
103             .m$posted unpost
104             debug::debug "unpost $posted other"
105         }
106         debug::debug "post $b"
107         set posted $b
108         .m$b post $x $y
109     }
110 }
111
112 proc msel {} {
113     variable posted
114     set posted 0
115 }
116
117 #----- tooltips -----
118
119 variable tooltip_on_vis {}
120 variable tooltip_on_invis {}
121
122 proc tooltip-starttimer {state x y} {
123     variable tooltip_after
124     variable posted
125     variable tooltip_inwindow
126     if {$state || $posted || !$tooltip_inwindow} { tooltip-cancel; return }
127     catch { after cancel $tooltip_after }
128     set tooltip_after [after 500 applet::tooltip-show $x $y]
129 }
130
131 proc tooltip-cancel {} {
132     variable tooltip_after
133     variable tooltip_on_invis
134     catch { after cancel $tooltip_after }
135     catch { unset $tooltip_after }
136     wm withdraw .tt
137     uplevel #0 $tooltip_on_invis
138 }
139
140 set tooltip_inwindow 0
141
142 proc tooltip-enter {state x y} {
143     variable tooltip_inwindow
144     set tooltip_inwindow 1
145     tooltip-starttimer $state $x $y
146 }
147
148 proc tooltip-leave {} {
149     variable tooltip_inwindow
150     set tooltip_inwindow 0
151     tooltip-cancel
152 }
153
154 proc setup-tooltip {on_vis on_invis} {
155     foreach v {vis invis} {
156         variable tooltip_on_$v [set on_$v]
157     }
158     bind .i <Enter> { applet::tooltip-enter %s %X %Y }
159     bind .i <Leave> { applet::tooltip-leave }
160     bind .i <ButtonRelease> { 
161         applet::tooltip-cancel
162         applet::tooltip-starttimer %s %X %Y
163     }
164     bind .i <Motion> { applet::tooltip-starttimer %s %X %Y }
165     toplevel .tt -background black
166     wm withdraw .tt
167     wm overrideredirect .tt 1
168     label .tt.t -justify left -background {#EEE1B3}
169     pack .tt.t -padx 1 -pady 1
170     tooltip-set {}
171 }
172
173 proc tooltip-set {s} {
174     .tt.t configure -text $s
175 }
176
177 proc tooltip-show {x y} {
178     variable tooltip_on_vis
179     variable tooltip_offset
180     incr x [lindex $tooltip_offset 0]
181     incr y [lindex $tooltip_offset 1]
182     wm geometry .tt +$x+$y
183     wm deiconify .tt
184     raise .tt
185     uplevel #0 $tooltip_on_vis
186 }
187
188 #----- simple images -----
189
190 proc setimage {image} {
191     .i configure -image $image
192 }
193
194 #----- subwindow -----
195
196 variable subwindow_on_destroying
197 variable subwindow_on_ready
198
199 variable w 50
200 variable h 50
201 variable deforient horizontal
202 variable border_colour darkblue
203 variable border_width 1
204 variable tray_width X
205 variable tray_height X
206 variable orientation vertical
207
208 proc subwindow-need-recreate {evtype why} {
209     variable orientation
210     variable innerwindow_after
211     variable tray_width
212     variable tray_height
213     debug::debug "IW-EVENT $evtype $why [winfo reqwidth .i] [winfo reqheight .i] [winfo width .i] [winfo height .i] $orientation"
214     switch -exact $orientation {
215         horizontal { set szv height }
216         vertical { set szv width }
217         unknown { return }
218     }
219     set new_sz [winfo req$szv .i]
220     if {![string compare $new_sz [set tray_$szv]]} {
221         return
222     }
223     set tray_$szv $new_sz
224 #    switch -exact -- $evtype 35 { return }
225     if {[info exists innerwindow_after]} return
226     set innerwindow_after [after idle applet::innerwindow-resetup]
227 }
228
229 proc innerwindow-resetup {} {
230     variable innerwindow_after
231     variable subwindow_on_destroying
232     variable subwindow_on_ready
233     variable border_colour
234     variable border_width
235     variable deforient
236     variable orientation
237     unset innerwindow_after
238
239     debug::debug RESETUP
240
241     if {![winfo exists .i.i]} return
242     destroy [frame .i.i.make-exist]
243
244     uplevel #0 $subwindow_on_destroying
245     catch { destroy .i.i.b }
246
247     set orientation [.i orientation]
248     debug::debug "orientation $orientation"
249     if {![string compare $orientation unknown]} {
250         set orientation $deforient
251     }
252     .i configure -image applet::innerwindow-ph-$orientation
253
254     frame .i.i.b -background $border_colour -bd $border_width
255     pack .i.i.b -fill both -side left -expand 1
256
257     uplevel #0 $subwindow_on_ready [list $orientation]
258 }
259
260 proc setup-subwindow {on_destroying on_ready} {
261     variable w
262     variable h
263
264     foreach v {on_destroying on_ready} {
265         variable subwindow_$v [set $v]
266     }
267
268     image create photo applet::innerwindow-ph-horizontal -width $w -height 2
269     image create photo applet::innerwindow-ph-vertical -width 2 -height $h
270     .i configure -image applet::innerwindow-ph-horizontal
271
272     destroy [frame .i.make-exist]
273     #destroy [frame .i.i.make-exist]
274     bind .i <<IconConfigure>> { 
275         applet::subwindow-need-recreate %T "%T i=%i k=%K N=%N R=%R S=%S k=%k m=%m d=%d s=%s a=%a b=%b c=%c f=%f w,h=%w,%h o=%o p=%p t=%t x,y=%x,%y B=%B D=%D E=%E P=%P W=%W X,Y=%X,%Y"
276     }
277 }
278
279 #----- subprocess -----
280
281 variable subproc none
282 variable ratelimit {}
283
284 proc setup-subproc {get_cmdline} {
285     variable subproc_get_cmdline $get_cmdline
286     setup-subwindow applet::subproc-destroying applet::subproc-ready
287 }
288
289 proc subproc-destroying {} {
290     variable subproc
291     debug::debug "DESTROYING $subproc"
292
293     catch { destroy .i.i.b.c }
294
295     switch -exact $subproc {
296         none { }
297         old { }
298         default { kill $subproc; set subproc old }
299     }
300 }
301
302 proc subproc-ready {orientation} {
303     variable subproc
304     variable subproc_orientation $orientation
305     debug::debug "READY $subproc"
306
307     frame .i.i.b.c -container 1 -background orange
308     pack .i.i.b.c -fill both -side left -expand 1
309
310     switch -exact $subproc {
311         none {
312             run-child
313         }
314         old {
315             # wait for it to die
316         }
317         default {
318             error "unexpected state $subproc"
319         }
320     }
321     debug::debug "READY-done $subproc"
322 }
323
324 proc run-child {} {
325     variable subproc
326     variable ratelimit
327     variable subproc_get_cmdline
328     variable subproc_orientation
329
330     set id [winfo id .i.i.b.c]
331     set cmd [uplevel #0 $subproc_get_cmdline [list $id $subproc_orientation]]
332
333     debug::debug "RUN-CHILD $subproc"
334     set now [clock seconds]
335     lappend ratelimit $now
336     while {[lindex $ratelimit 0] < {$now - 10}} {
337         set ratelimit [lrange $ratelimit 1 end]
338     }
339     if {[llength $ratelimit] > 10} {
340         puts stderr "crashing repeatedly, quitting $ratelimit"
341         exit 127
342     }
343
344     set subproc none
345     set subproc [subproc::fork applet::child-died {
346         execl [lindex $cmd 0] [lrange $cmd 1 end]
347     }]
348     debug::debug "FORKED $subproc"
349 }
350
351 proc child-died {how how2} {
352     debug::debug "DIED $how $how2"
353     variable subproc
354     switch -exact $subproc {
355         old {
356             set subproc none
357             run-child
358         }
359         default {
360             set subproc none
361             subwindow-need-recreate child-died child-died
362         }
363     }
364 }
365
366 }