Пример #1
0
int create_config_file(char* file) {
	FILE* fd;
	char *path;

	path = default_config_dir();
	create_config_dir(path);

	fd = fopen(file, "w");
	if (fd == NULL)
		return FALSE;

	fprintf(fd, "# lightum configuration file\n\n");
	fprintf(fd, "# manualmode\n");
	fprintf(fd, "#   0 = automatically adjust brightness/backlight based on light sensor\n");
	fprintf(fd, "#   1 = or control brightness/backlight manually using Fn+ F5/F6 keys\n");
	fprintf(fd, "manualmode=%d\n\n", default_config.manualmode);
	fprintf(fd, "# ignoreuser: only has effect in auto-mode (when manualmode=0)\n");
	fprintf(fd, "#   0 = change maxbrightness value dynamically when user presses Fn+ F5/F6\n");
	fprintf(fd, "#   1 = ignore brightness changes happening outside lightum and keep the\n");
	fprintf(fd, "#       maxbrightness value from the config file (fixes bug in ubuntu 12.04)\n");
	fprintf(fd, "ignoreuser=%d\n\n", default_config.ignoreuser);
	fprintf(fd, "# ignoresession: check if current user has X session active\n");
	fprintf(fd, "#   0 = useful if you are on a multi-user X server (default)\n");
	fprintf(fd, "#   1 = useful if you don't use ConsoleKit on your X server\n");
	fprintf(fd, "ignoresession=%d\n\n", default_config.ignoresession);
	fprintf(fd, "# workmode\n");
	fprintf(fd, "#   1 = only manage keyboard brightness (ignore screen backlight)\n");
	fprintf(fd, "#   2 = only manage screen backlight (ignore keyboard brightness)\n");
	fprintf(fd, "#   3 = manage both keyboard brightness and screen backlight\n");
	fprintf(fd, "workmode=%d\n\n", default_config.workmode);
	fprintf(fd, "# maximum keyboard brightness value (between 4 and 255)\n");
	fprintf(fd, "maxbrightness=%d\n\n", default_config.maxbrightness);
	fprintf(fd, "# minimum keyboard brightness value (between 0 and 3)\n");
	fprintf(fd, "minbrightness=%d\n\n", default_config.minbrightness);
	fprintf(fd, "# poll time in milliseconds (used for light sensor and session idle time)\n");
	fprintf(fd, "polltime=%d\n\n", default_config.polltime);
	fprintf(fd, "# turn off keyboard brightness if computer unused for X seconds (0 to disable)\n");
	fprintf(fd, "idleoff=%d\n\n", default_config.idleoff);
	fprintf(fd, "# screensaver\n");
	fprintf(fd, "#   1 = turn off keyboard brightness when screensaver is active\n");
	fprintf(fd, "#   0 = do not monitor screensaver status\n");
	fprintf(fd, "queryscreensaver=%d\n\n", default_config.queryscreensaver);
	fprintf(fd, "# maximum screen backlight value (between 7 and 15)\n");
	fprintf(fd, "maxbacklight=%d\n\n", default_config.maxbacklight);
	fprintf(fd, "# minimum screen backlight value (between 1 and 6)\n");
	fprintf(fd, "minbacklight=%d\n\n", default_config.minbacklight);
	fprintf(fd, "# turn down screen backlight if computer unused for X seconds (0 to disable)\n");
	fprintf(fd, "screenidle=%d\n\n", default_config.screenidle);
	fprintf(fd, "# fully dim the backlight when screenidle seconds reached (default 0)\n");
	fprintf(fd, "#   1 = when idle, backlight will be set to 1\n");
	fprintf(fd, "#   0 = when idle, backlight will be set to minbacklight\n");
	fprintf(fd, "fulldim=%d\n\n", default_config.fulldim);
	fclose(fd);

	return TRUE;
}
Пример #2
0
int create_pid_file() {

    int fd;
    char *path, *pidfile;
    char buf[100];
    ssize_t cnt;
    char* procpid = malloc( sizeof(buf) + 15 );

    path = default_config_dir();
    create_config_dir(path);

    pidfile = default_pid_file();

    if (file_exists(pidfile)) {

        // check if /proc/{pid}/cmdline exists and contains lightum
        // if it does, means lightum is already running, so we exit cowardly
        // if it does not contain lightum, then we remove the old pid file and continue

        fd = open(pidfile, O_RDONLY);
        if (fd < 0) {
            fprintf (stderr,"Could not open pid file: %s\n", pidfile);
            return FALSE;
        }
        cnt=read(fd, buf, sizeof(buf)-1);
        buf[cnt]='\0';

        close(fd);

        strcpy(procpid, "");
        strcat(procpid, "/proc/");
        strcat(procpid, buf);
        strcat(procpid, "/cmdline");

        if (file_exists(procpid)) {
            fd = open(procpid, O_RDONLY);
            if (fd < 0) {
                fprintf (stderr,"Could not open file: %s\n", procpid);
                return FALSE;
            }

            cnt=read(fd, buf, sizeof(buf)-1);
            buf[cnt]='\0';

            close(fd);

            if (strstr(buf,"lightum") != NULL) {
                fprintf (stderr,"Refusing to start as lightum is already running\n");
                return FALSE;
            } else {
                if (!remove_pid_file())
                    return FALSE;
            }
        }
    }

    fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0 ) {
        fprintf(stderr,"Could not write pid file: %s\n", pidfile);
        return FALSE;
    }

    sprintf( buf, "%d", getpid() );
    if (write(fd, buf, strlen(buf)) < 1) {
        perror("Something wrong happening while writing pid file");
        close(fd);
        return FALSE;
    }
    close(fd);

    free(procpid);

    return TRUE;
}