~aleteoryx/muditaos

ref: 2276ceed679b93a3a891e4f5739ade9e13991c5a muditaos/module-services/service-eink/board/linux/renderer/src/gui_renderer.cpp -rw-r--r-- 3.0 KiB
2276ceed — Radoslaw Wicik [EGD-3743] Update copyrights in fies 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

//============================================================================
// Name        : pure-gui.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>

#include <gtkmm.h>
#include <gtkmm/application.h>

#include "RWindow.hpp"
using namespace std;

static const int FrameBufferWidth  = 480;
static const int FrameBufferHeight = 600;
static const int FrameBufferSize   = FrameBufferWidth * FrameBufferHeight;

typedef struct
{
    uint32_t frameCount;
    uint32_t width;
    uint32_t height;
} shared_memory;

uint8_t *createSHMBuffer(std::string name)
{
    shared_memory *shared_mem_ptr;
    int fd_shm;

    // check if shared memory blok is already created
    if ((fd_shm = shm_open(name.c_str(), O_RDWR | O_CREAT, 0660)) == -1) {
        std::cerr << "shm is already created" << std::endl;
    }
    else {
        std::cout << "shm created" << std::endl;
        if (ftruncate(fd_shm, sizeof(shared_memory) + FrameBufferSize) == -1) {
            std::cerr << "shm is already created" << std::endl;
        }
    }
    if ((shared_mem_ptr = static_cast<shared_memory *>(
             mmap(NULL, sizeof(shared_memory) + FrameBufferSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm, 0))) ==
        MAP_FAILED) {
        std::cerr << "mmap failed" << std::endl;
    }

    return reinterpret_cast<uint8_t *>(shared_mem_ptr);
}

int createFIFO(void)
{
    int fd;
    const char *myfifo = "/tmp/myfifo3";
    fd                 = open(myfifo, O_WRONLY | O_NONBLOCK);

    if (fd < 0) {
        mkfifo(myfifo, 0666);
        fd = open(myfifo, O_WRONLY);
    }

    std::cerr << "mkfifo " << strerror(errno) << " " << myfifo << std::endl;
    //	ENOENT

    return fd;
}

int createBattFifo(void)
{
    int fd;
    const char *myfifo = "/tmp/fifoBattKeys";
    fd                 = open(myfifo, O_WRONLY | O_NONBLOCK);

    if (fd < 0) {
        mkfifo(myfifo, 0666);
        fd = open(myfifo, O_WRONLY);
    }

    std::cerr << "mkfifo " << strerror(errno) << " " << myfifo << std::endl;
    //	ENOENT

    return fd;
}
int main(int argc, char *argv[])
{

    std::string shnName = "pure_gui_fmbuf";

    shared_memory *shm_ptr = reinterpret_cast<shared_memory *>(createSHMBuffer(shnName));
    char *dataMemory       = reinterpret_cast<char *>(shm_ptr) + sizeof(shared_memory);

    int fifoFd     = createFIFO();
    int fifoBattfd = createBattFifo();

    auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

    RWindow rendererWindow(dataMemory, fifoFd, fifoBattfd, FrameBufferWidth, FrameBufferHeight);
    app->run(rendererWindow);

    //    shm_unlink(const char *name);
    return 0;
}