1 /* gltext, Copyright (c) 2001, 2002 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 #include <X11/Intrinsic.h>
14 extern XtAppContext app;
16 #define PROGCLASS "GLText"
17 #define HACK_INIT init_text
18 #define HACK_DRAW draw_text
19 #define HACK_RESHAPE reshape_text
20 #define HACK_HANDLE_EVENT text_handle_event
21 #define EVENT_MASK PointerMotionMask
22 #define sws_opts xlockmore_opts
24 #define DEF_TEXT "(default)"
25 #define DEF_SPIN "XYZ"
26 #define DEF_WANDER "True"
28 #define DEFAULTS "*delay: 10000 \n" \
29 "*showFPS: False \n" \
30 "*wireframe: False \n" \
31 "*spin: " DEF_SPIN "\n" \
32 "*wander: " DEF_WANDER "\n" \
33 "*text: " DEF_TEXT "\n"
36 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
39 # define TUBE_FACES 12 /* how densely to render tubes */
46 #define countof(x) (sizeof((x))/sizeof((*x)))
48 #include "xlockmore.h"
52 #include "gltrackball.h"
57 #ifdef USE_GL /* whole file */
60 # include <sys/utsname.h>
61 #endif /* HAVE_UNAME */
65 #include "glutstroke.h"
66 #include "glut_roman.h"
67 #define GLUT_FONT (&glutStrokeRoman)
71 GLXContext *glx_context;
73 trackball_state *trackball;
86 static text_configuration *tps = NULL;
88 static char *text_fmt;
90 static Bool do_wander;
92 static XrmOptionDescRec opts[] = {
93 { "-text", ".text", XrmoptionSepArg, 0 },
94 { "-spin", ".spin", XrmoptionSepArg, 0 },
95 { "+spin", ".spin", XrmoptionNoArg, "False" },
96 { "-wander", ".wander", XrmoptionNoArg, "True" },
97 { "+wander", ".wander", XrmoptionNoArg, "False" }
100 static argtype vars[] = {
101 {(caddr_t *) &text_fmt, "text", "Text", DEF_TEXT, t_String},
102 {(caddr_t *) &do_spin, "spin", "Spin", DEF_SPIN, t_String},
103 {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
106 ModeSpecOpt sws_opts = {countof(opts), opts, countof(vars), vars, NULL};
109 /* Window management, etc
112 reshape_text (ModeInfo *mi, int width, int height)
114 GLfloat h = (GLfloat) height / (GLfloat) width;
116 glViewport (0, 0, (GLint) width, (GLint) height);
118 glMatrixMode(GL_PROJECTION);
120 gluPerspective (30.0, 1/h, 1.0, 100.0);
122 glMatrixMode(GL_MODELVIEW);
124 gluLookAt( 0.0, 0.0, 30.0,
128 glClear(GL_COLOR_BUFFER_BIT);
133 gl_init (ModeInfo *mi)
135 text_configuration *tp = &tps[MI_SCREEN(mi)];
136 int wire = MI_IS_WIREFRAME(mi);
138 static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
142 glLightfv(GL_LIGHT0, GL_POSITION, pos);
143 glEnable(GL_CULL_FACE);
144 glEnable(GL_LIGHTING);
146 glEnable(GL_DEPTH_TEST);
149 tp->text_list = glGenLists (1);
150 glNewList (tp->text_list, GL_COMPILE);
156 parse_text (ModeInfo *mi)
158 text_configuration *tp = &tps[MI_SCREEN(mi)];
160 if (tp->text) free (tp->text);
162 if (!text_fmt || !*text_fmt || !strcmp(text_fmt, "(default)"))
167 if (uname (&uts) < 0)
169 tp->text = strdup("uname() failed");
174 if ((s = strchr(uts.nodename, '.')))
176 tp->text = (char *) malloc(strlen(uts.nodename) +
177 strlen(uts.sysname) +
178 strlen(uts.version) +
179 strlen(uts.release) + 10);
181 sprintf(tp->text, "%s\n%s %s.%s",
182 uts.nodename, uts.sysname, uts.version, uts.release);
184 sprintf(tp->text, "%s\n%s %s",
185 uts.nodename, uts.sysname, uts.release);
188 # else /* !HAVE_UNAME */
190 tp->text = strdup(getenv("SYS$NODE"));
192 tp->text = strdup("* *\n* * *\nxscreensaver\n* * *\n* *");
194 # endif /* !HAVE_UNAME */
196 else if (!strchr (text_fmt, '%'))
198 tp->text = strdup (text_fmt);
202 time_t now = time ((time_t *) 0);
203 struct tm *tm = localtime (&now);
204 int L = strlen(text_fmt) + 100;
205 tp->text = (char *) malloc (L);
207 strftime (tp->text, L-1, text_fmt, tm);
209 sprintf (tp->text, "strftime error:\n%s", text_fmt);
215 text_handle_event (ModeInfo *mi, XEvent *event)
217 text_configuration *tp = &tps[MI_SCREEN(mi)];
219 if (event->xany.type == ButtonPress &&
220 event->xbutton.button & Button1)
222 tp->button_down_p = True;
223 gltrackball_start (tp->trackball,
224 event->xbutton.x, event->xbutton.y,
225 MI_WIDTH (mi), MI_HEIGHT (mi));
228 else if (event->xany.type == ButtonRelease &&
229 event->xbutton.button & Button1)
231 tp->button_down_p = False;
234 else if (event->xany.type == MotionNotify &&
237 gltrackball_track (tp->trackball,
238 event->xmotion.x, event->xmotion.y,
239 MI_WIDTH (mi), MI_HEIGHT (mi));
248 init_text (ModeInfo *mi)
250 text_configuration *tp;
254 tps = (text_configuration *)
255 calloc (MI_NUM_SCREENS(mi), sizeof (text_configuration));
257 fprintf(stderr, "%s: out of memory\n", progname);
261 tp = &tps[MI_SCREEN(mi)];
264 tp = &tps[MI_SCREEN(mi)];
266 if ((tp->glx_context = init_GL(mi)) != NULL) {
268 reshape_text (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
272 double spin_speed = 1.0;
273 double wander_speed = 0.05;
274 double spin_accel = 1.0;
276 tp->rot = make_rotator (do_spin ? spin_speed : 0,
277 do_spin ? spin_speed : 0,
278 do_spin ? spin_speed : 0,
280 do_wander ? wander_speed : 0,
282 tp->trackball = gltrackball_init ();
286 tp->colors = (XColor *) calloc(tp->ncolors, sizeof(XColor));
287 make_smooth_colormap (0, 0, 0,
288 tp->colors, &tp->ncolors,
291 /* brighter colors, please... */
292 for (i = 0; i < tp->ncolors; i++)
294 tp->colors[i].red = (tp->colors[i].red / 2) + 32767;
295 tp->colors[i].green = (tp->colors[i].green / 2) + 32767;
296 tp->colors[i].blue = (tp->colors[i].blue / 2) + 32767;
305 fill_character (GLUTstrokeFont font, int c, Bool wire)
307 GLfloat tube_width = 10;
309 const StrokeCharRec *ch;
310 const StrokeRec *stroke;
311 const CoordRec *coord;
312 StrokeFontPtr fontinfo;
315 fontinfo = (StrokeFontPtr) font;
317 if (c < 0 || c >= fontinfo->num_chars)
319 ch = &(fontinfo->ch[c]);
323 for (i = ch->num_strokes, stroke = ch->stroke;
324 i > 0; i--, stroke++) {
325 for (j = stroke->num_coords, coord = stroke->coord;
333 if (j != stroke->num_coords)
335 coord->x, coord->y, 0,
338 TUBE_FACES, smooth, wire);
343 return (int) (ch->right + tube_width/2);
350 text_extents (const char *string, int *wP, int *hP)
352 const char *s, *start;
353 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
360 if (*s == '\n' || *s == 0)
365 w += glutStrokeWidth(GLUT_FONT, *start);
370 if (w > *wP) *wP = w;
384 fill_string (const char *string, Bool wire)
386 const char *s, *start;
387 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
389 GLfloat x = 0, y = 0;
393 lines = text_extents (string, &ow, &oh);
395 y = oh / 2 - line_height;
400 if (*s == '\n' || *s == 0)
404 const char *lstart = start;
405 const char *lend = s;
407 /* strip off whitespace at beginning and end of line
408 (since we're centering.) */
409 while (lend > lstart && isspace(lend[-1]))
411 while (lstart < lend && isspace(*lstart))
414 for (s2 = lstart; s2 < lend; s2++)
415 line_w += glutStrokeWidth (GLUT_FONT, *s2);
417 x = (-ow/2) + ((ow-line_w)/2);
418 for (s2 = lstart; s2 < lend; s2++)
421 glTranslatef(x, y, 0);
422 off = fill_character (GLUT_FONT, *s2, wire);
439 draw_text (ModeInfo *mi)
441 text_configuration *tp = &tps[MI_SCREEN(mi)];
442 Display *dpy = MI_DISPLAY(mi);
443 Window window = MI_WINDOW(mi);
444 int wire = MI_IS_WIREFRAME(mi);
446 static GLfloat color[4] = {0.0, 0.0, 0.0, 1.0};
447 static GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
449 if (!tp->glx_context)
452 if (strchr (text_fmt, '%'))
454 static time_t last_update = -1;
455 time_t now = time ((time_t *) 0);
456 if (now != last_update) /* do it once a second */
461 glShadeModel(GL_SMOOTH);
463 glEnable(GL_DEPTH_TEST);
464 glEnable(GL_NORMALIZE);
465 glEnable(GL_CULL_FACE);
467 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
471 glScalef(1.1, 1.1, 1.1);
475 get_position (tp->rot, &x, &y, &z, !tp->button_down_p);
476 glTranslatef((x - 0.5) * 8,
480 gltrackball_rotate (tp->trackball);
482 get_rotation (tp->rot, &x, &y, &z, !tp->button_down_p);
483 glRotatef (x * 360, 1.0, 0.0, 0.0);
484 glRotatef (y * 360, 0.0, 1.0, 0.0);
485 glRotatef (z * 360, 0.0, 0.0, 1.0);
491 color[0] = tp->colors[tp->ccolor].red / 65536.0;
492 color[1] = tp->colors[tp->ccolor].green / 65536.0;
493 color[2] = tp->colors[tp->ccolor].blue / 65536.0;
495 if (tp->ccolor >= tp->ncolors) tp->ccolor = 0;
497 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
499 glScalef(0.01, 0.01, 0.01);
501 fill_string(tp->text, wire);
505 if (mi->fps_p) do_fps (mi);
508 glXSwapBuffers(dpy, window);