Fixed missing headers when compiling with gcc 4.7
[barry.git] / tools / bwatch.cc
blobca0ca4605bea2228b5378afae32041d4b3ce344d
1 ///
2 /// \file bwatch.cc
3 /// Display a regularly updated video of the BlackBerry screen
4 ///
6 /*
7 Copyright (C) 2011, Alberto Mattea
8 Copyright (C) 2011-2012, Net Direct Inc. (http://www.netdirect.ca/)
10 Some parts are inspired from bjavaloader
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU General Public License in the COPYING file at the
22 root directory of this project for more details.
26 #include <barry/barry.h>
27 #include <iostream>
28 #include <vector>
29 #include <string>
30 #include <cstring>
31 #include <algorithm>
32 #include <fstream>
33 #include <string.h>
34 #include <unistd.h>
35 #include <SDL/SDL.h>
37 using namespace std;
38 using namespace Barry;
40 void Usage()
42 int logical, major, minor;
43 const char *Version = Barry::Version(logical, major, minor);
45 cerr
46 << "bwatch - View video of BlackBerry screenshots\n"
47 << " Copyright 2011, Alberto Mattea\n"
48 << " Copyright 2011-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
49 << " Using: " << Version << "\n"
50 << "\n"
51 << " -d delay Delay interval between screenshots, in milliseconds.\n"
52 << " The lower the value, the higher the load on the device.\n"
53 << " Default is 500ms.\n"
54 << " -p pin PIN of device to talk with\n"
55 << " If only one device is plugged in, this flag is optional\n"
56 << " -P pass Simplistic method to specify device password\n"
57 << " -v Dump protocol data during operation\n"
58 << endl;
61 int main(int argc, char *argv[])
63 try {
65 cout.sync_with_stdio(true); // leave this on, since libusb uses stdio for debug messages
67 uint32_t pin = 0;
68 bool data_dump = false;
69 string password;
70 int delay = 500; // default delay of 500 ms
72 // process command line options
73 for(;;) {
74 int cmd = getopt(argc, argv, "d:hp:P:v");
75 if( cmd == -1 )
76 break;
78 switch( cmd )
80 case 'd': // delay interval in milliseconds
81 delay = atoi(optarg);
82 if( !delay ) {
83 cerr << "Invalid interval value of '" << optarg << "'. Defaulting to 500ms." << endl;
84 delay = 500;
86 break;
88 case 'p': // Blackberry PIN
89 pin = strtoul(optarg, NULL, 16);
90 break;
92 case 'P': // Device password
93 password = optarg;
94 break;
96 case 'v': // data dump on
97 data_dump = true;
98 break;
100 case 'h': // help
101 default:
102 Usage();
103 return 0;
107 // Init SDL
108 int sdl_width = -1, sdl_height = -1;
109 SDL_Surface *screen = NULL;
110 SDL_Event event;
111 int keypress = 0;
112 if( SDL_Init(SDL_INIT_VIDEO) < 0 )
113 return 1;
115 // Initialize the barry library. Must be called before
116 // anything else.
117 Barry::Init(data_dump);
119 JLScreenInfo info;
120 Data image, bitmap;
122 // Probe the USB bus for Blackberry devices and display.
123 // If user has specified a PIN, search for it in the
124 // available device list here as well
125 Barry::Probe probe;
126 int activeDevice = probe.FindActive(pin);
127 if( activeDevice == -1 ) {
128 cerr << "No device selected, or PIN not found" << endl;
129 return 1;
132 // Main loop
133 cout << "Press a key to exit..." << endl;
134 while( !keypress ) {
135 // Put this inside it's own block to avoid blocking the handheld
137 // Create our controller object
138 Barry::Controller con(probe.Get(activeDevice));
139 Barry::Mode::JavaLoader javaloader(con);
140 javaloader.Open(password.c_str());
141 javaloader.StartStream();
142 // Take a screenshot
143 // - info object contains the screenshot properties
144 // (width, height...)
145 // - image will be filled with the raw pixel
146 // screenshot data
147 javaloader.GetScreenshot(info, image);
148 javaloader.StopStream();
151 // Set the video mode according to the screenshot data
152 if( sdl_width != info.width || sdl_height != info.height ) {
153 sdl_width = info.width;
154 sdl_height = info.height;
155 if( !(screen = SDL_SetVideoMode(info.width, info.height, 0, SDL_HWSURFACE)) ) {
156 SDL_Quit();
157 return 1;
159 SDL_WM_SetCaption("Blackberry", 0);
162 // May want to tune this between 100 and 1000
163 SDL_Delay(500);
165 // Convert to 24-bit RGB
166 ScreenshotToRGB(info, image, bitmap, 0, 24, false);
168 // Do the blit
169 SDL_Surface *tmp;
170 tmp = SDL_CreateRGBSurfaceFrom((char*)bitmap.GetData(), info.width, info.height, 24, info.width*3, 0, 0, 0, 0);
171 SDL_BlitSurface(tmp, 0, screen, 0);
172 SDL_Flip(screen);
173 while(SDL_PollEvent(&event)) {
174 switch (event.type) {
175 case SDL_QUIT:
176 keypress = 1;
177 break;
178 case SDL_KEYDOWN:
179 keypress = 1;
180 break;
185 // Stop SDL
186 SDL_Quit();
187 return 0;
191 catch( Usb::Error &ue) {
192 std::cout << endl; // flush any normal output first
193 std::cerr << "Usb::Error caught: " << ue.what() << endl;
194 return 1;
196 catch( Barry::Error &se ) {
197 std::cout << endl;
198 std::cerr << "Barry::Error caught: " << se.what() << endl;
199 return 1;
201 catch( std::exception &e ) {
202 std::cout << endl;
203 std::cerr << "std::exception caught: " << e.what() << endl;
204 return 1;