forked from SantX27/miyoo_st-sdl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for running sdl programs
- Loading branch information
Showing
6 changed files
with
221 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.