Stuff

Odds and ends that don't really deserve their own page.
Simple SWF information C program

Here's a (very) small program to output some basic information about Adobe Flash (SWF) files. It's only 900 bytes in size, and doesn't do much error checking...

#include <stdio.h>

int main (int argc, char *argv[]) {

  FILE *swf;
  unsigned char cmp, chr;
  unsigned long len;
  int i;

  for (i = 1; i < argc; i++) {

    swf = fopen (argv[i], "rb");

    if (swf) {

      cmp = fgetc (swf);
      chr = fgetc (swf);

      if (fgetc (swf) == 'S' && chr == 'W') {

        printf ("FILENAME: %s\nUSE ZLIB: ", argv[i]);

        if      (cmp == 'C') printf ("Yes");
        else if (cmp == 'F') printf ("No");
        else                 printf ("?");

        printf ("\nSWF TYPE: %d\n", fgetc (swf));

        len  = fgetc (swf);
        len += fgetc (swf) * 256;
        len += fgetc (swf) * 256 * 256;
        len += fgetc (swf) * 256 * 256 * 256;

        printf ("DATASIZE: %ld\n\n", len);

      } else { printf ("%s is not an SWF.\n\n", argv[i]); }

      fclose (swf);

    } else { printf ("Could not open %s\n\n", argv[i]); }

  }

  return 0;

}

To compile this program, simply save it as "swfinfo.c" and type the following into BASH:

gcc -o swfinfo swfinfo.c

You can then run the program as follows:

thomas@debian:~/Programming$ ./swfinfo ming-example.swf
FILENAME: ming-example.swf
USE ZLIB: Yes
SWF TYPE: 8
DATASIZE: 923

thomas@debian:~/Programming$

This program should be fully portable across all operating systems and architectures.

Cross-platform milliseconds function (C/C++)

Something that I didn't realise for a long time was that the clock() function in C/C++ doesn't always return results in milliseconds - it actually varies by platform. The value returned is actually determined by CLOCKS_PER_SEC.

When CLOCKS_PER_SEC is 1000, there isn't a problem - every second is divided into 1000 units AKA milliseconds. However, if a platform defines CLOCKS_PER_SEC as 100, two seconds would be represented as 200 rather than 2000.

Windows NT systems (including XP, Vista, and 7) define CLOCKS_PER_SEC as 1000. However, MacOSX and GNU/Linux do not, which can be a problem for programmers wanting to write cross-platform, time-sensitive applications.

In order to avoid running into this issue when porting a program from one platform to another, the following code can be used:

/* [PUBLIC] Convert the return value of clock() into milliseconds. */

clock_t millisecs (void)
{

  clock_t timer = clock () * 1000;

  return timer / CLOCKS_PER_SEC;

}


This code simply multiplies the result of clock() by 1000 (i.e. the number of milliseconds in one second) and then divides the result by the original number reported.

For platforms where CLOCKS_PER_SEC is 1000, this function would simply multiply the value by 1000 before dividing it again so that the original value was returned.

However, if CLOCKS_PER_SEC were to be 1 (i.e. 1 clock = 1 second) then this code would multiply the result by 1000 (converting it to milliseconds) before dividing it by 1 - which would have no effect, leaving the result in milliseconds.

Microsoft Xbox 360 Controller Definitions (Windows)

The following file can be used in combination with the official Microsoft DirectInput driver on Windows platforms. It was written and tested using Windows XP.

Note that the triggers are treated as a single axis, so that pressing both at the same time causes the two inputs to cancel each other out. This is a limitation of the DirectInput driver - if you need access to each trigger individually, consider using the newer XInput method instead.

Also, the central Xbox 360 Menu button can't be used, due to Microsoft reserving it for possible future use.

/*
 
Copyright (c) 2009-2010 Thomas Dennis.
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
 
*/
 
#ifndef __TGDXBOX360_H__
#define __TGDXBOX360_H__
 
#ifdef __cplusplus
extern "C" {
#endif
 
/* Driver string - use this to verify that the expected driver is being used. */
 
#define XBOX_DRIVER_NAME "XBOX 360 For Windows (Controller)"
 
/* Define the number of hats, buttons & axes (Also balls - for completeness). */
 
#define XBOX_MAX_AXES 5
#define XBOX_MAX_BTTN 10
#define XBOX_MAX_BALL 0
#define XBOX_MAX_HATS 1
 
/* Axis list. */
 
#define XBOX_AXIS_LEFTX   0 /* Left stick X axis.  */
#define XBOX_AXIS_LEFTY   1 /* Left stick Y axis.  */
#define XBOX_AXIS_TRIGGER 2 /* L & R Trigger axis. */
#define XBOX_AXIS_RIGHTY  3 /* Right stick Y axis. */
#define XBOX_AXIS_RIGHTX  4 /* Right stick X axis. */
 
/* Button list. */
 
#define XBOX_BTTN_A      0  /* A button. */
#define XBOX_BTTN_B      1  /* B button. */
#define XBOX_BTTN_X      2  /* X button. */
#define XBOX_BTTN_Y      3  /* Y button. */
 
#define XBOX_BTTN_LB     4  /* Left bumper.  */
#define XBOX_BTTN_RB     5  /* Right bumper. */
 
#define XBOX_BTTN_BACK   6  /* Back button.  */
#define XBOX_BTTN_START  7  /* Start button. */
 
#define XBOX_BTTN_LSTICK 8  /* Left stick click.  */
#define XBOX_BTTN_RSTICK 9  /* Right stick click. */
 
#ifdef __cplusplus
}
#endif
 
#endif /* __TGDXBOX360_H__ */

Microsoft Xbox 360 Controller Definitions (GNU/Linux)

Xbox 360 pads do not behave in the same way on UNIX-like systems. The input mappings are different, the central logo button is accessible, and the triggers are separated so that each has an individual axis.

The following information was compiled and tested using Trisquel 4.5 (64-bit):

/*

Copyright (c) 2011 Thomas Dennis.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/

#ifndef __TGDXBOXUNIX_H__
#define __TGDXBOXUNIX_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Driver string - use this to verify that the expected driver is being used. */

#define UXBOX_DRIVER_NAME "Microsoft X-Box 360 pad"

/* Define the number of hats, buttons & axes (Also balls - for completeness). */

#define UXBOX_MAX_AXES 6
#define UXBOX_MAX_BTTN 11
#define UXBOX_MAX_BALL 0
#define UXBOX_MAX_HATS 1

/* Axis list. */

#define UXBOX_AXIS_LEFTX    0 /* Left stick X axis.  */
#define UXBOX_AXIS_LEFTY    1 /* Left stick Y axis.  */
#define UXBOX_AXIS_LTRIGGER 2 /* L Trigger axis.     */

#define UXBOX_AXIS_RIGHTX   3 /* Right stick X axis. */
#define UXBOX_AXIS_RIGHTY   4 /* Right stick Y axis. */
#define UXBOX_AXIS_RTRIGGER 5 /* R Trigger axis.     */

/* Button list. */

#define UXBOX_BTTN_A      0  /* A button. */
#define UXBOX_BTTN_B      1  /* B button. */
#define UXBOX_BTTN_X      2  /* X button. */
#define UXBOX_BTTN_Y      3  /* Y button. */

#define UXBOX_BTTN_LB     4  /* Left bumper.  */
#define UXBOX_BTTN_RB     5  /* Right bumper. */

#define UXBOX_BTTN_START  6  /* Start button.        */
#define UXBOX_BTTN_LOGO   7  /* Central Logo button. */

#define UXBOX_BTTN_LSTICK 8  /* Left stick click.  */
#define UXBOX_BTTN_RSTICK 9  /* Right stick click. */

#define UXBOX_BTTN_BACK   10 /* Back button. */

#ifdef __cplusplus
}
#endif

#endif /* __TGDXBOXUNIX_H__ */

Sony PlayStation 3 SIXAXIS Pad Definitions (GNU/Linux)

Similar to the above, but for PS3 controllers. This was written and tested on Trisquel GNU/Linux and will probably work on most variations of the GNU/Linux family of operating systems.

Unfortunately, it doesn't look like the tilt sensor functionality can be accessed using the current version of the driver. However, all other buttons (including the central "PS Logo" button can be accessed.

Being a former Sony employee, I have attempted to label all buttons using the "official" names that we were instructed to use for internal communications. For example, it's the "Cross" button, not the "Ecks" button. Likewise, the "PS Logo" button is not called the "Menu" button or "XMB" button.

You might have noticed that some of the buttons are also present as axes. This is so that you can either use them as normal on/off buttons or read them as pressure-sensitive buttons instead (I seem to recall that the PlayStation 2 did something similar).

As for what the unused buttons are for: I have no idea.

As a side note: I'm not sure that it's possible to use a SIXAXIS pad wirelessly (by using a bluetooth connection) on a GNU/Linux system, but if you connect it directly to a USB port and the pad does not seem to be responding (despite the red lights flashing on and off), try pressing the PS Logo button to "activate" it and it should work.



/*

Copyright (c) 2009-2010 Thomas Dennis.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/

#ifndef __TGDSIXAXIS_H__
#define __TGDSIXAXIS_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Driver string - use this to verify that the expected driver is being used. */

#define PS3_DRIVER_NAME "Sony PLAYSTATION(R)3 Controller"

/* Define the number of buttons & axes (Also balls & hats, for completeness). */

#define PS3_MAX_AXES 28
#define PS3_MAX_BTTN 19
#define PS3_MAX_BALL 0
#define PS3_MAX_HATS 0

/* Axis list (some buttons are also present as axes for presure sensitivity). */

#define PS3_AXIS_LEFTX    0  /* Left stick X axis.  */
#define PS3_AXIS_LEFTY    1  /* Left stick Y axis.  */
#define PS3_AXIS_RIGHTX   2  /* Right stick X axis. */
#define PS3_AXIS_RIGHTY   3  /* Right stick Y axis. */

#define PS3_AXIS_UNKNOWNA 4  /* Unknown. */
#define PS3_AXIS_UNKNOWNB 5  /* Unknown. */
#define PS3_AXIS_UNKNOWNC 6  /* Unknown. */
#define PS3_AXIS_UNKNOWND 7  /* Unknown. */

#define PS3_AXIS_UP       8  /* Direction button UP.    */
#define PS3_AXIS_RIGHT    9  /* Direction button RIGHT. */
#define PS3_AXIS_DOWN     10 /* Direction button DOWN.  */
#define PS3_AXIS_LEFT     11 /* Direction button LEFT.  */

#define PS3_AXIS_L2       12 /* L2 button. */
#define PS3_AXIS_R2       13 /* R2 button. */
#define PS3_AXIS_L1       14 /* L1 button. */
#define PS3_AXIS_R1       15 /* R1 button. */

#define PS3_AXIS_TRIANGLE 16 /* Triangle button. */
#define PS3_AXIS_CIRCLE   17 /* Circle button.   */
#define PS3_AXIS_CROSS    18 /* Cross button.    */
#define PS3_AXIS_SQUARE   19 /* Square button.   */

#define PS3_AXIS_UNKNOWNE 20 /* Unknown. */
#define PS3_AXIS_UNKNOWNF 21 /* Unknown. */
#define PS3_AXIS_UNKNOWNG 22 /* Unknown. */
#define PS3_AXIS_UNKNOWNH 23 /* Unknown. */

#define PS3_AXIS_UNKNOWNI 24 /* Unknown. */
#define PS3_AXIS_UNKNOWNJ 25 /* Unknown. */
#define PS3_AXIS_UNKNOWNK 26 /* Unknown. */
#define PS3_AXIS_UNKNOWNL 27 /* Unknown. */

/* Button list. */

#define PS3_BTTN_SELECT   0  /* Select button. */
#define PS3_BTTN_L3       1  /* L3 button.     */
#define PS3_BTTN_R3       2  /* R3 button.     */
#define PS3_BTTN_START    3  /* Start button.  */

#define PS3_BTTN_UP       4  /* Direction button UP.    */
#define PS3_BTTN_RIGHT    5  /* Direction button RIGHT. */
#define PS3_BTTN_DOWN     6  /* Direction button DOWN.  */
#define PS3_BTTN_LEFT     7  /* Direction button LEFT.  */

#define PS3_BTTN_L2       8  /* L2 button. */
#define PS3_BTTN_R2       9  /* R2 button. */
#define PS3_BTTN_L1       10 /* L1 button. */
#define PS3_BTTN_R1       11 /* R1 button. */

#define PS3_BTTN_TRIANGLE 12 /* Triangle button. */
#define PS3_BTTN_CIRCLE   13 /* Circle button.   */
#define PS3_BTTN_CROSS    14 /* Cross button.    */
#define PS3_BTTN_SQUARE   15 /* Square button.   */

#define PS3_BTTN_PSLOGO   16 /* PS Logo button.  */

#define PS3_BTTN_UNKNOWNA 17 /* Unknown. */
#define PS3_BTTN_UNKNOWNB 18 /* Unknown. */

#ifdef __cplusplus
}
#endif

#endif /* __TGDSIXAXIS_H__ */

Don't - A program that doesn't do things.

See this quote on bash.org for details:

<@charlie_x> i have a command line program on my pc called dont.com
<@charlie_x> if you type dont format c:\
<@charlie_x> it says
<@charlie_x> "ok i wont"


Here's an implementation of it in C:

#include <stdio.h>

int main (int argc, char *argv[])
{

    if (argc < 2) { printf ("Don't what?\n");    }
    else          { printf ("Okay, I won't.\n"); }

    return 0;

}

...and in Python:

import sys

if len(sys.argv) < 2:
    print "Don't what?"
else:
    print "Okay, I won't."

...and in Java:

public class dont
{

    public static void main (String args[])
    {

        if (args.length < 1) { System.out.println ("Don't what?");    }
        else                 { System.out.println ("Okay, I won't."); }

    }

}

...and, finally, a simple MS Windows .BAT file version:

echo off
cls
echo Okay, I won't.
echo on

SWF Constants

As part of my work on Hitoha, I needed some information on how a "proper" SWF should work, including it's constraints and limitations. What I have learned is provided here for anyone else who might find it useful.

/*

    SWF Constants: Taken from various information resources on the Internet.

Copyright (c) 2010 Thomas Dennis.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/

#ifndef __SWFCONST_H__
#define __SWFCONST_H__

#ifdef __cplusplus
extern "C" {
#endif

/* From http://kb2.adobe.com/cps/144/tn_14437.html */

#define SWF_MAXWIDTH  2880   /* Maximum canvas width.  */
#define SWF_MAXHEIGHT 2880   /* Maximum canvas height. */
#define SWF_MAXFRAMES 16000  /* Maximum movie frames.  */
#define SWF_MAXLAYERS 16000  /* Maximum movie layers.  */
#define SWF_MAXSYMBOL 16000  /* Maximum movie symbols. */
#define SWF_MAXMOVIES 16000  /* Maximum Flash movies.  */

#define SWF_MAXASFILE (32 * 1024)  /* File size (KB) for a single AS script.  */
#define SWF_MAXASVMEM 12           /* Minimum memory (B) used for AS vars.    */
#define SWF_LOOPTIME  15           /* Maximum time (Secs) a loop can run for. */
#define SWF_COMPONENT 125          /* Maximum component guideline (not rule). */

/* From http://www.adobe.com/devnet/freehand/articles/logo_browser09.html */

#define SWF_MINLINE 0.25  /* Minimum line width. */

/* help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001316.html */

#define SWF_MAXLINE 255.0  /* Maximum line width. */

/* From various unofficial sources. */

#define SWF_MAXFPS      120  /* Maximum playback rate.  */
#define SWF_MAXFONTSIZE 127  /* Maximum text font size. */

#ifdef __cplusplus
}
#endif

#endif /* __SWFCONST_H__ */

SelectionFile type iconFile nameDescriptionSizeRevisionTimeUser
ċ

Download
Dont in C.  1k v. 3 4 Dec 2010, 04:59 Go Busto
ċ

Download
Dont in Java.  1k v. 1 4 Dec 2010, 05:00 Go Busto
ċ

Download
Dont in Python.  1k v. 1 4 Dec 2010, 04:59 Go Busto
ċ

Download
SIXAXIS UNIX defines.   4k v. 2 2 Apr 2011, 06:31 Go Busto
ċ

Download
SWF defines.  2k v. 1 4 Dec 2010, 05:28 Go Busto
ċ

Download
  1k v. 1 16 Dec 2011, 14:08 Go Busto
ċ

Download
Xbox 360 Windows defines.   2k v. 2 2 Apr 2011, 06:31 Go Busto
ċ

Download
Xbox 360 UNIX defines.  2k v. 1 2 Apr 2011, 06:30 Go Busto
Comments