diff options
author | Daniel Borca <[email protected]> | 2005-01-14 08:37:20 +0000 |
---|---|---|
committer | Daniel Borca <[email protected]> | 2005-01-14 08:37:20 +0000 |
commit | 7da875d55f6dc9f18e23dd8af9ae7f5782cd3a72 (patch) | |
tree | f14744a4eb497de9045a20726dbf2fdbf474d87b /src/glut/dos/PC_HW | |
parent | f4bff83c3211297159e498c1115b993a5cba9b4b (diff) |
changed coding style
Diffstat (limited to 'src/glut/dos/PC_HW')
-rw-r--r-- | src/glut/dos/PC_HW/pc_hw.c | 178 | ||||
-rw-r--r-- | src/glut/dos/PC_HW/pc_hw.h | 2 | ||||
-rw-r--r-- | src/glut/dos/PC_HW/pc_irq.S | 2 | ||||
-rw-r--r-- | src/glut/dos/PC_HW/pc_keyb.c | 592 | ||||
-rw-r--r-- | src/glut/dos/PC_HW/pc_mouse.c | 269 | ||||
-rw-r--r-- | src/glut/dos/PC_HW/pc_timer.c | 462 |
6 files changed, 768 insertions, 737 deletions
diff --git a/src/glut/dos/PC_HW/pc_hw.c b/src/glut/dos/PC_HW/pc_hw.c index b1fd84bdb67..100b3721651 100644 --- a/src/glut/dos/PC_HW/pc_hw.c +++ b/src/glut/dos/PC_HW/pc_hw.c @@ -1,7 +1,7 @@ /* * PC/HW routine collection v1.3 for DOS/DJGPP * - * Copyright (C) 2002 - Borca Daniel + * Copyright (C) 2002 - Daniel Borca * Email : [email protected] * Web : http://www.geocities.com/dborca */ @@ -16,6 +16,7 @@ #include "pc_hw.h" + /* * atexit */ @@ -24,52 +25,61 @@ static volatile int atexitcnt; static VFUNC atexittbl[MAX_ATEXIT]; -static void __attribute__((destructor)) doexit (void) + +static void __attribute__((destructor)) +doexit (void) { - while (atexitcnt) atexittbl[--atexitcnt](); + while (atexitcnt) atexittbl[--atexitcnt](); } -int pc_clexit (VFUNC f) + +int +pc_clexit (VFUNC f) { - int i; - - for (i=0;i<atexitcnt;i++) { - if (atexittbl[i]==f) { - for (atexitcnt--;i<atexitcnt;i++) atexittbl[i] = atexittbl[i+1]; - atexittbl[i] = 0; - return 0; - } - } - return -1; + int i; + + for (i = 0; i < atexitcnt; i++) { + if (atexittbl[i] == f) { + for (atexitcnt--; i < atexitcnt; i++) atexittbl[i] = atexittbl[i+1]; + atexittbl[i] = 0; + return 0; + } + } + return -1; } -int pc_atexit (VFUNC f) + +int +pc_atexit (VFUNC f) { - pc_clexit(f); - if (atexitcnt<MAX_ATEXIT) { - atexittbl[atexitcnt++] = f; - return 0; - } - return -1; + pc_clexit(f); + if (atexitcnt < MAX_ATEXIT) { + atexittbl[atexitcnt++] = f; + return 0; + } + return -1; } + /* * locked memory allocation */ -void *pc_malloc (size_t size) +void * +pc_malloc (size_t size) { - void *p = malloc(size); + void *p = malloc(size); - if (p) { - if (_go32_dpmi_lock_data(p, size)) { - free(p); - return NULL; - } - } + if (p) { + if (_go32_dpmi_lock_data(p, size)) { + free(p); + return NULL; + } + } - return p; + return p; } + /* * standard redirection */ @@ -78,68 +88,76 @@ static int h_out, h_outbak; static char errname[L_tmpnam]; static int h_err, h_errbak; -int pc_open_stdout (void) + +int +pc_open_stdout (void) { - tmpnam(outname); + tmpnam(outname); - if ((h_out=open(outname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) { - h_outbak = dup(STDOUT_FILENO); - fflush(stdout); - dup2(h_out, STDOUT_FILENO); - } + if ((h_out=open(outname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) { + h_outbak = dup(STDOUT_FILENO); + fflush(stdout); + dup2(h_out, STDOUT_FILENO); + } - return h_out; + return h_out; } -void pc_close_stdout (void) + +void +pc_close_stdout (void) { - FILE *f; - char *line = alloca(512); - - if (h_out > 0) { - dup2(h_outbak, STDOUT_FILENO); - close(h_out); - close(h_outbak); - - f = fopen(outname, "rt"); - while (fgets(line, 512, f)) { - fputs(line, stdout); - } - fclose(f); - - remove(outname); - } + FILE *f; + char *line = alloca(512); + + if (h_out > 0) { + dup2(h_outbak, STDOUT_FILENO); + close(h_out); + close(h_outbak); + + f = fopen(outname, "rt"); + while (fgets(line, 512, f)) { + fputs(line, stdout); + } + fclose(f); + + remove(outname); + } } -int pc_open_stderr (void) + +int +pc_open_stderr (void) { - tmpnam(errname); + tmpnam(errname); - if ((h_err=open(errname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) { - h_errbak = dup(STDERR_FILENO); - fflush(stderr); - dup2(h_err, STDERR_FILENO); - } + if ((h_err=open(errname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) { + h_errbak = dup(STDERR_FILENO); + fflush(stderr); + dup2(h_err, STDERR_FILENO); + } - return h_err; + return h_err; } -void pc_close_stderr (void) + +void +pc_close_stderr (void) { - FILE *f; - char *line = alloca(512); - - if (h_err > 0) { - dup2(h_errbak, STDERR_FILENO); - close(h_err); - close(h_errbak); - - f = fopen(errname, "rt"); - while (fgets(line, 512, f)) { - fputs(line, stderr); - } - fclose(f); - - remove(errname); - } + FILE *f; + char *line = alloca(512); + + if (h_err > 0) { + dup2(h_errbak, STDERR_FILENO); + close(h_err); + close(h_errbak); + + f = fopen(errname, "rt"); + while (fgets(line, 512, f)) { + fputs(line, stderr); + } + fclose(f); + + remove(errname); + } } diff --git a/src/glut/dos/PC_HW/pc_hw.h b/src/glut/dos/PC_HW/pc_hw.h index bd2293eb29d..41948ec991e 100644 --- a/src/glut/dos/PC_HW/pc_hw.h +++ b/src/glut/dos/PC_HW/pc_hw.h @@ -1,7 +1,7 @@ /* * PC/HW routine collection v1.4 for DOS/DJGPP * - * Copyright (C) 2002 - Borca Daniel + * Copyright (C) 2002 - Daniel Borca * Email : [email protected] * Web : http://www.geocities.com/dborca */ diff --git a/src/glut/dos/PC_HW/pc_irq.S b/src/glut/dos/PC_HW/pc_irq.S index 6d99bd805ee..7d62ac74ce0 100644 --- a/src/glut/dos/PC_HW/pc_irq.S +++ b/src/glut/dos/PC_HW/pc_irq.S @@ -1,7 +1,7 @@ /* * PC/HW routine collection v1.3 for DOS/DJGPP * - * Copyright (C) 2002 - Borca Daniel + * Copyright (C) 2002 - Daniel Borca * Email : [email protected] * Web : http://www.geocities.com/dborca */ diff --git a/src/glut/dos/PC_HW/pc_keyb.c b/src/glut/dos/PC_HW/pc_keyb.c index d3c87bc5bde..d7e3257b9d5 100644 --- a/src/glut/dos/PC_HW/pc_keyb.c +++ b/src/glut/dos/PC_HW/pc_keyb.c @@ -1,7 +1,7 @@ /* * PC/HW routine collection v1.3 for DOS/DJGPP * - * Copyright (C) 2002 - Borca Daniel + * Copyright (C) 2002 - Daniel Borca * Email : [email protected] * Web : http://www.geocities.com/dborca */ @@ -14,7 +14,6 @@ #include "pc_hw.h" - #define KEYB_IRQ 1 #define KEY_BUFFER_SIZE 64 @@ -25,8 +24,8 @@ static int keyboard_installed; static volatile struct { - volatile int start, end; - volatile int key[KEY_BUFFER_SIZE]; + volatile int start, end; + volatile int key[KEY_BUFFER_SIZE]; } key_buffer; static volatile int key_enhanced, key_pause_loop, key_shifts; @@ -35,10 +34,8 @@ static int in_a_terrupt = FALSE; static volatile char pc_key[KEY_MAX]; - /* convert Allegro format scancodes into key_shifts flag bits */ -static unsigned short modifier_table[KEY_MAX - KEY_MODIFIERS] = -{ +static unsigned short modifier_table[KEY_MAX - KEY_MODIFIERS] = { KB_SHIFT_FLAG, KB_SHIFT_FLAG, KB_CTRL_FLAG, KB_CTRL_FLAG, KB_ALT_FLAG, KB_ALT_FLAG, KB_LWIN_FLAG, KB_RWIN_FLAG, KB_MENU_FLAG, @@ -46,10 +43,8 @@ static unsigned short modifier_table[KEY_MAX - KEY_MODIFIERS] = }; - /* lookup table for converting hardware scancodes into Allegro format */ -static unsigned char hw_to_mycode[128] = -{ +static unsigned char hw_to_mycode[128] = { /* 0x00 */ 0, KEY_ESC, KEY_1, KEY_2, /* 0x04 */ KEY_3, KEY_4, KEY_5, KEY_6, /* 0x08 */ KEY_7, KEY_8, KEY_9, KEY_0, @@ -85,10 +80,8 @@ static unsigned char hw_to_mycode[128] = }; - /* lookup table for converting extended hardware codes into Allegro format */ -static unsigned char hw_to_mycode_ex[128] = -{ +static unsigned char hw_to_mycode_ex[128] = { /* 0x00 */ 0, KEY_ESC, KEY_1, KEY_2, /* 0x04 */ KEY_3, KEY_4, KEY_5, KEY_6, /* 0x08 */ KEY_7, KEY_8, KEY_9, KEY_0, @@ -124,10 +117,8 @@ static unsigned char hw_to_mycode_ex[128] = }; - /* default mapping table for the US keyboard layout */ -static unsigned short standard_key_ascii_table[KEY_MAX] = -{ +static unsigned short standard_key_ascii_table[KEY_MAX] = { /* start */ 0, /* alphabet */ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', /* numbers */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', @@ -140,10 +131,8 @@ static unsigned short standard_key_ascii_table[KEY_MAX] = }; - /* capslock mapping table for the US keyboard layout */ -static unsigned short standard_key_capslock_table[KEY_MAX] = -{ +static unsigned short standard_key_capslock_table[KEY_MAX] = { /* start */ 0, /* alphabet */ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', /* numbers */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', @@ -156,10 +145,8 @@ static unsigned short standard_key_capslock_table[KEY_MAX] = }; - /* shifted mapping table for the US keyboard layout */ -static unsigned short standard_key_shift_table[KEY_MAX] = -{ +static unsigned short standard_key_shift_table[KEY_MAX] = { /* start */ 0, /* alphabet */ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', /* numbers */ ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', @@ -172,10 +159,8 @@ static unsigned short standard_key_shift_table[KEY_MAX] = }; - /* ctrl+key mapping table for the US keyboard layout */ -static unsigned short standard_key_control_table[KEY_MAX] = -{ +static unsigned short standard_key_control_table[KEY_MAX] = { /* start */ 0, /* alphabet */ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, /* numbers */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -188,215 +173,217 @@ static unsigned short standard_key_control_table[KEY_MAX] = }; - /* convert numeric pad scancodes into arrow codes */ -static unsigned char numlock_table[10] = -{ +static unsigned char numlock_table[10] = { KEY_INSERT, KEY_END, KEY_DOWN, KEY_PGDN, KEY_LEFT, KEY_5_PAD, KEY_RIGHT, KEY_HOME, KEY_UP, KEY_PGUP }; - /* kb_wait_for_write_ready: * Wait for the keyboard controller to set the ready-for-write bit. */ -static __inline int kb_wait_for_write_ready (void) +static __inline int +kb_wait_for_write_ready (void) { - int timeout = 4096; + int timeout = 4096; - while ((timeout>0) && (inportb(0x64)&2)) timeout--; + while ((timeout > 0) && (inportb(0x64) & 2)) timeout--; - return (timeout>0); + return (timeout > 0); } - /* kb_wait_for_read_ready: * Wait for the keyboard controller to set the ready-for-read bit. */ -static __inline int kb_wait_for_read_ready (void) +static __inline int +kb_wait_for_read_ready (void) { - int timeout = 16384; + int timeout = 16384; - while ((timeout>0) && (!(inportb(0x64)&1))) timeout--; + while ((timeout > 0) && (!(inportb(0x64) & 1))) timeout--; - return (timeout>0); + return (timeout > 0); } - /* kb_send_data: * Sends a byte to the keyboard controller. Returns 1 if all OK. */ -static __inline int kb_send_data (unsigned char data) +static __inline int +kb_send_data (unsigned char data) { - int resends = 4; - int timeout, temp; + int resends = 4; + int timeout, temp; - do { - if (!kb_wait_for_write_ready()) - return 0; + do { + if (!kb_wait_for_write_ready()) + return 0; - outportb(0x60, data); - timeout = 4096; + outportb(0x60, data); + timeout = 4096; - while (--timeout>0) { - if (!kb_wait_for_read_ready()) - return 0; + while (--timeout > 0) { + if (!kb_wait_for_read_ready()) + return 0; - temp = inportb(0x60); + temp = inportb(0x60); - if (temp == 0xFA) - return 1; + if (temp == 0xFA) + return 1; - if (temp == 0xFE) - break; - } - } while ((resends-- > 0) && (timeout > 0)); + if (temp == 0xFE) + break; + } + } while ((resends-- > 0) && (timeout > 0)); - return 0; + return 0; } - -static void update_leds (int leds) +static void +update_leds (int leds) { - if (leds_ok) { - if (!in_a_terrupt) - DISABLE(); - - if (!kb_send_data(0xED)) { - kb_send_data(0xF4); - leds_ok = FALSE; - } else if (!kb_send_data((leds>>8)&7)) { - kb_send_data(0xF4); - leds_ok = FALSE; - } - - if (!in_a_terrupt) - ENABLE(); - } + if (leds_ok) { + if (!in_a_terrupt) + DISABLE(); + + if (!kb_send_data(0xED)) { + kb_send_data(0xF4); + leds_ok = FALSE; + } else if (!kb_send_data((leds >> 8) & 7)) { + kb_send_data(0xF4); + leds_ok = FALSE; + } + + if (!in_a_terrupt) + ENABLE(); + } } ENDOFUNC(update_leds) - -static void inject_key (int scancode) +static void +inject_key (int scancode) { - unsigned short *table; - - if ((scancode >= KEY_0_PAD) && (scancode <= KEY_9_PAD)) { - if (((key_shifts & KB_NUMLOCK_FLAG) != 0) == ((key_shifts & KB_SHIFT_FLAG) != 0)) { - scancode = numlock_table[scancode - KEY_0_PAD]; - } - table = standard_key_ascii_table; - } else if (key_shifts & KB_CTRL_FLAG) { - table = standard_key_control_table; - } else if (key_shifts & KB_SHIFT_FLAG) { - if (key_shifts & KB_CAPSLOCK_FLAG) { - if (standard_key_ascii_table[scancode]==standard_key_capslock_table[scancode]) { - table = standard_key_shift_table; - } else { - table = standard_key_ascii_table; - } - } else { - table = standard_key_shift_table; - } - } else if (key_shifts & KB_CAPSLOCK_FLAG) { - table = standard_key_capslock_table; - } else { - table = standard_key_ascii_table; - } - - key_buffer.key[key_buffer.end++] = (scancode<<16)|table[scancode]; - - if (key_buffer.end>=KEY_BUFFER_SIZE) - key_buffer.end = 0; - if (key_buffer.end==key_buffer.start) { - key_buffer.start++; - if (key_buffer.start>=KEY_BUFFER_SIZE) - key_buffer.start = 0; - } + unsigned short *table; + + if ((scancode >= KEY_0_PAD) && (scancode <= KEY_9_PAD)) { + if (((key_shifts & KB_NUMLOCK_FLAG) != 0) == ((key_shifts & KB_SHIFT_FLAG) != 0)) { + scancode = numlock_table[scancode - KEY_0_PAD]; + } + table = standard_key_ascii_table; + } else if (key_shifts & KB_CTRL_FLAG) { + table = standard_key_control_table; + } else if (key_shifts & KB_SHIFT_FLAG) { + if (key_shifts & KB_CAPSLOCK_FLAG) { + if (standard_key_ascii_table[scancode] == standard_key_capslock_table[scancode]) { + table = standard_key_shift_table; + } else { + table = standard_key_ascii_table; + } + } else { + table = standard_key_shift_table; + } + } else if (key_shifts & KB_CAPSLOCK_FLAG) { + table = standard_key_capslock_table; + } else { + table = standard_key_ascii_table; + } + + key_buffer.key[key_buffer.end++] = (scancode << 16) | table[scancode]; + + if (key_buffer.end >= KEY_BUFFER_SIZE) + key_buffer.end = 0; + if (key_buffer.end == key_buffer.start) { + key_buffer.start++; + if (key_buffer.start >= KEY_BUFFER_SIZE) + key_buffer.start = 0; + } } ENDOFUNC(inject_key) -static void handle_code (int scancode, int keycode) + +static void +handle_code (int scancode, int keycode) { - in_a_terrupt++; - - if (keycode==0) { /* pause */ - inject_key(scancode); - pc_key[KEY_PAUSE] ^= TRUE; - } else if (scancode) { - int flag; - - if (scancode>=KEY_MODIFIERS) { - flag = modifier_table[scancode - KEY_MODIFIERS]; - } else { - flag = 0; - } - if ((char)keycode<0) { /* release */ - pc_key[scancode] = FALSE; - if (flag&KB_MODIFIERS) { - key_shifts &= ~flag; - } - } else { /* keypress */ - pc_key[scancode] = TRUE; - if (flag&KB_MODIFIERS) { - key_shifts |= flag; - } - if (flag&KB_LED_FLAGS) { - key_shifts ^= flag; - update_leds(key_shifts); - } - if (scancode<KEY_MODIFIERS) { - inject_key(scancode); - } - } - } - - in_a_terrupt--; + in_a_terrupt++; + + if (keycode == 0) { /* pause */ + inject_key(scancode); + pc_key[KEY_PAUSE] ^= TRUE; + } else if (scancode) { + int flag; + + if (scancode >= KEY_MODIFIERS) { + flag = modifier_table[scancode - KEY_MODIFIERS]; + } else { + flag = 0; + } + if ((char)keycode < 0) { /* release */ + pc_key[scancode] = FALSE; + if (flag & KB_MODIFIERS) { + key_shifts &= ~flag; + } + } else { /* keypress */ + pc_key[scancode] = TRUE; + if (flag & KB_MODIFIERS) { + key_shifts |= flag; + } + if (flag & KB_LED_FLAGS) { + key_shifts ^= flag; + update_leds(key_shifts); + } + if (scancode < KEY_MODIFIERS) { + inject_key(scancode); + } + } + } + + in_a_terrupt--; } ENDOFUNC(handle_code) -static int keyboard () + +static int +keyboard () { - unsigned char temp, scancode; - - temp = inportb(0x60); - - if (temp<=0xe1) { - if (key_pause_loop) { - if (!--key_pause_loop) handle_code(KEY_PAUSE, 0); - } else - switch (temp) { - case 0xe0: - key_enhanced = TRUE; - break; - case 0xe1: - key_pause_loop = 5; - break; - default: - if (key_enhanced) { - key_enhanced = FALSE; - scancode = hw_to_mycode_ex[temp&0x7f]; - } else { - scancode = hw_to_mycode[temp&0x7f]; - } - handle_code(scancode, temp); - } - } - - if (((temp==0x4F)||(temp==0x53))&&(key_shifts&KB_CTRL_FLAG)&&(key_shifts&KB_ALT_FLAG)) { - /* Hack alert: - only SIGINT (but not Ctrl-Break) - calls the destructors and will safely clean up - */ - __asm("\n\ + unsigned char temp, scancode; + + temp = inportb(0x60); + + if (temp <= 0xe1) { + if (key_pause_loop) { + if (!--key_pause_loop) handle_code(KEY_PAUSE, 0); + } else + switch (temp) { + case 0xe0: + key_enhanced = TRUE; + break; + case 0xe1: + key_pause_loop = 5; + break; + default: + if (key_enhanced) { + key_enhanced = FALSE; + scancode = hw_to_mycode_ex[temp & 0x7f]; + } else { + scancode = hw_to_mycode[temp & 0x7f]; + } + handle_code(scancode, temp); + } + } + + if (((temp==0x4F)||(temp==0x53))&&(key_shifts&KB_CTRL_FLAG)&&(key_shifts&KB_ALT_FLAG)) { + /* Hack alert: + * only SIGINT (but not Ctrl-Break) + * calls the destructors and will safely clean up + */ + __asm("\n\ movb $0x79, %%al \n\ call ___djgpp_hw_exception \n\ - ":::"%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); - } + ":::"%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); + } - __asm("\n\ + __asm("\n\ inb $0x61, %%al \n\ movb %%al, %%ah \n\ orb $0x80, %%al \n\ @@ -405,138 +392,149 @@ static int keyboard () outb %%al, $0x61 \n\ movb $0x20, %%al \n\ outb %%al, $0x20 \n\ - ":::"%eax"); - return 0; + ":::"%eax"); + return 0; } ENDOFUNC(keyboard) -int pc_keypressed (void) +int +pc_keypressed (void) { - return (key_buffer.start!=key_buffer.end); + return (key_buffer.start!=key_buffer.end); } -int pc_readkey (void) + +int +pc_readkey (void) { - if (keyboard_installed) { - int key; - - while (key_buffer.start==key_buffer.end) { - __dpmi_yield(); - } - - DISABLE(); - key = key_buffer.key[key_buffer.start++]; - if (key_buffer.start>=KEY_BUFFER_SIZE) - key_buffer.start = 0; - ENABLE(); - - return key; - } else { - return 0; - } + if (keyboard_installed) { + int key; + + while (key_buffer.start == key_buffer.end) { + __dpmi_yield(); + } + + DISABLE(); + key = key_buffer.key[key_buffer.start++]; + if (key_buffer.start >= KEY_BUFFER_SIZE) + key_buffer.start = 0; + ENABLE(); + + return key; + } else { + return 0; + } } -int pc_keydown (int code) + +int +pc_keydown (int code) { - return pc_key[code]; + return pc_key[code]; } -int pc_keyshifts (void) + +int +pc_keyshifts (void) { - return key_shifts; + return key_shifts; } -void pc_remove_keyb (void) + +void +pc_remove_keyb (void) { - if (keyboard_installed) { - int s1, s2, s3; - - keyboard_installed = FALSE; - pc_clexit(pc_remove_keyb); - - DISABLE(); - _farsetsel(__djgpp_dos_sel); - _farnspokew(0x41c, _farnspeekw(0x41a)); - - s1 = _farnspeekb(0x417) & 0x80; - s2 = _farnspeekb(0x418) & 0xFC; - s3 = _farnspeekb(0x496) & 0xF3; - - if (pc_key[KEY_RSHIFT]) { s1 |= 1; } - if (pc_key[KEY_LSHIFT]) { s1 |= 2; } - if (pc_key[KEY_LCONTROL]) { s2 |= 1; s1 |= 4; } - if (pc_key[KEY_ALT]) { s1 |= 8; s2 |= 2; } - if (pc_key[KEY_RCONTROL]) { s1 |= 4; s3 |= 4; } - if (pc_key[KEY_ALTGR]) { s1 |= 8; s3 |= 8; } - - if (key_shifts&KB_SCROLOCK_FLAG) s1 |= 16; - if (key_shifts&KB_NUMLOCK_FLAG) s1 |= 32; - if (key_shifts&KB_CAPSLOCK_FLAG) s1 |= 64; - - _farnspokeb(0x417, s1); - _farnspokeb(0x418, s2); - _farnspokeb(0x496, s3); - update_leds(key_shifts); - - ENABLE(); - pc_remove_irq(KEYB_IRQ); - } + if (keyboard_installed) { + int s1, s2, s3; + + keyboard_installed = FALSE; + pc_clexit(pc_remove_keyb); + + DISABLE(); + _farsetsel(__djgpp_dos_sel); + _farnspokew(0x41c, _farnspeekw(0x41a)); + + s1 = _farnspeekb(0x417) & 0x80; + s2 = _farnspeekb(0x418) & 0xFC; + s3 = _farnspeekb(0x496) & 0xF3; + + if (pc_key[KEY_RSHIFT]) { s1 |= 1; } + if (pc_key[KEY_LSHIFT]) { s1 |= 2; } + if (pc_key[KEY_LCONTROL]) { s2 |= 1; s1 |= 4; } + if (pc_key[KEY_ALT]) { s1 |= 8; s2 |= 2; } + if (pc_key[KEY_RCONTROL]) { s1 |= 4; s3 |= 4; } + if (pc_key[KEY_ALTGR]) { s1 |= 8; s3 |= 8; } + + if (key_shifts & KB_SCROLOCK_FLAG) s1 |= 16; + if (key_shifts & KB_NUMLOCK_FLAG) s1 |= 32; + if (key_shifts & KB_CAPSLOCK_FLAG) s1 |= 64; + + _farnspokeb(0x417, s1); + _farnspokeb(0x418, s2); + _farnspokeb(0x496, s3); + update_leds(key_shifts); + + ENABLE(); + pc_remove_irq(KEYB_IRQ); + } } -int pc_install_keyb (void) + +int +pc_install_keyb (void) { - if (keyboard_installed||pc_install_irq(KEYB_IRQ, keyboard)) { - return -1; - } else { - int s1, s2, s3; - - LOCKDATA(key_buffer); - LOCKDATA(key_enhanced); - LOCKDATA(key_pause_loop); - LOCKDATA(key_shifts); - LOCKDATA(leds_ok); - LOCKDATA(in_a_terrupt); - LOCKDATA(pc_key); - LOCKDATA(modifier_table); - LOCKDATA(hw_to_mycode); - LOCKDATA(hw_to_mycode_ex); - LOCKDATA(standard_key_ascii_table); - LOCKDATA(standard_key_capslock_table); - LOCKDATA(standard_key_shift_table); - LOCKDATA(standard_key_control_table); - LOCKDATA(numlock_table); - LOCKFUNC(update_leds); - LOCKFUNC(inject_key); - LOCKFUNC(handle_code); - LOCKFUNC(keyboard); - - DISABLE(); - _farsetsel(__djgpp_dos_sel); - _farnspokew(0x41c, _farnspeekw(0x41a)); - - key_shifts = 0; - s1 = _farnspeekb(0x417); - s2 = _farnspeekb(0x418); - s3 = _farnspeekb(0x496); - - if (s1&1) { key_shifts |= KB_SHIFT_FLAG; pc_key[KEY_RSHIFT] = TRUE; } - if (s1&2) { key_shifts |= KB_SHIFT_FLAG; pc_key[KEY_LSHIFT] = TRUE; } - if (s2&1) { key_shifts |= KB_CTRL_FLAG; pc_key[KEY_LCONTROL] = TRUE; } - if (s2&2) { key_shifts |= KB_ALT_FLAG; pc_key[KEY_ALT] = TRUE; } - if (s3&4) { key_shifts |= KB_CTRL_FLAG; pc_key[KEY_RCONTROL] = TRUE; } - if (s3&8) { key_shifts |= KB_ALT_FLAG; pc_key[KEY_ALTGR] = TRUE; } - - if (s1&16) key_shifts |= KB_SCROLOCK_FLAG; - if (s1&32) key_shifts |= KB_NUMLOCK_FLAG; - if (s1&64) key_shifts |= KB_CAPSLOCK_FLAG; - update_leds(key_shifts); - - key_enhanced = key_pause_loop = 0; - key_buffer.start = key_buffer.end = 0; - ENABLE(); - - pc_atexit(pc_remove_keyb); - keyboard_installed = TRUE; - return 0; - } + if (keyboard_installed || pc_install_irq(KEYB_IRQ, keyboard)) { + return -1; + } else { + int s1, s2, s3; + + LOCKDATA(key_buffer); + LOCKDATA(key_enhanced); + LOCKDATA(key_pause_loop); + LOCKDATA(key_shifts); + LOCKDATA(leds_ok); + LOCKDATA(in_a_terrupt); + LOCKDATA(pc_key); + LOCKDATA(modifier_table); + LOCKDATA(hw_to_mycode); + LOCKDATA(hw_to_mycode_ex); + LOCKDATA(standard_key_ascii_table); + LOCKDATA(standard_key_capslock_table); + LOCKDATA(standard_key_shift_table); + LOCKDATA(standard_key_control_table); + LOCKDATA(numlock_table); + LOCKFUNC(update_leds); + LOCKFUNC(inject_key); + LOCKFUNC(handle_code); + LOCKFUNC(keyboard); + + DISABLE(); + _farsetsel(__djgpp_dos_sel); + _farnspokew(0x41c, _farnspeekw(0x41a)); + + key_shifts = 0; + s1 = _farnspeekb(0x417); + s2 = _farnspeekb(0x418); + s3 = _farnspeekb(0x496); + + if (s1 & 1) { key_shifts |= KB_SHIFT_FLAG; pc_key[KEY_RSHIFT] = TRUE; } + if (s1 & 2) { key_shifts |= KB_SHIFT_FLAG; pc_key[KEY_LSHIFT] = TRUE; } + if (s2 & 1) { key_shifts |= KB_CTRL_FLAG; pc_key[KEY_LCONTROL] = TRUE; } + if (s2 & 2) { key_shifts |= KB_ALT_FLAG; pc_key[KEY_ALT] = TRUE; } + if (s3 & 4) { key_shifts |= KB_CTRL_FLAG; pc_key[KEY_RCONTROL] = TRUE; } + if (s3 & 8) { key_shifts |= KB_ALT_FLAG; pc_key[KEY_ALTGR] = TRUE; } + + if (s1 & 16) key_shifts |= KB_SCROLOCK_FLAG; + if (s1 & 32) key_shifts |= KB_NUMLOCK_FLAG; + if (s1 & 64) key_shifts |= KB_CAPSLOCK_FLAG; + update_leds(key_shifts); + + key_enhanced = key_pause_loop = 0; + key_buffer.start = key_buffer.end = 0; + ENABLE(); + + pc_atexit(pc_remove_keyb); + keyboard_installed = TRUE; + return 0; + } } diff --git a/src/glut/dos/PC_HW/pc_mouse.c b/src/glut/dos/PC_HW/pc_mouse.c index fc0902ce81b..5bf99d367fd 100644 --- a/src/glut/dos/PC_HW/pc_mouse.c +++ b/src/glut/dos/PC_HW/pc_mouse.c @@ -1,7 +1,7 @@ /* * PC/HW routine collection v1.3 for DOS/DJGPP * - * Copyright (C) 2002 - Borca Daniel + * Copyright (C) 2002 - Daniel Borca * Email : [email protected] * Web : http://www.geocities.com/dborca */ @@ -14,16 +14,15 @@ #include "pc_hw.h" - #define PC_CUTE_WHEEL 1 /* CuteMouse WheelAPI */ #define MOUSE_STACK_SIZE 16384 #define CLEAR_MICKEYS() \ - do { \ - __asm __volatile ("movw $0xb, %%ax; int $0x33":::"%eax", "%ecx", "%edx"); \ - ox = oy = 0; \ - } while (0) + do { \ + __asm __volatile ("movw $0xb, %%ax; int $0x33":::"%eax", "%ecx", "%edx"); \ + ox = oy = 0; \ + } while (0) extern void mouse_wrap (void); extern int mouse_wrap_end[]; @@ -33,7 +32,7 @@ static long mouse_callback; static __dpmi_regs mouse_regs; static volatile struct { - volatile int x, y, z, b; + volatile int x, y, z, b; } pc_mouse; static int minx = 0; @@ -51,41 +50,44 @@ static int emulat3 = FALSE; static int ox, oy; -static void mouse (__dpmi_regs *r) +static void +mouse (__dpmi_regs *r) { - int nx = (signed short)r->x.si / sx; - int ny = (signed short)r->x.di / sy; - int dx = nx - ox; - int dy = ny - oy; + int nx = (signed short)r->x.si / sx; + int ny = (signed short)r->x.di / sy; + int dx = nx - ox; + int dy = ny - oy; #if PC_CUTE_WHEEL - int dz = (signed char)r->h.bh; + int dz = (signed char)r->h.bh; #endif - ox = nx; - oy = ny; + ox = nx; + oy = ny; - pc_mouse.b = r->h.bl; - pc_mouse.x = MID(minx, pc_mouse.x + dx, maxx); - pc_mouse.y = MID(miny, pc_mouse.y + dy, maxy); + pc_mouse.b = r->h.bl; + pc_mouse.x = MID(minx, pc_mouse.x + dx, maxx); + pc_mouse.y = MID(miny, pc_mouse.y + dy, maxy); #if PC_CUTE_WHEEL - pc_mouse.z = MID(minz, pc_mouse.z + dz, maxz); + pc_mouse.z = MID(minz, pc_mouse.z + dz, maxz); #endif - if (emulat3) { - if ((pc_mouse.b&3)==3) { - pc_mouse.b = 4; - } - } + if (emulat3) { + if ((pc_mouse.b & 3) == 3) { + pc_mouse.b = 4; + } + } - if (mouse_func) { - mouse_func(pc_mouse.x, pc_mouse.y, pc_mouse.z, pc_mouse.b); - } + if (mouse_func) { + mouse_func(pc_mouse.x, pc_mouse.y, pc_mouse.z, pc_mouse.b); + } } ENDOFUNC(mouse) -void pc_remove_mouse (void) + +void +pc_remove_mouse (void) { - if (mouse_callback) { - pc_clexit(pc_remove_mouse); - __asm("\n\ + if (mouse_callback) { + pc_clexit(pc_remove_mouse); + __asm("\n\ movl %%edx, %%ecx \n\ shrl $16, %%ecx \n\ movw $0x0304, %%ax \n\ @@ -93,61 +95,63 @@ void pc_remove_mouse (void) movw $0x000c, %%ax \n\ xorl %%ecx, %%ecx \n\ int $0x33 \n\ - "::"d"(mouse_callback):"%eax", "%ecx"); + "::"d"(mouse_callback):"%eax", "%ecx"); - mouse_callback = 0; + mouse_callback = 0; - free((void *)(mouse_wrap_end[0] - MOUSE_STACK_SIZE)); - } + free((void *)(mouse_wrap_end[0] - MOUSE_STACK_SIZE)); + } } -int pc_install_mouse (void) + +int +pc_install_mouse (void) { - int buttons; + int buttons; - /* fail if already call-backed */ - if (mouse_callback) { - return 0; - } + /* fail if already call-backed */ + if (mouse_callback) { + return 0; + } - /* reset mouse and get status */ - __asm("\n\ + /* reset mouse and get status */ + __asm("\n\ xorl %%eax, %%eax \n\ int $0x33 \n\ andl %%ebx, %%eax \n\ movl %%eax, %0 \n\ - ":"=g" (buttons)::"%eax", "%ebx"); - if (!buttons) { - return 0; - } - - /* lock wrapper */ - LOCKDATA(mouse_func); - LOCKDATA(mouse_callback); - LOCKDATA(mouse_regs); - LOCKDATA(pc_mouse); - LOCKDATA(minx); - LOCKDATA(maxx); - LOCKDATA(miny); - LOCKDATA(maxy); - LOCKDATA(minz); - LOCKDATA(maxz); - LOCKDATA(sx); - LOCKDATA(sy); - LOCKDATA(emulat3); - LOCKDATA(ox); - LOCKDATA(oy); - LOCKFUNC(mouse); - LOCKFUNC(mouse_wrap); - - mouse_wrap_end[1] = __djgpp_ds_alias; - /* grab a locked stack */ - if ((mouse_wrap_end[0] = (int)pc_malloc(MOUSE_STACK_SIZE)) == NULL) { - return 0; - } - - /* try to hook a call-back */ - __asm("\n\ + ":"=g" (buttons)::"%eax", "%ebx"); + if (!buttons) { + return 0; + } + + /* lock wrapper */ + LOCKDATA(mouse_func); + LOCKDATA(mouse_callback); + LOCKDATA(mouse_regs); + LOCKDATA(pc_mouse); + LOCKDATA(minx); + LOCKDATA(maxx); + LOCKDATA(miny); + LOCKDATA(maxy); + LOCKDATA(minz); + LOCKDATA(maxz); + LOCKDATA(sx); + LOCKDATA(sy); + LOCKDATA(emulat3); + LOCKDATA(ox); + LOCKDATA(oy); + LOCKFUNC(mouse); + LOCKFUNC(mouse_wrap); + + mouse_wrap_end[1] = __djgpp_ds_alias; + /* grab a locked stack */ + if ((mouse_wrap_end[0] = (int)pc_malloc(MOUSE_STACK_SIZE)) == NULL) { + return 0; + } + + /* try to hook a call-back */ + __asm("\n\ pushl %%ds \n\ pushl %%es \n\ movw $0x0303, %%ax \n\ @@ -163,86 +167,97 @@ int pc_install_mouse (void) movw %%dx, %%cx \n\ movl %%ecx, %0 \n\ 0: \n\ - ":"=g"(mouse_callback) - :"S" (mouse_wrap), "D"(&mouse_regs) - :"%eax", "%ecx", "%edx"); - if (!mouse_callback) { - free((void *)mouse_wrap_end[0]); - return 0; - } - - /* adjust stack */ - mouse_wrap_end[0] += MOUSE_STACK_SIZE; - - /* install the handler */ - mouse_regs.x.ax = 0x000c; + ":"=g"(mouse_callback) + :"S" (mouse_wrap), "D"(&mouse_regs) + :"%eax", "%ecx", "%edx"); + if (!mouse_callback) { + free((void *)mouse_wrap_end[0]); + return 0; + } + + /* adjust stack */ + mouse_wrap_end[0] += MOUSE_STACK_SIZE; + + /* install the handler */ + mouse_regs.x.ax = 0x000c; #if PC_CUTE_WHEEL - mouse_regs.x.cx = 0x7f | 0x80; + mouse_regs.x.cx = 0x7f | 0x80; #else - mouse_regs.x.cx = 0x7f; + mouse_regs.x.cx = 0x7f; #endif - mouse_regs.x.dx = mouse_callback&0xffff; - mouse_regs.x.es = mouse_callback>>16; - __dpmi_int(0x33, &mouse_regs); + mouse_regs.x.dx = mouse_callback & 0xffff; + mouse_regs.x.es = mouse_callback >> 16; + __dpmi_int(0x33, &mouse_regs); - CLEAR_MICKEYS(); + CLEAR_MICKEYS(); - emulat3 = buttons<3; - pc_atexit(pc_remove_mouse); - return buttons; + emulat3 = (buttons < 3); + pc_atexit(pc_remove_mouse); + return buttons; } -MFUNC pc_install_mouse_handler (MFUNC handler) + +MFUNC +pc_install_mouse_handler (MFUNC handler) { - MFUNC old; + MFUNC old; - if (!mouse_callback && !pc_install_mouse()) { - return NULL; - } + if (!mouse_callback && !pc_install_mouse()) { + return NULL; + } - old = mouse_func; - mouse_func = handler; - return old; + old = mouse_func; + mouse_func = handler; + return old; } -void pc_mouse_area (int x1, int y1, int x2, int y2) + +void +pc_mouse_area (int x1, int y1, int x2, int y2) { - minx = x1; - maxx = x2; - miny = y1; - maxy = y2; + minx = x1; + maxx = x2; + miny = y1; + maxy = y2; } -void pc_mouse_speed (int xspeed, int yspeed) + +void +pc_mouse_speed (int xspeed, int yspeed) { - DISABLE(); + DISABLE(); - sx = MAX(1, xspeed); - sy = MAX(1, yspeed); + sx = MAX(1, xspeed); + sy = MAX(1, yspeed); - ENABLE(); + ENABLE(); } -int pc_query_mouse (int *x, int *y, int *z) + +int +pc_query_mouse (int *x, int *y, int *z) { - *x = pc_mouse.x; - *y = pc_mouse.y; - *z = pc_mouse.z; - return pc_mouse.b; + *x = pc_mouse.x; + *y = pc_mouse.y; + *z = pc_mouse.z; + return pc_mouse.b; } -void pc_warp_mouse (int x, int y) + +void +pc_warp_mouse (int x, int y) { - CLEAR_MICKEYS(); + CLEAR_MICKEYS(); - pc_mouse.x = MID(minx, x, maxx); - pc_mouse.y = MID(miny, y, maxy); + pc_mouse.x = MID(minx, x, maxx); + pc_mouse.y = MID(miny, y, maxy); - if (mouse_func) { - mouse_func(pc_mouse.x, pc_mouse.y, pc_mouse.z, pc_mouse.b); - } + if (mouse_func) { + mouse_func(pc_mouse.x, pc_mouse.y, pc_mouse.z, pc_mouse.b); + } } + /* Hack alert: * `mouse_wrap_end' actually holds the * address of stack in a safe data selector. diff --git a/src/glut/dos/PC_HW/pc_timer.c b/src/glut/dos/PC_HW/pc_timer.c index 530ed72a493..e7cbe70a1f2 100644 --- a/src/glut/dos/PC_HW/pc_timer.c +++ b/src/glut/dos/PC_HW/pc_timer.c @@ -1,7 +1,7 @@ /* * PC/HW routine collection v1.5 for DOS/DJGPP * - * Copyright (C) 2002 - Borca Daniel + * Copyright (C) 2002 - Daniel Borca * Email : [email protected] * Web : http://www.geocities.com/dborca */ @@ -25,15 +25,14 @@ static int timer_installed; typedef struct { - volatile unsigned int counter, clock_ticks, freq; - volatile PFUNC func; - volatile void *parm; + volatile unsigned int counter, clock_ticks, freq; + volatile PFUNC func; + volatile void *parm; } TIMER; static TIMER timer_main, timer_func[MAX_TIMERS]; - /* Desc: main timer callback * * In : - @@ -41,33 +40,33 @@ static TIMER timer_main, timer_func[MAX_TIMERS]; * * Note: - */ -static int timer () +static int +timer () { - int i; - - for (i = 0; i < MAX_TIMERS; i++) { - TIMER *t = &timer_func[i]; - if (t->func) { - t->clock_ticks += t->counter; - if (t->clock_ticks >= timer_main.counter) { - t->clock_ticks -= timer_main.counter; - t->func(unvolatile(t->parm, void *)); - } - } - } - - timer_main.clock_ticks += timer_main.counter; - if (timer_main.clock_ticks >= 0x10000) { - timer_main.clock_ticks -= 0x10000; - return 1; - } else { - outportb(0x20, 0x20); - return 0; - } + int i; + + for (i = 0; i < MAX_TIMERS; i++) { + TIMER *t = &timer_func[i]; + if (t->func) { + t->clock_ticks += t->counter; + if (t->clock_ticks >= timer_main.counter) { + t->clock_ticks -= timer_main.counter; + t->func(unvolatile(t->parm, void *)); + } + } + } + + timer_main.clock_ticks += timer_main.counter; + if (timer_main.clock_ticks >= 0x10000) { + timer_main.clock_ticks -= 0x10000; + return 1; + } else { + outportb(0x20, 0x20); + return 0; + } } ENDOFUNC(timer) - /* Desc: uninstall timer engine * * In : - @@ -75,24 +74,24 @@ static int timer () * * Note: - */ -void pc_remove_timer (void) +void +pc_remove_timer (void) { - if (timer_installed) { - timer_installed = FALSE; - pc_clexit(pc_remove_timer); - - DISABLE(); - outportb(0x43, 0x34); - outportb(0x40, 0); - outportb(0x40, 0); - ENABLE(); - - pc_remove_irq(TIMER_IRQ); - } + if (timer_installed) { + timer_installed = FALSE; + pc_clexit(pc_remove_timer); + + DISABLE(); + outportb(0x43, 0x34); + outportb(0x40, 0); + outportb(0x40, 0); + ENABLE(); + + pc_remove_irq(TIMER_IRQ); + } } - /* Desc: remove timerfunc * * In : timerfunc id @@ -100,65 +99,65 @@ void pc_remove_timer (void) * * Note: tries to relax the main timer whenever possible */ -int pc_remove_int (int fid) +int +pc_remove_int (int fid) { - int i; - unsigned int freq = 0; - - /* are we installed? */ - if (!timer_installed) { - return -1; - } - - /* sanity check */ - if ((fid < 0) || (fid >= MAX_TIMERS) || (timer_func[fid].func == NULL)) { - return -1; - } - timer_func[fid].func = NULL; - - /* scan for maximum frequency */ - for (i = 0; i < MAX_TIMERS; i++) { - TIMER *t = &timer_func[i]; - if (t->func) { - if (freq < t->freq) { - freq = t->freq; - } - } - } - - /* if there are no callbacks left, cleanup */ - if (!freq) { - pc_remove_timer(); - return 0; - } - - /* if we just lowered the maximum frequency, try to relax the timer engine */ - if (freq < timer_main.freq) { - unsigned int new_counter = PIT_FREQ / freq; - - DISABLE(); - - for (i = 0; i < MAX_TIMERS; i++) { - if (timer_func[i].func) { - ADJUST(timer_func[i], freq); - } - } - - outportb(0x43, 0x34); - outportb(0x40, (unsigned char)new_counter); - outportb(0x40, (unsigned char)(new_counter>>8)); - timer_main.clock_ticks = 0; - timer_main.counter = new_counter; - timer_main.freq = freq; - - ENABLE(); - } + int i; + unsigned int freq = 0; + + /* are we installed? */ + if (!timer_installed) { + return -1; + } + + /* sanity check */ + if ((fid < 0) || (fid >= MAX_TIMERS) || (timer_func[fid].func == NULL)) { + return -1; + } + timer_func[fid].func = NULL; + + /* scan for maximum frequency */ + for (i = 0; i < MAX_TIMERS; i++) { + TIMER *t = &timer_func[i]; + if (t->func) { + if (freq < t->freq) { + freq = t->freq; + } + } + } + + /* if there are no callbacks left, cleanup */ + if (!freq) { + pc_remove_timer(); + return 0; + } + + /* if we just lowered the maximum frequency, try to relax the timer engine */ + if (freq < timer_main.freq) { + unsigned int new_counter = PIT_FREQ / freq; + + DISABLE(); + + for (i = 0; i < MAX_TIMERS; i++) { + if (timer_func[i].func) { + ADJUST(timer_func[i], freq); + } + } + + outportb(0x43, 0x34); + outportb(0x40, (unsigned char)new_counter); + outportb(0x40, (unsigned char)(new_counter>>8)); + timer_main.clock_ticks = 0; + timer_main.counter = new_counter; + timer_main.freq = freq; + + ENABLE(); + } - return 0; + return 0; } ENDOFUNC(pc_remove_int) - /* Desc: adjust timerfunc * * In : timerfunc id, new frequency (Hz) @@ -166,64 +165,64 @@ int pc_remove_int (int fid) * * Note: might change the main timer frequency */ -int pc_adjust_int (int fid, unsigned int freq) +int +pc_adjust_int (int fid, unsigned int freq) { - int i; - - /* are we installed? */ - if (!timer_installed) { - return -1; - } - - /* sanity check */ - if ((fid < 0) || (fid >= MAX_TIMERS) || (timer_func[fid].func == NULL)) { - return -1; - } - timer_func[fid].freq = freq; - - /* scan for maximum frequency */ - freq = 0; - for (i = 0; i < MAX_TIMERS; i++) { - TIMER *t = &timer_func[i]; - if (t->func) { - if (freq < t->freq) { - freq = t->freq; - } - } - } - - /* update main timer / sons to match highest frequency */ - DISABLE(); - - /* using '>' is correct still (and avoids updating - * the HW timer too often), but doesn't relax the timer! - */ - if (freq != timer_main.freq) { - unsigned int new_counter = PIT_FREQ / freq; - - for (i = 0; i < MAX_TIMERS; i++) { - if (timer_func[i].func) { - ADJUST(timer_func[i], freq); - } - } - - outportb(0x43, 0x34); - outportb(0x40, (unsigned char)new_counter); - outportb(0x40, (unsigned char)(new_counter>>8)); - timer_main.clock_ticks = 0; - timer_main.counter = new_counter; - timer_main.freq = freq; - } else { - ADJUST(timer_func[fid], timer_main.freq); - } - - ENABLE(); - - return 0; + int i; + + /* are we installed? */ + if (!timer_installed) { + return -1; + } + + /* sanity check */ + if ((fid < 0) || (fid >= MAX_TIMERS) || (timer_func[fid].func == NULL)) { + return -1; + } + timer_func[fid].freq = freq; + + /* scan for maximum frequency */ + freq = 0; + for (i = 0; i < MAX_TIMERS; i++) { + TIMER *t = &timer_func[i]; + if (t->func) { + if (freq < t->freq) { + freq = t->freq; + } + } + } + + /* update main timer / sons to match highest frequency */ + DISABLE(); + + /* using '>' is correct still (and avoids updating + * the HW timer too often), but doesn't relax the timer! + */ + if (freq != timer_main.freq) { + unsigned int new_counter = PIT_FREQ / freq; + + for (i = 0; i < MAX_TIMERS; i++) { + if (timer_func[i].func) { + ADJUST(timer_func[i], freq); + } + } + + outportb(0x43, 0x34); + outportb(0x40, (unsigned char)new_counter); + outportb(0x40, (unsigned char)(new_counter>>8)); + timer_main.clock_ticks = 0; + timer_main.counter = new_counter; + timer_main.freq = freq; + } else { + ADJUST(timer_func[fid], timer_main.freq); + } + + ENABLE(); + + return 0; } ENDOFUNC(pc_adjust_int) - /* Desc: install timer engine * * In : - @@ -231,36 +230,36 @@ int pc_adjust_int (int fid, unsigned int freq) * * Note: initial frequency is 18.2 Hz */ -static int install_timer (void) +static int +install_timer (void) { - if (timer_installed || pc_install_irq(TIMER_IRQ, timer)) { - return -1; - } else { - memset(timer_func, 0, sizeof(timer_func)); - - LOCKDATA(timer_func); - LOCKDATA(timer_main); - LOCKFUNC(timer); - LOCKFUNC(pc_adjust_int); - LOCKFUNC(pc_remove_int); - - timer_main.counter = 0x10000; - - DISABLE(); - outportb(0x43, 0x34); - outportb(0x40, 0); - outportb(0x40, 0); - timer_main.clock_ticks = 0; - ENABLE(); - - pc_atexit(pc_remove_timer); - timer_installed = TRUE; - return 0; - } + if (timer_installed || pc_install_irq(TIMER_IRQ, timer)) { + return -1; + } else { + memset(timer_func, 0, sizeof(timer_func)); + + LOCKDATA(timer_func); + LOCKDATA(timer_main); + LOCKFUNC(timer); + LOCKFUNC(pc_adjust_int); + LOCKFUNC(pc_remove_int); + + timer_main.counter = 0x10000; + + DISABLE(); + outportb(0x43, 0x34); + outportb(0x40, 0); + outportb(0x40, 0); + timer_main.clock_ticks = 0; + ENABLE(); + + pc_atexit(pc_remove_timer); + timer_installed = TRUE; + return 0; + } } - /* Desc: install timerfunc * * In : callback function, opaque pointer to be passed to callee, freq (Hz) @@ -268,60 +267,61 @@ static int install_timer (void) * * Note: returns -1 if error */ -int pc_install_int (PFUNC func, void *parm, unsigned int freq) +int +pc_install_int (PFUNC func, void *parm, unsigned int freq) { - int i; - TIMER *t = NULL; - - /* ensure the timer engine is set up */ - if (!timer_installed) { - if (install_timer()) { - return -1; - } - } - - /* find an empty slot */ - for (i = 0; i < MAX_TIMERS; i++) { - if (!timer_func[i].func) { - t = &timer_func[i]; - break; - } - } - if (t == NULL) { - return -1; - } - - DISABLE(); - - t->func = func; - t->parm = parm; - t->freq = freq; - t->clock_ticks = 0; - - /* update main timer / sons to match highest frequency */ - if (freq > timer_main.freq) { - unsigned int new_counter = PIT_FREQ / freq; - - for (i = 0; i < MAX_TIMERS; i++) { - if (timer_func[i].func) { - ADJUST(timer_func[i], freq); - } - } - - outportb(0x43, 0x34); - outportb(0x40, (unsigned char)new_counter); - outportb(0x40, (unsigned char)(new_counter>>8)); - timer_main.clock_ticks = 0; - timer_main.counter = new_counter; - timer_main.freq = freq; - } else { - /* t == &timer_func[i] */ - ADJUST(timer_func[i], timer_main.freq); - } - - i = t - timer_func; - - ENABLE(); - - return i; + int i; + TIMER *t = NULL; + + /* ensure the timer engine is set up */ + if (!timer_installed) { + if (install_timer()) { + return -1; + } + } + + /* find an empty slot */ + for (i = 0; i < MAX_TIMERS; i++) { + if (!timer_func[i].func) { + t = &timer_func[i]; + break; + } + } + if (t == NULL) { + return -1; + } + + DISABLE(); + + t->func = func; + t->parm = parm; + t->freq = freq; + t->clock_ticks = 0; + + /* update main timer / sons to match highest frequency */ + if (freq > timer_main.freq) { + unsigned int new_counter = PIT_FREQ / freq; + + for (i = 0; i < MAX_TIMERS; i++) { + if (timer_func[i].func) { + ADJUST(timer_func[i], freq); + } + } + + outportb(0x43, 0x34); + outportb(0x40, (unsigned char)new_counter); + outportb(0x40, (unsigned char)(new_counter>>8)); + timer_main.clock_ticks = 0; + timer_main.counter = new_counter; + timer_main.freq = freq; + } else { + /* t == &timer_func[i] */ + ADJUST(timer_func[i], timer_main.freq); + } + + i = t - timer_func; + + ENABLE(); + + return i; } |