~ubuntu-branches/ubuntu/feisty/powerpc-utils/feisty

« back to all changes in this revision

Viewing changes to fblevel.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Schmitz
  • Date: 2005-05-09 12:31:24 UTC
  • Revision ID: james.westby@ubuntu.com-20050509123124-m6cnwua15r7say20
Tags: 1.1.3-15
Merge back patches from ubuntu (thanks to Adam Conrad <adconrad@0c3.net>
for the hint): fix linuxdoc-tools b-d, drop adb_mouse.h header. Update 
description to include autoboot and lsprop tools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ----------------------------------------------------------------------------
 
3
 * fblevel - sets the fb device brightness level on Apple Powerbooks
 
4
 *
 
5
 * Copyright 2000 Stephan Leemburg <stephan@jvc.nl>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public License
 
9
 * as published by the Free Software Foundation; either version
 
10
 * 2 of the License, or (at your option) any later version.
 
11
 * ----------------------------------------------------------------------------
 
12
 * $Log: fblevel.c,v $
 
13
 * Revision 1.1.1.1  2001/12/07 11:31:50  sleemburg
 
14
 * Initial CVS import of the unreleased pmud-0.8 to apmud (new project name
 
15
 * because of a name clash at sourceforge.net).
 
16
 *
 
17
 * Revision 1.3  2001/11/09 10:55:29  stephan
 
18
 * use "pmu.h"
 
19
 *
 
20
 * Revision 1.2  2000/10/09 14:16:33  stephan
 
21
 * use /dev/fb0 in stead of /dev/fb
 
22
 *
 
23
 * Revision 1.1  2000/05/11 14:48:46  stephan
 
24
 * Initial revision
 
25
 *
 
26
 * ----------------------------------------------------------------------------
 
27
 */
 
28
static char *rcsid = "@(#)$Id: fblevel.c,v 1.1.1.1 2001/12/07 11:31:50 sleemburg Exp $";
 
29
 
 
30
#include <stdio.h>
 
31
#include <ctype.h>
 
32
#include <errno.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <sys/ioctl.h>
 
36
#include <sys/types.h>
 
37
#include <sys/stat.h>
 
38
#include <fcntl.h>
 
39
#include <unistd.h>
 
40
 
 
41
#include <linux/fb.h>
 
42
#include <linux/pmu.h>
 
43
 
 
44
#ifndef FBIOBLANK
 
45
#define FBIOBLANK      0x4611          /* 0 or vesa-level+1 */
 
46
#endif
 
47
 
 
48
static void fbon(int on);
 
49
static int set_fblevel(int level);
 
50
static int get_fblevel();
 
51
static void usage(char *program);
 
52
 
 
53
int main(int argc, char **argv)
 
54
{
 
55
        char *program = strrchr(*argv, '/');
 
56
 
 
57
        program = program ? program + 1 : *argv;
 
58
 
 
59
        switch(argc)
 
60
        {
 
61
        case 1:
 
62
                printf("%d\n", get_fblevel());
 
63
                break;
 
64
        case 2:
 
65
                if(!strcasecmp(*++argv, "on")
 
66
                || !strcasecmp(*argv, "off"))
 
67
                        fbon(strcasecmp(*argv, "off"));
 
68
                else
 
69
                {
 
70
                        register char *p;
 
71
 
 
72
                        for(p = *argv; *p && isdigit(*p); p++)
 
73
                                ;
 
74
                        if(*p)
 
75
                        {
 
76
                                usage(program);
 
77
                                return 1;
 
78
                        }
 
79
                        if(!set_fblevel(atoi(*argv)))
 
80
                                return 1;
 
81
                }
 
82
                break;
 
83
        default:
 
84
                usage(program);
 
85
                return 1;
 
86
 
 
87
        }
 
88
        return 0;
 
89
}
 
90
 
 
91
static void fbon(int on)
 
92
{
 
93
        static int fd = -1;
 
94
        if(fd<0)
 
95
        {
 
96
                fd = open("/dev/fb0", O_RDONLY);
 
97
                if(fd<0)
 
98
                {
 
99
                        perror("/dev/fb0");
 
100
                        return;
 
101
                }
 
102
        }
 
103
        (void)ioctl(fd, FBIOBLANK, !on);
 
104
}
 
105
 
 
106
static int set_fblevel(int level)
 
107
{
 
108
        int fd, ret=0;
 
109
        if ( (fd = open("/dev/pmu", O_RDONLY)) < 0 ) {
 
110
                perror("/dev/pmu");
 
111
                goto out;
 
112
        }
 
113
        if(ioctl(fd, PMU_IOC_SET_BACKLIGHT, &level) == -1) {
 
114
                perror("ioctl PMU_IOC_SET_BACKLIGHT");
 
115
                goto out;
 
116
        }
 
117
        ret = 1;        /* success */
 
118
out:
 
119
        close(fd);
 
120
        return ret;
 
121
}
 
122
 
 
123
static int get_fblevel()
 
124
{
 
125
        int fd, level=0;
 
126
        if ( (fd = open("/dev/pmu", O_RDONLY)) < 0 ) {
 
127
                perror("/dev/pmu");
 
128
                goto out;
 
129
        }
 
130
        if(ioctl(fd, PMU_IOC_GET_BACKLIGHT, &level) == -1) {
 
131
                perror("ioctl PMU_IOC_GET_BACKLIGHT");
 
132
                goto out;
 
133
        }
 
134
out:
 
135
        close(fd);
 
136
        return level;
 
137
}
 
138
 
 
139
static void usage(char *program)
 
140
{
 
141
        fprintf(stderr, 
 
142
                "usage: %s [on|off|<level>]\n"
 
143
                "       on     : powers display on\n"
 
144
                "       off    : powers display off\n"
 
145
                "       <level>: set display brightness to <level>\n"
 
146
                "                (level is an integer)\n"
 
147
                "no argument prints the current brightness level\n",
 
148
                program
 
149
                );
 
150
}