Ascender: Disable color or attributes when unsupported or asked
authorMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 15 Apr 2023 20:00:01 +0000 (20:00 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 15 Apr 2023 20:00:01 +0000 (20:00 +0000)
mmsoftware/ascender/src/main.c
mmsoftware/ascender/src/shapes.txt

index 43fe752..ae6940f 100644 (file)
@@ -65,7 +65,9 @@ typedef struct aobject {
 } aobject_t;
 
 
-int                    main(void);
+int                    main(int, char **);
+static void            usage(void);
+
 static void            shape_draw(int, int, int);
 static void            anim_init(void);
 static void            anim_redraw(void);
@@ -75,7 +77,14 @@ static void          rarray_empty(rarray_t *);
 static void            read_shapes(void);
 
 
+/* Simulate BSD set/getprogname(3) on GNU */
+static const char      *progname = "";
+
 /* Color related */
+static bool            cfg_use_color = true;
+static bool            cfg_use_attributes = true;
+static bool            use_color = false;
+static bool            use_attributes = false;
 static short           ncolpairs = -1;
 
 /* Loaded shapes */
@@ -89,8 +98,28 @@ static aobject_t     avatar = { AOT_AVATAR,
 
 
 int
-main(void)
+main(int argc, char **argv)
 {
+       int ch;
+
+       progname = strdup(argv[0]);
+       while ((ch = getopt(argc, argv, "?mp"))
+              != -1) {
+               switch (ch) {
+               case 'm':
+                       cfg_use_color = false;
+                       break;
+               case 'p':
+                       cfg_use_attributes = false;
+                       break;
+               case '?':
+               default:
+                       usage();
+               }
+       }
+       argc -= optind;
+       argv += optind;
+
        screen_init();
 
        /*
@@ -101,16 +130,21 @@ main(void)
         * enforced color pairs to really only define the foreground or
         * background color.
         */
-       (void)use_default_colors();
-       if (start_color() == ERR)
-               err(EXIT_FAILURE, "start_color()");
+       if (cfg_use_color && has_colors()) {
+               (void)use_default_colors();
+               if (start_color() == OK) {
+                       /*
+                        * Allocate a few basic pairs before loading shapes
+                        * that will also allocate new pairs as needed.
+                        */
+                       (void)init_pair(++ncolpairs, -1, -1);
+                       (void)init_pair(++ncolpairs, COLOR_WHITE, COLOR_BLACK);
 
-       /*
-        * Allocate a few basic pairs before loading shapes that will also
-        * allocate new pairs as needed.
-        */
-       (void)init_pair(++ncolpairs, -1, -1);
-       (void)init_pair(++ncolpairs, COLOR_WHITE, COLOR_BLACK);
+                       use_color = true;
+               }
+       }
+       if (cfg_use_attributes && termattrs() != 0)
+               use_attributes = true;
 
        /* Read shape definitions from file */
        read_shapes();
@@ -204,6 +238,20 @@ end:
        exit(EXIT_SUCCESS);
 }
 
+static void
+usage(void)
+{
+
+       (void)fprintf(stderr,
+           "\nUsage: %s [-m] [-p]\n\n"
+           "Where:\n"
+           " -m - Monochrome mode, do not use colors.\n"
+           " -p - Plain, do not use text attributes like bold.\n\n",
+           progname);
+
+       exit(EXIT_FAILURE);
+}
+
 /* 2d text shape blitting routine */
 static void
 shape_draw(int shape, int y, int x)
@@ -253,7 +301,8 @@ anim_redraw(void)
        int i;
 
        /* Redraw screen with all objects */
-       (void)attrset(COLOR_PAIR(0));
+       if (use_color)
+               (void)attrset(COLOR_PAIR(0));
        (void)erase();
        for (i = 0; i < NOBJECTS; i++) {
                aobject_t *o = &objects[i];
@@ -261,12 +310,14 @@ anim_redraw(void)
                if (o->t == AOT_SHAPE)
                        shape_draw(o->s, o->y, o->x);
                else {
-                       (void)attrset(COLOR_PAIR(o->col));
+                       if (use_color && o->col != -1)
+                               (void)attrset(COLOR_PAIR(o->col));
                        (void)mvaddch(o->y, o->x, o->c);
                }
        }
 
        /* Avatar */
+       /* XXX */
        (void)attr_on(A_REVERSE, NULL);
        (void)mvaddch(avatar.y, avatar.x, avatar.c);
        (void)attr_off(A_REVERSE, NULL);
@@ -390,16 +441,22 @@ read_shapes(void)
                                continue;
                        }
                        if (line[0] == 'F' && line[1] == ' ') {
+                               if (!use_color)
+                                       continue;
                                rarray_append(&flines,
                                    (intptr_t)strdup(&line[2]));
                                continue;
                        }
                        if (line[0] == 'B' && line[1] == ' ') {
+                               if (!use_color)
+                                       continue;
                                rarray_append(&blines,
                                    (intptr_t)strdup(&line[2]));
                                continue;
                        }
                        if (line[0] == 'A' && line[1] == ' ') {
+                               if (!use_attributes)
+                                       continue;
                                rarray_append(&alines,
                                    (intptr_t)strdup(&line[2]));
                                continue;
@@ -434,14 +491,15 @@ read_shapes(void)
                (void)werase(pad);
 
                /* Fill with characters marked with attributes as needed */
-               /* XXX Also avoid to apply colors or attributes depending on
-                * options like mono/nocolor */
                for (y = 0; y < tlines.count; y++) {
                        for (len = strlen((const char *)tlines.array[y]),
                            x = 0; x < len; x++) {
                                chtype ch;
                                int fg = -1, bg = -1;
                                char *cptr, c;
+                               int supattr;
+                               
+                               supattr = termattrs();
 
                                cptr = (char *)tlines.array[y];
                                ch = cptr[x];
@@ -468,16 +526,26 @@ read_shapes(void)
                                                c = cptr[x];
                                                switch (c) {
                                                case 'E':
-                                                       ch |= A_BOLD;
+                                                       if ((supattr & A_BOLD)
+                                                           != 0)
+                                                               ch |= A_BOLD;
                                                        break;
                                                case 'I':
-                                                       ch |= A_REVERSE;
+                                                       if ((supattr &
+                                                            A_REVERSE) != 0)
+                                                               ch |=
+                                                                   A_REVERSE;
                                                        break;
                                                case 'U':
-                                                       ch |= A_UNDERLINE;
+                                                       if ((supattr &
+                                                            A_UNDERLINE) != 0)
+                                                               ch |=
+                                                                  A_UNDERLINE;
                                                        break;
                                                case 'B':
-                                                       ch |= A_BLINK;
+                                                       if ((supattr & A_BLINK)
+                                                           != 0)
+                                                               ch |= A_BLINK;
                                                        break;
                                                }
                                        }
@@ -506,6 +574,12 @@ read_shapes(void)
                                        }
                                        ch |= COLOR_PAIR(p);
                                }
+                               /*
+                                * If color is used disable the use of
+                                * attributes that are incompatible with it
+                                */
+                               if (fg != -1 || bg != -1)
+                                       ch &= ~(no_color_attributes());
 
                                /* Finally add character to pad */
                                (void)mvwaddch(pad, y, x, ch);
index 874de03..a5990d3 100644 (file)
@@ -54,7 +54,7 @@ B
 B                            
 B                             
 B                           
-B   000000 000 00 000 00 
+B   444444 444 44 444 44 
 B                      
 A                   B
 A