/* mouse_init: * Here we open the mouse device, initialise anything that needs it, * and chain to the framework init routine. */ static int mouse_init (void) { char tmp1[128], tmp2[128]; AL_CONST char *udevice; /* Set the current tool */ current_tool = default_tool; /* Find the device filename */ udevice = get_config_string (uconvert_ascii ("mouse", tmp1), uconvert_ascii ("mouse_device", tmp2), NULL); /* Open mouse device. Devices are cool. */ if (udevice) { TRACE(PREFIX_I "Trying %s device\n", udevice); intdrv.device = open_mouse_device (uconvert_toascii (udevice, tmp1)); if (intdrv.device < 0) { uszprintf (allegro_error, ALLEGRO_ERROR_SIZE, get_config_text ("Unable to open %s: %s"), udevice, ustrerror (errno)); return -1; } } else { /* If not specified in the config file, try several /dev/input/event<n> * devices. */ const char *device_name[] = { "/dev/input/event0", "/dev/input/event1", "/dev/input/event2", "/dev/input/event3", NULL }; int i; TRACE(PREFIX_I "Trying /dev/input/event[0-3] devices\n"); for (i=0; device_name[i]; i++) { intdrv.device = open_mouse_device (device_name[i]); if (intdrv.device >= 0) { break; } } if (!device_name[i]) { uszprintf (allegro_error, ALLEGRO_ERROR_SIZE, get_config_text ("Unable to open a mouse device: %s"), ustrerror (errno)); return -1; } } intdrv.num_buttons = get_num_buttons(intdrv.device); /* Init the tablet data */ init_tablet(intdrv.device); return __al_linux_mouse_init (&intdrv); }
/* load_bitmap: * Loads a bitmap from disk. */ BITMAP *load_bitmap(AL_CONST char *filename, RGB *pal) { char tmp[32], *aext; BITMAP_TYPE_INFO *iter; ASSERT(filename); aext = uconvert_toascii(get_extension(filename), tmp); for (iter = bitmap_type_list; iter; iter = iter->next) { if (stricmp(iter->ext, aext) == 0) { if (iter->load) return iter->load(filename, pal); return NULL; } } return NULL; }
/* save_bitmap: * Writes a bitmap to disk. */ int save_bitmap(AL_CONST char *filename, BITMAP *bmp, AL_CONST RGB *pal) { char tmp[32], *aext; BITMAP_TYPE_INFO *iter; ASSERT(filename); ASSERT(bmp); aext = uconvert_toascii(get_extension(filename), tmp); for (iter = bitmap_type_list; iter; iter = iter->next) { if (stricmp(iter->ext, aext) == 0) { if (iter->save) return iter->save(filename, bmp, pal); return 1; } } return 1; }
/* load_font: * Loads a font from disk. Will try to load a font from a bitmap if all else * fails. */ FONT *load_font(AL_CONST char *filename, RGB *pal, void *param) { char tmp[32], *aext; FONT_TYPE_INFO *iter; ASSERT(filename); aext = uconvert_toascii(get_extension(filename), tmp); for (iter = font_type_list; iter; iter = iter->next) { if (stricmp(iter->ext, aext) == 0) { if (iter->load) return iter->load(filename, pal, param); return NULL; } } /* Try to load the file as a bitmap image and grab the font from there */ return load_bitmap_font(filename, pal, param); }
/* register_font_file_type: * Informs Allegro of a new font file type, telling it how to load files of * this format. */ void register_font_file_type(AL_CONST char *ext, FONT *(*load)(AL_CONST char *filename, RGB *pal, void *param)) { char tmp[32], *aext; FONT_TYPE_INFO *iter = font_type_list; aext = uconvert_toascii(ext, tmp); if (strlen(aext) == 0) return; if (!iter) iter = font_type_list = _AL_MALLOC(sizeof(struct FONT_TYPE_INFO)); else { for (iter = font_type_list; iter->next; iter = iter->next); iter = iter->next = _AL_MALLOC(sizeof(struct FONT_TYPE_INFO)); } if (iter) { iter->load = load; iter->ext = strdup(aext); iter->next = NULL; } }
/* register_bitmap_file_type: * Informs Allegro of a new image file type, telling it how to load and * save files of this format (either function may be NULL). */ void register_bitmap_file_type(AL_CONST char *ext, BITMAP *(*load)(AL_CONST char *filename, RGB *pal), int (*save)(AL_CONST char *filename, BITMAP *bmp, AL_CONST RGB *pal)) { char tmp[32], *aext; BITMAP_TYPE_INFO *iter = bitmap_type_list; aext = uconvert_toascii(ext, tmp); if (strlen(aext) == 0) return; if (!iter) iter = bitmap_type_list = _AL_MALLOC(sizeof(struct BITMAP_TYPE_INFO)); else { for (iter = bitmap_type_list; iter->next; iter = iter->next); iter = iter->next = _AL_MALLOC(sizeof(struct BITMAP_TYPE_INFO)); } if (iter) { iter->load = load; iter->save = save; iter->ext = _al_strdup(aext); iter->next = NULL; } }
/* mouse_init: * Here we open the mouse device, initialise anything that needs it, * and chain to the framework init routine. */ static int mouse_init (void) { char tmp1[128], tmp2[128], tmp3[128]; AL_CONST char *udevice; /* Find the device filename */ udevice = get_config_string (uconvert_ascii ("mouse", tmp1), uconvert_ascii ("mouse_device", tmp2), uconvert_ascii (DEVICE_FILENAME, tmp3)); /* Open mouse device. Devices are cool. */ intdrv.device = open (uconvert_toascii (udevice, tmp1), O_RDONLY | O_NONBLOCK); if (intdrv.device < 0) { uszprintf (allegro_error, ALLEGRO_ERROR_SIZE, get_config_text ("Unable to open %s: %s"), udevice, ustrerror (errno)); return -1; } /* Discard any garbage, so the next thing we read is a packet header */ sync_mouse (intdrv.device); return __al_linux_mouse_init (&intdrv); }
static int joy_init(void) { JOYSTICK_INFO *j; AL_CONST char *device_name = NULL; char tmp[128], tmp1[128], tmp2[128]; unsigned int raw_version; struct { unsigned char build, minor, major; } version; char num_axes, num_buttons; int throttle; int i, s, a, b; for (i = 0; i < MAX_JOYSTICKS; i++) { /* Check for a user override on the device to use. */ uszprintf(tmp, sizeof(tmp), uconvert_ascii("joystick_device_%d", tmp1), i); device_name = get_config_string(uconvert_ascii("joystick", tmp1), tmp, NULL); /* Special case for the first joystick. */ if (!device_name && (i == 0)) device_name = get_config_string(uconvert_ascii("joystick", tmp1), uconvert_ascii("joystick_device", tmp2), NULL); if (device_name) { joy_fd[i] = open(uconvert_toascii(device_name, tmp), O_RDONLY|O_NONBLOCK); if (joy_fd[i] == -1) break; } else { snprintf(tmp, sizeof(tmp), "/dev/input/js%d", i); tmp[sizeof(tmp)-1] = 0; joy_fd[i] = open(tmp, O_RDONLY|O_NONBLOCK); if (joy_fd[i] == -1) { snprintf(tmp, sizeof(tmp), "/dev/js%d", i); tmp[sizeof(tmp)-1] = 0; joy_fd[i] = open(tmp, O_RDONLY|O_NONBLOCK); if (joy_fd[i] == -1) break; } } if (ioctl(joy_fd[i], JSIOCGVERSION, &raw_version) < 0) { /* NOTE: IOCTL fails if the joystick API is version 0.x */ uszprintf(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Your Linux joystick API is version 0.x which is unsupported.")); return -1; } version.major = (raw_version & 0xFF0000) >> 16; version.minor = (raw_version & 0xFF00) >> 8; version.build = (raw_version & 0xFF); ioctl(joy_fd[i], JSIOCGAXES, &num_axes); ioctl(joy_fd[i], JSIOCGBUTTONS, &num_buttons); if (num_axes > TOTAL_JOYSTICK_AXES) num_axes = TOTAL_JOYSTICK_AXES; if (num_buttons > MAX_JOYSTICK_BUTTONS) num_buttons = MAX_JOYSTICK_BUTTONS; /* User is allowed to override our simple assumption of which * axis number (kernel) the throttle is located at. */ uszprintf(tmp, sizeof(tmp), uconvert_ascii("throttle_axis_%d", tmp1), i); throttle = get_config_int(uconvert_ascii("joystick", tmp1), tmp, -1); if (throttle == -1) { throttle = get_config_int(uconvert_ascii("joystick", tmp1), uconvert_ascii("throttle_axis", tmp2), -1); } /* Each pair of axes is assumed to make up a stick unless it * is the sole remaining axis, or has been user specified, in * which case it is a throttle. */ j = &joy[i]; j->flags = JOYFLAG_ANALOGUE; for (s = 0, a = 0; (s < MAX_JOYSTICK_STICKS) && (a < num_axes); s++) { if ((a == throttle) || (a == num_axes-1)) { /* One axis throttle */ j->stick[s].flags = JOYFLAG_ANALOGUE | JOYFLAG_UNSIGNED; j->stick[s].num_axis = 1; j->stick[s].axis[0].name = get_config_text("Throttle"); j->stick[s].name = ustrdup(j->stick[s].axis[0].name); axis[i][a++] = &j->stick[s].axis[0]; } else { /* Two axis stick. */ j->stick[s].flags = JOYFLAG_ANALOGUE | JOYFLAG_SIGNED; j->stick[s].num_axis = 2; j->stick[s].axis[0].name = get_config_text("X"); j->stick[s].axis[1].name = get_config_text("Y"); j->stick[s].name = _AL_MALLOC_ATOMIC(32); ASSERT(j->stick[s].name); uszprintf((char *)j->stick[s].name, 32, get_config_text("Stick %d"), s+1); axis[i][a++] = &j->stick[s].axis[0]; axis[i][a++] = &j->stick[s].axis[1]; } } j->num_sticks = s; for (b = 0; b < num_buttons; b++) { j->button[b].name = _AL_MALLOC_ATOMIC(16); ASSERT(j->button[b].name); uszprintf((char *)j->button[b].name, 16, uconvert_ascii("%c", tmp), 'A' + b); } j->num_buttons = num_buttons; } num_joysticks = i; if (num_joysticks == 0) { uszprintf(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Unable to open %s: %s"), device_name ? device_name : uconvert_ascii("/dev/js0", tmp), ustrerror(errno)); return -1; } return 0; }
/* _unix_load_modules: * Find a modules.lst file and load the modules listed in it. */ void _unix_load_modules(int system_driver) { PACKFILE *f; char fullpath[1024]; char *fullpath_slash; char buf[1024]; char buf2[1024]; char **pathptr; char *filename; void *handle; void (*init)(int); MODULE *m; /* Read the ALLEGRO_MODULES environment variable. * But don't do it if we are root (for obvious reasons). */ if (geteuid() != 0) { char *env = getenv("ALLEGRO_MODULES"); if (env) { snprintf(fullpath, sizeof fullpath, "%s/%s", env, "modules.lst"); fullpath[(sizeof fullpath) - 1] = 0; f = pack_fopen(uconvert_ascii(fullpath, buf), F_READ); if (f) goto found; } } for (pathptr = module_path; *pathptr; pathptr++) { snprintf(fullpath, sizeof fullpath, "%s/%d.%d.%d/modules.lst", *pathptr, ALLEGRO_VERSION, ALLEGRO_SUB_VERSION, ALLEGRO_WIP_VERSION); fullpath[(sizeof fullpath) - 1] = 0; f = pack_fopen(uconvert_ascii(fullpath, buf), F_READ); if (f) goto found; } return; found: TRACE(PREFIX_I "Loading modules from \"%s\".\n", fullpath); fullpath_slash = strrchr(fullpath, '/'); while (true) { if (!pack_fgets(buf, sizeof buf, f)) break; filename = uconvert_toascii(buf, buf2); strip(filename); if ((filename[0] == '#') || (strlen(filename) == 0)) continue; if (!fullpath_slash) { snprintf(fullpath, sizeof fullpath, filename); fullpath[(sizeof fullpath) - 1] = 0; } else { snprintf(fullpath_slash+1, (sizeof fullpath) - (fullpath_slash - fullpath) - 1, filename); fullpath[(sizeof fullpath) - 1] = 0; } if (!exists(uconvert_ascii(fullpath, buf))) continue; handle = dlopen(fullpath, RTLD_NOW); if (!handle) { /* useful during development */ /* printf("Error loading module: %s\n", dlerror()); */ continue; } init = dlsym(handle, "_module_init"); if (init) init(system_driver); m = al_malloc(sizeof(MODULE)); if (m) { m->handle = handle; m->next = module_list; module_list = m; } } pack_fclose(f); }