blob: 18fef451e005a4b2b6a481803a262a2fb69b5df3 [file] [log] [blame]
Guido van Rossum05e46dd1992-12-14 13:00:14 +00001#! /usr/local/bin/python
Guido van Rossum453bd401992-03-30 13:18:37 +00002
3# "M Clock"
4#
5# An implementation in software of an original design by Rob Juda.
6# Clock implementation: Guido van Rossum.
7# Alarm and Gong features: Sape Mullender.
8#
9# XXX TO DO:
10# add arguments to specify initial window position and size
11# find out local time zone difference automatically
12# add a date indicator
13# allow multiple alarms
14# allow the menu to change more parameters
15
16import sys
17
18from gl import *
19from GL import *
20from DEVICE import *
21import time
22import getopt
23import string
24import os
25from math import pi
26import math
27
28FULLC = 3600 # Full circle in 1/10-ths of a degree
29MIDN = 900 # Angle of the 12 o'clock position
30R, G, B = 0, 1, 2 # Indices of colors in RGB list
31
32HOUR = 3600 # Number of seconds per hour
33MINUTE = 60 # Number of seconds per minute
34
35class struct: pass # Class to define featureless structures
36Gl = struct() # Object to hold writable global variables
37
38# Default constants (used in multiple places)
39
40SCREENBG = 127, 156, 191
41NPARTS = 9
42TITLE = 'M Clock'
Guido van Rossum05e46dd1992-12-14 13:00:14 +000043
44import tzparse
45TZDIFF = tzparse.timezone
46if tzparse.isdst(time.time()):
47 TZDIFF = tzparse.altzone
Guido van Rossum453bd401992-03-30 13:18:37 +000048
49# Default parameters
50
51Gl.foreground = 0 # If set, run in the foreground
52Gl.fullscreen = 0 # If set, run on full screen
53Gl.tzdiff = TZDIFF # Seconds west of Greenwich (winter time)
54Gl.nparts = NPARTS # Number of parts each circle is divided in (>= 2)
55Gl.debug = 0 # If set, print debug output
56Gl.doublebuffer = 1 # If set, use double buffering
57Gl.update = 0 # Update interval; seconds hand is suppressed if > 1
58Gl.colorsubset = 0 # If set, display only a subset of the colors
59Gl.cyan = 0 # If set, display cyan overlay (big hand)
60Gl.magenta = 0 # If set, display magenta overlay (little hand)
61Gl.yellow = 0 # If set, display yellow overlay (fixed background)
62Gl.black = 0 # If set, display black overlay (hands)
63Gl.colormap = 0 # If set, use colormap mode instead of RGB mode
64Gl.warnings = 0 # If set, print warnings
65Gl.title = '' # Window title (default set later)
66Gl.name = 'mclock' # Window title for resources
67Gl.border = 1 # If set, use a window border (and title)
68Gl.bg = 0, 0, 0 # Background color R, G, B value
69Gl.iconic = 0 # Set in iconic state
70Gl.fg = 255, 0, 0 # Alarm background RGB (either normal or alarm)
71Gl.ox,Gl.oy = 0,0 # Window origin
72Gl.cx,Gl.cy = 0,0 # Window size
73Gl.alarm_set = 0 # Alarm on or off
74Gl.alarm_on = 0 # Alarm is ringing
75Gl.alarm_time = 0 # Alarm time in seconds after midnight
76Gl.alarm_hours = 0 # Alarm hour setting, 24 hour clock
77Gl.alarm_minutes = 0 # Alarm minutes setting
78Gl.alarm_rgb = 0,0,0 # Alarm display RGB colors
79Gl.alarm_cmd = '' # Command to execute when alarm goes off
80Gl.mouse2down = 0 # Mouse button state
81Gl.mouse3down = 0 # Mouse button state
82Gl.gong_cmd = '' # Command to execute when chimes go off
83Gl.gong_int = 3600 # Gong interval
84Gl.indices = R, G, B # Colors (permuted when alarm is on)
85
86def main():
87 #
88 sys.stdout = sys.stderr # All output is errors/warnings etc.
89 #
90 try:
91 args = getoptions()
92 except string.atoi_error, value:
93 usage(string.atoi_error, value)
94 except getopt.error, msg:
95 usage(getopt.error, msg)
96 #
97 if args:
98 realtime = 0
99 hours = string.atoi(args[0])
100 minutes = seconds = 0
101 if args[1:]: minutes = string.atoi(args[1])
102 if args[2:]: seconds = string.atoi(args[2])
103 localtime = ((hours*60)+minutes)*60+seconds
104 else:
105 realtime = 1
106 #
107 if Gl.title == '':
108 if realtime:
109 Gl.title = TITLE
110 else:
111 title = ''
112 for arg in args: title = title + ' ' + arg
113 Gl.title = title[1:]
114 del title
115 #
116 wid = makewindow()
117 Gl.ox,Gl.oy = getorigin()
118 Gl.cx,Gl.cy = getsize()
119 initmenu()
120 clearall()
121 #
122 if not Gl.update:
123 Gl.update = 60
124 #
125 if Gl.update <= 1:
126 Gl.timernoise = 6
127 else:
128 Gl.timernoise = 60
129 noise(TIMER0, Gl.timernoise)
130 #
131 qdevice(WINSHUT)
132 qdevice(WINQUIT)
133 qdevice(ESCKEY)
134 if realtime:
135 qdevice(TIMER0)
136 qdevice(REDRAW)
137 qdevice(WINFREEZE)
138 qdevice(WINTHAW)
139 qdevice(MENUBUTTON) # MOUSE1
140 qdevice(MOUSE3) # Left button
141 qdevice(MOUSE2) # Middle button
142 unqdevice(INPUTCHANGE)
143 #
144 lasttime = 0
145 Gl.change = 1
146 while 1:
147 if realtime:
148 localtime = time.time() - Gl.tzdiff
149 if Gl.alarm_set:
150 if localtime%(24*HOUR) == Gl.alarm_time:
151 # Ring the alarm!
152 if Gl.debug:
153 print 'Rrrringg!'
154 Gl.alarm_on = 1
155 if Gl.alarm_cmd <> '':
156 d = os.system(Gl.alarm_cmd+' '+`Gl.alarm_time/3600`+' '+`(Gl.alarm_time/60)%60` + ' &')
157 Gl.change = 1
158 clearall()
159 if Gl.alarm_on:
160 if (localtime - Gl.alarm_time) % (24*HOUR) > 300:
161 # More than 5 minutes away from alarm
162 Gl.alarm_on = 0
163 if Gl.debug:
164 print 'Alarm turned off'
165 Gl.change = 1
166 clearall()
167 Gl.indices = R, G, B
168 else:
169 if localtime % 2 == 0:
170 # Permute color indices
171 Gl.indices = Gl.indices[2:] + Gl.indices[:2]
172 Gl.change = 1
173 if Gl.gong_cmd <> '' and localtime%Gl.gong_int == 0:
174 d = os.system(Gl.gong_cmd+' '+`(localtime/3600)%24`+' '+`(localtime/60)%60` + ' &')
175 if localtime/Gl.update <> lasttime/Gl.update:
176 if Gl.debug: print 'new time'
177 Gl.change = 1
178 if Gl.change:
179 if Gl.debug: print 'drawing'
180 doit(localtime)
181 lasttime = localtime
182 Gl.change = 0
183 dev, data = qread()
184 if Gl.debug and dev <> TIMER0:
185 print dev, data
186 if dev == TIMER0:
187 if Gl.debug > 1:
188 print dev, data
189 elif dev == MOUSE3:
190 mousex = getvaluator(MOUSEX)
191 mousey = getvaluator(MOUSEY)
192 if mouseclick(3, data, mousex, mousey):
193 Gl.change = 1
194 elif dev == MOUSE2:
195 mousex = getvaluator(MOUSEX)
196 mousey = getvaluator(MOUSEY)
197 if mouseclick(2, data, mousex, mousey):
198 Gl.change = 1
199 elif dev == MOUSEX:
200 mousex = data
201 if Gl.mouse2down:
202 mouse2track(mousex, mousey)
203 if Gl.mouse3down:
204 mouse3track(mousex, mousey)
205 elif dev == MOUSEY:
206 mousey = data
207 if Gl.mouse2down:
208 mouse2track(mousex, mousey)
209 if Gl.mouse3down:
210 mouse3track(mousex, mousey)
211 elif dev == REDRAW or dev == REDRAWICONIC:
212 if Gl.debug:
213 if dev == REDRAW: print 'REDRAW'
214 else: print 'REDRAWICONIC'
215 reshapeviewport()
216 Gl.ox,Gl.oy = getorigin()
217 Gl.cx,Gl.cy = getsize()
218 Gl.change = 1
219 clearall()
220 elif dev == MENUBUTTON:
221 if Gl.debug: print 'MENUBUTTON'
222 handlemenu()
223 elif dev == WINFREEZE:
224 if Gl.debug: print 'WINFREEZE'
225 Gl.iconic = 1
226 noise(TIMER0, 60*60) # Redraw every 60 seconds only
227 elif dev == WINTHAW:
228 if Gl.debug: print 'WINTHAW'
229 Gl.iconic = 0
230 noise(TIMER0, Gl.timernoise)
231 Gl.change = 1
232 elif dev == ESCKEY or dev == WINSHUT or dev == WINQUIT:
233 if Gl.debug: print 'Exit'
234 sys.exit(0)
235
236def getoptions():
237 optlist, args = getopt.getopt(sys.argv[1:], 'A:a:B:bc:dFfG:g:n:sT:t:u:wCMYK')
238 for optname, optarg in optlist:
239 if optname == '-A':
240 Gl.fg = eval(optarg) # Should be (r,g,b)
241 elif optname == '-a':
242 Gl.alarm_cmd = optarg
243 elif optname == '-B':
244 Gl.bg = eval(optarg) # Should be (r,g,b)
245 elif optname == '-b':
246 Gl.border = 0
247 elif optname == '-c':
248 Gl.colormap = string.atoi(optarg)
249 elif optname == '-d':
250 Gl.debug = Gl.debug + 1
251 Gl.warnings = 1
252 elif optname == '-F':
253 Gl.foreground = 1
254 elif optname == '-f':
255 Gl.fullscreen = 1
256 elif optname == '-G':
257 Gl.gong_int = 60*string.atoi(optarg)
258 elif optname == '-g':
259 Gl.gong_cmd = optarg
260 elif optname == '-n':
261 Gl.nparts = string.atoi(optarg)
262 elif optname == '-s':
263 Gl.doublebuffer = 0
264 elif optname == '-T':
265 Gl.title = Gl.name = optarg
266 elif optname == '-t':
267 Gl.tzdiff = string.atoi(optarg)
268 elif optname == '-u':
269 Gl.update = string.atoi(optarg)
270 elif optname == '-w':
271 Gl.warnings = 1
272 elif optname == '-C':
273 Gl.cyan = Gl.colorsubset = 1
274 elif optname == '-M':
275 Gl.magenta = Gl.colorsubset = 1
276 elif optname == '-Y':
277 Gl.yellow = Gl.colorsubset = 1
278 elif optname == '-K':
279 Gl.black = Gl.colorsubset = 1
280 else:
281 print 'Unsupported option', optname
282 return args
283
284def usage(exc, msg):
285 if sys.argv:
286 progname = os.path.basename(sys.argv[0])
287 else:
288 progname = 'mclock'
289 #
290 print progname + ':',
291 if exc == string.atoi_error:
292 print 'non-numeric argument:',
293 print msg
294 #
295 print 'usage:', progname, '[options] [hh [mm [ss]]]'
296 #
297 print '-A r,g,b : alarm background red,green,blue [255,0,0]'
298 print '-a cmd : shell command executed when alarm goes off'
299 print '-B r,g,b : background red,green,blue [0,0,0]'
300 print ' (-B SCREENBG uses the default screen background)'
301 print '-b : suppress window border and title'
302 print '-c cmapid : select explicit colormap'
303 print '-d : more debug output (implies -F, -w)'
304 print '-F : run in foreground'
305 print '-f : use full screen'
306 print '-G intrvl : interval between chimes in minutes [60]'
307 print '-g cmd : shell command executed when chimes go off'
308 print '-s : single buffer mode'
309 print '-w : print various warnings'
310 print '-n nparts : number of parts [' + `NPARTS` + ']'
311 print '-T title : alternate window title [\'' + TITLE + '\']'
312 print '-t tzdiff : time zone difference [' + `TZDIFF` + ']'
313 print '-u update : update interval [60]'
314 print '-CMYK : Cyan, Magenta, Yellow or blacK overlay only'
315 print 'if hh [mm [ss]] is specified, display that time statically'
316 print 'on machines with < 12 bitplanes, -c and -s are forced on'
317 #
318 sys.exit(2)
319
320def doit(localtime):
321 hands = makehands(localtime)
322 list = makelist(hands)
323 render(list, hands)
324
325def makehands(localtime):
326 localtime = localtime % (12*HOUR)
327 seconds_hand = MIDN + FULLC - (localtime*60) % FULLC
328 big_hand = (MIDN + FULLC - (localtime%HOUR)) % FULLC
329 little_hand = (MIDN + FULLC - ((localtime/12) % HOUR)) % FULLC
330 return little_hand, big_hand, seconds_hand
331
332def makelist(little_hand, big_hand, seconds_hand):
333 total = []
334 if Gl.cyan or not Gl.colorsubset:
335 total = total + makesublist(big_hand, Gl.indices[0])
336 if Gl.magenta or not Gl.colorsubset:
337 total = total + makesublist(little_hand, Gl.indices[1])
338 if Gl.yellow or not Gl.colorsubset:
339 total = total + makesublist(MIDN, Gl.indices[2])
340 total.sort()
341 return total
342
343def makesublist(first, icolor):
344 list = []
345 alpha = FULLC/Gl.nparts
346 a = first - alpha/2
347 for i in range(Gl.nparts):
348 angle = (a + i*alpha + FULLC) % FULLC
349 value = 255*(Gl.nparts-1-i)/(Gl.nparts-1)
350 list.append(angle, icolor, value)
351 list.sort()
352 a, icolor, value = list[0]
353 if a <> 0:
354 a, icolor, value = list[len(list)-1]
355 t = 0, icolor, value
356 list.insert(0, t)
357 return list
358
359def rgb_fg():
360 return Gl.fg
361 # Obsolete code:
362 if Gl.alarm_on:
363 return Gl.bg
364 else:
365 return Gl.fg
366
367def rgb_bg():
368 return Gl.bg
369 # Obsolete code:
370 if Gl.alarm_on:
371 return Gl.fg
372 else:
373 return Gl.bg
374
375def clearall():
376 Gl.c3i(rgb_bg())
377 clear()
378 if Gl.doublebuffer:
379 swapbuffers()
380 clear()
381
382def draw_alarm(color):
383 frontbuffer(TRUE)
384 Gl.c3i(color)
385 pushmatrix()
386 rotate(-((Gl.alarm_time/12)%3600), 'z')
387 bgnpolygon()
388 v2f( 0.00,1.00)
389 v2f( 0.04,1.05)
390 v2f(-0.04,1.05)
391 endpolygon()
392 popmatrix()
393 #
394 pushmatrix()
395 rotate(-((Gl.alarm_time)%3600), 'z')
396 bgnpolygon()
397 v2f( 0.00,1.05)
398 v2f( 0.07,1.10)
399 v2f(-0.07,1.10)
400 endpolygon()
401 popmatrix()
402 #
403 cmov2(-1.06, -1.06)
404 charstr(string.rjust(`Gl.alarm_time/3600`,2))
405 charstr(':')
406 charstr(string.zfill((Gl.alarm_time/60)%60,2))
407 frontbuffer(FALSE)
408
409def render(list, (little_hand, big_hand, seconds_hand)):
410 #
411 if Gl.colormap:
412 resetindex()
413 #
414 if not list:
Guido van Rossum05e46dd1992-12-14 13:00:14 +0000415 Gl.c3i((255, 255, 255)) # White
Guido van Rossum453bd401992-03-30 13:18:37 +0000416 circf(0.0, 0.0, 1.0)
417 else:
418 list.append(3600, 0, 255) # Sentinel
419 #
420 rgb = [255, 255, 255]
421 a_prev = 0
422 for a, icolor, value in list:
423 if a <> a_prev:
424 [r, g, b] = rgb
425 if Gl.debug > 1:
426 print rgb, a_prev, a
Guido van Rossum05e46dd1992-12-14 13:00:14 +0000427 Gl.c3i((r, g, b))
Guido van Rossum453bd401992-03-30 13:18:37 +0000428 arcf(0.0, 0.0, 1.0, a_prev, a)
429 rgb[icolor] = value
430 a_prev = a
431 #
432 if Gl.black or not Gl.colorsubset:
433 #
434 # Draw the hands -- in black
435 #
Guido van Rossum05e46dd1992-12-14 13:00:14 +0000436 Gl.c3i((0, 0, 0))
Guido van Rossum453bd401992-03-30 13:18:37 +0000437 #
438 if Gl.update == 1 and not Gl.iconic:
439 # Seconds hand is only drawn if we update every second
440 pushmatrix()
441 rotate(seconds_hand, 'z')
442 bgnline()
443 v2f(0.0, 0.0)
444 v2f(1.0, 0.0)
445 endline()
446 popmatrix()
447 #
448 pushmatrix()
449 rotate(big_hand, 'z')
450 rectf(0.0, -0.01, 0.97, 0.01)
451 circf(0.0, 0.0, 0.01)
452 circf(0.97, 0.0, 0.01)
453 popmatrix()
454 #
455 pushmatrix()
456 rotate(little_hand, 'z')
457 rectf(0.04, -0.02, 0.63, 0.02)
458 circf(0.04, 0.0, 0.02)
459 circf(0.63, 0.0, 0.02)
460 popmatrix()
461 #
462 # Draw the alarm time, if set or being set
463 #
464 if Gl.alarm_set:
465 draw_alarm(rgb_fg())
466 #
467 if Gl.doublebuffer: swapbuffers()
468
469def makewindow():
470 #
471 if Gl.debug or Gl.foreground:
472 foreground()
473 #
474 if Gl.fullscreen:
475 scrwidth, scrheight = getgdesc(GD_XPMAX), getgdesc(GD_YPMAX)
476 prefposition(0, scrwidth-1, 0, scrheight-1)
477 else:
478 keepaspect(1, 1)
479 prefsize(100, 100)
480 #
481 if not Gl.border:
482 noborder()
483 wid = winopen(Gl.name)
484 wintitle(Gl.title)
485 #
486 if not Gl.fullscreen:
487 keepaspect(1, 1)
488 minsize(10, 10)
489 maxsize(2000, 2000)
490 iconsize(66, 66)
491 winconstraints()
492 #
493 nplanes = getplanes()
494 nmaps = getgdesc(GD_NMMAPS)
495 if Gl.warnings:
496 print nplanes, 'color planes,', nmaps, 'color maps'
497 #
498 if nplanes < 12 or Gl.colormap:
499 if not Gl.colormap:
500 Gl.colormap = nmaps - 1
501 if Gl.warnings:
502 print 'not enough color planes available',
503 print 'for RGB mode; forcing colormap mode'
504 print 'using color map number', Gl.colormap
505 if not Gl.colorsubset:
506 needed = 3
507 else:
508 needed = Gl.cyan + Gl.magenta + Gl.yellow
509 needed = needed*Gl.nparts
510 if Gl.bg <> (0, 0, 0):
511 needed = needed+1
512 if Gl.fg <> (0, 0, 0):
513 needed = needed+1
514 if Gl.doublebuffer:
515 if needed > available(nplanes/2):
516 Gl.doublebuffer = 0
517 if Gl.warnings:
518 print 'not enough colors available',
519 print 'for double buffer mode;',
520 print 'forcing single buffer mode'
521 else:
522 nplanes = nplanes/2
523 if needed > available(nplanes):
524 # Do this warning always
525 print 'still not enough colors available;',
526 print 'parts will be left white'
527 print '(needed', needed, 'but have only',
528 print available(nplanes), 'colors available)'
529 #
530 if Gl.doublebuffer:
531 doublebuffer()
532 gconfig()
533 #
534 if Gl.colormap:
535 Gl.c3i = pseudo_c3i
536 fixcolormap()
537 else:
538 Gl.c3i = c3i
539 RGBmode()
540 gconfig()
541 #
542 if Gl.fullscreen:
543 # XXX Should find out true screen size using getgdesc()
544 ortho2(-1.1*1.280, 1.1*1.280, -1.1*1.024, 1.1*1.024)
545 else:
546 ortho2(-1.1, 1.1, -1.1, 1.1)
547 #
548 return wid
549
550def available(nplanes):
551 return pow(2, nplanes) - 1 # Reserve one pixel for black
552
553def fixcolormap():
554 multimap()
555 gconfig()
556 nplanes = getplanes()
557 if Gl.warnings:
558 print 'multimap mode has', nplanes, 'color planes'
559 imap = Gl.colormap
560 Gl.startindex = pow(2, nplanes) - 1
561 Gl.stopindex = 1
562 setmap(imap)
563 mapcolor(0, 0, 0, 0) # Fixed entry for black
564 if Gl.bg <> (0, 0, 0):
565 r, g, b = Gl.bg
566 mapcolor(1, r, g, b) # Fixed entry for Gl.bg
567 Gl.stopindex = 2
568 if Gl.fg <> (0, 0, 0):
569 r, g, b = Gl.fg
570 mapcolor(2, r, g, b) # Fixed entry for Gl.fg
571 Gl.stopindex = 3
572 Gl.overflow_seen = 0
573 resetindex()
574
575def resetindex():
576 Gl.index = Gl.startindex
577
578r0g0b0 = (0, 0, 0)
579
580def pseudo_c3i(rgb):
581 if rgb == r0g0b0:
582 index = 0
583 elif rgb == Gl.bg:
584 index = 1
585 elif rgb == Gl.fg:
586 index = 2
587 else:
588 index = definecolor(rgb)
589 color(index)
590
591def definecolor(rgb):
592 index = Gl.index
593 if index < Gl.stopindex:
594 if Gl.debug: print 'definecolor hard case', rgb
595 # First see if we already have this one...
596 for index in range(Gl.stopindex, Gl.startindex+1):
597 if rgb == getmcolor(index):
598 if Gl.debug: print 'return', index
599 return index
600 # Don't clobber reserverd colormap entries
601 if not Gl.overflow_seen:
602 # Shouldn't happen any more, hence no Gl.warnings test
603 print 'mclock: out of colormap entries'
604 Gl.overflow_seen = 1
605 return Gl.stopindex
606 r, g, b = rgb
607 if Gl.debug > 1: print 'mapcolor', (index, r, g, b)
608 mapcolor(index, r, g, b)
609 Gl.index = index - 1
610 return index
611
612# Compute n**i
613def pow(n, i):
614 x = 1
615 for j in range(i): x = x*n
616 return x
617
618def mouseclick(mouse, updown, x, y):
619 if updown == 1:
620 # mouse button came down, start tracking
621 if Gl.debug:
622 print 'mouse', mouse, 'down at', x, y
623 if mouse == 2:
624 Gl.mouse2down = 1
625 mouse2track(x, y)
626 elif mouse == 3:
627 Gl.mouse3down = 1
628 mouse3track(x, y)
629 else:
630 print 'fatal error'
631 qdevice(MOUSEX)
632 qdevice(MOUSEY)
633 return 0
634 else:
635 # mouse button came up, stop tracking
636 if Gl.debug:
637 print 'mouse', mouse, 'up at', x, y
638 unqdevice(MOUSEX)
639 unqdevice(MOUSEY)
640 if mouse == 2:
641 mouse2track(x, y)
642 Gl.mouse2down = 0
643 elif mouse == 3:
644 mouse3track(x, y)
645 Gl.mouse3down = 0
646 else:
647 print 'fatal error'
648 Gl.alarm_set = 1
649 return 1
650
651def mouse3track(x, y):
652 # first compute polar coordinates from x and y
653 cx, cy = Gl.ox + Gl.cx/2, Gl.oy + Gl.cy/2
654 x, y = x - cx, y - cy
655 if (x, y) == (0, 0): return # would cause an exception
656 minutes = int(30.5 + 30.0*math.atan2(float(-x), float(-y))/pi)
657 if minutes == 60: minutes = 0
658 a,b = Gl.alarm_minutes/15, minutes/15
659 if (a,b) == (0,3):
660 # Moved backward through 12 o'clock:
661 Gl.alarm_hours = Gl.alarm_hours - 1
662 if Gl.alarm_hours < 0: Gl.alarm_hours = Gl.alarm_hours + 24
663 if (a,b) == (3,0):
664 # Moved forward through 12 o'clock:
665 Gl.alarm_hours = Gl.alarm_hours + 1
666 if Gl.alarm_hours >= 24: Gl.alarm_hours = Gl.alarm_hours - 24
667 Gl.alarm_minutes = minutes
668 seconds = Gl.alarm_hours * HOUR + Gl.alarm_minutes * MINUTE
669 if seconds <> Gl.alarm_time:
670 draw_alarm(rgb_bg())
671 Gl.alarm_time = seconds
672 draw_alarm(rgb_fg())
673
674def mouse2track(x, y):
675 # first compute polar coordinates from x and y
676 cx, cy = Gl.ox + Gl.cx/2, Gl.oy + Gl.cy/2
677 x, y = x - cx, y - cy
678 if (x, y) == (0, 0): return # would cause an exception
679 hours = int(6.5 - float(Gl.alarm_minutes)/60.0 + 6.0*math.atan2(float(-x), float(-y))/pi)
680 if hours == 12: hours = 0
681 if (Gl.alarm_hours,hours) == (0,11):
682 # Moved backward through midnight:
683 Gl.alarm_hours = 23
684 elif (Gl.alarm_hours,hours) == (12,11):
685 # Moved backward through noon:
686 Gl.alarm_hours = 11
687 elif (Gl.alarm_hours,hours) == (11,0):
688 # Moved forward through noon:
689 Gl.alarm_hours = 12
690 elif (Gl.alarm_hours,hours) == (23,0):
691 # Moved forward through midnight:
692 Gl.alarm_hours = 0
693 elif Gl.alarm_hours < 12:
694 Gl.alarm_hours = hours
695 else:
696 Gl.alarm_hours = hours + 12
697 seconds = Gl.alarm_hours * HOUR + Gl.alarm_minutes * MINUTE
698 if seconds <> Gl.alarm_time:
699 draw_alarm(rgb_bg())
700 Gl.alarm_time = seconds
701 draw_alarm(rgb_fg())
702
703def initmenu():
704 Gl.pup = pup = newpup()
705 addtopup(pup, 'M Clock%t|Alarm On/Off|Seconds Hand On/Off|Quit', 0)
706
707def handlemenu():
708 item = dopup(Gl.pup)
709 if item == 1:
710 # Toggle alarm
711 if Gl.alarm_set:
712 Gl.alarm_set = 0
713 Gl.alarm_on = 0
714 else:
715 Gl.alarm_set = 1
716 Gl.change = 1
717 clearall()
718 elif item == 2:
719 # Toggle Seconds Hand
720 if Gl.update == 1:
721 Gl.update = 60
722 Gl.timernoise = 60
723 else:
724 Gl.update = 1
725 Gl.timernoise = 6
726 Gl.change = 1
727 elif item == 3:
728 if Gl.debug: print 'Exit'
729 sys.exit(0)
730
731main()