File: osdsh.c

package info (click to toggle)
osdsh 0.7.0-10.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 452 kB
  • ctags: 346
  • sloc: ansic: 1,063; tcl: 335; makefile: 73
file content (204 lines) | stat: -rw-r--r-- 5,140 bytes parent folder | download | duplicates (3)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "osdsh.h"
#include "../config.h"


/* ==============================global variables=============================*/
/*
messages_t messages;
*/

/*basic setup here*/

extern settings_t settings;
/*
extern settings_t mixerset;
extern settings_t clockset;
extern settings_t pppset;
extern settings_t apmset;

void *apm_watch(void *arg);
void *control_sh(void *arg);
void *mixer_watch(void *arg);
void *connection_watch(void *arg);
void *clock_display(void *arg);
*/

/* ============================ usage () =================================== */
void usage(const int exitcode, const char *error, const char *more)
{
    fprintf(stderr, "osdsh %s\n", VERSION);
    fprintf(stderr, "usage: osdsh [options]\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -h         display usage\n");
    fprintf(stderr, "  -m mixer   set mixer         %s\n", MIXER);
    fprintf(stderr, "  -f font    set font          %s \n", FONT);
    fprintf(stderr, "  -c color   set color         %s \n", COLOR);
    fprintf(stderr, "  -d int     set OSD delay     %d \n", TIMEOUT);
    fprintf(stderr, "  -o int     set Shadow Offset %d \n", SHADOW_OFFSET);
    fprintf(stderr, "  -p <0|1>   position of the osd, 0 for top,\n");
    fprintf(stderr, "             1 for bottom. Default 0 (top).\n");
    fprintf(stderr, "  -a <0|1|2> align of the osd. left,center or right.\n");
    fprintf(stderr, "  -n number  set the nice number so osdsh won't eat your cpu\n");
    fprintf(stderr, "\n");
    if (error)
	fprintf(stderr, "%s: %s\n\n", error, more);
    exit(exitcode);
}



/* ============================ parse_args () ============================== */
void parse_args(int argc, char *argv[], settings_t * settings)
{
    int c;
    int opt_index = 0;
    struct option opts[] = {
	{"help", 0, 0, 'h'},
	{"position", 1, 0, 'p'},
	{"soffset", 1, 0, 'o'},
	{"ooffset", 1, 0, 'O'},
	{"mixer", 1, 0, 'm'},
	{"font", 1, 0, 'f'},
	{"color", 1, 0, 'c'},
	{"ocolor", 1, 0, 'k'},
	{"delay", 1, 0, 'd'},
	{"align", 1, 0, 'a'},
	{"nice", 1, 0, 'n'}
    };
    while ((c = getopt_long(argc, argv, "hm:f:c:p:d:o:a:n:", opts, &opt_index)) >= 0) {
	switch (c) {
	case 'h':
	    usage(0, NULL, NULL);
	    exit(0);
	case 'f':
	    strncpy(settings->font, optarg, BUFSIZ);
	    settings->font[BUFSIZ] = '\0';
	    break;
	case 'c':
	    strncpy(settings->color, optarg, BUFSIZ);
	    settings->color[BUFSIZ] = '\0';
	    break;
	case 'k':
	    strncpy(settings->ocolor, optarg, BUFSIZ);
	    settings->ocolor[BUFSIZ] = '\0';
	    break;
	case 'p':
		settings->position = atoi(optarg);
	    break;
	case 'a':
	    settings->align = atoi(optarg);
	    break;
	case 'd':
	    settings->delay = atoi(optarg);
	    break;
	case 'm':
	    strncpy(mixerdevice, optarg, PATH_MAX);
	    mixerdevice[PATH_MAX] = '\0';
	    break;
	case 'o':
	    settings->soffset = atoi(optarg);
	    break;
	case 'O':
	    settings->ooffset = atoi(optarg);
	    break;
	case 'n':
	    nice(atoi(optarg));
	    break;
	default:
	    usage(1, NULL, NULL);
	    exit(1);
	}
    }
}

void load_basic_plugins(void) {
	int a;
	char file[PATH_MAX+255];

	sprintf(file, "%s/libosdshmixer.so", PLUGINSDIR);
	a = load_plugin(file);
	if (a<0) {
		xosd_display(settings.myosd, 0, XOSD_string, "No Mixer Support");
		xosd_display(settings.myosd, 1, XOSD_string, "");
	}

	sprintf(file, "%s/libosdshclock.so", PLUGINSDIR);
	a = load_plugin(file);
	if (a<0) {
		xosd_display(settings.myosd, 0, XOSD_string, "No Clock Support");
		xosd_display(settings.myosd, 1, XOSD_string, "");
	}

	sprintf(file, "%s/libosdshnet.so", PLUGINSDIR);
	a = load_plugin(file);
	if (a<0) {
		xosd_display(settings.myosd, 0, XOSD_string, "No Net Support");
		xosd_display(settings.myosd, 1, XOSD_string, "");
	}

	sprintf(file, "%s/libosdshapm.so", PLUGINSDIR);
	a = load_plugin(file);
	if (a<0) {
		xosd_display(settings.myosd, 0, XOSD_string, "No APM/Battery support");
		xosd_display(settings.myosd, 1, XOSD_string, "");
	}
}

/* ============================ main () ==================================== */

int main(int argc, char *argv[], char *env[])
{
    pid_t childpid;
    pid_t osdsh_pid;

    char pid_file[PATH_MAX+1];
    FILE *fp;
    char fifo_file[PATH_MAX +1];

    set_defaults(&settings);
    initialize_osd(&settings);

    /* HACK */
    strncpy(mixerdevice, MIXER, PATH_MAX);
    mixerdevice[PATH_MAX] = '\0';


    parse_args(argc, argv, &settings);
/*
#ifndef NOAPM
    apmset = settings;
#endif
    pppset = mixerset = settings;
*/

    sprintf(pid_file, "%s.%d.pid", OSD_FIFO_PATH, getuid()) ;

    if(fp = fopen(pid_file, "r")) {
	if((fscanf(fp, "%d", &osdsh_pid)) && !kill(osdsh_pid, 0)) {
            fprintf(stderr, "osdSH is already running (pid %d)\n", osdsh_pid);
	    exit(1);
	}
	fclose(fp);
    }

    /* create the fifo file*/
    sprintf(fifo_file, "%s.%d", OSD_FIFO_PATH, getuid());
    unlink(fifo_file);
    umask(0);
    mknod(fifo_file, S_IFIFO|0600, 0);

    if((childpid=fork())<0) {
	perror("fork");
	exit(1);
    }
    if (childpid==0) {
	    control_sh(NULL);
    }
    else {
	fp=fopen(pid_file, "w");
	fprintf(fp, "%d", childpid);
	fclose(fp);
	exit(0);
    }	
    return(0);
}