forked from OneLoneCoder/Javidx9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneLoneCoder_LogitechG13Twitch.cpp
153 lines (126 loc) · 3.75 KB
/
OneLoneCoder_LogitechG13Twitch.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
OneLoneCoder.com - Program a Logitech Keyboard To Display Twitch Chat
"Put Your Money Where Your Mouth Is" - @Javidx9
License
~~~~~~~
Copyright (C) 2018 Javidx9
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; See license for details.
Original works located at:
https://www.github.com/onelonecoder
https://www.onelonecoder.com
https://www.youtube.com/javidx9
GNU GPLv3
https://github.com/OneLoneCoder/videos/blob/master/LICENSE
From Javidx9 :)
~~~~~~~~~~~~~~~
Hello! Ultimately I don't care what you use this for. It's intended to be
educational, and perhaps to the oddly minded - a little bit of fun.
Please hack this, change it and use it in any way you see fit. You acknowledge
that I am not responsible for anything bad that happens as a result of
your actions. However this code is protected by GNU GPLv3, see the license in the
github repo. This means you must attribute me if you use it. You can view this
license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
Cheers!
Background
~~~~~~~~~~
A very minimal example of using the Logitech SDK to interface with a keyboard display,
then uses a really sloppy implementation of sockets, to connect to a twitch chat
session, displaying the chat on the keybaord screen.
Future Modifications
~~~~~~~~~~~~~~~~~~~~
1) Wrap around text display on screen
Author
~~~~~~
Twitter: @javidx9
Blog: www.onelonecoder.com
Video:
~~~~~~
https://youtu.be/8UXCo-GhiF0
Last Updated: 23/05/2017
*/
#include <iostream>
#include <list>
using namespace std;
// Include Winsock
#include <winsock2.h>
#include <ws2tcpip.h>
// Include Logitech library
#include "LogitechLCDLib.h"
int main()
{
// Load the driver
LogiLcdInit(L"OneLoneCoder Test", LOGI_LCD_TYPE_MONO);
// Check hardware is connected
if (!LogiLcdIsConnected(LOGI_LCD_TYPE_MONO))
wcout << "Hardware not found" << endl;
// Load WinSock
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
wcout << "Could not start WinSock" << endl;
// Get address to twitch server
struct addrinfo *addr = nullptr;
if (getaddrinfo("irc.chat.twitch.tv", "6667", nullptr, &addr) != 0)
wcout << "Failed to get address info" << endl;
// Create socket
SOCKET sock = INVALID_SOCKET;
sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
// Connect to server via socket
int i = connect(sock, addr->ai_addr, (int)addr->ai_addrlen);
if (i != SOCKET_ERROR)
{
// Handshake with twitch
string s;
// Authenticate
s = "PASS oauth:###INSERT YOUR OAUTH HERE###\r\n";
send(sock, s.c_str(), s.length(), 0);
// Register your twitch name
s = "NICK javidx9\r\n";
send(sock, s.c_str(), s.length(), 0);
// Join a twitch chat
s = "JOIN #javidx9\r\n";
send(sock, s.c_str(), s.length(), 0);
}
char buffer[100];
string s;
list<string> sLines = { "","","","" };
while (1)
{
// Get Twitch Server Message
int i = recv(sock, buffer, 100, 0);
for (int j = 0; j < i; j++)
{
s.append(1, buffer[j]);
if (buffer[j] == '\n')
{
// User name is between first ':' and '!'.
// User chat is after second ':'
size_t m = s.find('!');
string sUserName = s.substr(1, m - 1);
size_t n = s.find(':', m);
if (n != string::npos)
{
string chat = s.substr(n + 1);
cout << sUserName.c_str() << ": " << chat.c_str() << endl;
sLines.pop_front();
sLines.push_back(sUserName + ": " + chat);
}
s.clear();
}
}
// Display 4 lines of text from list
int p = 0;
for (auto k : sLines)
{
wstring ws;
ws.assign(k.begin(), k.end());
LogiLcdMonoSetText(p, (wchar_t*)ws.c_str()); // yuck...
p++;
}
// Update display
LogiLcdUpdate();
}
LogiLcdShutdown();
return 0;
}