summaryrefslogtreecommitdiffstats
path: root/src/fbshm.cpp
blob: 48d0c38160a4c14c47e9380798f34d598b7041bc (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
/*
 *   Copyright © 2018 IBM Corporation
 *   This file is part of FbTerm.
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "fbconfig.h"
#include "fbshm.h"

int FbShm::parse_shm_desc(const char *desc, char **file,
		int *rows, int *cols, int *bpp)
{
	char *str, *sep, *endp;
	unsigned long tmp;

	str = strdup(desc);

	/* <file>:<cols>x<rows>@<bpp> */

	/* file */
	sep = strchr(str, ':');
	if (!sep)
		goto err_free;

	*file = str;
	*sep = '\0';

	/* cols */
	str = sep + 1;
	sep = strchr(str, 'x');
	if (!sep) {
		fprintf(stderr, "can't parse shm descriptor %s: no geom\n",
				desc);
		goto err_free;
	}
	*sep = '\0';

	tmp = strtoul(str, &endp, 10);
	if (endp != sep) {
		fprintf(stderr,
			"can't parse shm descriptor %s: invalid cols %s\n",
			desc, str);
		goto err_free;
	}

	*cols = (int)(tmp & 0xffffffff);

	/* rows */
	str = sep + 1;
	sep = strchr(str, '@');
	if (!sep) {
		fprintf(stderr, "can't parse shm descriptor %s: no geom\n",
				desc);
		goto err_free;
	}
	*sep = '\0';

	tmp = strtoul(str, &endp, 10);
	if (endp != sep) {
		fprintf(stderr,
			"can't parse shm descriptor %s: invalid rows %s\n",
			desc, str);
		goto err_free;
	}

	*rows = (int)(tmp & 0xffffffff);

	/* bpp */
	tmp = strtoul(sep + 1, &endp, 10);
	if (*endp != '\0' || endp == str) {
		fprintf(stderr,
			"can't parse shm descriptor %s: invalid bpp\n",
			desc);
		goto err_free;
	}

	*bpp = (int)(tmp & 0xff);

	printf("%s: rows %d, cols %d, bpp %d\n", *file, *rows, *cols, *bpp);

	return 0;

err_free:
	free(str);
	return -1;
}

FbShm *FbShm::initFbShm()
{
	int rows, cols, bpp;
	s8 *file, desc[4096];
	int fd;

	Config::instance()->getOption("shared-mem", desc, sizeof(desc));

	if (!strlen(desc))
		return 0;

	if (parse_shm_desc(desc, &file, &rows, &cols, &bpp))
		return 0;

	fd = open(file, O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "can't open frame buffer shm %s: %m\n", file);
		free(file);
		return 0;
	}

	free(file);

	return new FbShm(fd, rows, cols, bpp);
}

FbShm::FbShm(int fd, int rows, int cols, int bpp)
{
	mWidth = cols;
	mHeight = rows;
	mBitsPerPixel = bpp;
	mBytesPerLine = cols * (bpp >> 3);
	mFD = fd;


	mVMemBase = (u8 *)mmap(0, mHeight * mBytesPerLine,
			PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
}

FbShm::~FbShm()
{
	munmap(mVMemBase, mHeight * mBytesPerLine);
	close(mFD);
}

const s8 *FbShm::drvId()
{
	return "shm";
}

OpenPOWER on IntegriCloud