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 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 const 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)];
159 char *old = tp->text;
161 if (program_str && *program_str && !!strcmp(program_str, "(default)"))
169 tp->tc = textclient_open (mi->dpy);
171 while (p < buf + sizeof(buf) - 1 &&
174 int c = textclient_getc (tp->tc);
183 if (lines == 0 && buf[0])
186 tp->text = strdup (buf);
188 tp->reload = 7; /* Let this one linger for a few seconds */
193 else if (!text_fmt || !*text_fmt || !strcmp(text_fmt, "(default)"))
198 if (uname (&uts) < 0)
200 tp->text = strdup("uname() failed");
205 if ((s = strchr(uts.nodename, '.')))
207 tp->text = (char *) malloc(strlen(uts.nodename) +
208 strlen(uts.sysname) +
209 strlen(uts.version) +
210 strlen(uts.release) + 10);
212 sprintf(tp->text, "%s\n%s %s.%s",
213 uts.nodename, uts.sysname, uts.version, uts.release);
214 # elif defined(USE_IPHONE)
215 /* "My iPhone\n iPhone4,1\n Darwin 11.0.0" */
216 sprintf(tp->text, "%s\n%s\n%s %s",
217 uts.nodename, uts.machine, uts.sysname, uts.release);
218 # elif defined(__APPLE__) /* MacOS X + XDarwin */
221 "/System/Library/CoreServices/SystemVersion.plist";
222 FILE *f = fopen (file, "r");
223 char *pbv = 0, *pn = 0, *puvv = 0;
227 while (fgets (buf, sizeof(buf)-1, f)) {
229 if (strstr(buf, S)) { \
230 fgets (buf, sizeof(buf)-1, f); \
231 if ((s = strchr (buf, '>'))) V = strdup(s+1); \
232 if ((s = strchr (V, '<'))) *s = 0; \
234 GRAB ("ProductName", pn)
235 GRAB ("ProductBuildVersion", pbv)
236 GRAB ("ProductUserVisibleVersion", puvv)
241 sprintf (tp->text, "%s\n%s\n%s\n%s",
242 uts.nodename, pn, puvv, uts.machine);
244 sprintf(tp->text, "%s\n%s %s\n%s",
245 uts.nodename, uts.sysname, uts.release, uts.machine);
248 sprintf(tp->text, "%s\n%s %s",
249 uts.nodename, uts.sysname, uts.release);
250 # endif /* special system types */
252 # else /* !HAVE_UNAME */
254 tp->text = strdup(getenv("SYS$NODE"));
256 tp->text = strdup("* *\n* * *\nxscreensaver\n* * *\n* *");
258 # endif /* !HAVE_UNAME */
260 else if (!strchr (text_fmt, '%'))
262 tp->text = strdup (text_fmt);
266 time_t now = time ((time_t *) 0);
267 struct tm *tm = localtime (&now);
268 int L = strlen(text_fmt) + 100;
269 tp->text = (char *) malloc (L);
271 strftime (tp->text, L-1, text_fmt, tm);
273 sprintf (tp->text, "strftime error:\n%s", text_fmt);
278 /* The GLUT font only has ASCII characters. */
279 char *s1 = utf8_to_latin1 (tp->text, True);
284 /* If we had text before but got no text this time, hold on to the
285 old one, to avoid flickering.
287 if (old && *old && !*tp->text)
298 text_handle_event (ModeInfo *mi, XEvent *event)
300 text_configuration *tp = &tps[MI_SCREEN(mi)];
302 if (gltrackball_event_handler (event, tp->trackball,
303 MI_WIDTH (mi), MI_HEIGHT (mi),
312 init_text (ModeInfo *mi)
314 text_configuration *tp;
318 tps = (text_configuration *)
319 calloc (MI_NUM_SCREENS(mi), sizeof (text_configuration));
321 fprintf(stderr, "%s: out of memory\n", progname);
326 tp = &tps[MI_SCREEN(mi)];
328 if ((tp->glx_context = init_GL(mi)) != NULL) {
330 reshape_text (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
334 double spin_speed = 0.5;
335 double wander_speed = 0.02;
336 double tilt_speed = 0.03;
337 double spin_accel = 0.5;
342 if (*s == 'x' || *s == 'X') tp->spinx = True;
343 else if (*s == 'y' || *s == 'Y') tp->spiny = True;
344 else if (*s == 'z' || *s == 'Z') tp->spinz = True;
345 else if (*s == '0') ;
349 "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
356 tp->rot = make_rotator (tp->spinx ? spin_speed : 0,
357 tp->spiny ? spin_speed : 0,
358 tp->spinz ? spin_speed : 0,
360 do_wander ? wander_speed : 0,
362 tp->rot2 = (face_front_p
363 ? make_rotator (0, 0, 0, 0, tilt_speed, True)
365 tp->trackball = gltrackball_init (False);
369 tp->colors = (XColor *) calloc(tp->ncolors, sizeof(XColor));
370 make_smooth_colormap (0, 0, 0,
371 tp->colors, &tp->ncolors,
374 /* brighter colors, please... */
375 for (i = 0; i < tp->ncolors; i++)
377 tp->colors[i].red = (tp->colors[i].red / 2) + 32767;
378 tp->colors[i].green = (tp->colors[i].green / 2) + 32767;
379 tp->colors[i].blue = (tp->colors[i].blue / 2) + 32767;
388 fill_character (GLUTstrokeFont font, int c, Bool wire, int *polysP)
390 GLfloat tube_width = 10;
392 const StrokeCharRec *ch;
393 const StrokeRec *stroke;
394 const CoordRec *coord;
395 StrokeFontPtr fontinfo;
398 fontinfo = (StrokeFontPtr) font;
400 if (c < 0 || c >= fontinfo->num_chars)
402 ch = &(fontinfo->ch[c]);
406 for (i = ch->num_strokes, stroke = ch->stroke;
407 i > 0; i--, stroke++) {
408 for (j = stroke->num_coords, coord = stroke->coord;
417 if (j != stroke->num_coords)
418 *polysP += tube (lx, ly, 0,
419 coord->x, coord->y, 0,
422 TUBE_FACES, smooth, False, wire);
426 /* Put a sphere at the endpoint of every line segment. Wasteful
427 on curves like "0" but necessary on corners like "4". */
431 glTranslatef (lx, ly, 0);
432 glScalef (tube_width, tube_width, tube_width);
433 *polysP += unit_sphere (TUBE_FACES, TUBE_FACES, wire);
438 return (int) (ch->right + tube_width);
445 text_extents (const char *string, int *wP, int *hP)
447 const char *s, *start;
448 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
455 if (*s == '\n' || *s == 0)
460 w += glutStrokeWidth(GLUT_FONT, *start);
465 if (w > *wP) *wP = w;
479 fill_string (const char *string, Bool wire)
482 const char *s, *start;
483 int line_height = GLUT_FONT->top - GLUT_FONT->bottom;
485 GLfloat x = 0, y = 0;
488 text_extents (string, &ow, &oh);
490 y = oh / 2 - line_height;
495 if (*s == '\n' || *s == 0)
499 const char *lstart = start;
500 const char *lend = s;
502 /* strip off whitespace at beginning and end of line
503 (since we're centering.) */
504 while (lend > lstart && isspace(lend[-1]))
506 while (lstart < lend && isspace(*lstart))
509 for (s2 = lstart; s2 < lend; s2++)
510 line_w += glutStrokeWidth (GLUT_FONT, *s2);
512 x = (-ow/2) + ((ow-line_w)/2);
513 for (s2 = lstart; s2 < lend; s2++)
516 glTranslatef(x, y, 0);
517 off = fill_character (GLUT_FONT, *s2, wire, &polys);
535 draw_text (ModeInfo *mi)
537 text_configuration *tp = &tps[MI_SCREEN(mi)];
538 Display *dpy = MI_DISPLAY(mi);
539 Window window = MI_WINDOW(mi);
540 int wire = MI_IS_WIREFRAME(mi);
542 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
543 GLfloat color[4] = {0.0, 0.0, 0.0, 1.0};
545 if (!tp->glx_context)
548 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(tp->glx_context));
552 if (time ((time_t *) 0) >= tp->last_update + tp->reload)
555 tp->last_update = time ((time_t *) 0);
559 glShadeModel(GL_SMOOTH);
561 glEnable(GL_DEPTH_TEST);
562 glEnable(GL_NORMALIZE);
563 glEnable(GL_CULL_FACE);
566 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
569 glScalef(1.1, 1.1, 1.1);
570 glRotatef(current_device_rotation(), 0, 0, 1);
574 get_position (tp->rot, &x, &y, &z, !tp->button_down_p);
575 glTranslatef((x - 0.5) * 8,
579 gltrackball_rotate (tp->trackball);
584 get_position (tp->rot2, &x, &y, &z, !tp->button_down_p);
585 if (tp->spinx) glRotatef (max/2 - x*max, 1, 0, 0);
586 if (tp->spiny) glRotatef (max/2 - y*max, 0, 1, 0);
587 if (tp->spinz) glRotatef (max/2 - z*max, 0, 0, 1);
591 get_rotation (tp->rot, &x, &y, &z, !tp->button_down_p);
592 glRotatef (x * 360, 1, 0, 0);
593 glRotatef (y * 360, 0, 1, 0);
594 glRotatef (z * 360, 0, 0, 1);
601 color[0] = tp->colors[tp->ccolor].red / 65536.0;
602 color[1] = tp->colors[tp->ccolor].green / 65536.0;
603 color[2] = tp->colors[tp->ccolor].blue / 65536.0;
605 if (tp->ccolor >= tp->ncolors) tp->ccolor = 0;
607 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
609 glScalef(0.01, 0.01, 0.01);
611 mi->polygon_count = fill_string(tp->text, wire);
615 if (mi->fps_p) do_fps (mi);
618 glXSwapBuffers(dpy, window);
622 release_text(ModeInfo * mi)
627 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
629 text_configuration *tp = &tps[MI_SCREEN(mi)];
631 textclient_close (tp->tc);
640 XSCREENSAVER_MODULE_2 ("GLText", gltext, text)