clock.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <event.h>
  13. Image *hrhand, *minhand;
  14. Image *dots, *back;
  15. Point
  16. circlept(Point c, int r, int degrees)
  17. {
  18. double rad;
  19. rad = (double) degrees * PI/180.0;
  20. c.x += cos(rad)*r;
  21. c.y -= sin(rad)*r;
  22. return c;
  23. }
  24. void
  25. redraw(Image *screen)
  26. {
  27. static int tm, ntm;
  28. static Rectangle r;
  29. static Point c;
  30. static int rad;
  31. int i;
  32. int anghr, angmin;
  33. static Tm ntms;
  34. ntm = time(0);
  35. if(ntm == tm && eqrect(screen->r, r))
  36. return;
  37. ntms = *localtime(ntm);
  38. anghr = 90-(ntms.hour*5 + ntms.min/12)*6;
  39. angmin = 90-ntms.min*6;
  40. tm = ntm;
  41. r = screen->r;
  42. c = divpt(addpt(r.min, r.max), 2);
  43. rad = Dx(r) < Dy(r) ? Dx(r) : Dy(r);
  44. rad /= 2;
  45. rad -= 8;
  46. draw(screen, screen->r, back, nil, ZP);
  47. for(i=0; i<12; i++)
  48. fillellipse(screen, circlept(c, rad, i*(360/12)), 2, 2, dots, ZP);
  49. line(screen, c, circlept(c, (rad*3)/4, angmin), 0, 0, 1, minhand, ZP);
  50. line(screen, c, circlept(c, rad/2, anghr), 0, 0, 1, hrhand, ZP);
  51. flushimage(display, 1);
  52. }
  53. void
  54. eresized(int new)
  55. {
  56. if(new && getwindow(display, Refnone) < 0)
  57. fprint(2,"can't reattach to window");
  58. redraw(screen);
  59. }
  60. void
  61. main(int i, char **c)
  62. {
  63. Event e;
  64. Mouse m;
  65. Menu menu;
  66. char *mstr[] = {"exit", 0};
  67. int key, timer;
  68. int t;
  69. if (initdraw(0, 0, "clock") < 0)
  70. sysfatal("initdraw failed");
  71. back = allocimagemix(display, DPalebluegreen, DWhite);
  72. hrhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DDarkblue);
  73. minhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPaleblue);
  74. dots = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
  75. redraw(screen);
  76. einit(Emouse);
  77. t = (30*1000);
  78. timer = etimer(0, t);
  79. menu.item = mstr;
  80. menu.lasthit = 0;
  81. for(;;) {
  82. key = event(&e);
  83. if(key == Emouse) {
  84. m = e.mouse;
  85. if(m.buttons & 4) {
  86. if(emenuhit(3, &m, &menu) == 0)
  87. exits(0);
  88. }
  89. } else if(key == timer) {
  90. redraw(screen);
  91. }
  92. }
  93. }