aboutsummaryrefslogtreecommitdiffstats
path: root/src/kern-linux.c
blob: 071d3fc5b7a9b043c71d94727ad0e4289f034b29 (plain) (blame)
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
/**
 * scrotty — Screenshot program for Linux's TTY
 * 
 * Copyright © 2014, 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
#include "common.h"
#include "kern.h"
#include "png.h"

#include <sys/ioctl.h>
#include <linux/fb.h>



/**
 * Stop when `try_alt_fbpath` (in scrotty.c) reaches this value.
 */
const int alt_fbpath_limit = 2;

struct data
{
  unsigned long start;
  unsigned long end;
  long hblank;
  unsigned long position;
};



/**
 * This is called if no framebuffer is found.
 * 
 * It prints a message describing this condition
 * and suggests how to resolve it.
 */
void
print_not_found_help (void)
{
  fprintf (stderr, _("%s: Unable to find a framebuffer. "
		     "If you have a file named %s/MAKEDEV, "
		     "run it with '-d /dev/fb' as root, "
		     "or try running '%s'.\n"),
	   execname, DEVDIR, "mknod /dev/fb0 c 29 0 && chgrp video /dev/fb0");
}


/**
 * Construct the path to a framebuffer device.
 * 
 * @param   altpath  The index of the alternative path-pattern to use.
 * @param   fbno     The index of the framebuffer.
 * @return           The path to the framebuffer device. Errors are impossible.
 *                   This string is statically allocated and must not be deallocated.
 */
char *
get_fbpath (int altpath, int fbno)
{
  static char pathbuf[sizeof (DEVDIR "/fb/") + 3 * sizeof (int)];
  sprintf (pathbuf, "%s/fb%s%i", DEVDIR, (altpath ? "/" : ""), fbno);
  return pathbuf;
}


/**
 * Get the dimensions of a framebuffer.
 * 
 * @param   fbno    The number of the framebuffer.
 * @param   fbfd    File descriptor for framebuffer device.
 * @param   width   Output parameter for the width of the image.
 * @param   height  Output parameter for the height of the image.
 * @parma   data    Additional data to pass to `convert_fb_to_png`.
 * @return          Zero on success, -1 on error.
 */
int
measure (int fbno, int fbfd, long *restrict width, long *restrict height, void **restrict data)
{
  static struct data d;
  struct fb_fix_screeninfo fixinfo;
  struct fb_var_screeninfo varinfo;
  unsigned long int linelength;
  
  if (ioctl (fbfd, FBIOGET_FSCREENINFO, &fixinfo))
    goto fail;
  
  if (ioctl (fbfd, FBIOGET_VSCREENINFO, &varinfo))
    goto fail;
  
  *width  = varinfo.xres;
  *height = varinfo.yres;
  
  if (varinfo.bits_per_pixel & 7)
    {
      fprintf(stderr, _("%s: Unsupported framebuffer configurations: "
			"pixels are not encoded in whole bytes.\n"), execname);
      exit(1);
    }
  
  linelength = fixinfo.line_length / (varinfo.bits_per_pixel / 8);
  d.start = varinfo.yoffset * linelength;
  d.start += varinfo.xoffset;
  d.end = d.start + linelength * varinfo.yres;
  d.hblank = linelength - *width;
  d.position = 0;
  
  /* TODO depth support */
  
  *data = &d;
  return 0;
 fail:
  return -1;
}


/**
 * Convert read data from a framebuffer to PNG pixel data.
 * 
 * @param   file        The output image file.
 * @param   buf         Buffer with read data.
 * @param   n           The number of read characters.
 * @param   width3      The width of the image multipled by 3.
 * @param   adjustment  Set to zero if all bytes were converted
 *                      (a whole number of pixels where available,)
 *                      otherwise, set to the number of bytes a
 *                      pixel is encoded.
 * @param   state       Use this to keep track of where in the you
 *                      stopped. It will be 0 on the first call.
 * @param   data        Data from `measure`.
 * @return              Zero on success, -1 on error.
 */
int
convert_fb_to_png (png_struct *pngbuf, png_byte *restrict pixbuf, const char *restrict buf, size_t n,
		   long width3, size_t *restrict adjustment, long *restrict state, void *restrict data)
{
  const uint32_t *restrict pixel;
  int r, g, b;
  size_t off;
  long x3 = *state;
  struct data d = *(struct data *)data;
  unsigned long pos = d.position;
  long lineend = width3 + d.hblank * 3;
  
  for (off = 0; off < n; off += 4, pos++)
    {
      /* A pixel in the framebuffer is formatted as `%{blue}%{green}%{red}%{x}`
	 in big-endian binary, or `%{x}%{red}%{green}%{blue}` in little-endian binary. */
      pixel = (const uint32_t *)(buf + off);
      r = (*pixel >> 16) & 255;
      g = (*pixel >> 8) & 255;
      b = (*pixel >> 0) & 255;
      
      if ((pos < d.start) || (pos >= d.end))
	continue;
      
      if (x3 < width3)
	SAVE_PNG_PIXEL (pixbuf, x3, r, g, b);
      x3 += 3;
      if (x3 == lineend)
	{
	  SAVE_PNG_ROW (pngbuf, pixbuf);
	  x3 = 0;
	}
    }
  
  *adjustment = (off != n ? 4 : 0);
  *state = x3;
  ((struct data *)data)->position = pos;
  return 0;
}