Hello, I'm trying to run a minimal SDL2 window under X11.
When binary is run with sudo, it works.
With standard user permissions, I get an "Inappropriate ioctl for device".
My test user is in the "video" group.
Does someone know why ?
Thanks for your help.
When binary is run with sudo, it works.
With standard user permissions, I get an "Inappropriate ioctl for device".
My test user is in the "video" group.
Does someone know why ?
Thanks for your help.
Makefile:
CC = clang++
CFLAGS = -g -std=c++17 -Wunused-variable
drm_cflags = -I/usr/local/include/SDL2 -I/usr/local/include -L/usr/local/lib
targets = sdltest
all: $(targets)
sdltest: sdltest.cpp
$(CC) $(CFLAGS) $(drm_cflags) -o $@ $^ -lncursesw -lSDL2
.PHONY: clean
clean:
$(RM) *.o
$(RM) $(targets)
C++:
#include <SDL.h>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <curses.h>
#include <iostream>
#include <fstream>
// #define DRIVER_NAME "KMSDRM"
#define DRIVER_NAME "x11"
// #define DRIVER_NAME "directfb"
// #define DRIVER_NAME "wayland"
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
WINDOW *win = NULL;
std::ofstream logger("log.txt");
void freeIfNeeded(SDL_Window *window, SDL_Renderer *renderer, WINDOW *win)
{
if (renderer) SDL_DestroyRenderer(renderer);
if (window) SDL_DestroyWindow(window);
if (win) endwin();
logger.close();
}
int main(int argc, char *argv[])
{
int ch;
win = initscr();
nodelay(win, true);
noecho();
SDL_Init(0);
// Find the driver
bool init = false;
for (int i = 0; i < SDL_GetNumVideoDrivers(); i++) {
const char *name = SDL_GetVideoDriver(i);
if (strcmp(name, DRIVER_NAME) == 0) {
SDL_VideoInit(name);
init = true;
break;
}
}
if (!init) {
perror("Couldn't find a suitable video driver !");
logger << "Couldn't find a suitable video driver !" << std::endl;
freeIfNeeded(window, renderer, win);
return -1;
}
// logger << "SDL Video Driver selected :" << SDL_GetCurrentVideoDriver() << std::endl;
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow(
"sdl",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_SHOWN);
if (window == nullptr) {
perror("Could not create window !");
logger << "Could not create window !" << std::endl;
freeIfNeeded(window, renderer, win);
return -1;
}
// select resolution and refresh rate of the main window
// i915 kmsdrm : rgba order
const SDL_DisplayMode mode = { SDL_PIXELTYPE(SDL_PIXELTYPE_ARRAYU32), 640, 480, 75, 0 };
SDL_SetWindowDisplayMode(window, &mode);
for (int i = 0; i < SDL_GetNumRenderDrivers(); i++) {
SDL_RendererInfo info;
SDL_GetRenderDriverInfo(i, &info);
logger << "Renderer[" << i << "]=" << info.name << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) {
perror("Could not create renderer !");
logger << "Could not create renderer !" << std::endl;
freeIfNeeded(window, renderer, win);
return -1;
}
SDL_RendererInfo rendererInfo;
SDL_GetRendererInfo(renderer, &rendererInfo);
logger << "Current renderer" << rendererInfo.name << std::endl;
SDL_ShowCursor(false);
SDL_RenderClear(renderer);
bool loop = true;
int i = 0;
while (loop) {
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
loop = false;
}
}
// KMSDRM : without window manager, keyboard events must be read on term
// curses keyboard management
ch = getch();
if (ch == 'q') {
loop = false;
}
SDL_SetRenderDrawColor(renderer, 0x00, 0x80, 0x80, 0xFF);
SDL_RenderClear(renderer);
if (i % 2 == 0)
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
else
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
SDL_RenderDrawLine(renderer, 0, 0, 640, 480);
SDL_RenderPresent(renderer);
SDL_Delay(20);
i++;
}
freeIfNeeded(window, renderer, win);
SDL_Quit();
}