1 /* fps, Copyright (c) 2001-2008 Jamie Zawinski <jwz@jwz.org>
2 * Draw a frames-per-second display (Xlib and OpenGL).
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
15 #endif /* HAVE_CONFIG_H */
17 #include "screenhackI.h"
21 fps_init (Display *dpy, Window window)
28 if (! get_boolean_resource (dpy, "doFPS", "DoFPS"))
31 st = (fps_state *) calloc (1, sizeof(*st));
35 st->clear_p = get_boolean_resource (dpy, "fpsSolid", "FPSSolid");
37 font = get_string_resource (dpy, "fpsFont", "Font");
39 if (!font) font = "-*-courier-bold-r-normal-*-180-*";
40 f = XLoadQueryFont (dpy, font);
41 if (!f) f = XLoadQueryFont (dpy, "fixed");
43 first = f->min_char_or_byte2;
44 last = f->max_char_or_byte2;
47 XWindowAttributes xgwa;
49 XGetWindowAttributes (dpy, window, &xgwa);
52 get_pixel_resource (st->dpy, xgwa.colormap, "foreground", "Foreground");
53 st->draw_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
55 get_pixel_resource (st->dpy, xgwa.colormap, "background", "Background");
56 st->erase_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
62 if (get_boolean_resource (dpy, "fpsTop", "FPSTop"))
63 st->y = - (st->font->ascent + st->font->descent + 10);
65 strcpy (st->string, "FPS: ... ");
71 fps_free (fps_state *st)
73 if (st->draw_gc) XFreeGC (st->dpy, st->draw_gc);
74 if (st->erase_gc) XFreeGC (st->dpy, st->erase_gc);
75 if (st->font) XFreeFont (st->dpy, st->font);
81 fps_slept (fps_state *st, unsigned long usecs)
88 fps_compute (fps_state *st, unsigned long polys)
90 if (! st) return 0; /* too early? */
92 /* Every N frames (where N is approximately one second's worth of frames)
93 check the wall clock. We do this because checking the wall clock is
96 if (st->frame_count++ >= st->last_ifps)
98 # ifdef GETTIMEOFDAY_TWO_ARGS
100 gettimeofday(&st->this_frame_end, &tzp);
102 gettimeofday(&st->this_frame_end);
105 if (st->prev_frame_end.tv_sec == 0)
106 st->prev_frame_end = st->this_frame_end;
109 /* If we've probed the wall-clock time, regenerate the string.
111 if (st->this_frame_end.tv_sec != st->prev_frame_end.tv_sec)
113 double uprev_frame_end = (st->prev_frame_end.tv_sec +
114 ((double) st->prev_frame_end.tv_usec
116 double uthis_frame_end = (st->this_frame_end.tv_sec +
117 ((double) st->this_frame_end.tv_usec
119 double fps = st->frame_count / (uthis_frame_end - uprev_frame_end);
120 double idle = (((double) st->slept * 0.000001) /
121 (uthis_frame_end - uprev_frame_end));
122 double load = 100 * (1 - idle);
124 if (load < 0) load = 0; /* well that's obviously nonsense... */
126 st->prev_frame_end = st->this_frame_end;
132 sprintf (st->string, (polys
133 ? "FPS: %.1f \nLoad: %.1f%% "
134 : "FPS: %.1f \nLoad: %.1f%% "),
141 if (polys >= (1024 * 1024)) polys >>= 20, s = "M";
142 else if (polys >= 2048) polys >>= 10, s = "K";
145 strcat (st->string, "\nPolys: ");
146 if (polys >= 1000000)
147 sprintf (st->string + strlen(st->string), "%lu,%03lu,%03lu%s ",
148 (polys / 1000000), ((polys / 1000) % 1000),
150 else if (polys >= 1000)
151 sprintf (st->string + strlen(st->string), "%lu,%03lu%s ",
152 (polys / 1000), (polys % 1000), s);
154 sprintf (st->string + strlen(st->string), "%lu%s ", polys, s);
163 /* Width (and optionally height) of the string in pixels.
166 string_width (XFontStruct *f, const char *c, int *height_ret)
170 int h = f->ascent + f->descent;
173 int cc = *((unsigned char *) c);
176 if (x > max_w) max_w = x;
178 h += f->ascent + f->descent;
182 ? f->per_char[cc-f->min_char_or_byte2].width
183 : f->min_bounds.rbearing);
186 if (x > max_w) max_w = x;
187 if (height_ret) *height_ret = h;
193 /* This function is used only in Xlib mode. For GL mode, see glx/fps-glx.c.
196 fps_draw (fps_state *st)
198 XWindowAttributes xgwa;
199 const char *string = st->string;
204 int lh = st->font->ascent + st->font->descent;
206 XGetWindowAttributes (st->dpy, st->window, &xgwa);
208 for (s = string; *s; s++)
209 if (*s == '\n') lines++;
212 y = -y + (lines-1) * lh;
216 y -= lh * (lines-1) + st->font->descent;
218 /* clear the background */
222 w = string_width (st->font, string, &h);
223 XFillRectangle (st->dpy, st->window, st->erase_gc,
224 x - st->font->descent,
226 w + 2*st->font->descent,
227 h + 2*st->font->descent);
233 s = strchr (string, '\n');
234 if (! s) s = string + strlen(string);
235 XDrawString (st->dpy, st->window, st->draw_gc,
236 x, y, string, s - string);