home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2805 < prev    next >
Encoding:
Internet Message Format  |  1991-02-20  |  60.6 KB

  1. From: guido@cwi.nl (Guido van Rossum)
  2. Newsgroups: alt.sources
  3. Subject: Python 0.9.1 part 13/21
  4. Message-ID: <2975@charon.cwi.nl>
  5. Date: 19 Feb 91 17:42:15 GMT
  6.  
  7. : This is a shell archive.
  8. : Extract with 'sh this_file'.
  9. :
  10. : Extract part 01 first since it makes all directories
  11. echo 'Start of pack.out, part 13 out of 21:'
  12. if test -s 'demo/sgi/audio_stdwin/jukebox.py'
  13. then echo '*** I will not over-write existing file demo/sgi/audio_stdwin/jukebox.py'
  14. else
  15. echo 'x - demo/sgi/audio_stdwin/jukebox.py'
  16. sed 's/^X//' > 'demo/sgi/audio_stdwin/jukebox.py' << 'EOF'
  17. X#! /ufs/guido/bin/sgi/python
  18. X
  19. X# JUKEBOX: browse directories full of sampled sound files.
  20. X#
  21. X# One or more "list windows" display the files and subdirectories of
  22. X# the arguments.  Double-clicking on a subdirectory opens a new window
  23. X# displaying its contents (and so on recursively).  Double clicking
  24. X# on a file plays it as a sound file (assuming it is one).
  25. X#
  26. X# Playing is asynchronous: the application keeps listening to events
  27. X# while the sample is playing, so you can change the volume (gain)
  28. X# during playing, cancel playing or start a new sample right away.
  29. X#
  30. X# The control window displays the current output gain and a primitive
  31. X# "stop button" to cancel the current play request.
  32. X#
  33. X# Sound files must currently be in Dik Winter's compressed Mac format.
  34. X# Since decompression is costly, decompressed samples are saved in
  35. X# /usr/tmp/@j* until the application is left.  The files are read
  36. X# afresh each time, though.
  37. X
  38. Ximport audio
  39. Ximport sunaudio
  40. Ximport commands
  41. Ximport getopt
  42. Ximport path
  43. Ximport posix
  44. Ximport rand
  45. Ximport stdwin
  46. Xfrom stdwinevents import *
  47. Ximport string
  48. Ximport sys
  49. X
  50. Xfrom WindowParent import WindowParent
  51. Xfrom HVSplit import VSplit
  52. Xfrom Buttons import PushButton
  53. Xfrom Sliders import ComplexSlider
  54. X
  55. X# Pathnames
  56. X
  57. XHOME_BIN_SGI = '/ufs/guido/bin/sgi/'    # Directory where macsound2sgi lives
  58. XDEF_DB = '/ufs/dik/sounds/Mac/HCOM'    # Default directory of sounds
  59. X
  60. X
  61. X# Global variables
  62. X
  63. Xclass struct(): pass        # Class to define featureless structures
  64. X
  65. XG = struct()            # Holds writable global variables
  66. X
  67. X
  68. X# Main program
  69. X
  70. Xdef main():
  71. X    G.synchronous = 0    # If set, use synchronous audio.write()
  72. X    G.debug = 0        # If set, print debug messages
  73. X    G.gain = 75        # Output gain
  74. X    G.rate = 3        # Sampling rate
  75. X    G.busy = 0        # Set while asynchronous playing is active
  76. X    G.windows = []        # List of open windows (except control)
  77. X    G.mode = 'mac'        # Macintosh mode
  78. X    G.tempprefix = '/usr/tmp/@j' + `rand.rand()` + '-'
  79. X    #
  80. X    optlist, args = getopt.getopt(sys.argv[1:], 'dg:r:sSa')
  81. X    for optname, optarg in optlist:
  82. X        if   optname = '-d':
  83. X            G.debug = 1
  84. X        elif optname = '-g':
  85. X            G.gain = string.atoi(optarg)
  86. X            if not (0 < G.gain < 256):
  87. X                raise optarg.error, '-g gain out of range'
  88. X        elif optname = '-r':
  89. X            G.rate = string.atoi(optarg)
  90. X            if not (1 <= G.rate <= 3):
  91. X                raise optarg.error, '-r rate out of range'
  92. X        elif optname = '-s':
  93. X            G.synchronous = 1
  94. X        elif optname = '-S':
  95. X            G.mode = 'sgi'
  96. X        elif optname = '-a':
  97. X            G.mode = 'sun'
  98. X    #
  99. X    if not args:
  100. X        args = [DEF_DB]
  101. X    #
  102. X    G.cw = opencontrolwindow()
  103. X    for dirname in args:
  104. X        G.windows.append(openlistwindow(dirname))
  105. X    #
  106. X    #
  107. X    savegain = audio.getoutgain()
  108. X    try:
  109. X        # Initialize stdaudio
  110. X        audio.setoutgain(0)
  111. X        audio.start_playing('')
  112. X        dummy = audio.wait_playing()
  113. X        audio.setoutgain(0)
  114. X        maineventloop()
  115. X    finally:
  116. X        audio.setoutgain(savegain)
  117. X        audio.done()
  118. X        clearcache()
  119. X
  120. Xdef maineventloop():
  121. X    mouse_events = WE_MOUSE_DOWN, WE_MOUSE_MOVE, WE_MOUSE_UP
  122. X    while G.windows:
  123. X        type, w, detail = event = stdwin.getevent()
  124. X        if w = G.cw.win:
  125. X            if type = WE_CLOSE:
  126. X                return
  127. X            G.cw.dispatch(event)
  128. X        else:
  129. X            if type = WE_DRAW:
  130. X                w.drawproc(w, detail)
  131. X            elif type in mouse_events:
  132. X                w.mouse(w, type, detail)
  133. X            elif type = WE_CLOSE:
  134. X                w.close(w)
  135. X                del w, event
  136. X            else:
  137. X                if G.debug: print type, w, detail
  138. X
  139. X# Control window -- to set gain and cancel play operations in progress
  140. X
  141. Xdef opencontrolwindow():
  142. X    cw = WindowParent().create('Jukebox', (0, 0))
  143. X    v = VSplit().create(cw)
  144. X    #
  145. X    gain = ComplexSlider().define(v)
  146. X    gain.setminvalmax(0, G.gain, 255)
  147. X    gain.settexts('  ', '  ')
  148. X    gain.sethook(gain_setval_hook)
  149. X    #
  150. X    stop = PushButton().definetext(v, 'Stop')
  151. X    stop.hook = stop_hook
  152. X    #
  153. X    cw.realize()
  154. X    return cw
  155. X
  156. Xdef gain_setval_hook(self):
  157. X    G.gain = self.val
  158. X    if G.busy: audio.setoutgain(G.gain)
  159. X
  160. Xdef stop_hook(self):
  161. X    if G.busy:
  162. X        audio.setoutgain(0)
  163. X        dummy = audio.stop_playing()
  164. X        G.busy = 0
  165. X
  166. X
  167. X# List windows -- to display list of files and subdirectories
  168. X
  169. Xdef openlistwindow(dirname):
  170. X    list = posix.listdir(dirname)
  171. X    list.sort()
  172. X    i = 0
  173. X    while i < len(list):
  174. X        if list[i] = '.' or list[i] = '..':
  175. X            del list[i]
  176. X        else:
  177. X            i = i+1
  178. X    for i in range(len(list)):
  179. X        name = list[i]
  180. X        if path.isdir(path.cat(dirname, name)):
  181. X            list[i] = list[i] + '/'
  182. X    width = maxwidth(list)
  183. X    width = width + stdwin.textwidth(' ')    # XXX X11 stdwin bug workaround
  184. X    height = len(list) * stdwin.lineheight()
  185. X    stdwin.setdefwinsize(width, min(height, 500))
  186. X    w = stdwin.open(dirname)
  187. X    stdwin.setdefwinsize(0, 0)
  188. X    w.setdocsize(width, height)
  189. X    w.drawproc = drawlistwindow
  190. X    w.mouse = mouselistwindow
  191. X    w.close = closelistwindow
  192. X    w.dirname = dirname
  193. X    w.list = list
  194. X    w.selected = -1
  195. X    return w
  196. X
  197. Xdef maxwidth(list):
  198. X    width = 1
  199. X    for name in list:
  200. X        w = stdwin.textwidth(name)
  201. X        if w > width: width = w
  202. X    return width
  203. X
  204. Xdef drawlistwindow(w, area):
  205. X    d = w.begindrawing()
  206. X    d.erase((0, 0), (1000, 10000))
  207. X    lh = d.lineheight()
  208. X    h, v = 0, 0
  209. X    for name in w.list:
  210. X        d.text((h, v), name)
  211. X        v = v + lh
  212. X    showselection(w, d)
  213. X
  214. Xdef hideselection(w, d):
  215. X    if w.selected >= 0:
  216. X        invertselection(w, d)
  217. X
  218. Xdef showselection(w, d):
  219. X    if w.selected >= 0:
  220. X        invertselection(w, d)
  221. X
  222. Xdef invertselection(w, d):
  223. X    lh = d.lineheight()
  224. X    h1, v1 = p1 = 0, w.selected*lh
  225. X    h2, v2 = p2 = 1000, v1 + lh
  226. X    d.invert(p1, p2)
  227. X
  228. Xdef mouselistwindow(w, type, detail):
  229. X    (h, v), clicks, button = detail[:3]
  230. X    d = w.begindrawing()
  231. X    lh = d.lineheight()
  232. X    if 0 <= v < lh*len(w.list):
  233. X        i = v / lh
  234. X    else:
  235. X        i = -1
  236. X    if w.selected <> i:
  237. X        hideselection(w, d)
  238. X        w.selected = i
  239. X        showselection(w, d)
  240. X    if type = WE_MOUSE_DOWN and clicks >= 2 and i >= 0:
  241. X        name = path.cat(w.dirname, w.list[i])
  242. X        if name[-1:] = '/':
  243. X            if clicks = 2:
  244. X                G.windows.append(openlistwindow(name[:-1]))
  245. X        else:
  246. X            playfile(name)
  247. X
  248. Xdef closelistwindow(w):
  249. X    remove(G.windows, w)
  250. X
  251. Xdef remove(list, item):
  252. X    for i in range(len(list)):
  253. X        if list[i] = item:
  254. X            del list[i]
  255. X            break
  256. X
  257. X
  258. X# Playing tools
  259. X
  260. Xcache = {}
  261. X
  262. Xdef clearcache():
  263. X    for x in cache.keys():
  264. X        try:
  265. X            sts = posix.system('rm -f ' + cache[x])
  266. X            if sts:
  267. X                print cmd
  268. X                print 'Exit status', sts
  269. X        except:
  270. X            print cmd
  271. X            print 'Exception?!'
  272. X        del cache[x]
  273. X
  274. Xdef playfile(name):
  275. X    if G.mode <> 'mac':
  276. X        tempname = name
  277. X    elif cache.has_key(name):
  278. X        tempname = cache[name]
  279. X    else:
  280. X        tempname = G.tempprefix + `rand.rand()`
  281. X        cmd = HOME_BIN_SGI + 'macsound2sgi'
  282. X        cmd = cmd + ' ' + commands.mkarg(name)
  283. X        cmd = cmd + ' >' + tempname
  284. X        if G.debug: print cmd
  285. X        sts = posix.system(cmd)
  286. X        if sts:
  287. X            print cmd
  288. X            print 'Exit status', sts
  289. X            stdwin.fleep()
  290. X            return
  291. X        cache[name] = tempname
  292. X    fp = open(tempname, 'r')
  293. X    try:
  294. X        hdr = sunaudio.gethdr(fp)
  295. X    except sunaudio.error, msg:
  296. X        hdr = ()
  297. X    if hdr:
  298. X        data_size = hdr[0]
  299. X        data = fp.read(data_size)
  300. X        # XXX this doesn't work yet, need to convert from uLAW!!!
  301. X        del fp
  302. X    else:
  303. X        del fp
  304. X        data = readfile(tempname)
  305. X    if G.debug: print len(data), 'bytes read from', tempname
  306. X    if G.busy:
  307. X        G.busy = 0
  308. X        dummy = audio.stop_playing()
  309. X    #
  310. X    # Completely reset the audio device
  311. X    audio.setrate(G.rate)
  312. X    audio.setduration(0)
  313. X    audio.setoutgain(G.gain)
  314. X    #
  315. X    if G.synchronous:
  316. X        audio.write(data)
  317. X        audio.setoutgain(0)
  318. X    else:
  319. X        try:
  320. X            audio.start_playing(data)
  321. X            G.busy = 1
  322. X        except:
  323. X            stdwin.fleep()
  324. X    del data
  325. X
  326. Xdef readfile(filename):
  327. X    return readfp(open(filename, 'r'))
  328. X
  329. Xdef readfp(fp):
  330. X    data = ''
  331. X    while 1:
  332. X        buf = fp.read(102400) # Reads most samples in one fell swoop
  333. X        if not buf:
  334. X            return data
  335. X        data = data + buf
  336. X
  337. Xmain()
  338. EOF
  339. chmod +x 'demo/sgi/audio_stdwin/jukebox.py'
  340. fi
  341. if test -s 'demo/sgi/gl_panel/twoview/twoview.py'
  342. then echo '*** I will not over-write existing file demo/sgi/gl_panel/twoview/twoview.py'
  343. else
  344. echo 'x - demo/sgi/gl_panel/twoview/twoview.py'
  345. sed 's/^X//' > 'demo/sgi/gl_panel/twoview/twoview.py' << 'EOF'
  346. X#! /ufs/guido/bin/sgi/python
  347. X
  348. X# A demo of GL's viewing transformations, showing two views on one scene.
  349. X# Requires the NASA AMES Panel Library.  Requires Z buffer.
  350. X
  351. Xfrom gl import *
  352. Xfrom GL import *
  353. Ximport panel
  354. Xfrom math import sin, cos, pi
  355. X
  356. Xinf = 1000000.0
  357. Xfar = 1000.0
  358. Xnear = 100.0
  359. X
  360. Xdef main():
  361. X    foreground()
  362. X    #
  363. X    keepaspect(1, 1)
  364. X    prefposition(10, 610, 10, 610)
  365. X    obswid = winopen('Observer View')
  366. X    doublebuffer()
  367. X    RGBmode()
  368. X    gconfig()
  369. X    #
  370. X    keepaspect(1, 1)
  371. X    prefposition(10, 310, 650, 950)
  372. X    topwid = winopen('Top View')
  373. X    doublebuffer()
  374. X    RGBmode()
  375. X    gconfig()
  376. X    #
  377. X    panels = panel.defpanellist('observer.s')
  378. X    panels = panels + panel.defpanellist('camera.s')
  379. X    panels = panels + panel.defpanellist('topview.s')
  380. X    #
  381. X    p = panels[0]
  382. X    q = panels[1]
  383. X    r = panels[2]
  384. X    #
  385. X    p.farclip = q.farclip
  386. X    p.nearclip = q.nearclip
  387. X    p.zoom = q.zoom
  388. X    p.quitbutton = q.quitbutton
  389. X    #
  390. X    p.xpos = r.xpos
  391. X    p.zpos = r.zpos
  392. X    p.direction = r.direction
  393. X    #
  394. X    p.direction.winds = 1.0        # allow full rotation
  395. X    #
  396. X    def quit(act):
  397. X        import sys
  398. X        sys.exit(0)
  399. X    p.quitbutton.downfunc = quit
  400. X    #
  401. X    p.left.back = p
  402. X    p.fast_left.back = p
  403. X    p.right.back = p
  404. X    p.fast_right.back = p
  405. X    p.forward.back = p
  406. X    p.fast_forward.back = p
  407. X    p.reverse.back = p
  408. X    p.fast_reverse.back = p
  409. X    p.up.back = p
  410. X    p.down.back = p
  411. X    #
  412. X    p.left.activefunc = left
  413. X    p.fast_left.activefunc = fast_left
  414. X    p.right.activefunc = right
  415. X    p.fast_right.activefunc = fast_right
  416. X    p.forward.activefunc = forward
  417. X    p.fast_forward.activefunc = fast_forward
  418. X    p.reverse.activefunc = reverse
  419. X    p.fast_reverse.activefunc = fast_reverse
  420. X    p.up.activefunc = up
  421. X    p.down.activefunc = down
  422. X    #
  423. X    makeobjects()
  424. X    #
  425. X    drawall(p, obswid, topwid)
  426. X    panel.needredraw()
  427. X    while 1:
  428. X        act = panel.dopanel()
  429. X        if panel.userredraw() or act:
  430. X            drawall(p, obswid, topwid)
  431. X
  432. Xdef left(a):
  433. X    doturn(a.back, 0.01)
  434. X
  435. Xdef fast_left(a):
  436. X    doturn(a.back, 0.1)
  437. X
  438. Xdef right(a):
  439. X    doturn(a.back, -0.01)
  440. X
  441. Xdef fast_right(a):
  442. X    doturn(a.back, -0.1)
  443. X
  444. Xdef doturn(p, angle):
  445. X    alpha = lookangle(p) + angle
  446. X    # Reverse the following assignment:
  447. X    #    alpha = pi*1.5 - p.direction.val*2.0*pi
  448. X    val = (pi*1.5 - alpha) / 2.0 / pi
  449. X    while val < 0.0: val = val + 1.0
  450. X    while val > 1.0: val = val - 1.0
  451. X    p.direction.val = val
  452. X    p.direction.fixact()
  453. X
  454. Xdef forward(a):
  455. X    dostep(a.back, 1.0)
  456. X
  457. Xdef fast_forward(a):
  458. X    dostep(a.back, 10.0)
  459. X
  460. Xdef reverse(a):
  461. X    dostep(a.back, -1.0)
  462. X
  463. Xdef fast_reverse(a):
  464. X    dostep(a.back, -10.0)
  465. X
  466. Xdef dostep(p, step):
  467. X    x, y, z = observerpos(p)
  468. X    alpha = lookangle(p)
  469. X    x = x + step*cos(alpha)
  470. X    z = z - step*sin(alpha)
  471. X    # Reverse the following assignments:
  472. X    #    x = 2.0 * p.xpos.val * near - near
  473. X    #    z = near - 2.0 * p.zpos.val * near
  474. X    p.xpos.val = (x + near) / 2.0 / near
  475. X    p.zpos.val = - (z - near) / 2.0 / near
  476. X    p.xpos.fixact()
  477. X    p.zpos.fixact()
  478. X
  479. Xdef up(a):
  480. X    doup(a.back, 0.2)
  481. X
  482. Xdef down(a):
  483. X    doup(a.back, -0.2)
  484. X
  485. Xdef doup(p, step):
  486. X    x, y, z = observerpos(p)
  487. X    y = y + step
  488. X    # Reverse:
  489. X    #    y = p.ypos.val * near
  490. X    p.ypos.val = y/near
  491. X    p.ypos.fixact()
  492. X
  493. Xdef drawall(p, obswid, topwid):
  494. X    #
  495. X    winset(obswid)
  496. X    obsview(p)
  497. X    drawscene()
  498. X    swapbuffers()
  499. X    #
  500. X    winset(topwid)
  501. X    topview(p)
  502. X    drawscene()
  503. X    drawobserver(p)
  504. X    swapbuffers()
  505. X
  506. Xdef drawobserver(p):
  507. X    x, y, z = observerpos(p)
  508. X    alpha = lookangle(p)
  509. X    fov = 2.0 + 1798.0 * p.zoom.val
  510. X    beta = fov*pi/3600.0        # Half fov, expressed in radians
  511. X    #
  512. X    c3i(0, 255, 0)
  513. X    #
  514. X    move(x, y, z)
  515. X    x1 = x + inf*cos(alpha+beta)
  516. X    y1 = y
  517. X    z1 = z - inf*sin(alpha+beta)
  518. X    draw(x1, y1, z1)
  519. X    #
  520. X    move(x, y, z)
  521. X    x1 = x + inf*cos(alpha-beta)
  522. X    y1 = y
  523. X    z1 = z - inf*sin(alpha-beta)
  524. X    draw(x1, y1, z1)
  525. X
  526. Xdef observerlookat(p):
  527. X    x, y, z = observerpos(p)
  528. X    alpha = lookangle(p)
  529. X    return x, y, z, x+near*cos(alpha), y, z-near*sin(alpha), 0
  530. X
  531. Xdef observerpos(p):
  532. X    x = 2.0 * p.xpos.val * near - near
  533. X    y = p.ypos.val * near
  534. X    z = near - 2.0 * p.zpos.val * near
  535. X    return x, y, z
  536. X
  537. Xdef lookangle(p):
  538. X    return pi*1.5 - p.direction.val*2.0*pi
  539. X
  540. Xidmat = 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1
  541. X
  542. Xdef topview(p):
  543. X    mmode(MVIEWING)
  544. X    ortho(-far, far, -far, far, far, -far)
  545. X    loadmatrix(idmat)
  546. X    rotate(900, 'x')
  547. X
  548. Xdef obsview(p):
  549. X    fov = 2.0 + 1798.0 * p.zoom.val
  550. X    nearclip = p.nearclip.val * 10.0
  551. X    farclip = p.farclip.val * 10.0*far
  552. X    aspectratio = 1.0
  553. X    mmode(MVIEWING)
  554. X    perspective(int(fov), aspectratio, nearclip, farclip)
  555. X    loadmatrix(idmat)
  556. X    lookat(observerlookat(p))
  557. X
  558. Xdef drawscene():
  559. X    #
  560. X    # clear window
  561. X    #
  562. X    c3i(0, 0, 0)
  563. X    clear()
  564. X    #
  565. X    # turn on z buffering and clear it
  566. X    #
  567. X    zbuffer(TRUE)
  568. X    zclear()
  569. X    #
  570. X    # dark blue sky (depending on your gamma value!)
  571. X    #
  572. X    c3i(0, 0, 150)
  573. X    callobj(41)
  574. X    #
  575. X    # bright red near and far units circle
  576. X    # (use rotate since circ() always draws in x-y plane)
  577. X    #
  578. X    c3i(255, 0, 0)
  579. X    pushmatrix()
  580. X    rotate(900, 'x')
  581. X    circ(0.0, 0.0, near)
  582. X    circ(0.0, 0.0, far)
  583. X    popmatrix()
  584. X    #
  585. X    # bright white striping
  586. X    #
  587. X    c3i(255, 255, 200)
  588. X    callobj(42)
  589. X    #
  590. X    # building (does its own colors)
  591. X    #
  592. X    building()
  593. X    #
  594. X    # some other objects
  595. X    #
  596. X    dice()
  597. X
  598. Xdef makeobjects():
  599. X    #
  600. X    # sky object
  601. X    #
  602. X    makeobj(41)
  603. X    pmv(-inf, 0.0, -far)
  604. X    pdr(inf, 0.0, -far)
  605. X    pdr(inf, inf, -far)
  606. X    pdr(-inf, inf, -far)
  607. X    pclos()
  608. X    closeobj()
  609. X    #
  610. X    # road stripes object
  611. X    #
  612. X    makeobj(42)
  613. X    stripes()
  614. X    closeobj()
  615. X    #
  616. X    # lighting model definitions
  617. X    #
  618. X    deflight()
  619. X
  620. Xdef stripes():
  621. X    #
  622. X    # left line
  623. X    #
  624. X    botrect(-11, -10, far, -far)
  625. X    #
  626. X    # right line
  627. X    #
  628. X    botrect(10, 11, far, -far)
  629. X    #
  630. X    # center lines
  631. X    #
  632. X    z = far
  633. X    while z > -far:
  634. X        botrect(-0.5, 0.5, z, z - 4.0)
  635. X        z = z - 10.0
  636. X
  637. Xdef dice():
  638. X    from block import block
  639. X    uselight()
  640. X    pushmatrix()
  641. X    translate(0.0, 1.0, -20.0)
  642. X    rotate(200, 'y')
  643. X    block(1, 0, 0, 0, 0, 0)
  644. X    translate(1.0, 0.0, 3.0)
  645. X    rotate(500, 'y')
  646. X    block(2, 0, 0, 0, 0, 0)
  647. X    popmatrix()
  648. X
  649. Xdef deflight():
  650. X    # Material for first die (red)
  651. X    lmdef(DEFMATERIAL, 1, (DIFFUSE, 1.0, 0.0, 0.0))
  652. X    # Material for second die (green)
  653. X    lmdef(DEFMATERIAL, 2, (DIFFUSE, 0.0, 1.0, 0.0))
  654. X    # First light source (default: white, from front)
  655. X    lmdef(DEFLIGHT, 1, ())
  656. X    # Second light source (red, from back)
  657. X    lmdef(DEFLIGHT, 2, (POSITION, 0.0, 1.0, -1.0, 0.0))
  658. X    lmdef(DEFLIGHT, 2, (LCOLOR, 1.0, 0.0, 0.0))
  659. X    # Lighting model
  660. X    lmdef(DEFLMODEL, 1, (AMBIENT, 0.0, 0.0, 1.0))
  661. X
  662. Xdef uselight():
  663. X    lmbind(LIGHT0, 1)
  664. X    lmbind(LIGHT1, 2)
  665. X    lmbind(LMODEL, 1)
  666. X    # (materials are bound later)
  667. X
  668. Xdef building():
  669. X    #
  670. X    c3i(0, 255, 255)
  671. X    #
  672. X    # house bounding coordinates
  673. X    #
  674. X    x1 = 20.0
  675. X    x1a = 25.0
  676. X    x2 = 30.0
  677. X    y1 = 0.0
  678. X    y2 = 15.0
  679. X    y2a = 20.0
  680. X    z1 = -40.0
  681. X    z2 = -55.0
  682. X    #
  683. X    # door y and z coordinates
  684. X    #
  685. X    dy1 = 0.0
  686. X    dy2 = 4.0
  687. X    dz1 = -45.0
  688. X    dz2 = -47.0
  689. X    #
  690. X    # front side (seen from origin)
  691. X    #
  692. X    A1 = (x1, y1, z1)
  693. X    B1 = (x2, y1, z1)
  694. X    C1 = (x2, y2, z1)
  695. X    D1 = (x1a, y2a, z1)
  696. X    E1 = (x1, y2, z1)
  697. X    #
  698. X    # back size
  699. X    #
  700. X    A2 = (x1, y1, z2)
  701. X    B2 = (x2, y1, z2)
  702. X    C2 = (x2, y2, z2)
  703. X    D2 = (x1a, y2a, z2)
  704. X    E2 = (x1, y2, z2)
  705. X    #
  706. X    # door in the left side
  707. X    #
  708. X    P = x1, dy1, dz2
  709. X    Q = x1, dy2, dz2
  710. X    R = x1, dy2, dz1
  711. X    S = x1, dy1, dz1
  712. X    #
  713. X    # draw it
  714. X    #
  715. X    concave(TRUE)
  716. X    c3i(255, 0, 0)
  717. X    face(A1, B1, C1, D1, E1)
  718. X    c3i(127, 127, 0)
  719. X    face(A1, E1, E2, A2, P, Q, R, S)
  720. X    c3i(0, 255, 0)
  721. X    face(E1, D1, D2, E2)
  722. X    c3i(0, 127, 127)
  723. X    face(D1, C1, C2, D2)
  724. X    c3i(0, 0, 255)
  725. X    face(C1, B1, B2, C2)
  726. X    c3i(127, 0, 127)
  727. X    face(E2, D2, C2, B2, A2)
  728. X    concave(FALSE)
  729. X
  730. Xdef face(points):
  731. X    bgnpolygon()
  732. X    varray(points)
  733. X    endpolygon()
  734. X
  735. X# draw a rectangle at y=0.0
  736. X#
  737. Xdef botrect(x1, x2, z1, z2):
  738. X    polf(x1, 0.0, z1, x2, 0.0, z1, x2, 0.0, z2, x1, 0.0, z2)
  739. X
  740. Xmain()
  741. EOF
  742. chmod +x 'demo/sgi/gl_panel/twoview/twoview.py'
  743. fi
  744. if test -s 'lib/DEVICE.py'
  745. then echo '*** I will not over-write existing file lib/DEVICE.py'
  746. else
  747. echo 'x - lib/DEVICE.py'
  748. sed 's/^X//' > 'lib/DEVICE.py' << 'EOF'
  749. X#/**************************************************************************
  750. X# *                                      *
  751. X# *          Copyright (C) 1984, Silicon Graphics, Inc.          *
  752. X# *                                      *
  753. X# *  These coded instructions, statements, and computer programs  contain  *
  754. X# *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
  755. X# *  are protected by Federal copyright law.  They  may  not be disclosed  *
  756. X# *  to  third  parties  or copied or duplicated in any form, in whole or  *
  757. X# *  in part, without the prior written consent of Silicon Graphics, Inc.  *
  758. X# *                                      *
  759. X# **************************************************************************/
  760. X#/* file with device definitions (see /usr/include/device.h) */
  761. X
  762. XNULLDEV =  0
  763. XBUTOFFSET = 1
  764. XVALOFFSET = 256
  765. XTIMOFFSET = 515
  766. XXKBDOFFSET = 143
  767. XINOFFSET = 1024
  768. XOUTOFFSET = 1033
  769. XBUTCOUNT = 190
  770. XVALCOUNT = 27
  771. XTIMCOUNT = 4
  772. XXKBDCOUNT = 28
  773. XINCOUNT =  8
  774. XOUTCOUNT = 8
  775. X#
  776. X#
  777. X#
  778. X#
  779. XBUT0 =   1    
  780. XBUT1 =   2    
  781. XBUT2 =   3    
  782. XBUT3 =   4    
  783. XBUT4 =   5    
  784. XBUT5 =   6    
  785. XBUT6 =   7    
  786. XBUT7 =   8    
  787. XBUT8 =   9    
  788. XBUT9 =  10    
  789. XBUT10 =  11    
  790. XBUT11 =  12    
  791. XBUT12 =  13    
  792. XBUT13 =  14    
  793. XBUT14 =  15    
  794. XBUT15 =  16    
  795. XBUT16 =  17    
  796. XBUT17 =  18    
  797. XBUT18 =  19    
  798. XBUT19 =  20    
  799. XBUT20 =  21    
  800. XBUT21 =  22    
  801. XBUT22 =  23    
  802. XBUT23 =  24    
  803. XBUT24 =  25    
  804. XBUT25 =  26    
  805. XBUT26 =  27    
  806. XBUT27 =  28    
  807. XBUT28 =  29    
  808. XBUT29 =  30    
  809. XBUT30 =  31    
  810. XBUT31 =  32    
  811. XBUT32 =  33    
  812. XBUT33 =  34    
  813. XBUT34 =  35    
  814. XBUT35 =  36    
  815. XBUT36 =  37    
  816. XBUT37 =  38    
  817. XBUT38 =  39    
  818. XBUT39 =  40    
  819. XBUT40 =  41    
  820. XBUT41 =  42    
  821. XBUT42 =  43    
  822. XBUT43 =  44    
  823. XBUT44 =  45    
  824. XBUT45 =  46    
  825. XBUT46 =  47    
  826. XBUT47 =  48    
  827. XBUT48 =  49    
  828. XBUT49 =  50    
  829. XBUT50 =  51    
  830. XBUT51 =  52    
  831. XBUT52 =  53    
  832. XBUT53 =  54    
  833. XBUT54 =  55    
  834. XBUT55 =  56    
  835. XBUT56 =  57    
  836. XBUT57 =  58    
  837. XBUT58 =  59    
  838. XBUT59 =  60    
  839. XBUT60 =  61    
  840. XBUT61 =  62    
  841. XBUT62 =  63    
  842. XBUT63 =  64    
  843. XBUT64 =  65    
  844. XBUT65 =  66    
  845. XBUT66 =  67    
  846. XBUT67 =  68    
  847. XBUT68 =  69    
  848. XBUT69 =  70    
  849. XBUT70 =  71    
  850. XBUT71 =  72    
  851. XBUT72 =  73    
  852. XBUT73 =  74    
  853. XBUT74 =  75    
  854. XBUT75 =  76    
  855. XBUT76 =  77    
  856. XBUT77 =  78    
  857. XBUT78 =  79    
  858. XBUT79 =  80    
  859. XBUT80 =  81    
  860. XBUT81 =  82    
  861. XBUT82 =  83    
  862. XMAXKBDBUT = 83 
  863. XBUT100 =  101    
  864. XBUT101 =  102    
  865. XBUT102 =  103    
  866. XBUT110 =  111    
  867. XBUT111 =  112    
  868. XBUT112 =  113    
  869. XBUT113 =  114    
  870. XBUT114 =  115    
  871. XBUT115 =  116    
  872. XBUT116 =  117    
  873. XBUT117 =  118    
  874. XBUT118 =  119    
  875. XBUT119 =  120    
  876. XBUT120 =  121    
  877. XBUT121 =  122    
  878. XBUT122 =  123    
  879. XBUT123 =  124    
  880. XBUT124 =  125    
  881. XBUT125 =  126    
  882. XBUT126 =  127    
  883. XBUT127 =  128    
  884. XBUT128 =  129    
  885. XBUT129 =  130    
  886. XBUT130 =  131    
  887. XBUT131 =  132    
  888. XBUT132 =  133    
  889. XBUT133 =  134    
  890. XBUT134 =  135    
  891. XBUT135 =  136    
  892. XBUT136 =  137    
  893. XBUT137 =  138    
  894. XBUT138 =  139    
  895. XBUT139 =  140    
  896. XBUT140 =  141    
  897. XBUT141 =  142    
  898. XBUT142 =  143    
  899. XBUT143 =  144    
  900. XBUT144 =  145    
  901. XBUT145 =  146    
  902. XBUT146 =  147    
  903. XBUT147 =  148    
  904. XBUT148 =  149    
  905. XBUT149 =  150    
  906. XBUT150 =  151    
  907. XBUT151 =  152    
  908. XBUT152 =  153    
  909. XBUT153 =  154    
  910. XBUT154 =  155    
  911. XBUT155 =  156     
  912. XBUT156 =  157    
  913. XBUT157 =  158    
  914. XBUT158 =  159    
  915. XBUT159 =  160    
  916. XBUT160 =  161    
  917. XBUT161 =  162    
  918. XBUT162 =  163    
  919. XBUT163 =  164    
  920. XBUT164 =  165    
  921. XBUT165 =  166    
  922. XBUT166 =  167    
  923. XBUT167 =  168    
  924. XBUT168 =  169    
  925. XBUT181 =  182    
  926. XBUT182 =  183    
  927. XBUT183 =  184    
  928. XBUT184 =  185    
  929. XBUT185 =  186    
  930. XBUT186 =  187    
  931. XBUT187 =  188    
  932. XBUT188 =  189    
  933. XBUT189 =  190    
  934. XMOUSE1 =  101    
  935. XMOUSE2 =  102    
  936. XMOUSE3 =  103    
  937. XLEFTMOUSE = 103 
  938. XMIDDLEMOUSE = 102 
  939. XRIGHTMOUSE = 101 
  940. XLPENBUT =  104    
  941. XBPAD0 =  105    
  942. XBPAD1 =  106    
  943. XBPAD2 =  107    
  944. XBPAD3 =  108    
  945. XLPENVALID = 109 
  946. XSWBASE =  111    
  947. XSW0 =  111    
  948. XSW1 =  112    
  949. XSW2 =  113    
  950. XSW3 =  114    
  951. XSW4 =  115    
  952. XSW5 =  116    
  953. XSW6 =  117    
  954. XSW7 =  118    
  955. XSW8 =  119    
  956. XSW9 =  120    
  957. XSW10 =  121    
  958. XSW11 =  122    
  959. XSW12 =  123    
  960. XSW13 =  124    
  961. XSW14 =  125    
  962. XSW15 =  126    
  963. XSW16 =  127    
  964. XSW17 =  128    
  965. XSW18 =  129    
  966. XSW19 =  130    
  967. XSW20 =  131    
  968. XSW21 =  132    
  969. XSW22 =  133    
  970. XSW23 =  134    
  971. XSW24 =  135    
  972. XSW25 =  136    
  973. XSW26 =  137    
  974. XSW27 =  138    
  975. XSW28 =  139    
  976. XSW29 =  140    
  977. XSW30 =  141    
  978. XSW31 =  142    
  979. XSBBASE =  182    
  980. XSBPICK =  182    
  981. XSBBUT1 =  183    
  982. XSBBUT2 =  184    
  983. XSBBUT3 =  185    
  984. XSBBUT4 =  186    
  985. XSBBUT5 =  187    
  986. XSBBUT6 =  188    
  987. XSBBUT7 =  189    
  988. XSBBUT8 =  190    
  989. XAKEY =  11    
  990. XBKEY =  36    
  991. XCKEY =  28    
  992. XDKEY =  18    
  993. XEKEY =  17    
  994. XFKEY =  19    
  995. XGKEY =  26    
  996. XHKEY =  27    
  997. XIKEY =  40    
  998. XJKEY =  34    
  999. XKKEY =  35    
  1000. XLKEY =  42    
  1001. XMKEY =  44    
  1002. XNKEY =  37    
  1003. XOKEY =  41    
  1004. XPKEY =  48    
  1005. XQKEY =  10    
  1006. XRKEY =  24    
  1007. XSKEY =  12    
  1008. XTKEY =  25    
  1009. XUKEY =  33    
  1010. XVKEY =  29    
  1011. XWKEY =  16    
  1012. XXKEY =  21    
  1013. XYKEY =  32    
  1014. XZKEY =  20    
  1015. XZEROKEY =  46    
  1016. XONEKEY =  8    
  1017. XTWOKEY =  14    
  1018. XTHREEKEY = 15 
  1019. XFOURKEY =  22    
  1020. XFIVEKEY =  23    
  1021. XSIXKEY =  30    
  1022. XSEVENKEY = 31 
  1023. XEIGHTKEY = 38 
  1024. XNINEKEY =  39    
  1025. XBREAKKEY = 1 
  1026. XSETUPKEY = 2 
  1027. XCTRLKEY =  3    
  1028. XLEFTCTRLKEY = CTRLKEY 
  1029. XCAPSLOCKKEY = 4 
  1030. XRIGHTSHIFTKEY = 5 
  1031. XLEFTSHIFTKEY = 6 
  1032. XNOSCRLKEY = 13 
  1033. XESCKEY =  7    
  1034. XTABKEY =  9    
  1035. XRETKEY =  51    
  1036. XSPACEKEY = 83 
  1037. XLINEFEEDKEY = 60 
  1038. XBACKSPACEKEY = 61 
  1039. XDELKEY =  62    
  1040. XSEMICOLONKEY = 43 
  1041. XPERIODKEY = 52 
  1042. XCOMMAKEY = 45 
  1043. XQUOTEKEY = 50 
  1044. XACCENTGRAVEKEY = 55 
  1045. XMINUSKEY = 47 
  1046. XVIRGULEKEY = 53 
  1047. XBACKSLASHKEY = 57 
  1048. XEQUALKEY = 54 
  1049. XLEFTBRACKETKEY = 49 
  1050. XRIGHTBRACKETKEY = 56 
  1051. XLEFTARROWKEY = 73 
  1052. XDOWNARROWKEY = 74 
  1053. XRIGHTARROWKEY = 80 
  1054. XUPARROWKEY = 81 
  1055. XPAD0 =  59    
  1056. XPAD1 =  58    
  1057. XPAD2 =  64    
  1058. XPAD3 =  65    
  1059. XPAD4 =  63    
  1060. XPAD5 =  69    
  1061. XPAD6 =  70    
  1062. XPAD7 =  67    
  1063. XPAD8 =  68    
  1064. XPAD9 =  75    
  1065. XPADPF1 =  72    
  1066. XPADPF2 =  71    
  1067. XPADPF3 =  79    
  1068. XPADPF4 =  78    
  1069. XPADPERIOD = 66 
  1070. XPADMINUS = 76 
  1071. XPADCOMMA = 77 
  1072. XPADENTER = 82 
  1073. XLEFTALTKEY  = 143
  1074. XRIGHTALTKEY  = 144
  1075. XRIGHTCTRLKEY  = 145
  1076. XF1KEY  =  146
  1077. XF2KEY  =  147
  1078. XF3KEY  =  148
  1079. XF4KEY  =  149
  1080. XF5KEY  =  150
  1081. XF6KEY  =  151
  1082. XF7KEY  =  152
  1083. XF8KEY  =  153
  1084. XF9KEY  =  154
  1085. XF10KEY =  155
  1086. XF11KEY =  156
  1087. XF12KEY =  157
  1088. XPRINTSCREENKEY = 158
  1089. XSCROLLLOCKKEY = 159
  1090. XPAUSEKEY = 160
  1091. XINSERTKEY = 161
  1092. XHOMEKEY  = 162
  1093. XPAGEUPKEY  = 163
  1094. XENDKEY =  164
  1095. XPAGEDOWNKEY = 165
  1096. XNUMLOCKKEY = 166
  1097. XPADVIRGULEKEY  = 167
  1098. XPADASTERKEY  = 168
  1099. XPADPLUSKEY  = 169
  1100. XSGIRESERVED = 256 
  1101. XDIAL0 =  257    
  1102. XDIAL1 =  258    
  1103. XDIAL2 =  259    
  1104. XDIAL3 =  260    
  1105. XDIAL4 =  261    
  1106. XDIAL5 =  262    
  1107. XDIAL6 =  263    
  1108. XDIAL7 =  264    
  1109. XDIAL8 =  265    
  1110. XMOUSEX =  266    
  1111. XMOUSEY =  267    
  1112. XLPENX =  268    
  1113. XLPENY =  269    
  1114. XBPADX =  270    
  1115. XBPADY =  271    
  1116. XCURSORX =  272    
  1117. XCURSORY =  273    
  1118. XGHOSTX =  274    
  1119. XGHOSTY =  275    
  1120. XSBTX =  276    
  1121. XSBTY  =  277    
  1122. XSBTZ =  278    
  1123. XSBRX =   279    
  1124. XSBRY =  280    
  1125. XSBRZ  =  281    
  1126. XSBPERIOD = 282 
  1127. XTIMER0 =  515    
  1128. XTIMER1 =  516    
  1129. XTIMER2 =  517    
  1130. XTIMER3 =  518    
  1131. XKEYBD =  513    
  1132. XRAWKEYBD = 514 
  1133. XVALMARK =  523    
  1134. XGERROR =  524    
  1135. XREDRAW =  528    
  1136. XWMSEND =  529    
  1137. XWMREPLY =  530    
  1138. XWMGFCLOSE = 531 
  1139. XWMTXCLOSE = 532 
  1140. XMODECHANGE = 533 
  1141. XINPUTCHANGE = 534 
  1142. XQFULL =  535    
  1143. XPIECECHANGE = 536 
  1144. XWINCLOSE = 537 
  1145. XQREADERROR = 538 
  1146. XWINFREEZE = 539 
  1147. XWINTHAW =  540    
  1148. XREDRAWICONIC = 541 
  1149. XWINQUIT =  542    
  1150. XDEPTHCHANGE = 543 
  1151. XKEYBDFNAMES = 544 
  1152. XKEYBDFSTRINGS = 545 
  1153. XWINSHUT =  546    
  1154. XINPUT0 =  1024    
  1155. XINPUT1 =  1025
  1156. XINPUT2 =  1026
  1157. XINPUT3 =  1027
  1158. XINPUT4 =  1028
  1159. XINPUT5 =  1029
  1160. XINPUT6 =  1030
  1161. XINPUT7 =  1032
  1162. XOUTPUT0 =  1033    
  1163. XOUTPUT1 =  1034
  1164. XOUTPUT2 =  1035
  1165. XOUTPUT3 =  1036
  1166. XOUTPUT4 =  1037
  1167. XOUTPUT5 =  1038
  1168. XOUTPUT6 =  1039
  1169. XOUTPUT7 =  1040
  1170. XMAXSGIDEVICE = 20000
  1171. XMENUBUTTON = RIGHTMOUSE 
  1172. EOF
  1173. fi
  1174. if test -s 'lib/GL.py'
  1175. then echo '*** I will not over-write existing file lib/GL.py'
  1176. else
  1177. echo 'x - lib/GL.py'
  1178. sed 's/^X//' > 'lib/GL.py' << 'EOF'
  1179. X# Constants defined in <gl.h>
  1180. X
  1181. X#**************************************************************************
  1182. X#*                                      *
  1183. X#*          Copyright (C) 1984, Silicon Graphics, Inc.          *
  1184. X#*                                      *
  1185. X#*  These coded instructions, statements, and computer programs  contain  *
  1186. X#*  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
  1187. X#*  are protected by Federal copyright law.  They  may  not be disclosed  *
  1188. X#*  to  third  parties  or copied or duplicated in any form, in whole or  *
  1189. X#*  in part, without the prior written consent of Silicon Graphics, Inc.  *
  1190. X#*                                      *
  1191. X#**************************************************************************
  1192. X
  1193. X# Graphics Libary constants
  1194. X
  1195. X# Booleans
  1196. XTRUE = 1
  1197. XFALSE = 0
  1198. X
  1199. X# maximum X and Y screen coordinates 
  1200. XXMAXSCREEN = 1279
  1201. XYMAXSCREEN = 1023
  1202. XXMAXMEDIUM = 1023        # max for medium res monitor 
  1203. XYMAXMEDIUM = 767
  1204. XXMAX170 = 645        # max for RS-170 
  1205. XYMAX170 = 484
  1206. XXMAXPAL = 779        # max for PAL 
  1207. XYMAXPAL = 574
  1208. X
  1209. X# various hardware/software limits 
  1210. XATTRIBSTACKDEPTH = 10
  1211. XVPSTACKDEPTH = 8
  1212. XMATRIXSTACKDEPTH = 32
  1213. XNAMESTACKDEPTH = 1025
  1214. XSTARTTAG = -2
  1215. XENDTAG = -3
  1216. XCPOSX_INVALID = -(2*XMAXSCREEN)
  1217. X
  1218. X# names for colors in color map loaded by greset 
  1219. XBLACK = 0
  1220. XRED = 1
  1221. XGREEN = 2
  1222. XYELLOW = 3
  1223. XBLUE = 4
  1224. XMAGENTA = 5
  1225. XCYAN = 6
  1226. XWHITE = 7
  1227. X
  1228. X# popup colors 
  1229. XPUP_CLEAR = 0
  1230. XPUP_COLOR = 1
  1231. XPUP_BLACK = 2
  1232. XPUP_WHITE = 3
  1233. X
  1234. X# defines for drawmode 
  1235. XNORMALDRAW = 0
  1236. XPUPDRAW = 1
  1237. XOVERDRAW = 2
  1238. XUNDERDRAW = 3
  1239. XCURSORDRAW = 4
  1240. X
  1241. X# defines for defpattern 
  1242. XPATTERN_16 = 16
  1243. XPATTERN_32 = 32
  1244. XPATTERN_64 = 64
  1245. X
  1246. XPATTERN_16_SIZE = 16
  1247. XPATTERN_32_SIZE = 64
  1248. XPATTERN_64_SIZE = 256
  1249. X
  1250. X# defines for readsource 
  1251. XSRC_AUTO = 0
  1252. XSRC_FRONT = 1
  1253. XSRC_BACK = 2
  1254. XSRC_ZBUFFER = 3
  1255. XSRC_PUP = 4
  1256. XSRC_OVER = 5
  1257. XSRC_UNDER = 6
  1258. XSRC_FRAMEGRABBER = 7
  1259. X
  1260. X# defines for blendfunction 
  1261. XBF_ZERO = 0
  1262. XBF_ONE = 1
  1263. XBF_DC = 2
  1264. XBF_SC = 2
  1265. XBF_MDC = 3
  1266. XBF_MSC = 3
  1267. XBF_SA = 4
  1268. XBF_MSA = 5
  1269. XBF_DA = 6
  1270. XBF_MDA = 7
  1271. X
  1272. X# defines for zfunction 
  1273. XZF_NEVER = 0
  1274. XZF_LESS = 1
  1275. XZF_EQUAL = 2
  1276. XZF_LEQUAL = 3
  1277. XZF_GREATER = 4
  1278. XZF_NOTEQUAL = 5
  1279. XZF_GEQUAL = 6
  1280. XZF_ALWAYS = 7
  1281. X
  1282. X# defines for zsource 
  1283. XZSRC_DEPTH = 0
  1284. XZSRC_COLOR = 1
  1285. X
  1286. X# defines for pntsmooth 
  1287. XSMP_OFF = 0
  1288. XSMP_ON = 1
  1289. X
  1290. X# defines for linesmooth 
  1291. XSML_OFF = 0
  1292. XSML_ON = 1
  1293. X
  1294. X# defines for setpup 
  1295. XPUP_NONE = 0
  1296. XPUP_GREY = 1
  1297. X
  1298. X# defines for glcompat 
  1299. XGLC_OLDPOLYGON = 0
  1300. XGLC_ZRANGEMAP = 1
  1301. X
  1302. X# defines for curstype 
  1303. XC16X1 = 0
  1304. XC16X2 = 1
  1305. XC32X1 = 2
  1306. XC32X2 = 3
  1307. XCCROSS = 4
  1308. X
  1309. X# defines for shademodel 
  1310. XFLAT = 0
  1311. XGOURAUD = 1
  1312. X
  1313. X# defines for logicop 
  1314. X### LO_ZERO = 0x0
  1315. X### LO_AND = 0x1
  1316. X### LO_ANDR = 0x2
  1317. X### LO_SRC = 0x3
  1318. X### LO_ANDI = 0x4
  1319. X### LO_DST = 0x5
  1320. X### LO_XOR = 0x6
  1321. X### LO_OR = 0x7
  1322. X### LO_NOR = 0x8
  1323. X### LO_XNOR = 0x9
  1324. X### LO_NDST = 0xa
  1325. X### LO_ORR = 0xb
  1326. X### LO_NSRC = 0xc
  1327. X### LO_ORI = 0xd
  1328. X### LO_NAND = 0xe
  1329. X### LO_ONE = 0xf
  1330. X
  1331. X
  1332. X#
  1333. X# START defines for getgdesc 
  1334. X#
  1335. X
  1336. XGD_XPMAX = 0
  1337. XGD_YPMAX = 1
  1338. XGD_XMMAX = 2
  1339. XGD_YMMAX = 3
  1340. XGD_ZMIN = 4
  1341. XGD_ZMAX = 5
  1342. XGD_BITS_NORM_SNG_RED = 6
  1343. XGD_BITS_NORM_SNG_GREEN = 7
  1344. XGD_BITS_NORM_SNG_BLUE = 8
  1345. XGD_BITS_NORM_DBL_RED = 9
  1346. XGD_BITS_NORM_DBL_GREEN = 10
  1347. XGD_BITS_NORM_DBL_BLUE = 11
  1348. XGD_BITS_NORM_SNG_CMODE = 12
  1349. XGD_BITS_NORM_DBL_CMODE = 13
  1350. XGD_BITS_NORM_SNG_MMAP = 14
  1351. XGD_BITS_NORM_DBL_MMAP = 15
  1352. XGD_BITS_NORM_ZBUFFER = 16
  1353. XGD_BITS_OVER_SNG_CMODE = 17
  1354. XGD_BITS_UNDR_SNG_CMODE = 18
  1355. XGD_BITS_PUP_SNG_CMODE = 19
  1356. XGD_BITS_NORM_SNG_ALPHA = 21 
  1357. XGD_BITS_NORM_DBL_ALPHA = 22
  1358. XGD_BITS_CURSOR = 23
  1359. XGD_OVERUNDER_SHARED = 24
  1360. XGD_BLEND = 25
  1361. XGD_CIFRACT = 26
  1362. XGD_CROSSHAIR_CINDEX = 27
  1363. XGD_DITHER = 28
  1364. XGD_LINESMOOTH_CMODE = 30
  1365. XGD_LINESMOOTH_RGB = 31
  1366. XGD_LOGICOP = 33
  1367. XGD_NSCRNS = 35
  1368. XGD_NURBS_ORDER = 36
  1369. XGD_NBLINKS = 37
  1370. XGD_NVERTEX_POLY = 39
  1371. XGD_PATSIZE_64 = 40
  1372. XGD_PNTSMOOTH_CMODE = 41
  1373. XGD_PNTSMOOTH_RGB = 42
  1374. XGD_PUP_TO_OVERUNDER = 43
  1375. XGD_READSOURCE = 44
  1376. XGD_READSOURCE_ZBUFFER = 48
  1377. XGD_STEREO = 50
  1378. XGD_SUBPIXEL_LINE = 51
  1379. XGD_SUBPIXEL_PNT = 52
  1380. XGD_SUBPIXEL_POLY = 53
  1381. XGD_TRIMCURVE_ORDER = 54
  1382. XGD_WSYS = 55
  1383. XGD_ZDRAW_GEOM = 57
  1384. XGD_ZDRAW_PIXELS = 58
  1385. XGD_SCRNTYPE = 61
  1386. XGD_TEXTPORT = 62
  1387. XGD_NMMAPS = 63
  1388. XGD_FRAMEGRABBER = 64
  1389. XGD_TIMERHZ = 66
  1390. XGD_DBBOX = 67
  1391. XGD_AFUNCTION = 68
  1392. XGD_ALPHA_OVERUNDER = 69
  1393. XGD_BITS_ACBUF = 70
  1394. XGD_BITS_ACBUF_HW = 71
  1395. XGD_BITS_STENCIL = 72
  1396. XGD_CLIPPLANES = 73
  1397. XGD_FOGVERTEX = 74
  1398. XGD_LIGHTING_TWOSIDE = 76
  1399. XGD_POLYMODE = 77
  1400. XGD_POLYSMOOTH = 78
  1401. XGD_SCRBOX = 79
  1402. XGD_TEXTURE = 80
  1403. X
  1404. X# return value for inquiries when there is no limit
  1405. XGD_NOLIMIT = 2
  1406. X
  1407. X# return values for GD_WSYS
  1408. XGD_WSYS_NONE = 0
  1409. XGD_WSYS_4S = 1
  1410. X
  1411. X# return values for GD_SCRNTYPE
  1412. XGD_SCRNTYPE_WM = 0
  1413. XGD_SCRNTYPE_NOWM = 1
  1414. X
  1415. X# 
  1416. X# END defines for getgdesc 
  1417. X#
  1418. X
  1419. X
  1420. X# 
  1421. X# START NURBS interface definitions 
  1422. X#
  1423. X
  1424. X# NURBS Rendering Properties 
  1425. XN_PIXEL_TOLERANCE = 1
  1426. XN_CULLING = 2
  1427. XN_DISPLAY = 3
  1428. XN_ERRORCHECKING = 4
  1429. XN_SUBDIVISIONS = 5
  1430. XN_S_STEPS = 6
  1431. XN_T_STEPS = 7
  1432. XN_TILES = 8
  1433. X
  1434. XN_SHADED = 1.0     
  1435. X
  1436. X# ---------------------------------------------------------------------------
  1437. X# FLAGS FOR NURBS SURFACES AND CURVES             
  1438. X# 
  1439. X# Bit: 9876 5432 1 0 
  1440. X#     |tttt|nnnn|f|r| :    r - 1 bit = 1 if rational coordinate exists
  1441. X#                    :    f - 1 bit = 1 if rational coordinate is before rest 
  1442. X#                    :              = 0 if rational coordinate is after rest 
  1443. X#                 : nnnn - 4 bits for number of coordinates
  1444. X#                : tttt - 4 bits for type of data (color, position, etc.)
  1445. X# 
  1446. X# NURBS data type
  1447. X# N_T_ST         0     parametric space data
  1448. X# N_T_XYZ        1     model space data
  1449. X# 
  1450. X# rational or non-rational data and position in memory 
  1451. X# N_NONRATIONAL    0     non-rational data
  1452. X# N_RATAFTER        1     rational data with rat coord after rest
  1453. X# N_RATBEFORE        3     rational data with rat coord before rest
  1454. X# 
  1455. X# N_MKFLAG(a,b,c) ((a<<6) | (b<<2) | c)
  1456. X#     
  1457. X# ---------------------------------------------------------------------------
  1458. X# 
  1459. XN_ST = 0x8    # N_MKFLAG( N_T_ST, 2, N_NONRATIONAL ) 
  1460. XN_STW = 0xd    # N_MKFLAG( N_T_ST, 3, N_RATAFTER ) 
  1461. XN_WST = 0xf    # N_MKFLAG( N_T_ST, 3, N_RATBEFORE ) 
  1462. XN_XYZ = 0x4c    # N_MKFLAG( N_T_XYZ, 3, N_NONRATIONAL ) 
  1463. XN_XYZW = 0x51    # N_MKFLAG( N_T_XYZ, 4, N_RATAFTER ) 
  1464. XN_WXYZ = 0x53    # N_MKFLAG( N_T_XYZ, 4, N_RATBEFORE ) 
  1465. X
  1466. X# 
  1467. X# END NURBS interface definitions 
  1468. X# 
  1469. X
  1470. X
  1471. X# 
  1472. X# START lighting model defines 
  1473. X# 
  1474. X
  1475. XLMNULL = 0.0
  1476. X
  1477. X# MATRIX modes    
  1478. XMSINGLE = 0
  1479. XMPROJECTION = 1
  1480. XMVIEWING = 2
  1481. X
  1482. X# LIGHT constants 
  1483. XMAXLIGHTS = 8
  1484. XMAXRESTRICTIONS = 4
  1485. X
  1486. X# MATERIAL properties 
  1487. XDEFMATERIAL = 0
  1488. XEMISSION = 1
  1489. XAMBIENT = 2
  1490. XDIFFUSE = 3
  1491. XSPECULAR = 4
  1492. XSHININESS = 5
  1493. XCOLORINDEXES = 6
  1494. XALPHA = 7
  1495. X
  1496. X# LIGHT properties 
  1497. XDEFLIGHT = 100
  1498. XLCOLOR = 101
  1499. XPOSITION = 102
  1500. X
  1501. X# LIGHTINGMODEL properties 
  1502. XDEFLMODEL = 200
  1503. XLOCALVIEWER = 201
  1504. XATTENUATION = 202
  1505. X
  1506. X# TARGET constants 
  1507. XMATERIAL = 1000
  1508. XLIGHT0 = 1100
  1509. XLIGHT1 = 1101
  1510. XLIGHT2 = 1102
  1511. XLIGHT3 = 1103
  1512. XLIGHT4 = 1104
  1513. XLIGHT5 = 1105
  1514. XLIGHT6 = 1106
  1515. XLIGHT7 = 1107
  1516. XLMODEL = 1200
  1517. X
  1518. X# lmcolor modes 
  1519. XLMC_COLOR = 0
  1520. XLMC_EMISSION = 1
  1521. XLMC_AMBIENT = 2
  1522. XLMC_DIFFUSE = 3
  1523. XLMC_SPECULAR = 4
  1524. XLMC_AD = 5
  1525. XLMC_NULL = 6
  1526. X
  1527. X# 
  1528. X# END lighting model defines 
  1529. X# 
  1530. X
  1531. X
  1532. X# 
  1533. X# START distributed graphics library defines 
  1534. X# 
  1535. X
  1536. XDGLSINK = 0    # sink connection    
  1537. XDGLLOCAL = 1    # local connection    
  1538. XDGLTSOCKET = 2    # tcp socket connection
  1539. XDGL4DDN = 3    # 4DDN (DECnet)    
  1540. X
  1541. X# 
  1542. X# END distributed graphics library defines 
  1543. X# 
  1544. EOF
  1545. fi
  1546. if test -s 'lib/calendar.py'
  1547. then echo '*** I will not over-write existing file lib/calendar.py'
  1548. else
  1549. echo 'x - lib/calendar.py'
  1550. sed 's/^X//' > 'lib/calendar.py' << 'EOF'
  1551. X# module calendar
  1552. X
  1553. X##############################
  1554. X# Calendar support functions #
  1555. X##############################
  1556. X
  1557. X# This is based on UNIX ctime() et al. (also Standard C and POSIX)
  1558. X# Subtle but crucial differences:
  1559. X# - the order of the elements of a 'struct tm' differs, to ease sorting
  1560. X# - months numbers are 1-12, not 0-11; month arrays have a dummy element 0
  1561. X# - Monday is the first day of the week (numbered 0)
  1562. X
  1563. X# These are really parameters of the 'time' module:
  1564. Xepoch = 1970        # Time began on January 1 of this year (00:00:00 UCT)
  1565. Xday_0 = 3        # The epoch begins on a Thursday (Monday = 0)
  1566. X
  1567. X# Return 1 for leap years, 0 for non-leap years
  1568. Xdef isleap(year):
  1569. X    return year % 4 = 0 and (year % 100 <> 0 or year % 400 = 0)
  1570. X
  1571. X# Constants for months referenced later
  1572. XJanuary = 1
  1573. XFebruary = 2
  1574. X
  1575. X# Number of days per month (except for February in leap years)
  1576. Xmdays = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
  1577. X
  1578. X# Exception raised for bad input (with string parameter for details)
  1579. Xerror = 'calendar error'
  1580. X
  1581. X# Turn seconds since epoch into calendar time
  1582. Xdef gmtime(secs):
  1583. X    if secs < 0: raise error, 'negative input to gmtime()'
  1584. X    mins, secs = divmod(secs, 60)
  1585. X    hours, mins = divmod(mins, 60)
  1586. X    days, hours = divmod(hours, 24)
  1587. X    wday = (days + day_0) % 7
  1588. X    year = epoch
  1589. X    # XXX Most of the following loop can be replaced by one division
  1590. X    while 1:
  1591. X        yd = 365 + isleap(year)
  1592. X        if days < yd: break
  1593. X        days = days - yd
  1594. X        year = year + 1
  1595. X    yday = days
  1596. X    month = January
  1597. X    while 1:
  1598. X        md = mdays[month] + (month = February and isleap(year))
  1599. X        if days < md: break
  1600. X        days = days - md
  1601. X        month = month + 1
  1602. X    return year, month, days + 1, hours, mins, secs, yday, wday
  1603. X    # XXX Week number also?
  1604. X
  1605. X# Return number of leap years in range [y1, y2)
  1606. X# Assume y1 <= y2 and no funny (non-leap century) years
  1607. Xdef leapdays(y1, y2):
  1608. X    return (y2+3)/4 - (y1+3)/4
  1609. X
  1610. X# Inverse of gmtime():
  1611. X# Turn UCT calendar time (less yday, wday) into seconds since epoch
  1612. Xdef mktime(year, month, day, hours, mins, secs):
  1613. X    days = day - 1
  1614. X    for m in range(January, month): days = days + mdays[m]
  1615. X    if isleap(year) and month > February: days = days+1
  1616. X    days = days + (year-epoch)*365 + leapdays(epoch, year)
  1617. X    return ((days*24 + hours)*60 + mins)*60 + secs
  1618. X
  1619. X# Full and abbreviated names of weekdays
  1620. Xday_name = ('Monday', 'Tuesday', 'Wednesday', 'Thursday')
  1621. Xday_name = day_name + ('Friday', 'Saturday', 'Sunday')
  1622. Xday_abbr = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
  1623. X
  1624. X# Full and abbreviated of months (1-based arrays!!!)
  1625. Xmonth_name =          ('', 'January',   'February', 'March',    'April')
  1626. Xmonth_name = month_name + ('May',       'June',     'July',     'August')
  1627. Xmonth_name = month_name + ('September', 'October',  'November', 'December')
  1628. Xmonth_abbr =       ('   ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun')
  1629. Xmonth_abbr = month_abbr + ('Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
  1630. X
  1631. X# Zero-fill string to two positions (helper for asctime())
  1632. Xdef dd(s):
  1633. X    while len(s) < 2: s = '0' + s
  1634. X    return s
  1635. X
  1636. X# Blank-fill string to two positions (helper for asctime())
  1637. Xdef zd(s):
  1638. X    while len(s) < 2: s = ' ' + s
  1639. X    return s
  1640. X
  1641. X# Turn calendar time as returned by gmtime() into a string
  1642. X# (the yday parameter is for compatibility with gmtime())
  1643. Xdef asctime(year, month, day, hours, mins, secs, yday, wday):
  1644. X    s = day_abbr[wday] + ' ' + month_abbr[month] + ' ' + zd(`day`)
  1645. X    s = s + ' ' + dd(`hours`) + ':' + dd(`mins`) + ':' + dd(`secs`)
  1646. X    return s + ' ' + `year`
  1647. X
  1648. X# Localization: Minutes West from Greenwich
  1649. X# timezone = -2*60    # Middle-European time with DST on
  1650. Xtimezone = 5*60        # EST (sigh -- THINK time() doesn't return UCT)
  1651. X
  1652. X# Local time ignores DST issues for now -- adjust 'timezone' to fake it
  1653. Xdef localtime(secs):
  1654. X    return gmtime(secs - timezone*60)
  1655. X
  1656. X# UNIX-style ctime (except it doesn't append '\n'!)
  1657. Xdef ctime(secs):
  1658. X    return asctime(localtime(secs))
  1659. X
  1660. X######################
  1661. X# Non-UNIX additions #
  1662. X######################
  1663. X
  1664. X# Calendar printing etc.
  1665. X
  1666. X# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
  1667. Xdef weekday(year, month, day):
  1668. X    secs = mktime(year, month, day, 0, 0, 0)
  1669. X    days = secs / (24*60*60)
  1670. X    return (days + day_0) % 7
  1671. X
  1672. X# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
  1673. Xdef monthrange(year, month):
  1674. X    day1 = weekday(year, month, 1)
  1675. X    ndays = mdays[month] + (month = February and isleap(year))
  1676. X    return day1, ndays
  1677. X
  1678. X# Return a matrix representing a month's calendar
  1679. X# Each row represents a week; days outside this month are zero
  1680. Xdef _monthcalendar(year, month):
  1681. X    day1, ndays = monthrange(year, month)
  1682. X    rows = []
  1683. X    r7 = range(7)
  1684. X    day = 1 - day1
  1685. X    while day <= ndays:
  1686. X        row = [0, 0, 0, 0, 0, 0, 0]
  1687. X        for i in r7:
  1688. X            if 1 <= day <= ndays: row[i] = day
  1689. X            day = day + 1
  1690. X        rows.append(row)
  1691. X    return rows
  1692. X
  1693. X# Caching interface to _monthcalendar
  1694. Xmc_cache = {}
  1695. Xdef monthcalendar(year, month):
  1696. X    key = `year` + month_abbr[month]
  1697. X    try:
  1698. X        return mc_cache[key]
  1699. X    except RuntimeError:
  1700. X        mc_cache[key] = ret = _monthcalendar(year, month)
  1701. X        return ret
  1702. X
  1703. X# Center a string in a field
  1704. Xdef center(str, width):
  1705. X    n = width - len(str)
  1706. X    if n < 0: return str
  1707. X    return ' '*(n/2) + str + ' '*(n-n/2)
  1708. X
  1709. X# XXX The following code knows that print separates items with space!
  1710. X
  1711. X# Print a single week (no newline)
  1712. Xdef prweek(week, width):
  1713. X    for day in week:
  1714. X        if day = 0: print ' '*width,
  1715. X        else:
  1716. X            if width > 2: print ' '*(width-3),
  1717. X            if day < 10: print '',
  1718. X            print day,
  1719. X
  1720. X# Return a header for a week
  1721. Xdef weekheader(width):
  1722. X    str = ''
  1723. X    for i in range(7):
  1724. X        if str: str = str + ' '
  1725. X        str = str + day_abbr[i%7][:width]
  1726. X    return str
  1727. X
  1728. X# Print a month's calendar
  1729. Xdef prmonth(year, month):
  1730. X    print weekheader(3)
  1731. X    for week in monthcalendar(year, month):
  1732. X        prweek(week, 3)
  1733. X        print
  1734. X
  1735. X# Spacing between month columns
  1736. Xspacing = '    '
  1737. X
  1738. X# 3-column formatting for year calendars
  1739. Xdef format3c(a, b, c):
  1740. X    print center(a, 20), spacing, center(b, 20), spacing, center(c, 20)
  1741. X
  1742. X# Print a year's calendar
  1743. Xdef prcal(year):
  1744. X    header = weekheader(2)
  1745. X    format3c('', `year`, '')
  1746. X    for q in range(January, January+12, 3):
  1747. X        print
  1748. X        format3c(month_name[q], month_name[q+1], month_name[q+2])
  1749. X        format3c(header, header, header)
  1750. X        data = []
  1751. X        height = 0
  1752. X        for month in range(q, q+3):
  1753. X            cal = monthcalendar(year, month)
  1754. X            if len(cal) > height: height = len(cal)
  1755. X            data.append(cal)
  1756. X        for i in range(height):
  1757. X            for cal in data:
  1758. X                if i >= len(cal):
  1759. X                    print ' '*20,
  1760. X                else:
  1761. X                    prweek(cal[i], 2)
  1762. X                print spacing,
  1763. X            print
  1764. EOF
  1765. fi
  1766. if test -s 'lib/panel.py'
  1767. then echo '*** I will not over-write existing file lib/panel.py'
  1768. else
  1769. echo 'x - lib/panel.py'
  1770. sed 's/^X//' > 'lib/panel.py' << 'EOF'
  1771. X# Module 'panel'
  1772. X#
  1773. X# Support for the Panel library.
  1774. X# Uses built-in module 'pnl'.
  1775. X# Applciations should use 'panel.function' instead of 'pnl.function';
  1776. X# most 'pnl' functions are transparently exported by 'panel',
  1777. X# but dopanel() is overridden and you have to use this version
  1778. X# if you want to use callbacks.
  1779. X
  1780. X
  1781. Ximport pnl
  1782. X
  1783. X
  1784. Xdebug = 0
  1785. X
  1786. X
  1787. X# Test if an object is a list.
  1788. X#
  1789. Xdef is_list(x):
  1790. X    return type(x) = type([])
  1791. X
  1792. X
  1793. X# Reverse a list.
  1794. X#
  1795. Xdef reverse(list):
  1796. X    res = []
  1797. X    for item in list:
  1798. X        res.insert(0, item)
  1799. X    return res
  1800. X
  1801. X
  1802. X# Get an attribute of a list, which may itself be another list.
  1803. X# Don't use 'prop' for name.
  1804. X#
  1805. Xdef getattrlist(list, name):
  1806. X    for item in list:
  1807. X        if item and is_list(item) and item[0] = name:
  1808. X            return item[1:]
  1809. X    return []
  1810. X
  1811. X
  1812. X# Get a property of a list, which may itself be another list.
  1813. X#
  1814. Xdef getproplist(list, name):
  1815. X    for item in list:
  1816. X        if item and is_list(item) and item[0] = 'prop':
  1817. X            if len(item) > 1 and item[1] = name:
  1818. X                return item[2:]
  1819. X    return []
  1820. X
  1821. X
  1822. X# Test if an actuator description contains the property 'end-of-group'
  1823. X#
  1824. Xdef is_endgroup(list):
  1825. X    x = getproplist(list, 'end-of-group')
  1826. X    return (x and x[0] = '#t')
  1827. X
  1828. X
  1829. X# Neatly display an actuator definition given as S-expression
  1830. X# the prefix string is printed before each line.
  1831. X#
  1832. Xdef show_actuator(prefix, a):
  1833. X    for item in a:
  1834. X        if not is_list(item):
  1835. X            print prefix, item
  1836. X        elif item and item[0] = 'al':
  1837. X            print prefix, 'Subactuator list:'
  1838. X            for a in item[1:]:
  1839. X                show_actuator(prefix + '    ', a)
  1840. X        elif len(item) = 2:
  1841. X            print prefix, item[0], '=>', item[1]
  1842. X        elif len(item) = 3 and item[0] = 'prop':
  1843. X            print prefix, 'Prop', item[1], '=>',
  1844. X            print item[2]
  1845. X        else:
  1846. X            print prefix, '?', item
  1847. X
  1848. X
  1849. X# Neatly display a panel.
  1850. X#
  1851. Xdef show_panel(prefix, p):
  1852. X    for item in p:
  1853. X        if not is_list(item):
  1854. X            print prefix, item
  1855. X        elif item and item[0] = 'al':
  1856. X            print prefix, 'Actuator list:'
  1857. X            for a in item[1:]:
  1858. X                show_actuator(prefix + '    ', a)
  1859. X        elif len(item) = 2:
  1860. X            print prefix, item[0], '=>', item[1]
  1861. X        elif len(item) = 3 and item[0] = 'prop':
  1862. X            print prefix, 'Prop', item[1], '=>',
  1863. X            print item[2]
  1864. X        else:
  1865. X            print prefix, '?', item
  1866. X
  1867. X
  1868. X# Exception raised by build_actuator or build_panel.
  1869. X#
  1870. Xpanel_error = 'panel error'
  1871. X
  1872. X
  1873. X# Dummy callback used to initialize the callbacks.
  1874. X#
  1875. Xdef dummy_callback(arg):
  1876. X    pass
  1877. X
  1878. X
  1879. X# Assign attributes to members of the target.
  1880. X# Attribute names in exclist are ignored.
  1881. X# The member name is the attribute name prefixed with the prefix.
  1882. X#
  1883. Xdef assign_members(target, attrlist, exclist, prefix):
  1884. X    for item in attrlist:
  1885. X        if is_list(item) and len(item) = 2 and item[0] not in exclist:
  1886. X            name, value = item[0], item[1]
  1887. X            ok = 1
  1888. X            if value[0] in '-0123456789':
  1889. X                value = eval(value)
  1890. X            elif value[0] = '"':
  1891. X                value = value[1:-1]
  1892. X            elif value = 'move-then-resize':
  1893. X                # Strange default set by Panel Editor...
  1894. X                ok = 0
  1895. X            else:
  1896. X                print 'unknown value', value, 'for', name
  1897. X                ok = 0
  1898. X            if ok:
  1899. X                lhs = 'target.' + prefix + name
  1900. X                stmt = lhs + '=' + `value`
  1901. X                if debug: print 'exec', stmt
  1902. X                try:
  1903. X                    exec(stmt + '\n')
  1904. X                except KeyboardInterrupt: # Don't catch this!
  1905. X                    raise KeyboardInterrupt
  1906. X                except:
  1907. X                    print 'assign failed:', stmt
  1908. X
  1909. X
  1910. X# Build a real actuator from an actuator descriptior.
  1911. X# Return a pair (actuator, name).
  1912. X#
  1913. Xdef build_actuator(descr):
  1914. X    namelist = getattrlist(descr, 'name')
  1915. X    if namelist:
  1916. X        # Assume it is a string
  1917. X        actuatorname = namelist[0][1:-1]
  1918. X    else:
  1919. X        actuatorname = ''
  1920. X    type = descr[0]
  1921. X    if type[:4] = 'pnl_': type = type[4:]
  1922. X    act = pnl.mkact(type)
  1923. X    act.downfunc = act.activefunc = act.upfunc = dummy_callback
  1924. X    #
  1925. X    assign_members(act, descr[1:], ['al', 'data', 'name'], '')
  1926. X    #
  1927. X    # Treat actuator-specific data
  1928. X    #
  1929. X    datalist = getattrlist(descr, 'data')
  1930. X    prefix = ''
  1931. X    if type[-4:] = 'puck':
  1932. X        prefix = 'puck_'
  1933. X    elif type = 'mouse':
  1934. X        prefix = 'mouse_'
  1935. X    assign_members(act, datalist, [], prefix)
  1936. X    #
  1937. X    return act, actuatorname
  1938. X
  1939. X
  1940. X# Build all sub-actuators and add them to the super-actuator.
  1941. X# The super-actuator must already have been added to the panel.
  1942. X# Sub-actuators with defined names are added as members to the panel
  1943. X# so they can be referenced as p.name.
  1944. X#
  1945. X# Note: I have no idea how panel.endgroup() works when applied
  1946. X# to a sub-actuator.
  1947. X#
  1948. Xdef build_subactuators(panel, super_act, al):
  1949. X    #
  1950. X    # This is nearly the same loop as below in build_panel(),
  1951. X    # except a call is made to addsubact() instead of addact().
  1952. X    #
  1953. X    for a in al:
  1954. X        act, name = build_actuator(a)
  1955. X        act.addsubact(super_act)
  1956. X        if name:
  1957. X            stmt = 'panel.' + name + ' = act'
  1958. X            if debug: print 'exec', stmt
  1959. X            exec(stmt + '\n')
  1960. X        if is_endgroup(a):
  1961. X            panel.endgroup()
  1962. X        sub_al = getattrlist(a, 'al')
  1963. X        if sub_al:
  1964. X            build_subactuators(panel, act, sub_al)
  1965. X    #
  1966. X    # Fix the actuator to which whe just added subactuators.
  1967. X    # This can't hurt (I hope) and is needed for the scroll actuator.
  1968. X    #
  1969. X    super_act.fixact()
  1970. X
  1971. X
  1972. X# Build a real panel from a panel definition.
  1973. X# Return a panel object p, where for each named actuator a, p.name is a
  1974. X# reference to a.
  1975. X#
  1976. Xdef build_panel(descr):
  1977. X    #
  1978. X    # Sanity check
  1979. X    #
  1980. X    if (not descr) or descr[0] <> 'panel':
  1981. X        raise panel_error, 'panel description must start with "panel"'
  1982. X    #
  1983. X    if debug: show_panel('', descr)
  1984. X    #
  1985. X    # Create an empty panel
  1986. X    #
  1987. X    panel = pnl.mkpanel()
  1988. X    #
  1989. X    # Assign panel attributes
  1990. X    #
  1991. X    assign_members(panel, descr[1:], ['al'], '')
  1992. X    #
  1993. X    # Look for actuator list
  1994. X    #
  1995. X    al = getattrlist(descr, 'al')
  1996. X    #
  1997. X    # The order in which actuators are created is important
  1998. X    # because of the endgroup() operator.
  1999. X    # Unfortunately the Panel Editor outputs the actuator list
  2000. X    # in reverse order, so we reverse it here.
  2001. X    #
  2002. X    al = reverse(al)
  2003. X    #
  2004. X    for a in al:
  2005. X        act, name = build_actuator(a)
  2006. X        act.addact(panel)
  2007. X        if name:
  2008. X            stmt = 'panel.' + name + ' = act'
  2009. X            exec(stmt + '\n')
  2010. X        if is_endgroup(a):
  2011. X            panel.endgroup()
  2012. X        sub_al = getattrlist(a, 'al')
  2013. X        if sub_al:
  2014. X            build_subactuators(panel, act, sub_al)
  2015. X    #
  2016. X    return panel
  2017. X
  2018. X
  2019. X# Wrapper around pnl.dopanel() which calls call-back functions.
  2020. X#
  2021. Xdef my_dopanel():
  2022. X    # Extract only the first 4 elements to allow for future expansion
  2023. X    a, down, active, up = pnl.dopanel()[:4]
  2024. X    if down:
  2025. X        down.downfunc(down)
  2026. X    if active:
  2027. X        active.activefunc(active)
  2028. X    if up:
  2029. X        up.upfunc(up)
  2030. X    return a
  2031. X
  2032. X
  2033. X# Create one or more panels from a description file (S-expressions)
  2034. X# generated by the Panel Editor.
  2035. X# 
  2036. Xdef defpanellist(file):
  2037. X    import panelparser
  2038. X    descrlist = panelparser.parse_file(open(file, 'r'))
  2039. X    panellist = []
  2040. X    for descr in descrlist:
  2041. X        panellist.append(build_panel(descr))
  2042. X    return panellist
  2043. X
  2044. X
  2045. X# Import everything from built-in method pnl, so the user can always
  2046. X# use panel.foo() instead of pnl.foo().
  2047. X# This gives *no* performance penalty once this module is imported.
  2048. X#
  2049. Xfrom pnl import *            # for export
  2050. X
  2051. Xdopanel = my_dopanel            # override pnl.dopanel
  2052. EOF
  2053. fi
  2054. if test -s 'src/acceler.c'
  2055. then echo '*** I will not over-write existing file src/acceler.c'
  2056. else
  2057. echo 'x - src/acceler.c'
  2058. sed 's/^X//' > 'src/acceler.c' << 'EOF'
  2059. X/***********************************************************
  2060. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2061. XNetherlands.
  2062. X
  2063. X                        All Rights Reserved
  2064. X
  2065. XPermission to use, copy, modify, and distribute this software and its 
  2066. Xdocumentation for any purpose and without fee is hereby granted, 
  2067. Xprovided that the above copyright notice appear in all copies and that
  2068. Xboth that copyright notice and this permission notice appear in 
  2069. Xsupporting documentation, and that the names of Stichting Mathematisch
  2070. XCentrum or CWI not be used in advertising or publicity pertaining to
  2071. Xdistribution of the software without specific, written prior permission.
  2072. X
  2073. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2074. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2075. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2076. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2077. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2078. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2079. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2080. X
  2081. X******************************************************************/
  2082. X
  2083. X/* Parser accelerator module */
  2084. X
  2085. X/* The parser as originally conceived had disappointing performance.
  2086. X   This module does some precomputation that speeds up the selection
  2087. X   of a DFA based upon a token, turning a search through an array
  2088. X   into a simple indexing operation.  The parser now cannot work
  2089. X   without the accelerators installed.  Note that the accelerators
  2090. X   are installed dynamically when the parser is initialized, they
  2091. X   are not part of the static data structure written on graminit.[ch]
  2092. X   by the parser generator. */
  2093. X
  2094. X#include "pgenheaders.h"
  2095. X#include "grammar.h"
  2096. X#include "token.h"
  2097. X#include "parser.h"
  2098. X
  2099. X/* Forward references */
  2100. Xstatic void fixdfa PROTO((grammar *, dfa *));
  2101. Xstatic void fixstate PROTO((grammar *, dfa *, state *));
  2102. X
  2103. Xvoid
  2104. Xaddaccelerators(g)
  2105. X    grammar *g;
  2106. X{
  2107. X    dfa *d;
  2108. X    int i;
  2109. X#ifdef DEBUG
  2110. X    printf("Adding parser accellerators ...\n");
  2111. X#endif
  2112. X    d = g->g_dfa;
  2113. X    for (i = g->g_ndfas; --i >= 0; d++)
  2114. X        fixdfa(g, d);
  2115. X    g->g_accel = 1;
  2116. X#ifdef DEBUG
  2117. X    printf("Done.\n");
  2118. X#endif
  2119. X}
  2120. X
  2121. Xstatic void
  2122. Xfixdfa(g, d)
  2123. X    grammar *g;
  2124. X    dfa *d;
  2125. X{
  2126. X    state *s;
  2127. X    int j;
  2128. X    s = d->d_state;
  2129. X    for (j = 0; j < d->d_nstates; j++, s++)
  2130. X        fixstate(g, d, s);
  2131. X}
  2132. X
  2133. Xstatic void
  2134. Xfixstate(g, d, s)
  2135. X    grammar *g;
  2136. X    dfa *d;
  2137. X    state *s;
  2138. X{
  2139. X    arc *a;
  2140. X    int k;
  2141. X    int *accel;
  2142. X    int nl = g->g_ll.ll_nlabels;
  2143. X    s->s_accept = 0;
  2144. X    accel = NEW(int, nl);
  2145. X    for (k = 0; k < nl; k++)
  2146. X        accel[k] = -1;
  2147. X    a = s->s_arc;
  2148. X    for (k = s->s_narcs; --k >= 0; a++) {
  2149. X        int lbl = a->a_lbl;
  2150. X        label *l = &g->g_ll.ll_label[lbl];
  2151. X        int type = l->lb_type;
  2152. X        if (a->a_arrow >= (1 << 7)) {
  2153. X            printf("XXX too many states!\n");
  2154. X            continue;
  2155. X        }
  2156. X        if (ISNONTERMINAL(type)) {
  2157. X            dfa *d1 = finddfa(g, type);
  2158. X            int ibit;
  2159. X            if (type - NT_OFFSET >= (1 << 7)) {
  2160. X                printf("XXX too high nonterminal number!\n");
  2161. X                continue;
  2162. X            }
  2163. X            for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
  2164. X                if (testbit(d1->d_first, ibit)) {
  2165. X                    if (accel[ibit] != -1)
  2166. X                        printf("XXX ambiguity!\n");
  2167. X                    accel[ibit] = a->a_arrow | (1 << 7) |
  2168. X                        ((type - NT_OFFSET) << 8);
  2169. X                }
  2170. X            }
  2171. X        }
  2172. X        else if (lbl == EMPTY)
  2173. X            s->s_accept = 1;
  2174. X        else if (lbl >= 0 && lbl < nl)
  2175. X            accel[lbl] = a->a_arrow;
  2176. X    }
  2177. X    while (nl > 0 && accel[nl-1] == -1)
  2178. X        nl--;
  2179. X    for (k = 0; k < nl && accel[k] == -1;)
  2180. X        k++;
  2181. X    if (k < nl) {
  2182. X        int i;
  2183. X        s->s_accel = NEW(int, nl-k);
  2184. X        if (s->s_accel == NULL) {
  2185. X            fprintf(stderr, "no mem to add parser accelerators\n");
  2186. X            exit(1);
  2187. X        }
  2188. X        s->s_lower = k;
  2189. X        s->s_upper = nl;
  2190. X        for (i = 0; k < nl; i++, k++)
  2191. X            s->s_accel[i] = accel[k];
  2192. X    }
  2193. X    DEL(accel);
  2194. X}
  2195. EOF
  2196. fi
  2197. if test -s 'src/classobject.c'
  2198. then echo '*** I will not over-write existing file src/classobject.c'
  2199. else
  2200. echo 'x - src/classobject.c'
  2201. sed 's/^X//' > 'src/classobject.c' << 'EOF'
  2202. X/***********************************************************
  2203. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2204. XNetherlands.
  2205. X
  2206. X                        All Rights Reserved
  2207. X
  2208. XPermission to use, copy, modify, and distribute this software and its 
  2209. Xdocumentation for any purpose and without fee is hereby granted, 
  2210. Xprovided that the above copyright notice appear in all copies and that
  2211. Xboth that copyright notice and this permission notice appear in 
  2212. Xsupporting documentation, and that the names of Stichting Mathematisch
  2213. XCentrum or CWI not be used in advertising or publicity pertaining to
  2214. Xdistribution of the software without specific, written prior permission.
  2215. X
  2216. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2217. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2218. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2219. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2220. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2221. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2222. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2223. X
  2224. X******************************************************************/
  2225. X
  2226. X/* Class object implementation */
  2227. X
  2228. X#include "allobjects.h"
  2229. X
  2230. X#include "structmember.h"
  2231. X
  2232. Xtypedef struct {
  2233. X    OB_HEAD
  2234. X    object    *cl_bases;    /* A tuple */
  2235. X    object    *cl_methods;    /* A dictionary */
  2236. X} classobject;
  2237. X
  2238. Xobject *
  2239. Xnewclassobject(bases, methods)
  2240. X    object *bases; /* NULL or tuple of classobjects! */
  2241. X    object *methods;
  2242. X{
  2243. X    classobject *op;
  2244. X    op = NEWOBJ(classobject, &Classtype);
  2245. X    if (op == NULL)
  2246. X        return NULL;
  2247. X    if (bases != NULL)
  2248. X        INCREF(bases);
  2249. X    op->cl_bases = bases;
  2250. X    INCREF(methods);
  2251. X    op->cl_methods = methods;
  2252. X    return (object *) op;
  2253. X}
  2254. X
  2255. X/* Class methods */
  2256. X
  2257. Xstatic void
  2258. Xclass_dealloc(op)
  2259. X    classobject *op;
  2260. X{
  2261. X    int i;
  2262. X    if (op->cl_bases != NULL)
  2263. X        DECREF(op->cl_bases);
  2264. X    DECREF(op->cl_methods);
  2265. X    free((ANY *)op);
  2266. X}
  2267. X
  2268. Xstatic object *
  2269. Xclass_getattr(op, name)
  2270. X    register classobject *op;
  2271. X    register char *name;
  2272. X{
  2273. X    register object *v;
  2274. X    v = dictlookup(op->cl_methods, name);
  2275. X    if (v != NULL) {
  2276. X        INCREF(v);
  2277. X        return v;
  2278. X    }
  2279. X    if (op->cl_bases != NULL) {
  2280. X        int n = gettuplesize(op->cl_bases);
  2281. X        int i;
  2282. X        for (i = 0; i < n; i++) {
  2283. X            v = class_getattr(gettupleitem(op->cl_bases, i), name);
  2284. X            if (v != NULL)
  2285. X                return v;
  2286. X            err_clear();
  2287. X        }
  2288. X    }
  2289. X    err_setstr(NameError, name);
  2290. X    return NULL;
  2291. X}
  2292. X
  2293. Xtypeobject Classtype = {
  2294. X    OB_HEAD_INIT(&Typetype)
  2295. X    0,
  2296. X    "class",
  2297. X    sizeof(classobject),
  2298. X    0,
  2299. X    class_dealloc,    /*tp_dealloc*/
  2300. X    0,        /*tp_print*/
  2301. X    class_getattr,    /*tp_getattr*/
  2302. X    0,        /*tp_setattr*/
  2303. X    0,        /*tp_compare*/
  2304. X    0,        /*tp_repr*/
  2305. X    0,        /*tp_as_number*/
  2306. X    0,        /*tp_as_sequence*/
  2307. X    0,        /*tp_as_mapping*/
  2308. X};
  2309. X
  2310. X
  2311. X/* We're not done yet: next, we define class member objects... */
  2312. X
  2313. Xtypedef struct {
  2314. X    OB_HEAD
  2315. X    classobject    *cm_class;    /* The class object */
  2316. X    object        *cm_attr;    /* A dictionary */
  2317. X} classmemberobject;
  2318. X
  2319. Xobject *
  2320. Xnewclassmemberobject(class)
  2321. X    register object *class;
  2322. X{
  2323. X    register classmemberobject *cm;
  2324. X    if (!is_classobject(class)) {
  2325. X        err_badcall();
  2326. X        return NULL;
  2327. X    }
  2328. X    cm = NEWOBJ(classmemberobject, &Classmembertype);
  2329. X    if (cm == NULL)
  2330. X        return NULL;
  2331. X    INCREF(class);
  2332. X    cm->cm_class = (classobject *)class;
  2333. X    cm->cm_attr = newdictobject();
  2334. X    if (cm->cm_attr == NULL) {
  2335. X        DECREF(cm);
  2336. X        return NULL;
  2337. X    }
  2338. X    return (object *)cm;
  2339. X}
  2340. X
  2341. X/* Class member methods */
  2342. X
  2343. Xstatic void
  2344. Xclassmember_dealloc(cm)
  2345. X    register classmemberobject *cm;
  2346. X{
  2347. X    DECREF(cm->cm_class);
  2348. X    if (cm->cm_attr != NULL)
  2349. X        DECREF(cm->cm_attr);
  2350. X    free((ANY *)cm);
  2351. X}
  2352. X
  2353. Xstatic object *
  2354. Xclassmember_getattr(cm, name)
  2355. X    register classmemberobject *cm;
  2356. X    register char *name;
  2357. X{
  2358. X    register object *v = dictlookup(cm->cm_attr, name);
  2359. X    if (v != NULL) {
  2360. X        INCREF(v);
  2361. X        return v;
  2362. X    }
  2363. X    v = class_getattr(cm->cm_class, name);
  2364. X    if (v == NULL)
  2365. X        return v; /* class_getattr() has set the error */
  2366. X    if (is_funcobject(v)) {
  2367. X        object *w = newclassmethodobject(v, (object *)cm);
  2368. X        DECREF(v);
  2369. X        return w;
  2370. X    }
  2371. X    DECREF(v);
  2372. X    err_setstr(NameError, name);
  2373. X    return NULL;
  2374. X}
  2375. X
  2376. Xstatic int
  2377. Xclassmember_setattr(cm, name, v)
  2378. X    classmemberobject *cm;
  2379. X    char *name;
  2380. X    object *v;
  2381. X{
  2382. X    if (v == NULL)
  2383. X        return dictremove(cm->cm_attr, name);
  2384. X    else
  2385. X        return dictinsert(cm->cm_attr, name, v);
  2386. X}
  2387. X
  2388. Xtypeobject Classmembertype = {
  2389. X    OB_HEAD_INIT(&Typetype)
  2390. X    0,
  2391. X    "class member",
  2392. X    sizeof(classmemberobject),
  2393. X    0,
  2394. X    classmember_dealloc,    /*tp_dealloc*/
  2395. X    0,            /*tp_print*/
  2396. X    classmember_getattr,    /*tp_getattr*/
  2397. X    classmember_setattr,    /*tp_setattr*/
  2398. X    0,            /*tp_compare*/
  2399. X    0,            /*tp_repr*/
  2400. X    0,            /*tp_as_number*/
  2401. X    0,            /*tp_as_sequence*/
  2402. X    0,            /*tp_as_mapping*/
  2403. X};
  2404. X
  2405. X
  2406. X/* And finally, here are class method objects */
  2407. X/* (Really methods of class members) */
  2408. X
  2409. Xtypedef struct {
  2410. X    OB_HEAD
  2411. X    object    *cm_func;    /* The method function */
  2412. X    object    *cm_self;    /* The object to which this applies */
  2413. X} classmethodobject;
  2414. X
  2415. Xobject *
  2416. Xnewclassmethodobject(func, self)
  2417. X    object *func;
  2418. X    object *self;
  2419. X{
  2420. X    register classmethodobject *cm;
  2421. X    if (!is_funcobject(func)) {
  2422. X        err_badcall();
  2423. X        return NULL;
  2424. X    }
  2425. X    cm = NEWOBJ(classmethodobject, &Classmethodtype);
  2426. X    if (cm == NULL)
  2427. X        return NULL;
  2428. X    INCREF(func);
  2429. X    cm->cm_func = func;
  2430. X    INCREF(self);
  2431. X    cm->cm_self = self;
  2432. X    return (object *)cm;
  2433. X}
  2434. X
  2435. Xobject *
  2436. Xclassmethodgetfunc(cm)
  2437. X    register object *cm;
  2438. X{
  2439. X    if (!is_classmethodobject(cm)) {
  2440. X        err_badcall();
  2441. X        return NULL;
  2442. X    }
  2443. X    return ((classmethodobject *)cm)->cm_func;
  2444. X}
  2445. X
  2446. Xobject *
  2447. Xclassmethodgetself(cm)
  2448. X    register object *cm;
  2449. X{
  2450. X    if (!is_classmethodobject(cm)) {
  2451. X        err_badcall();
  2452. X        return NULL;
  2453. X    }
  2454. X    return ((classmethodobject *)cm)->cm_self;
  2455. X}
  2456. X
  2457. X/* Class method methods */
  2458. X
  2459. X#define OFF(x) offsetof(classmethodobject, x)
  2460. X
  2461. Xstatic struct memberlist classmethod_memberlist[] = {
  2462. X    {"cm_func",    T_OBJECT,    OFF(cm_func)},
  2463. X    {"cm_self",    T_OBJECT,    OFF(cm_self)},
  2464. X    {NULL}    /* Sentinel */
  2465. X};
  2466. X
  2467. Xstatic object *
  2468. Xclassmethod_getattr(cm, name)
  2469. X    register classmethodobject *cm;
  2470. X    char *name;
  2471. X{
  2472. X    return getmember((char *)cm, classmethod_memberlist, name);
  2473. X}
  2474. X
  2475. Xstatic void
  2476. Xclassmethod_dealloc(cm)
  2477. X    register classmethodobject *cm;
  2478. X{
  2479. X    DECREF(cm->cm_func);
  2480. X    DECREF(cm->cm_self);
  2481. X    free((ANY *)cm);
  2482. X}
  2483. X
  2484. Xtypeobject Classmethodtype = {
  2485. X    OB_HEAD_INIT(&Typetype)
  2486. X    0,
  2487. X    "class method",
  2488. X    sizeof(classmethodobject),
  2489. X    0,
  2490. X    classmethod_dealloc,    /*tp_dealloc*/
  2491. X    0,            /*tp_print*/
  2492. X    classmethod_getattr,    /*tp_getattr*/
  2493. X    0,            /*tp_setattr*/
  2494. X    0,            /*tp_compare*/
  2495. X    0,            /*tp_repr*/
  2496. X    0,            /*tp_as_number*/
  2497. X    0,            /*tp_as_sequence*/
  2498. X    0,            /*tp_as_mapping*/
  2499. X};
  2500. EOF
  2501. fi
  2502. if test -s 'src/intobject.c'
  2503. then echo '*** I will not over-write existing file src/intobject.c'
  2504. else
  2505. echo 'x - src/intobject.c'
  2506. sed 's/^X//' > 'src/intobject.c' << 'EOF'
  2507. X/***********************************************************
  2508. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2509. XNetherlands.
  2510. X
  2511. X                        All Rights Reserved
  2512. X
  2513. XPermission to use, copy, modify, and distribute this software and its 
  2514. Xdocumentation for any purpose and without fee is hereby granted, 
  2515. Xprovided that the above copyright notice appear in all copies and that
  2516. Xboth that copyright notice and this permission notice appear in 
  2517. Xsupporting documentation, and that the names of Stichting Mathematisch
  2518. XCentrum or CWI not be used in advertising or publicity pertaining to
  2519. Xdistribution of the software without specific, written prior permission.
  2520. X
  2521. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2522. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2523. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2524. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2525. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2526. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2527. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2528. X
  2529. X******************************************************************/
  2530. X
  2531. X/* Integer object implementation */
  2532. X
  2533. X#include "allobjects.h"
  2534. X
  2535. X/* Standard Booleans */
  2536. X
  2537. Xintobject FalseObject = {
  2538. X    OB_HEAD_INIT(&Inttype)
  2539. X    0
  2540. X};
  2541. X
  2542. Xintobject TrueObject = {
  2543. X    OB_HEAD_INIT(&Inttype)
  2544. X    1
  2545. X};
  2546. X
  2547. Xstatic object *
  2548. Xerr_ovf()
  2549. X{
  2550. X    err_setstr(OverflowError, "integer overflow");
  2551. X    return NULL;
  2552. X}
  2553. X
  2554. Xstatic object *
  2555. Xerr_zdiv()
  2556. X{
  2557. X    err_setstr(ZeroDivisionError, "integer division by zero");
  2558. X    return NULL;
  2559. X}
  2560. X
  2561. X/* Integers are quite normal objects, to make object handling uniform.
  2562. X   (Using odd pointers to represent integers would save much space
  2563. X   but require extra checks for this special case throughout the code.)
  2564. X   Since, a typical Python program spends much of its time allocating
  2565. X   and deallocating integers, these operations should be very fast.
  2566. X   Therefore we use a dedicated allocation scheme with a much lower
  2567. X   overhead (in space and time) than straight malloc(): a simple
  2568. X   dedicated free list, filled when necessary with memory from malloc().
  2569. X*/
  2570. X
  2571. X#define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  2572. X#define N_INTOBJECTS    (BLOCK_SIZE / sizeof(intobject))
  2573. X
  2574. Xstatic intobject *
  2575. Xfill_free_list()
  2576. X{
  2577. X    intobject *p, *q;
  2578. X    p = NEW(intobject, N_INTOBJECTS);
  2579. X    if (p == NULL)
  2580. X        return (intobject *)err_nomem();
  2581. X    q = p + N_INTOBJECTS;
  2582. X    while (--q > p)
  2583. X        *(intobject **)q = q-1;
  2584. X    *(intobject **)q = NULL;
  2585. X    return p + N_INTOBJECTS - 1;
  2586. X}
  2587. X
  2588. Xstatic intobject *free_list = NULL;
  2589. X
  2590. Xobject *
  2591. Xnewintobject(ival)
  2592. X    long ival;
  2593. X{
  2594. X    register intobject *v;
  2595. X    if (free_list == NULL) {
  2596. X        if ((free_list = fill_free_list()) == NULL)
  2597. X            return NULL;
  2598. X    }
  2599. X    v = free_list;
  2600. X    free_list = *(intobject **)free_list;
  2601. X    NEWREF(v);
  2602. X    v->ob_type = &Inttype;
  2603. X    v->ob_ival = ival;
  2604. X    return (object *) v;
  2605. X}
  2606. X
  2607. Xstatic void
  2608. Xint_dealloc(v)
  2609. X    intobject *v;
  2610. X{
  2611. X    *(intobject **)v = free_list;
  2612. X    free_list = v;
  2613. X}
  2614. X
  2615. Xlong
  2616. Xgetintvalue(op)
  2617. X    register object *op;
  2618. X{
  2619. X    if (!is_intobject(op)) {
  2620. X        err_badcall();
  2621. X        return -1;
  2622. X    }
  2623. X    else
  2624. X        return ((intobject *)op) -> ob_ival;
  2625. X}
  2626. X
  2627. X/* Methods */
  2628. X
  2629. Xstatic void
  2630. Xint_print(v, fp, flags)
  2631. X    intobject *v;
  2632. X    FILE *fp;
  2633. X    int flags;
  2634. X{
  2635. X    fprintf(fp, "%ld", v->ob_ival);
  2636. X}
  2637. X
  2638. Xstatic object *
  2639. Xint_repr(v)
  2640. X    intobject *v;
  2641. X{
  2642. X    char buf[20];
  2643. X    sprintf(buf, "%ld", v->ob_ival);
  2644. X    return newstringobject(buf);
  2645. X}
  2646. X
  2647. Xstatic int
  2648. Xint_compare(v, w)
  2649. X    intobject *v, *w;
  2650. X{
  2651. X    register long i = v->ob_ival;
  2652. X    register long j = w->ob_ival;
  2653. X    return (i < j) ? -1 : (i > j) ? 1 : 0;
  2654. X}
  2655. X
  2656. Xstatic object *
  2657. Xint_add(v, w)
  2658. X    intobject *v;
  2659. X    register object *w;
  2660. X{
  2661. X    register long a, b, x;
  2662. X    if (!is_intobject(w)) {
  2663. X        err_badarg();
  2664. X        return NULL;
  2665. X    }
  2666. X    a = v->ob_ival;
  2667. X    b = ((intobject *)w) -> ob_ival;
  2668. X    x = a + b;
  2669. X    if ((x^a) < 0 && (x^b) < 0)
  2670. X        return err_ovf();
  2671. X    return newintobject(x);
  2672. X}
  2673. X
  2674. Xstatic object *
  2675. Xint_sub(v, w)
  2676. X    intobject *v;
  2677. X    register object *w;
  2678. X{
  2679. X    register long a, b, x;
  2680. X    if (!is_intobject(w)) {
  2681. X        err_badarg();
  2682. X        return NULL;
  2683. X    }
  2684. X    a = v->ob_ival;
  2685. X    b = ((intobject *)w) -> ob_ival;
  2686. X    x = a - b;
  2687. X    if ((x^a) < 0 && (x^~b) < 0)
  2688. X        return err_ovf();
  2689. X    return newintobject(x);
  2690. X}
  2691. X
  2692. Xstatic object *
  2693. Xint_mul(v, w)
  2694. X    intobject *v;
  2695. X    register object *w;
  2696. X{
  2697. X    register long a, b;
  2698. X    double x;
  2699. X    if (!is_intobject(w)) {
  2700. X        err_badarg();
  2701. X        return NULL;
  2702. X    }
  2703. X    a = v->ob_ival;
  2704. X    b = ((intobject *)w) -> ob_ival;
  2705. X    x = (double)a * (double)b;
  2706. X    if (x > 0x7fffffff || x < (double) (long) 0x80000000)
  2707. X        return err_ovf();
  2708. X    return newintobject(a * b);
  2709. X}
  2710. X
  2711. Xstatic object *
  2712. Xint_div(v, w)
  2713. X    intobject *v;
  2714. X    register object *w;
  2715. X{
  2716. X    if (!is_intobject(w)) {
  2717. X        err_badarg();
  2718. X        return NULL;
  2719. X    }
  2720. X    if (((intobject *)w) -> ob_ival == 0)
  2721. X        return err_zdiv();
  2722. X    return newintobject(v->ob_ival / ((intobject *)w) -> ob_ival);
  2723. X}
  2724. X
  2725. Xstatic object *
  2726. Xint_rem(v, w)
  2727. X    intobject *v;
  2728. X    register object *w;
  2729. X{
  2730. X    if (!is_intobject(w)) {
  2731. X        err_badarg();
  2732. X        return NULL;
  2733. X    }
  2734. X    if (((intobject *)w) -> ob_ival == 0)
  2735. X        return err_zdiv();
  2736. X    return newintobject(v->ob_ival % ((intobject *)w) -> ob_ival);
  2737. X}
  2738. X
  2739. Xstatic object *
  2740. Xint_pow(v, w)
  2741. X    intobject *v;
  2742. X    register object *w;
  2743. X{
  2744. X    register long iv, iw, ix;
  2745. X    register int neg;
  2746. X    if (!is_intobject(w)) {
  2747. X        err_badarg();
  2748. X        return NULL;
  2749. X    }
  2750. X    iv = v->ob_ival;
  2751. X    iw = ((intobject *)w)->ob_ival;
  2752. X    neg = 0;
  2753. X    if (iw < 0)
  2754. X        neg = 1, iw = -iw;
  2755. X    ix = 1;
  2756. X    for (; iw > 0; iw--)
  2757. X        ix = ix * iv;
  2758. X    if (neg) {
  2759. X        if (ix == 0)
  2760. X            return err_zdiv();
  2761. X        ix = 1/ix;
  2762. X    }
  2763. X    /* XXX How to check for overflow? */
  2764. X    return newintobject(ix);
  2765. X}
  2766. X
  2767. Xstatic object *
  2768. Xint_neg(v)
  2769. X    intobject *v;
  2770. X{
  2771. X    register long a, x;
  2772. X    a = v->ob_ival;
  2773. X    x = -a;
  2774. X    if (a < 0 && x < 0)
  2775. X        return err_ovf();
  2776. X    return newintobject(x);
  2777. X}
  2778. X
  2779. Xstatic object *
  2780. Xint_pos(v)
  2781. X    intobject *v;
  2782. X{
  2783. X    INCREF(v);
  2784. X    return (object *)v;
  2785. X}
  2786. X
  2787. Xstatic number_methods int_as_number = {
  2788. X    int_add,    /*tp_add*/
  2789. X    int_sub,    /*tp_subtract*/
  2790. X    int_mul,    /*tp_multiply*/
  2791. X    int_div,    /*tp_divide*/
  2792. X    int_rem,    /*tp_remainder*/
  2793. X    int_pow,    /*tp_power*/
  2794. X    int_neg,    /*tp_negate*/
  2795. X    int_pos,    /*tp_plus*/
  2796. X};
  2797. X
  2798. Xtypeobject Inttype = {
  2799. X    OB_HEAD_INIT(&Typetype)
  2800. X    0,
  2801. X    "int",
  2802. X    sizeof(intobject),
  2803. X    0,
  2804. X    int_dealloc,    /*tp_dealloc*/
  2805. X    int_print,    /*tp_print*/
  2806. X    0,        /*tp_getattr*/
  2807. X    0,        /*tp_setattr*/
  2808. X    int_compare,    /*tp_compare*/
  2809. X    int_repr,    /*tp_repr*/
  2810. X    &int_as_number,    /*tp_as_number*/
  2811. X    0,        /*tp_as_sequence*/
  2812. X    0,        /*tp_as_mapping*/
  2813. X};
  2814. EOF
  2815. fi
  2816. echo 'Part 13 out of 21 of pack.out complete.'
  2817. exit 0
  2818.