Browse Source

merge factor

Olivier Marty 9 years ago
parent
commit
c486f9dd38
5 changed files with 65 additions and 48 deletions
  1. 21 16
      main.c
  2. 30 29
      printer.c
  3. 6 3
      printer.h
  4. 7 0
      time.c
  5. 1 0
      time.h

+ 21 - 16
main.c

@@ -30,9 +30,9 @@ 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("  -t x\t\t: time factor x1000\n");
-  printf("  -w px\t\t: width of the window\n");
-  printf("  -H px\t\t: height of the window\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");
@@ -42,15 +42,14 @@ void displayUsage(char *name)
 
 int main(int argc, char **argv)
 {
-  int i, shift = 0, delay = 5, width = -1, height = 240, margin_bottom = 50;
+  int i, shift = 0, delay = 5, margin_bottom = 50, padding = 5, gap = 5;
   float factor = 1.0;
-    
   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:t:w:H:m:f:i:b:j:h")) != -1)
+  while((c = getopt (argc, argv, "s:d:t:m:p:g:f:i:b:j:h")) != -1)
     switch(c)
     {
       case 's':
@@ -62,15 +61,15 @@ int main(int argc, char **argv)
       case 't':
         factor = (float)atoi(optarg)/1000;
         break;
-      case 'w':
-        width = atoi(optarg);
-        break;
-      case 'H':
-        height = atoi(optarg);
-        break;
       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);
@@ -110,8 +109,12 @@ int main(int argc, char **argv)
   }
   
   // open the window
-  struct printerEnv penv = printerOpenWindow(width, height, 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--)
@@ -119,7 +122,7 @@ int main(int argc, char **argv)
     char t[16];
     sprintf(t, "%d...\n", i);
     printf("%s", t);
-    printerShow(penv, t, T_ITALIC);
+    printerShow(&penv, t, T_ITALIC);
     sleep(1);
   }
   printf("0 !\n");
@@ -131,12 +134,14 @@ int main(int argc, char **argv)
   while(!feof(f))
   {
     id = next(f, id+1, &sline);
-    if(!timeSleepUntil(timeFactor(sline.begin, factor))) // no error and in the future
+    if(timeInFuture(timeFactor(sline.end, factor)))
     {
+      timeSleepUntil(timeFactor(sline.begin, factor));
+      
       printf("%ds\n", sline.begin.tv_sec);
       // show
       printf("%s\n", sline.text);
-      printerShow(penv, sline.text, 0);
+      printerShow(&penv, sline.text, 0);
       
       // hide
       timeSleepUntil(timeFactor(sline.end, factor));

+ 30 - 29
printer.c

@@ -61,8 +61,8 @@ void loadFont(struct printerEnv *env, XFontStruct **font, char* fontname)
   }
 }
 
-struct printerEnv printerOpenWindow(int width, int height, 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 width, int height, int margin_bottom,
   // 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;
@@ -95,16 +97,14 @@ struct printerEnv printerOpenWindow(int width, int height, int margin_bottom,
   XParseColor(env.d, attr.colormap, "#222222", &tmp);
   XAllocColor(env.d, attr.colormap, &tmp);
   env.color_background = tmp.pixel;
-  //env.color_text = -1;
-  //env.color_background = (100 * 256 + 100) * 256 + 100;
+  //unsigned short r = 34, g = 34, b = 34, a = 192; // TODO where is the doc ?
+  //env.color_background = a*256*256*256 + r*256*256 + g*256 + b;
   
   // create the window
-  env.width = (width < 0) ? RootAttr.width : width;
-  env.height = height;
-  env.w = XCreateWindow(env.d, RootWindow(env.d, env.s),
-    (RootAttr.width - env.width)/2,
-    RootAttr.height - env.height - margin_bottom, env.width, env.height, 0,
-    vinfo.depth, InputOutput, vinfo.visual,
+  env.width = 1;
+  env.height = 1;
+  env.w = XCreateWindow(env.d, RootWindow(env.d, env.s), 0, 0, env.width,
+    env.height, 0, vinfo.depth, InputOutput, vinfo.visual,
     CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, &attr);
   
   // setting fonts
@@ -271,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;
@@ -304,19 +304,17 @@ 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;
   return width;
 }
 
-void printerShow(struct printerEnv env, char* text, enum t_type font)
+void printerShow(struct printerEnv *env, char* text, enum t_type font)
 {
-  int gap = 5, padding = 5;
-  int nlines = 1,
-      width, height;
+  int nlines = 1;
   char *p = text;
   while((p = strchr(p, '\n')) != NULL)
   {
@@ -325,22 +323,25 @@ void printerShow(struct printerEnv env, char* text, enum t_type font)
       break;
     nlines++;
   }
+  //  width and height
+  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);
-  //  max width
-  width = printLines(env, text, gap, 0, font, 0);
-  height = nlines * (env.maxascent + env.maxdescent + gap) - gap;
+  XClearWindow(env->d, env->w);
+  
+  XMoveResizeWindow(env->d, env->w, (env->root_width - env->width)/2,
+    env->root_height - env->margin_bottom - env->height, env->width,
+    env->height);
   
   // frame
-  XSetForeground(env.d, env.gc, env.color_background);
-  XFillRectangle(env.d, env.w, env.gc, (env.width - width)/2 - padding,
-    env.height - 2*padding - height, width + 2*padding, height + 2*padding);
-  XSetForeground(env.d, env.gc, env.color_text);
+  XSetForeground(env->d, env->gc, env->color_background);
+  XFillRectangle(env->d, env->w, env->gc, 0, 0, env->width, env->height);
+  XSetForeground(env->d, env->gc, env->color_text);
   
   // text
-  printLines(env, text, gap,
-    env.height - padding - height + env.maxascent, font, 1);
-  XFlush(env.d);
+  printLines(*env, text, env->padding + env->maxascent, font, 1);
+  XFlush(env->d);
 }
 
 void printerClean(struct printerEnv env)

+ 6 - 3
printer.h

@@ -33,17 +33,20 @@ 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;
 };
 
 enum t_type {T_ITALIC = 1, T_BOLD = 2};
 
 // if width < 0 the window will be as larger as possible
-struct printerEnv printerOpenWindow(int width, int height, 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);
+void printerShow(struct printerEnv *env, char* text, enum t_type font);
 void printerClean(struct printerEnv env);
 
 #endif

+ 7 - 0
time.c

@@ -96,3 +96,10 @@ struct timespec timeCreate(time_t s, long ns)
   return r;
 }
 
+int timeInFuture(struct timespec t)
+{
+  struct timespec tmp = timeGetRelative();
+  tmp = timeDiff(t, tmp);
+  return tmp.tv_sec >= 0;
+}
+

+ 1 - 0
time.h

@@ -27,6 +27,7 @@ void timeInitialize(int rel); // add rel seconds to the clock
 mytime timeGetRelative();
 // return -2 if t is in the past
 int timeSleepUntil(mytime t);
+int timeInFuture(mytime t);
 
 mytime timeCreate(time_t s, long ns);