Skip to content

Commit

Permalink
fix segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Taylor committed Nov 8, 2010
1 parent df36438 commit bc8025d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 72 deletions.
3 changes: 1 addition & 2 deletions Makefile
Expand Up @@ -8,8 +8,7 @@ sources = $(wildcard *.c)
headers = $(wildcard *.h)
objects = $(patsubst %.c,%.o,$(sources))

#libraries = -lwiiuse -lpthread -lrt -lcsfml-graphics
libraries = -lwiiuse -lpthread -lrt -lSDL
libraries = -lwiiuse -lpthread -lrt -lSDL -lGL

all: $(target)

Expand Down
52 changes: 15 additions & 37 deletions display.c
Expand Up @@ -4,9 +4,7 @@
#include <unistd.h>

#include <SDL/SDL.h>
#if 0
#include <SFML/Graphics.h>
#endif
#include <GL/gl.h>

#include "controller.h"
#include "controller_state.h"
Expand All @@ -16,20 +14,15 @@

/* - - - - - - - - - - - - - - - - - - - - */

struct {
static struct {
SDL_Surface *surface;
} g;

#if 0
struct {
sfRenderWindow *window;
} g;
#endif

/* - - - - - - - - - - - - - - - - - - - - */

static void graphics_init(void);
static void graphics_deinit(void);
static void draw_display(void);

/* - - - - - - - - - - - - - - - - - - - - */

Expand All @@ -49,7 +42,9 @@ display_run(void *v)
break;
}

usleep(10000);
SDL_Delay(100);
draw_display();
print_info("display looping");
}

graphics_deinit();
Expand All @@ -61,39 +56,22 @@ display_run(void *v)

/* - - - - - - - - - - - - - - - - - - - - */

void graphics_init(void)
{
SDL_Init(SDL_INIT_VIDEO);
g.surface = SDL_SetVideoMode(640, 480, 8, SDL_OPENGL);
static void draw_display(void) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
SDL_GL_SwapBuffers();
}

void graphics_deinit(void)
{
SDL_Quit();
}
/* - - - - - - - - - - - - - - - - - - - - */

#if 0
void graphics_init(void)
{
sfVideoMode video_mode = {
.Width = 320,
.Height = 480,
.BitsPerPixel = 32
};

sfWindowSettings window_settings = {
.DepthBits = 24,
.StencilBits = 8,
.AntialiasingLevel = 2
};

g.window = sfRenderWindow_Create(video_mode, "quiz", sfResize | sfClose, window_settings);
sfRenderWindow_Clear(g.window, sfBlack);
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
g.surface = SDL_SetVideoMode(640, 480, 24, SDL_OPENGL);
}

void graphics_deinit(void)
{
sfRenderWindow_Close(g.window);
sfRenderWindow_Destroy(g.window);
SDL_Quit();
}
#endif
27 changes: 19 additions & 8 deletions main.c
Expand Up @@ -55,29 +55,36 @@ main(int argc, char **argv)
g.buttonsq = rqueue_new();
g.hlcommandsq = rqueue_new();

/* start controller thread */
/* controller thread arguments */
c_args.ending = g.ending;
c_args.buttonsq = g.buttonsq;
c_args.hlcommandsq = g.hlcommandsq;
pthread_create(&g.threads[0], NULL, controller_run, &c_args);

/* start wiimotes thread */
/* wiimote thread arguments */
w_args.ending = g.ending;
w_args.max_num_wiimotes = WIIMOTES_MAX_NUM;
w_args.find_time_in_sec = WIIMOTES_FIND_TIME_IN_SEC;
w_args.buttonsq = g.buttonsq;
w_args.hlcommandsq = g.hlcommandsq;
pthread_create(&g.threads[1], NULL, wiimotes_run, &w_args);

/* display thread arguments */
d_args.ending = g.ending;
d_args.cs = g.cs;

struct controller_state_s s;
memset(&s, 0, sizeof(s));
g.cs = controller_state_new(&s);

/* start controller thread */
pthread_create(&g.threads[0], NULL, controller_run, &c_args);

/* start wiimotes thread */
pthread_create(&g.threads[1], NULL, wiimotes_run, &w_args);

/* start display thread */
d_args.ending = g.ending;
d_args.cs = g.cs;
pthread_create(&g.threads[2], NULL, display_run, &d_args);


/* wait for ctrl-c */
sigemptyset(&sigs);
sigaddset(&sigs, SIGINT);
Expand All @@ -88,10 +95,9 @@ main(int argc, char **argv)
if (sig == SIGINT) {
ending_set(g.ending, true);

int i;
int num_threads = sizeof(g.threads) / sizeof(pthread_t);

for (i=0; i<num_threads; i++) {
for (int i=0; i<num_threads; i++) {
pthread_join(g.threads[i], NULL);
}

Expand All @@ -106,5 +112,10 @@ main(int argc, char **argv)

print_info("main exiting");

ending_free(g.ending);
rqueue_free(g.buttonsq);
rqueue_free(g.hlcommandsq);
controller_state_free(g.cs);

return EXIT_SUCCESS;
}
44 changes: 19 additions & 25 deletions wiimotes.c
Expand Up @@ -7,7 +7,6 @@
#include <signal.h>
#include <errno.h>

#include <pthread.h>
#include <wiiuse.h>

#include "button_event.h"
Expand Down Expand Up @@ -75,7 +74,7 @@ wiimotes_run(void *v)
num_events = wiiuse_poll(w->wiimotes, w->size);
for (i=0; num_events>0 && i<w->size; i++) {
if (w->wiimotes[i]->event == WIIUSE_EVENT) {
handle_button_event(w, i, buttonsq);
handle_button_event(w, w->wiimotes[i]->unid, buttonsq);
num_events--;
}
}
Expand All @@ -96,6 +95,7 @@ wiimotes_run(void *v)
when_to_send = llcmdqueue_peek_key(llcommandsq);
if (when_to_send <= t) {
llc = llcmdqueue_remove(llcommandsq);
print_info("DEQUEUED LLC @ %ld", when_to_send);
handle_lowlevel_command(llc, w);
lowlevel_command_free(llc);
} else {
Expand All @@ -104,6 +104,7 @@ wiimotes_run(void *v)
}

if (ending_get(ending)) {
print_info("wimotes thread ending");
break;
}

Expand Down Expand Up @@ -190,14 +191,10 @@ calc_leds_by_bits(int bits)
}

static void
handle_button_event(wiimotes_t w, int n, rqueue_t buttonsq)
handle_button_event(wiimotes_t w, int wiimote_num, rqueue_t buttonsq)
{
struct wiimote_t *wm;
int id;

wm = w->wiimotes[n];
id = wm->unid;

wiimote *wm = wiiuse_get_by_id(w->wiimotes, w->size, wiimote_num);
int id = wm->unid;
if (IS_PRESSED(wm, WIIMOTE_BUTTON_A)) add_button_event(buttonsq, id, WIIMOTE_BUTTON_A);
if (IS_PRESSED(wm, WIIMOTE_BUTTON_B)) add_button_event(buttonsq, id, WIIMOTE_BUTTON_B);
if (IS_PRESSED(wm, WIIMOTE_BUTTON_UP)) add_button_event(buttonsq, id, WIIMOTE_BUTTON_UP);
Expand Down Expand Up @@ -265,24 +262,21 @@ handle_highlevel_command(const struct highlevel_command *hlc, llcmdqueue_t llcom
static void
handle_lowlevel_command(const struct lowlevel_command *llc, wiimotes_t w)
{
int wmidx = llc->wiimote_num - 1;

if (wmidx < 0 || wmidx >= w->size) {
print_info("bad wiimote index: %d w->size:%d", wmidx, w->size);
} else {
struct wiimote_t *wm = w->wiimotes[wmidx];
wiimote *wm = wiiuse_get_by_id(w->wiimotes, w->size, llc->wiimote_num);

switch (llc->type) {
case LOWLEVEL_COMMAND_SET_LEDS:
wiiuse_set_leds(wm, llc->parameters.set_leds.leds);
break;
switch (llc->type) {
case LOWLEVEL_COMMAND_SET_LEDS:
print_info("LOWLEVEL_COMMAND_SET_LEDS %d -> %d", llc->parameters.set_leds.leds, llc->wiimote_num);
wiiuse_set_leds(wm, llc->parameters.set_leds.leds);
break;

case LOWLEVEL_COMMAND_RUMBLE:
wiiuse_rumble(wm, llc->parameters.rumble.active ? 1 : 0);
break;
case LOWLEVEL_COMMAND_RUMBLE:
print_info("LOWLEVEL_COMMAND_RUMBLE %d -> %d", llc->parameters.rumble.active, llc->wiimote_num);
wiiuse_rumble(wm, llc->parameters.rumble.active ? 1 : 0);
break;

case LOWLEVEL_COMMAND_UNKNOWN:
break;
}
case LOWLEVEL_COMMAND_UNKNOWN:
print_info("LOWLEVEL_COMMAND_UNKNOWN -> %d");
break;
}
}

0 comments on commit bc8025d

Please sign in to comment.