Skip to content

Commit

Permalink
add support for running sdl programs
Browse files Browse the repository at this point in the history
  • Loading branch information
benob committed Feb 23, 2019
1 parent bed7d75 commit e220ea5
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 21 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@

include config_rs97.mk

SRC = st.c keyboard.c font.c
SRC = st.c keyboard.c font.c msg_queue.c
OBJ = ${SRC:.c=.o}

all: options st
all: options st libst-preload.so sdl_test

libst-preload.so: st-preload.o msg_queue.o
${CC} -shared -o $@ $^

sdl_test: sdl_test.o

options:
@echo st build options:
Expand Down
42 changes: 42 additions & 0 deletions msg_queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>

#include "msg_queue.h"

queue_t queue_create() {
key_t keyval = ftok(QUEUE_NAME, QUEUE_ID);
return msgget( keyval, IPC_CREAT | 0660 );
}

queue_t queue_open() {
key_t keyval = ftok(QUEUE_NAME, QUEUE_ID);
return msgget( keyval, 0660 );
}

int queue_send(queue_t qid, message_t *message) {
int length = sizeof(message_t) - sizeof(long);

return msgsnd( qid, message, length, 0);
}

int queue_read(queue_t qid, long type, message_t *message) {
int length = sizeof(message_t) - sizeof(long);

return msgrcv( qid, message, length, type, 0);
}

int queue_peek(queue_t qid, long type) {
if(msgrcv( qid, NULL, 0, type, IPC_NOWAIT) == -1) {
if(errno == E2BIG) return 1;
}

return 0;
}

int queue_remove(queue_t qid) {
return msgctl(qid, IPC_RMID, 0);
}

23 changes: 23 additions & 0 deletions msg_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __MSG_QUEUE__
#define __MSG_QUEUE__

#define QUEUE_NAME "st-sdl"
#define QUEUE_ID 1

typedef struct {
long type;
long data;
} message_t;

typedef int queue_t;

enum { MSG_CLIENT=1, MSG_SERVER, MSG_REQUEST_SHUTDOWN, MSG_SHUTDOWN, MSG_REQUEST_INIT };

queue_t queue_create();
queue_t queue_open();
int queue_send(queue_t qid, message_t *message);
int queue_read(queue_t qid, long type, message_t *message);
int queue_peek(queue_t qid, long type);
int queue_remove(queue_t qid);

#endif
38 changes: 38 additions & 0 deletions sdl_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <SDL/SDL.h>

int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
printf("Testing SDL\n");

SDL_Surface* screen = SDL_SetVideoMode(320, 240, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);

int quit = 0;
while( !quit ){
SDL_Event event;
while( SDL_PollEvent( &event ) ){

switch( event.type ){
case SDL_KEYDOWN:
case SDL_KEYUP:
quit = 1;
break;
case SDL_QUIT:
quit = 1;
break;
default:
break;
}

}
SDL_Rect rect;
rect.x = rand() % screen->w;
rect.y = rand() % screen->h;
rect.w = rand() % (screen->w - rect.x);
rect.h = rand() % (screen->h - rect.y);
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, rand() % 255, rand() % 255, rand() % 255));
SDL_Flip(screen);
SDL_Delay(1000 / 60);
}

SDL_Quit();
}
37 changes: 37 additions & 0 deletions st-preload.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>

#include "msg_queue.h"

static int sdl_init_was_called = 0;
static int (*real_SDL_Init)(int) = NULL;

int SDL_Init(int flags) {
if (sdl_init_was_called == 0) {
queue_t qid = queue_open();
if(qid != -1) {
message_t message = {.type = MSG_CLIENT, .data = MSG_REQUEST_SHUTDOWN };
queue_send(qid, &message);
queue_read(qid, MSG_SERVER, &message);
}

real_SDL_Init = dlsym(RTLD_NEXT, "SDL_Init");
sdl_init_was_called = 1;
}
return real_SDL_Init(flags);
}

void __attribute__((destructor)) preload_cleanup() {
if(sdl_init_was_called) {
queue_t qid = queue_open();
if(qid != -1) {
message_t message = {.type = MSG_CLIENT, .data = MSG_REQUEST_INIT };
queue_send(qid, &message);
}
}
}

Loading

0 comments on commit e220ea5

Please sign in to comment.