File: vidmode_parse.cc

package info (click to toggle)
gvidm 0.8-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 420 kB
  • sloc: cpp: 1,049; sh: 153; python: 104; makefile: 64
file content (166 lines) | stat: -rw-r--r-- 5,069 bytes parent folder | download | duplicates (8)
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
/*
    vidmode_parse.cc - vidmode parsing functions
    Copyright (C) 2001-2004 Matthew Mueller <donut AT dakotacom DOT net>

    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; either version 2 of the License, or
    (at your option) any later version.

    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 program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "vidmode.h"

#include "_ios.h"
#include "_sstream.h"
#include <iomanip>


string VidMode::str() const {
	ostringstream oss;
	oss << width << 'x' << height << '@' << fixed << setprecision(0) << refresh;
	return oss.str();
}

string VidMode::pretty_str() const {
	ostringstream oss;
	oss << setw(4) << width << 'x' << left << setw(4) << height << ' ' << fixed << setprecision(0) << refresh << "Hz";
	if (!info.empty())
		oss << " (" << info << ')';
	return oss.str();
}


int parse_mode(int &w, int &h, int &r, const string &arg) {
	r=-1;
	if (arg == "max")
		w = h = INT_MAX;
	else if (arg == "best")
		w = h = -2;
	else if (arg == "all")
		w = h = -1;
	else {	
		int x = arg.find('x');
		int a = arg.find('@');
		int hlen = ((a>=0)?a:arg.size())-x-1;
		if (x<0) return 1;
		if (parse_int(w,arg.substr(0,x)) || parse_int(h,arg.substr(x+1,hlen))) return 1;
		if (a>=0 && parse_int(r, arg.substr(a+1))) return 1;
		if (w<=0 || h<=0) return 1;
	}
	return 0;
}

int parse_arg(screen_list_t &screens, int &w, int &h, int &r, const string &arg, int num_screens, int cur_screen) {
	int c = arg.find(':');
	if (c>=0) {
		if (parse_screen(screens, arg.substr(0, c), num_screens, cur_screen)) return 1;
		if (parse_mode(w, h, r, arg.substr(c+1))) return 1;
	}
	else {
		screens.push_back(cur_screen);
		if (parse_mode(w, h, r, arg)) return 1;
	}
	return 0;	
}

int find_mode(mode_list_t &modes, int screen, int w, int h, int r, const screen_mode_list_t &available_modes) {
	assert(screen<(signed)available_modes.size());
	const mode_list_t &smodes = available_modes[screen];
	int errs=0;

	if (w==INT_MAX && h==INT_MAX) {
		unsigned long maxpixels = 0;
		int maxrefresh = 0;
		mode_list_t::const_iterator max_mode=smodes.end();
		for (mode_list_t::const_iterator modeitr=smodes.begin(); modeitr!=smodes.end(); ++modeitr) {
			const VidMode &mode = **modeitr;
			unsigned long pixelcount = mode.width * mode.height;
			if (pixelcount > maxpixels || 
					(pixelcount==maxpixels && mode.refresh>maxrefresh)) {
				maxpixels = pixelcount;
				maxrefresh = mode.refresh;
				max_mode = modeitr;
			}
		}
		modes.push_back(*max_mode);
	}
	else if (w==-2 && h==-2) {
		for (mode_list_t::const_iterator i=smodes.begin(); i!=smodes.end(); ++i) {
			const VidMode &imode = **i;
			bool best=true;
			for (mode_list_t::const_iterator j=smodes.begin(); j!=smodes.end(); ++j) {
				const VidMode &jmode = **j;
				if (j!=i && jmode.width==imode.width && jmode.height==imode.height && jmode.refresh>imode.refresh) {
					best=false;
					break;
				}
			}
			if (best)
				modes.push_back(*i);
		}
				
	}
	else if (w==-1 && h==-1) {
		modes.insert(modes.end(), smodes.begin(), smodes.end());
	}
	else {
		mode_list_t::const_iterator modeitr;
		VidMode *matchmode=NULL;
		for (modeitr=smodes.begin(); modeitr!=smodes.end(); ++modeitr) {
			VidMode *mode = *modeitr;
			if (mode->width==w && mode->height==h) {
				if (r<0) { // If no refresh specified, choose the best one.
					if (!matchmode || mode->refresh>matchmode->refresh)
						matchmode=mode;
				}
				else if (mode->refresh == r) {
						matchmode=mode;
						break;
				}
			}
		}
		if (matchmode)
			modes.push_back(matchmode);
		else
			errs++;
	}
	return errs;
}

int parse_arg_mode(screen_mode_list_t &arg_modes, const char *arg, const screen_mode_list_t &available_modes, int cur_screen) {
	screen_list_t screens;
	int w, h, r, errs = 0;

	if (parse_arg(screens, w, h, r, arg, available_modes.size(), cur_screen)) {
		fprintf(stderr, "invalid mode \"%s\"\n",arg);
		errs++;
	}
	else {
		for (screen_list_t::const_iterator screenitr=screens.begin(); screenitr!=screens.end(); ++screenitr) {
			int screen = *screenitr;
			if (find_mode(arg_modes[screen], screen, w, h, r, available_modes)) {
				fprintf(stderr, "screen %i mode %ix%i", screen, w, h);
				if (r>=0) fprintf(stderr, "@%i", r);
				fprintf(stderr, " not found\n");
				errs++;
			}
		}
	}
	return errs;
}

int parse_arg_modes(screen_mode_list_t &arg_modes, char const*const*arg0, char const*const*argend, const screen_mode_list_t &available_modes, int cur_screen) {
	int errs = 0;
	for (; arg0<argend; ++arg0) {
		errs += parse_arg_mode(arg_modes, *arg0, available_modes, cur_screen);
	}
	return errs;
}