summaryrefslogtreecommitdiffstats
path: root/ps3-video-mode.c
blob: 5238f10464a60cb1a33a1904249464b9100678f5 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 *  PS3 video mode utility.
 *
 *  Copyright (C) 2006 Sony Computer Entertainment Inc.
 *  Copyright 2006 Sony Corp.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this rrogram; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "ps3-av.h"

#define PS3_UTILS_APP_NAME "ps3-video-mode"

#if defined(PACKAGE_VERSION) && defined(PACKAGE_NAME)
#define PS3_UTILS_VERSION PS3_UTILS_APP_NAME " (" PACKAGE_NAME ") " PACKAGE_VERSION "\n"
#else
#define PS3_UTILS_VERSION ""
#endif

#if defined(PACKAGE_BUGREPORT)
#define PS3_UTILS_BUGREPORT "Send bug reports to " PACKAGE_BUGREPORT ".\n"
#else
#define PS3_UTILS_BUGREPORT ""
#endif

#if defined(DEBUG)
#define DBG(_args...) do {fprintf(stderr, _args);} while(0)
#else
static inline int __attribute__ ((format (printf, 1, 2))) DBG(
	__attribute__((unused)) const char *fmt, ...) {return 0;}
#endif

static const char fb_dev[] = "/dev/fb0";

static void print_version(void)
{
	printf(PS3_UTILS_VERSION);
}

static void print_usage(void)
{
	fprintf(stderr, PS3_UTILS_VERSION);
	fprintf(stderr,
"SYNOPSIS\n"
"     ps3-video-mode [-m, --mode mode-id] [-r, --rgb] [-f, --full-screen]\n"
"                    [-g, --full-range] [-d, --dither] [-h, --help]\n"
"                    [-V, --version]\n"
"OPTIONS\n"
"     -m, --mode mode-id\n"
"             Set the system video mode to mode-id.\n"
"             AUTO Detect Mode:\n"
"                0: auto (480i/576i if not HDMI)\n"
"             60 Hz Broadcast Modes:\n"
"                1: 480i    (576 x 384)\n"
"                2: 480p    (576 x 384)\n"
"                3: 720p   (1124 x 644)\n"
"                4: 1080i  (1688 x 964)\n"
"                5: 1080p  (1688 x 964)\n"
"             50 Hz Broadcast Modes:\n"
"                6: 576i    (576 x 460)\n"
"                7: 576p    (576 x 460)\n"
"                8: 720p   (1124 x 644)\n"
"                9: 1080i  (1688 x 964)\n"
"               10: 1080p  (1688 x 964)\n"
"             VESA Modes:\n"
"               11: wxga   (1280 x 768)\n"
"               12: sxga   (1280 x 1024)\n"
"               13: wuxga  (1920 x 1200)\n"
"             60 Hz Full Screen Broadcast Modes:\n"
"              129: 480if   (720 x 480)\n"
"              130: 480pf   (720 x 480)\n"
"              131: 720pf  (1280 x 720)\n"
"              132: 1080if (1920 x 1080)\n"
"              133: 1080pf (1920 x 1080)\n"
"             50 Hz Full Screen Broadcast Modes:\n"
"              134: 576if   (720 x 576)\n"
"              135: 576pf   (720 x 576)\n"
"              136: 720pf  (1280 x 720)\n"
"              137: 1080if (1920 x 1080)\n"
"              138: 1080pf (1920 x 1080)\n"
"     -r, --rgb\n"
"             Use RGB color space mode.\n"
"     -f, --full-screen\n"
"             Use full screen mode.\n"
"     -g, --full-range\n"
"             Use full range mode.\n"
"     -d, --dither\n"
"             Use dither mode.\n"
"     -h, --help\n"
"             Print a help message.\n"
"     -V, --version\n"
"             Display the program version number.\n"
"See the ps3-video-mode man page for more detailed information.\n"
	);
	fprintf(stderr, PS3_UTILS_BUGREPORT);
}

static const struct option long_options[] = {
	{"mode",        required_argument, NULL, 'm'},
	{"rgb",         no_argument,       NULL, 'r'},
	{"full-screen", no_argument,       NULL, 'f'},
	{"full-range",  no_argument,       NULL, 'g'},
	{"dither",      no_argument,       NULL, 'd'},
	{"hdcp" ,       no_argument,       NULL, 'H'}, /* HDCP OFF, no support on retail PS3 */
	{"video" ,      required_argument, NULL, 'v'}, /* legacy option */
	{"help",        no_argument,       NULL, 'h'},
	{"version",     no_argument,       NULL, 'V'},
	{ NULL,         0,                 NULL, 0},
};

static const char short_options[] = "m:rfgdHv:hV";

int main(int argc, char *argv[])
{
	int result;
	int mode = -1;
	int flags = 0;
	int fd;

	while(1) {
		int c = getopt_long(argc, argv, short_options, long_options,
			NULL);

		if (c == EOF)
			break;

		switch(c) {
		case 'v':
			fprintf(stderr, "WARNING: Option -v obsolete, use -m.\n");
			/* fall through to 'm' */
		case 'm':
			mode = atoi(optarg);
			break;
		case 'r':
			flags |= PS3AV_MODE_RGB;
			break;
		case 'f':
			flags |= PS3AV_MODE_FULL;
			break;
		case 'g':
			flags |= PS3AV_MODE_COLOR;
			flags |= PS3AV_MODE_WHITE;
			break;
		case 'd':
			flags |= PS3AV_MODE_DITHER;
			break;
		case 'H':
			flags |= PS3AV_MODE_HDCP_OFF;
			break;
		case 'V':
			print_version();
			exit(0);
		case 'h':
		default:
			print_usage();
			exit(0);
		}
	}

	fd = open(fb_dev, O_RDWR);

	if (fd < 0) {
		DBG("%s:%d: open failed.\n", __func__, __LINE__);
		perror(fb_dev);
		exit(1);
	}

	if (mode < 0 && flags) {
		DBG("%s:%d: flags without mode.\n", __func__, __LINE__);
		print_usage();
		exit(1);
	}

	if (mode < 0) {
		result = ioctl(fd, PS3FB_IOCTL_GETMODE, (unsigned long)&mode);
		if (result < 0) {
			close(fd);
			perror("PS3FB_IOCTL_GETMODE failed");
			exit(1);
		}
		printf("%d\n", mode);
	} else {
		mode |= flags;
		result = ioctl(fd, PS3FB_IOCTL_SETMODE, (unsigned long)&mode);
		if (result < 0) {
			close(fd);
			perror("PS3FB_IOCTL_SETMODE failed");
			exit(1);
		}
		printf("video mode:%d\n", mode);
	}

	close(fd);

	return 0;
}