// File: /workspace/github_c_makefile_star2_O0/OnionIoT[P]lv_example/lv_drivers/display/fbdev.c
// Function: fbdev_init
void fbdev_init(void)
{
    // Open the file for reading and writing
    fbfd = open(FBDEV_PATH, O_RDWR);
    if(fbfd == -1) {
        perror("Error: cannot open framebuffer device");
        return;
    }
    printf("The framebuffer device was opened successfully.\n");

#if USE_BSD_FBDEV
    struct fbtype fb;
    unsigned line_length;

    //Get fb type
    if (ioctl(fbfd, FBIOGTYPE, &fb) != 0) {
        perror("ioctl(FBIOGTYPE)");
        return;
    }

    //Get screen width
    if (ioctl(fbfd, FBIO_GETLINEWIDTH, &line_length) != 0) {
        perror("ioctl(FBIO_GETLINEWIDTH)");
        return;
    }

    vinfo.xres = (unsigned) fb.fb_width;
    vinfo.yres = (unsigned) fb.fb_height;
    vinfo.bits_per_pixel = fb.fb_depth + 8;
    vinfo.xoffset = 0;
    vinfo.yoffset = 0;
    finfo.line_length = line_length;
#else /* USE_BSD_FBDEV */

    // Get fixed screen information
    if(ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
        perror("Error reading fixed information");
        return;
    }

    // Get variable screen information
    if(ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
        perror("Error reading variable information");
        return;
    }
#endif /* USE_BSD_FBDEV */

    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);

    // Figure out the size of the screen in bytes
    screensize =  finfo.smem_len; //finfo.line_length * vinfo.yres;    

    // Map the device to memory
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
    if((intptr_t)fbp == -1) {
        perror("Error: failed to map framebuffer device to memory");
        return;
    }
    memset(fbp, 0, screensize);

    printf("The framebuffer device was mapped to memory successfully.\n");

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
leaq 0x558b(%rip), %rdi      # 0x6c0b6
movl $0x2, %esi
movb $0x0, %al
callq 0x20f0
movl %eax, 0x34093(%rip)     # 0x9abd0
cmpl $-0x1, 0x3408c(%rip)    # 0x9abd0
jne 0x66b57
leaq 0x5572(%rip), %rdi      # 0x6c0bf
callq 0x2100
jmp 0x66c5a
leaq 0x5587(%rip), %rdi      # 0x6c0e5
movb $0x0, %al
callq 0x2080
movl 0x34065(%rip), %edi     # 0x9abd0
movl $0x4602, %esi           # imm = 0x4602
leaq 0x34061(%rip), %rdx     # 0x9abd8
movb $0x0, %al
callq 0x20a0
cmpl $-0x1, %eax
jne 0x66b94
leaq 0x558c(%rip), %rdi      # 0x6c116
callq 0x2100
jmp 0x66c5a
movl 0x34036(%rip), %edi     # 0x9abd0
movl $0x4600, %esi           # imm = 0x4600
leaq 0x34082(%rip), %rdx     # 0x9ac28
movb $0x0, %al
callq 0x20a0
cmpl $-0x1, %eax
jne 0x66bc3
leaq 0x557d(%rip), %rdi      # 0x6c136
callq 0x2100
jmp 0x66c5a
movl 0x3405f(%rip), %esi     # 0x9ac28
movl 0x3405d(%rip), %edx     # 0x9ac2c
movl 0x3406b(%rip), %ecx     # 0x9ac40
leaq 0x557d(%rip), %rdi      # 0x6c159
movb $0x0, %al
callq 0x2080
movl 0x34007(%rip), %eax     # 0x9abf0
movq %rax, 0x340d8(%rip)     # 0x9acc8
movq 0x340d1(%rip), %rsi     # 0x9acc8
movl 0x33fd2(%rip), %r8d     # 0x9abd0
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x2070
movq %rax, 0x340b4(%rip)     # 0x9acd0
movq 0x340ad(%rip), %rax     # 0x9acd0
cmpq $-0x1, %rax
jne 0x66c37
leaq 0x5537(%rip), %rdi      # 0x6c167
callq 0x2100
jmp 0x66c5a
movq 0x34092(%rip), %rdi     # 0x9acd0
movq 0x34083(%rip), %rdx     # 0x9acc8
xorl %esi, %esi
callq 0x2090
leaq 0x5546(%rip), %rdi      # 0x6c199
movb $0x0, %al
callq 0x2080
popq %rbp
retq
nopl (%rax)