From 53a47eaf2c394aec9c9549bf1a603fe7dfde17ac Mon Sep 17 00:00:00 2001 From: Edu Garcia <28616+Arcnor@users.noreply.github.com> Date: Mon, 6 May 2024 15:58:46 +0100 Subject: [PATCH 1/2] remove Rock'n Ride code --- Descent3/CMakeLists.txt | 2 - Descent3/Controls.cpp | 5 - Descent3/D3ForceFeedback.cpp | 21 ---- Descent3/Player.cpp | 6 - Descent3/gamesequence.cpp | 2 - Descent3/init.cpp | 11 -- Descent3/lnxmain.cpp | 1 - Descent3/rocknride.cpp | 229 ----------------------------------- Descent3/rocknride.h | 92 -------------- 9 files changed, 369 deletions(-) delete mode 100644 Descent3/rocknride.cpp delete mode 100644 Descent3/rocknride.h diff --git a/Descent3/CMakeLists.txt b/Descent3/CMakeLists.txt index b553836d8..a0bd3baa4 100644 --- a/Descent3/CMakeLists.txt +++ b/Descent3/CMakeLists.txt @@ -110,7 +110,6 @@ set(HEADERS robotfire.h robotfirestruct.h robotfirestruct_external.h - rocknride.h room.h room_external.h scorch.h @@ -244,7 +243,6 @@ set(CPPS render.cpp renderobject.cpp robotfire.cpp - rocknride.cpp room.cpp scorch.cpp screens.cpp diff --git a/Descent3/Controls.cpp b/Descent3/Controls.cpp index b5349581c..362ad8717 100644 --- a/Descent3/Controls.cpp +++ b/Descent3/Controls.cpp @@ -430,8 +430,6 @@ #include "sounds.h" #include "soundload.h" -#include "rocknride.h" - #include #include @@ -740,9 +738,6 @@ void ReadPlayerControls(game_controls *controls) { DoMisc(controls); DoMovement(controls); // controls for moving the object - // Do any rock'n'ride updates - RNR_UpdateControllerInfo(controls); - // only read at the specified rate to keep things consistant. manager control system timer // Control_current_time += Frametime; // Control_frametime = Control_current_time - Control_interval_time; diff --git a/Descent3/D3ForceFeedback.cpp b/Descent3/D3ForceFeedback.cpp index ee7858543..4f55dc5b0 100644 --- a/Descent3/D3ForceFeedback.cpp +++ b/Descent3/D3ForceFeedback.cpp @@ -97,7 +97,6 @@ #include "weapon.h" #include "ddio.h" #include "psrand.h" -#include "rocknride.h" extern float Gametime; @@ -360,10 +359,6 @@ void ForceEffectsClose(void) { ddio_ffb_DestroyAll(); } // Plays an effect // ----------------------------------------------------------------- void ForceEffectsPlay(int id, float *scale, int *direction) { - if (RocknRide_enabled && direction) { - RNR_UpdateForceFeedbackInfo((scale) ? *scale : 1.0f, direction); - } - if (!D3Force_init || !D3Use_force_feedback) return; @@ -389,10 +384,6 @@ void ForceEffectsPlay(int id, float *scale, int *direction) { ddio_ffb_effectPlay(low_id); } void ForceEffectsPlay(int id, float *scale, vector *direction) { - if (RocknRide_enabled && direction) { - RNR_UpdateForceFeedbackInfo((scale) ? *scale : 1.0f, direction); - } - if (!D3Force_init || !D3Use_force_feedback) return; @@ -437,9 +428,6 @@ void ForceEffectsPlay(int id, float *scale, vector *direction) { } void DoForceForWeapon(object *me_obj, object *it_obj, vector *force_vec) { - if (!RocknRide_enabled && (!D3Force_init || !D3Use_force_feedback)) - return; - if (it_obj->id < 0 || it_obj->id >= MAX_WEAPONS) return; @@ -462,9 +450,6 @@ void DoForceForWeapon(object *me_obj, object *it_obj, vector *force_vec) { } void DoForceForWall(object *playerobj, float hitspeed, int hitseg, int hitwall, vector *wall_normal) { - if (!RocknRide_enabled && (!D3Force_init || !D3Use_force_feedback)) - return; - vector local_norm; float scale = 1.00f; @@ -484,9 +469,6 @@ void DoForceForWall(object *playerobj, float hitspeed, int hitseg, int hitwall, } void DoForceForRecoil(object *playerobj, object *weap) { - if (!RocknRide_enabled && (!D3Force_init || !D3Use_force_feedback)) - return; - weapon *w_ptr = &Weapons[weap->id]; vector local_norm; @@ -513,9 +495,6 @@ void DoForceForRecoil(object *playerobj, object *weap) { float Force_time_since_last_shake; #define SHAKE_TIME 0.2f void DoForceForShake(float magnitude) { - if (!RocknRide_enabled && (!D3Force_init || !D3Use_force_feedback)) - return; - if (Force_time_since_last_shake + SHAKE_TIME > Gametime) { if (Force_time_since_last_shake < Gametime) { return; diff --git a/Descent3/Player.cpp b/Descent3/Player.cpp index 72ca19403..8676927e8 100644 --- a/Descent3/Player.cpp +++ b/Descent3/Player.cpp @@ -1130,7 +1130,6 @@ #include "config.h" #include "osiris_dll.h" #include "gamesequence.h" -#include "rocknride.h" #include "vibeinterface.h" @@ -2078,11 +2077,6 @@ void InitiatePlayerDeath(object *playerobj, bool melee, int fate) { // Start the death sequence StartPlayerDeath(playerobj->id, -playerobj->shields, melee, fate); - - // Send the Game status to the rock'n'ride chair - if (playerobj->id == Player_num) { - RNR_UpdateGameStatus(RNRGSC_PLAYERDIES); - } } } diff --git a/Descent3/gamesequence.cpp b/Descent3/gamesequence.cpp index 3bfd52f3f..44c587b93 100644 --- a/Descent3/gamesequence.cpp +++ b/Descent3/gamesequence.cpp @@ -1085,7 +1085,6 @@ #include "debuggraph.h" #include "multi_dll_mgr.h" #include "multi_ui.h" -#include "rocknride.h" #include "gamepath.h" #include "vclip.h" #include "bsp.h" @@ -2047,7 +2046,6 @@ void RunGameMenu() { NewUIWindow_alpha = 226; SetUICallback(GameFrameUI); SuspendControls(); - RNR_UpdateGameStatus(RNRGSC_INMENU); // reset bail flag. Multi_bail_ui_menu = false; diff --git a/Descent3/init.cpp b/Descent3/init.cpp index e9c45f759..cbd1968b2 100644 --- a/Descent3/init.cpp +++ b/Descent3/init.cpp @@ -985,7 +985,6 @@ #include "marker.h" #include "gamecinematics.h" #include "debuggraph.h" -#include "rocknride.h" #include "vibeinterface.h" // Uncomment this for all non-US versions!! @@ -1597,16 +1596,6 @@ void InitIOSystems(bool editor) { Error("I/O initialization failed."); } - int rocknride_arg = FindArg("-rocknride"); - if (rocknride_arg) { - int comm_port = atoi(GameArgs[rocknride_arg + 1]); - if (!RNR_Initialize(comm_port)) { - mprintf((0, "Rock'n'Ride Init failed!\n")); - } else { - mprintf((0, "Rock'n'Ride Init success!\n")); - } - } - rtp_Init(); RTP_ENABLEFLAGS(RTI_FRAMETIME | RTI_RENDERFRAMETIME | RTI_MULTIFRAMETIME | RTI_MUSICFRAMETIME | RTI_AMBSOUNDFRAMETIME); diff --git a/Descent3/lnxmain.cpp b/Descent3/lnxmain.cpp index bcc868234..293fccfd6 100644 --- a/Descent3/lnxmain.cpp +++ b/Descent3/lnxmain.cpp @@ -92,7 +92,6 @@ static cmdLineArg d3ArgTable[] = { {"glidelibrary", 'l', "Select Glide rendering library."}, {"gllibrary", 'g', "Select OpenGL rendering library."}, - {"rocknride", 'r', "Enable Rock'n'Ride (http://www.rocknride.com)."}, {"cobra", 'R', "Enable Cobra chair support."}, #if (!defined(DEMO)) diff --git a/Descent3/rocknride.cpp b/Descent3/rocknride.cpp deleted file mode 100644 index 24faa5e1a..000000000 --- a/Descent3/rocknride.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/* -* Descent 3 -* Copyright (C) 2024 Parallax Software -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*/ - -/* - * $Logfile: /DescentIII/main/rocknride.cpp $ - * $Revision: 5 $ - * $Date: 9/23/99 12:02p $ - * $Author: Jeff $ - * - * Rock 'n' Ride - * - * $Log: /DescentIII/main/rocknride.cpp $ - * - * 5 9/23/99 12:02p Jeff - * include stdlib for atexit - * - * 4 7/28/99 3:44p Kevin - * Mac! - * - * 3 5/11/99 11:18a Jeff - * finished rock 'n' ride - * - * 2 5/10/99 9:25p Jeff - * first phase of Rock 'n' Ride support added - * - * $NoKeywords: $ - */ -#include "mono.h" -#include "ddio.h" -#include "debug.h" -#include "game.h" -#include "pserror.h" -#include "rocknride.h" -#include -#include -#include -bool RocknRide_enabled = false; -float RocknRide_lastcontroller_update = 0.0f; -float RocknRide_lastforce_update = 0.0f; -#define RNR_CONTROLLER_UPDATE 1.0f / 20.0f -#define RNR_FORCE_UPDATE 1.0f / 60.0f -tSerialPort RocknRide_serial_port; -// RNR_Shutdown -// -// Shutsdown the Rock 'n' Ride device -void RNR_Shutdown(void) { - if (!RocknRide_enabled) - return; - if (RocknRide_serial_port) - ddio_SerialClosePort(RocknRide_serial_port); -} -// RNR_Initialize -// -// Initializes the Rock 'n' Ride device -// Pass in the COMM port -bool RNR_Initialize(int comm_port) { - if (RocknRide_enabled) - return true; - static bool called = false; - RocknRide_serial_port = ddio_SerialOpenPort(comm_port, 9600); - if (!RocknRide_serial_port) { - mprintf((0, "Couldn't open serial port\n")); - return false; - } - RocknRide_enabled = true; - // only setup once - if (!called) { - called = true; - atexit(RNR_Shutdown); - } - return true; -} -// RNR_SendPacket -// -// Sends off a packet to the Rock 'n' Ride device -void RNR_SendPacket(tRocknride_packet *packet) { - if (!RocknRide_enabled) - return; - ASSERT(packet); - if (!packet) - return; - ubyte data[32]; - int size = -1; - switch (packet->packet_type) { - case RNRP_POSITION: - size = 3; - data[0] = 'P'; - data[1] = packet->pos.x; - data[2] = packet->pos.y; - break; - case RNRP_HIT: - size = 3; - data[0] = 'H'; - data[1] = packet->pos.x; - data[2] = packet->pos.y; - break; - case RNRP_GAMESTATUS: - size = 2; - data[0] = 'G'; - data[1] = packet->status; - break; - } - if (size != -1) { - // send off the packet to the device - //@@char test[32]; - //@@memcpy(test,data,size); - //@@test[size] = '\0'; - //@@mprintf((0,"DATA: %s\n",test)); - for (int i = 0; i < size; i++) { - if (!ddio_SerialWriteByte(RocknRide_serial_port, data[i])) - break; - } - } -} -// RNR_UpdateControllerInfo -// -// Updates any Rock 'n' Ride controller data for the frame (if needed) -void RNR_UpdateControllerInfo(game_controls *controls) { - if (!RocknRide_enabled) - return; - ASSERT(controls); - if (!controls) - return; - if (RocknRide_lastcontroller_update > Gametime) { - RocknRide_lastcontroller_update = 0.0f; // handle new game started - } - if (RocknRide_lastcontroller_update + RNR_CONTROLLER_UPDATE > Gametime) - return; // don't update yet - - RocknRide_lastcontroller_update = Gametime; - ubyte x_val, y_val; - x_val = 128 + (127.0f * controls->heading_thrust); - y_val = 128 + (127.0f * controls->pitch_thrust); - tRocknride_packet packet; - packet.packet_type = RNRP_POSITION; - packet.pos.x = x_val; - packet.pos.y = y_val; - RNR_SendPacket(&packet); -} -// RNR_UpdateForceFeedbackInfo -// -// Updates any Force Feedback effects -void RNR_UpdateForceFeedbackInfo(float magnitude, vector *direction) { - if (!RocknRide_enabled) - return; - ASSERT(magnitude >= 0 && magnitude <= 1); - - if (RocknRide_lastforce_update > Gametime) { - RocknRide_lastforce_update = 0.0f; // handle new game started - } - if (RocknRide_lastforce_update + RNR_FORCE_UPDATE > Gametime) - return; // don't update yet - - RocknRide_lastforce_update = Gametime; - matrix mat = Identity_matrix; - angvec ag; - vm_VectorToMatrix(&mat, direction); - vm_ExtractAnglesFromMatrix(&ag, &mat); - - float sh, ch; - vm_SinCos(ag.h, &sh, &ch); - - float x_val = magnitude * ch; - float y_val = magnitude * sh; - x_val *= 255.0f; - y_val *= 255.0f; - ubyte x_b = (ubyte)x_val; - ubyte y_b = (ubyte)y_val; - tRocknride_packet packet; - packet.packet_type = RNRP_HIT; - packet.pos.x = x_b; - packet.pos.y = y_b; - RNR_SendPacket(&packet); -} -// RNR_UpdateForceFeedbackInfo -// -// Updates any Force Feedback effects -void RNR_UpdateForceFeedbackInfo(float magnitude, int *direction) { - if (!RocknRide_enabled) - return; - if (RocknRide_lastforce_update > Gametime) { - RocknRide_lastforce_update = 0.0f; // handle new game started - } - if (RocknRide_lastforce_update + RNR_FORCE_UPDATE > Gametime) - return; // don't update yet - - RocknRide_lastforce_update = Gametime; - - Int3(); // I don't think we ever use FF with an int direction -} -// RNR_UpdateGameStatus -// -// Updates a game status to a Rock 'n' Ride chair -void RNR_UpdateGameStatus(ubyte status) { - if (!RocknRide_enabled) - return; - tRocknride_packet packet; - packet.packet_type = RNRP_GAMESTATUS; - - bool send = true; - switch (status) { - case RNRGSC_PLAYERDIES: - packet.status = 'D'; - break; - case RNRGSC_INMENU: - packet.status = 'P'; - break; - default: - send = false; - break; - } - if (send) - RNR_SendPacket(&packet); -} \ No newline at end of file diff --git a/Descent3/rocknride.h b/Descent3/rocknride.h deleted file mode 100644 index 76905f28d..000000000 --- a/Descent3/rocknride.h +++ /dev/null @@ -1,92 +0,0 @@ -/* -* Descent 3 -* Copyright (C) 2024 Parallax Software -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*/ - -/* - * $Logfile: /DescentIII/Main/rocknride.h $ - * $Revision: 2 $ - * $Date: 5/10/99 9:25p $ - * $Author: Jeff $ - * - * Rock 'n' Ride - * - * $Log: /DescentIII/Main/rocknride.h $ - * - * 2 5/10/99 9:25p Jeff - * first phase of Rock 'n' Ride support added - * - * $NoKeywords: $ - */ - -#ifndef __ROCK_N_RIDE_H_ -#define __ROCK_N_RIDE_H_ - -#include "pstypes.h" -#include "controls.h" -#include "vecmat.h" - -extern bool RocknRide_enabled; - -#define RNRP_POSITION 0x00 // Rock 'n' Ride Position Packet -#define RNRP_HIT 0x01 // Rock 'n' Ride Hit Packet -#define RNRP_GAMESTATUS 0x02 // Rock 'n' Ride Game Status Packet -typedef struct { - ubyte packet_type; - union { - struct { - ubyte x, y; - } pos; - ubyte status; - }; -} tRocknride_packet; - -// Rock 'n' Ride Game Status codes -#define RNRGSC_PLAYERDIES 0 // The player has died -#define RNRGSC_INMENU 1 // The player is in a UI menu - -// RNR_Initialize -// -// Initializes the Rock 'n' Ride device -// Pass in the COMM port -bool RNR_Initialize(int comm_port); - -// RNR_SendPacket -// -// Sends off a packet to the Rock 'n' Ride device -void RNR_SendPacket(tRocknride_packet *packet); - -// RNR_UpdateControllerInfo -// -// Updates any Rock 'n' Ride controller data for the frame (if needed) -void RNR_UpdateControllerInfo(game_controls *controls); - -// RNR_UpdateForceFeedbackInfo -// -// Updates any Force Feedback effects -void RNR_UpdateForceFeedbackInfo(float magnitude, vector *direction); - -// RNR_UpdateForceFeedbackInfo -// -// Updates any Force Feedback effects -void RNR_UpdateForceFeedbackInfo(float magnitude, int *direction); - -// RNR_UpdateGameStatus -// -// Updates a game status to a Rock 'n' Ride chair -void RNR_UpdateGameStatus(ubyte status); - -#endif From a26ced7d4cf13f7fc1ca5bb7ad57041de3c023e6 Mon Sep 17 00:00:00 2001 From: Edu Garcia <28616+Arcnor@users.noreply.github.com> Date: Mon, 6 May 2024 16:06:29 +0100 Subject: [PATCH 2/2] remove unused serial code --- ddio_lnx/lnxio.cpp | 125 --------------------------- ddio_win/CMakeLists.txt | 1 - ddio_win/serial.cpp | 182 ---------------------------------------- lib/ddio_common.h | 15 ---- 4 files changed, 323 deletions(-) delete mode 100644 ddio_win/serial.cpp diff --git a/ddio_lnx/lnxio.cpp b/ddio_lnx/lnxio.cpp index 96332b43d..5c5d57b68 100644 --- a/ddio_lnx/lnxio.cpp +++ b/ddio_lnx/lnxio.cpp @@ -56,16 +56,7 @@ // ---------------------------------------------------------------------------- #include -#include -#include #include -#include - -#include -#include -#include - -#include #include "pserror.h" #include "application.h" @@ -74,8 +65,6 @@ bool DDIO_init = false; oeLnxApplication *Lnx_app_obj = NULL; -static struct termios oldtio[4]; -static int portfd[4] = {-1, -1, -1, -1}; // ---------------------------------------------------------------------------- // Initialization and destruction functions @@ -92,11 +81,6 @@ void ddio_InternalClose() { mprintf((0, "DDIO: ddio_InternalClose() called.")); if (DDIO_init) { - for (int i = 0; i < (sizeof(portfd) / sizeof(portfd[0])); i++) { - if (portfd[i] != -1) - ddio_SerialClosePort((tSerialPort)&portfd[i]); - } // for - DDIO_init = false; Lnx_app_obj = NULL; } // if @@ -115,112 +99,3 @@ void ddio_DebugMessage(unsigned err, char *fmt, ...) { mprintf((0, "%s\n", buf)); } -// takes port number 1-4, returns a port object -tSerialPort ddio_SerialOpenPort(int port_number, int baud) { -#if MACOSX - return NULL; -#else - char devName[50]; - struct termios newtio; - unsigned int _baud; - - mprintf((0, "DDIO: ddio_SerialOpenPort(%d) called.", port_number)); - - if ((port_number < 0) || (port_number > 3)) - return (NULL); - - if (baud == 1200) - _baud = B1200; - else if (baud == 2400) - _baud = B2400; - else if (baud == 4800) - _baud = B4800; - else if (baud == 9600) - _baud = B9600; - else if (baud == 19200) - _baud = B19200; - else if (baud == 38400) - _baud = B38400; - else if (baud == 57600) - _baud = B57600; - else if (baud == 115200) - _baud = B115200; - else - return (NULL); - - snprintf(devName, sizeof(devName), "/dev/ttyS%d", port_number); - int fd = open(devName, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK); - if (fd == -1) { - mprintf((0, "DDIO: ddio_SerialOpenPort(%d) FAILED.", port_number)); - return (NULL); - } // if - - mprintf((0, "DDIO: opened file descriptor is (%d).", fd)); - - portfd[port_number] = fd; - - tcgetattr(fd, &oldtio[port_number]); - memset(&newtio, '\0', sizeof(newtio)); - - newtio.c_cflag = _baud | CLOCAL | CS8; //_baud | CRTSCTS | CLOCAL | CS8; - newtio.c_iflag = 0; // IGNPAR | IGNBRK; - newtio.c_oflag = 0; - newtio.c_lflag = 0; // ICANON; - - newtio.c_cc[VINTR] = 0; /* Ctrl-c */ - newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */ - newtio.c_cc[VERASE] = 0; /* del */ - newtio.c_cc[VKILL] = 0; /* @ */ - newtio.c_cc[VEOF] = 4; /* Ctrl-d */ - newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ - newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ - newtio.c_cc[VSWTC] = 0; /* '\0' */ - newtio.c_cc[VSTART] = 0; /* Ctrl-q */ - newtio.c_cc[VSTOP] = 0; /* Ctrl-s */ - newtio.c_cc[VSUSP] = 0; /* Ctrl-z */ - newtio.c_cc[VEOL] = 0; /* '\0' */ - newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */ - newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */ - newtio.c_cc[VWERASE] = 0; /* Ctrl-w */ - newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */ - newtio.c_cc[VEOL2] = 0; /* '\0' */ - - tcflush(fd, TCIOFLUSH); - tcsetattr(fd, TCSANOW, &newtio); - - mprintf((0, "DDIO: ddio_SerialOpenPort(%d) succeeded.", port_number)); - return (&portfd[port_number]); -#endif -} - -// takes port structure and frees it. -void ddio_SerialClosePort(tSerialPort port) { -#if !MACOSX - int fd = *((int *)port); - int index = (int)((((char *)port) - ((char *)&portfd)) / sizeof(int)); - - mprintf((0, "DDIO: ddio_SerialClosePort(comport == %d) called.", index)); - mprintf((0, "DDIO: Serial port file descriptor to close is (%d).", fd)); - - if (fd != -1) { - tcsetattr(fd, TCSANOW, &oldtio[index]); - close(fd); - *((int *)port) = -1; - } // if -#endif -} - -// writes one byte. true return value means it worked. -bool ddio_SerialWriteByte(tSerialPort port, ubyte b) { -#if MACOSX - return false; -#else - int fd = *((int *)port); - bool retVal = ((write(fd, &b, sizeof(b)) == sizeof(b)) ? true : false); - if (retVal == false) { - mprintf((0, "DDIO: Writing byte (%u) to descriptor (%d) failed!", (unsigned int)b, fd)); - } // if - - return (retVal); -#endif -} diff --git a/ddio_win/CMakeLists.txt b/ddio_win/CMakeLists.txt index 61e9ca6d4..8b9be7d62 100644 --- a/ddio_win/CMakeLists.txt +++ b/ddio_win/CMakeLists.txt @@ -1,6 +1,5 @@ set(HEADERS ddio_win.h) set(CPPS - serial.cpp winfile.cpp winforcefeedback.cpp winio.cpp diff --git a/ddio_win/serial.cpp b/ddio_win/serial.cpp deleted file mode 100644 index 50f70ad29..000000000 --- a/ddio_win/serial.cpp +++ /dev/null @@ -1,182 +0,0 @@ -/* -* Descent 3 -* Copyright (C) 2024 Parallax Software -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*/ - -/* - * $Source: $ - * $Revision: 2 $ - * $Author: Jeff $ - * $Date: 5/11/99 11:28a $ - * - * COM port interface - * - * $Log: /DescentIII/Main/ddio_win/serial.cpp $ - * - * 2 5/11/99 11:28a Jeff - * added serial support (SAMIR) - * - * 1 5/10/99 9:27p Jeff - * - */ - -#define WIN32_LEAN_AND_MEAN -#include -#include "ddio_win.h" -#include "mem.h" -#include "pserror.h" -#include "ddio.h" - -#include - -#define ASCII_XON 0x11 -#define ASCII_XOFF 0x13 - -#define N_SERIAL_PORTS 4 - -// internal struct -typedef struct tWin32SerialPort { - sbyte port; - sbyte connect; - HANDLE hFile; - DCB dcb; -} tWin32SerialPort; - -// takes port number 1-4, returns a port object -tSerialPort ddio_SerialOpenPort(int port_number, int baud) { - COMMTIMEOUTS ctimeouts; - char filename[16]; - tWin32SerialPort port_obj; - - ASSERT(port_number >= 1 && port_number <= N_SERIAL_PORTS); - - if (port_number < 1 || port_number > N_SERIAL_PORTS) { - return NULL; - } - - sprintf(filename, "COM%d", port_number); - - port_obj.hFile = - CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if (port_obj.hFile == INVALID_HANDLE_VALUE) { - return NULL; - } - port_obj.port = (sbyte)port_number; - - // Modem COMport is open. - SetCommMask(port_obj.hFile, 0); - SetupComm(port_obj.hFile, 1024, 1024); - PurgeComm(port_obj.hFile, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); - - // Timeout after 10 sec. - ctimeouts.ReadIntervalTimeout = 0xffffffff; - ctimeouts.ReadTotalTimeoutMultiplier = 0; - ctimeouts.ReadTotalTimeoutConstant = 10000; - ctimeouts.WriteTotalTimeoutMultiplier = 0; - ctimeouts.WriteTotalTimeoutConstant = 10000; - SetCommTimeouts(port_obj.hFile, &ctimeouts); - - // Setup parameters for Connection - // 38400 8N1 - port_obj.dcb.DCBlength = sizeof(DCB); - GetCommState(port_obj.hFile, &port_obj.dcb); - - port_obj.dcb.BaudRate = baud; - - mprintf((0, "COM%d (%d) is opened.\n", port_obj.port, baud)); - - port_obj.dcb.fBinary = 1; - port_obj.dcb.Parity = NOPARITY; - port_obj.dcb.fNull = 0; - port_obj.dcb.XonChar = ASCII_XON; - port_obj.dcb.XoffChar = ASCII_XOFF; - port_obj.dcb.XonLim = 1024; - port_obj.dcb.XoffLim = 1024; - port_obj.dcb.EofChar = 0; - port_obj.dcb.EvtChar = 0; - port_obj.dcb.fDtrControl = DTR_CONTROL_ENABLE; // dtr=on - port_obj.dcb.fRtsControl = RTS_CONTROL_ENABLE; - - port_obj.dcb.ByteSize = 8; - port_obj.dcb.StopBits = ONESTOPBIT; - port_obj.dcb.fParity = FALSE; - - port_obj.dcb.fOutxDsrFlow = FALSE; - port_obj.dcb.fOutxCtsFlow = FALSE; // rts/cts off - - // obj->dcb.fInX = obj->dcb.fOutX = 1; // Software flow control XON/XOFF - - if (SetCommState(port_obj.hFile, &port_obj.dcb) == TRUE) { - // Send DTR - EscapeCommFunction(port_obj.hFile, SETDTR); - port_obj.connect = 1; - } else { - mprintf((1, "COMM: Unable to set CommState: (%x)\n", GetLastError())); - CloseHandle(port_obj.hFile); - port_obj.connect = 0; - - return NULL; - } - - tWin32SerialPort *obj = (tWin32SerialPort *)mem_malloc(sizeof(tWin32SerialPort)); - if (obj) { - memcpy(obj, &port_obj, sizeof(port_obj)); - } else { - CloseHandle(port_obj.hFile); - port_obj.connect = 0; - return NULL; - } - - return obj; -} - -// takes port structure -void ddio_SerialClosePort(tSerialPort port) { - tWin32SerialPort *obj = (tWin32SerialPort *)port; - - if (!port) - return; - - if (obj->connect) { - SetCommMask(obj->hFile, 0); - EscapeCommFunction(obj->hFile, CLRDTR); - PurgeComm(obj->hFile, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); - CloseHandle(obj->hFile); - - obj->connect = 0; - } - - obj->hFile = NULL; - mprintf((0, "COM%d closed.\n", obj->port)); - - mem_free(obj); -} - -bool ddio_SerialWriteByte(tSerialPort port, ubyte b) { - tWin32SerialPort *obj = (tWin32SerialPort *)port; - DWORD length; - - if (!obj->connect) - return false; - - if (!WriteFile(obj->hFile, &b, 1, &length, NULL)) { - mprintf((0, "Failed to write byte to COM%d\n", obj->port)); - return false; - } - - return true; -} \ No newline at end of file diff --git a/lib/ddio_common.h b/lib/ddio_common.h index 68b9beda3..39dc77c6a 100644 --- a/lib/ddio_common.h +++ b/lib/ddio_common.h @@ -195,21 +195,6 @@ int ddio_AsciiToKey(int ascii); void ddio_SetKeyboardLanguage(int language); -// ---------------------------------------------------------------------------- -// SERIAL CONSTANTS AND FUNCTIONS -// ---------------------------------------------------------------------------- -// serial port data type -typedef void *tSerialPort; - -// takes port number 1-4, returns a port object -tSerialPort ddio_SerialOpenPort(int port_number, int baud); - -// takes port structure and frees it. -void ddio_SerialClosePort(tSerialPort port); - -// writes one byte. true return value means it worked. -bool ddio_SerialWriteByte(tSerialPort port, ubyte b); - // ---------------------------------------------------------------------------- // KEYBOARD CONSTANTS // ----------------------------------------------------------------------------