File: larsclock.c

package info (click to toggle)
larswm 7.5.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 444 kB
  • ctags: 476
  • sloc: ansic: 6,585; makefile: 42; sh: 18
file content (82 lines) | stat: -rw-r--r-- 1,577 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Copyright (c) 2000 Lars Bernhardsson, see README for licence details */

/*
 * Minimal clock app for larswm. No fork()/exec() is done, it updates
 * the status bar by setting the LARSWM_BARTEXT atom every second.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

#ifdef USE_LOCALE
#include <locale.h>
#endif

#include <X11/X.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#define CLOCK_LENGTH 100

int
main (int argc, char **argv)
{
  int i;
  time_t t;
  char *slist;
  char *format = "%c";
  char *display_string = "";
  Display *dpy;
  Atom bartext_larswm;
  XTextProperty pr;

#ifdef USE_LOCALE
  setlocale (LC_ALL, "");
#endif

  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "-display") == 0 && i + 1 < argc)
	{
	  display_string = argv[++i];
	}
      else if (strcmp (argv[i], "-format") == 0 && i + 1 < argc)
	{
	  format = argv[++i];
	}
      else
	{
	  fprintf (stderr, "usage: %s [-display display] [-format format]\n",
		   argv[0]);
	  exit (1);
	}
    }

  dpy = XOpenDisplay (display_string);

  if (dpy == 0)
    {
      perror ("XOpenDisplay");
      exit (1);
    }

  bartext_larswm = XInternAtom (dpy, "LARSWM_BARTEXT", False);
  slist = malloc (CLOCK_LENGTH);

  while (1)
    {
      time (&t);
      strftime (slist, CLOCK_LENGTH, format, localtime (&t));

      XStringListToTextProperty (&slist, 1, &pr);
      XSetTextProperty (dpy, DefaultRootWindow (dpy), &pr, bartext_larswm);
      XFree (pr.value);
      XSync (dpy, False);

      sleep (1);
    }
}