clock.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. static Image *im;
  32. int i;
  33. int anghr, angmin;
  34. static Tm tms;
  35. static Tm ntms;
  36. ntm = time(0);
  37. if(ntm == tm && eqrect(screen->r, r))
  38. return;
  39. ntms = *localtime(ntm);
  40. anghr = 90-(ntms.hour*5 + ntms.min/12)*6;
  41. angmin = 90-ntms.min*6;
  42. tm = ntm;
  43. tms = ntms;
  44. r = screen->r;
  45. c = divpt(addpt(r.min, r.max), 2);
  46. rad = Dx(r) < Dy(r) ? Dx(r) : Dy(r);
  47. rad /= 2;
  48. rad -= 8;
  49. draw(screen, screen->r, back, nil, ZP);
  50. for(i=0; i<12; i++)
  51. fillellipse(screen, circlept(c, rad, i*(360/12)), 2, 2, dots, ZP);
  52. line(screen, c, circlept(c, (rad*3)/4, angmin), 0, 0, 1, minhand, ZP);
  53. line(screen, c, circlept(c, rad/2, anghr), 0, 0, 1, hrhand, ZP);
  54. flushimage(display, 1);
  55. }
  56. void
  57. eresized(int new)
  58. {
  59. if(new && getwindow(display, Refnone) < 0)
  60. fprint(2,"can't reattach to window");
  61. redraw(screen);
  62. }
  63. void
  64. main(int i, char **c)
  65. {
  66. Event e;
  67. Mouse m;
  68. Menu menu;
  69. char *mstr[] = {"exit", 0};
  70. int key, timer;
  71. int t;
  72. if (initdraw(0, 0, "clock") < 0)
  73. sysfatal("initdraw failed");
  74. back = allocimagemix(display, DPalebluegreen, DWhite);
  75. hrhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DDarkblue);
  76. minhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPaleblue);
  77. dots = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
  78. redraw(screen);
  79. einit(Emouse);
  80. t = (30*1000);
  81. timer = etimer(0, t);
  82. menu.item = mstr;
  83. menu.lasthit = 0;
  84. for(;;) {
  85. key = event(&e);
  86. if(key == Emouse) {
  87. m = e.mouse;
  88. if(m.buttons & 4) {
  89. if(emenuhit(3, &m, &menu) == 0)
  90. exits(0);
  91. }
  92. } else if(key == timer) {
  93. redraw(screen);
  94. }
  95. }
  96. }