tools: grouped common tool code into tools/util.{h,cc}
[barry/progweb.git] / tools / bwatch.cc
blob7b0210a8f7599712ab7946e2d4489fb4d89cf6cf
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 <SDL/SDL.h>
36 using namespace std;
37 using namespace Barry;
39 void Usage()
41 int logical, major, minor;
42 const char *Version = Barry::Version(logical, major, minor);
44 cerr
45 << "bwatch - View video of BlackBerry screenshots\n"
46 << " Copyright 2011, Alberto Mattea\n"
47 << " Copyright 2011-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
48 << " Using: " << Version << "\n"
49 << "\n"
50 << " -d delay Delay interval between screenshots, in milliseconds.\n"
51 << " The lower the value, the higher the load on the device.\n"
52 << " Default is 500ms.\n"
53 << " -p pin PIN of device to talk with\n"
54 << " If only one device is plugged in, this flag is optional\n"
55 << " -P pass Simplistic method to specify device password\n"
56 << " -v Dump protocol data during operation\n"
57 << endl;
60 int main(int argc, char *argv[])
62 try {
64 cout.sync_with_stdio(true); // leave this on, since libusb uses stdio for debug messages
66 uint32_t pin = 0;
67 bool data_dump = false;
68 string password;
69 int delay = 500; // default delay of 500 ms
71 // process command line options
72 for(;;) {
73 int cmd = getopt(argc, argv, "d:hp:P:v");
74 if( cmd == -1 )
75 break;
77 switch( cmd )
79 case 'd': // delay interval in milliseconds
80 delay = atoi(optarg);
81 if( !delay ) {
82 cerr << "Invalid interval value of '" << optarg << "'. Defaulting to 500ms." << endl;
83 delay = 500;
85 break;
87 case 'p': // Blackberry PIN
88 pin = strtoul(optarg, NULL, 16);
89 break;
91 case 'P': // Device password
92 password = optarg;
93 break;
95 case 'v': // data dump on
96 data_dump = true;
97 break;
99 case 'h': // help
100 default:
101 Usage();
102 return 0;
106 // Init SDL
107 int sdl_width = -1, sdl_height = -1;
108 SDL_Surface *screen = NULL;
109 SDL_Event event;
110 int keypress = 0;
111 if( SDL_Init(SDL_INIT_VIDEO) < 0 )
112 return 1;
114 // Initialize the barry library. Must be called before
115 // anything else.
116 Barry::Init(data_dump);
118 JLScreenInfo info;
119 Data image, bitmap;
121 // Probe the USB bus for Blackberry devices and display.
122 // If user has specified a PIN, search for it in the
123 // available device list here as well
124 Barry::Probe probe;
125 int activeDevice = probe.FindActive(pin);
126 if( activeDevice == -1 ) {
127 cerr << "No device selected, or PIN not found" << endl;
128 return 1;
131 // Main loop
132 cout << "Press a key to exit..." << endl;
133 while( !keypress ) {
134 // Put this inside it's own block to avoid blocking the handheld
136 // Create our controller object
137 Barry::Controller con(probe.Get(activeDevice));
138 Barry::Mode::JavaLoader javaloader(con);
139 javaloader.Open(password.c_str());
140 javaloader.StartStream();
141 // Take a screenshot
142 // - info object contains the screenshot properties
143 // (width, height...)
144 // - image will be filled with the raw pixel
145 // screenshot data
146 javaloader.GetScreenshot(info, image);
147 javaloader.StopStream();
150 // Set the video mode according to the screenshot data
151 if( sdl_width != info.width || sdl_height != info.height ) {
152 sdl_width = info.width;
153 sdl_height = info.height;
154 if( !(screen = SDL_SetVideoMode(info.width, info.height, 0, SDL_HWSURFACE)) ) {
155 SDL_Quit();
156 return 1;
158 SDL_WM_SetCaption("Blackberry", 0);
161 // May want to tune this between 100 and 1000
162 SDL_Delay(500);
164 // Convert to 24-bit RGB
165 ScreenshotToRGB(info, image, bitmap, 0, 24, false);
167 // Do the blit
168 SDL_Surface *tmp;
169 tmp = SDL_CreateRGBSurfaceFrom((char*)bitmap.GetData(), info.width, info.height, 24, info.width*3, 0, 0, 0, 0);
170 SDL_BlitSurface(tmp, 0, screen, 0);
171 SDL_Flip(screen);
172 while(SDL_PollEvent(&event)) {
173 switch (event.type) {
174 case SDL_QUIT:
175 keypress = 1;
176 break;
177 case SDL_KEYDOWN:
178 keypress = 1;
179 break;
184 // Stop SDL
185 SDL_Quit();
186 return 0;
190 catch( Usb::Error &ue) {
191 std::cout << endl; // flush any normal output first
192 std::cerr << "Usb::Error caught: " << ue.what() << endl;
193 return 1;
195 catch( Barry::Error &se ) {
196 std::cout << endl;
197 std::cerr << "Barry::Error caught: " << se.what() << endl;
198 return 1;
200 catch( std::exception &e ) {
201 std::cout << endl;
202 std::cerr << "std::exception caught: " << e.what() << endl;
203 return 1;