aboutsummaryrefslogtreecommitdiffstats
path: root/dwmstatus.c
blob: 1cb707d4aa2d6261121cfe1b925e4ce8a6188f70 (plain) (blame)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>

#include "kconfig.h"
#include "dwmstatus.h"

static Display *dpy;


int
stradd( bbuf *a, bbuf *b )
{
	int i=0,j=0;
	for ( i=0; 
	  i<a->size-1, a->str[i]!='\0'; 
	  i++){}
	
	for (j=0;
	  (i+j)<a->size-1, j<b->size-1;
	  j++)
	{
		a->str[i+j] = b->str[j];
	}
	return 0;
}

void
setstatus(char *str)
{
	XStoreName(dpy, DefaultRootWindow(dpy), str);
	XSync(dpy, False);
}

int main( int argc, char **argv )
{
	int cmd_status=0;

	int i=0;
	for (i=1; i<argc; i++ )
	{
		if ( strncmp( argv[i], "-t", 2 ) == 0 )
			cmd_status = 1;
		if ( strncmp( argv[i], "-h", 2) == 0)
		{
			printf("Help\n-t : run in terminal mode\n");
			return 0;
		}
	}

#ifdef CONFIG_STATUS_UTF8
	setlocale( LC_CTYPE, "" );
#endif
	//char *status=NULL;
	bbuf status; memset( &status, 0, sizeof(status) );
	bbuf time; memset( &time, 0, sizeof(time) );
	bbuf date; memset( &date, 0, sizeof(date) );
	bbuf temp; memset( &temp, 0, sizeof(temp) );
	bbuf cpu;  memset( &cpu, 0, sizeof(cpu) );
	bbuf batt; memset( &batt, 0, sizeof(batt) );

	if (status.size == 0)
	{
		status.size = 1024;
		status.str = malloc( status.size );
		memset( status.str, 0, status.size );
	}

	//if cannot open display maybe we are in terminal but we ignore it
	//while there was no direct command to workin in terminal we dont
	//run programm in terminal mode
	if (!(dpy = XOpenDisplay(NULL)))
	{
		printf("cannot open display.\n");
		return -1;
	}

	for (;;sleep(1))
	{
		status.str[0]=0;

	#ifdef CONFIG_STATUS_BATTERY
		print_batt( &batt );
		stradd( &status, &batt );
	#endif

	#ifdef CONFIG_STATUS_CPU
		print_cpu( &cpu );
		stradd( &status, &cpu );
	#endif

	#ifdef CONFIG_STATUS_TEMP
		print_temp( &temp );
		stradd( &status, &temp );
	#endif

	#ifdef CONFIG_STATUS_TIME
		print_time( &time );
		stradd( &status, &time );
	#endif

	#ifdef CONFIG_STATUS_DATE
		print_date( &date );
		stradd( &status, &date );
	#endif

		if ( cmd_status == 1)
		{
			wprintf(L"%s\n", status.str);
		}
		else
		{
			setstatus(status.str);
		}
	}

	XCloseDisplay(dpy);

	return 0;
}