The Pedigree Project  0.1
util-linux.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifdef TARGET_LINUX
21 
22 #include <assert.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <syslog.h>
29 #include <unistd.h>
30 
31 #include <SDL/SDL.h>
32 
33 #include "util.h"
34 
35 size_t SharedBuffer::m_NextId = 0;
36 
37 Framebuffer::Framebuffer()
38  : m_pFramebuffer(0), m_FramebufferSize(0), m_Format(), m_Width(0),
39  m_Height(0), m_pScreen(0), m_pBackbuffer(0)
40 {
41 }
42 
43 Framebuffer::~Framebuffer()
44 {
45  SDL_FreeSurface(m_pBackbuffer);
46 }
47 
49 {
50  return true;
51 }
52 
54 {
55  // no-op for SDL
56 }
57 
59 {
60  // no-op for SDL
61 }
62 
63 int Framebuffer::enterMode(size_t desiredW, size_t desiredH, size_t desiredBpp)
64 {
65  m_pScreen = SDL_SetVideoMode(
66  desiredW, desiredH, desiredBpp, SDL_DOUBLEBUF | SDL_SWSURFACE);
68 
69  m_pBackbuffer = SDL_CreateRGBSurface(
70  SDL_DOUBLEBUF | SDL_SWSURFACE, desiredW, desiredH, 32, 0x00FF0000,
71  0x0000FF00, 0x000000FF, 0);
72 
73  m_pFramebuffer = (void *) m_pBackbuffer->pixels;
74 
75  m_Width = m_pScreen->w;
76  m_Height = m_pScreen->h;
77  m_Format = CAIRO_FORMAT_RGB24;
78 
79  return 0;
80 }
81 
82 void Framebuffer::flush(size_t x, size_t y, size_t w, size_t h)
83 {
84  // Don't bother doing a dirty rectangle flush just yet.
85  SDL_BlitSurface(m_pBackbuffer, NULL, m_pScreen, NULL);
86  SDL_Flip(m_pScreen);
87 }
88 
89 SharedBuffer::SharedBuffer(size_t size)
90  : m_ShmName(), m_ShmFd(-1), m_pBuffer(0), m_Size(size)
91 {
92  size_t bufferId = m_NextId++;
93 
94  memset(m_ShmName, 0, sizeof m_ShmName);
95  sprintf(m_ShmName, "wm%zd", bufferId);
96 
97  m_ShmFd = shm_open(m_ShmName, O_RDWR | O_CREAT, 0777);
98  syslog(LOG_INFO, "opening shm %s [fd=%d]", m_ShmName, m_ShmFd);
99  int r = ftruncate(m_ShmFd, size);
100  assert(r == 0);
101 
102  m_pBuffer = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_ShmFd, 0);
103 }
104 
105 SharedBuffer::SharedBuffer(size_t size, void *handle)
106  : m_ShmName(), m_ShmFd(-1), m_pBuffer(0), m_Size(size)
107 {
109  memcpy(m_ShmName, &handle, 8);
110 
111  m_ShmFd = shm_open(m_ShmName, O_RDWR, 0777);
112  syslog(LOG_INFO, "opening client shm %s [fd=%d]", m_ShmName, m_ShmFd);
113 
114  m_pBuffer = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_ShmFd, 0);
115 }
116 
118 {
119  syslog(LOG_INFO, "unmapping %zd bytes @%p", m_Size, m_pBuffer);
120  munmap(m_pBuffer, m_Size);
121  close(m_ShmFd);
122  shm_unlink(m_ShmName);
123 }
124 
126 {
127  uint64_t v = 0;
128  memcpy(&v, m_ShmName, 8);
129  return (void *) v;
130 }
131 
133 {
134  return m_pBuffer;
135 }
136 
137 #endif
void restoreMode()
Definition: fb.cc:85
int enterMode(size_t desiredW, size_t desiredH, size_t desiredBpp)
Definition: fb.cc:98
bool initialise()
Definition: fb.cc:56
void flush(size_t x, size_t y, size_t w, size_t h)
Definition: fb.cc:186
#define assert(x)
Definition: assert.h:37
void storeMode()
Definition: fb.cc:70