1 /* gltext, Copyright (c) 2001-2014 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 #define DEFAULTS "*delay: 20000 \n" \
13 "*showFPS: False \n" \
14 "*wireframe: False \n" \
17 # define refresh_text 0
18 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
21 # define TUBE_FACES 12 /* how densely to render tubes */
28 #define countof(x) (sizeof((x))/sizeof((*x)))
30 #include "xlockmore.h"
35 #include "gltrackball.h"
36 #include "textclient.h"
41 #ifdef USE_GL /* whole file */
43 #define DEF_TEXT "(default)"
44 #define DEF_PROGRAM "(default)"
45 #define DEF_SPIN "XYZ"
46 #define DEF_WANDER "True"
47 #define DEF_FACE_FRONT "True"
50 # include <sys/utsname.h>
51 #endif /* HAVE_UNAME */
53 #include "glutstroke.h"
54 #include "glut_roman.h"
55 #define GLUT_FONT (&glutStrokeRoman)
59 GLXContext *glx_context;
61 trackball_state *trackball;
63 Bool spinx, spiny, spinz;
79 static text_configuration *tps = NULL;
81 static char *text_fmt;
82 static char *program_str;
84 static Bool do_wander;
85 static Bool face_front_p;
87 static XrmOptionDescRec opts[] = {
88 { "-text", ".text", XrmoptionSepArg, 0 },
89 { "-program", ".program", XrmoptionSepArg, 0 },
90 { "-spin", ".spin", XrmoptionSepArg, 0 },
91 { "+spin", ".spin", XrmoptionNoArg, "" },
92 { "-wander", ".wander", XrmoptionNoArg, "True" },
93 { "+wander", ".wander", XrmoptionNoArg, "False" },
94 { "-front", ".faceFront", XrmoptionNoArg, "True" },
95 { "+front", ".faceFront", XrmoptionNoArg, "False" }
98 static argtype vars[] = {
99 {&text_fmt, "text", "Text", DEF_TEXT, t_String},
100 {&program_str, "program", "Program", DEF_PROGRAM, t_String},
101 {&do_spin, "spin", "Spin", DEF_SPIN, t_String},
102 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
103 {&face_front_p, "faceFront", "FaceFront", DEF_FACE_FRONT, t_Bool},
106 ENTRYPOINT ModeSpecOpt text_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 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
130 int o = (int) current_device_rotation();
131 if (o != 0 && o != 180 && o != -180)
132 glScalef (1/h, 1/h, 1/h);
136 glClear(GL_COLOR_BUFFER_BIT);
141 gl_init (ModeInfo *mi)
143 text_configuration *tp = &tps[MI_SCREEN(mi)];
144 int wire = MI_IS_WIREFRAME(mi);
146 static const GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
150 glLightfv(GL_LIGHT0, GL_POSITION, pos);
151 glEnable(GL_CULL_FACE);
152 glEnable(GL_LIGHTING);
154 glEnable(GL_DEPTH_TEST);
157 tp->text_list = glGenLists (1);
158 glNewList (tp->text_list, GL_COMPILE);
164 parse_text (ModeInfo *mi)
166 text_configuration *tp = &tps[MI_SCREEN(mi)];
167 char *old = tp->text;
169 if (program_str && *program_str && !!strcmp(program_str, "(default)"))
177 tp->tc = textclient_open (mi->dpy);
179 while (p < buf + sizeof(buf) - 1 &&
182 int c = textclient_getc (tp->tc);
191 if (lines == 0 && buf[0])
194 tp->text = strdup (buf);
196 tp->reload = 7; /* Let this one linger for a few seconds */
201 else if (!text_fmt || !*text_fmt || !strcmp(text_fmt, "(default)"))
206 if (uname (&uts) < 0)
208 tp->text = strdup("uname() failed");
213 if ((s = strchr(uts.nodename, '.')))
215 tp->text = (char *) malloc(strlen(uts.nodename) +
216 strlen(uts.sysname) +
217 strlen(uts.version) +
218 strlen(uts.release) + 10);
220 sprintf(tp->text, "%s\n%s %s.%s",
221 uts.nodename, uts.sysname, uts.version, uts.release);
222 # elif defined(USE_IPHONE)
223 /* "My iPhone\n iPhone4,1\n Darwin 11.0.0" */
224 sprintf(tp->text, "%s\n%s\n%s %s",
225 uts.nodename, uts.machine, uts.sysname, uts.release);
226 # elif defined(__APPLE__) /* MacOS X + XDarwin */
229 "/System/Library/CoreServices/SystemVersion.plist";
230 FILE *f = fopen (file, "r");
231 char *pbv = 0, *pn = 0, *puvv = 0;
235 while (fgets (buf, sizeof(buf)-1, f)) {
237 if (strstr(buf, S)) { \
238 fgets (buf, sizeof(buf)-1, f); \
239 if ((s = strchr (buf, '>'))) V = strdup(s+1); \
240 if ((s = strchr (V, '<'))) *s = 0; \
242 GRAB ("ProductName", pn)
243 GRAB ("ProductBuildVersion", pbv)
244 GRAB ("ProductUserVisibleVersion", puvv)
249 sprintf (tp->text, "%s\n%s\n%s\n%s",
250 uts.nodename, pn, puvv, uts.machine);
252 sprintf(tp->text, "%s\n%s %s\n%s",
253 uts.nodename, uts.sysname, uts.release, uts.machine);
256 sprintf(tp->text, "%s\n%s %s",
257 uts.nodename, uts.sysname, uts.release);
258 # endif /* special system types */
260 # else /* !HAVE_UNAME */
262 tp->text = strdup(getenv("SYS$NODE"));
264 tp->text = strdup("* *\n* * *\nxscreensaver\n* * *\n* *");
266 # endif /* !HAVE_UNAME */
268 else if (!strchr (text_fmt, '%'))
270 tp->text = strdup (text_fmt);
274 time_t now = time ((time_t *) 0);
275 struct tm *tm = localtime (&now);
276 int L = strlen(text_fmt) + 100;
277 tp->text = (char *) malloc (L);
279 strftime (tp->text, L-1, text_fmt, tm);
281 sprintf (tp->text, "strftime error:\n%s", text_fmt);
286 /* The GLUT font only has ASCII characters. */
287 char *s1 = utf8_to_latin1 (tp->text, True);
292 /* If we had text before but got no text this time, hold on to the
293 old one, to avoid flickering.
295 if (old && *old && !*tp->text)
306 text_handle_event (ModeInfo *mi, XEvent *event)
308 text_configuration *tp = &tps[MI_SCREEN(mi)];
310 if (gltrackball_event_handler (event, tp->trackball,
311 MI_WIDTH (mi), MI_HEIGHT (mi),
320 init_text (ModeInfo *mi)
322 text_configuration *tp;
326 tps = (text_configuration *)
327 calloc (MI_NUM_SCREENS(mi), sizeof (text_configuration));
329 fprintf(stderr, "%s: out of memory\n", progname);
334 tp = &tps[MI_SCREEN(mi)];
336 if ((tp->glx_context = init_GL(mi)) != NULL) {
338 reshape_text (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
342 double spin_speed = 0.5;
343 double wander_speed = 0.02;
344 double tilt_speed = 0.03;
345 double spin_accel = 0.5;
350 if (*s == 'x' || *s == 'X') tp->spinx = True;
351 else if (*s == 'y' || *s == 'Y') tp->spiny = True;
352 else if (*s == 'z' || *s == 'Z') tp->spinz = True;
353 else if (*s == '0') ;
357 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
364 tp->rot = make_rotator (tp->spinx ? spin_speed : 0,
365 tp->spiny ? spin_speed : 0,
366 tp->spinz ? spin_speed : 0,
368 do_wander ? wander_speed : 0,
370 tp->rot2 = (face_front_p
371 ? make_rotator (0, 0, 0, 0, tilt_speed, True)
373 tp->trackball = gltrackball_init (False);
377 tp->colors = (XColor *) calloc(tp->ncolors, sizeof(XColor));
378 make_smooth_colormap (0, 0, 0,
379 tp->colors, &tp->ncolors,
382 /* brighter colors, please... */
383 for (i = 0; i < tp->ncolors; i++)
385 tp->colors[i].red = (tp->colors[i].red / 2) + 32767;
386 tp->colors[i].green = (tp->colors[i].green / 2) + 32767;
387 tp->colors[i].blue = (tp->colors[i].blue / 2) + 32767;
396 fill_character (GLUTstrokeFont font, int c, Bool wire, int *polysP)
398 GLfloat tube_width = 10;
400 const StrokeCharRec *ch;
401 const StrokeRec *stroke;
402 const CoordRec *coord;
403 StrokeFontPtr fontinfo;
406 fontinfo = (StrokeFontPtr) font;
408 if (c < 0 || c >= fontinfo->num_chars)
410 ch = &(fontinfo->ch[c]);
414 for (i = ch->num_strokes, stroke = ch->stroke;
415 i > 0; i--, stroke++) {
416 for (j = stroke->num_coords, coord = stroke->coord;
425 if (j != stroke->num_coords)
426 *polysP += tube (lx, ly, 0,
427 coord->x, coord->y, 0,
430 TUBE_FACES, smooth, False, wire);
434 /* Put a sphere at the endpoint of every line segment. Wasteful
435 on curves like "0" but necessary on corners like "4". */
439 glTranslatef (lx, ly, 0);
440 glScalef (tube_width, tube_width, tube_width);
441 *polysP += unit_sphere (TUBE_FACES, TUBE_FACES, wire);
446 return (int) (ch->right + tube_width);
453 text_extents (const char *string, int *wP, int *hP)
455 const char *s, *start;
456 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
463 if (*s == '\n' || *s == 0)
468 w += glutStrokeWidth(GLUT_FONT, *start);
473 if (w > *wP) *wP = w;
487 fill_string (const char *string, Bool wire)
490 const char *s, *start;
491 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
493 GLfloat x = 0, y = 0;
496 text_extents (string, &ow, &oh);
498 y = oh / 2 - line_height;
503 if (*s == '\n' || *s == 0)
507 const char *lstart = start;
508 const char *lend = s;
510 /* strip off whitespace at beginning and end of line
511 (since we're centering.) */
512 while (lend > lstart && isspace(lend[-1]))
514 while (lstart < lend && isspace(*lstart))
517 for (s2 = lstart; s2 < lend; s2++)
518 line_w += glutStrokeWidth (GLUT_FONT, *s2);
520 x = (-ow/2) + ((ow-line_w)/2);
521 for (s2 = lstart; s2 < lend; s2++)
524 glTranslatef(x, y, 0);
525 off = fill_character (GLUT_FONT, *s2, wire, &polys);
543 draw_text (ModeInfo *mi)
545 text_configuration *tp = &tps[MI_SCREEN(mi)];
546 Display *dpy = MI_DISPLAY(mi);
547 Window window = MI_WINDOW(mi);
548 int wire = MI_IS_WIREFRAME(mi);
550 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
551 GLfloat color[4] = {0.0, 0.0, 0.0, 1.0};
553 if (!tp->glx_context)
556 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(tp->glx_context));
560 if (time ((time_t *) 0) >= tp->last_update + tp->reload)
563 tp->last_update = time ((time_t *) 0);
567 glShadeModel(GL_SMOOTH);
569 glEnable(GL_DEPTH_TEST);
570 glEnable(GL_NORMALIZE);
571 glEnable(GL_CULL_FACE);
574 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
577 glScalef(1.1, 1.1, 1.1);
578 glRotatef(current_device_rotation(), 0, 0, 1);
582 get_position (tp->rot, &x, &y, &z, !tp->button_down_p);
583 glTranslatef((x - 0.5) * 8,
587 gltrackball_rotate (tp->trackball);
592 get_position (tp->rot2, &x, &y, &z, !tp->button_down_p);
593 if (tp->spinx) glRotatef (max/2 - x*max, 1, 0, 0);
594 if (tp->spiny) glRotatef (max/2 - y*max, 0, 1, 0);
595 if (tp->spinz) glRotatef (max/2 - z*max, 0, 0, 1);
599 get_rotation (tp->rot, &x, &y, &z, !tp->button_down_p);
600 glRotatef (x * 360, 1, 0, 0);
601 glRotatef (y * 360, 0, 1, 0);
602 glRotatef (z * 360, 0, 0, 1);
609 color[0] = tp->colors[tp->ccolor].red / 65536.0;
610 color[1] = tp->colors[tp->ccolor].green / 65536.0;
611 color[2] = tp->colors[tp->ccolor].blue / 65536.0;
613 if (tp->ccolor >= tp->ncolors) tp->ccolor = 0;
615 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
617 glScalef(0.01, 0.01, 0.01);
619 mi->polygon_count = fill_string(tp->text, wire);
623 if (mi->fps_p) do_fps (mi);
626 glXSwapBuffers(dpy, window);
630 release_text(ModeInfo * mi)
635 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
637 text_configuration *tp = &tps[MI_SCREEN(mi)];
639 textclient_close (tp->tc);
648 XSCREENSAVER_MODULE_2 ("GLText", gltext, text)