Browse Source

more attributes

Olivier Marty 9 years ago
parent
commit
d291b37086
3 changed files with 31 additions and 20 deletions
  1. 16 4
      main.c
  2. 12 14
      printer.c
  3. 3 2
      printer.h

+ 16 - 4
main.c

@@ -30,6 +30,8 @@ void displayUsage(char *name)
   printf("  -s sec\t: skip the first x seconds\n");
   printf("  -d sec\t: wait x seconds before starting (default : 5)\n");
   printf("  -m px\t\t: margin with the bottom of the screen\n");
+  printf("  -p px\t\t: padding of the box\n");
+  printf("  -g px\t\t: gap between two lines\n");
   printf("  -f fontname\t: name of the font to use\n");
   printf("  -i fontname\t: name of the italic font to use\n");
   printf("  -b fontname\t: name of the bold font to use\n");
@@ -39,13 +41,13 @@ void displayUsage(char *name)
 
 int main(int argc, char **argv)
 {
-  int i, shift = 0, delay = 5, margin_bottom = 50;
+  int i, shift = 0, delay = 5, margin_bottom = 50, padding = 5, gap = 5;
   char *font = NULL, *font_i = NULL, *font_b = NULL, *font_bi = NULL;
   FILE *f = NULL;
   
   // parse arguments
   int c;
-  while((c = getopt (argc, argv, "s:d:m:f:i:b:j:h")) != -1)
+  while((c = getopt (argc, argv, "s:d:m:p:g:f:i:b:j:h")) != -1)
     switch(c)
     {
       case 's':
@@ -57,6 +59,12 @@ int main(int argc, char **argv)
       case 'm':
         margin_bottom = atoi(optarg);
         break;
+      case 'p':
+        padding = atoi(optarg);
+        break;
+      case 'g':
+        gap = atoi(optarg);
+        break;
       case 'f':
         font = malloc(strlen(optarg)+1);
         strcpy(font, optarg);
@@ -96,8 +104,12 @@ int main(int argc, char **argv)
   }
   
   // open the window
-  struct printerEnv penv = printerOpenWindow(margin_bottom, font, font_i,
-    font_b, font_bi);
+  struct printerEnv penv = printerOpenWindow(font, font_i, font_b, font_bi);
+  
+  // set attributes
+  penv.margin_bottom = margin_bottom;
+  penv.padding = padding;
+  penv.gap = gap;
   
   // show a counter before start the clock
   for(i = delay; i > 0; i--)

+ 12 - 14
printer.c

@@ -61,8 +61,8 @@ void loadFont(struct printerEnv *env, XFontStruct **font, char* fontname)
   }
 }
 
-struct printerEnv printerOpenWindow(int margin_bottom, char *font, char *font_i,
-  char *font_b, char *font_bi)
+struct printerEnv printerOpenWindow(char *font, char *font_i, char *font_b,
+  char *font_bi)
 {
   struct printerEnv env;
 
@@ -80,6 +80,8 @@ struct printerEnv printerOpenWindow(int margin_bottom, char *font, char *font_i,
   // size of the screen
   XWindowAttributes RootAttr;
   XGetWindowAttributes(env.d, RootWindow(env.d, env.s), &RootAttr);
+  env.root_width = RootAttr.width;
+  env.root_height = RootAttr.height;
   
   // set window attributes
   XSetWindowAttributes attr;
@@ -99,9 +101,6 @@ struct printerEnv printerOpenWindow(int margin_bottom, char *font, char *font_i,
   //env.color_background = a*256*256*256 + r*256*256 + g*256 + b;
   
   // create the window
-  env.margin_bottom = margin_bottom;
-  env.root_width = RootAttr.width;
-  env.root_height = RootAttr.height;
   env.width = 1;
   env.height = 1;
   env.w = XCreateWindow(env.d, RootWindow(env.d, env.s), 0, 0, env.width,
@@ -272,8 +271,8 @@ int drawText(struct printerEnv env, char *text, int size,
 }
 
 // return a the max width
-int printLines(struct printerEnv env, char *text, int gap, int y,
-  enum t_type flags, int draw)
+int printLines(struct printerEnv env, char *text, int y, enum t_type flags,
+  int draw)
 {
   char *next;
   int size;
@@ -305,8 +304,8 @@ int printLines(struct printerEnv env, char *text, int gap, int y,
   
   // print next lines
   if(*next != '\0')
-    width = printLines(env, next, gap, y + env.maxascent + env.maxdescent + gap,
-      flags, draw);
+    width = printLines(env, next, y + env.maxascent + env.maxdescent
+      + env.gap, flags, draw);
   
   if(tmp > width)
     return tmp;
@@ -315,7 +314,6 @@ int printLines(struct printerEnv env, char *text, int gap, int y,
 
 void printerShow(struct printerEnv *env, char* text, enum t_type font)
 {
-  int gap = 5, padding = 5;
   int nlines = 1;
   char *p = text;
   while((p = strchr(p, '\n')) != NULL)
@@ -326,9 +324,9 @@ void printerShow(struct printerEnv *env, char* text, enum t_type font)
     nlines++;
   }
   //  width and height
-  env->width = printLines(*env, text, gap, 0, font, 0) + 2*padding;
-  env->height = nlines * (env->maxascent + env->maxdescent + gap) - gap +
-    2*padding;
+  env->width = printLines(*env, text, 0, font, 0) + 2 * env->padding;
+  env->height = nlines * (env->maxascent + env->maxdescent + env->gap)
+    - env->gap + 2 * env->padding;
   
   XClearWindow(env->d, env->w);
   
@@ -342,7 +340,7 @@ void printerShow(struct printerEnv *env, char* text, enum t_type font)
   XSetForeground(env->d, env->gc, env->color_text);
   
   // text
-  printLines(*env, text, gap, padding + env->maxascent, font, 1);
+  printLines(*env, text, env->padding + env->maxascent, font, 1);
   XFlush(env->d);
 }
 

+ 3 - 2
printer.h

@@ -33,6 +33,7 @@ struct printerEnv
   XFontStruct *fontinfo_bi; // both
   int maxascent, maxdescent;
   int width, height;
+  int padding, gap;
   int root_width, root_height;
   int margin_bottom;
   unsigned long color_background, color_text;
@@ -41,8 +42,8 @@ struct printerEnv
 enum t_type {T_ITALIC = 1, T_BOLD = 2};
 
 // if width < 0 the window will be as larger as possible
-struct printerEnv printerOpenWindow(int margin_bottom, char *font, char *font_i,
-  char *font_b, char *font_bi);
+struct printerEnv printerOpenWindow(char *font, char *font_i, char *font_b,
+  char *font_bi);
 void printerCloseWindow(struct printerEnv env);
 
 void printerShow(struct printerEnv *env, char* text, enum t_type font);