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 release_text 0
19 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
22 # define TUBE_FACES 12 /* how densely to render tubes */
29 #define countof(x) (sizeof((x))/sizeof((*x)))
31 #include "xlockmore.h"
36 #include "gltrackball.h"
37 #include "textclient.h"
42 #ifdef USE_GL /* whole file */
44 #define DEF_TEXT "(default)"
45 #define DEF_PROGRAM "(default)"
46 #define DEF_SPIN "XYZ"
47 #define DEF_WANDER "True"
48 #define DEF_FACE_FRONT "True"
51 # include <sys/utsname.h>
52 #endif /* HAVE_UNAME */
54 #include "glutstroke.h"
55 #include "glut_roman.h"
56 #define GLUT_FONT (&glutStrokeRoman)
60 GLXContext *glx_context;
62 trackball_state *trackball;
64 Bool spinx, spiny, spinz;
80 static text_configuration *tps = NULL;
82 static char *text_fmt;
83 static char *program_str;
85 static Bool do_wander;
86 static Bool face_front_p;
88 static XrmOptionDescRec opts[] = {
89 { "-text", ".text", XrmoptionSepArg, 0 },
90 { "-program", ".program", XrmoptionSepArg, 0 },
91 { "-spin", ".spin", XrmoptionSepArg, 0 },
92 { "+spin", ".spin", XrmoptionNoArg, "" },
93 { "-wander", ".wander", XrmoptionNoArg, "True" },
94 { "+wander", ".wander", XrmoptionNoArg, "False" },
95 { "-front", ".faceFront", XrmoptionNoArg, "True" },
96 { "+front", ".faceFront", XrmoptionNoArg, "False" }
99 static argtype vars[] = {
100 {&text_fmt, "text", "Text", DEF_TEXT, t_String},
101 {&program_str, "program", "Program", DEF_PROGRAM, t_String},
102 {&do_spin, "spin", "Spin", DEF_SPIN, t_String},
103 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
104 {&face_front_p, "faceFront", "FaceFront", DEF_FACE_FRONT, t_Bool},
107 ENTRYPOINT ModeSpecOpt text_opts = {countof(opts), opts, countof(vars), vars, NULL};
110 /* Window management, etc
113 reshape_text (ModeInfo *mi, int width, int height)
115 GLfloat h = (GLfloat) height / (GLfloat) width;
117 glViewport (0, 0, (GLint) width, (GLint) height);
119 glMatrixMode(GL_PROJECTION);
121 gluPerspective (30.0, 1/h, 1.0, 100.0);
123 glMatrixMode(GL_MODELVIEW);
125 gluLookAt( 0.0, 0.0, 30.0,
129 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
131 int o = (int) current_device_rotation();
132 if (o != 0 && o != 180 && o != -180)
133 glScalef (1/h, 1/h, 1/h);
137 glClear(GL_COLOR_BUFFER_BIT);
142 gl_init (ModeInfo *mi)
144 text_configuration *tp = &tps[MI_SCREEN(mi)];
145 int wire = MI_IS_WIREFRAME(mi);
147 static const GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
151 glLightfv(GL_LIGHT0, GL_POSITION, pos);
152 glEnable(GL_CULL_FACE);
153 glEnable(GL_LIGHTING);
155 glEnable(GL_DEPTH_TEST);
158 tp->text_list = glGenLists (1);
159 glNewList (tp->text_list, GL_COMPILE);
165 parse_text (ModeInfo *mi)
167 text_configuration *tp = &tps[MI_SCREEN(mi)];
168 char *old = tp->text;
170 if (program_str && *program_str && !!strcmp(program_str, "(default)"))
178 tp->tc = textclient_open (mi->dpy);
180 while (p < buf + sizeof(buf) - 1 &&
183 int c = textclient_getc (tp->tc);
192 if (lines == 0 && buf[0])
195 tp->text = strdup (buf);
197 tp->reload = 7; /* Let this one linger for a few seconds */
202 else if (!text_fmt || !*text_fmt || !strcmp(text_fmt, "(default)"))
207 if (uname (&uts) < 0)
209 tp->text = strdup("uname() failed");
214 if ((s = strchr(uts.nodename, '.')))
216 tp->text = (char *) malloc(strlen(uts.nodename) +
217 strlen(uts.sysname) +
218 strlen(uts.version) +
219 strlen(uts.release) + 10);
221 sprintf(tp->text, "%s\n%s %s.%s",
222 uts.nodename, uts.sysname, uts.version, uts.release);
223 # elif defined(USE_IPHONE)
224 /* "My iPhone\n iPhone4,1\n Darwin 11.0.0" */
225 sprintf(tp->text, "%s\n%s\n%s %s",
226 uts.nodename, uts.machine, uts.sysname, uts.release);
227 # elif defined(__APPLE__) /* MacOS X + XDarwin */
230 "/System/Library/CoreServices/SystemVersion.plist";
231 FILE *f = fopen (file, "r");
232 char *pbv = 0, *pn = 0, *puvv = 0;
236 while (fgets (buf, sizeof(buf)-1, f)) {
238 if (strstr(buf, S)) { \
239 fgets (buf, sizeof(buf)-1, f); \
240 if ((s = strchr (buf, '>'))) V = strdup(s+1); \
241 if ((s = strchr (V, '<'))) *s = 0; \
243 GRAB ("ProductName", pn)
244 GRAB ("ProductBuildVersion", pbv)
245 GRAB ("ProductUserVisibleVersion", puvv)
250 sprintf (tp->text, "%s\n%s\n%s\n%s",
251 uts.nodename, pn, puvv, uts.machine);
253 sprintf(tp->text, "%s\n%s %s\n%s",
254 uts.nodename, uts.sysname, uts.release, uts.machine);
257 sprintf(tp->text, "%s\n%s %s",
258 uts.nodename, uts.sysname, uts.release);
259 # endif /* special system types */
261 # else /* !HAVE_UNAME */
263 tp->text = strdup(getenv("SYS$NODE"));
265 tp->text = strdup("* *\n* * *\nxscreensaver\n* * *\n* *");
267 # endif /* !HAVE_UNAME */
269 else if (!strchr (text_fmt, '%'))
271 tp->text = strdup (text_fmt);
275 time_t now = time ((time_t *) 0);
276 struct tm *tm = localtime (&now);
277 int L = strlen(text_fmt) + 100;
278 tp->text = (char *) malloc (L);
280 strftime (tp->text, L-1, text_fmt, tm);
282 sprintf (tp->text, "strftime error:\n%s", text_fmt);
287 /* The GLUT font only has ASCII characters. */
288 char *s1 = utf8_to_latin1 (tp->text, True);
293 /* If we had text before but got no text this time, hold on to the
294 old one, to avoid flickering.
296 if (old && *old && !*tp->text)
307 text_handle_event (ModeInfo *mi, XEvent *event)
309 text_configuration *tp = &tps[MI_SCREEN(mi)];
311 if (gltrackball_event_handler (event, tp->trackball,
312 MI_WIDTH (mi), MI_HEIGHT (mi),
320 static void free_text(ModeInfo * mi);
324 init_text (ModeInfo *mi)
326 text_configuration *tp;
329 MI_INIT (mi, tps, free_text);
331 tp = &tps[MI_SCREEN(mi)];
333 if ((tp->glx_context = init_GL(mi)) != NULL) {
335 reshape_text (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
339 double spin_speed = 0.5;
340 double wander_speed = 0.02;
341 double tilt_speed = 0.03;
342 double spin_accel = 0.5;
347 if (*s == 'x' || *s == 'X') tp->spinx = True;
348 else if (*s == 'y' || *s == 'Y') tp->spiny = True;
349 else if (*s == 'z' || *s == 'Z') tp->spinz = True;
350 else if (*s == '0') ;
354 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
361 tp->rot = make_rotator (tp->spinx ? spin_speed : 0,
362 tp->spiny ? spin_speed : 0,
363 tp->spinz ? spin_speed : 0,
365 do_wander ? wander_speed : 0,
367 tp->rot2 = (face_front_p
368 ? make_rotator (0, 0, 0, 0, tilt_speed, True)
370 tp->trackball = gltrackball_init (False);
374 tp->colors = (XColor *) calloc(tp->ncolors, sizeof(XColor));
375 make_smooth_colormap (0, 0, 0,
376 tp->colors, &tp->ncolors,
379 /* brighter colors, please... */
380 for (i = 0; i < tp->ncolors; i++)
382 tp->colors[i].red = (tp->colors[i].red / 2) + 32767;
383 tp->colors[i].green = (tp->colors[i].green / 2) + 32767;
384 tp->colors[i].blue = (tp->colors[i].blue / 2) + 32767;
393 fill_character (GLUTstrokeFont font, int c, Bool wire, int *polysP)
395 GLfloat tube_width = 10;
397 const StrokeCharRec *ch;
398 const StrokeRec *stroke;
399 const CoordRec *coord;
400 StrokeFontPtr fontinfo;
403 fontinfo = (StrokeFontPtr) font;
405 if (c < 0 || c >= fontinfo->num_chars)
407 ch = &(fontinfo->ch[c]);
411 for (i = ch->num_strokes, stroke = ch->stroke;
412 i > 0; i--, stroke++) {
413 for (j = stroke->num_coords, coord = stroke->coord;
422 if (j != stroke->num_coords)
423 *polysP += tube (lx, ly, 0,
424 coord->x, coord->y, 0,
427 TUBE_FACES, smooth, False, wire);
431 /* Put a sphere at the endpoint of every line segment. Wasteful
432 on curves like "0" but necessary on corners like "4". */
436 glTranslatef (lx, ly, 0);
437 glScalef (tube_width, tube_width, tube_width);
438 *polysP += unit_sphere (TUBE_FACES, TUBE_FACES, wire);
443 return (int) (ch->right + tube_width);
450 text_extents (const char *string, int *wP, int *hP)
452 const char *s, *start;
453 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
460 if (*s == '\n' || *s == 0)
465 w += glutStrokeWidth(GLUT_FONT, *start);
470 if (w > *wP) *wP = w;
484 fill_string (const char *string, Bool wire)
487 const char *s, *start;
488 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
490 GLfloat x = 0, y = 0;
493 text_extents (string, &ow, &oh);
495 y = oh / 2 - line_height;
500 if (*s == '\n' || *s == 0)
504 const char *lstart = start;
505 const char *lend = s;
507 /* strip off whitespace at beginning and end of line
508 (since we're centering.) */
509 while (lend > lstart && isspace(lend[-1]))
511 while (lstart < lend && isspace(*lstart))
514 for (s2 = lstart; s2 < lend; s2++)
515 line_w += glutStrokeWidth (GLUT_FONT, *s2);
517 x = (-ow/2) + ((ow-line_w)/2);
518 for (s2 = lstart; s2 < lend; s2++)
521 glTranslatef(x, y, 0);
522 off = fill_character (GLUT_FONT, *s2, wire, &polys);
540 draw_text (ModeInfo *mi)
542 text_configuration *tp = &tps[MI_SCREEN(mi)];
543 Display *dpy = MI_DISPLAY(mi);
544 Window window = MI_WINDOW(mi);
545 int wire = MI_IS_WIREFRAME(mi);
547 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
548 GLfloat color[4] = {0.0, 0.0, 0.0, 1.0};
550 if (!tp->glx_context)
553 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(tp->glx_context));
557 if (time ((time_t *) 0) >= tp->last_update + tp->reload)
560 tp->last_update = time ((time_t *) 0);
564 glShadeModel(GL_SMOOTH);
566 glEnable(GL_DEPTH_TEST);
567 glEnable(GL_NORMALIZE);
568 glEnable(GL_CULL_FACE);
571 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
574 glScalef(1.1, 1.1, 1.1);
575 glRotatef(current_device_rotation(), 0, 0, 1);
579 get_position (tp->rot, &x, &y, &z, !tp->button_down_p);
580 glTranslatef((x - 0.5) * 8,
584 gltrackball_rotate (tp->trackball);
589 get_position (tp->rot2, &x, &y, &z, !tp->button_down_p);
590 if (tp->spinx) glRotatef (max/2 - x*max, 1, 0, 0);
591 if (tp->spiny) glRotatef (max/2 - y*max, 0, 1, 0);
592 if (tp->spinz) glRotatef (max/2 - z*max, 0, 0, 1);
596 get_rotation (tp->rot, &x, &y, &z, !tp->button_down_p);
597 glRotatef (x * 360, 1, 0, 0);
598 glRotatef (y * 360, 0, 1, 0);
599 glRotatef (z * 360, 0, 0, 1);
606 color[0] = tp->colors[tp->ccolor].red / 65536.0;
607 color[1] = tp->colors[tp->ccolor].green / 65536.0;
608 color[2] = tp->colors[tp->ccolor].blue / 65536.0;
610 if (tp->ccolor >= tp->ncolors) tp->ccolor = 0;
612 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
614 glScalef(0.01, 0.01, 0.01);
616 mi->polygon_count = fill_string(tp->text, wire);
620 if (mi->fps_p) do_fps (mi);
623 glXSwapBuffers(dpy, window);
627 free_text(ModeInfo * mi)
629 text_configuration *tp = &tps[MI_SCREEN(mi)];
631 textclient_close (tp->tc);
635 XSCREENSAVER_MODULE_2 ("GLText", gltext, text)