/[rdesktop]/sourceforge.net/trunk/rdesktop/uiports/qtewin.cpp
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/rdesktop/uiports/qtewin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 791 - (hide annotations)
Sat Oct 30 17:02:54 2004 UTC (19 years, 6 months ago) by jsorg71
File size: 61729 byte(s)
got 24 bpp working in qtewin.cpp

1 jsorg71 748 /*
2     rdesktop: A Remote Desktop Protocol client.
3 jsorg71 780 User interface services - QT Emb System
4 jsorg71 776 Copyright (C) Jay Sorg 2004
5 jsorg71 748
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10    
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     GNU General Public License for more details.
15    
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19     */
20    
21     //#define SHARP
22    
23     #ifdef SHARP
24     #include <qpe/qpeapplication.h>
25     #else
26     #include <qapplication.h>
27     #endif
28 jsorg71 791 #include <qcursor.h>
29 jsorg71 748 #include <qmainwindow.h>
30     #include <qwidget.h>
31     #include <qpainter.h>
32     #include <qimage.h>
33     #include <qsocketnotifier.h>
34     #include <qscrollview.h>
35     #include <qmessagebox.h>
36     #include <qpushbutton.h>
37     #include <qlineedit.h>
38     #include <qcombobox.h>
39     #include <qlabel.h>
40     #include <qfile.h>
41     #include <qcheckbox.h>
42     #include <qpopupmenu.h>
43     #include <stdlib.h>
44     #include <stdarg.h> // va_list va_start va_end
45 jsorg71 780 #include <unistd.h> // gethostname
46     #include <pwd.h> // getpwuid
47 jsorg71 748
48 jsorg71 776 #include "../rdesktop.h"
49     #include "qtewin.h"
50    
51     /* types */
52     struct QColorMap
53     {
54     uint32 RGBColors[256];
55 jsorg71 780 uint32 NumColors;
56 jsorg71 776 };
57    
58     struct bitmap
59     {
60     int w;
61     int h;
62 jsorg71 780 uint8 * data;
63 jsorg71 776 };
64    
65 jsorg71 780 extern int g_tcp_port_rdp;
66     extern int g_dsp_fd;
67    
68     #ifdef WITH_RDPSND
69     BOOL g_rdpsnd = True;
70     extern int g_dsp_busy;
71     #endif
72 jsorg71 748 int g_encryption = 1;
73     int g_bitmap_cache = 1;
74     int g_bitmap_cache_persist_enable = 0;
75     int g_bitmap_cache_precache = 1;
76     int g_use_rdp5 = 1;
77     int g_desktop_save = 1;
78     int g_bitmap_compression = 1;
79     int g_rdp5_performanceflags = 0;
80     int g_console_session = 0;
81     int g_keylayout = 0x409; /* Defaults to US keyboard layout */
82     int g_width = 640;
83     int g_height = 480;
84     int g_server_bpp = 8;
85     char g_hostname[16] = "";
86     char g_username[100] = "";
87    
88 jsorg71 780 static uint32 g_flags = RDP_LOGON_NORMAL;
89     static char g_server[64] = "";
90     static char g_domain[16] = "";
91     static char g_password[16] = "";
92     static char g_shell[128] = "";
93     static char g_directory[32] = "";
94     static int g_fullscreen = 0;
95     static int g_global_sock = 0;
96     static int g_deactivated = 0;
97     static uint32 g_ext_disc_reason = 0;
98 jsorg71 748
99 jsorg71 780 static QSocketNotifier * g_SocketNotifier = 0;
100     static QSocketNotifier * g_SoundNotifier = 0;
101 jsorg71 748 #ifdef SHARP
102 jsorg71 780 static QPEApplication * g_App = 0;
103 jsorg71 748 #else
104 jsorg71 780 static QApplication * g_App = 0;
105 jsorg71 748 #endif
106 jsorg71 780 static QMyMainWindow * g_MW = 0;
107     static QMyScrollView * g_SV = 0;
108     static struct QColorMap * g_CM = 0;
109     static uint8 * g_BS = 0; /* the screen data */
110 jsorg71 776 static int g_clipx = 0;
111     static int g_clipy = 0;
112     static int g_clipcx = 0;
113     static int g_clipcy = 0;
114 jsorg71 748
115 jsorg71 780 #define BPP ((g_server_bpp + 7) / 8)
116     #define GETPIXEL8(d, x, y, w) (*(((uint8*)d) + ((y) * (w) + (x))))
117     #define GETPIXEL16(d, x, y, w) (*(((uint16*)d) + ((y) * (w) + (x))))
118     #define GETPIXEL32(d, x, y, w) (*(((uint32*)d) + ((y) * (w) + (x))))
119     #define SETPIXEL8(d, x, y, w, v) *(((uint8*)d) + ((y) * (w) + (x))) = v
120     #define SETPIXEL16(d, x, y, w, v) *(((uint16*)d) + ((y) * (w) + (x))) = v
121     #define SETPIXEL32(d, x, y, w, v) *(((uint32*)d) + ((y) * (w) + (x))) = v
122    
123 jsorg71 791 /******************************************************************************/
124 jsorg71 780 void CleanString(QString * Item)
125 jsorg71 748 {
126     int i;
127    
128     i = Item->length() - 1;
129     while (i >= 0)
130     {
131     if (Item->at(i) == 10 || Item->at(i) == 13)
132     Item->remove(i, 1);
133     i--;
134     }
135     }
136    
137 jsorg71 791 /******************************************************************************/
138 jsorg71 780 QMyDialog::QMyDialog(QWidget * parent) : QDialog(parent, "Settings", true)
139 jsorg71 748 {
140     int i, j;
141 jsorg71 780 char * home;
142 jsorg71 748 char Text[256];
143     QString Line;
144     QString ItemName;
145     QString ItemValue;
146    
147     // resize dialog
148     resize(230, 270);
149     // main list box
150     ListBox = new QListBox(this);
151     ListBox->move(10, 10);
152     ListBox->resize(200, 100);
153     connect(ListBox, SIGNAL(selectionChanged()), this, SLOT(ListBoxChanged()));
154     connect(ListBox, SIGNAL(selected(int)), this, SLOT(ListBoxSelected(int)));
155     // server
156     Label1 = new QLabel(this);
157     Label1->setText("Server Desc");
158     Label1->move(10, 120);
159     Label1->resize(100, 20);
160     ServerNameEdit = new QLineEdit(this);
161     ServerNameEdit->move(75, 120);
162     ServerNameEdit->resize(100, 20);
163     // username
164     Label2 = new QLabel(this);
165     Label2->setText("User Name");
166     Label2->move(10, 150);
167     Label2->resize(100, 20);
168     UserNameEdit = new QLineEdit(this);
169     UserNameEdit->move(75, 150);
170     UserNameEdit->resize(100, 20);
171     // ip
172     Label3 = new QLabel(this);
173     Label3->setText("Server IP");
174     Label3->move(10, 180);
175     Label3->resize(100, 20);
176     IPEdit = new QLineEdit(this);
177     IPEdit->move(75, 180);
178     IPEdit->resize(100, 20);
179     // width and height
180     WidthHeightBox = new QComboBox(this);
181     WidthHeightBox->move(10, 210);
182     WidthHeightBox->resize(100, 20);
183     WidthHeightBox->insertItem("240x320");
184     WidthHeightBox->insertItem("640x480");
185     WidthHeightBox->insertItem("800x600");
186     connect(WidthHeightBox, SIGNAL(activated(int)), this, SLOT(ComboChanged(int)));
187     WidthHeightBox->setCurrentItem(1);
188     WidthEdit = new QLineEdit(this);
189     WidthEdit->move(110, 210);
190     WidthEdit->resize(30, 20);
191     WidthEdit->setText("800");
192     HeightEdit = new QLineEdit(this);
193     HeightEdit->move(140, 210);
194     HeightEdit->resize(30, 20);
195     HeightEdit->setText("600");
196     // add to list button
197     AddButton = new QPushButton(this);
198     AddButton->move(180, 120);
199     AddButton->resize(50, 20);
200     AddButton->setText("Add");
201     connect(AddButton, SIGNAL(clicked()), this, SLOT(AddClicked()));
202     // change list item button
203     EditButton = new QPushButton(this);
204     EditButton->move(180, 140);
205     EditButton->resize(50, 20);
206     EditButton->setText("Edit");
207     connect(EditButton, SIGNAL(clicked()), this, SLOT(EditClicked()));
208     // save to file button
209     SaveButton = new QPushButton(this);
210     SaveButton->move(180, 160);
211     SaveButton->resize(50, 20);
212     SaveButton->setText("Save");
213     connect(SaveButton, SIGNAL(clicked()), this, SLOT(SaveClicked()));
214     // remove an item button
215     RemoveButton = new QPushButton(this);
216     RemoveButton->move(180, 180);
217     RemoveButton->resize(50, 20);
218     RemoveButton->setText("Remove");
219     connect(RemoveButton, SIGNAL(clicked()), this, SLOT(RemoveClicked()));
220     // full screen check box
221     FullScreenCheckBox = new QCheckBox(this, "Full Screen");
222     FullScreenCheckBox->setText("Full Screen");
223     FullScreenCheckBox->move(10, 230);
224     // ok button
225     OKButton = new QPushButton(this);
226     OKButton->setText("OK");
227     OKButton->move(100, 240);
228     OKButton->resize(50, 20);
229     connect(OKButton, SIGNAL(clicked()), this, SLOT(OKClicked()));
230     // cancel button
231     CancelButton = new QPushButton(this);
232     CancelButton->setText("Cancel");
233     CancelButton->move(160, 240);
234     CancelButton->resize(50, 20);
235     connect(CancelButton, SIGNAL(clicked()), this, SLOT(CancelClicked()));
236    
237     for (i = 0; i < 10; i++)
238     {
239     ConnectionList[i] = new QMyConnectionItem;
240     ConnectionList[i]->ServerName = "";
241     ConnectionList[i]->UserName = "";
242     ConnectionList[i]->ServerIP = "";
243     ConnectionList[i]->Width = 0;
244     ConnectionList[i]->Height = 0;
245     ConnectionList[i]->FullScreen = 0;
246     }
247     home = getenv("HOME");
248     if (home != NULL)
249     {
250     sprintf(Text, "%s/rdesktop.ini", home);
251 jsorg71 791 QFile * File = new QFile(Text);
252 jsorg71 748 if (File->open(IO_ReadOnly))
253     {
254     i = -1;
255     while (!File->atEnd())
256     {
257     File->readLine(Line, 255);
258     j = Line.find("=");
259     if (j > 0)
260     {
261     ItemName = Line.mid(0, j);
262     CleanString(&ItemName);
263     ItemValue = Line.mid(j + 1);
264     CleanString(&ItemValue);
265     if (ItemName == "Server")
266     {
267     i++;
268     ConnectionList[i]->ServerName = ItemValue;
269     ListBox->insertItem(ItemValue);
270     }
271     else if (ItemName == "UserName")
272     ConnectionList[i]->UserName = ItemValue;
273     else if (ItemName == "Width")
274     ConnectionList[i]->Width = ItemValue.toInt();
275     else if (ItemName == "Height")
276     ConnectionList[i]->Height = ItemValue.toInt();
277     else if (ItemName == "IP")
278     ConnectionList[i]->ServerIP = ItemValue;
279     else if (ItemName == "FullScreen")
280     ConnectionList[i]->FullScreen = (ItemValue != "0");
281     }
282     }
283     }
284     delete File;
285     }
286     }
287    
288 jsorg71 791 /******************************************************************************/
289 jsorg71 748 QMyDialog::~QMyDialog()
290     {
291 jsorg71 791 QMyConnectionItem * Item;
292 jsorg71 748 int i;
293    
294     for (i = 0; i < 10; i++)
295     {
296     Item = ConnectionList[i];
297     delete Item;
298     }
299     }
300    
301 jsorg71 791 /******************************************************************************/
302 jsorg71 748 void QMyDialog::ComboChanged(int index)
303     {
304     if (index == 0)
305     {
306     WidthEdit->setText("240");
307     HeightEdit->setText("320");
308     }
309     if (index == 1)
310     {
311     WidthEdit->setText("640");
312     HeightEdit->setText("480");
313     }
314     else if (index == 2)
315     {
316     WidthEdit->setText("800");
317     HeightEdit->setText("600");
318     }
319     }
320    
321 jsorg71 791 /******************************************************************************/
322 jsorg71 748 void QMyDialog::OKClicked()
323     {
324     ServerName = ServerNameEdit->text();
325     UserName = UserNameEdit->text();
326     Width = WidthEdit->text().toInt();
327     Height = HeightEdit->text().toInt();
328     ServerIP = IPEdit->text();
329     FullScreen = FullScreenCheckBox->isChecked();
330     done(1);
331     }
332    
333 jsorg71 791 /******************************************************************************/
334 jsorg71 748 void QMyDialog::CancelClicked()
335     {
336     done(0);
337     }
338    
339 jsorg71 791 /******************************************************************************/
340 jsorg71 748 void QMyDialog::AddClicked()
341     {
342     int i;
343 jsorg71 780 QMyConnectionItem * Item;
344 jsorg71 748
345     i = ListBox->count();
346     if (i < 10)
347     {
348     ListBox->insertItem(ServerNameEdit->text());
349     Item = ConnectionList[i];
350     Item->ServerName = ServerNameEdit->text();
351     Item->UserName = UserNameEdit->text();
352     Item->Width = WidthEdit->text().toInt();
353     Item->Height = HeightEdit->text().toInt();
354     Item->ServerIP = IPEdit->text();
355     Item->FullScreen = FullScreenCheckBox->isChecked();
356     }
357     }
358    
359 jsorg71 791 /******************************************************************************/
360 jsorg71 748 void QMyDialog::EditClicked()
361     {
362     int i;
363 jsorg71 780 QMyConnectionItem * Item;
364 jsorg71 748
365     i = ListBox->currentItem();
366     if (i >= 0)
367     {
368     Item = ConnectionList[i];
369     Item->ServerName = ServerNameEdit->text();
370     Item->UserName = UserNameEdit->text();
371     Item->Width = WidthEdit->text().toInt();
372     Item->Height = HeightEdit->text().toInt();
373     Item->ServerIP = IPEdit->text();
374     Item->FullScreen = FullScreenCheckBox->isChecked();
375     ListBox->changeItem(ServerNameEdit->text(), i);
376     }
377     }
378    
379 jsorg71 791 /******************************************************************************/
380 jsorg71 748 void WriteString(QFile* File, QString* Line)
381     {
382     File->writeBlock((const char*)(*Line), Line->length());
383     }
384    
385 jsorg71 791 /******************************************************************************/
386 jsorg71 748 void QMyDialog::SaveClicked()
387     {
388     int i, j;
389 jsorg71 780 QMyConnectionItem * Item;
390 jsorg71 748 QString Line;
391 jsorg71 780 char * home;
392 jsorg71 748 char Text[256];
393     QFile* File;
394    
395     home = getenv("HOME");
396     if (home != NULL)
397     {
398     sprintf(Text, "%s/rdesktop.ini", home);
399     File = new QFile(Text);
400     if (File->open(IO_Truncate | IO_ReadWrite))
401     {
402     i = ListBox->count();
403     for (j = 0; j < i; j++)
404     {
405     Item = ConnectionList[j];
406     Line = "Server=";
407     Line += Item->ServerName;
408     Line += (char)10;
409     WriteString(File, &Line);
410     Line = "UserName=";
411     Line += Item->UserName;
412     Line += (char)10;
413     WriteString(File, &Line);
414     Line = "Width=";
415     sprintf(Text, "%d", Item->Width);
416     Line += Text;
417     Line += (char)10;
418     WriteString(File, &Line);
419     Line = "Height=";
420     sprintf(Text, "%d", Item->Height);
421     Line += Text;
422     Line += (char)10;
423     WriteString(File, &Line);
424     Line = "IP=";
425     Line += Item->ServerIP;
426     Line += (char)10;
427     WriteString(File, &Line);
428     Line = "FullScreen=";
429     if (Item->FullScreen)
430     Line += "1";
431     else
432     Line += "0";
433     Line += (char)10;
434     WriteString(File, &Line);
435     }
436     }
437     File->flush();
438     File->close();
439     delete File;
440     }
441     }
442    
443 jsorg71 791 /******************************************************************************/
444 jsorg71 748 void QMyDialog::RemoveClicked()
445     {
446     int i, j, c;
447 jsorg71 780 QMyConnectionItem * Item1;
448     QMyConnectionItem * Item2;
449 jsorg71 748
450     i = ListBox->currentItem();
451     if (i >= 0)
452     {
453     c = ListBox->count();
454     for (j = i; j < c - 1; j++)
455     {
456     Item1 = ConnectionList[i];
457     Item2 = ConnectionList[i + 1];
458     Item1->ServerName = Item2->ServerName;
459     Item1->UserName = Item2->UserName;
460     Item1->Width = Item2->Width;
461     Item1->Height = Item2->Height;
462     Item1->ServerIP = Item2->ServerIP;
463     Item1->FullScreen = Item2->FullScreen;
464     }
465     ListBox->removeItem(i);
466     }
467     }
468    
469 jsorg71 791 /******************************************************************************/
470 jsorg71 748 void QMyDialog::ListBoxChanged()
471     {
472     int i;
473 jsorg71 780 QMyConnectionItem * Item;
474 jsorg71 748 char Text[100];
475    
476     i = ListBox->currentItem();
477     if (i >= 0 && i < 10)
478     {
479     Item = ConnectionList[i];
480     ServerNameEdit->setText(Item->ServerName);
481     UserNameEdit->setText(Item->UserName);
482     sprintf(Text, "%d", Item->Width);
483     WidthEdit->setText(Text);
484     sprintf(Text, "%d", Item->Height);
485     HeightEdit->setText(Text);
486     IPEdit->setText(Item->ServerIP);
487     FullScreenCheckBox->setChecked(Item->FullScreen != 0);
488     }
489     }
490    
491 jsorg71 791 /******************************************************************************/
492 jsorg71 748 void QMyDialog::ListBoxSelected(int /*index*/)
493     {
494     }
495    
496 jsorg71 791 /******************************************************************************/
497 jsorg71 780 void GetScanCode(QKeyEvent * e, int * ScanCode, int * code)
498 jsorg71 748 {
499     int key;
500     int mod;
501     int ascii;
502    
503     key = e->key();
504     mod = e->state();
505     ascii = e->ascii();
506    
507     *ScanCode = 0;
508     *code = mod; // 8 shift, 16 control, 32 alt
509    
510     switch (key)
511     {
512     case 4096: // esc
513     case 4097: // tab
514     case 4099: // backspace
515     case 4100: // enter
516     case 4101: // enter
517     case 4103: // delete
518     ascii = 0;
519     }
520    
521     if (ascii == 0)
522     {
523     switch (key)
524     {
525     case 4096: *ScanCode = 0x01; break; // esc
526     case 4097: *ScanCode = 0x0f; break; // tab
527     case 4099: *ScanCode = 0x0e; break; // backspace
528     case 4100: *ScanCode = 0x1c; break; // enter
529     case 4101: *ScanCode = 0x1c; break; // enter
530     case 4112: *ScanCode = 0xc7; break; // home
531     case 4113: *ScanCode = 0xcf; break; // end
532     case 4102: *ScanCode = 0xd2; break; // insert
533     case 4103: *ScanCode = 0xd3; break; // delete
534     case 4118: *ScanCode = 0xc9; break; // page up
535     case 4119: *ScanCode = 0xd1; break; // page down
536     case 4117: *ScanCode = 0xd0; break; // down arrow
537     case 4115: *ScanCode = 0xc8; break; // up arrow
538     case 4114: *ScanCode = 0xcb; break; // left arrow
539     case 4116: *ScanCode = 0xcd; break; // right arrow
540     case 4128: *ScanCode = 0x2a; break; // shift
541     case 4131: *ScanCode = 0x38; break; // alt
542     case 4129: *ScanCode = 0x1d; break; // ctrl
543     }
544     if (*ScanCode != 0)
545     return;
546     }
547    
548     switch (ascii)
549     {
550     // first row
551     case 'q': *ScanCode = 0x10; break;
552     case 'Q': *ScanCode = 0x10; *code |= 8; break;
553     case '1': *ScanCode = 0x02; break;
554     case 'w': *ScanCode = 0x11; break;
555     case 'W': *ScanCode = 0x11; *code |= 8; break;
556     case '2': *ScanCode = 0x03; break;
557     case 'e': *ScanCode = 0x12; break;
558     case 'E': *ScanCode = 0x12; *code |= 8; break;
559     case '3': *ScanCode = 0x04; break;
560     case 'r': *ScanCode = 0x13; break;
561     case 'R': *ScanCode = 0x13; *code |= 8; break;
562     case '4': *ScanCode = 0x05; break;
563     case 't': *ScanCode = 0x14; break;
564     case 'T': *ScanCode = 0x14; *code |= 8; break;
565     case '5': *ScanCode = 0x06; break;
566     case 'y': *ScanCode = 0x15; break;
567     case 'Y': *ScanCode = 0x15; *code |= 8; break;
568     case '6': *ScanCode = 0x07; break;
569     case 'u': *ScanCode = 0x16; break;
570     case 'U': *ScanCode = 0x16; *code |= 8; break;
571     case '7': *ScanCode = 0x08; break;
572     case 'i': *ScanCode = 0x17; break;
573     case 'I': *ScanCode = 0x17; *code |= 8; break;
574     case '8': *ScanCode = 0x09; break;
575     case 'o': *ScanCode = 0x18; break;
576     case 'O': *ScanCode = 0x18; *code |= 8; break;
577     case '9': *ScanCode = 0x0a; break;
578     case 'p': *ScanCode = 0x19; break;
579     case 'P': *ScanCode = 0x19; *code |= 8; break;
580     case '0': *ScanCode = 0x0b; break;
581     // second row
582     case 'a': *ScanCode = 0x1e; break;
583     case 'A': *ScanCode = 0x1e; *code |= 8; break;
584     case '!': *ScanCode = 0x02; *code |= 8; break;
585     case 's': *ScanCode = 0x1f; break;
586     case 'S': *ScanCode = 0x1f; *code |= 8; break;
587     case '@': *ScanCode = 0x03; *code |= 8; break;
588     case 'd': *ScanCode = 0x20; break;
589     case 'D': *ScanCode = 0x20; *code |= 8; break;
590     case '#': *ScanCode = 0x04; *code |= 8; break;
591     case 'f': *ScanCode = 0x21; break;
592     case 'F': *ScanCode = 0x21; *code |= 8; break;
593     case '$': *ScanCode = 0x05; *code |= 8; break;
594     case 'g': *ScanCode = 0x22; break;
595     case 'G': *ScanCode = 0x22; *code |= 8; break;
596     case '%': *ScanCode = 0x06; *code |= 8; break;
597     case 'h': *ScanCode = 0x23; break;
598     case 'H': *ScanCode = 0x23; *code |= 8; break;
599     case '_': *ScanCode = 0x0c; *code |= 8; break;
600     case 'j': *ScanCode = 0x24; break;
601     case 'J': *ScanCode = 0x24; *code |= 8; break;
602     case '&': *ScanCode = 0x08; *code |= 8; break;
603     case 'k': *ScanCode = 0x25; break;
604     case 'K': *ScanCode = 0x25; *code |= 8; break;
605     case '*': *ScanCode = 0x09; *code |= 8; break;
606     case 'l': *ScanCode = 0x26; break;
607     case 'L': *ScanCode = 0x26; *code |= 8; break;
608     case '(': *ScanCode = 0x0a; *code |= 8; break;
609     // case 8: *ScanCode = 0x0e; break; // backspace
610     // third row
611     case 'z': *ScanCode = 0x2c; break;
612     case 'Z': *ScanCode = 0x2c; *code |= 8; break;
613     case 'x': *ScanCode = 0x2d; break;
614     case 'X': *ScanCode = 0x2d; *code |= 8; break;
615     case 'c': *ScanCode = 0x2e; break;
616     case 'C': *ScanCode = 0x2e; *code |= 8; break;
617     case 'v': *ScanCode = 0x2f; break;
618     case 'V': *ScanCode = 0x2f; *code |= 8; break;
619     case 'b': *ScanCode = 0x30; break;
620     case 'B': *ScanCode = 0x30; *code |= 8; break;
621     case '-': *ScanCode = 0x0c; break;
622     case 'n': *ScanCode = 0x31; break;
623     case 'N': *ScanCode = 0x31; *code |= 8; break;
624     case '+': *ScanCode = 0x0d; *code |= 8; break;
625     case 'm': *ScanCode = 0x32; break;
626     case 'M': *ScanCode = 0x32; *code |= 8; break;
627     case '=': *ScanCode = 0x0d; break;
628     case ',': *ScanCode = 0x33; break;
629     case ';': *ScanCode = 0x27; break;
630     case ')': *ScanCode = 0x0b; *code |= 8; break;
631     // fourth row
632     // case 9: *ScanCode = 0x0f; break; // tab
633     case '/': *ScanCode = 0x35; break;
634     case '?': *ScanCode = 0x35; *code |= 8; break;
635     case ' ': *ScanCode = 0x39; break;
636     case '\'': *ScanCode = 0x28; break;
637     case '"': *ScanCode = 0x28; *code |= 8; break;
638     case '~': *ScanCode = 0x29; *code |= 8; break;
639     case '.': *ScanCode = 0x34; break;
640     case ':': *ScanCode = 0x27; *code |= 8; break;
641     case '<': *ScanCode = 0x33; *code |= 8; break;
642     // case 13: *ScanCode = 0x1c; break; // enter
643     case '>': *ScanCode = 0x34; *code |= 8; break;
644     // others
645     // case 27: *ScanCode = 0x01; break; // esc
646     case '`': *ScanCode = 0x29; break;
647     case '^': *ScanCode = 0x07; *code |= 8; break;
648     case '[': *ScanCode = 0x1a; break;
649     case '{': *ScanCode = 0x1a; *code |= 8; break;
650     case ']': *ScanCode = 0x1b; break;
651     case '}': *ScanCode = 0x1b; *code |= 8; break;
652     case '\\': *ScanCode = 0x2b; break;
653     case '|': *ScanCode = 0x2b; *code |= 8; break;
654     // ctrl keys
655     case 1: *ScanCode = 0x1e; *code |= 16; break; // a
656     case 2: *ScanCode = 0x30; *code |= 16; break; // b
657     }
658    
659     if (*ScanCode == 0 && key < 3000)
660     printf("unknown key %d mod %d ascii %d\n", key, mod, ascii);
661    
662     }
663    
664 jsorg71 791 /******************************************************************************/
665 jsorg71 748 QMyScrollView::QMyScrollView() : QScrollView()
666     {
667     }
668    
669 jsorg71 791 /******************************************************************************/
670 jsorg71 748 QMyScrollView::~QMyScrollView()
671     {
672     }
673    
674 jsorg71 791 /******************************************************************************/
675 jsorg71 748 void QMyScrollView::keyPressEvent(QKeyEvent* e)
676     {
677     int ScanCode, code;
678     GetScanCode(e, &ScanCode, &code);
679     if (ScanCode != 0)
680     {
681     if (code & 8) // send shift
682     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x2a, 0);
683     if (code & 16) // send control
684     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x1d, 0);
685     if (code & 32) // send alt
686     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYPRESS, 0x38, 0);
687     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYPRESS, ScanCode, 0);
688     e->accept();
689     }
690     }
691    
692 jsorg71 791 /******************************************************************************/
693 jsorg71 748 void QMyScrollView::keyReleaseEvent(QKeyEvent* e)
694     {
695     int ScanCode, code;
696     GetScanCode(e, &ScanCode, &code);
697     if (ScanCode != 0)
698     {
699     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, ScanCode, 0);
700     if (code & 8) // send shift
701     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x2a, 0);
702     if (code & 16) // send control
703     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x1d, 0);
704     if (code & 32) // send alt
705     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x38, 0);
706     e->accept();
707     }
708     }
709    
710 jsorg71 791 /******************************************************************************/
711 jsorg71 748 void QMyScrollView::showEvent(QShowEvent* e)
712     {
713     QScrollView::showEvent(e);
714     }
715    
716 jsorg71 791 /******************************************************************************/
717 jsorg71 748 void QMyScrollView::show()
718     {
719     QScrollView::show();
720     }
721    
722 jsorg71 791 /******************************************************************************/
723 jsorg71 748 void QMyScrollView::polish()
724     {
725     QScrollView::polish();
726     }
727    
728 jsorg71 791 /******************************************************************************/
729 jsorg71 780 void QMyScrollView::timerEvent(QTimerEvent * e)
730 jsorg71 748 {
731     QScrollView::timerEvent(e);
732     killTimer(timer_id);
733 jsorg71 780 QMyDialog * d;
734     QWidget * Desktop;
735 jsorg71 748 int dw;
736     int dh;
737    
738     d = new QMyDialog(this);
739     if (d->exec() == 1) // ok clicked
740     {
741     g_width = d->Width;
742     g_height = d->Height;
743     g_fullscreen = d->FullScreen;
744     sprintf(g_server, "%s", (const char*)d->ServerIP);
745     sprintf(g_username, "%s", (const char*)d->UserName);
746     if (!rdp_connect(g_server, g_flags, g_domain, g_password, g_shell,
747     g_directory))
748     {
749     delete d;
750     g_SV->close();
751     return;
752     }
753 jsorg71 780 g_BS = (uint8*)xmalloc(g_width * g_height * 4);
754     memset(g_BS, 0, g_width * g_height * 4);
755 jsorg71 748 g_clipx = 0;
756     g_clipy = 0;
757     g_clipcx = g_width;
758     g_clipcy = g_height;
759     g_CM = (QColorMap*)xmalloc(sizeof(struct QColorMap));
760     memset(g_CM, 0, sizeof(struct QColorMap));
761     g_CM->NumColors = 256;
762     g_MW = new QMyMainWindow();
763     g_MW->resize(g_width, g_height);
764     g_MW->show();
765     g_SV->addChild(g_MW);
766     g_MW->setMouseTracking(true);
767     g_SocketNotifier = new QSocketNotifier(g_global_sock,
768     QSocketNotifier::Read,
769     g_MW);
770     g_MW->connect(g_SocketNotifier, SIGNAL(activated(int)), g_MW,
771     SLOT(dataReceived()));
772     if (g_fullscreen)
773     {
774     Desktop = g_App->desktop();
775     dw = Desktop->width();
776     dh = Desktop->height();
777     if (dw == g_width && dh == g_height)
778     g_MW->resize(g_width - 4, g_height - 4);
779     g_SV->showFullScreen();
780     }
781     delete d;
782     }
783     else // cancel clicked
784     {
785     delete d;
786     g_SV->close();
787     }
788     }
789    
790 jsorg71 791 /******************************************************************************/
791 jsorg71 748 QMyMainWindow::QMyMainWindow() : QWidget(g_SV->viewport())
792     {
793     PopupMenu = new QPopupMenu(this);
794     PopupMenu->insertItem("Right click", 1, 0);
795     PopupMenu->insertItem("Toggle fullscreen", 2, 1);
796     PopupMenu->insertItem("Reset keyboard", 3, 2);
797     PopupMenu->insertItem("Double click", 4, 3);
798     connect(PopupMenu, SIGNAL(activated(int)), this, SLOT(MemuClicked(int)));
799     }
800    
801 jsorg71 791 /******************************************************************************/
802 jsorg71 748 QMyMainWindow::~QMyMainWindow()
803     {
804     delete PopupMenu;
805     }
806    
807 jsorg71 791 /******************************************************************************/
808 jsorg71 780 void QMyMainWindow::timerEvent(QTimerEvent * e)
809 jsorg71 748 {
810     QWidget::timerEvent(e);
811     if (e->timerId() == timer_id)
812     {
813     // send mouse up
814     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON1, mx, my);
815     // if in fullscreen, take it out or the menu won't work
816     if (g_fullscreen)
817     {
818     g_fullscreen = 0;
819     g_SV->showNormal();
820     g_SV->showMaximized();
821     }
822     else
823     PopupMenu->popup(mapToGlobal(QPoint(mx, my)));
824     }
825     killTimer(timer_id);
826     }
827    
828 jsorg71 791 /******************************************************************************/
829 jsorg71 748 void QMyMainWindow::MemuClicked(int MenuID)
830     {
831 jsorg71 780 QWidget * Desktop;
832 jsorg71 748 int dw;
833     int dh;
834    
835     if (MenuID == 1) // right click
836     {
837     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON2, mx, my);
838     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON2, mx, my);
839     }
840     else if (MenuID == 2) // toggle full screen
841     {
842     g_fullscreen = ~g_fullscreen;
843     if (g_fullscreen)
844     {
845     Desktop = g_App->desktop();
846     dw = Desktop->width();
847     dh = Desktop->height();
848     if (dw == g_width && dh == g_height)
849     g_MW->resize(g_width - 4, g_height - 4);
850     g_SV->showFullScreen();
851     }
852     else
853     {
854     g_SV->showNormal();
855     g_SV->showMaximized();
856     g_MW->resize(g_width, g_height);
857     }
858     }
859     else if (MenuID == 3) // reset keyboard
860     {
861     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x2a, 0); // shift
862     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x1d, 0); // control
863     rdp_send_input(0, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x38, 0); // alt
864     }
865     else if (MenuID == 4) // double click
866     {
867     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON1, mx, my);
868     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON1, mx, my);
869     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON1, mx, my);
870     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON1, mx, my);
871     }
872     }
873    
874 jsorg71 791 /******************************************************************************/
875 jsorg71 748 void QMyMainWindow::mouseMoveEvent(QMouseEvent* e)
876     {
877     int x;
878     int y;
879    
880     x = e->x();
881     y = e->y();
882    
883     if (timer_id)
884     {
885     x = x - mx;
886     y = y - my;
887     if (x < -10 || x > 10 || y < -10 || y > 10)
888     {
889     killTimer(timer_id);
890     timer_id = 0;
891     }
892     }
893     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE, e->x(), e->y());
894     }
895    
896 jsorg71 791 /******************************************************************************/
897 jsorg71 748 void QMyMainWindow::mousePressEvent(QMouseEvent* e)
898     {
899     timer_id = startTimer(1000);
900     mx = e->x();
901     my = e->y();
902     if (e->button() == LeftButton)
903     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON1, e->x(), e->y());
904     else if (e->button() == RightButton)
905     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON2, e->x(), e->y());
906     else if (e->button() == MidButton)
907     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON3, e->x(), e->y());
908     }
909    
910 jsorg71 791 /******************************************************************************/
911 jsorg71 748 void QMyMainWindow::mouseReleaseEvent(QMouseEvent* e)
912     {
913     killTimer(timer_id);
914     timer_id = 0;
915     if (e->button() == LeftButton)
916     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON1, e->x(), e->y());
917     else if (e->button() == RightButton)
918     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON2, e->x(), e->y());
919     else if (e->button() == MidButton)
920     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON3, e->x(), e->y());
921     }
922    
923 jsorg71 791 /******************************************************************************/
924 jsorg71 748 void QMyMainWindow::wheelEvent(QWheelEvent* e)
925     {
926     if (e->delta() > 0)
927     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON4, e->x(), e->y());
928     else if (e->delta() < 0)
929     rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_BUTTON5, e->x(), e->y());
930     }
931    
932 jsorg71 780 #define NOT(x) (~x)
933 jsorg71 748
934 jsorg71 791 /******************************************************************************/
935 jsorg71 780 int rop(int rop, int src, int dst)
936 jsorg71 748 {
937     switch (rop)
938     {
939     case 0x0: return 0;
940     case 0x1: return NOT (src | dst);
941     case 0x2: return NOT (src) & dst;
942     case 0x3: return NOT (src);
943     case 0x4: return src & NOT (dst);
944     case 0x5: return NOT (dst);
945     case 0x6: return src ^ dst;
946     case 0x7: return NOT (src & dst);
947     case 0x8: return src & dst;
948     case 0x9: return NOT (src) ^ dst;
949     case 0xa: return dst;
950     case 0xb: return NOT (src) | dst;
951     case 0xc: return src;
952     case 0xd: return src | NOT (dst);
953     case 0xe: return src | dst;
954     case 0xf: return NOT (0);
955     }
956     return dst;
957     }
958    
959     /*****************************************************************************/
960 jsorg71 780 int get_pixel(int x, int y)
961 jsorg71 748 {
962     if (x >= 0 && x < g_width && y >= 0 && y < g_height)
963 jsorg71 780 {
964     if (g_server_bpp == 8)
965     return g_BS[y * g_width + x];
966     else if (g_server_bpp == 16)
967     return *(((uint16*)g_BS) + (y * g_width + x));
968 jsorg71 791 else if (g_server_bpp == 24)
969     return *(((uint32*)g_BS) + (y * g_width + x));
970 jsorg71 780 else
971     return 0;
972     }
973 jsorg71 748 else
974     return 0;
975     }
976    
977 jsorg71 791 /******************************************************************************/
978 jsorg71 780 void set_pixel(int x, int y, int pixel, int op = 0xc)
979 jsorg71 748 {
980 jsorg71 780 uint32 p;
981    
982 jsorg71 748 if (x >= g_clipx && x < (g_clipx + g_clipcx) &&
983     y >= g_clipy && y < (g_clipy + g_clipcy))
984 jsorg71 780 {
985 jsorg71 748 if (x >= 0 && x < g_width && y >= 0 && y < g_height)
986 jsorg71 780 {
987 jsorg71 748 if (op == 0xc)
988 jsorg71 780 {
989     if (g_server_bpp == 8)
990     {
991     SETPIXEL8(g_BS, x, y, g_width, pixel);
992     }
993     else if (g_server_bpp == 16)
994     {
995     SETPIXEL16(g_BS, x, y, g_width, pixel);
996     }
997 jsorg71 791 else if (g_server_bpp == 24)
998     {
999     SETPIXEL32(g_BS, x, y, g_width, pixel);
1000     }
1001 jsorg71 780 }
1002 jsorg71 748 else
1003 jsorg71 780 {
1004     if (g_server_bpp == 8)
1005     {
1006     p = GETPIXEL8(g_BS, x, y, g_width);
1007     p = rop(op, pixel, p);
1008     SETPIXEL8(g_BS, x, y, g_width, p);
1009     }
1010     else if (g_server_bpp == 16)
1011     {
1012     p = GETPIXEL16(g_BS, x, y, g_width);
1013     p = rop(op, pixel, p);
1014     SETPIXEL16(g_BS, x, y, g_width, p);
1015     }
1016 jsorg71 791 else if (g_server_bpp == 24)
1017     {
1018     p = GETPIXEL32(g_BS, x, y, g_width);
1019     p = rop(op, pixel, p);
1020     SETPIXEL32(g_BS, x, y, g_width, p);
1021     }
1022 jsorg71 780 }
1023     }
1024     }
1025 jsorg71 748 }
1026    
1027 jsorg71 791 /******************************************************************************/
1028 jsorg71 748 // adjust coordinates for cliping rect
1029 jsorg71 780 bool WarpCoords(int * x, int * y, int * cx, int * cy, int * srcx, int * srcy)
1030 jsorg71 748 {
1031     int dx, dy;
1032     QRect InRect(*x, *y, *cx, *cy);
1033     QRect OutRect;
1034     QRect CRect(g_clipx, g_clipy, g_clipcx, g_clipcy);
1035     OutRect = InRect.intersect(CRect);
1036     if (OutRect.isEmpty())
1037     return false;
1038     dx = OutRect.x() - InRect.x();
1039     dy = OutRect.y() - InRect.y();
1040     *x = OutRect.x();
1041     *y = OutRect.y();
1042     *cx = OutRect.width();
1043     *cy = OutRect.height();
1044     if (srcx != NULL)
1045     *srcx = *srcx + dx;
1046     if (srcy != NULL)
1047     *srcy = *srcy + dy;
1048     return true;
1049     }
1050    
1051 jsorg71 791 /******************************************************************************/
1052 jsorg71 780 uint32 color16to32(uint32 colour)
1053 jsorg71 748 {
1054 jsorg71 780 uint32 r, g, b;
1055     r = ((colour >> 8) & 0xf8) | ((colour >> 13) & 0x7);
1056     g = ((colour >> 3) & 0xfc) | ((colour >> 9) & 0x3);
1057     b = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7);
1058     return ((r << 16) | (g << 8) | b);
1059     }
1060    
1061 jsorg71 791 /******************************************************************************/
1062     uint32 color24to32(uint32 colour)
1063     {
1064     uint32 r, g, b;
1065     r = (colour >> 0) & 0xff;
1066     g = (colour >> 8) & 0xff;
1067     b = (colour >> 16) & 0xff;
1068     return ((r << 16) | (g << 8) | b);
1069     }
1070    
1071     /******************************************************************************/
1072 jsorg71 780 void QMyMainWindow::paintEvent(QPaintEvent * pe)
1073     {
1074     QImage * Image;
1075     QPainter * Painter;
1076 jsorg71 748 QRect Rect;
1077     int i, j, w, h, l, t;
1078 jsorg71 791 int pixel;
1079 jsorg71 780 uint8 * data;
1080 jsorg71 748
1081 jsorg71 780 Image = 0;
1082     data = 0;
1083 jsorg71 748 if (!testWFlags(WRepaintNoErase))
1084     setWFlags(WRepaintNoErase);
1085 jsorg71 780 if (g_CM != NULL || g_server_bpp > 8)
1086 jsorg71 748 {
1087     Rect = pe->rect();
1088     l = Rect.left();
1089     t = Rect.top();
1090     w = Rect.width();
1091     h = Rect.height();
1092 jsorg71 780 if (w > 0 && h > 0)
1093 jsorg71 748 {
1094 jsorg71 780 if (g_server_bpp == 8 && g_CM->NumColors > 0)
1095     {
1096     w = (w + 3) & ~3;
1097     data = (uint8*)xmalloc(w * h);
1098     for (i = 0; i < h; i++)
1099     for (j = 0; j < w; j++)
1100     data[i * w + j] = get_pixel(l + j, t + i);
1101     Image = new QImage(data, w, h, 8,(QRgb*)g_CM->RGBColors,
1102     g_CM->NumColors, QImage::IgnoreEndian);
1103     }
1104     else if (g_server_bpp == 16)
1105     {
1106     w = (w + 3) & ~3;
1107     data = (uint8*)xmalloc(w * h * 4);
1108     for (i = 0; i < h; i++)
1109     for (j = 0; j < w; j++)
1110 jsorg71 791 {
1111     pixel = GETPIXEL16(g_BS, l + j, t + i, g_width);
1112     pixel = color16to32(pixel);
1113     SETPIXEL32(data, j, i, w, pixel);
1114     }
1115 jsorg71 780 Image = new QImage(data, w, h, 32, NULL,
1116     0, QImage::IgnoreEndian);
1117     }
1118 jsorg71 791 else if (g_server_bpp == 24)
1119     {
1120     w = (w + 3) & ~3;
1121     data = (uint8*)xmalloc(w * h * 4);
1122     for (i = 0; i < h; i++)
1123     for (j = 0; j < w; j++)
1124     {
1125     pixel = GETPIXEL32(g_BS, l + j, t + i, g_width);
1126     pixel = color24to32(pixel);
1127     SETPIXEL32(data, j, i, w, pixel);
1128     }
1129     Image = new QImage(data, w, h, 32, NULL,
1130     0, QImage::IgnoreEndian);
1131     }
1132 jsorg71 780 if (Image != 0)
1133     {
1134     Painter = new QPainter(this);
1135     Painter->drawImage(l, t, *Image, 0, 0, w, h);
1136     delete Painter;
1137     delete Image;
1138     }
1139 jsorg71 748 xfree(data);
1140     }
1141     }
1142     }
1143    
1144 jsorg71 791 /******************************************************************************/
1145 jsorg71 780 void QMyMainWindow::closeEvent(QCloseEvent * e)
1146 jsorg71 748 {
1147     e->accept();
1148     }
1149    
1150 jsorg71 791 /******************************************************************************/
1151 jsorg71 748 void QMyMainWindow::dataReceived()
1152     {
1153     if (!rdp_loop(&g_deactivated, &g_ext_disc_reason))
1154     g_SV->close();
1155 jsorg71 780 #ifdef WITH_RDPSND
1156     if (g_dsp_busy)
1157     {
1158     if (g_SoundNotifier == 0)
1159     {
1160     g_SoundNotifier = new QSocketNotifier(g_dsp_fd, QSocketNotifier::Write,
1161     g_MW);
1162     g_MW->connect(g_SoundNotifier, SIGNAL(activated(int)), g_MW,
1163     SLOT(soundSend()));
1164     }
1165     else
1166     {
1167     if (!g_SoundNotifier->isEnabled())
1168     g_SoundNotifier->setEnabled(true);
1169     }
1170     }
1171     #endif
1172 jsorg71 748 }
1173    
1174 jsorg71 791 /******************************************************************************/
1175 jsorg71 780 void QMyMainWindow::soundSend()
1176     {
1177     g_SoundNotifier->setEnabled(false);
1178     #ifdef WITH_RDPSND
1179     wave_out_play();
1180     if (g_dsp_busy)
1181     g_SoundNotifier->setEnabled(true);
1182     #endif
1183     }
1184    
1185 jsorg71 791 /******************************************************************************/
1186 jsorg71 748 void redraw(int x, int y, int cx, int cy)
1187     {
1188     if (WarpCoords(&x, &y, &cx, &cy, NULL, NULL))
1189     {
1190     g_MW->update(x, y, cx, cy);
1191     }
1192     }
1193    
1194 jsorg71 791 /******************************************************************************/
1195 jsorg71 748 /* Returns 0 after user quit, 1 otherwise */
1196     int ui_select(int rdp_socket)
1197     {
1198 jsorg71 780 if (g_global_sock == 0)
1199     g_global_sock = rdp_socket;
1200 jsorg71 748 return 1;
1201     }
1202    
1203 jsorg71 791 /******************************************************************************/
1204 jsorg71 748 void ui_move_pointer(int /*x*/, int /*y*/)
1205     {
1206     }
1207    
1208 jsorg71 791 /******************************************************************************/
1209 jsorg71 748 void ui_set_null_cursor(void)
1210     {
1211     }
1212    
1213 jsorg71 791 /******************************************************************************/
1214 jsorg71 748 HBITMAP ui_create_bitmap(int width, int height, uint8 * data)
1215     {
1216 jsorg71 780 struct bitmap * the_bitmap;
1217     uint8 * bitmap_data;
1218 jsorg71 748 int i, j;
1219 jsorg71 791 int r, g, b, pixel;
1220 jsorg71 748
1221 jsorg71 791 bitmap_data = (uint8*)xmalloc(width * height * 4);
1222 jsorg71 748 the_bitmap = (struct bitmap*)xmalloc(sizeof(struct bitmap));
1223     the_bitmap->w = width;
1224     the_bitmap->h = height;
1225     the_bitmap->data = bitmap_data;
1226 jsorg71 780 if (g_server_bpp == 8)
1227     {
1228     for (i = 0; i < height; i++)
1229     for (j = 0; j < width; j++)
1230     bitmap_data[i * width + j] = data[i * width + j];
1231     }
1232     else if (g_server_bpp == 16)
1233     {
1234     for (i = 0; i < height; i++)
1235     for (j = 0; j < width; j++)
1236 jsorg71 791 *(((uint16*)bitmap_data) + (i * width + j)) =
1237     *(((uint16*)data) + (i * width + j));
1238 jsorg71 780 }
1239 jsorg71 791 else if (g_server_bpp == 24)
1240     {
1241     for (i = 0; i < height; i++)
1242     for (j = 0; j < width; j++)
1243     {
1244     r = data[(i * width + j) * 3 + 0];
1245     g = data[(i * width + j) * 3 + 1];
1246     b = data[(i * width + j) * 3 + 2];
1247     pixel = (r << 16) | (g << 8) | b;
1248     SETPIXEL32(bitmap_data, j, i, width, pixel);
1249     }
1250     }
1251 jsorg71 748 return the_bitmap;
1252     }
1253    
1254 jsorg71 791 /******************************************************************************/
1255 jsorg71 748 void ui_paint_bitmap(int x, int y, int cx, int cy, int width,
1256     int height, uint8 * data)
1257     {
1258     int i, j;
1259 jsorg71 791 int r, g, b, pixel;
1260 jsorg71 748
1261 jsorg71 780 if (g_server_bpp == 8)
1262     {
1263     for (i = 0; i < cy; i++)
1264     for (j = 0; j < cx; j++)
1265     if (i < height)
1266     if (j < width)
1267     set_pixel(x + j, y + i, data[i * width + j]);
1268     }
1269     else if (g_server_bpp == 16)
1270     {
1271     for (i = 0; i < cy; i++)
1272     for (j = 0; j < cx; j++)
1273     if (i < height)
1274     if (j < width)
1275     set_pixel(x + j, y + i, *(((uint16*)data) + (i * width + j)));
1276     }
1277 jsorg71 791 else if (g_server_bpp == 24)
1278     {
1279     for (i = 0; i < cy; i++)
1280     for (j = 0; j < cx; j++)
1281     if (i < height)
1282     if (j < width)
1283     {
1284     r = data[(i * width + j) * 3 + 0];
1285     g = data[(i * width + j) * 3 + 1];
1286     b = data[(i * width + j) * 3 + 2];
1287     pixel = (r << 16) | (g << 8) | b;
1288     set_pixel(x + j, y + i, pixel);
1289     }
1290     }
1291 jsorg71 748 redraw(x, y, cx, cy);
1292     }
1293    
1294 jsorg71 791 /******************************************************************************/
1295 jsorg71 748 void ui_destroy_bitmap(HBITMAP bmp)
1296     {
1297     struct bitmap* the_bitmap;
1298    
1299     the_bitmap = (struct bitmap*)bmp;
1300     if (the_bitmap != NULL)
1301     {
1302     if (the_bitmap->data != NULL)
1303     xfree(the_bitmap->data);
1304     xfree(the_bitmap);
1305     }
1306     }
1307    
1308 jsorg71 791 /******************************************************************************/
1309     bool is_pixel_on(uint8 * data, int x, int y, int width, int bpp)
1310 jsorg71 748 {
1311     int start, shift;
1312    
1313     if (bpp == 1)
1314     {
1315     width = (width + 7) / 8;
1316     start = (y * width) + x / 8;
1317     shift = x % 8;
1318     return (data[start] & (0x80 >> shift)) != 0;
1319     }
1320     else if (bpp == 8)
1321     return data[y * width + x] != 0;
1322     else
1323     return false;
1324     }
1325    
1326 jsorg71 791 /******************************************************************************/
1327     void set_pixel_on(uint8 * data, int x, int y, int width, int bpp, uint8 pixel)
1328 jsorg71 748 {
1329     if (bpp == 8)
1330     data[y * width + x] = pixel;
1331     }
1332    
1333 jsorg71 791 /******************************************************************************/
1334 jsorg71 748 HGLYPH ui_create_glyph(int width, int height, uint8 * data)
1335     {
1336     int i, j;
1337     uint8* glyph_data;
1338     struct bitmap* the_glyph;
1339    
1340     glyph_data = (uint8*)xmalloc(width * height);
1341     the_glyph = (struct bitmap*)xmalloc(sizeof(struct bitmap));
1342     the_glyph->w = width;
1343     the_glyph->h = height;
1344     the_glyph->data = glyph_data;
1345     memset(glyph_data, 0, width * height);
1346     for (i = 0; i < height; i++)
1347     for (j = 0; j < width; j++)
1348     if (is_pixel_on(data, j, i, width, 1))
1349     set_pixel_on(glyph_data, j, i, width, 8, 255);
1350     return the_glyph;
1351     }
1352    
1353 jsorg71 791 /******************************************************************************/
1354 jsorg71 748 void ui_destroy_glyph(HGLYPH glyph)
1355     {
1356     struct bitmap* the_glyph;
1357    
1358     the_glyph = (struct bitmap*)glyph;
1359     if (the_glyph != NULL)
1360     {
1361     if (the_glyph->data != NULL)
1362     xfree(the_glyph->data);
1363     xfree(the_glyph);
1364     }
1365     }
1366    
1367 jsorg71 791 /******************************************************************************/
1368 jsorg71 780 HCURSOR ui_create_cursor(uint32 x, uint32 y,
1369     int width, int height,
1370     uint8 * andmask, uint8 * xormask)
1371 jsorg71 748 {
1372     return (void*)1;
1373     }
1374    
1375 jsorg71 791 /******************************************************************************/
1376 jsorg71 748 void ui_set_cursor(HCURSOR /*cursor*/)
1377     {
1378     }
1379    
1380     /*****************************************************************************/
1381 jsorg71 780 uint16 ui_get_numlock_state(uint32 state)
1382 jsorg71 748 {
1383     return 0;
1384     }
1385    
1386     /*****************************************************************************/
1387     unsigned int read_keyboard_state(void)
1388     {
1389     return 0;
1390     }
1391    
1392     /*****************************************************************************/
1393     void ui_resize_window(void)
1394     {
1395     }
1396 jsorg71 791
1397     /******************************************************************************/
1398 jsorg71 748 void ui_destroy_cursor(HCURSOR /*cursor*/)
1399     {
1400     }
1401    
1402 jsorg71 791 /******************************************************************************/
1403 jsorg71 748 HCOLOURMAP ui_create_colourmap(COLOURMAP * colours)
1404     {
1405     int i;
1406     int x;
1407     uint8 r, g, b;
1408     i = 0;
1409     while (i < colours->ncolours && i < 256)
1410     {
1411     r = colours->colours[i].red;
1412     g = colours->colours[i].green;
1413     b = colours->colours[i].blue;
1414     x = (r << 16) | (g << 8) | b;
1415     g_CM->RGBColors[i] = x;
1416     i++;
1417     }
1418     g_CM->NumColors = colours->ncolours;
1419     return g_CM;
1420     }
1421    
1422 jsorg71 791 /******************************************************************************/
1423 jsorg71 780 void ui_set_colourmap(HCOLOURMAP map)
1424 jsorg71 748 {
1425     }
1426    
1427 jsorg71 791 /******************************************************************************/
1428 jsorg71 780 void ui_destroy_colourmap(HCOLOURMAP map)
1429 jsorg71 748 {
1430     }
1431    
1432 jsorg71 791 /******************************************************************************/
1433 jsorg71 748 void ui_begin_update(void)
1434     {
1435     }
1436    
1437 jsorg71 791 /******************************************************************************/
1438 jsorg71 748 void ui_end_update(void)
1439     {
1440     }
1441    
1442 jsorg71 791 /******************************************************************************/
1443 jsorg71 748 void ui_set_clip(int x, int y, int cx, int cy)
1444     {
1445     g_clipx = x;
1446     g_clipy = y;
1447     g_clipcx = cx;
1448     g_clipcy = cy;
1449     }
1450    
1451 jsorg71 791 /******************************************************************************/
1452 jsorg71 748 void ui_reset_clip(void)
1453     {
1454     g_clipx = 0;
1455     g_clipy = 0;
1456     g_clipcx = g_width;
1457     g_clipcy = g_height;
1458     }
1459    
1460 jsorg71 791 /******************************************************************************/
1461 jsorg71 748 void ui_bell(void)
1462     {
1463     g_App->beep();
1464     }
1465    
1466 jsorg71 791 /******************************************************************************/
1467 jsorg71 748 void ui_destblt(uint8 opcode, int x, int y, int cx, int cy)
1468     {
1469     int i, j;
1470    
1471     for (i = 0; i < cy; i++)
1472     for (j = 0; j < cx; j++)
1473     set_pixel(x + j, y + i, get_pixel(x + j, y + i), opcode);
1474     redraw(x, y, cx, cy);
1475     }
1476    
1477 jsorg71 791 /******************************************************************************/
1478 jsorg71 748 // does not repaint
1479     void fill_rect(int x, int y, int cx, int cy, int colour, int opcode = 0xc)
1480     {
1481     int i, j;
1482    
1483 jsorg71 780 if (x + cx > g_width)
1484     cx = g_width - x;
1485     if (y + cy > g_height)
1486     cy = g_height - y;
1487 jsorg71 748 for (i = 0; i < cy; i++)
1488     for (j = 0; j < cx; j++)
1489     set_pixel(x + j, y + i, colour, opcode);
1490     }
1491    
1492 jsorg71 791 /******************************************************************************/
1493 jsorg71 748 void ui_rect(int x, int y, int cx, int cy, int colour)
1494     {
1495     fill_rect(x, y, cx, cy, colour);
1496     redraw(x, y, cx, cy);
1497     }
1498    
1499 jsorg71 791 /******************************************************************************/
1500 jsorg71 748 void ui_patblt(uint8 opcode, int x, int y, int cx, int cy,
1501     BRUSH * brush, int bgcolour, int fgcolour)
1502     {
1503     int i, j;
1504     uint8 ipattern[8];
1505    
1506     switch (brush->style)
1507     {
1508     case 0:
1509     fill_rect(x, y, cx, cy, fgcolour, opcode);
1510     break;
1511     case 3:
1512     for (i = 0; i < 8; i++)
1513     ipattern[i] = ~brush->pattern[7 - i];
1514     for (i = 0; i < cy; i++)
1515     for (j = 0; j < cx; j++)
1516     if (is_pixel_on(ipattern, (x + j + brush->xorigin) % 8,
1517     (y + i + brush->yorigin) % 8, 8, 1))
1518     set_pixel(x + j, y + i, fgcolour, opcode);
1519     else
1520     set_pixel(x + j, y + i, bgcolour, opcode);
1521     break;
1522     }
1523     redraw(x, y, cx, cy);
1524     }
1525    
1526 jsorg71 791 /******************************************************************************/
1527 jsorg71 748 void ui_screenblt(uint8 opcode, int x, int y, int cx, int cy,
1528     int srcx, int srcy)
1529     {
1530     int i, j;
1531 jsorg71 791 uint8 * temp;
1532 jsorg71 748
1533 jsorg71 791 temp = (uint8*)xmalloc(cx * cy * (BPP + 1));
1534 jsorg71 780 if (g_server_bpp == 8)
1535     {
1536     for (i = 0; i < cy; i++)
1537     for (j = 0; j < cx; j++)
1538     temp[i * cx + j] = get_pixel(srcx + j, srcy + i);
1539     for (i = 0; i < cy; i++)
1540     for (j = 0; j < cx; j++)
1541     set_pixel(x + j, y + i, temp[i * cx + j], opcode);
1542     }
1543     else if (g_server_bpp == 16)
1544     {
1545     for (i = 0; i < cy; i++)
1546     for (j = 0; j < cx; j++)
1547     *(((uint16*)temp) + (i * cx + j)) = get_pixel(srcx + j, srcy + i);
1548     for (i = 0; i < cy; i++)
1549     for (j = 0; j < cx; j++)
1550     set_pixel(x + j, y + i, *(((uint16*)temp) + (i * cx + j)), opcode);
1551     }
1552 jsorg71 791 else if (g_server_bpp == 24)
1553     {
1554     for (i = 0; i < cy; i++)
1555     for (j = 0; j < cx; j++)
1556     *(((uint32*)temp) + (i * cx + j)) = get_pixel(srcx + j, srcy + i);
1557     for (i = 0; i < cy; i++)
1558     for (j = 0; j < cx; j++)
1559     set_pixel(x + j, y + i, *(((uint32*)temp) + (i * cx + j)), opcode);
1560     }
1561 jsorg71 748 xfree(temp);
1562     redraw(x, y, cx, cy);
1563     }
1564    
1565 jsorg71 791 /******************************************************************************/
1566 jsorg71 748 void ui_memblt(uint8 opcode, int x, int y, int cx, int cy,
1567     HBITMAP src, int srcx, int srcy)
1568     {
1569     int i, j;
1570 jsorg71 791 struct bitmap * the_bitmap;
1571 jsorg71 748
1572     the_bitmap = (struct bitmap*)src;
1573     if (the_bitmap == NULL)
1574     return;
1575 jsorg71 780 if (g_server_bpp == 8)
1576     {
1577     for (i = 0; i < cy; i++)
1578     for (j = 0; j < cx; j++)
1579     if ((i + srcy) < the_bitmap->h && (j + srcx) < the_bitmap->w)
1580     set_pixel(x + j, y + i,
1581     the_bitmap->data[(i + srcy) * the_bitmap->w + (j + srcx)],
1582     opcode);
1583     }
1584     else if (g_server_bpp == 16)
1585     {
1586     for (i = 0; i < cy; i++)
1587     for (j = 0; j < cx; j++)
1588     if ((i + srcy) < the_bitmap->h && (j + srcx) < the_bitmap->w)
1589     set_pixel(x + j, y + i,
1590     *(((uint16*)the_bitmap->data) + ((i + srcy) * the_bitmap->w + (j + srcx))),
1591     opcode);
1592     }
1593 jsorg71 791 else if (g_server_bpp == 24)
1594     {
1595     for (i = 0; i < cy; i++)
1596     for (j = 0; j < cx; j++)
1597     if ((i + srcy) < the_bitmap->h && (j + srcx) < the_bitmap->w)
1598     set_pixel(x + j, y + i,
1599     *(((uint32*)the_bitmap->data) + ((i + srcy) * the_bitmap->w + (j + srcx))),
1600     opcode);
1601     }
1602 jsorg71 748 redraw(x, y, cx, cy);
1603     }
1604    
1605 jsorg71 791 /******************************************************************************/
1606 jsorg71 748 // not used
1607     void ui_triblt(uint8 opcode, int x, int y, int cx, int cy,
1608     HBITMAP src, int srcx, int srcy, BRUSH * brush,
1609     int bgcolour, int fgcolour)
1610     {
1611     }
1612    
1613 jsorg71 791 /******************************************************************************/
1614     /* Bresenham's line drawing algorithm */
1615 jsorg71 748 void ui_line(uint8 opcode, int startx, int starty, int endx,
1616     int endy, PEN * pen)
1617     {
1618     int dx, dy, incx, incy, dpr, dpru, p, left, top, right, bottom;
1619    
1620     if (startx > endx)
1621     {
1622     dx = startx - endx;
1623     incx = -1;
1624     left = endx;
1625     right = startx;
1626     }
1627     else
1628     {
1629     dx = endx - startx;
1630     incx = 1;
1631     left = startx;
1632     right = endx;
1633     }
1634     if (starty > endy)
1635     {
1636     dy = starty - endy;
1637     incy = -1;
1638     top = endy;
1639     bottom = starty;
1640     }
1641     else
1642     {
1643     dy = endy - starty;
1644     incy = 1;
1645     top = starty;
1646     bottom = endy;
1647     }
1648     if (dx >= dy)
1649     {
1650     dpr = dy << 1;
1651     dpru = dpr - (dx << 1);
1652     p = dpr - dx;
1653     for (; dx >= 0; dx--)
1654     {
1655     set_pixel(startx, starty, pen->colour, opcode);
1656     if (p > 0)
1657     {
1658     startx += incx;
1659     starty += incy;
1660     p += dpru;
1661     }
1662     else
1663     {
1664     startx += incx;
1665     p += dpr;
1666     }
1667     }
1668     }
1669     else
1670     {
1671     dpr = dx << 1;
1672     dpru = dpr - (dy << 1);
1673     p = dpr - dy;
1674     for (; dy >= 0; dy--)
1675     {
1676     set_pixel(startx, starty, pen->colour, opcode);
1677     if (p > 0)
1678     {
1679     startx += incx;
1680     starty += incy;
1681     p += dpru;
1682     }
1683     else
1684     {
1685     starty += incy;
1686     p += dpr;
1687     }
1688     }
1689     }
1690     redraw(left, top, (right - left) + 1, (bottom - top) + 1);
1691     }
1692    
1693 jsorg71 791 /******************************************************************************/
1694 jsorg71 748 void draw_glyph (int x, int y, HGLYPH glyph, int fgcolour)
1695     {
1696 jsorg71 780 struct bitmap *the_glyph;
1697 jsorg71 748 int i, j;
1698    
1699     the_glyph = (struct bitmap*)glyph;
1700     if (the_glyph == NULL)
1701     return;
1702     for (i = 0; i < the_glyph->h; i++)
1703     for (j = 0; j < the_glyph->w; j++)
1704     if (is_pixel_on(the_glyph->data, j, i, the_glyph->w, 8))
1705     set_pixel(x + j, y + i, fgcolour);
1706     }
1707    
1708     #define DO_GLYPH(ttext,idx) \
1709     {\
1710     glyph = cache_get_font (font, ttext[idx]);\
1711     if (!(flags & TEXT2_IMPLICIT_X))\
1712     {\
1713     xyoffset = ttext[++idx];\
1714     if ((xyoffset & 0x80))\
1715     {\
1716     if (flags & TEXT2_VERTICAL) \
1717     y += ttext[idx+1] | (ttext[idx+2] << 8);\
1718     else\
1719     x += ttext[idx+1] | (ttext[idx+2] << 8);\
1720     idx += 2;\
1721     }\
1722     else\
1723     {\
1724     if (flags & TEXT2_VERTICAL) \
1725     y += xyoffset;\
1726     else\
1727     x += xyoffset;\
1728     }\
1729     }\
1730     if (glyph != NULL)\
1731     {\
1732     draw_glyph (x + glyph->offset, y + glyph->baseline, glyph->pixmap, fgcolour);\
1733     if (flags & TEXT2_IMPLICIT_X)\
1734     x += glyph->width;\
1735     }\
1736     }
1737    
1738 jsorg71 791 /******************************************************************************/
1739 jsorg71 748 void ui_draw_text(uint8 font, uint8 flags, int mixmode,
1740     int x, int y, int clipx, int clipy,
1741     int clipcx, int clipcy, int boxx,
1742     int boxy, int boxcx, int boxcy, int bgcolour,
1743     int fgcolour, uint8 * text, uint8 length)
1744     {
1745     FONTGLYPH *glyph;
1746     int i, j, xyoffset;
1747     DATABLOB *entry;
1748    
1749     if (boxcx > 1)
1750     fill_rect(boxx, boxy, boxcx, boxcy, bgcolour);
1751     else if (mixmode == MIX_OPAQUE)
1752     fill_rect(clipx, clipy, clipcx, clipcy, bgcolour);
1753    
1754     /* Paint text, character by character */
1755     for (i = 0; i < length;)
1756     {
1757     switch (text[i])
1758     {
1759     case 0xff:
1760     if (i + 2 < length)
1761     cache_put_text(text[i + 1], text, text[i + 2]);
1762     else
1763     {
1764     error("this shouldn't be happening\n");
1765     exit(1);
1766     }
1767     /* this will move pointer from start to first character after FF command */
1768     length -= i + 3;
1769     text = &(text[i + 3]);
1770     i = 0;
1771     break;
1772    
1773     case 0xfe:
1774     entry = cache_get_text(text[i + 1]);
1775     if (entry != NULL)
1776     {
1777     if ((((uint8 *) (entry->data))[1] == 0) && (!(flags & TEXT2_IMPLICIT_X)))
1778     {
1779     if (flags & TEXT2_VERTICAL)
1780     y += text[i + 2];
1781     else
1782     x += text[i + 2];
1783     }
1784     for (j = 0; j < entry->size; j++)
1785     DO_GLYPH(((uint8 *) (entry->data)), j);
1786     }
1787     if (i + 2 < length)
1788     i += 3;
1789     else
1790     i += 2;
1791     length -= i;
1792     /* this will move pointer from start to first character after FE command */
1793     text = &(text[i]);
1794     i = 0;
1795     break;
1796    
1797     default:
1798     DO_GLYPH(text, i);
1799     i++;
1800     break;
1801     }
1802     }
1803     if (boxcx > 1)
1804     redraw(boxx, boxy, boxcx, boxcy);
1805     else
1806     redraw(clipx, clipy, clipcx, clipcy);
1807     }
1808    
1809 jsorg71 791 /******************************************************************************/
1810 jsorg71 748 void ui_desktop_save(uint32 offset, int x, int y, int cx, int cy)
1811     {
1812 jsorg71 791 uint8 * data;
1813 jsorg71 748 int i, j;
1814 jsorg71 791 int Bpp;
1815 jsorg71 748
1816 jsorg71 791 Bpp = 4;
1817     switch (g_server_bpp)
1818     {
1819     case 8: Bpp = 1; break;
1820     case 15: Bpp = 2; break;
1821     case 16: Bpp = 2; break;
1822     }
1823     data = (uint8*)xmalloc(cx * cy * Bpp);
1824 jsorg71 780 if (g_server_bpp == 8)
1825     {
1826     for (i = 0; i < cy; i++)
1827     for (j = 0; j < cx; j++)
1828     data[i * cx + j] = get_pixel(x + j, y + i);
1829     }
1830     else if (g_server_bpp == 16)
1831     {
1832     for (i = 0; i < cy; i++)
1833     for (j = 0; j < cx; j++)
1834     *(((uint16*)data) + (i * cx + j)) = get_pixel(x + j, y + i);
1835     }
1836 jsorg71 791 else if (g_server_bpp == 24)
1837     {
1838     for (i = 0; i < cy; i++)
1839     for (j = 0; j < cx; j++)
1840     *(((uint32*)data) + (i * cx + j)) = get_pixel(x + j, y + i);
1841     }
1842     offset *= Bpp;
1843     cache_put_desktop(offset, cx, cy, cx * Bpp, Bpp, data);
1844 jsorg71 748 xfree(data);
1845     }
1846    
1847 jsorg71 791 /******************************************************************************/
1848 jsorg71 748 void ui_desktop_restore(uint32 offset, int x, int y, int cx, int cy)
1849     {
1850     uint8* data;
1851     int i, j;
1852 jsorg71 791 int Bpp;
1853 jsorg71 748
1854 jsorg71 791 Bpp = 4;
1855     switch (g_server_bpp)
1856     {
1857     case 8: Bpp = 1; break;
1858     case 15: Bpp = 2; break;
1859     case 16: Bpp = 2; break;
1860     }
1861     offset *= Bpp;
1862     data = cache_get_desktop(offset, cx, cy, Bpp);
1863 jsorg71 780 if (g_server_bpp == 8)
1864     {
1865     for (i = 0; i < cy; i++)
1866     for (j = 0; j < cx; j++)
1867     set_pixel(x + j, y + i, data[i * cx + j]);
1868     }
1869     else if (g_server_bpp == 16)
1870     {
1871     for (i = 0; i < cy; i++)
1872     for (j = 0; j < cx; j++)
1873     set_pixel(x + j, y + i, *(((uint16*)data) + (i * cx + j)));
1874     }
1875 jsorg71 791 else if (g_server_bpp == 24)
1876     {
1877     for (i = 0; i < cy; i++)
1878     for (j = 0; j < cx; j++)
1879     set_pixel(x + j, y + i, *(((uint32*)data) + (i * cx + j)));
1880     }
1881 jsorg71 748 redraw(x, y, cx, cy);
1882     }
1883    
1884     /*****************************************************************************/
1885 jsorg71 780 void* xrealloc(void *in_val, int size)
1886 jsorg71 748 {
1887 jsorg71 780 if (size < 1)
1888     size = 1;
1889 jsorg71 748 return realloc(in_val, size);
1890     }
1891    
1892     /*****************************************************************************/
1893     void* xmalloc(int size)
1894     {
1895     return malloc(size);
1896     }
1897    
1898     /*****************************************************************************/
1899 jsorg71 780 void xfree(void * in_val)
1900 jsorg71 748 {
1901 jsorg71 780 if (in_val != NULL)
1902     free(in_val);
1903 jsorg71 748 }
1904    
1905     /*****************************************************************************/
1906 jsorg71 780 void warning(char * format, ...)
1907 jsorg71 748 {
1908     va_list ap;
1909    
1910     fprintf(stderr, "WARNING: ");
1911     va_start(ap, format);
1912     vfprintf(stderr, format, ap);
1913     va_end(ap);
1914     }
1915    
1916     /*****************************************************************************/
1917 jsorg71 780 void unimpl(char * format, ...)
1918 jsorg71 748 {
1919     va_list ap;
1920    
1921     fprintf(stderr, "NOT IMPLEMENTED: ");
1922     va_start(ap, format);
1923     vfprintf(stderr, format, ap);
1924     va_end(ap);
1925     }
1926    
1927     /*****************************************************************************/
1928 jsorg71 780 void error(char * format, ...)
1929 jsorg71 748 {
1930     va_list ap;
1931    
1932     fprintf(stderr, "ERROR: ");
1933     va_start(ap, format);
1934     vfprintf(stderr, format, ap);
1935     va_end(ap);
1936     }
1937    
1938     /*****************************************************************************/
1939     BOOL rd_pstcache_mkdir(void)
1940     {
1941     return 0;
1942     }
1943    
1944     /*****************************************************************************/
1945 jsorg71 780 int rd_open_file(char * filename)
1946 jsorg71 748 {
1947     return 0;
1948     }
1949    
1950     /*****************************************************************************/
1951     void rd_close_file(int fd)
1952     {
1953     return;
1954     }
1955    
1956     /*****************************************************************************/
1957 jsorg71 780 int rd_read_file(int fd, void * ptr, int len)
1958 jsorg71 748 {
1959     return 0;
1960     }
1961    
1962     /*****************************************************************************/
1963 jsorg71 780 int rd_write_file(int fd, void * ptr, int len)
1964 jsorg71 748 {
1965     return 0;
1966     }
1967    
1968     /*****************************************************************************/
1969     int rd_lseek_file(int fd, int offset)
1970     {
1971     return 0;
1972     }
1973    
1974     /*****************************************************************************/
1975     BOOL rd_lock_file(int fd, int start, int len)
1976     {
1977     return False;
1978     }
1979    
1980     /*****************************************************************************/
1981 jsorg71 780 int load_licence(uint8 ** data)
1982 jsorg71 748 {
1983 jsorg71 780 return 0;
1984 jsorg71 748 }
1985    
1986     /*****************************************************************************/
1987 jsorg71 780 void save_licence(uint8 * data, int length)
1988 jsorg71 748 {
1989 jsorg71 780 }
1990    
1991     /*****************************************************************************/
1992     void generate_random(uint8 * random)
1993     {
1994 jsorg71 748 QFile File("/dev/random");
1995     File.open(IO_ReadOnly);
1996     if (File.readBlock((char*)random, 32) == 32)
1997     return;
1998     warning("no /dev/random\n");
1999     memcpy(random, "12345678901234567890123456789012", 32);
2000     }
2001    
2002     /*****************************************************************************/
2003 jsorg71 780 /* produce a hex dump */
2004     void hexdump(uint8 * p, uint32 len)
2005 jsorg71 748 {
2006 jsorg71 780 uint8 * line = p;
2007     int i, thisline;
2008     uint32 offset = 0;
2009    
2010     while (offset < len)
2011     {
2012     printf("%04x ", offset);
2013     thisline = len - offset;
2014     if (thisline > 16)
2015     thisline = 16;
2016    
2017     for (i = 0; i < thisline; i++)
2018     printf("%02x ", line[i]);
2019    
2020     for (; i < 16; i++)
2021     printf(" ");
2022    
2023     for (i = 0; i < thisline; i++)
2024     printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
2025    
2026     printf("\n");
2027     offset += thisline;
2028     line += thisline;
2029     }
2030     }
2031    
2032     /*****************************************************************************/
2033     void get_username_and_hostname(void)
2034     {
2035     char fullhostname[64];
2036     char* p;
2037     struct passwd* pw;
2038    
2039     STRNCPY(g_username, "unknown", sizeof(g_username));
2040     STRNCPY(g_hostname, "unknown", sizeof(g_hostname));
2041     pw = getpwuid(getuid());
2042     if (pw != NULL && pw->pw_name != NULL)
2043     {
2044     STRNCPY(g_username, pw->pw_name, sizeof(g_username));
2045     }
2046     if (gethostname(fullhostname, sizeof(fullhostname)) != -1)
2047     {
2048     p = strchr(fullhostname, '.');
2049     if (p != NULL)
2050     *p = 0;
2051     STRNCPY(g_hostname, fullhostname, sizeof(g_hostname));
2052     }
2053     }
2054    
2055     /*****************************************************************************/
2056     void out_params(void)
2057     {
2058     fprintf(stderr, "qterdesktop: A Remote Desktop Protocol client.\n");
2059     fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2004 Matt Chapman.\n");
2060     fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
2061     fprintf(stderr, "Usage: qterdesktop [options] server\n");
2062     fprintf(stderr, " -g: desktop geometry (WxH)\n");
2063     fprintf(stderr, " -4: use RDP version 4\n");
2064     fprintf(stderr, " -5: use RDP version 5 (default)\n");
2065     fprintf(stderr, " -t: tcp port)\n");
2066     fprintf(stderr, " -a: connection colour depth\n");
2067    
2068     fprintf(stderr, " -u: user name\n");
2069     fprintf(stderr, " -d: domain\n");
2070     fprintf(stderr, " -s: shell\n");
2071     fprintf(stderr, " -c: working directory\n");
2072     fprintf(stderr, " -p: password (- to prompt)\n");
2073     fprintf(stderr, " -n: client hostname\n");
2074    
2075     fprintf(stderr, "\n");
2076     }
2077    
2078     /*****************************************************************************/
2079     int parse_parameters(int in_argc, char ** in_argv)
2080     {
2081     int i;
2082     char * p;
2083    
2084     for (i = 1; i < in_argc; i++)
2085     {
2086     strcpy(g_server, in_argv[i]);
2087     if (strcmp(in_argv[i], "-h") == 0)
2088     {
2089     out_params();
2090     return 0;
2091     }
2092     else if (strcmp(in_argv[i], "-g") == 0)
2093     {
2094     g_width = strtol(in_argv[i + 1], &p, 10);
2095     if (g_width <= 0)
2096     {
2097     error("invalid geometry\n");
2098     return 0;
2099     }
2100     if (*p == 'x')
2101     g_height = strtol(p + 1, NULL, 10);
2102     if (g_height <= 0)
2103     {
2104     error("invalid geometry\n");
2105     return 0;
2106     }
2107     g_width = (g_width + 3) & ~3;
2108     }
2109     else if (strcmp(in_argv[i], "-4") == 0)
2110     g_use_rdp5 = 0;
2111     else if (strcmp(in_argv[i], "-5") == 0)
2112     g_use_rdp5 = 1;
2113     else if (strcmp(in_argv[i], "-a") == 0)
2114     {
2115     g_server_bpp = strtol(in_argv[i + 1], &p, 10);
2116     if (g_server_bpp != 8 && g_server_bpp != 15 &&
2117     g_server_bpp != 16 && g_server_bpp != 24)
2118     {
2119     error("invalid bpp\n");
2120     return 0;
2121     }
2122     }
2123     else if (strcmp(in_argv[i], "-t") == 0)
2124     g_tcp_port_rdp = strtol(in_argv[i + 1], &p, 10);
2125    
2126     else if (strcmp(in_argv[i], "-u") == 0)
2127     {
2128     STRNCPY(g_username, in_argv[i + 1], sizeof(g_username));
2129     }
2130     else if (strcmp(in_argv[i], "-d") == 0)
2131     {
2132     STRNCPY(g_domain, in_argv[i + 1], sizeof(g_domain));
2133     }
2134     else if (strcmp(in_argv[i], "-s") == 0)
2135     {
2136     STRNCPY(g_shell, in_argv[i + 1], sizeof(g_shell));
2137     }
2138     else if (strcmp(in_argv[i], "-c") == 0)
2139     {
2140     STRNCPY(g_directory, in_argv[i + 1], sizeof(g_directory));
2141     }
2142     else if (strcmp(in_argv[i], "-p") == 0)
2143     {
2144     STRNCPY(g_password, in_argv[i + 1], sizeof(g_password));
2145     g_flags |= RDP_LOGON_AUTO;
2146     }
2147     else if (strcmp(in_argv[i], "-n") == 0)
2148     {
2149     STRNCPY(g_hostname, in_argv[i + 1], sizeof(g_hostname));
2150     }
2151     }
2152     return 1;
2153     }
2154    
2155 jsorg71 791 /******************************************************************************/
2156 jsorg71 780 int param_connect(void)
2157     {
2158     QWidget * Desktop;
2159     int dw, dh;
2160    
2161     #ifdef WITH_RDPSND
2162     rdpsnd_init();
2163     #endif
2164     if (rdp_connect(g_server, g_flags, g_domain, g_password, g_shell,
2165     g_directory))
2166     {
2167     g_BS = (uint8*)xmalloc(g_width * g_height * 4);
2168     memset(g_BS, 0, g_width * g_height * 4);
2169     g_clipx = 0;
2170     g_clipy = 0;
2171     g_clipcx = g_width;
2172     g_clipcy = g_height;
2173     g_CM = (QColorMap*)xmalloc(sizeof(struct QColorMap));
2174     memset(g_CM, 0, sizeof(struct QColorMap));
2175     g_CM->NumColors = 256;
2176     g_MW = new QMyMainWindow();
2177     g_MW->resize(g_width, g_height);
2178     g_MW->show();
2179     g_SV->addChild(g_MW);
2180     g_MW->setMouseTracking(true);
2181     g_SocketNotifier = new QSocketNotifier(g_global_sock,
2182     QSocketNotifier::Read,
2183     g_MW);
2184     g_MW->connect(g_SocketNotifier, SIGNAL(activated(int)), g_MW,
2185     SLOT(dataReceived()));
2186     if (g_fullscreen)
2187     {
2188     Desktop = g_App->desktop();
2189     dw = Desktop->width();
2190     dh = Desktop->height();
2191     if (dw == g_width && dh == g_height)
2192     g_MW->resize(g_width - 4, g_height - 4);
2193     g_SV->showFullScreen();
2194     }
2195 jsorg71 791 g_MW->setCursor((int)10); /* Qt::BlankCursor */
2196 jsorg71 780 g_App->exec();
2197     }
2198 jsorg71 748 return 0;
2199     }
2200    
2201 jsorg71 791 /******************************************************************************/
2202 jsorg71 780 int main(int argc, char ** argv)
2203 jsorg71 748 {
2204     #ifdef SHARP
2205     g_App = new QPEApplication(argc, argv);
2206     #else
2207     g_App = new QApplication(argc, argv, QApplication::GuiServer);
2208     #endif
2209     g_SV = new QMyScrollView();
2210     g_App->setMainWidget(g_SV);
2211     g_SV->showMaximized();
2212 jsorg71 780 if (argc > 1)
2213     {
2214     get_username_and_hostname();
2215     if (parse_parameters(argc, argv))
2216     param_connect();
2217     }
2218     else
2219     {
2220     g_SV->timer_id = g_SV->startTimer(1000); /* one sec delay, then dialog */
2221 jsorg71 791 g_MW->setCursor((int)10); /* Qt::BlankCursor */
2222 jsorg71 780 g_App->exec();
2223     }
2224 jsorg71 748 delete g_SV;
2225     delete g_App;
2226 jsorg71 780 xfree(g_CM);
2227     xfree(g_BS);
2228 jsorg71 748 return 0;
2229     }

  ViewVC Help
Powered by ViewVC 1.1.26